import React, { useCallback } from 'react'; import { Line, Path } from 'react-konva'; import { theme } from "antd"; import { LineProps } from "./data"; const { useToken } = theme; interface Props { node: LineProps; // 当前组件信息 getNodePosition: (val: string) => { x: number, y: number }; // 获取关联组件坐标 lineType: "line" | "smooth"; // 连线类型 折线/曲线,默认折线 style?: { color?: string; } } const Index = (props: Props) => { const { node, getNodePosition, lineType = "line", style } = props; const { sourceId, targetId } = node || {}; const { token } = useToken(); if (!sourceId || !targetId) return null; const sourcePos = getNodePosition(sourceId); const targetPos = getNodePosition(targetId); if (!sourcePos || !targetPos) return null; // 计算折线 const getPolylinePoints = useCallback(() => { const x = sourcePos.x; const y = sourcePos.y; const endX = targetPos.x; const endY = targetPos.y; const distX = endX - x; const midX = Math.abs(distX) < 80 ? x + distX / 2 : x + (distX > 0 ? 80 : -80); return [x, y, midX, y, midX, endY, endX, endY]; }, [sourcePos, targetPos]); // 计算曲线 const getSmoothPathData = useCallback(() => { const x1 = sourcePos.x; const y1 = sourcePos.y const x2 = targetPos.x; const y2 = targetPos.y; const deltaX = Math.abs(x2 - x1); const cp1x = x1 + Math.max(50, deltaX * 0.5); const cp1y = y1; const cp2x = x2 - 20; const cp2y = y2; return `M ${x1} ${y1} C ${cp1x} ${cp1y}, ${cp2x} ${cp2y}, ${x2} ${y2}`; }, [sourcePos, targetPos]); return ( lineType === "line" ? : ); }; export default Index;