package(core): use system fitView

This commit is contained in:
moklick
2023-02-19 16:15:26 +01:00
parent 6f8782bbf6
commit 0f62a3d07a
3 changed files with 49 additions and 75 deletions
+23 -3
View File
@@ -1,11 +1,10 @@
import { useMemo } from 'react';
import { zoomIdentity } from 'd3-zoom';
import { shallow } from 'zustand/shallow';
import { pointToRendererPoint, getTransformForBounds, getD3Transition } from '@reactflow/utils';
import { pointToRendererPoint, getTransformForBounds, getD3Transition, fitView } from '@reactflow/utils';
import type { ViewportHelperFunctions, ReactFlowState, XYPosition } from '@reactflow/system';
import { useStoreApi, useStore } from '../hooks/useStore';
import { fitView } from '../store/utils';
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
@@ -51,7 +50,28 @@ const useViewportHelper = (): ViewportHelperFunctions => {
const [x, y, zoom] = store.getState().transform;
return { x, y, zoom };
},
fitView: (options) => fitView(store.getState, options),
fitView: (options) => {
const { getNodes, width, height, nodeOrigin, minZoom, maxZoom, d3Selection, d3Zoom } = store.getState();
const d3Initialized = d3Selection && d3Zoom;
if (!d3Initialized) {
return false;
}
return fitView(
{
nodes: getNodes(),
width,
height,
nodeOrigin,
minZoom,
maxZoom,
d3Selection,
d3Zoom,
},
options
);
},
setCenter: (x, y, options) => {
const { width, height, maxZoom } = store.getState();
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : maxZoom;