$rescueTableSelection function

void $rescueTableSelection(
  1. TableNode table
)

Puts the caret back in table when the cell it was in has been removed.

Deleting the row or column the caret happens to be in is the ordinary case, not an edge one — and a selection left pointing at detached nodes is an error the core refuses outright, so every structural removal has to end with the caret somewhere that still exists.

Must be called inside an update.

Implementation

void $rescueTableSelection(TableNode table) {
  final selection = $getSelection();
  if (selection == null) return;
  final nodes = selection.getNodes();
  if (nodes.isNotEmpty && nodes.every((node) => node.isAttached)) return;

  for (final row in table.children.whereType<TableRowNode>()) {
    final cell = row.getFirstChild();
    if (cell is TableCellNode) {
      $selectTableCellStart(cell);
      return;
    }
  }
  // A table with no cell left is not a table; the caller is removing it.
  table.selectPrevious();
}