首页 > 解决方案 > 使用 slatejs 在编辑器中的光标处插入图像

问题描述

我正在尝试使用 reactjs 和 slate.js 实现富文本编辑器。到目前为止,我能够使大多数功能正常工作,但是无法将图像添加到光标处的文档中不起作用(但是,在文档开头插入图像是有效的)。

当我试图将它插入任何其他点时,我都会收到错误。在渲染部分,即执行编辑器的 onchange 方法。

    const target = getEventRange(e, this.state.EditorComp.state.value)
change = this.state.EditorComp.state.value.change().call(this.insertImage, filelocation,target)
this.state.EditorComp.onChange(change);

slate-react.es.js:1229 Uncaught Error: Unable to find a DOM node for "51". This is often because of forgetting to add `props.attributes` to a custom component.
at findDOMNode$1 (slate-react.es.js:1229)
at findDOMPoint (slate-react.es.js:1247)
at findDOMRange (slate-react.es.js:1290)

我的代码基于链接https://github.com/ianstormtaylor/slate/blob/master/examples/images/index.js

请帮忙。

标签: reactjsimageinsertionslatejs

解决方案


你有为你的编辑器定义的模式吗?isVoid在为设置为 true的图像添加架构之前,我遇到了同样的错误。您的架构中至少需要以下内容:

const schema = {
  blocks: {
    image: {
      isVoid: true
    }
  }
};

推荐阅读