-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Comments
I also tried the following quickfix. But strangely the Mod-z function is only called when the isEditable is true.
|
I can also reproduce the bug. Just tried it out in my application. Can anyone have a look on this one? |
Maybe this is a browser default behavior? I wonder if the element is still a contenteditable when editable=false |
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! |
Any updates on this one? |
Feel free to look into it and contribute a PR fixing it. |
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. |
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.
|
I'm finding nothing is blocked since TipTap 2.5.9 when I'm not sure if some particular configuration is required for this to manifest I don't know what configuration is required but I'm quite confident |
@nperez0111 I believe the root cause of this is that a change to how
import "./styles.scss";
import { useEffect, useState } from "react";
import { EditorContent, useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
export default function App() {
const [counter, setCounter] = useState(0);
const editor = useEditor({
immediatelyRender: true,
shouldRerenderOnTransaction: false,
extensions: [StarterKit],
editable: true,
editorProps: {
attributes: {
class: "Editor",
},
},
content: `
<h1>Welcome to your fresh Tiptap Code Sandbox</h1>
<p>You can create a demo for your issue inside of this sandbox and share it with us.</p>
`,
});
useEffect(() => {
editor.setEditable(false);
setCounter(1); // Causes "App" to re-render, which seems to causes `editable` from `useEditor` to take precedence
}, []);
return (
<div className="App">
<EditorContent editor={editor} />
</div>
);
} Suspected problem commit: 7c8889a#diff-2e5eb93343b2ec03e98abdd0c8612de5492886ae9a7ddb6b1cdd22d222bb483cR10 |
Good intuition @Nantris, so tiptap/packages/core/src/Editor.ts Line 191 in 33e1305
Which in React, will recreate & reapply whenever it needs to switch editor instances like you found (which I firmly believe is the correct behavior and in-line with React's model of completely re-rendering things from scratch). The tricky thing is that in the case of React recreating the instance, it is unclear when it should actually enforce the value. Like even in your example, you have So, what I think is probably needed here is that the Any opinions here? Different approaches? |
As for history specifically, I think that all that may be needed is a check here: tiptap/packages/extension-history/src/history.ts Lines 58 to 63 in b941eea
Which would early exit with false if the editor is not editable. Would be happy to take a PR to fix that, since that is simpler than the issue that @Nantris brought up |
@nperez0111 thanks for your reply! I'm not sure what the best way would be really, but I can at least share what I believe might be most intuitive. In my mind, I guess I was expecting the Is it possible/reasonable to store the original Thanks again for the great work @nperez0111 - really appreciate you! |
Created #5731 to address the original issue here. |
Hey @Nantris #5731 doesn't seem to be working. Created #5745 to make sure that Regarding the history related issue: 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. |
@guarmo great work! I can't test at the moment, but are we certain that There's some more discussion on this topic here: https://discuss.prosemirror.net/t/native-undo-history/1823/20 PS: All commands that I tested are able to modify the document when it's otherwise uneditable, when they're called directly. |
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 |
Editable is user editable I think commands are out of scope for this |
@guarmo's change is released with v2.9.0 |
@nperez0111 is there a plan to incorporate @guarmo's plugin into Tiptap to fix the originally reported issue? |
Confirming this aspect of the issue is resolved: by #5745. Undo still goes through though. |
@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 |
Sounds like a good approach. @guarmo were you planning to make a PR to |
Alright PR is merged. You'd need to update |
Amazing!! Thank you @nperez0111 and @guarmo. |
Affected Packages
core, react, @tiptap/extension-history
Version(s)
2.6.6
Bug Description
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
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
The text was updated successfully, but these errors were encountered: