loadGraph method
Loads a complete graph into the controller.
This method:
- Clears the existing graph state
- Bulk loads all nodes and connections
- Sets the viewport to match the saved state
- Sets up visual positioning and hit-testing infrastructure (if editor initialized)
This is the preferred method for loading saved graphs as it performs efficient bulk loading rather than individual additions.
Note: If the editor is not yet initialized (i.e., _initController hasn't
been called), the infrastructure setup is deferred until initialization.
This is the correct behavior for graphs loaded before the editor widget mounts.
Parameters:
graph: The graph to load containing nodes, connections, and viewport state
Example:
final graph = NodeGraph<MyData>(
nodes: savedNodes,
connections: savedConnections,
viewport: savedViewport,
);
controller.loadGraph(graph);
Implementation
void loadGraph(NodeGraph<T> graph) {
runInAction(() {
// Clear existing state
clearGraph();
// Bulk load all data structures
for (final node in graph.nodes) {
_nodes[node.id] = node;
}
_connections.addAll(graph.connections);
// Set viewport
_viewport.value = graph.viewport;
// Set up infrastructure if editor is already initialized.
// If not initialized yet, _initController will handle this when called.
if (_editorInitialized) {
_initializeLoadedNodes();
_rebuildSpatialIndexes();
}
});
}