Skip to content

Commit 96db316

Browse files
committed
[undo plugin] optimize #183
1 parent e534deb commit 96db316

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

src/plugins/undo-plugin.js

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,32 @@ import { yUndoPluginKey, ySyncPluginKey } from './keys.js'
1313
*/
1414

1515
/**
16-
* Undo the last user action if there are undo operations available
17-
* @type {import('prosemirror-state').Command}
16+
* Undo the last user action
17+
*
18+
* @param {import('prosemirror-state').EditorState} state
19+
* @return {boolean} whether a change was undone
1820
*/
19-
export const undoCommand = (state, dispatch) => {
20-
const undoManager = yUndoPluginKey.getState(state).undoManager
21-
if (undoManager == null || !undoManager.undoStack.length) {
22-
return false
23-
}
24-
25-
if (dispatch) {
26-
undoManager.undo()
27-
}
28-
return true
29-
}
21+
export const undo = state => yUndoPluginKey.getState(state)?.undoManager?.undo() != null
3022

3123
/**
32-
* Redo the last user action if there are redo operations available
33-
* @type {import('prosemirror-state').Command}
24+
* Redo the last user action
25+
*
26+
* @param {import('prosemirror-state').EditorState} state
27+
* @return {boolean} whether a change was undone
3428
*/
35-
export const redoCommand = (state, dispatch) => {
36-
const undoManager = yUndoPluginKey.getState(state).undoManager
37-
if (undoManager == null || !undoManager.redoStack.length) {
38-
return false
39-
}
40-
41-
if (dispatch) {
42-
undoManager.redo()
43-
}
44-
return true
45-
}
29+
export const redo = state => yUndoPluginKey.getState(state)?.undoManager?.redo() != null
4630

4731
/**
48-
* Undo the last user action
49-
* @param {import('prosemirror-state').EditorState} state
50-
* @returns {boolean}
32+
* Undo the last user action if there are undo operations available
33+
* @type {import('prosemirror-state').Command}
5134
*/
52-
export const undo = (state) => {
53-
return undoCommand(state, () => {})
54-
}
35+
export const undoCommand = (state, dispatch) => dispatch == null ? yUndoPluginKey.getState(state)?.undoManager?.canUndo() : undo(state)
5536

5637
/**
57-
* Redo the last user action
58-
* @param {import('prosemirror-state').EditorState} state
59-
* @returns {boolean}
38+
* Redo the last user action if there are redo operations available
39+
* @type {import('prosemirror-state').Command}
6040
*/
61-
export const redo = (state) => {
62-
return redoCommand(state, () => {})
63-
}
41+
export const redoCommand = (state, dispatch) => dispatch == null ? yUndoPluginKey.getState(state)?.undoManager?.canRedo() : redo(state)
6442

6543
export const defaultProtectedNodes = new Set(['paragraph'])
6644

@@ -70,10 +48,10 @@ export const defaultProtectedNodes = new Set(['paragraph'])
7048
* @returns {boolean}
7149
*/
7250
export const defaultDeleteFilter = (item, protectedNodes) => !(item instanceof Item) ||
73-
!(item.content instanceof ContentType) ||
74-
!(item.content.type instanceof Text ||
51+
!(item.content instanceof ContentType) ||
52+
!(item.content.type instanceof Text ||
7553
(item.content.type instanceof XmlElement && protectedNodes.has(item.content.type.nodeName))) ||
76-
item.content.type._length === 0
54+
item.content.type._length === 0
7755

7856
/**
7957
* @param {object} [options]

0 commit comments

Comments
 (0)