feat(resizer): add resize event handlers
This commit is contained in:
@@ -1,13 +1,32 @@
|
|||||||
import { memo, FC } from 'react';
|
import { memo, FC } from 'react';
|
||||||
import { Handle, Position, NodeProps } from 'reactflow';
|
import { Handle, Position, NodeProps } from 'reactflow';
|
||||||
|
|
||||||
import { NodeResizer } from '@reactflow/node-resizer';
|
import { NodeResizer, ResizeDragEvent, ResizeEventParams } from '@reactflow/node-resizer';
|
||||||
import '@reactflow/node-resizer/dist/style.css';
|
import '@reactflow/node-resizer/dist/style.css';
|
||||||
|
|
||||||
|
const onResizeStart = (_: ResizeDragEvent, params: ResizeEventParams) => {
|
||||||
|
console.log('resize start', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onResize = (_: ResizeDragEvent, params: ResizeEventParams) => {
|
||||||
|
console.log('resize', params);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onResizeEnd = (_: ResizeDragEvent, params: ResizeEventParams) => {
|
||||||
|
console.log('resize end', params);
|
||||||
|
};
|
||||||
|
|
||||||
const CustomNode: FC<NodeProps> = ({ data, selected }) => {
|
const CustomNode: FC<NodeProps> = ({ data, selected }) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<NodeResizer minWidth={100} minHeight={100} isVisible={selected} />
|
<NodeResizer
|
||||||
|
minWidth={100}
|
||||||
|
minHeight={100}
|
||||||
|
isVisible={selected}
|
||||||
|
onResizeStart={onResizeStart}
|
||||||
|
onResize={onResize}
|
||||||
|
onResizeEnd={onResizeEnd}
|
||||||
|
/>
|
||||||
<Handle type="target" position={Position.Left} />
|
<Handle type="target" position={Position.Left} />
|
||||||
<div>{data.label}</div>
|
<div>{data.label}</div>
|
||||||
<Handle type="source" position={Position.Right} />
|
<Handle type="source" position={Position.Right} />
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ export default function NodeResizer({
|
|||||||
color,
|
color,
|
||||||
minWidth = 10,
|
minWidth = 10,
|
||||||
minHeight = 10,
|
minHeight = 10,
|
||||||
|
onResizeStart,
|
||||||
|
onResize,
|
||||||
|
onResizeEnd,
|
||||||
}: NodeResizerProps) {
|
}: NodeResizerProps) {
|
||||||
if (!isVisible) {
|
if (!isVisible) {
|
||||||
return null;
|
return null;
|
||||||
@@ -32,6 +35,9 @@ export default function NodeResizer({
|
|||||||
color={color}
|
color={color}
|
||||||
minWidth={minWidth}
|
minWidth={minWidth}
|
||||||
minHeight={minHeight}
|
minHeight={minHeight}
|
||||||
|
onResizeStart={onResizeStart}
|
||||||
|
onResize={onResize}
|
||||||
|
onResizeEnd={onResizeEnd}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{handleControls.map((c) => (
|
{handleControls.map((c) => (
|
||||||
@@ -44,6 +50,9 @@ export default function NodeResizer({
|
|||||||
color={color}
|
color={color}
|
||||||
minWidth={minWidth}
|
minWidth={minWidth}
|
||||||
minHeight={minHeight}
|
minHeight={minHeight}
|
||||||
|
onResizeStart={onResizeStart}
|
||||||
|
onResize={onResize}
|
||||||
|
onResizeEnd={onResizeEnd}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ function ResizeControl({
|
|||||||
color,
|
color,
|
||||||
minWidth = 10,
|
minWidth = 10,
|
||||||
minHeight = 10,
|
minHeight = 10,
|
||||||
|
onResizeStart,
|
||||||
|
onResize,
|
||||||
|
onResizeEnd,
|
||||||
}: ResizeControlProps) {
|
}: ResizeControlProps) {
|
||||||
const contextNodeId = useNodeId();
|
const contextNodeId = useNodeId();
|
||||||
const id = typeof nodeId === 'string' ? nodeId : contextNodeId;
|
const id = typeof nodeId === 'string' ? nodeId : contextNodeId;
|
||||||
@@ -65,6 +68,8 @@ function ResizeControl({
|
|||||||
pointerX: xSnapped,
|
pointerX: xSnapped,
|
||||||
pointerY: ySnapped,
|
pointerY: ySnapped,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
onResizeStart?.(event, { ...prevValues.current });
|
||||||
})
|
})
|
||||||
.on('drag', (event: ResizeDragEvent) => {
|
.on('drag', (event: ResizeDragEvent) => {
|
||||||
const { nodeInternals, triggerNodeChanges } = store.getState();
|
const { nodeInternals, triggerNodeChanges } = store.getState();
|
||||||
@@ -135,16 +140,18 @@ function ResizeControl({
|
|||||||
prevValues.current.height = height;
|
prevValues.current.height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onResize?.(event, { ...prevValues.current });
|
||||||
triggerNodeChanges(changes);
|
triggerNodeChanges(changes);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('end', () => {
|
.on('end', (event: ResizeDragEvent) => {
|
||||||
const dimensionChange: NodeDimensionChange = {
|
const dimensionChange: NodeDimensionChange = {
|
||||||
id: id,
|
id: id,
|
||||||
type: 'dimensions',
|
type: 'dimensions',
|
||||||
resizing: true,
|
resizing: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
onResizeEnd?.(event, { ...prevValues.current });
|
||||||
store.getState().triggerNodeChanges([dimensionChange]);
|
store.getState().triggerNodeChanges([dimensionChange]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import type { CSSProperties, ReactNode } from 'react';
|
import type { CSSProperties, ReactNode } from 'react';
|
||||||
import type { D3DragEvent, SubjectPosition } from 'd3-drag';
|
import type { D3DragEvent, SubjectPosition } from 'd3-drag';
|
||||||
|
|
||||||
|
export type ResizeEventParams = {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
};
|
||||||
|
|
||||||
export type NodeResizerProps = {
|
export type NodeResizerProps = {
|
||||||
nodeId?: string;
|
nodeId?: string;
|
||||||
color?: string;
|
color?: string;
|
||||||
@@ -11,6 +18,9 @@ export type NodeResizerProps = {
|
|||||||
isVisible?: boolean;
|
isVisible?: boolean;
|
||||||
minWidth?: number;
|
minWidth?: number;
|
||||||
minHeight?: number;
|
minHeight?: number;
|
||||||
|
onResizeStart?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
|
||||||
|
onResize?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
|
||||||
|
onResizeEnd?: (event: ResizeDragEvent, params: ResizeEventParams) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ControlLinePosition = 'top' | 'bottom' | 'left' | 'right';
|
export type ControlLinePosition = 'top' | 'bottom' | 'left' | 'right';
|
||||||
@@ -22,16 +32,15 @@ export enum ResizeControlVariant {
|
|||||||
Handle = 'handle',
|
Handle = 'handle',
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ResizeControlProps = {
|
export type ResizeControlProps = Pick<
|
||||||
nodeId?: string;
|
NodeResizerProps,
|
||||||
|
'nodeId' | 'color' | 'minWidth' | 'minHeight' | 'onResizeStart' | 'onResize' | 'onResizeEnd'
|
||||||
|
> & {
|
||||||
position?: ControlPosition;
|
position?: ControlPosition;
|
||||||
variant?: ResizeControlVariant;
|
variant?: ResizeControlVariant;
|
||||||
color?: string;
|
|
||||||
className?: string;
|
className?: string;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
children?: ReactNode;
|
children?: ReactNode;
|
||||||
minWidth?: number;
|
|
||||||
minHeight?: number;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ResizeControlLineProps = ResizeControlProps & {
|
export type ResizeControlLineProps = ResizeControlProps & {
|
||||||
|
|||||||
Reference in New Issue
Block a user