removeNode method
Removes a node from the graph along with all its connections.
This method will:
- Remove the node from the graph
- Remove it from the selection if selected
- Remove all connections involving this node
- Remove the node from any group annotations
- Delete empty group annotations that no longer contain any nodes
Triggers the onNodeDeleted callback after successful removal.
Implementation
void removeNode(String nodeId) {
final nodeToDelete = _nodes[nodeId]; // Capture before deletion
runInAction(() {
_nodes.remove(nodeId);
_selectedNodeIds.remove(nodeId);
// Remove from spatial index
_spatialIndex.removeNode(nodeId);
// Remove connections involving this node from spatial index first
final connectionsToRemove = _connections
.where((c) => c.sourceNodeId == nodeId || c.targetNodeId == nodeId)
.toList();
for (final connection in connectionsToRemove) {
_spatialIndex.removeConnection(connection.id);
// Also remove from path cache to prevent stale rendering
_connectionPainter?.removeConnectionFromCache(connection.id);
}
// Then remove from connections list
_connections.removeWhere(
(c) => c.sourceNodeId == nodeId || c.targetNodeId == nodeId,
);
// Note: Annotations are notified via MobX reaction in AnnotationController
// that watches _nodes.keys for additions/deletions
});
// Fire event after successful removal
if (nodeToDelete != null) {
events.node?.onDeleted?.call(nodeToDelete);
}
}