I am trying to insert image to Draft.js editor.
Based on my understanding, I need update entity by mergeData
and blocks by mergeBlockData
. (I am not sure)
Now I am trying to use mergeBlockData
to insert a block.
mergeBlockData( contentState: ContentState, selectionState: SelectionState, blockData: Map<any, any> ): ContentState
Please read comment in the code.
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);
mergeBlockData
is neither replace or add, what is the function of it? And which correct API should I use? – YorkContentBlock
if you want to add new blocks. – LarousseContentBlock
to add new block in answer? thanks – York