feat(subflow): add extendParent option

This commit is contained in:
moklick
2022-01-04 11:15:44 +01:00
parent f01e239947
commit eaeebdd12e
3 changed files with 43 additions and 3 deletions
+4 -3
View File
@@ -43,6 +43,7 @@ const initialNodes: Node[] = [
className: 'light', className: 'light',
style: { backgroundColor: 'rgba(50, 50, 255, 0.5)', height: 200, width: 300 }, style: { backgroundColor: 'rgba(50, 50, 255, 0.5)', height: 200, width: 300 },
parentNode: '4', parentNode: '4',
expandParent: true,
}, },
{ {
id: '4b1', id: '4b1',
@@ -110,9 +111,7 @@ const BasicFlow = () => {
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const onConnect = useCallback((connection: Connection) => { const onConnect = useCallback((connection: Connection) => {
setEdges((eds) => { setEdges((eds) => addEdge(connection, eds));
return addEdge(connection, eds);
});
}, []); }, []);
const onPaneReady = useCallback((reactFlowInstance: ReactFlowInstance) => setRfInstance(reactFlowInstance), []); const onPaneReady = useCallback((reactFlowInstance: ReactFlowInstance) => setRfInstance(reactFlowInstance), []);
@@ -150,6 +149,8 @@ const BasicFlow = () => {
}); });
}; };
console.log(nodes);
return ( return (
<ReactFlow <ReactFlow
nodes={nodes} nodes={nodes}
+1
View File
@@ -26,6 +26,7 @@ export interface Node<T = any> {
parentNode?: string; parentNode?: string;
zIndex?: number; zIndex?: number;
extent?: 'parent' | CoordinateExtent; extent?: 'parent' | CoordinateExtent;
expandParent?: boolean;
} }
// props that get passed to a custom node // props that get passed to a custom node
+38
View File
@@ -28,6 +28,44 @@ function applyChanges(changes: NodeChange[] | EdgeChange[], elements: any[]): an
updateItem.dragging = currentChange.dragging; 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); res.push(updateItem);
return res; return res;
} }