0

我正在尝试从 Draft-js 编辑器外部删除图像,但它始终插入到编辑器中光标/选择的最后位置(或者如果未设置光标/选择,则在末尾)。

这是我的环绕draft-js-drag-n-drop-plugin

const droppableBlockDndPlugin = {
  ...blockDndPlugin,
  handleDrop: (
    selection,
    dataTransfer,
    isInternal,
    {getEditorState, setEditorState}
  ) => {
    const editorState = getEditorState();
    const raw = dataTransfer.data.getData('text');
    const data = raw ? raw.split(IMAGE_BLOCK_TYPE_SEPARATOR) : [];
    if (data.length > 1 && data[0] === IMAGE_BLOCK_TYPE_PURE) {
      const url = data[1];
      if (url) {
        const newState = imagePlugin.addImage(editorState, url);
        setEditorState(newState);
      }
    }

    return blockDndPlugin.handleDrop(selection, dataTransfer, isInternal, {
      getEditorState,
      setEditorState
    });
  }
};

基本上我只是在handleDrop使用插入图像的基础发生之前做额外的逻辑imagePlugin.addImage。有没有办法将图像拖放到拖动位置?

4

1 回答 1

0

实际上这是非常明显的解决方案 - 你应该只使用传递selection并使用它创建新状态,然后将图像添加到该新状态:

const newState = imagePlugin.addImage(EditorState.forceSelection(editorState, selection), url);
setEditorState(newState);
于 2019-11-13T15:44:57.960 回答