broken parent expand on resize but almost there

This commit is contained in:
peterkogo
2024-04-11 18:34:17 +02:00
parent 05ac4ef9db
commit 979fe7d565
2 changed files with 53 additions and 26 deletions
@@ -9,11 +9,15 @@ import {
type NodeChange, type NodeChange,
type NodeDimensionChange, type NodeDimensionChange,
type NodePositionChange, type NodePositionChange,
handleParentExpand,
} from '@xyflow/system'; } from '@xyflow/system';
import { useStoreApi } from '../../hooks/useStore'; import { useStoreApi } from '../../hooks/useStore';
import { useNodeId } from '../../contexts/NodeIdContext'; import { useNodeId } from '../../contexts/NodeIdContext';
import type { ResizeControlProps, ResizeControlLineProps } from './types'; import type { ResizeControlProps, ResizeControlLineProps } from './types';
import { InternalNode } from '../../types';
type InternalNodeWithParentExpand = InternalNode & { expandParent: true; parentId: string };
function ResizeControl({ function ResizeControl({
nodeId, nodeId,
@@ -62,20 +66,44 @@ function ResizeControl({
}; };
}, },
onChange: (change: XYResizerChange, childChanges: XYResizerChildChange[]) => { onChange: (change: XYResizerChange, childChanges: XYResizerChildChange[]) => {
const { triggerNodeChanges } = store.getState(); const { triggerNodeChanges, nodeLookup } = store.getState();
const changes: NodeChange[] = []; const changes: NodeChange[] = [];
const newPosition = { x: change.x, y: change.y };
if (change.x !== undefined && change.y !== undefined) { const node = nodeLookup.get(id);
const positionChange: NodePositionChange = { if (node && node.expandParent && node.parentId) {
id, const nodeWithChange = {
type: 'position', ...node,
position: { position: {
x: change.x, x: change.x,
y: change.y, y: change.y,
}, },
}; width: change.width,
height: change.height,
measured: {
width: change.width ?? node.measured?.width,
height: change.height ?? node.measured?.height,
},
} as InternalNodeWithParentExpand;
const parentExpandChanges = handleParentExpand([nodeWithChange], nodeLookup);
if (parentExpandChanges.length > 0) {
newPosition.x = change.x && change.x < 0 ? 0 : change.x;
newPosition.y = change.y && change.y < 0 ? 0 : change.y;
} else {
console.log('there was no parent expand');
}
changes.push(...parentExpandChanges);
}
if (change.x !== undefined && change.y !== undefined) {
console.log(newPosition);
const positionChange: NodePositionChange = {
id,
type: 'position',
position: { ...newPosition } as { x: number; y: number },
};
changes.push(positionChange); changes.push(positionChange);
} }
+19 -20
View File
@@ -145,25 +145,25 @@ function calculateXYZPosition<NodeType extends NodeBase>(
); );
} }
type NodeWithExpandParent<NodeType> = NodeType & { parentId: string; expandParent: true };
export function handleParentExpand( export function handleParentExpand(
nodes: InternalNodeBase[], nodes: NodeWithExpandParent<InternalNodeBase>[],
nodeLookup: NodeLookup nodeLookup: NodeLookup
): (NodeDimensionChange | NodePositionChange)[] { ): (NodeDimensionChange | NodePositionChange)[] {
const changes: (NodeDimensionChange | NodePositionChange)[] = []; const changes: (NodeDimensionChange | NodePositionChange)[] = [];
const chilNodeRects = new Map<string, Rect>(); const chilNodeRects = new Map<string, Rect>();
nodes.forEach((node) => { for (const node of nodes) {
const parentId = node.parentId; const parentNode = nodeLookup.get(node.parentId);
if (node.expandParent && parentId) { if (!parentNode) {
const parentNode = nodeLookup.get(parentId); continue;
if (parentNode) {
const parentRect = chilNodeRects.get(parentId) || nodeToRect(parentNode, node.origin);
const expandedRect = getBoundsOfRects(parentRect, nodeToRect(node, node.origin));
chilNodeRects.set(parentId, expandedRect);
}
} }
});
const parentRect = chilNodeRects.get(node.parentId) || nodeToRect(parentNode, node.origin);
const expandedRect = getBoundsOfRects(parentRect, nodeToRect(node, node.origin));
chilNodeRects.set(node.parentId, expandedRect);
}
if (chilNodeRects.size > 0) { if (chilNodeRects.size > 0) {
chilNodeRects.forEach((rect, id) => { chilNodeRects.forEach((rect, id) => {
@@ -193,9 +193,8 @@ export function handleParentExpand(
height: dimensions.height + yChange, height: dimensions.height + yChange,
}, },
}); });
}
// @todo we need to reset child node positions if < 0 if (dimensions.width < rect.width || dimensions.height < rect.height) {
} else if (dimensions.width < rect.width || dimensions.height < rect.height) {
changes.push({ changes.push({
id, id,
type: 'dimensions', type: 'dimensions',
@@ -229,7 +228,7 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
const style = window.getComputedStyle(viewportNode); const style = window.getComputedStyle(viewportNode);
const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform); const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform);
// in this array we collect nodes, that might trigger changes (like expanding parent) // in this array we collect nodes, that might trigger changes (like expanding parent)
const triggerChangeNodes: NodeType[] = []; const parentExpandNodes: NodeWithExpandParent<NodeType>[] = [];
updates.forEach((update) => { updates.forEach((update) => {
const node = nodeLookup.get(update.id); const node = nodeLookup.get(update.id);
@@ -275,16 +274,16 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
dimensions, dimensions,
}); });
if (newNode.expandParent) { if (newNode.expandParent && newNode.parentId) {
triggerChangeNodes.push(newNode); parentExpandNodes.push(newNode as NodeWithExpandParent<NodeType>);
} }
} }
} }
} }
}); });
if (triggerChangeNodes.length > 0) { if (parentExpandNodes.length > 0) {
const parentExpandChanges = handleParentExpand(triggerChangeNodes, nodeLookup); const parentExpandChanges = handleParentExpand(parentExpandNodes, nodeLookup);
changes.push(...parentExpandChanges); changes.push(...parentExpandChanges);
} }