2

我正在对 draft.js 使用提及(例如 @yourname)并将其发送到数据库以保存并获取它以在网页上呈现,但事情没有按预期工作。

保存到数据库时->

const contentState = editorState.getCurrentContent();
const currentStateData = convertToRaw(contentState);
const richStringifyValue = JSON.stringify(currentStateData);
// sending richStringifyValue to save in Mongo DB

在编辑器中获取并设置 ->

const [editorState, setEditorState] = React.useState(() => EditorState.createEmpty());
const parsedData = JSON.parse(post.contentStyled);
const fromRawData = convertFromRaw(parsedData );
EditorState.createWithContent(fromRawData);


// here is the view rendered part -
   <Editor 
     readOnly={true}
     editorState={editorState}
   />

但是在编辑器中设置之​​后(在从 API 获取数据之后)我的提及(@...@...@...)丢失了 CSS。我们应该做什么?

关于使用编辑-> 在此处输入图像描述

在编辑器中再次获取和设置 -> 在此处输入图像描述

不知道为什么会这样,请帮忙解决这个问题!

4

1 回答 1

0

您应该执行以下操作:

const [editorState, setEditorState] = React.useState(() => {
  const parsedData = JSON.parse(post.contentStyled);
  const fromRawData = convertFromRaw(parsedData );
  return EditorState.createWithContent(fromRawData);
});

// ...
   <Editor 
     readOnly={true}
     editorState={editorState}
   />

如果您editorState使用新创建的状态覆盖,您将删除插件添加的所有装饰器。

于 2021-07-23T21:50:33.877 回答