Repository navigation
Conversation
…selection updates ## Description When Lexical moves the DOM selection itself and both points land inside a text node, the following selectionchange event is skipped as an echo. That early return also skipped SELECTION_CHANGE_COMMAND, so deleting a range inside a single text node never dispatched it. Dispatch the command in the skipped path too, matching the non-skipped path for DOM update echoes. Closes meta#9179 ## Test plan ### Before npx vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue8885SafariLinkSelectionChange.test.ts AssertionError: expected "vi.fn()" to be called 1 times, but got 0 times Tests 1 failed | 1 passed (2) ### After npx vitest --project browser --no-watch packages/lexical/src/__tests__/browser/Issue8885SafariLinkSelectionChange.test.ts Tests 2 passed (2)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
mayrang
left a comment
There was a problem hiding this comment.
Verified locally — both tests pass on the PR branch (Chromium).
The fix is correct. The root cause is clear: shouldSkipSelectionChange returning true caused an early exit before updateEditorSync was called, and since SELECTION_CHANGE_COMMAND is dispatched inside updateEditorSync in the non-skipped path, it was silently dropped.
One thing worth noting:
In the non-skipped path, SELECTION_CHANGE_COMMAND fires inside an updateEditorSync callback (L483), so listeners run as part of an active update — any editor.update() call inside a listener is batched synchronously with it.
With this fix, dispatchCommand is called at the event-handler level (outside any update), so listeners run in a separate update cycle. For the common case ($getSelection() lookups, toolbar state updates) this is fine — the selection committed by the delete operation is already readable. But listeners that call editor.update() will get a separate async cycle rather than batching.
This pattern does appear elsewhere in LexicalEvents.ts (e.g. L604, L962), so it's not unprecedented. Just worth a comment so future readers don't wonder why the dispatch lands before the return:
// Dispatch before returning: the non-skipped path emits this command
// inside updateEditorSync, but the selection was already committed by
// the prior update (e.g. DELETE_CHARACTER_COMMAND), so listeners will
// observe the correct state via $getSelection().
dispatchCommand(editor, SELECTION_CHANGE_COMMAND);
return;|
Thanks for verifying and for the detailed note. Added a short comment above the dispatch in 5bdbb59 explaining that the selection was already committed by the update that moved it, so listeners can read it outside of an update. |
|
dispatch always creates an update, so it's not outside of an update, but it is outside of the update where the selection was initially changed. While this is more correct than main, I think the most correct solution would fire the dispatch in the update where the selection is changed because that's when other selectionchange events for RangeSelection happen. I have a draft branch that consolidates all of the |
|
Thanks for the correction on the update timing, that makes sense. Consolidating the dispatch into the update where the selection changes is the better fix, happy for #9219 to supersede this one. |
Description
When Lexical moves the DOM selection itself, for example after deleting a range inside a single text node, the next
selectionchangeevent is skipped as an echo when both points land strictly inside a text node. That early return also skippedSELECTION_CHANGE_COMMAND, so the command fired or not depending on whether the new caret happened to sit at a text node boundary. The non-skipped path already dispatches the command for these DOM update echoes, so this dispatches it in the skipped path too.Closes #9179
Test plan
Before
Added a browser test to
Issue8885SafariLinkSelectionChange.test.tsthat selectsllo woinHello world, dispatchesDELETE_CHARACTER_COMMANDand waits forSELECTION_CHANGE_COMMAND.After
npx vitest --project unit --project browser --no-watchhas 4844 passing tests. The only failures are 3 tests inpackages/lexical-compiler(inline.test.ts,subpathImports.test.ts), which fail the same way onmainon my Windows machine.tsc, eslint and prettier pass. The browser tests ran in Chromium only, and I did not run the Playwright e2e suite.