1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 20:40:19 +02:00

fix: context menu undo support (#5822)

* fix: context menu undo support

* chore: add change set for context menu undo fix

* chore: fix lint
This commit is contained in:
Ravi Lamkoti
2025-03-22 02:38:03 +05:30
committed by GitHub
parent 8b8fc58efc
commit 68915e8cfa
2 changed files with 28 additions and 16 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': patch
---
add context menu undo support

View File

@@ -506,6 +506,7 @@ export const Editable = forwardRef(
// https://github.com/facebook/react/issues/11211
const onDOMBeforeInput = useCallback(
(event: InputEvent) => {
handleNativeHistoryEvents(editor, event)
const el = ReactEditor.toDOMNode(editor, editor)
const root = el.getRootNode()
@@ -1077,22 +1078,10 @@ export const Editable = forwardRef(
// This means undo can be triggered even when the div is not focused,
// and it only triggers the input event for the node. (2024/10/09)
if (!ReactEditor.isFocused(editor)) {
const native = event.nativeEvent as InputEvent
const maybeHistoryEditor: any = editor
if (
native.inputType === 'historyUndo' &&
typeof maybeHistoryEditor.undo === 'function'
) {
maybeHistoryEditor.undo()
return
}
if (
native.inputType === 'historyRedo' &&
typeof maybeHistoryEditor.redo === 'function'
) {
maybeHistoryEditor.redo()
return
}
handleNativeHistoryEvents(
editor,
event.nativeEvent as InputEvent
)
}
},
[attributes.onInput, editor]
@@ -1976,3 +1965,21 @@ export const isDOMEventHandled = <E extends Event>(
return event.defaultPrevented
}
const handleNativeHistoryEvents = (editor: Editor, event: InputEvent) => {
const maybeHistoryEditor: any = editor
if (
event.inputType === 'historyUndo' &&
typeof maybeHistoryEditor.undo === 'function'
) {
maybeHistoryEditor.undo()
return
}
if (
event.inputType === 'historyRedo' &&
typeof maybeHistoryEditor.redo === 'function'
) {
maybeHistoryEditor.redo()
return
}
}