Repository navigation
[lexical] Refactor: Simplify RangeSelection.insertText via removeText decomposition - #8870
Merged
Merged
Conversation
… 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.
mayrang
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
July 22, 2026 02:16
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
Jul 22, 2026
Comment on lines
+1054
to
+1056
| this.anchor.offset -= text.length; | ||
| this._cachedNodes = null; | ||
| this._cachedIsBackward = null; |
Collaborator
There was a problem hiding this comment.
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, | |
| ); |
Collaborator
Author
There was a problem hiding this comment.
Applied — switched both this site and the matching one in $insertTextAtPoint to anchor.set().
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
insertTexthad ~390 lines of interleaved deletion and insertion logic for non-collapsed selections — trackingmarkedNodeKeysForKeep, walking parent chains, and splicing across multi-node boundaries. etrepum's WIP comment at the top of the method noted thatremoveText()is now independent ofinsertText, so the non-collapsed case can be decomposed intoremoveText()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 collapsedspliceTextpath (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
needsRedirectguard, and routes the remaining cases through format-diff detection →$insertTextAtPointor directspliceText.Three pre-existing bugs are fixed along the way:
setStyle(style)was missing when replacing a segmented node outside compositionsetStyle(style)was missing when creating a redirect node at offset 0canInsertTextAfter()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 passpnpm test-unit -- LexicalSelectionHelpers.test.ts— 51 pass