我需要将焦点应用到 Draft.js 编辑器并将光标定位在第一行/块的开头。编辑器包含多行/块。
仅this.refs.editor.focus()
在应用时,光标始终位于编辑器中第二个块/行的开头。
使用这个问题和这个问题作为指导,我尝试了下面的代码但没有成功。我怀疑传递blockMap
给createFromBlockArray()
不正确:
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)
});
}