Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
jan-schweiger opened this issue Aug 24, 2024 · 8 comments
Open
1 task done

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

jan-schweiger opened this issue Aug 24, 2024 · 8 comments
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@jan-schweiger
Copy link

jan-schweiger commented Aug 24, 2024

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.
@jan-schweiger jan-schweiger added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Aug 24, 2024
@jan-schweiger
Copy link
Author

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;
            },
          };
        },
      }),

@tanja-kovakic
Copy link

I can also reproduce the bug. Just tried it out in my application. Can anyone have a look on this one?

@nperez0111
Copy link
Contributor

Maybe this is a browser default behavior? I wonder if the element is still a contenteditable when editable=false

@rosenbauerwillbacker
Copy link

rosenbauerwillbacker commented Aug 27, 2024

I can also reproduce the bug.

@nperez0111 In my case, the content is initially set to editable=false. I however change the content programmatically. In this case everything works fine. CTRL + Z doesn't change the content back. Only, when I make the content editable and then make it read-only again, the bug occurs.

I personally do not think it is the browser's default behaviour, because the bug is only limited to CTRL + Z, but not CTRL + Shift + Z.

I tried to pinpoint the cause, but my knowledge of the inner workings of tiptap is unfortunately limited. Could maybe someone investigate this issue? This is unfortunately a severe problem for my application. Thank you!

@jan-schweiger
Copy link
Author

Any updates on this one?

@nperez0111
Copy link
Contributor

Feel free to look into it and contribute a PR fixing it.

@jan-schweiger
Copy link
Author

Thank you @nperez0111. I have already looked into the potential causes but haven't found a solution yet. I will give it another try over the weekend.

@jan-schweiger
Copy link
Author

Unfortunately, I was unable to identify the root cause. As a temporary "fix" you can use the following code to clear the history whenever you programmatically set the editor to read-only again. This is definitely not the best solution, rather a dirty quickfix for production applications.

import { history } from '@tiptap/pm/history';

// clear history:
editor.unregisterPlugin('history');
editor.registerPlugin(history());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
Status: Triage open
Development

No branches or pull requests

4 participants