1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 01:33:58 +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
4 changed files with 26 additions and 0 deletions

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)
)
},
}