removeSelected method

int removeSelected()

Remove every node in the current multi-selection (and their subtrees), as a single undoable step. Ancestors are dropped from the work-list so a parent + child selected together don't double-remove. Returns the count.

Implementation

int removeSelected() {
  if (_selection.isEmpty) return 0;
  // Keep only the topmost selected nodes (skip any whose ancestor is selected).
  final ids = _selection.where((id) {
    final anc = TreeOps.ancestorsOf<T>(_roots, id).toSet();
    return !anc.any(_selection.contains);
  }).toList();
  var next = _roots.toList();
  for (final id in ids) {
    next = TreeOps.removeNode<T>(next, id);
  }
  _apply(next);
  _selection.clear();
  _checked.removeAll(_checked.where((c) => TreeOps.find<T>(_roots, c) == null).toList());
  if (_selected != null && TreeOps.find<T>(_roots, _selected!) == null) _selected = null;
  _focused = _selected;
  return ids.length;
}