Skip to content

[lexical] Refactor: Simplify RangeSelection.insertText via removeText decomposition - #8870

Merged
etrepum merged 2 commits into
meta:mainfrom
mayrang:refactor/simplify-insert-text
Jul 22, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
mayrang:refactor/simplify-insert-text

Conversation

@mayrang

@mayrang mayrang commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Description

insertText had ~390 lines of interleaved deletion and insertion logic for non-collapsed selections — tracking markedNodeKeysForKeep, walking parent chains, and splicing across multi-node boundaries. etrepum's WIP comment at the top of the method noted that removeText() is now independent of insertText, so the non-collapsed case can be decomposed into removeText() followed by a collapsed-cursor insert.

This PR implements that decomposition. The non-collapsed path captures format/style from the document-order first text node, calls removeText(), restores the format, then delegates to $insertTextAtPoint (normal case) or falls through to the collapsed spliceText path (during IME composition, to preserve DOM node identity for the browser's composition tracker).

The collapsed path consolidates the scattered token/segmented/boundary checks into a single needsRedirect guard, and routes the remaining cases through format-diff detection → $insertTextAtPoint or direct spliceText.

Three pre-existing bugs are fixed along the way:

  • setStyle(style) was missing when replacing a segmented node outside composition
  • setStyle(style) was missing when creating a redirect node at offset 0
  • canInsertTextAfter() was not checked on the previous sibling in the offset-0 redirect path

~390 → ~180 lines (net −100 in LexicalSelection.ts).

Test plan

  • pnpm test-unit -- LexicalSelection.test.ts — 85 pass
  • pnpm test-unit -- LexicalSelectionHelpers.test.ts — 51 pass
  • Full unit suite (lexical + lexical-selection) — 1121 pass
  • e2e (chromium) — 765 pass
  • Manual playground (Chrome, Safari, Firefox):
    • Basic text input, selection replacement, select-all replacement
    • Bold format inheritance across non-collapsed selection
    • Backward selection format inheritance
    • Link boundary insertion (start, end, middle)
    • Format toggle on empty paragraph then typing
    • Multi-paragraph selection replacement
    • Korean IME: basic input, bold toggle, selection replacement
    • Undo/redo after selection replacement
  • tsc / prettier / eslint clean

… decomposition

Decompose non-collapsed insertText into removeText() + collapsed insert, as suggested in etrepum's WIP comment. This reduces ~390 lines to ~175 lines by eliminating the manual multi-node deletion logic.

Bug fixes included: missing setStyle on segmented node replacement, missing canInsertTextAfter guard on previous sibling redirect, and missing setStyle on offset-0 redirect node creation.

During composition, the non-collapsed path falls through to the collapsed spliceText code to preserve DOM node identity for the browser's IME tracker.
@vercel

vercel Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 22, 2026 8:28am
lexical-playground Ready Ready Preview Jul 22, 2026 8:28am

Request Review

@meta-cla meta-cla 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 Jul 22, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 22, 2026
Comment on lines +1054 to +1056
this.anchor.offset -= text.length;
this._cachedNodes = null;
this._cachedIsBackward = null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It might be easier to maintain this sort of thing if we go through the point's set method instead, which takes care of invalidating caches. We could do this simplification in the other places with this pattern such as in $insertTextAtPoint

Suggested change
this.anchor.offset -= text.length;
this._cachedNodes = null;
this._cachedIsBackward = null;
this.anchor.set(
this.anchor.key,
this.anchor.offset - text.length,
this.anchor.type,
);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Applied — switched both this site and the matching one in $insertTextAtPoint to anchor.set().

This branch was successfully deployed

2 active deployments
Preview – lexical — d889f1c8 Deployed Jul 22, 2026 by vercel[bot]
Preview – lexical-playground — d889f1c8 Deployed Jul 22, 2026 by vercel[bot]
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.

2 participants