栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Draft.js中插入/上传图像(更新实体和块)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Draft.js中插入/上传图像(更新实体和块)?

花了一些时间弄清楚如何插入图像。

  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徽标图像。


如果要从本地文件插入图像,则可以尝试使用

FileReader
API来获取该base64。

对于如何获取base64,很简单,请检查

现场演示:

[jsbin](http://jsbin.com/xupisiraya/2/edit?js,console,output)

现在将它们放在一起,您可以从本地文件上传图像!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号