我需要一个类似的功能,所以我使用 reactjs 完成了以下操作。
创建的 ref 将保存当前索引
const currentBlockIndexRef = useRef('')
如何获取当前索引。
要获取当前索引,使用 api。当您单击用户想要添加内容的编辑器时,可以使用 Onchange(api) 将包含与用户想要添加内容的位置对应的当前块索引。如下图。
const editor = new EditorJS({
holder: editorId,
logLevel: "ERROR",
data: initData,
onReady: () => {
ejInstance.current = editor;
},
onChange: function(api, block) {
console.log('api', api.blocks.getCurrentBlockIndex())
currentBlockIndexRef.current= api.blocks.getCurrentBlockIndex() // update ref with the current index
editor.save().then((data) => {
setEditorData({ ...editorData,data,});
})
},
autofocus: true,
readOnly: false,
tools: editorTools
});
editorJs = editor
};
```
Then you can insert the block at the index
editorJs.blocks.insert('image', {...data.data},'',currentBlockIndexRef.current) // update the block