From 32ae60fc65f6e15c41d567ed76026f5da0ba0e8a Mon Sep 17 00:00:00 2001 From: zhaoying Date: Thu, 9 Apr 2026 16:14:24 +0800 Subject: [PATCH] fix(web): port add node front --- .../Workflow/components/PortClickHandler.tsx | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/web/src/views/Workflow/components/PortClickHandler.tsx b/web/src/views/Workflow/components/PortClickHandler.tsx index 7e2350ee..b556ffab 100644 --- a/web/src/views/Workflow/components/PortClickHandler.tsx +++ b/web/src/views/Workflow/components/PortClickHandler.tsx @@ -191,38 +191,39 @@ const PortClickHandler: React.FC = ({ graph }) => { setTimeout(() => { const newPorts = newNode.getPorts(); + const addedEdges: any[] = []; if (edgeInsertion) { // Edge insertion: create source→new and new→target edges const { targetCell, targetPort: origTargetPort } = edgeInsertion; const newLeftPort = newPorts.find((p: any) => p.group === 'left')?.id || 'left'; const newRightPort = newPorts.find((p: any) => p.group === 'right')?.id || 'right'; - graph.addEdge({ + addedEdges.push(graph.addEdge({ source: { cell: sourceNode.id, port: sourcePort }, target: { cell: newNode.id, port: newLeftPort }, ...edgeAttrs - }); - graph.addEdge({ + })); + addedEdges.push(graph.addEdge({ source: { cell: newNode.id, port: newRightPort }, target: { cell: targetCell.id, port: origTargetPort }, ...edgeAttrs - }); + })); setEdgeInsertion(null); } else if (sourcePortGroup === 'left') { // Connect from left port to new node's right side const targetPort = newPorts.find((port: any) => port.group === 'right')?.id || 'right'; - graph.addEdge({ + addedEdges.push(graph.addEdge({ source: { cell: newNode.id, port: targetPort }, target: { cell: sourceNode.id, port: sourcePort }, ...edgeAttrs - }); + })); } else { // Connect from right port to new node's left side const targetPort = newPorts.find((port: any) => port.group === 'left')?.id || 'left'; - graph.addEdge({ + addedEdges.push(graph.addEdge({ source: { cell: sourceNode.id, port: sourcePort }, target: { cell: newNode.id, port: targetPort }, ...edgeAttrs - }); + })); } // Adjust loop node size when child node is added via port within loop node @@ -269,6 +270,44 @@ const PortClickHandler: React.FC = ({ graph }) => { }); } } + + const isCycleContainer = (type: string) => type === 'loop' || type === 'iteration'; + const newNodeType = selectedNodeType.type; + + // Helper: bring all child nodes and their edges of a cycle container to front + const bringCycleChildrenToFront = (cycleContainerId: string) => { + + graph.getEdges().forEach((e: any) => { + const src = graph.getCellById(e.getSourceCellId()); + const tgt = graph.getCellById(e.getTargetCellId()); + if (src?.getData()?.cycle === cycleContainerId || tgt?.getData()?.cycle === cycleContainerId) e.toFront(); + }); + graph.getNodes().forEach((n: any) => { + if (n.getData()?.cycle === cycleContainerId) n.toFront(); + }); + }; + + if (isCycleContainer(sourceNodeType)) { + console.log('isCycleContainer(sourceNodeType)') + // Case 4: source is a loop/iteration node — bring new node to front, then its children + newNode.toFront(); + sourceNode.toFront(); + bringCycleChildrenToFront(sourceNodeData.id); + } else if (isCycleContainer(newNodeType)) { + console.log('isCycleContainer(newNodeType)') + // Case 3: adding a loop/iteration node from a normal node — bring new node to front, then its children + newNode.toFront(); + sourceNode.toFront() + bringCycleChildrenToFront(id); + } else { + // Case 2: normal node → normal node + addedEdges.forEach(e => { + const src = graph.getCellById(e.getSourceCellId()); + const tgt = graph.getCellById(e.getTargetCellId()); + if (src?.isNode()) src.toFront(); + if (tgt?.isNode()) tgt.toFront(); + }); + } }, 50); // Clean up temporary element