Bug: glitches with combination of $getSelection, $getSelectionStyleValueForProperty and $patchStyleText
Im having a warm time figuring out what the issue with this process is.
Problem: When a style is applied to text and the user tries to reselect and change that style, then the selection isnt updated or additional style changes are not reflected in the selection.
I thought it was something I did but realised the problem is also in the Lexial Playground
https://github.com/facebook/lexical/assets/6108793/db03c0de-2278-4afa-81dd-704aeb0e9e0a
Things to take note of:
- Font family disappears from toolbar
- Doesnt re-appear while selection is active and user keeps changing the font family
- new style applied but selection doesnt know about it
Lexical version: 0.12.2
Steps To Reproduce
- Apply font-family style to text
- select another piece of text
- re-select the previously styled text
- The selection isnt read correctly
- Try applying more styles that require
$patchStyleText, - Nothing happens
The current behavior
Selection isnt updated if reselecting a piece of text that had styles previously applied to it
The expected behavior
Should be able to apply styles at any time to any formatted text and see the selection data updated
Any help would be appreciated.
Thanks!
I think this is because you are running on Linux and the listed fonts in the dropdown - Times New Roman, etc not all of them are preinstalled on Linux. So a fallback font is applied, but since the code doesn't locate the failover font, the selection in the dropdown menu breaks. A way to confirm this is to change the font face options to fonts that your Linux distro has installed and see if the issue persists.
I don't think it has anything to do with the fonts.. you can test it with font sizes too.. the same thing happens
https://github.com/facebook/lexical/assets/6108793/33d660a7-ee3c-4086-98b2-af0565793f7b
Additional note: in the implementation I did, I used styles for bold, italic, strike through.
And the same thing happens
Also if I blur the editor and come back with a collapsed selection, the correct style is found by the selection
But good hint about the OS
I did a bit more digging:
Lexical Playground: Windows Chrome: ✅ Linux Chrome: ✅ Linux Firefox: ❌ Windows Firefox: ❌
My custom implementation:
Windows Chrome: : ❌ Linux Chrome: : ❌ Linux Firefox: ❌ Windows Firefox: ❌
Question: is the lexical source code in the playground slightly newer than the released version?
Yes, the repo code is always ahead of the published version, but not to an extent where this would be broken/fixed in the latest released version. I reproduced this in Firefox on Windows as well. I need to summon the Lexical gurus - @fantactuka and @zurfyx , to summarize selection is wrong in Firefox, I've added a screenshot for the outcome from identical operations of changing the font size:
Why would the offset be going wild in Firefox, but OK in Chromium?
i see.. Thanks for checking.
i asked because there is at least one change regarding selection since the last release and there is one difference between the playground results and mine.
https://github.com/facebook/lexical/commit/816ef987f161e1b22eba73f065e101544b4e941c
Also another thing i noticed while developing is that whenever there is an unexpected exception, the editor has some weird behaviours like this but without bubbling up the error. Could be unrelated but just a note.
I'm seeing similar issues.
In the editorState, if I look at my currently selected node in the _nodeMap with the key "4" it shows the style of {font-size: 20px}
in the same editorState, when I look at the selection for key "4", it's showing the styles from key "3"
This was fixed in #5082 (merged 2023-10-04, included since v0.12.3). The root cause was that $getSelectionStyleValueForProperty returned selection.style even for non-collapsed selections, but selection.style is only updated for collapsed selections — so re-selecting styled text with a range selection would return stale style values from the previous selection.
Additionally, #5087 and #6223 fixed related issues where $patchStyleText would lose the selection anchor/focus after splitting text nodes.
I verified on the current codebase (v0.47.0) that the reported scenario no longer reproduces — both in unit tests and in the Playground on Firefox.