Skip to content

Commit fac5ed4

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

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

src/plugins/undo-plugin.js

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,44 @@ import { yUndoPluginKey, ySyncPluginKey } from './keys.js'
1212
* @property {boolean} hasRedoOps
1313
*/
1414

15+
/**
16+
* Undo the last user action
17+
*
18+
* @param {import('prosemirror-state').EditorState} state
19+
* @return {boolean} whether a change was undone
20+
*/
21+
export const undo = state => yUndoPluginKey.getState(state)?.undoManager?.undo() != null
22+
23+
/**
24+
* Redo the last user action
25+
*
26+
* @param {import('prosemirror-state').EditorState} state
27+
* @return {boolean} whether a change was undone
28+
*/
29+
export const redo = state => yUndoPluginKey.getState(state)?.undoManager?.redo() != null
30+
1531
/**
1632
* Undo the last user action if there are undo operations available
1733
* @type {import('prosemirror-state').Command}
1834
*/
1935
export const undoCommand = (state, dispatch) => {
20-
const undoManager = yUndoPluginKey.getState(state).undoManager
21-
if (undoManager == null || !undoManager.undoStack.length) {
36+
const undoManager = yUndoPluginKey.getState(state)?.undoManager
37+
if (undoManager == null) {
2238
return false
2339
}
24-
25-
if (dispatch) {
26-
undoManager.undo()
27-
}
28-
return true
40+
return dispatch == null ? undoManager.canUndo() : (undoManager.undo() != null)
2941
}
3042

3143
/**
3244
* Redo the last user action if there are redo operations available
3345
* @type {import('prosemirror-state').Command}
3446
*/
3547
export const redoCommand = (state, dispatch) => {
36-
const undoManager = yUndoPluginKey.getState(state).undoManager
37-
if (undoManager == null || !undoManager.redoStack.length) {
48+
const undoManager = yUndoPluginKey.getState(state)?.undoManager
49+
if (undoManager == null) {
3850
return false
3951
}
40-
41-
if (dispatch) {
42-
undoManager.redo()
43-
}
44-
return true
45-
}
46-
47-
/**
48-
* Undo the last user action
49-
* @param {import('prosemirror-state').EditorState} state
50-
* @returns {boolean}
51-
*/
52-
export const undo = (state) => {
53-
return undoCommand(state, () => {})
54-
}
55-
56-
/**
57-
* Redo the last user action
58-
* @param {import('prosemirror-state').EditorState} state
59-
* @returns {boolean}
60-
*/
61-
export const redo = (state) => {
62-
return redoCommand(state, () => {})
52+
return dispatch == null ? undoManager.canRedo() : (undoManager.redo() != null)
6353
}
6454

6555
export const defaultProtectedNodes = new Set(['paragraph'])
@@ -70,10 +60,10 @@ export const defaultProtectedNodes = new Set(['paragraph'])
7060
* @returns {boolean}
7161
*/
7262
export const defaultDeleteFilter = (item, protectedNodes) => !(item instanceof Item) ||
73-
!(item.content instanceof ContentType) ||
74-
!(item.content.type instanceof Text ||
63+
!(item.content instanceof ContentType) ||
64+
!(item.content.type instanceof Text ||
7565
(item.content.type instanceof XmlElement && protectedNodes.has(item.content.type.nodeName))) ||
76-
item.content.type._length === 0
66+
item.content.type._length === 0
7767

7868
/**
7969
* @param {object} [options]

0 commit comments

Comments
 (0)