mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-11 09:43:58 +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:
5
.changeset/fifty-walls-search.md
Normal file
5
.changeset/fifty-walls-search.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
add context menu undo support
|
@@ -506,6 +506,7 @@ export const Editable = forwardRef(
|
|||||||
// https://github.com/facebook/react/issues/11211
|
// https://github.com/facebook/react/issues/11211
|
||||||
const onDOMBeforeInput = useCallback(
|
const onDOMBeforeInput = useCallback(
|
||||||
(event: InputEvent) => {
|
(event: InputEvent) => {
|
||||||
|
handleNativeHistoryEvents(editor, event)
|
||||||
const el = ReactEditor.toDOMNode(editor, editor)
|
const el = ReactEditor.toDOMNode(editor, editor)
|
||||||
const root = el.getRootNode()
|
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,
|
// 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)
|
// and it only triggers the input event for the node. (2024/10/09)
|
||||||
if (!ReactEditor.isFocused(editor)) {
|
if (!ReactEditor.isFocused(editor)) {
|
||||||
const native = event.nativeEvent as InputEvent
|
handleNativeHistoryEvents(
|
||||||
const maybeHistoryEditor: any = editor
|
editor,
|
||||||
if (
|
event.nativeEvent as InputEvent
|
||||||
native.inputType === 'historyUndo' &&
|
)
|
||||||
typeof maybeHistoryEditor.undo === 'function'
|
|
||||||
) {
|
|
||||||
maybeHistoryEditor.undo()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
native.inputType === 'historyRedo' &&
|
|
||||||
typeof maybeHistoryEditor.redo === 'function'
|
|
||||||
) {
|
|
||||||
maybeHistoryEditor.redo()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[attributes.onInput, editor]
|
[attributes.onInput, editor]
|
||||||
@@ -1976,3 +1965,21 @@ export const isDOMEventHandled = <E extends Event>(
|
|||||||
|
|
||||||
return event.defaultPrevented
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user