diff --git a/dashboard/src/components/BlueprintFlow/BlueprintFlow.tsx b/dashboard/src/components/BlueprintFlow/BlueprintFlow.tsx index 1fa34dbd..a11979d1 100644 --- a/dashboard/src/components/BlueprintFlow/BlueprintFlow.tsx +++ b/dashboard/src/components/BlueprintFlow/BlueprintFlow.tsx @@ -15,7 +15,10 @@ import { toInputProperty, } from "../ApiNode/apiNodes"; import { Flow, FlowMiniMap } from "@canmingir/link/platform/components"; -import NodeConnectionOverlay, { type DragState } from "./NodeConnectionOverlay"; +import NodeConnectionOverlay, { + type DragState, + type NodeDragState, +} from "./NodeConnectionOverlay"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; type FlowHandle = { @@ -53,6 +56,7 @@ export default function BlueprintFlow({ showConnections = false, onConnectParameter, onDisconnectParameter, + onConnectNode, onOverrideParameter, onToggleForceNode, liteMode = false, @@ -61,6 +65,7 @@ export default function BlueprintFlow({ const flowContainerRef = useRef(null); const flowRef = useRef(null); const [dragging, setDragging] = useState(null); + const [nodeDragging, setNodeDragging] = useState(null); const allIntegrations = useAllIntegrations(); useEffect(() => { @@ -323,17 +328,25 @@ export default function BlueprintFlow({ setDragging({ sourceNodeId: nodeId, sourceField: field }), onToggleForce: readOnly ? undefined : onToggleForceNode, onOverrideParameter: readOnly ? undefined : onOverrideParameter, + onNodeHandleDragStart: readOnly + ? undefined + : (nodeId: string) => setNodeDragging({ sourceNodeId: nodeId }), liteMode, })} /> - {!readOnly && showConnections && ( + {!readOnly && (showConnections || dragging || nodeDragging) && ( setDragging(null)} + onNodeDragEnd={() => setNodeDragging(null)} onConnect={(binding) => onConnectParameter?.(binding)} + onConnectNode={(sourceId, targetId) => + onConnectNode?.(sourceId, targetId) + } onDisconnect={(targetNodeId, targetField) => onDisconnectParameter?.(targetNodeId, targetField) } diff --git a/dashboard/src/components/BlueprintFlow/BlueprintFlow.types.ts b/dashboard/src/components/BlueprintFlow/BlueprintFlow.types.ts index f5688eb3..05d12cb7 100644 --- a/dashboard/src/components/BlueprintFlow/BlueprintFlow.types.ts +++ b/dashboard/src/components/BlueprintFlow/BlueprintFlow.types.ts @@ -37,6 +37,7 @@ export interface BlueprintFlowProps { showConnections?: boolean; onConnectParameter?: (binding: ParameterBinding) => void; onDisconnectParameter?: (targetNodeId: string, targetField: string) => void; + onConnectNode?: (sourceNodeId: string, targetNodeId: string) => void; onOverrideParameter?: (nodeId: string, field: string, value: unknown) => void; onToggleForceNode?: (node: { id: string }) => void; liteMode?: boolean; diff --git a/dashboard/src/components/BlueprintFlow/BlueprintFlowPlugin.styles.tsx b/dashboard/src/components/BlueprintFlow/BlueprintFlowPlugin.styles.tsx index 38327a59..7b8170b8 100644 --- a/dashboard/src/components/BlueprintFlow/BlueprintFlowPlugin.styles.tsx +++ b/dashboard/src/components/BlueprintFlow/BlueprintFlowPlugin.styles.tsx @@ -555,5 +555,27 @@ export const NodeWrapper = styled("div", { "perspective(1500px) rotateX(0deg) rotateY(-10deg) rotateZ(0deg)", boxShadow: steppedShadow, }, + "&:hover .node-connect-handle": { + opacity: 1, + }, }; }); + +export const NodeConnectHandle = styled("div")(({ theme }) => ({ + position: "absolute", + top: "50%", + right: "-10px", + transform: "translateY(-50%)", + width: "16px", + height: "16px", + borderRadius: "50%", + backgroundColor: theme.palette?.background.paper, + border: `2px solid ${theme.palette.text.secondary}`, + cursor: "crosshair", + zIndex: 11, + opacity: 0, + transition: "opacity 0.15s ease, transform 0.15s ease", + "&:hover": { + transform: "translateY(-50%) scale(1.25)", + }, +})); diff --git a/dashboard/src/components/BlueprintFlow/FlowNodeRenderer.tsx b/dashboard/src/components/BlueprintFlow/FlowNodeRenderer.tsx index 10e93b87..24f42241 100644 --- a/dashboard/src/components/BlueprintFlow/FlowNodeRenderer.tsx +++ b/dashboard/src/components/BlueprintFlow/FlowNodeRenderer.tsx @@ -8,6 +8,7 @@ import { ForceBadge, ForceIndicatorWrapper, HighlightOverlay, + NodeConnectHandle, NodeWrapper, PinnedBadge, StatusBadge, @@ -176,6 +177,7 @@ export default function FlowNodeRenderer({ field: string, value: unknown ) => void; + onNodeHandleDragStart?: (nodeId: string, e: React.PointerEvent) => void; liteMode?: boolean; [key: string]: unknown; }; @@ -193,6 +195,7 @@ export default function FlowNodeRenderer({ onHandleDragStart, onToggleForce, onOverrideParameter, + onNodeHandleDragStart, liteMode, } = nodeStyle || {}; const isLocked = isApiNode(node); @@ -285,8 +288,24 @@ export default function FlowNodeRenderer({ title={isForceNode ? "Force node: action is guaranteed" : undefined} onContextMenu={handleContextMenu} onMouseDownCapture={handleMouseDownCapture} + {...(node.nodeType !== "START" + ? { "data-node-connect-target": node.id } + : {})} > {isPinned && Pinned} + {!isLocked && !isReadOnly && node.nodeType === "NORMAL" && ( + { + e.stopPropagation(); + e.preventDefault(); + onNodeHandleDragStart?.(node.id, e); + }} + /> + )} {isForceNode && isReadOnly && ( ; graphData: DefinitionGraph; dragging: DragState | null; + nodeDragging?: NodeDragState | null; onDragEnd: () => void; + onNodeDragEnd?: () => void; onConnect: (binding: ParameterBinding) => void; + onConnectNode?: (sourceNodeId: string, targetNodeId: string) => void; onDisconnect: (targetNodeId: string, targetField: string) => void; }) { const theme = useTheme(); @@ -185,13 +195,44 @@ export default function NodeConnectionOverlay({ } } + if (nodeDragging && mousePosRef.current) { + const sourceNodeEl = container.querySelector( + `[data-node-id="${nodeDragging.sourceNodeId}"]` + ); + if (sourceNodeEl) { + const s = sourceAnchor( + sourceNodeEl.getBoundingClientRect(), + containerRect + ); + nextPaths.push({ + id: "__node_dragging__", + d: bezierPath( + s.x, + s.y, + mousePosRef.current.x - containerRect.left, + mousePosRef.current.y - containerRect.top + ), + targetNodeId: "", + targetField: "", + temp: true, + }); + } + } + setPaths(nextPaths); rafRef.current = requestAnimationFrame(tick); }; rafRef.current = requestAnimationFrame(tick); return () => cancelAnimationFrame(rafRef.current); - }, [containerRef, graphData, bindings, dragging, directParents]); + }, [ + containerRef, + graphData, + bindings, + dragging, + nodeDragging, + directParents, + ]); useEffect(() => { const container = containerRef.current; @@ -228,40 +269,74 @@ export default function NodeConnectionOverlay({ }, [containerRef]); useEffect(() => { - if (!dragging) return; + if (!dragging && !nodeDragging) return; - const handleMouseMove = (e: MouseEvent) => { + const handlePointerMove = (e: PointerEvent) => { mousePosRef.current = { x: e.clientX, y: e.clientY }; }; - const handleMouseUp = (e: MouseEvent) => { - const dropped = ( - document.elementFromPoint(e.clientX, e.clientY) as HTMLElement | null - )?.closest("[data-in-bindable]") as HTMLElement | null; - - if (dropped) { - const { nodeId, field } = dropped.dataset; - if (nodeId && field && nodeId !== dragging.sourceNodeId) { - onConnect({ - sourceNodeId: dragging.sourceNodeId, - sourceField: dragging.sourceField, - targetNodeId: nodeId, - targetField: field, - }); + const handlePointerUp = (e: PointerEvent) => { + if (dragging) { + const dropped = ( + document.elementFromPoint(e.clientX, e.clientY) as HTMLElement | null + )?.closest("[data-in-bindable]") as HTMLElement | null; + + if (dropped) { + const { nodeId, field } = dropped.dataset; + if (nodeId && field && nodeId !== dragging.sourceNodeId) { + onConnect({ + sourceNodeId: dragging.sourceNodeId, + sourceField: dragging.sourceField, + targetNodeId: nodeId, + targetField: field, + }); + } } + + mousePosRef.current = null; + onDragEnd(); + return; } + if (nodeDragging) { + const dropped = ( + document.elementFromPoint(e.clientX, e.clientY) as HTMLElement | null + )?.closest("[data-node-connect-target]") as HTMLElement | null; + + if (dropped) { + const targetNodeId = dropped.dataset.nodeConnectTarget; + if (targetNodeId && targetNodeId !== nodeDragging.sourceNodeId) { + onConnectNode?.(nodeDragging.sourceNodeId, targetNodeId); + } + } + + mousePosRef.current = null; + onNodeDragEnd?.(); + } + }; + + const handlePointerCancel = () => { mousePosRef.current = null; - onDragEnd(); + if (dragging) onDragEnd(); + if (nodeDragging) onNodeDragEnd?.(); }; - window.addEventListener("mousemove", handleMouseMove); - window.addEventListener("mouseup", handleMouseUp); + window.addEventListener("pointermove", handlePointerMove); + window.addEventListener("pointerup", handlePointerUp); + window.addEventListener("pointercancel", handlePointerCancel); return () => { - window.removeEventListener("mousemove", handleMouseMove); - window.removeEventListener("mouseup", handleMouseUp); + window.removeEventListener("pointermove", handlePointerMove); + window.removeEventListener("pointerup", handlePointerUp); + window.removeEventListener("pointercancel", handlePointerCancel); }; - }, [dragging, onConnect, onDragEnd]); + }, [ + dragging, + nodeDragging, + onConnect, + onConnectNode, + onDragEnd, + onNodeDragEnd, + ]); return ( Boolean(node)); } +export function canConnectNodes( + graph: DefinitionGraph, + sourceNodeId: string, + targetNodeId: string +): boolean { + if (sourceNodeId === targetNodeId) return false; + const targetNode = graph.nodes[targetNodeId]; + if (!targetNode || targetNode.nodeType === "START") return false; + const ancestorsOfSource = getAncestorNodes(graph, sourceNodeId); + if (ancestorsOfSource.some((n) => n.id === targetNodeId)) return false; + return true; +} + const BY_NODE_REF_PATTERN = /^byNode\.([^.]+)\.result(?:\.(.+))?$/; export function isRefValue(value: unknown): value is { $ref: string } { diff --git a/dashboard/src/widgets/BlueprintFlowWidget/BlueprintFlowWidget.tsx b/dashboard/src/widgets/BlueprintFlowWidget/BlueprintFlowWidget.tsx index 1b5a186a..8511654d 100644 --- a/dashboard/src/widgets/BlueprintFlowWidget/BlueprintFlowWidget.tsx +++ b/dashboard/src/widgets/BlueprintFlowWidget/BlueprintFlowWidget.tsx @@ -24,6 +24,7 @@ import { addStartNode, buildByNodeRef, buildDefinitionGraph, + canConnectNodes, getAncestorNodes, isBranchNodeType, isSwitchNodeType, @@ -1839,6 +1840,35 @@ function BlueprintFlowWidget({ setHasLocalChanges(true); }; + const handleConnectNode = (sourceNodeId: string, targetNodeId: string) => { + setGraphData((prev) => { + if (!prev) return prev; + const sourceNode = prev.nodes[sourceNodeId]; + const targetNode = prev.nodes[targetNodeId]; + if (!sourceNode || !targetNode) return prev; + if (sourceNode.nodeType !== "NORMAL") return prev; + if (!canConnectNodes(prev, sourceNodeId, targetNodeId)) return prev; + + const existingNextIds = nextIdsOf(sourceNode); + if (existingNextIds.includes(targetNodeId)) return prev; + + const newNextIds = [...existingNextIds, targetNodeId]; + const currentNodes = { ...prev.nodes }; + currentNodes[sourceNodeId] = { + ...sourceNode, + next: newNextIds.length === 1 ? newNextIds[0] : newNextIds, + raw: { ...sourceNode.raw, next: newNextIds }, + }; + + const newRoots = prev.roots.includes(targetNodeId) + ? prev.roots.filter((id) => id !== targetNodeId) + : prev.roots; + + return { ...prev, nodes: currentNodes, roots: newRoots }; + }); + setHasLocalChanges(true); + }; + const handleConnectParameter = ({ sourceNodeId, sourceField, @@ -2140,6 +2170,7 @@ function BlueprintFlowWidget({ showConnections={showConnections} onConnectParameter={handleConnectParameter} onDisconnectParameter={handleDisconnectParameter} + onConnectNode={handleConnectNode} onOverrideParameter={handleOverrideParameter} onToggleForceNode={handleToggleForceNode} liteMode={liteMode}