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

[Breaking Change][lexical][lexical-utils]: Bug Fix: Handle canBeEmpty in $splitNodes #7342

Merged
merged 10 commits into from
Mar 17, 2025

Conversation

etrepum
Copy link
Collaborator

@etrepum etrepum commented Mar 16, 2025

Breaking Change

Previously $insertNodeToNearestRoot could create empty ElementNode when splitting the current node, now it respects canBeEmpty() and will not split in those cases.

Description

This change makes $insertNodeToNearestRoot to respect the canBeEmpty() method of the nodes it is splitting and provides infrastructure to better control splitting (SplitAtPointCaretNextOptions, $insertNodeToNearestRootAtCaret, $splitAtPointCaretNext).

New lexical APIs:

  • $splitAtPointCaretNext - a lower-level API to split at the given caret exactly once (not recursive) with full control over how that happens
  • $getCollapsedCaretRange(pointCaret) - convenience for $getCaretRange(pointCaret, pointCaret) to reduce redundant code

New @lexical/utils API:

  • $insertNodeToNearestRootAtCaret - lower-level API that does $insertNodeToNearestRoot recursive split and then insert without the quirks and selection changes (those happen in the $insertNodeToNearestRoot function).

I'm not very fond of the manner in which nodes are split (the empty splits are a bit awkward especially when not a ParagraphNode), but this doesn't break backwards compatibility in any case that we test and this function is only used in the playground and the implementation of commands that can be easily overridden (e.g. INSERT_HORIZONTAL_RULE_COMMAND). Now that the lower-level functions are exposed, people can control how this works for better UX.

Closes #6849

Test plan

All existing e2e tests pass, new ListNode split test

Before

Splitting an empty ListItemNode creates a broken list:

<ul class="PlaygroundEditorTheme__ul"></ul>
<hr>
<ul class="PlaygroundEditorTheme__ul">
  <li value="1" class="PlaygroundEditorTheme__listItem"></li>
</ul>

After

Splitting an empty ListItemNode creates a complete second list:

<ul class="PlaygroundEditorTheme__ul">
  <li value="1" class="PlaygroundEditorTheme__listItem"></li>
</ul>
<hr>
<ul class="PlaygroundEditorTheme__ul">
  <li value="1" class="PlaygroundEditorTheme__listItem"></li>
</ul>

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Mar 16, 2025
Copy link

vercel bot commented Mar 16, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 17, 2025 7:41pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 17, 2025 7:41pm

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 16, 2025
@etrepum etrepum changed the title [WIP][lexical][lexical-utils]: Bug Fix: Handle canBeEmpty in $splitNodes [Breaking Change][lexical][lexical-utils]: Bug Fix: Handle canBeEmpty in $splitNodes Mar 17, 2025

export class TestDecoratorNode extends DecoratorNode<JSX.Element> {
__block: boolean = false;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

DecoratorNode are inline by default but some of these tests were inserting it directly into the root. I added this to make that reasonable.

@etrepum
Copy link
Collaborator Author

etrepum commented Mar 17, 2025

/cc @ivailop7

@ivailop7 ivailop7 added this pull request to the merge queue Mar 17, 2025
Merged via the queue into facebook:main with commit 3a52948 Mar 17, 2025
39 checks passed
@etrepum etrepum deleted the list-node-block-split branch March 18, 2025 05:28
@etrepum etrepum mentioned this pull request Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: If you add a table on a list it adds it above the list and results in invalid exported HTML
3 participants