Merge pull request #2749 from wbkd/node-resizer/before-resize

Node-resizer: add shouldResize handler
This commit is contained in:
Moritz Klack
2023-01-23 18:07:19 +01:00
committed by GitHub
12 changed files with 136 additions and 18 deletions
+1 -1
View File
@@ -38,7 +38,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@reactflow/core": "workspace:*",
"@reactflow/core": "workspace:^11.3.3",
"classcat": "^5.0.4",
"d3-drag": "^3.0.0",
"d3-selection": "^3.0.0",
@@ -14,6 +14,7 @@ export default function NodeResizer({
color,
minWidth = 10,
minHeight = 10,
shouldResize,
onResizeStart,
onResize,
onResizeEnd,
@@ -36,6 +37,7 @@ export default function NodeResizer({
minWidth={minWidth}
minHeight={minHeight}
onResizeStart={onResizeStart}
shouldResize={shouldResize}
onResize={onResize}
onResizeEnd={onResizeEnd}
/>
@@ -51,6 +53,7 @@ export default function NodeResizer({
minWidth={minWidth}
minHeight={minHeight}
onResizeStart={onResizeStart}
shouldResize={shouldResize}
onResize={onResize}
onResizeEnd={onResizeEnd}
/>
+24 -1
View File
@@ -12,6 +12,7 @@ import {
} from '@reactflow/core';
import { ResizeDragEvent, ResizeControlProps, ResizeControlLineProps, ResizeControlVariant } from './types';
import { getDirection } from './utils';
const initPrevValues = { width: 0, height: 0, x: 0, y: 0 };
@@ -31,6 +32,7 @@ function ResizeControl({
color,
minWidth = 10,
minHeight = 10,
shouldResize,
onResizeStart,
onResize,
onResizeEnd,
@@ -140,7 +142,28 @@ function ResizeControl({
prevValues.current.height = height;
}
onResize?.(event, { ...prevValues.current });
if (changes.length === 0) {
return;
}
const direction = getDirection({
width: prevValues.current.width,
prevWidth,
height: prevValues.current.height,
prevHeight,
invertX,
invertY,
});
const nextValues = { ...prevValues.current, direction };
const callResize = shouldResize?.(event, nextValues);
if (callResize === false) {
return;
}
onResize?.(event, nextValues);
triggerNodeChanges(changes);
}
})
+17 -5
View File
@@ -1,13 +1,24 @@
import type { CSSProperties, ReactNode } from 'react';
import type { D3DragEvent, SubjectPosition } from 'd3-drag';
export type ResizeEventParams = {
export type ResizeParams = {
x: number;
y: number;
width: number;
height: number;
};
export type ResizeParamsWithDirection = ResizeParams & {
direction: number[];
};
type OnResizeHandler<Params = ResizeParams, Result = void> = (event: ResizeDragEvent, params: Params) => Result;
export type ShouldResize = OnResizeHandler<ResizeParamsWithDirection, boolean>;
export type OnResizeStart = OnResizeHandler;
export type OnResize = OnResizeHandler<ResizeParamsWithDirection>;
export type OnResizeEnd = OnResizeHandler;
export type NodeResizerProps = {
nodeId?: string;
color?: string;
@@ -18,9 +29,10 @@ export type NodeResizerProps = {
isVisible?: boolean;
minWidth?: number;
minHeight?: number;
onResizeStart?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
onResize?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
onResizeEnd?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
shouldResize?: ShouldResize;
onResizeStart?: OnResizeStart;
onResize?: OnResize;
onResizeEnd?: OnResizeEnd;
};
export type ControlLinePosition = 'top' | 'bottom' | 'left' | 'right';
@@ -34,7 +46,7 @@ export enum ResizeControlVariant {
export type ResizeControlProps = Pick<
NodeResizerProps,
'nodeId' | 'color' | 'minWidth' | 'minHeight' | 'onResizeStart' | 'onResize' | 'onResizeEnd'
'nodeId' | 'color' | 'minWidth' | 'minHeight' | 'shouldResize' | 'onResizeStart' | 'onResize' | 'onResizeEnd'
> & {
position?: ControlPosition;
variant?: ResizeControlVariant;
+26
View File
@@ -0,0 +1,26 @@
type GetDirectionParams = {
width: number;
prevWidth: number;
height: number;
prevHeight: number;
invertX: boolean;
invertY: boolean;
};
// returns an array of two numbers (0, 1 or -1) representing the direction of the resize
// 0 = no change, 1 = increase, -1 = decrease
export function getDirection({ width, prevWidth, height, prevHeight, invertX, invertY }: GetDirectionParams) {
const deltaWidth = width - prevWidth;
const deltaHeight = height - prevHeight;
const direction = [deltaWidth > 0 ? 1 : deltaWidth < 0 ? -1 : 0, deltaHeight > 0 ? 1 : deltaHeight < 0 ? -1 : 0];
if (deltaWidth && invertX) {
direction[0] = direction[0] * -1;
}
if (deltaHeight && invertY) {
direction[1] = direction[1] * -1;
}
return direction;
}
+1 -1
View File
@@ -36,7 +36,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@reactflow/core": "workspace:*",
"@reactflow/core": "workspace:^11.3.3",
"classcat": "^5.0.3",
"zustand": "^4.3.1"
},