javascript - 从 HTML 创建的 DraftJS 编辑器不起作用

标签 javascript reactjs editor 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)} />
);

关于javascript - 从 HTML 创建的 DraftJS 编辑器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679805/

相关文章:

javascript - 如何在打开连接时从 Javascript 发送一些附加数据,例如用户名或时间?

Javascript:从原型(prototype)方法调用私有(private)方法

javascript - ReactJS 在渲染状态下循环

reactjs - React 无法设置状态

linux - sed 命令组合了两个相互依赖的命令

Asp.net 和 JavaScript 弹出窗口

javascript - Safari ios 上录音为空

javascript - 在reactjs中渲染大状态数据

css - Sublime Text : Hide all code and show only comments

Xcode 在代码编辑器中显示 Assets 图标文件名