Skip to content

Commit

Permalink
fix(subflows): node extent relative to parent closes #2232
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Jun 21, 2022
1 parent 02e7c90 commit 3b94186
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions example/src/Subflow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ const onNodeClick = (_: MouseEvent, node: Node) => console.log('click', node);
const onEdgeClick = (_: MouseEvent, edge: Edge) => console.log('click', edge);

const initialNodes: Node[] = [
{ id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 }, className: 'light' },
{
id: '1',
type: 'input',
data: { label: 'Node 1' },
position: { x: 250, y: 5 },
className: 'light',
},
{
id: '4',
data: { label: 'Node 4' },
Expand All @@ -34,7 +40,10 @@ const initialNodes: Node[] = [
position: { x: 15, y: 15 },
className: 'light',
parentNode: '4',
extent: 'parent',
extent: [
[0, 0],
[100, 100],
],
},
{
id: '4b',
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useDrag/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export function updatePosition(
}
currentExtent = nodeExtent;
}
} else if (dragItem.extent && dragItem.parentNode) {
const parent = nodeInternals.get(dragItem.parentNode);
const parentX = parent?.positionAbsolute?.x ?? 0;
const parentY = parent?.positionAbsolute?.y ?? 0;
currentExtent = [
[dragItem.extent[0][0] + parentX, dragItem.extent[0][1] + parentY],
[dragItem.extent[1][0] + parentX, dragItem.extent[1][1] + parentY],
];
}

dragItem.position = currentExtent ? clampPosition(nextPosition, currentExtent as CoordinateExtent) : nextPosition;
Expand Down

0 comments on commit 3b94186

Please sign in to comment.