花了一些时间弄清楚如何插入图像。
insertImage = (editorState, base64) => { const contentState = editorState.getCurrentContent(); const contentStateWithEntity = contentState.createEntity( 'image', 'IMMUTABLE', { src: base64 }, ); const entityKey = contentStateWithEntity.getLastCreatedEntityKey(); const newEditorState = EditorState.set( editorState, { currentContent: contentStateWithEntity }, ); return AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, ' '); };那你可以用
const base64 = 'aValidbase64String';const newEditorState = this.insertImage(this.state.editorState, base64);this.setState({ editorState: newEditorState });对于渲染图像,可以使用Draft.js图像插件。
现场演示:
[presandbox](https://presandbox.io/s/50pp3xlv24)
该演示将插入一个Twitter徽标图像。
如果要从本地文件插入图像,则可以尝试使用
FileReaderAPI来获取该base64。
对于如何获取base64,很简单,请检查
现场演示:
[jsbin](http://jsbin.com/xupisiraya/2/edit?js,console,output)
现在将它们放在一起,您可以从本地文件上传图像!



