以编程方式跳转到特定块并将 focus/caret 移动到修饰词
Programmatically jump to a specific block and move the focus/caret to a decorated word
是否可以通过编程方式跳转到特定块并将 focus/caret 移动到修饰词?
是的,您可以使用 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();
by "after" 我的意思是这应该放入 setTimeout 或者你应该以某种方式挂接到 Draft.js 内部
是否可以通过编程方式跳转到特定块并将 focus/caret 移动到修饰词?
是的,您可以使用 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();
by "after" 我的意思是这应该放入 setTimeout 或者你应该以某种方式挂接到 Draft.js 内部