Skip to content

Commit

Permalink
Added fitViewWithDimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Jaśpiński committed Aug 18, 2021
1 parent ad72546 commit 395e937
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 55 deletions.
76 changes: 53 additions & 23 deletions dist/ReactFlow.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ReactFlow.esm.js.map

Large diffs are not rendered by default.

76 changes: 53 additions & 23 deletions dist/ReactFlow.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ReactFlow.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { CSSProperties, MouseEvent as ReactMouseEvent, HTMLAttributes, ReactNode } from 'react';
import { Selection as D3Selection, ZoomBehavior } from 'd3';
import React, { CSSProperties, HTMLAttributes, MouseEvent as ReactMouseEvent, ReactNode } from 'react';
export declare type ElementId = string;
export declare type FlowElement<T = any> = Node<T> | Edge<T>;
export declare type Elements<T = any> = Array<FlowElement<T>>;
Expand Down Expand Up @@ -229,6 +229,14 @@ export declare type FitViewParams = {
minZoom?: number;
maxZoom?: number;
};
export declare type FitViewToDimensionsParams = {
width: number;
height: number;
padding?: number;
includeHiddenNodes?: boolean;
minZoom?: number;
maxZoom?: number;
};
export declare type FlowExportObject<T = any> = {
elements: Elements<T>;
position: [number, number];
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useResizeHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect, MutableRefObject } from 'react';
import { MutableRefObject, useEffect } from 'react';
import { useStoreActions } from '../store/hooks';

import { getDimensions } from '../utils';


export default (rendererNode: MutableRefObject<HTMLDivElement | null>) => {
const updateSize = useStoreActions((actions) => actions.updateSize);

Expand Down
23 changes: 21 additions & 2 deletions src/hooks/useZoomPanHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { zoomIdentity } from 'd3-zoom';
import { useMemo } from 'react';
import { useStore, useStoreState } from '../store/hooks';
import { FitViewParams, FlowTransform, Rect, XYPosition, ZoomPanHelperFunctions } from '../types';
import { FitViewParams, FitViewToDimensionsParams, FlowTransform, Rect, XYPosition, ZoomPanHelperFunctions } from '../types';
import { getRectOfNodes, getTransformForBounds, pointToRendererPoint } from '../utils/graph';


Expand Down Expand Up @@ -37,7 +37,6 @@ const useZoomPanHelper = (): ZoomPanHelperFunctions => {
},
fitView: (options: FitViewParams = { padding: DEFAULT_PADDING, includeHiddenNodes: false }) => {
const { nodes, width, height, minZoom, maxZoom } = store.getState();
console.log('fitView gets from store:',{height, width})

if (!nodes.length) {
return;
Expand All @@ -56,6 +55,26 @@ const useZoomPanHelper = (): ZoomPanHelperFunctions => {

d3Zoom.transform(d3Selection, transform);
},
fitViewToDimensions: (options: FitViewToDimensionsParams = { height: 0, width: 0, padding: DEFAULT_PADDING, includeHiddenNodes: false }) => {
const { nodes, minZoom, maxZoom } = store.getState();

if (!nodes.length) {
return;
}

const bounds = getRectOfNodes(options.includeHiddenNodes ? nodes : nodes.filter((node) => !node.isHidden));
const [x, y, zoom] = getTransformForBounds(
bounds,
options.width,
options.height,
options.minZoom ?? minZoom,
options.maxZoom ?? maxZoom,
options.padding ?? DEFAULT_PADDING
);
const transform = zoomIdentity.translate(x, y).scale(zoom);

d3Zoom.transform(d3Selection, transform);
},
setCenter: (x: number, y: number, zoom?: number) => {
const { width, height, maxZoom } = store.getState();

Expand Down
11 changes: 10 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { CSSProperties, MouseEvent as ReactMouseEvent, HTMLAttributes, ReactNode } from 'react';
import { Selection as D3Selection, ZoomBehavior } from 'd3';
import React, { CSSProperties, HTMLAttributes, MouseEvent as ReactMouseEvent, ReactNode } from 'react';

export type ElementId = string;

Expand Down Expand Up @@ -252,6 +252,15 @@ export type FitViewParams = {
maxZoom?: number;
};

export type FitViewToDimensionsParams = {
width: number,
height: number,
padding?: number;
includeHiddenNodes?: boolean;
minZoom?: number;
maxZoom?: number;
};

export type FlowExportObject<T = any> = {
elements: Elements<T>;
position: [number, number];
Expand Down
5 changes: 4 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const isInputDOMNode = (e: ReactMouseEvent | DraggableEvent | KeyboardEve
};

export const getDimensions = (node: HTMLDivElement): Dimensions => {
const rect = node.getBoundingClientRect();
const rect = {
width: node.offsetWidth,
height: node.offsetHeight,
};
console.log('getDimensions returns:',{width: rect.width, height: rect.height})
return { height: rect.height, width: rect.width}
}
Expand Down

0 comments on commit 395e937

Please sign in to comment.