From 8fc5213f937ee62dbcc79d25460ccb63efcbeb8b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 16 Feb 2024 13:48:37 -0500 Subject: [PATCH] fix: Allow undo/redo handler when outside of editor focus. closes #6533 --- app/editor/index.tsx | 15 +++++++++++++++ app/scenes/Document/components/Document.tsx | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 2c0346403..ccd78d4a3 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -4,6 +4,7 @@ import { transparentize } from "polished"; import { baseKeymap } from "prosemirror-commands"; import { dropCursor } from "prosemirror-dropcursor"; import { gapCursor } from "prosemirror-gapcursor"; +import { redo, undo } from "prosemirror-history"; import { inputRules, InputRule } from "prosemirror-inputrules"; import { keymap } from "prosemirror-keymap"; import { MarkdownParser } from "prosemirror-markdown"; @@ -586,6 +587,20 @@ export class Editor extends React.PureComponent< this.props ); + /** + * Undo the last change in the editor. + * + * @returns True if the undo was successful + */ + public undo = () => undo(this.view.state, this.view.dispatch, this.view); + + /** + * Redo the last change in the editor. + * + * @returns True if the change was successful + */ + public redo = () => redo(this.view.state, this.view.dispatch, this.view); + /** * Returns true if the trimmed content of the editor is an empty string. * diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index a231a90ee..97ae33e9b 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -202,6 +202,20 @@ class DocumentScene extends React.Component { } }; + onUndoRedo = (event: KeyboardEvent) => { + if (isModKey(event)) { + if (event.shiftKey) { + if (this.editor.current?.redo()) { + event.preventDefault(); + } + } else { + if (this.editor.current?.undo()) { + event.preventDefault(); + } + } + } + }; + onMove = (ev: React.MouseEvent | KeyboardEvent) => { ev.preventDefault(); const { document, dialogs, t, abilities } = this.props; @@ -406,6 +420,7 @@ class DocumentScene extends React.Component { /> )} +