lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Composition breaks when composing on a segmented node

Open zurfyx opened this issue 3 years ago • 2 comments

Notice how composition never trigger for the first character until it's converted to a plain text node

https://github.com/facebook/lexical/assets/193447/b3f3c3f1-98bc-4823-b1fe-ede7d47fa35e

zurfyx avatar Sep 28 '23 20:09 zurfyx

My hunch is that the span can never go away (replaced), we have to reuse the same DOM element

zurfyx avatar Sep 28 '23 20:09 zurfyx

PR #8741 adds a $handleCompositionStart guard that inserts COMPOSITION_START_CHAR when composition begins on a token or segmented node. This redirects composition to an adjacent TextNode, so the segmented node's DOM isn't destroyed mid-composition — the breakage shown in the video no longer happens.

There's one remaining visual quirk on Chrome: when composition starts in the middle of a segmented node, the cursor briefly appears before the first composed character instead of after it. Second character onward is normal, and the final text placement is correct. The root cause is that insertText replaces the segmented node with a plain TextNode (line 991 in LexicalSelection.ts), which swaps the DOM element under Chrome's composition tracker. Fixing this would require keeping the original DOM element alive during composition — essentially what @zurfyx suggested — but that needs an isComposing guard in $updateTextNodeFromDOMContent's segmented replacement path, and the interaction with Firefox composition handling makes it non-trivial. I'll revisit this after #8741 merges when I have time.

mayrang avatar Jun 26 '26 12:06 mayrang