I'm trying to move the cursor in react slate-editor.
I tried to do it in 2 ways.
First:
// 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);
Second:
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);
Unfortunately, select
and moveTo
doesn't seem to affect the cursor position.
Can someone assist ?
Try using
Transform.select
method combining with a location pointPut cursor after 3rd letter
Move to the very end
You also can try the method
Transforms.move
. Move cursor after 5th word:Or end the line:
It is also worth noting that in my case I had to run the transformation in a setTimeout after setting the editor value. Example: