Skip to content

fix(richtext-lexical): prevent extra paragraph when inserting blocks or uploadNodes. Add preemptive selection normalization #12077

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

Merged
merged 10 commits into from
Apr 29, 2025

Conversation

GermanJablo
Copy link
Contributor

@GermanJablo GermanJablo commented Apr 10, 2025

Fixes #11628

PR #6389 caused bug #11628, which is a regression, as it had already been fixed in #4441

It is likely that some things have changed because 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.

I'm also adding a new collection to the Lexical test suite, intending it to be used by default for most tests going forward. I've left an explanatory comment on the dashboard.


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.

…locks or uploadNodes. Add a preventative plugin to normalize the selection
Comment on lines +62 to +63
// Insert blocks node BEFORE potentially removing focusNode, as $insertNodeToNearestRoot errors if the focusNode doesn't exist
$insertNodeToNearestRoot(blockNode)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key point of the fix. We still insert before removing focusNode, but after getting focusNode, since $insertNodeToNearestRoot can change the selection.

Comment on lines -65 to 67
// First, delete currently selected node if it's an empty paragraph and if there are sufficient
// paragraph nodes (more than 1) left in the parent node, so that we don't "trap" the user
if (
$isParagraphNode(focusNode) &&
focusNode.getTextContentSize() === 0 &&
focusNode
.getParentOrThrow()
.getChildren()
.filter((node) => $isParagraphNode(node)).length > 1
) {
// Delete the node it it's an empty paragraph
if ($isParagraphNode(focusNode) && !focusNode.__first) {
focusNode.remove()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since PR #10530 this is no longer necessary

@@ -112,6 +107,7 @@ export const LexicalEditor: React.FC<
}
ErrorBoundary={LexicalErrorBoundary}
/>
<NormalizeSelectionPlugin />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was resolved with the changes above, so this isn't strictly necessary, but I decided to add it to make the editor more robust and prevent errors in the future. Additionally, I believe this will fix a bug in the email builder plugin.

@GermanJablo GermanJablo requested a review from AlessioGr April 21, 2025 21:52
@GermanJablo GermanJablo marked this pull request as ready for review April 21, 2025 21:52
Copy link
Contributor

@paulpopus paulpopus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed on call and it looks good to me!

@GermanJablo GermanJablo enabled auto-merge (squash) April 29, 2025 15:51
@GermanJablo GermanJablo merged commit 5492542 into main Apr 29, 2025
76 checks passed
@GermanJablo GermanJablo deleted the blocks-paragraph branch April 29, 2025 15:57
Copy link
Contributor

🚀 This is included in version v3.36.0

kendelljoseph pushed a commit that referenced this pull request May 15, 2025
…or uploadNodes. Add preemptive selection normalization (#12077)

Fixes #11628

PR #6389 caused bug #11628, which is a regression, as it had already
been fixed in #4441

It is likely that some things have changed because [Lexical had recently
made improvements](facebook/lexical#7046) 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.

I'm also adding a new collection to the Lexical test suite, intending it
to be used by default for most tests going forward. I've left an
explanatory comment on the dashboard.

___

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](#10530).
kendelljoseph pushed a commit that referenced this pull request May 19, 2025
…or uploadNodes. Add preemptive selection normalization (#12077)

Fixes #11628

PR #6389 caused bug #11628, which is a regression, as it had already
been fixed in #4441

It is likely that some things have changed because [Lexical had recently
made improvements](facebook/lexical#7046) 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.

I'm also adding a new collection to the Lexical test suite, intending it
to be used by default for most tests going forward. I've left an
explanatory comment on the dashboard.

___

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](#10530).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lexical Block feature adds unnecessary paragraphs
2 participants