Bug: hasFormat in RangeSelection is not consistent with dispatchCommand
When set a text style on part of text and selection goes over, hasFormat return false but dispatchCommand remove format of text. Ex: in playground write two word like "Bug Demo" and select "Bug" and press Bold button, now select "Bug Demo" you will see Bold button shows text is not bold so you expected when click on it all select text will become bold but Bold button removes bold style of all text.
Lexical version: Playground version
Steps To Reproduce
- Write "Bug Demo" in playground
- Make "Bug" bold
- Select "Bug Demo"
- Press Bold button
Link to code example: https://playground.lexical.dev/#doc=H4sIAAAAAAAAA52QQWvDMAyF_8rQ2RSn3botx1J6HuQ4ehCxlhocKyhKWSj577PTjq5jjLGT_R7S9ySdgJxXlkpRCcoTCLPmtz744IQilK_fhCNFH6C0Bt5YWkzlhYGWXeqHmJ0ABnodQzbSV-k91cBmaLIYu2zPnoEjSe85cYvJ_Ei2fyHfbanl39h7A84L1TprCCpwjcggHx3Fc9wF0qFgI9gdbkjnyN117Syrz4H-kzPf-2bYyUDAXis8kkv6cbV6Wj6sn--tXafleZA6970EHBvhIbqv3WAXy2JhYfoAl-I3i9gBAAA
The current behavior
Bold button shows text is not bold but when click on it, it removes style of bold texts.
The expected behavior
Bold button shows text is bold Or click on Bold button add bold format to all selected text
Looks like there are two issues here:
- The documentation for RangeSelection.hasFormat is inconsistent with the implementation. It says "This will be true if any node in the Selection has the specified format." but the implementation is "This will be true if all nodes in the selection have the specified format".
- FORMAT_TEXT_COMMAND (which calls RangeSelection.formatText) always toggles the format of the first node (in document order) and sets the format of other nodes to match that one
It would probably make more sense to have a command that sets the format specifically to some value, e.g. that takes a { [k in TextFormatType]?: undefined | boolean } as the payload. This way you could dispatch exactly how you expect the format to change and it wouldn't be dependent on whatever format the first node happens to have.