addNodeFromExisting method
This method is used to add a node from an existing node object.
This method is used when loading a project from a file or in copy/paste operations and preserves all properties of the node object.
Emits an AddNodeEvent event.
Implementation
void addNodeFromExisting(
NodeInstance node, {
bool isHandled = false,
String? eventId,
}) {
if (nodes.containsKey(node.id)) return;
Offset offset = node.offset;
if (config.enableSnapToGrid) {
offset = Offset(
(offset.dx / config.snapToGridSize).round() * config.snapToGridSize,
(offset.dy / config.snapToGridSize).round() * config.snapToGridSize,
);
}
nodes.putIfAbsent(node.id, () => node.copyWith(offset: offset));
_unboundNodeOffsets.putIfAbsent(node.id, () => node.offset);
if (node.state.isSelected) selectedNodeIds.add(node.id);
nodesDataDirty = true;
eventBus.emit(
AddNodeEvent(
id: eventId ?? const Uuid().v4(),
node,
isHandled: isHandled,
),
);
for (final port in node.ports.values) {
for (final link in port.links) {
addLinkFromExisting(link, isHandled: isHandled);
}
}
}