lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: glitches with combination of $getSelection, $getSelectionStyleValueForProperty and $patchStyleText

Open lwhiteley opened this issue 3 years ago • 9 comments

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

  1. Apply font-family style to text
  2. select another piece of text
  3. re-select the previously styled text
  4. The selection isnt read correctly
  5. Try applying more styles that require $patchStyleText,
  6. 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!

lwhiteley avatar Oct 07 '23 22:10 lwhiteley

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.

ivailop7 avatar Oct 08 '23 16:10 ivailop7

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

lwhiteley avatar Oct 08 '23 16:10 lwhiteley

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

lwhiteley avatar Oct 08 '23 16:10 lwhiteley

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: ❌

lwhiteley avatar Oct 08 '23 17:10 lwhiteley

Question: is the lexical source code in the playground slightly newer than the released version?

lwhiteley avatar Oct 08 '23 17:10 lwhiteley

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:

lexical_firefox_bug

Why would the offset be going wild in Firefox, but OK in Chromium?

ivailop7 avatar Oct 08 '23 21:10 ivailop7

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.

lwhiteley avatar Oct 09 '23 06:10 lwhiteley

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}

image

in the same editorState, when I look at the selection for key "4", it's showing the styles from key "3"

image

zaschrim avatar Nov 02 '23 21:11 zaschrim

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.

mayrang avatar Jul 15 '26 11:07 mayrang