tiptap: [Bug]: Mod-z Not Blocked When editor.isEditable Is False

Affected Packages

core, react, @tiptap/extension-history

Version(s)

2.6.6

Bug Description

  1. Edit the content
  2. Run editor.setEditable(false)
  3. Press CTRL + Z
  4. The undo operation is performed

However, this only happens for Undo but not for Redo.

Reproduce

I was also able to reproduce it by slightly adjusting the minimal setup: https://tiptap.dev/docs/examples/basics/minimal-setup

import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'
import History from '@tiptap/extension-history'

function App() {
  const editor = useEditor({
    extensions: [
      Document,
      Paragraph,
      Text,
      History  // <-- Added History
    ],
    content: "<p>Tiptap content</p>",
  })

  return (
    <div className="App">
      <EditorContent editor={editor} />

      <button onClick={() => editor.setEditable(!editor.isEditable)}>
        Toggle isEdit
      </button>
    </div>
  );
}

export default App;

You just need to edit some content and click the button to experience the bug for yourself.

Thank you very much in advance for your help!

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

Mod + Z shouldn’t change the content when the editor is read-only.

Additional Context (Optional)

No response

Dependency Updates

  • Yes, I’ve updated all my dependencies.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 26 (19 by maintainers)

Most upvoted comments

Alright PR is merged. You’d need to update prosemirror-history to see the changes. Closing this one.

Hey @Nantris #5731 doesn’t seem to be working.

Created #5745 to make sure that setEditable overrides the initial options provided to useEditor’s editable option.

Regarding the history related issue: We’ve found out that the Prosemirror’s history plugin allows undoing when the editor is not editable. We’ve looked into this with @nperez0111 and found out a workaround for it:

  addProseMirrorPlugins() { 
    return [ 
      new Plugin({
        key: new PluginKey('uneditableHistory'),
        props: {
          handleDOMEvents: {
            beforeinput: (view, e: Event) => {
              let inputType = (e as InputEvent).inputType
              if (inputType == 'historyUndo' || inputType == 'historyRedo') {
                if (!this.editor.isEditable) {
                  e.preventDefault()
                }
              }
            }
          }
        }
      })
    ]
  }

What this plugin does is blocking Prosemirror’s history’s default undo event when the editor is not editable.

@Nantris thanks for the reminder. I have just created a PR that aims to fix the issue.

@Nantris between @guarmo and I we decided that it was a little too hacky to incorporate into Tiptap itself. The bug has been there for a long time.

@guarmo was going to open an issue with prosemirror-history to see if we can get this resolved upstream since we identified it to be a 1 line fix in that package

@guarmo’s change is released with v2.9.0

@guarmo great work! I can’t test at the moment, but are we certain that historyUndo and historyRedo fire consistently on all platforms?

Those are the events that prosemirror history listens to so it would at least be consistent with what prosemirror history does so it won’t undo in that case

I also tried the following quickfix. But strangely the Mod-z function is only called when the isEditable is true.

     History.extend({
        addKeyboardShortcuts() {
          return {
            'Mod-z': () => {
              console.log('Mod-z', this.editor.isEditable);
              return this.editor.isEditable ? this.editor.commands.undo() : false;
            },
            'Mod-Shift-z': () => {
              return this.editor.isEditable ? this.editor.commands.redo() : false;
            },
          };
        },
      }),