1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 06:31:28 +02:00

Retain editor selection when using ReactEditor.focus() (#5516)

* Retain editor selection on ReactEditor.focus()

* Add changeset

* Fix lint
This commit is contained in:
Joseph Rollins
2023-09-21 15:40:02 -07:00
committed by GitHub
parent fd753b8bf1
commit 300dc57a00
2 changed files with 11 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': minor
---
Retain editor selection when using ReactEditor.focus()

View File

@@ -417,6 +417,12 @@ export const ReactEditor: ReactEditorInterface = {
IS_FOCUSED.set(editor, true) IS_FOCUSED.set(editor, true)
if (root.activeElement !== el) { 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 }) el.focus({ preventScroll: true })
} }
}, },