fix: improve flow layout for more complex flow

This commit is contained in:
Ruben Fiszel
2025-08-14 21:43:39 +00:00
parent af226162f5
commit eae0c09979
2 changed files with 20 additions and 1 deletions
@@ -17,6 +17,7 @@
import {
graphBuilder,
isTriggerStep,
topologicalSort,
type InlineScript,
type InsertKind,
type NodeLayout,
@@ -224,7 +225,7 @@
const nodes2 = nodes.map((n) => {
return { ...n, position: { x: 0, y: 0 } }
})
for (const n of nodes.reverse()) {
for (const n of topologicalSort(nodes)) {
const endId = n.id + '-end'
if (nodeWidths[endId] != undefined) {
@@ -291,6 +291,24 @@ export type AssetsOverflowedN = {
}
}
export function topologicalSort(nodes: NodeLayout[]): NodeLayout[] {
const nodeMap = new Map(nodes.map(n => [n.id, n]));
const result: NodeLayout[] = [];
const visited = new Set<string>();
function visit(id: string): void {
if (visited.has(id)) return;
visited.add(id);
const node = nodeMap.get(id)!;
node.parentIds?.forEach(visit);
result.push(node);
}
nodes.forEach(n => visit(n.id));
return result.reverse();
}
// input2: InputNode,
// module: ModuleNode,
// branchAllStart: BranchAllStart,