以编程方式跳转到特定块并将焦点/插入符移动到装饰的单词

时间:2018-01-16 17:40:16

标签: draftjs

是否可以以编程方式跳转到特定的块并将焦点/插入符移动到装饰的单词?

1 个答案:

答案 0 :(得分:2)

是的,您可以使用EditorState.forceSelection:

设置文本选择
let selection = new SelectionState({
  anchorKey: 'abc', // key of block
  anchorOffset: 5,
  focusKey: 'abc',
  focusOffset: 10, // key of block
  hasFocus: true,
  isBackward: false // isBackward = (focusOffset < anchorOffset)
});

let s = new SelectionState(selection);
editorState = EditorState.forceSelection(editorState, s);
// Make sure to set this new EditorState as the editorState of Draft.js component

强制文档跳转到此位置比较棘手,但在 Draft.js设置选择后运行以下应该可以解决问题:

window.getSelection().focusNode.parentElement.scrollIntoView();
window.getSelection().anchorNode.parentElement.scrollIntoView();
&#34; &#34;我的意思是这应该放入一个setTimeout,或者你应该以某种方式挂钩到Draft.js内部