Skip to content

Commit d0d192b

Browse files
authored
Allow onValueChange and onSelectionChange to trigger on the same frame, fixing a few bugs where one was not being called (#5975)
* changed onContextChange to account for frames with both selection and nonselection operations * removed old hack * changeset
1 parent 3d38db8 commit d0d192b

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

.changeset/honest-frogs-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate-react': patch
3+
---
4+
5+
Allow onValueChange and onSelectionChange to trigger on the same frame, fixing a few bugs where one was not being called

packages/slate-react/src/components/editable.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,8 @@ export const Editable = forwardRef(
693693
exactMatch: false,
694694
suppressThrow: false,
695695
})
696-
const flippedRange = {
697-
anchor: range.focus,
698-
focus: range.anchor,
699-
}
700696

701-
if (
702-
!selection ||
703-
!(
704-
Range.equals(selection, range) ||
705-
Range.equals(selection, flippedRange)
706-
)
707-
) {
697+
if (!selection || !Range.equals(selection, range)) {
708698
native = false
709699

710700
const selectionRef =

packages/slate-react/src/components/slate.tsx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,25 @@ export const Slate = (props: {
5757
const { selectorContext, onChange: handleSelectorChange } =
5858
useSelectorContext()
5959

60-
const onContextChange = useCallback(
61-
(options?: { operation?: Operation }) => {
62-
if (onChange) {
63-
onChange(editor.children)
64-
}
65-
66-
switch (options?.operation?.type) {
67-
case 'set_selection':
68-
onSelectionChange?.(editor.selection)
69-
break
70-
default:
71-
onValueChange?.(editor.children)
72-
}
60+
const onContextChange = useCallback(() => {
61+
if (onChange) {
62+
onChange(editor.children)
63+
}
64+
if (
65+
onSelectionChange &&
66+
editor.operations.find(op => op.type === 'set_selection')
67+
) {
68+
onSelectionChange(editor.selection)
69+
}
70+
if (
71+
onValueChange &&
72+
editor.operations.find(op => op.type !== 'set_selection')
73+
) {
74+
onValueChange(editor.children)
75+
}
7376

74-
handleSelectorChange()
75-
},
76-
[editor, handleSelectorChange, onChange, onSelectionChange, onValueChange]
77-
)
77+
handleSelectorChange()
78+
}, [editor, handleSelectorChange, onChange, onSelectionChange, onValueChange])
7879

7980
useEffect(() => {
8081
EDITOR_TO_ON_CHANGE.set(editor, onContextChange)

0 commit comments

Comments
 (0)