Files
deerflow-code/offline-backend-20260512/backend/templates/web-app-template/src/components/FlowCanvas.tsx
T

173 lines
5.4 KiB
TypeScript

import { useMemo } from "react";
import {
Background,
Controls,
MarkerType,
MiniMap,
ReactFlow,
} from "@xyflow/react";
import type { Edge, Node } from "@xyflow/react";
import "./FlowCanvas.css";
/**
* 可交互流程图(基于 @xyflow/react):支持拖拽、缩放、小地图。
*
* 数据格式(与 DeerFlow flow-json 对齐,edges 两套字段名都接受):
* {
* direction: "TB" | "LR",
* nodes: [{ id: "n1", label: "开始", type?: "start|end|stage|process|decision|risk|deliverable" }],
* edges: [{ source: "n1", target: "n2", label?: "是" }]
* }
*/
export interface FlowNodeData {
id: string;
label: string;
type?: string;
}
export interface FlowEdgeData {
source: string;
target: string;
label?: string;
from?: string;
to?: string;
}
export interface FlowData {
direction?: "TB" | "LR";
nodes: FlowNodeData[];
edges: FlowEdgeData[];
}
const NODE_COLORS: Record<string, { bg: string; border: string; color: string }> = {
start: { bg: "#e8f7ef", border: "#17895a", color: "#116a45" },
end: { bg: "#e8f7ef", border: "#17895a", color: "#116a45" },
stage: { bg: "#e9efff", border: "#2f5cff", color: "#1f44bd" },
process: { bg: "#f0f3f8", border: "#8a93a6", color: "#1c2333" },
task: { bg: "#f0f3f8", border: "#8a93a6", color: "#1c2333" },
decision: { bg: "#fff6e5", border: "#b97509", color: "#8f5c07" },
risk: { bg: "#fdecec", border: "#d64545", color: "#b03535" },
deliverable: { bg: "#eef6ff", border: "#0d7cd6", color: "#0b61a8" },
role: { bg: "#f3efff", border: "#7353d4", color: "#5a3fb4" },
};
const DEFAULT_NODE_COLOR = { bg: "#f0f3f8", border: "#8a93a6", color: "#1c2333" };
/** 最长路径分层 + 同层顺序排布的简易自动布局 */
function layoutFlow(data: FlowData): { nodes: Node[]; edges: Edge[] } {
const vertical = (data.direction ?? "TB") === "TB";
const nodeIds = new Set(data.nodes.map((n) => n.id));
const normalizedEdges: { id: string; source: string; target: string; label?: string }[] = [];
const outgoing = new Map<string, string[]>();
const indegree = new Map<string, number>();
for (const n of data.nodes) {
indegree.set(n.id, 0);
outgoing.set(n.id, []);
}
let edgeSeq = 0;
for (const e of data.edges) {
const source = e.source ?? e.from ?? "";
const target = e.target ?? e.to ?? "";
if (!nodeIds.has(source) || !nodeIds.has(target) || source === target) continue;
normalizedEdges.push({ id: `e${edgeSeq++}`, source, target, label: e.label });
outgoing.get(source)!.push(target);
indegree.set(target, (indegree.get(target) ?? 0) + 1);
}
// Kahn 拓扑排序求层号(取最长路径)
const layer = new Map<string, number>();
const queue: string[] = [];
for (const [id, deg] of indegree) {
if (deg === 0) {
queue.push(id);
layer.set(id, 0);
}
}
let guard = 0;
const limit = nodeIds.size * (nodeIds.size + 1);
while (queue.length > 0 && guard++ < limit) {
const id = queue.shift()!;
const layerNo = layer.get(id) ?? 0;
for (const next of outgoing.get(id) ?? []) {
layer.set(next, Math.max(layer.get(next) ?? 0, layerNo + 1));
const deg = (indegree.get(next) ?? 1) - 1;
indegree.set(next, deg);
if (deg === 0) queue.push(next);
}
}
// 环上节点兜底:未分层的放第 0 层
for (const n of data.nodes) if (!layer.has(n.id)) layer.set(n.id, 0);
const byLayer = new Map<number, string[]>();
for (const n of data.nodes) {
const l = layer.get(n.id) ?? 0;
if (!byLayer.has(l)) byLayer.set(l, []);
byLayer.get(l)!.push(n.id);
}
const GAP_MAIN = 130;
const GAP_CROSS = 210;
const nodes: Node[] = data.nodes.map((n) => {
const l = layer.get(n.id) ?? 0;
const siblings = byLayer.get(l) ?? [];
const index = siblings.indexOf(n.id);
const offset = index - (siblings.length - 1) / 2;
const colors = NODE_COLORS[n.type ?? ""] ?? DEFAULT_NODE_COLOR;
return {
id: n.id,
position: vertical
? { x: offset * GAP_CROSS, y: l * GAP_MAIN }
: { x: l * GAP_CROSS, y: offset * GAP_MAIN },
data: { label: n.label },
style: {
background: colors.bg,
border: `1.5px solid ${colors.border}`,
color: colors.color,
borderRadius: 8,
padding: "8px 14px",
fontSize: 13,
fontWeight: 500,
width: "auto",
},
};
});
const edges: Edge[] = normalizedEdges.map((e) => ({
id: e.id,
source: e.source,
target: e.target,
label: e.label,
type: "smoothstep",
style: { stroke: "#9aa5ba", strokeWidth: 1.6 },
labelStyle: { fontSize: 11, fill: "#5a6478" },
labelBgStyle: { fill: "#ffffff" },
markerEnd: { type: MarkerType.ArrowClosed, color: "#9aa5ba" },
}));
return { nodes, edges };
}
export default function FlowCanvas({ data, height = 460 }: { data: FlowData; height?: number }) {
const { nodes, edges } = useMemo(() => layoutFlow(data), [data]);
return (
<div className="flow-canvas" style={{ height }}>
<ReactFlow
nodes={nodes}
edges={edges}
fitView
minZoom={0.2}
maxZoom={2}
proOptions={{ hideAttribution: true }}
nodesConnectable={false}
defaultEdgeOptions={{ type: "smoothstep" }}
>
<Background color="#dfe5ee" gap={18} />
<Controls showInteractive={false} />
{nodes.length > 12 ? <MiniMap pannable zoomable /> : null}
</ReactFlow>
</div>
);
}