lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: Typing after deleting a root-level block decorator inserts a phantom blank paragraph

Open mayrang opened this issue 5 months ago • 0 comments

Lexical version: latest on playground.lexical.dev (also reproducible against current main)

Steps To Reproduce

  1. Open https://playground.lexical.dev/.
  2. Type /page break (or /equation) to insert a block decorator at the start of an empty editor. Lexical inserts a trailing paragraph after the decorator.
  3. Press Backspace once — the trailing paragraph is removed and the decorator becomes a NodeSelection.
  4. Press Backspace again — the decorator is removed; the editor is left with the original empty paragraph at root.
  5. Type any character.

example:

https://github.com/user-attachments/assets/ea0777b0-d0fc-441b-9e41-b67a95772164

The current behavior

Step 5 produces a phantom blank line above the typed character. The original empty paragraph is left untouched and a brand-new paragraph is appended after it for the typed text.

Trace: at typing time the RangeSelection's anchor is { key: 'root', offset: 1, type: 'element' }. RangeSelection.insertText routes through $transferStartingElementPointToTextPoint, which lands on the placementNode === null + isRootNode(element) branch (packages/lexical/src/LexicalSelection.ts:295-302). That branch unconditionally appends $createParagraphNode().append(textNode) to root, so the empty paragraph already at root is left behind.

The expected behavior

The typed character should land in the existing empty paragraph (the natural caret target after the decorator delete), with no extra blank line.

Impact of fix

  • Affects every block decorator that root can host (PageBreak, Equation, HorizontalRule, custom Card / Figure decorators, …).
  • One-line guard in $transferStartingElementPointToTextPoint: when root's last child is an empty non-inline ElementNode, reuse it instead of appending a new paragraph. PR coming up.

mayrang avatar Jun 12 '26 00:06 mayrang