1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-19 12:51:59 +02:00

Fixed an issue with controlled value messing up editor.selection (#3652)

* Fixed an issue with controlled value messing up editor.selection

* Create fifty-ducks-sip.md

Co-authored-by: Ian Storm Taylor <ian@ianstormtaylor.com>
This commit is contained in:
Mateusz Burzyński 2021-03-31 22:13:42 +02:00 committed by GitHub
parent d5b2d7f55e
commit f3fb40cce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'slate-react': patch
---
Fixed selection logic when a controlled editor's nodes change out from under it.

View File

@ -182,6 +182,15 @@ export const Editable = (props: EditableProps) => {
return
}
// when <Editable/> is being controlled through external value
// then its children might just change - DOM responds to it on its own
// but Slate's value is not being updated through any operation
// and thus it doesn't transform selection on its own
if (selection && !ReactEditor.hasRange(editor, selection)) {
editor.selection = ReactEditor.toSlateRange(editor, domSelection)
return
}
// Otherwise the DOM selection is out of sync, so update it.
const el = ReactEditor.toDOMNode(editor, editor)
state.isUpdatingSelection = true

View File

@ -32,6 +32,7 @@ import { IS_CHROME } from '../utils/environment'
export interface ReactEditor extends BaseEditor {
insertData: (data: DataTransfer) => void
setFragmentData: (data: DataTransfer) => void
hasRange: (editor: ReactEditor, range: Range) => boolean
}
export const ReactEditor = {
@ -573,4 +574,11 @@ export const ReactEditor = {
return { anchor, focus }
},
hasRange(editor: ReactEditor, range: Range): boolean {
const { anchor, focus } = range
return (
Editor.hasPath(editor, anchor.path) && Editor.hasPath(editor, focus.path)
)
},
}

View File

@ -1060,6 +1060,10 @@ export const Editor: EditorInterface = {
return at
},
hasPath(editor: Editor, path: Path): boolean {
return Node.has(editor, path)
},
/**
* Create a mutable ref for a `Path` object, which will stay in sync as new
* operations are applied to the editor.