diff --git a/.eslintrc.js b/.eslintrc.js index 6c04ef404..c39496c71 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -59,6 +59,7 @@ module.exports = { 'no-param-reassign': 'off', 'import/prefer-default-export': 'off', 'consistent-return': 'off', + 'prefer-destructuring': 'off', 'no-redeclare': 'off', '@typescript-eslint/no-redeclare': ['error'], 'no-unused-vars': 'off', diff --git a/packages/extension-collaboration/src/collaboration.ts b/packages/extension-collaboration/src/collaboration.ts index 2f8683892..9f08febe2 100644 --- a/packages/extension-collaboration/src/collaboration.ts +++ b/packages/extension-collaboration/src/collaboration.ts @@ -1,9 +1,11 @@ import { Extension, Command } from '@tiptap/core' +import { UndoManager } from 'yjs' import { redo, undo, ySyncPlugin, yUndoPlugin, + yUndoPluginKey, } from 'y-prosemirror' declare module '@tiptap/core' { @@ -47,14 +49,34 @@ export const Collaboration = Extension.create({ addCommands() { return { - undo: () => ({ tr, state }) => { + undo: () => ({ tr, state, dispatch }) => { tr.setMeta('preventDispatch', true) + const undoManager: UndoManager = yUndoPluginKey.getState(state).undoManager + + if (undoManager.undoStack.length === 0) { + return false + } + + if (!dispatch) { + return true + } + return undo(state) }, - redo: () => ({ tr, state }) => { + redo: () => ({ tr, state, dispatch }) => { tr.setMeta('preventDispatch', true) + const undoManager: UndoManager = yUndoPluginKey.getState(state).undoManager + + if (undoManager.redoStack.length === 0) { + return false + } + + if (!dispatch) { + return true + } + return redo(state) }, }