javascript - 从 HTML 创建的 DraftJS 编辑器不起作用
问题描述
编辑器正在启动,就好像它有空字符串一样!
我试过这个:
EditorState.createWithContent(ContentState.createFromText('Hello'))
和这个:
const html = '<p>Hey this <strong>editor</strong> rocks </p>';
const contentBlock = htmlToDraft(html);
if (contentBlock) {
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
const editorState = EditorState.createWithContent(contentState);
this.state = {
editorState,
};
}
和这个:
import htmlToDraft from 'html-to-draftjs'
htmlToDraft(text)
没有任何效果!
解决方案
您没有显示您对内容所做的操作,但以下(即您的第一个选项)可以正常工作
<Editor
editorState={EditorState.createWithContent(
ContentState.createFromText("Hello")
)}
/>
就像这样:
import {
Editor,
EditorState,
ContentState,
convertFromHTML
} from "draft-js";
...
const blocksFromHTML = convertFromHTML(
"<p>Hey this <strong>editor</strong> rocks </p>"
);
const content = ContentState.createFromBlockArray(
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap
);
return (
<Editor editorState={EditorState.createWithContent(content)} />
);
推荐阅读
- jquery - checkall 在分页的第二页中不起作用
- azure - 从 API 调用 Azure DevOps 以启动 Terraform 文件
- sql - 包含默认空数组的正则表达式替换
- kubernetes - 关于 Kubernetes pod 分配给节点的概念问题
- r - 奇怪的 `tinytex` 行为 - `~/.TinyTex` 一直消失?
- react-native - ExpendableCalendar 在 iOS 上不消耗,反应原生日历
- r - Bookdown 章节格式:每段不同的页面
- amazon-web-services - kinesis 无法将数据发送到 redshift:Redshift.ConnectionFailed
- linq - ASP.NET Core 在使用 LINQ GroupBy 时尝试将对象强制转换为 System.Int64
- c# - 两个字典的最佳合并(但首先是异步输出问题)