我正在尝试在 react slate-editor 中移动光标。
我尝试以两种方式做到这一点。
第一的:
// This code saving key in offset in variables
const nativeSelection = this.getSelectedText();
const nativeRange = nativeSelection.getRangeAt(0);
const range = findRange(nativeRange, this.editor);
const offset = nativeRange.endOffset;
const key = range.anchor.key;
// OnChange is triggered by running the next line and cursor moves back
this.editor.blur();
// Trying to move to the cursor
this.editor.moveTo(key, offset);
第二:
const nativeSelection = this.getSelectedText();
const nativeRange = nativeSelection.getRangeAt(0);
const range = findRange(nativeRange, this.editor);
const clonedRange = _.cloneDeep(range);
// OnChange is triggered by running the next line and cursor moves back
this.editor.blur();
// Trying to move the cursor
this.editor.select(clonedRange);
不幸的是,似乎select
并moveTo
没有影响光标位置。有人可以帮忙吗?