Skip to content

Commit

Permalink
Retain editor selection when using ReactEditor.focus() (#5516)
Browse files Browse the repository at this point in the history
* Retain editor selection on ReactEditor.focus()

* Add changeset

* Fix lint
  • Loading branch information
josephmr committed Sep 21, 2023
1 parent fd753b8 commit 300dc57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-frogs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': minor
---

Retain editor selection when using ReactEditor.focus()
6 changes: 6 additions & 0 deletions packages/slate-react/src/plugin/react-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ export const ReactEditor: ReactEditorInterface = {
IS_FOCUSED.set(editor, true)

if (root.activeElement !== el) {
if (editor.selection && root instanceof Document) {
const domSelection = root.getSelection()
const domRange = ReactEditor.toDOMRange(editor, editor.selection)
domSelection?.removeAllRanges()
domSelection?.addRange(domRange)
}
el.focus({ preventScroll: true })
}
},
Expand Down

0 comments on commit 300dc57

Please sign in to comment.