2025-11-03 21:22:16 +08:00
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
|
import { ConnectionRenderer } from './ConnectionRenderer';
|
2025-11-18 14:46:51 +08:00
|
|
|
import { ConnectionViewData } from '../../types';
|
|
|
|
|
import { Node } from '../../domain/models/Node';
|
|
|
|
|
import { Connection } from '../../domain/models/Connection';
|
2025-11-03 21:22:16 +08:00
|
|
|
|
|
|
|
|
interface ConnectionLayerProps {
|
|
|
|
|
connections: Connection[];
|
|
|
|
|
nodes: Node[];
|
|
|
|
|
selectedConnection?: { from: string; to: string } | null;
|
|
|
|
|
getPortPosition: (nodeId: string, propertyName?: string, portType?: 'input' | 'output') => { x: number; y: number } | null;
|
|
|
|
|
onConnectionClick?: (e: React.MouseEvent, fromId: string, toId: string) => void;
|
|
|
|
|
onConnectionContextMenu?: (e: React.MouseEvent, fromId: string, toId: string) => void;
|
|
|
|
|
}
|
|
|
|
|
export const ConnectionLayer: React.FC<ConnectionLayerProps> = ({
|
|
|
|
|
connections,
|
|
|
|
|
nodes,
|
|
|
|
|
selectedConnection,
|
|
|
|
|
getPortPosition,
|
|
|
|
|
onConnectionClick,
|
|
|
|
|
onConnectionContextMenu
|
|
|
|
|
}) => {
|
|
|
|
|
const nodeMap = useMemo(() => {
|
|
|
|
|
return new Map(nodes.map((node) => [node.id, node]));
|
|
|
|
|
}, [nodes]);
|
|
|
|
|
|
|
|
|
|
const connectionViewData = useMemo(() => {
|
|
|
|
|
return connections
|
|
|
|
|
.map((connection) => {
|
|
|
|
|
const fromNode = nodeMap.get(connection.from);
|
|
|
|
|
const toNode = nodeMap.get(connection.to);
|
|
|
|
|
|
|
|
|
|
if (!fromNode || !toNode) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-18 14:46:51 +08:00
|
|
|
return { connection, fromNode, toNode };
|
2025-11-03 21:22:16 +08:00
|
|
|
})
|
|
|
|
|
.filter((item): item is NonNullable<typeof item> => item !== null);
|
2025-11-18 14:46:51 +08:00
|
|
|
}, [connections, nodeMap]);
|
|
|
|
|
|
|
|
|
|
const isConnectionSelected = (connection: { from: string; to: string }) => {
|
|
|
|
|
return selectedConnection?.from === connection.from &&
|
|
|
|
|
selectedConnection?.to === connection.to;
|
|
|
|
|
};
|
2025-11-03 21:22:16 +08:00
|
|
|
|
|
|
|
|
if (connectionViewData.length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<svg
|
|
|
|
|
className="connection-layer"
|
|
|
|
|
style={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
inset: 0,
|
|
|
|
|
pointerEvents: 'none',
|
2025-11-18 14:46:51 +08:00
|
|
|
overflow: 'visible',
|
|
|
|
|
zIndex: 0
|
2025-11-03 21:22:16 +08:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<g style={{ pointerEvents: 'auto' }}>
|
2025-11-18 14:46:51 +08:00
|
|
|
{connectionViewData.map(({ connection, fromNode, toNode }) => {
|
|
|
|
|
const viewData: ConnectionViewData = {
|
|
|
|
|
connection,
|
|
|
|
|
isSelected: isConnectionSelected(connection)
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<ConnectionRenderer
|
|
|
|
|
key={`${connection.from}-${connection.to}`}
|
|
|
|
|
connectionData={viewData}
|
|
|
|
|
fromNode={fromNode}
|
|
|
|
|
toNode={toNode}
|
|
|
|
|
getPortPosition={getPortPosition}
|
|
|
|
|
onClick={onConnectionClick}
|
|
|
|
|
onContextMenu={onConnectionContextMenu}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2025-11-03 21:22:16 +08:00
|
|
|
</g>
|
|
|
|
|
</svg>
|
|
|
|
|
);
|
|
|
|
|
};
|