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.
Parameters:
nodeId: The ID of the node to remove
Implementation
void removeNode(String nodeId) {
final nodeToDelete = _nodes[nodeId]; // Capture before deletion
runInAction(() {
_nodes.remove(nodeId);
_selectedNodeIds.remove(nodeId);
// Remove connections involving this node
_connections.removeWhere(
(c) => c.sourceNodeId == nodeId || c.targetNodeId == nodeId,
);
// Clean up empty group annotations
// Find all group annotations that contain this node
final groupsToCheck = <String>[];
for (final annotation in annotations.annotations.values) {
if (annotation is GroupAnnotation &&
annotation.dependencies.contains(nodeId)) {
// Remove the node from the group's dependencies
annotation.dependencies.remove(nodeId);
groupsToCheck.add(annotation.id);
}
}
// Remove any groups that are now empty
for (final groupId in groupsToCheck) {
final group = annotations.getAnnotation(groupId);
if (group is GroupAnnotation && group.dependencies.isEmpty) {
annotations.removeAnnotation(groupId);
}
}
});
// Fire callback after successful removal
if (nodeToDelete != null) {
callbacks.onNodeDeleted?.call(nodeToDelete);
}
}