fix(richtext-lexical): prevent extra paragraph when inserting blocks or uploadNodes. Add preemptive selection normalization #12077
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #11628 (partially, explanation below)
PR #6389 caused bug #11628, which is a regression, as it had already been fixed in #4441
I reverted those changes and was able to verify that the error didn't occur, even when I forced it programmatically. The changes in that PR were made because the selection sometimes ended up on deleted nodes, and Lexical would throw an error. Upon investigation, I found that Lexical had recently made improvements to address selection normalization.
Although it wasn't necessary to resolve the issue, I added a
NormalizeSelectionPlugin
to the editor, which makes selection handling in the editor more robust.Partly because of what I explained above, and partly because I remember seeing several issues related to this bug, and I had the same problem a couple of weeks ago while developing a feature for the email builder.
So, what this plugin does is move the selection to the end of the editor if it doesn't find the selected nodes, instead of throwing an error.
Note: This PR fixes block insertion as it was before #6389. That is, no empty paragraphs are left, unless they are the first paragraph in the editor. This was because inserting a block at the beginning of the editor would otherwise be difficult due to lack of room to click.
However, looking at #11628's video, it seems users also want to be able to prevent the first paragraph from being empty. This makes sense to me, so I think in another PR we could add a button at the top, just like we did at the bottom of the editor.