import React, { useState } from "react"; import type { MouseEvent as ReactMouseEvent } from "react"; import { canvasThemes } from "@/lib/canvas-theme"; import { useThemeStore } from "@/stores/use-theme-store"; import { STORYBOARD_HEADER_HEIGHT, STORYBOARD_ROW_HEIGHT, storyboardTableHeight } from "@/components/canvas/canvas-script-node"; import type { CanvasConnection, CanvasNodeData, ConnectionHandle, Position } from "@/types/canvas"; export const ConnectionPath = React.memo(function ConnectionPath({ connection, from, to, fromScrollTop = 0, toScrollTop = 0, active, onSelect, onContextMenu, }: { connection: CanvasConnection; from: CanvasNodeData; to: CanvasNodeData; fromScrollTop?: number; toScrollTop?: number; active: boolean; onSelect: () => void; onContextMenu?: (event: ReactMouseEvent) => void; }) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; const [hovered, setHovered] = useState(false); const startX = from.position.x + from.width; const startY = connectionHandleY(from, connection.fromHandleId, fromScrollTop); const endX = to.position.x; const endY = connectionHandleY(to, connection.toHandleId, toScrollTop); const dx = Math.abs(endX - startX); const curvature = Math.max(dx * 0.5, 50); const pathD = `M ${startX} ${startY} C ${startX + curvature} ${startY}, ${endX - curvature} ${endY}, ${endX} ${endY}`; const emphasized = active || hovered; const gradientId = `canvas-flow-${connection.id.replace(/[^a-zA-Z0-9_-]/g, "")}`; return ( {emphasized ? : null} setHovered(true)} onMouseLeave={() => setHovered(false)} onClick={(event) => { event.stopPropagation(); onSelect(); }} onContextMenu={(event) => { event.preventDefault(); event.stopPropagation(); onContextMenu?.(event); }} /> {emphasized ? : null} ); }, (previous, next) => previous.connection === next.connection && previous.from === next.from && previous.to === next.to && previous.active === next.active && previous.fromScrollTop === next.fromScrollTop && previous.toScrollTop === next.toScrollTop); export function ActiveConnectionPath({ node, handle, mouseWorld, target, nodeScrollTop = 0 }: { node?: CanvasNodeData; handle: ConnectionHandle; mouseWorld: Position; target?: CanvasNodeData; nodeScrollTop?: number }) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; if (!node) return null; const startX = handle.handleType === "source" ? node.position.x + node.width : mouseWorld.x; const startY = handle.handleType === "source" ? connectionHandleY(node, handle.handleId, nodeScrollTop) : mouseWorld.y; const endX = handle.handleType === "source" ? mouseWorld.x : node.position.x; const endY = handle.handleType === "source" ? mouseWorld.y : connectionHandleY(node, handle.handleId, nodeScrollTop); const snappedStartX = handle.handleType === "target" && target ? target.position.x + target.width : startX; const snappedStartY = handle.handleType === "target" && target ? target.position.y + target.height / 2 : startY; const snappedEndX = handle.handleType === "source" && target ? target.position.x : endX; const snappedEndY = handle.handleType === "source" && target ? target.position.y + target.height / 2 : endY; const distance = Math.abs(snappedEndX - snappedStartX); const pathD = `M ${snappedStartX} ${snappedStartY} C ${snappedStartX + distance * 0.5} ${snappedStartY}, ${snappedEndX - distance * 0.5} ${snappedEndY}, ${snappedEndX} ${snappedEndY}`; return ; } function connectionHandleY(node: CanvasNodeData, handleId?: string, scrollTop = 0) { if (handleId === "storyboard:context") return node.position.y + node.height - (node.metadata?.storyboardComposerHeight || 104) / 2; if (!handleId?.startsWith("row:")) return node.position.y + node.height / 2; const rowId = handleId.slice(4); const index = (node.metadata?.storyboard?.rows || []).findIndex((row) => row.id === rowId); if (index < 0) return node.position.y + node.height / 2; const tableHeight = storyboardTableHeight(node.height, node.metadata?.storyboardComposerHeight); const localY = Math.min(Math.max(index * STORYBOARD_ROW_HEIGHT + STORYBOARD_ROW_HEIGHT / 2 - scrollTop, 4), tableHeight - 4); return node.position.y + STORYBOARD_HEADER_HEIGHT + localY; }