Skip to content

Commit

Permalink
feat(pipelines): Add focus on newly expanded/collapsed groups (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-phillips-18 authored Jun 3, 2024
1 parent 6ee2109 commit 871677a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
39 changes: 30 additions & 9 deletions packages/module/src/elements/BaseGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,21 @@ export default class BaseGraph<E extends GraphModel = GraphModel, D = any>
this.setPosition(new Point(x, y));
}

fit(padding = 0): void {
fit(padding = 0, node?: Node): void {
let rect: Rect | undefined;
this.getNodes().forEach((c) => {
const b = c.getBounds();
if (!rect) {
rect = b.clone();
} else {
rect.union(b);
}
});
if (node) {
rect = node.getBounds();
} else {
this.getNodes().forEach((c) => {
const b = c.getBounds();
if (!rect) {
rect = b.clone();
} else {
rect.union(b);
}
});
}

if (!rect) {
return;
}
Expand Down Expand Up @@ -309,6 +314,22 @@ export default class BaseGraph<E extends GraphModel = GraphModel, D = any>
this.setPosition(new Point(tx, ty));
}

centerInView = (nodeElement: Node): void => {
if (!nodeElement) {
return;
}
const { x: viewX, y: viewY, width: viewWidth, height: viewHeight } = this.getBounds();
const boundingBox = nodeElement.getBounds().clone().scale(this.scale).translate(viewX, viewY);
const { x, y, width, height } = boundingBox;

const newLocation = {
x: viewX - (x + width / 2) + viewWidth / 2,
y: viewY - (y + height / 2) + viewHeight / 2
};

this.setBounds(new Rect(newLocation.x, newLocation.y, viewWidth, viewHeight));
};

panIntoView = (
nodeElement: Node,
{ offset = 0, minimumVisible = 0 }: { offset?: number; minimumVisible?: number } = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { observer } from 'mobx-react';
import { OnSelect, WithDndDragProps, ConnectDragSource, ConnectDropTarget } from '../../../behavior';
import { ShapeProps } from '../../../components';
import { Dimensions } from '../../../geom';
import { Dimensions, Rect } from '../../../geom';
import { GraphElement, LabelPosition, BadgeLocation, isNode, Node } from '../../../types';
import { action } from '../../../mobx-exports';
import { getEdgesFromNodes, getSpacerNodes } from '../../utils';
Expand Down Expand Up @@ -170,6 +170,33 @@ const DefaultTaskGroupInner: React.FunctionComponent<PipelinesDefaultGroupInnerP
}
}

const graph = group.getGraph();
if (graph) {
if (!collapsed) {
graph.fit(80, group);
} else {
// Get the current graph required size
let rect: Rect | undefined;
graph.getNodes().forEach((c) => {
const b = c.getBounds();
if (!rect) {
rect = b.clone();
} else {
rect.union(b);
}
});

// If the required size is smaller, zoom in to fit the current graph
const graphBounds = graph.getBounds();
if (rect.width < graphBounds.width || rect.height < graphBounds.height) {
graph.fit(80);
}

// Center the graph on the group that was collapsed
graph.centerInView(group);
}
}

onCollapseChange && onCollapseChange(group, collapsed);
});

Expand Down
3 changes: 2 additions & 1 deletion packages/module/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ export interface Graph<E extends GraphModel = GraphModel, D = any> extends Graph
// viewport operations
reset(): void;
scaleBy(scale: number, location?: Point): void;
fit(padding?: number): void;
fit(padding?: number, node?: Node): void;
centerInView(nodeElement: Node): void;
panIntoView(element: Node, options?: { offset?: number; minimumVisible?: number }): void;
isNodeInView(element: Node, options?: { padding: number }): boolean;
expandAll(): void;
Expand Down

0 comments on commit 871677a

Please sign in to comment.