fix: Allow undo/redo handler when outside of editor focus.

closes #6533
This commit is contained in:
Tom Moor
2024-02-16 13:48:37 -05:00
parent 7555240413
commit 8fc5213f93
2 changed files with 30 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -202,6 +202,20 @@ class DocumentScene extends React.Component<Props> {
}
};
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<Props> {
/>
)}
<RegisterKeyDown trigger="m" handler={this.onMove} />
<RegisterKeyDown trigger="z" handler={this.onUndoRedo} />
<RegisterKeyDown trigger="e" handler={this.goToEdit} />
<RegisterKeyDown trigger="Escape" handler={this.goBack} />
<RegisterKeyDown trigger="h" handler={this.goToHistory} />