Fixed issue where can() returned false when the command was executable#5744
Fixed issue where can() returned false when the command was executable#5744guanriyue wants to merge 8 commits intoueberdosis:developfrom
Conversation
…le default editor.
…let list, ordered list, and heading are allowed to convert between each other.
…().run()` because the state required by later commands may depend on the commands executed earlier.
…e`, otherwise `clearNodes` will throw an exception during execution.
|
✅ Deploy Preview for tiptap-embed ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
…o buttons, which will throw an exception and cause the test to fail.
|
Hey @guanriyue I haven't reviewed this super thoroughly, because I think that this approach is wrong. What I do not believe that you need to actually change |
| public createCan(startTr?: Transaction): CanCommands { | ||
| const { rawCommands, state } = this | ||
| const dispatch = false | ||
| const dispatch = true |
There was a problem hiding this comment.
I believe this is wrong.
If dispatch is true, commands will think that they can modify the transaction when they should not be able to.
dispatch should probably be named shouldDispatch where it being false means you should not, as a command, actually apply your change to the editor.
There was a problem hiding this comment.
This test was correct. Please revert
There was a problem hiding this comment.
The actual problem in this file is:
if (!dispatch) {
return true
}There was a problem hiding this comment.
Remove this section of code will resolve the current issue. However, it seems there is a deeper problem that needs to be addressed, which would involve a significant change.
I will take your advice and work to close this PR as soon as possible.
There was a problem hiding this comment.
This is what the PR should be addressing. Everything else around this PR is incorrect with the intention behind can chains and how they should work
|
Have you looked at the description of this issue #5721? I explained why I believe Perhaps it is a mistake; in fact, setting Four years ago, at the very beginning, the About five months later, it was changed to About a year and a half later, After that fix, several new issues with can checks emerged.
I am now trying to change it back to
Yes, you're right. However, in
Yes, that is an inappropriate naming; it should be renamed to |
|
I'm sorry, but, I still believe that this is all misguided. Please refer to the documentation of Prosemirror https://prosemirror.net/docs/guide/#commands It specifically says that:
It specifically says it should be called like function blinkView(_state, dispatch, view) {
if (dispatch) {
view.dom.style.background = "yellow"
setTimeout(() => view.dom.style.background = "", 1000)
}
return true
}This is done purposefully, so that a command can be "tried" without actually committing the side-effect. If you pass You are not solving the real problem which is the implementation of the command. You are attempting to change how can chains work to fit a wrong implementation, fix the implementation not how can chains work. |
Changes Overview
Fix the issue where can() check returns false even when the command is actually executable. And added cypress test cases.
Implementation Approach
When chainCommand is executed, multiple commands are run sequentially. The editor state used by later commands may depend on the preceding commands. Therefore, when executing can(), createChain must ensure that shouldDispatch is true to prevent skipping operations on tr.
This fix effectively reverts the code changes from the pull request #3026 and resolves the issue #3025 caused by the clearNodes error.
At the same time, it will also fix issue #3229 and issue #4057.
For more details, please refer to this issue #5721.
Testing Done
First, add the disabled attribute to the heading and list action buttons in the example default editor.
Before the fix, if the selection was on a heading node, the list action buttons would remain disabled, even though the commands were actually available. After the change, the list action buttons will appear enabled.
Additionally, corresponding Cypress test cases have been added.
Verification Steps
Access the default example editor at https://tiptap.dev/docs/examples/basics/default-text-editor or by executing cypress test
Additional Notes
Checklist
Related Issues