Skip to content

Commit

Permalink
Add string type input to fitNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsteff committed Jul 10, 2024
1 parent bd768fe commit f2e6461
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/Advanced/Refs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface CanvasRef {
/**
* Fit a group of nodes to the viewport.
*/
fitNodes?: (nodeIds: string[], animated?: boolean) => void;
fitNodes?: (nodeIds: string | string[], animated?: boolean) => void;

/**
* Scroll to X/Y
Expand Down
66 changes: 33 additions & 33 deletions src/layout/useLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface LayoutResult {
/**
* Fit a group of nodes to the viewport.
*/
fitNodes?: (nodeIds: string[], animated?: boolean) => void;
fitNodes?: (nodeIds: string | string[], animated?: boolean) => void;

/**
* Scroll to X/Y
Expand Down Expand Up @@ -138,21 +138,21 @@ export const useLayout = ({ maxWidth, maxHeight, nodes = [], edges = [], fit, pa
const centerX = (canvasWidth - layout.width * zoom) / 2;
const centerY = (canvasHeight - layout.height * zoom) / 2;
switch (position) {
case CanvasPosition.CENTER:
setXY([centerX, centerY]);
break;
case CanvasPosition.TOP:
setXY([centerX, 0]);
break;
case CanvasPosition.LEFT:
setXY([0, centerY]);
break;
case CanvasPosition.RIGHT:
setXY([canvasWidth - layout.width * zoom, centerY]);
break;
case CanvasPosition.BOTTOM:
setXY([centerX, canvasHeight - layout.height * zoom]);
break;
case CanvasPosition.CENTER:
setXY([centerX, centerY]);
break;
case CanvasPosition.TOP:
setXY([centerX, 0]);
break;
case CanvasPosition.LEFT:
setXY([0, centerY]);
break;
case CanvasPosition.RIGHT:
setXY([canvasWidth - layout.width * zoom, centerY]);
break;
case CanvasPosition.BOTTOM:
setXY([centerX, canvasHeight - layout.height * zoom]);
break;
}
}
},
Expand All @@ -165,21 +165,21 @@ export const useLayout = ({ maxWidth, maxHeight, nodes = [], edges = [], fit, pa
const scrollCenterY = (canvasHeight - height) / 2;
if (pannable) {
switch (position) {
case CanvasPosition.CENTER:
scrollToXY([scrollCenterX, scrollCenterY], animated);
break;
case CanvasPosition.TOP:
scrollToXY([scrollCenterX, 0], animated);
break;
case CanvasPosition.LEFT:
scrollToXY([0, scrollCenterY], animated);
break;
case CanvasPosition.RIGHT:
scrollToXY([canvasWidth - width, scrollCenterY], animated);
break;
case CanvasPosition.BOTTOM:
scrollToXY([scrollCenterX, canvasHeight - height], animated);
break;
case CanvasPosition.CENTER:
scrollToXY([scrollCenterX, scrollCenterY], animated);
break;
case CanvasPosition.TOP:
scrollToXY([scrollCenterX, 0], animated);
break;
case CanvasPosition.LEFT:
scrollToXY([0, scrollCenterY], animated);
break;
case CanvasPosition.RIGHT:
scrollToXY([canvasWidth - width, scrollCenterY], animated);
break;
case CanvasPosition.BOTTOM:
scrollToXY([scrollCenterX, canvasHeight - height], animated);
break;
}
}
},
Expand Down Expand Up @@ -217,9 +217,9 @@ export const useLayout = ({ maxWidth, maxHeight, nodes = [], edges = [], fit, pa
* This centers the chart on the canvas, zooms in to fit the specified nodes, and scrolls to center the nodes in the viewport
*/
const fitNodes = useCallback(
(nodeIds: string[], animated = true) => {
(nodeIds: string | string[], animated = true) => {
if (layout && layout.children) {
const nodes = nodeIds.map((nodeId) => findNode(layout.children, nodeId));
const nodes = Array.isArray(nodeIds) ? nodeIds.map((nodeId) => findNode(layout.children, nodeId)) : [findNode(layout.children, nodeIds)];

if (nodes) {
// center the chart
Expand Down
4 changes: 2 additions & 2 deletions stories/Controls.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ export const Zoom = () => {
<button style={{ display: 'block', width: '100%', margin: '5px 0' }} onClick={() => ref.current.zoomIn()}>Zoom In</button>
<button style={{ display: 'block', width: '100%', margin: '5px 0' }} onClick={() => ref.current.zoomOut()}>Zoom Out</button>
<button style={{ display: 'block', width: '100%' }} onClick={() => ref.current.fitCanvas(true)}>Fit</button>
<button style={{ display: 'block', width: '100%' }} onClick={() => ref.current?.fitNodes(['1'])}>Fit to Node 1</button>
<button style={{ display: 'block', width: '100%' }} onClick={() => ref.current?.fitNodes(['2'])}>Fit to Node 2</button>
<button style={{ display: 'block', width: '100%' }} onClick={() => ref.current?.fitNodes('1')}>Fit to Node 1</button>
<button style={{ display: 'block', width: '100%' }} onClick={() => ref.current?.fitNodes('2')}>Fit to Node 2</button>
</pre>
<Canvas
maxZoom={10}
Expand Down

0 comments on commit f2e6461

Please sign in to comment.