我正在尝试将图像插入 Draft.js 编辑器。
根据我的理解,我需要 update entity bymergeData
和 blocks by mergeBlockData
。(我不确定)
现在我正在尝试使用mergeBlockData
插入块。
mergeBlockData( contentState: ContentState, selectionState: SelectionState, blockData: Map<any, any> ): ContentState
请阅读代码中的注释。
import { Map } from 'immutable';
const selection = this.state.editorState.getSelection();
const contentState = this.state.editorState.getCurrentContent();
console.log(convertToRaw(contentState)); // for example, I have 3 blocks
const blockData = Map({ ov72: { // here how to generate a random valid key?
"key": "ov72",
"text": " ",
"type": "atomic",
"depth": 0,
"inlineStyleRanges": [],
"entityRanges": [{
"offset": 0,
"length": 1,
"key": 1
}],
"data": {}
}});
const newContentState = Modifier.mergeBlockData(contentState, selection, blockData);
console.log(convertToRaw(newContentState)); // here is wrong, still 3 blocks. Also original blocks have no change
const newEditorState = EditorState.push(this.state.editorState, newContentState);