lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: RangeSelection.is method does not always detect that selections have different formats

Open GermanJablo opened this issue 2 years ago • 1 comments

I noticed this because my toolbar buttons do not activate when appropriate.

The reason they are activated in the playground is because an unnecessary registerListener is being used in addition to the SELECTION_CHANGE_COMMAND.

I think the cause of the bug has to do with the way the formats are encoded.

// Text node formatting
export const IS_BOLD = 1;
export const IS_ITALIC = 1 << 1;
export const IS_STRIKETHROUGH = 1 << 2;
export const IS_UNDERLINE = 1 << 3;
export const IS_CODE = 1 << 4;
export const IS_SUBSCRIPT = 1 << 5;
export const IS_SUPERSCRIPT = 1 << 6;
export const IS_HIGHLIGHT = 1 << 7;

Lexical version: 0.12.2

Steps To Reproduce

  1. Select text and play with the bold, italic, underline and strikethrough buttons.
  2. Notice how the console.log is fired only when the pressed button has no other buttons to the left active, and is being switched to active mode.

Link to code example: https://codesandbox.io/s/ecstatic-breeze-6mzm37?file=/src/Editor.js

The current behavior

RangeSelection.is method does not always detect that selections have different formats

The expected behavior

RangeSelection.is method should always detect that selections have different formats.

GermanJablo avatar Sep 20 '23 15:09 GermanJablo

Plus one!! I think you've probably hit it on the head.

My "SELECTION_CHANGE_COMMAND" was triggering for "bold" and "italic" as expected but not for "underline". Then if I first bold, the same text block, toggling underline does actually trigger the selection change event as expected.

I guess the main issue is the behavour isn't consistent. I could add another listener but it would be nice if they all worked using the same "SELECTION_CHANGE_COMMAND" listener.

rodhoward avatar Aug 23 '24 10:08 rodhoward