lexical
lexical copied to clipboard
Bug: $selectAll do not set the selection `style` parameter from the anchored text node
Lexical version: v0.16.1
Steps To Reproduce
{"editorState":{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #d0021b;","text":"This is red text","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}},"lastSaved":1721138622895,"source":"Playground","version":"0.16.1"}
Notes: in my use-case, this problem comes up as I run
$selectAll
. We can replicate that by putting the cursor on an empty line and pressing CMD/CTRL+A.
- Load the sample above in the playground
- Put the cursor on the second line (empty)
- Keyboard shortcut the select all command (CTRL+A)
Notice the selection values:
root
> ├ (54) paragraph
> | └ (55) text "This is red text"
> └ (56) paragraph
selection: range
├ anchor { key: 55, offset: 0, type: text }
└ focus { key: 56, offset: 0, type: element }
We can see here the selection is a range with the 55
text node as the anchor. This text node has a style attribute, but that isn't set in the selection.
The current behavior
Selection from $selectAll()
do not load the style
attribute from the anchored node. Thus starting to type will remove the styling attribute for subsequent text.
The expected behavior
Selection from $selectAll()
should load the style
attribute from the anchored node.
Impact of fix
I traced the fix down to the LexicalEvents.ts onSelectionChange handler.
https://github.com/facebook/lexical/blob/0fbab8fd38e8122a78cfaf1279f64e42e6ba5834/packages/lexical/src/LexicalEvents.ts#L362-L400
In this code, we can see that there is a // TODO what about styles
that pinpoint this exact issue.
I will send a PR with the fix I came up with.