chore(node-resizer): cleanup

This commit is contained in:
moklick
2022-12-06 18:28:54 +01:00
parent e0fc4ae08f
commit 531171234a
2 changed files with 27 additions and 33 deletions
@@ -43,9 +43,8 @@ const initialNodes = [
id: '3', id: '3',
type: 'customResizer', type: 'customResizer',
data: { label: 'resize control with child component' }, data: { label: 'resize control with child component' },
position: { x: 250, y: 150 }, position: { x: 250, y: 200 },
style: { border: '1px solid #222', fontSize: 10, width: 100 }, style: { border: '1px solid #222', fontSize: 10, width: 100 },
parentNode: '2',
}, },
{ {
id: '4', id: '4',
@@ -53,13 +52,12 @@ const initialNodes = [
data: { label: 'resize controls' }, data: { label: 'resize controls' },
position: { x: 100, y: 150 }, position: { x: 100, y: 150 },
style: { border: '1px solid #222', fontSize: 10 }, style: { border: '1px solid #222', fontSize: 10 },
parentNode: '2',
}, },
{ {
id: '5', id: '5',
type: 'customResizer2', type: 'customResizer2',
data: { label: 'min width and height' }, data: { label: 'min width and height' },
position: { x: 100, y: 150 }, position: { x: 250, y: 250 },
style: { border: '1px solid #222', fontSize: 10 }, style: { border: '1px solid #222', fontSize: 10 },
}, },
]; ];
@@ -82,9 +80,10 @@ const CustomNodeFlow = () => {
onEdgesChange={onEdgesChange} onEdgesChange={onEdgesChange}
onConnect={onConnect} onConnect={onConnect}
nodeTypes={nodeTypes} nodeTypes={nodeTypes}
minZoom={-5} minZoom={0.2}
maxZoom={5} maxZoom={5}
snapToGrid={snapToGrid} snapToGrid={snapToGrid}
fitView
> >
<Controls /> <Controls />
<Panel position="bottom-right"> <Panel position="bottom-right">
+23 -28
View File
@@ -10,10 +10,17 @@ import {
useNodeId, useNodeId,
NodePositionChange, NodePositionChange,
} from '@reactflow/core'; } from '@reactflow/core';
import type { Dimensions, XYPosition } from '@reactflow/core';
import { ResizeDragEvent, ResizeControlProps, ResizeControlLineProps, ResizeControlVariant } from './types'; import { ResizeDragEvent, ResizeControlProps, ResizeControlLineProps, ResizeControlVariant } from './types';
const initPrevValues = { width: 0, height: 0, x: 0, y: 0 };
const initStartValues = {
...initPrevValues,
pointerX: 0,
pointerY: 0,
};
function ResizeControl({ function ResizeControl({
nodeId, nodeId,
position, position,
@@ -22,22 +29,15 @@ function ResizeControl({
style = {}, style = {},
children, children,
color, color,
minWidth = 1, minWidth = 10,
minHeight = 1, minHeight = 10,
}: ResizeControlProps) { }: ResizeControlProps) {
const contextNodeId = useNodeId(); const contextNodeId = useNodeId();
const id = typeof nodeId === 'string' ? nodeId : contextNodeId; const id = typeof nodeId === 'string' ? nodeId : contextNodeId;
const store = useStoreApi(); const store = useStoreApi();
const resizeControlRef = useRef<HTMLDivElement>(null); const resizeControlRef = useRef<HTMLDivElement>(null);
const startValues = useRef<Dimensions & XYPosition & { nodeX: number; nodeY: number }>({ const startValues = useRef<typeof initStartValues>(initStartValues);
width: 0, const prevValues = useRef<typeof initPrevValues>(initPrevValues);
height: 0,
x: 0,
y: 0,
nodeX: 0,
nodeY: 0,
});
const prevValues = useRef<Dimensions & XYPosition>({ width: 0, height: 0, x: 0, y: 0 });
const getPointerPosition = useGetPointerPosition(); const getPointerPosition = useGetPointerPosition();
const defaultPosition = variant === ResizeControlVariant.Line ? 'right' : 'bottom-right'; const defaultPosition = variant === ResizeControlVariant.Line ? 'right' : 'bottom-right';
const controlPosition = position ?? defaultPosition; const controlPosition = position ?? defaultPosition;
@@ -53,21 +53,18 @@ function ResizeControl({
const node = store.getState().nodeInternals.get(id); const node = store.getState().nodeInternals.get(id);
const { xSnapped, ySnapped } = getPointerPosition(event); const { xSnapped, ySnapped } = getPointerPosition(event);
startValues.current = {
width: node?.width ?? 0,
height: node?.height ?? 0,
nodeX: node?.position.x ?? 0,
nodeY: node?.position.y ?? 0,
x: xSnapped,
y: ySnapped,
};
prevValues.current = { prevValues.current = {
width: node?.width ?? 0, width: node?.width ?? 0,
height: node?.height ?? 0, height: node?.height ?? 0,
x: node?.position.x ?? 0, x: node?.position.x ?? 0,
y: node?.position.y ?? 0, y: node?.position.y ?? 0,
}; };
startValues.current = {
...prevValues.current,
pointerX: xSnapped,
pointerY: ySnapped,
};
}) })
.on('drag', (event: ResizeDragEvent) => { .on('drag', (event: ResizeDragEvent) => {
const { nodeInternals, triggerNodeChanges } = store.getState(); const { nodeInternals, triggerNodeChanges } = store.getState();
@@ -81,12 +78,12 @@ function ResizeControl({
if (node) { if (node) {
const changes: NodeChange[] = []; const changes: NodeChange[] = [];
const { const {
x: startX, pointerX: startX,
y: startY, pointerY: startY,
width: startWidth, width: startWidth,
height: startHeight, height: startHeight,
nodeX: startNodeX, x: startNodeX,
nodeY: startNodeY, y: startNodeY,
} = startValues.current; } = startValues.current;
const { x: prevX, y: prevY, width: prevWidth, height: prevHeight } = prevValues.current; const { x: prevX, y: prevY, width: prevWidth, height: prevHeight } = prevValues.current;
@@ -143,15 +140,13 @@ function ResizeControl({
} }
}) })
.on('end', () => { .on('end', () => {
const { triggerNodeChanges } = store.getState();
const dimensionChange: NodeDimensionChange = { const dimensionChange: NodeDimensionChange = {
id: id, id: id,
type: 'dimensions', type: 'dimensions',
resizing: true, resizing: true,
}; };
triggerNodeChanges([dimensionChange]); store.getState().triggerNodeChanges([dimensionChange]);
}); });
selection.call(dragHandler); selection.call(dragHandler);