Drag and Drop
Easily reorganize content within your document using drag and drop.
How to use:
- Hover over the left side of a block to see the drag handle (six dots).
- Click and hold the handle, then drag the block to a new location.
- Release to drop the block in its new position.
Try it out! Drag these items to reorder them:
- First item
- Second item
- Third item
1
100%
Files
'use client';
import * as React from 'react';
import { Plate, usePlateEditor } from 'platejs/react';
import { EditorKit } from '@/components/editor/editor-kit';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { DEMO_VALUES } from './values/demo-values';
export default function Demo({ id }: { id: string }) {
const editor = usePlateEditor({
plugins: EditorKit,
value: DEMO_VALUES[id],
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
Implements draggable functionality for editor blocks, including drag handles and drop indicators.
dnd-demo


功能特性
- 通过拖拽区块实现内容移动和插入
- 拖拽至视窗边缘时自动滚动
- 支持文件拖放插入媒体区块
- 可视化拖放指示器和拖拽手柄
套件使用
安装
最快捷的方式是使用 DndKit 套件,它包含预配置的 DndPlugin、React DnD 设置以及 BlockDraggable UI组件。
'use client';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { DndPlugin } from '@platejs/dnd';
import { PlaceholderPlugin } from '@platejs/media/react';
import { BlockDraggable } from '@/components/ui/block-draggable';
export const DndKit = [
DndPlugin.configure({
options: {
enableScroller: true,
onDropFiles: ({ dragItem, editor, target }) => {
editor
.getTransforms(PlaceholderPlugin)
.insert.media(dragItem.files, { at: target, nextBlock: false });
},
},
render: {
aboveNodes: BlockDraggable,
aboveSlate: ({ children }) => (
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
),
},
}),
];
BlockDraggable: 为区块渲染拖拽手柄和放置指示器
添加套件
import { createPlateEditor } from 'platejs/react';
import { DndKit } from '@/components/editor/plugins/dnd-kit';
const editor = createPlateEditor({
plugins: [
// ...其他插件
...DndKit,
],
});手动配置
安装依赖
pnpm add @platejs/dnd react-dnd react-dnd-html5-backend
添加插件
import { DndPlugin } from '@platejs/dnd';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...其他插件
DndPlugin,
],
});配置插件
通过可拖拽组件和DnD provider配置拖放功能:
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { DndPlugin } from '@platejs/dnd';
DndPlugin.configure({
render: {
aboveSlate: ({ children }) => (
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
),
},
});render.aboveNodes: 分配BlockDraggable组件在区块上方渲染拖拽手柄render.aboveSlate: 用DndProvider包裹编辑器。如果React树中已有DndProvider可移除此配置
高级配置
添加自动滚动和文件拖放处理:
import { DndPlugin } from '@platejs/dnd';
import { PlaceholderPlugin } from '@platejs/media/react';
DndPlugin.configure({
options: {
enableScroller: true,
onDropFiles: ({ dragItem, editor, target }) => {
editor
.getTransforms(PlaceholderPlugin)
.insert.media(dragItem.files, { at: target, nextBlock: false });
},
},
render: {
aboveNodes: BlockDraggable,
aboveSlate: ({ children }) => (
<DndProvider backend={HTML5Backend}>{children}</DndProvider>
),
},
});enableScroller: 启用拖拽至视窗边缘时的自动滚动onDropFiles: 处理文件拖放,在目标位置插入媒体区块
插件
DndPlugin
实现编辑器内拖拽功能的插件。
API
focusBlockStartById
通过ID选中区块起始位置并聚焦编辑器。
getBlocksWithId
返回带有ID的区块数组。
selectBlockById
通过ID选中区块。
钩子
useDndNode
组合了 useDragNode 和 useDropNode 的自定义钩子,用于启用编辑器节点的拖拽功能。提供默认的拖拽预览效果,可自定义或禁用。
useDragNode
使用 react-dnd 的 useDrag 钩子实现节点拖拽功能的自定义钩子。
useDraggable
使元素具备可拖拽行为的自定义钩子,提供可自定义或禁用的拖拽预览效果。
useDropNode
使用 react-dnd 的 useDrop 钩子实现节点放置功能的自定义钩子。
useDropLine
返回元素的当前放置线状态。