Skip to content

Commit

Permalink
fix(DagreGroupLayout): Keep node order when groups are collapsed (pat…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-phillips-18 committed Oct 25, 2024
1 parent d37a664 commit be4fc91
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/module/src/layouts/DagreGroupsLayout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as dagre from '@dagrejs/dagre';
import { Edge, Graph, GRAPH_LAYOUT_END_EVENT, Layout, Node } from '../types';
import { Edge, Graph, GRAPH_LAYOUT_END_EVENT, isNode, Layout, Node } from '../types';
import { BaseLayout, LAYOUT_DEFAULTS } from './BaseLayout';
import { LayoutLink } from './LayoutLink';
import { LayoutNode } from './LayoutNode';
Expand Down Expand Up @@ -120,7 +120,7 @@ export class DagreGroupsLayout extends BaseLayout implements Layout {
(n) =>
n.element.getParent()?.getId() === parentGroup?.id ||
(!parentGroup && n.element.getParent()?.getId() === graph.getId())
);
) as DagreNode[];
const layerEdges = this.edges.filter(
(edge) =>
(layerGroups.find((n) => n.id === edge.sourceNode.id) ||
Expand All @@ -129,20 +129,30 @@ export class DagreGroupsLayout extends BaseLayout implements Layout {
layerNodes.find((n) => n.id === edge.targetNode.id))
);

const dagreNodes: DagreNode[] = [];

// Layout any child groups first
layerGroups.forEach((group) => {
doLayout(group);

// Add the child group node (now with the correct dimensions) to the graph
const dagreNode = new DagreNode(group.element, group.padding);
const updateNode = dagreNode.getUpdatableNode();
dagreGraph.setNode(group.id, updateNode);
dagreNodes.push(dagreNode);
});

layerNodes?.forEach((node) => {
const updateNode = (node as DagreNode).getUpdatableNode();
dagreGraph.setNode(node.id, updateNode);
});
dagreNodes.push(...layerNodes);

// Set the nodes in the graph in the same order give to maintain ordering when groups are collapsed
this.graph
.getController()
.getElements()
.filter((e) => isNode(e))
.forEach((node) => {
const updateNode = dagreNodes.find((dagreNode) => dagreNode.id === node.getId());
if (updateNode) {
dagreGraph.setNode(updateNode.id, updateNode.getUpdatableNode());
}
});

layerEdges?.forEach((dagreEdge) => {
dagreGraph.setEdge(dagreEdge.source.id, dagreEdge.target.id, dagreEdge);
Expand Down

0 comments on commit be4fc91

Please sign in to comment.