I need to apply focus to a Draft.js editor and position the cursor at the start of the first line/block. The editor contains multiple lines/blocks.
With only this.refs.editor.focus()
being applied, the cursor is always positioned at the start of the second block/line within the editor.
Using this question and this issue as guides, I tried the code below without success. I suspect that passing blockMap
to createFromBlockArray()
is not correct:
focusTopLine() {
this.refs.editor.focus();
const { editorState } = this.state;
const contentState = editorState.getCurrentContent();
const selectionState = editorState.getSelection();
const blockMap = contentState.getBlockMap();
const newContentState = ContentState.createFromBlockArray(blockMap);
const newEditorState = EditorState.createWithContent(newContentState);
this.setState({
editorState: EditorState.forceSelection(newEditorState, selectionState)
});
}