From eaeebdd12e8165093e6c421e4abcee439de88cd2 Mon Sep 17 00:00:00 2001 From: moklick Date: Tue, 4 Jan 2022 11:15:44 +0100 Subject: [PATCH] feat(subflow): add extendParent option --- example/src/Subflow/index.tsx | 7 ++++--- src/types/nodes.ts | 1 + src/utils/changes.ts | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/example/src/Subflow/index.tsx b/example/src/Subflow/index.tsx index 97a113b2..aa3ad9b3 100644 --- a/example/src/Subflow/index.tsx +++ b/example/src/Subflow/index.tsx @@ -43,6 +43,7 @@ const initialNodes: Node[] = [ className: 'light', style: { backgroundColor: 'rgba(50, 50, 255, 0.5)', height: 200, width: 300 }, parentNode: '4', + expandParent: true, }, { id: '4b1', @@ -110,9 +111,7 @@ const BasicFlow = () => { const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const onConnect = useCallback((connection: Connection) => { - setEdges((eds) => { - return addEdge(connection, eds); - }); + setEdges((eds) => addEdge(connection, eds)); }, []); const onPaneReady = useCallback((reactFlowInstance: ReactFlowInstance) => setRfInstance(reactFlowInstance), []); @@ -150,6 +149,8 @@ const BasicFlow = () => { }); }; + console.log(nodes); + return ( { parentNode?: string; zIndex?: number; extent?: 'parent' | CoordinateExtent; + expandParent?: boolean; } // props that get passed to a custom node diff --git a/src/utils/changes.ts b/src/utils/changes.ts index d61e939f..2ab40c59 100644 --- a/src/utils/changes.ts +++ b/src/utils/changes.ts @@ -28,6 +28,44 @@ function applyChanges(changes: NodeChange[] | EdgeChange[], elements: any[]): an updateItem.dragging = currentChange.dragging; } + if (updateItem.expandParent) { + const parent = res.find((e) => e.id === updateItem.parentNode); + + if (parent) { + const extendWidth = updateItem.position.x + updateItem.width - parent.width; + const extendHeight = updateItem.position.y + updateItem.height - parent.height; + + if (extendWidth > 0 || extendHeight > 0 || updateItem.position.x < 0 || updateItem.position.y < 0) { + parent.style = { ...parent.style } || {}; + + if (extendWidth > 0) { + parent.style.width += extendWidth; + } + + if (extendHeight > 0) { + parent.style.height += extendHeight; + } + + if (updateItem.position.x < 0) { + const xDiff = Math.abs(updateItem.position.x); + parent.position.x = parent.position.x - xDiff; + parent.style.width += xDiff; + updateItem.position.x = 0; + } + + if (updateItem.position.y < 0) { + const yDiff = Math.abs(updateItem.position.y); + parent.position.y = parent.position.y - yDiff; + parent.style.height += yDiff; + updateItem.position.y = 0; + } + + parent.width = parent.style.width; + parent.height = parent.style.height; + } + } + } + res.push(updateItem); return res; }