let resizer params be updated dynamically

This commit is contained in:
peterkogo
2025-09-30 10:40:57 +02:00
parent 8f934d7793
commit f29593072c
2 changed files with 34 additions and 10 deletions
@@ -1,7 +1,9 @@
import { memo, FC } from 'react'; import { memo, FC } from 'react';
import { Handle, Position, NodeProps, NodeResizer } from '@xyflow/react'; import { Handle, Position, NodeProps, NodeResizer, useKeyPress } from '@xyflow/react';
const DefaultResizerNode: FC<NodeProps> = ({ data, selected }) => { const DefaultResizerNode: FC<NodeProps> = ({ data, selected }) => {
const keepAspectRatio = useKeyPress('k');
return ( return (
<> <>
<NodeResizer <NodeResizer
@@ -14,7 +16,7 @@ const DefaultResizerNode: FC<NodeProps> = ({ data, selected }) => {
onResizeStart={data.onResizeStart ?? undefined} onResizeStart={data.onResizeStart ?? undefined}
onResize={data.onResize ?? undefined} onResize={data.onResize ?? undefined}
onResizeEnd={data.onResizeEnd ?? undefined} onResizeEnd={data.onResizeEnd ?? undefined}
keepAspectRatio={data.keepAspectRatio ?? undefined} keepAspectRatio={keepAspectRatio || (data.keepAspectRatio ?? undefined)}
/> />
<Handle type="target" position={Position.Left} /> <Handle type="target" position={Position.Left} />
<div>{data.label}</div> <div>{data.label}</div>
+30 -8
View File
@@ -104,6 +104,18 @@ function nodeToChildExtent(child: NodeBase, parent: NodeBase, nodeOrigin: NodeOr
export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: XYResizerParams): XYResizerInstance { export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: XYResizerParams): XYResizerInstance {
const selection = select(domNode); const selection = select(domNode);
let params = {
controlDirection: getControlDirection('bottom-right'),
boundaries: {
minWidth: 0,
minHeight: 0,
maxWidth: Number.MAX_VALUE,
maxHeight: Number.MAX_VALUE,
},
resizeDirection: undefined as ResizeControlDirection | undefined,
keepAspectRatio: false,
};
function update({ function update({
controlPosition, controlPosition,
boundaries, boundaries,
@@ -117,7 +129,12 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: X
let prevValues = { ...initPrevValues }; let prevValues = { ...initPrevValues };
let startValues = { ...initStartValues }; let startValues = { ...initStartValues };
const controlDirection = getControlDirection(controlPosition); params = {
boundaries,
resizeDirection,
keepAspectRatio,
controlDirection: getControlDirection(controlPosition),
};
let node: InternalNodeBase | undefined = undefined; let node: InternalNodeBase | undefined = undefined;
let containerBounds: DOMRect | null = null; let containerBounds: DOMRect | null = null;
@@ -212,16 +229,17 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: X
if (!node) { if (!node) {
return; return;
} }
const { x: prevX, y: prevY, width: prevWidth, height: prevHeight } = prevValues; const { x: prevX, y: prevY, width: prevWidth, height: prevHeight } = prevValues;
const change: XYResizerChange = {}; const change: XYResizerChange = {};
const nodeOrigin = node.origin ?? storeNodeOrigin; const nodeOrigin = node.origin ?? storeNodeOrigin;
const { width, height, x, y } = getDimensionsAfterResize( const { width, height, x, y } = getDimensionsAfterResize(
startValues, startValues,
controlDirection, params.controlDirection,
pointerPosition, pointerPosition,
boundaries, params.boundaries,
keepAspectRatio, params.keepAspectRatio,
nodeOrigin, nodeOrigin,
parentExtent, parentExtent,
childExtent childExtent
@@ -264,9 +282,13 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: X
if (isWidthChange || isHeightChange) { if (isWidthChange || isHeightChange) {
change.width = change.width =
isWidthChange && (!resizeDirection || resizeDirection === 'horizontal') ? width : prevValues.width; isWidthChange && (!params.resizeDirection || params.resizeDirection === 'horizontal')
? width
: prevValues.width;
change.height = change.height =
isHeightChange && (!resizeDirection || resizeDirection === 'vertical') ? height : prevValues.height; isHeightChange && (!params.resizeDirection || params.resizeDirection === 'vertical')
? height
: prevValues.height;
prevValues.width = change.width; prevValues.width = change.width;
prevValues.height = change.height; prevValues.height = change.height;
} }
@@ -291,8 +313,8 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange, onEnd }: X
prevWidth, prevWidth,
height: prevValues.height, height: prevValues.height,
prevHeight, prevHeight,
affectsX: controlDirection.affectsX, affectsX: params.controlDirection.affectsX,
affectsY: controlDirection.affectsY, affectsY: params.controlDirection.affectsY,
}); });
const nextValues = { ...prevValues, direction }; const nextValues = { ...prevValues, direction };