Refactor fitView
This commit is contained in:
@@ -15,7 +15,15 @@ import useViewportHelper from './useViewportHelper';
|
||||
import { useStore, useStoreApi } from './useStore';
|
||||
import { useBatchContext } from '../components/BatchProvider';
|
||||
import { elementToRemoveChange, isEdge, isNode } from '../utils';
|
||||
import type { ReactFlowInstance, Node, Edge, InternalNode, ReactFlowState, GeneralHelpers } from '../types';
|
||||
import type {
|
||||
ReactFlowInstance,
|
||||
Node,
|
||||
Edge,
|
||||
InternalNode,
|
||||
ReactFlowState,
|
||||
GeneralHelpers,
|
||||
FitViewOptions,
|
||||
} from '../types';
|
||||
|
||||
const selector = (s: ReactFlowState) => !!s.panZoom;
|
||||
|
||||
@@ -271,6 +279,10 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
|
||||
.connectionLookup.get(`${nodeId}${type ? (handleId ? `-${type}-${handleId}` : `-${type}`) : ''}`)
|
||||
?.values() ?? []
|
||||
),
|
||||
fitView: (options: FitViewOptions<NodeType> | undefined) => {
|
||||
store.setState({ fitViewQueued: true, fitViewOptions: options });
|
||||
batchContext.nodeQueue.push((nodes) => [...nodes]);
|
||||
},
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -2,11 +2,8 @@ import { useMemo } from 'react';
|
||||
import {
|
||||
pointToRendererPoint,
|
||||
getViewportForBounds,
|
||||
getFitViewNodes,
|
||||
fitView,
|
||||
type XYPosition,
|
||||
rendererPointToPoint,
|
||||
getDimensions,
|
||||
SnapGrid,
|
||||
} from '@xyflow/system';
|
||||
|
||||
@@ -65,28 +62,6 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
||||
const [x, y, zoom] = store.getState().transform;
|
||||
return { x, y, zoom };
|
||||
},
|
||||
fitView: (options) => {
|
||||
const { nodeLookup, minZoom, maxZoom, panZoom, domNode } = store.getState();
|
||||
|
||||
if (!panZoom || !domNode) {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
const fitViewNodes = getFitViewNodes(nodeLookup, options);
|
||||
const { width, height } = getDimensions(domNode);
|
||||
|
||||
return fitView(
|
||||
{
|
||||
nodes: fitViewNodes,
|
||||
width,
|
||||
height,
|
||||
minZoom,
|
||||
maxZoom,
|
||||
panZoom,
|
||||
},
|
||||
options
|
||||
);
|
||||
},
|
||||
setCenter: async (x, y, options) => {
|
||||
const { width, height, maxZoom, panZoom } = store.getState();
|
||||
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : maxZoom;
|
||||
|
||||
Reference in New Issue
Block a user