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:
parent
d5b2d7f55e
commit
f3fb40cce0
5
.changeset/fifty-ducks-sip.md
Normal file
5
.changeset/fifty-ducks-sip.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'slate-react': patch
|
||||
---
|
||||
|
||||
Fixed selection logic when a controlled editor's nodes change out from under it.
|
@ -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
|
||||
|
@ -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)
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user