块编辑器开发文档

💡 云策文档标注

概述

Draggable 是一个 WordPress 组件,用于设置跨浏览器(包括 IE)的可自定义拖拽图像和拖拽事件的传输数据。它将拖拽手柄与要拖拽的元素解耦,通过包装成为拖拽手柄的组件并提供要拖拽元素的 DOM ID 来使用。

关键要点

  • Draggable 组件处理拖拽图像和传输数据的逻辑,但不创建实际可拖拽的 DOM 元素。
  • 拖拽手柄需要声明 draggable="true" 属性,并将 Draggable 的 onDraggableStart 和 onDraggableEnd 事件处理器分别绑定到自身的 onDragStart 和 onDragEnd。
  • 组件接受多个 props,包括 elementId(必需)、transferData(必需)、onDragStart、onDragEnd、onDragOver 和 appendToOwnerDocument。

代码示例

import { Draggable, Panel, PanelBody } from '@wordpress/components';
import { Icon, more } from '@wordpress/icons';

const MyDraggable = () => (
    <div id="draggable-panel">
        <Panel header="Draggable panel">
            <PanelBody>
                <Draggable elementId="draggable-panel" transferData={ {} }>
                    { ( { onDraggableStart, onDraggableEnd } ) => (
                        <div
                            className="example-drag-handle"
                            draggable
                            onDragStart={ onDraggableStart }
                            onDragEnd={ onDraggableEnd }
                        >
                            <Icon icon={ more } />
                        </div>
                    ) }
                </Draggable>
            </PanelBody>
        </Panel>
    </div>
);

注意事项

如果需要同时调用自定义的 dragstart/dragend 事件处理器,可以将它们传递给 Draggable 组件,Draggable 会在自身逻辑后调用它们。


📄 原文内容

Draggable is a Component that provides a way to set up a cross-browser (including IE) customizable drag image and the transfer data for the drag event. It decouples the drag handle and the element to drag: use it by wrapping the component that will become the drag handle and providing the DOM ID of the element to drag.

Note that the drag handle needs to declare the draggable="true" property and bind the Draggables onDraggableStart and onDraggableEnd event handlers to its own onDragStart and onDragEnd respectively. Draggable takes care of the logic to setup the drag image and the transfer data, but is not concerned with creating an actual DOM element that is draggable.

Props

The component accepts the following props:

appendToOwnerDocument: boolean

Whether to append the cloned element to the ownerDocument body. By default, elements sourced by id are appended to the element’s wrapper.

  • Required: No
  • Default: false

elementId: string

The HTML id of the element to clone on drag.

  • Required: Yes

onDragEnd: ( event: DragEvent ) => void

A function called when dragging ends. This callback receives the event object from the dragend event as its first parameter.

  • Required: No
  • Default: noop

onDragOver: ( event: DragEvent ) => void

A function called when the element being dragged is dragged over a valid drop target. This callback receives the event object from the dragover event as its first parameter.

  • Required: No
  • Default: noop

onDragStart: ( event: DragEvent ) => void

A function called when dragging starts. This callback receives the event object from the dragstart event as its first parameter.

  • Required: No
  • Default: noop

transferData: unknown

Arbitrary data object attached to the drag and drop event.

  • Required: Yes

Usage

import { Draggable, Panel, PanelBody } from '@wordpress/components';
import { Icon, more } from '@wordpress/icons';

const MyDraggable = () => (
    <div id="draggable-panel">
        <Panel header="Draggable panel">
            <PanelBody>
                <Draggable elementId="draggable-panel" transferData={ {} }>
                    { ( { onDraggableStart, onDraggableEnd } ) => (
                        <div
                            className="example-drag-handle"
                            draggable
                            onDragStart={ onDraggableStart }
                            onDragEnd={ onDraggableEnd }
                        >
                            <Icon icon={ more } />
                        </div>
                    ) }
                </Draggable>
            </PanelBody>
        </Panel>
    </div>
);

In case you want to call your own dragstart / dragend event handlers as well, you can pass them to Draggable and it’ll take care of calling them after their own:

import { Draggable, Panel, PanelBody } from '@wordpress/components';
import { Icon, more } from '@wordpress/icons';

const MyDraggable = ( { onDragStart, onDragEnd } ) => (
    <div id="draggable-panel">
        <Panel header="Draggable panel">
            <PanelBody>
                <Draggable
                    elementId="draggable-panel"
                    transferData={ {} }
                    onDragStart={ onDragStart }
                    onDragEnd={ onDragEnd }
                >
                    { ( { onDraggableStart, onDraggableEnd } ) => (
                        <div
                            className="example-drag-handle"
                            draggable
                            onDragStart={ onDraggableStart }
                            onDragEnd={ onDraggableEnd }
                        >
                            <Icon icon={ more } />
                        </div>
                    ) }
                </Draggable>
            </PanelBody>
        </Panel>
    </div>
);