首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从超文本标记语言创建的DraftJS编辑器不起作用

从超文本标记语言创建的DraftJS编辑器不起作用
EN

Stack Overflow用户
提问于 2018-06-04 19:43:31
回答 2查看 1.7K关注 0票数 2

编辑器正在启动,就好像它有空字符串!

我试过这个:

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)

什么都不管用!

EN

回答 2

Stack Overflow用户

发布于 2018-06-06 05:47:00

您没有显示您正在对内容做什么,但以下内容(即您的第一个选项)可以正常工作

<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)} />
);
票数 5
EN

Stack Overflow用户

发布于 2020-01-30 01:38:19

如果您感兴趣,我已经通过函数组件将HTML语言引入DraftJS编辑器。下面是构建在@Grantnz答案之上的代码。

import React, { useState } from 'react'
import { Editor, EditorState, RichUtils, ContentState, convertFromHTML } from 'draft-js'

const DraftJS = () => {
    // assert hard-coded HTML string, convert it, bring it in as state
    const blocksFromHTML = convertFromHTML(
        "<p>Hey this <strong>editor</strong> rocks </p>"
    );

    const content = ContentState.createFromBlockArray(
        blocksFromHTML.contentBlocks,
        blocksFromHTML.entityMap
    );

    const [editorState, setEditorState] = useState(EditorState.createWithContent(content));

    // handle changes to the Editor component
    const onChange = editorState => {
      setEditorState(editorState);
    }

    // build the Editor component
    return (
      <div>
        <h4 className="green-text">From DraftJS.js</h4>
        <Editor
          editorState={editorState}
          onChange={onChange}
        />
      </div>
    );
}

export default DraftJS;

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50679805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档