reactjs - Programmatically jump to a specific block and move the focus/caret to a decorated word - Stack Overflow


Fri Sep 17 2021 16:06:23 GMT+0000 (UTC)

Saved by @nabilashraf #javascript

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

DraftJS Selection