lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: selection points to missing nodes after calling $deleteTableRow__EXPERIMENTAL() on all table rows

Open mike-atticus opened this issue 11 months ago • 2 comments

Calling $deleteTableRow__EXPERIMENTAL() when all table rows are selected produces an editor state (processed by update listeners) that has an invalid selection. The selection points to nodes that have been removed from the editor, so if you call $getSelection()?.getNodes() you will get an error Point.getNode: node not found.

Note: in the recording below the rendered HTML table doesn't show the selection, but the tree view below the editor shows the table cells are getting selected.

https://github.com/user-attachments/assets/30f574d1-1684-4672-a006-c814342307fc

Lexical version: 0.23.1

Steps To Reproduce

  1. Have an editor state with a table and at least one other sibling node before it (e.g. paragraph).
  2. Have an update listener that calls $getSelection?.getNodes() on every update.
  3. Select all rows of the table, then take an action that runs $deleteTableRow__EXPERIMENTAL().
  4. Observe the error Point.getNode: node not found is thrown.

Link to code example: https://stackblitz.com/edit/facebook-lexical-akmwfitx?file=src%2FApp.tsx

The current behavior

The error Point.getNode: node not found is thrown because the selection is pointing to nodes that have been removed from the document.

The expected behavior

The selection should be fixed up (pointing to valid nodes) before update listeners are called with new updates.

Impact of fix

The most severe impact of this bug exists when you also use CollaborationPlugin, as this error causes Lexical and the Yjs doc to get out of sync. The table deletion doesn't propagate to the Yjs doc, so until the page is refreshed there is risk that user edits are corrupting the document in this unexpected state, and upon refresh users will see the table re-appear unexpectedly.

As a workaround, the error can be caught and handled to avoid this problem.

mike-atticus avatar Feb 06 '25 11:02 mike-atticus

I haven't fixed the problem but here are the relevant changes to the theme so you can see the selection properly

https://stackblitz.com/edit/facebook-lexical-akmwfitx?file=src%2Fstyles.css,src%2FExampleTheme.ts,index.html,src%2Fmain.tsx

etrepum avatar Feb 06 '25 23:02 etrepum

Thanks! Have updated the link in the original description.

mike-atticus avatar Feb 07 '25 00:02 mike-atticus