Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DagreGroupLayout): Keep node order when groups are collapsed #231

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading