-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend Map: Move viewport logic to useGrpahViewport hook
Signed-off-by: Oleksandr Dubenko <[email protected]>
- Loading branch information
Showing
3 changed files
with
102 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { getNodesBounds, getViewportForBounds, Node, useReactFlow, useStore } from '@xyflow/react'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { maxZoom, minZoom, viewportPaddingPx } from './graphConstants'; | ||
|
||
/** | ||
* Zoom Mode represents different approaches to viewport calculation | ||
* | ||
* - 100% (default) | ||
* Will try to fit nodes without exceeding 100% zoom | ||
* Often results in content overflowing but keeps text readable | ||
* | ||
* - Fit | ||
* Will show everything and zoom out as needed | ||
*/ | ||
type zoomMode = '100%' | 'fit'; | ||
|
||
/** Helper hook to deal with viewport zooming */ | ||
export const useGraphViewport = () => { | ||
const reactFlowWidth = useStore(it => it.width); | ||
const reactFlowHeight = useStore(it => it.height); | ||
const aspectRatio = useStore(it => it.width / it.height); | ||
const flow = useReactFlow(); | ||
|
||
const updateViewport = useCallback( | ||
({ | ||
nodes = flow.getNodes(), | ||
mode = '100%', | ||
}: { | ||
/** List of nodes, if not provided will use current nodes in the graph */ | ||
nodes?: Node[]; | ||
/** Zoom mode. More info in the type definition {@link zoomMode} */ | ||
mode?: zoomMode; | ||
}) => { | ||
const bounds = getNodesBounds(nodes); | ||
|
||
if (mode === 'fit') { | ||
const viewport = getViewportForBounds( | ||
{ | ||
x: bounds.x - viewportPaddingPx, | ||
y: bounds.y - viewportPaddingPx, | ||
width: bounds.width + viewportPaddingPx * 2, | ||
height: bounds.height + viewportPaddingPx * 2, | ||
}, | ||
reactFlowWidth, | ||
reactFlowHeight, | ||
minZoom, | ||
maxZoom, | ||
0 | ||
); | ||
|
||
flow.setViewport(viewport); | ||
return; | ||
} | ||
|
||
if (mode === '100%') { | ||
const topLeftOrigin = { x: viewportPaddingPx, y: viewportPaddingPx }; | ||
const centerOrigin = { | ||
x: reactFlowWidth / 2 - bounds.width / 2, | ||
y: reactFlowHeight / 2 - bounds.height / 2, | ||
}; | ||
|
||
const xFits = bounds.width + viewportPaddingPx * 2 <= reactFlowWidth; | ||
const yFits = bounds.height + viewportPaddingPx * 2 <= reactFlowHeight; | ||
|
||
const defaultZoomViewport = { | ||
x: xFits ? centerOrigin.x : topLeftOrigin.x, | ||
y: yFits ? centerOrigin.y : topLeftOrigin.y, | ||
zoom: 1, | ||
}; | ||
|
||
flow.setViewport(defaultZoomViewport); | ||
return; | ||
} | ||
|
||
console.error('Unknown zoom mode', mode); | ||
}, | ||
[flow, reactFlowWidth, reactFlowHeight] | ||
); | ||
|
||
return useMemo(() => ({ updateViewport, aspectRatio }), [updateViewport, aspectRatio]); | ||
}; |