distributeNodesVertically method
Distributes nodes evenly along the vertical axis.
Requires at least 3 nodes. Sorts nodes by Y position, keeps the topmost and bottommost nodes in place, and distributes the middle nodes evenly.
Implementation
void distributeNodesVertically(List<String> nodeIds) {
if (nodeIds.length < 3) return;
final nodes = nodeIds.map((id) => _nodes[id]).whereType<Node<T>>().toList();
if (nodes.length < 3) return;
nodes.sort((a, b) => a.position.value.dy.compareTo(b.position.value.dy));
final topmost = nodes.first.position.value.dy;
final bottommost = nodes.last.position.value.dy;
final spacing = (bottommost - topmost) / (nodes.length - 1);
runInAction(() {
for (int i = 1; i < nodes.length - 1; i++) {
final targetY = topmost + spacing * i;
final newPosition = Offset(nodes[i].position.value.dx, targetY);
nodes[i].position.value = newPosition;
nodes[i].setVisualPosition(_config.snapToGridIfEnabled(newPosition));
}
});
internalMarkNodesDirty(nodeIds);
rebuildConnectionSegmentsForNodes(nodeIds);
}