【问题标题】:How to set initial editor state in react draft wysiwyg如何在反应草稿所见即所得中设置初始编辑器状态
【发布时间】:2021-10-01 19:05:58
【问题描述】:

在将数据从 react-draft-wysiwyg 文本编辑器存储到服务器端时,我使用此函数将其转换为 JSON。

content = JSON.stringify(
  convertToRaw(state.editorState.getCurrentContent()),
);

当我检索数据时,它在 JSON 中是这样的

"{\"blocks\":[{\"key\":\"b4n4u\",\"text\":\"Hi \",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[],\"data\":{}},{\"key\":\"bp5jr\",\"text\":\"fgfgfgfgfgfg\",\"type\":\"header-two\",\"depth\":0,\"inlineStyleRanges\":[{\"offset\":0,\"length\":12,\"style\":\"fontsize-8\"}],\"entityRanges\":[],\"data\":{}},{\"key\":\"81j28\",\"text\":\"\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[],\"data\":{}},{\"key\":\"1nljr\",\"text\":\"dfdfdf\",\"type\":\"unordered-list-item\",\"depth\":0,\"inlineStyleRanges\":[{\"offset\":0,\"length\":6,\"style\":\"fontsize-8\"}],\"entityRanges\":[],\"data\":{}},{\"key\":\"b629m\",\"text\":\"dfdfdf\",\"type\":\"unordered-list-item\",\"depth\":0,\"inlineStyleRanges\":[{\"offset\":0,\"length\":6,\"style\":\"fontsize-8\"}],\"entityRanges\":[],\"data\":{}},{\"key\":\"3afsd\",\"text\":\"dfdfdf\",\"type\":\"unordered-list-item\",\"depth\":0,\"inlineStyleRanges\":[{\"offset\":0,\"length\":6,\"style\":\"fontsize-8\"}],\"entityRanges\":[],\"data\":{}},{\"key\":\"1rq6j\",\"text\":\"dfdf\",\"type\":\"unordered-list-item\",\"depth\":0,\"inlineStyleRanges\":[{\"offset\":0,\"length\":4,\"style\":\"fontsize-8\"}],\"entityRanges\":[],\"data\":{}},{\"key\":\"536hu\",\"text\":\"df\",\"type\":\"unordered-list-item\",\"depth\":0,\"inlineStyleRanges\":[{\"offset\":0,\"length\":2,\"style\":\"fontsize-8\"}],\"entityRanges\":[],\"data\":{}}],\"entityMap\":{}}",

现在,我希望这是另一个页面中编辑器的初始状态。

所以我尝试使用文档 https://jpuri.github.io/react-draft-wysiwyg/#/docs 中提到的 defaultEditorState

<Editor
        defaultEditorState={convertFromRaw(JSON.parse(post.content))}
        editorState={state.editorState}
        wrapperClassName='blogpost-text-editor-wrapper'
        editorClassName='blogpost-text-editor'
        toolbarClassName='blogpost-text-editor-toolbar'
        onEditorStateChange={onChange}
      />

但它不起作用。 Draft.js 文档中的 EditorState 页面丢失了,所以我不知道如何创建 EditorState 对象。请帮我解决这个问题。

我们将不胜感激。

【问题讨论】:

    标签: reactjs react-draft-wysiwyg


    【解决方案1】:

    我找到了一种方法。假设 post.content 有 ContentState,我们可以像这样初始化 EditorState。

      const [state, setState] = useState({
        editorState: EditorState.createWithContent(
          convertFromRaw(JSON.parse(post.content)),
        ),
      });
    

    【讨论】:

      【解决方案2】:
      import htmlToDraft from 'html-to-draftjs';
          
      const contentBlocks = htmlToDraft(initialState)
      const contentState = ContentState.createFromBlockArray(contentBlocks)
      const rawHtml = convertToRaw(contentState)
          
          
      <Editor contentState={rawHtml} />
      

      使用htmlToDraft保持HTML的样式/css; convertFromRaw 会导致颜色丢失。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-07
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        • 2018-06-19
        相关资源
        最近更新 更多