feat: add interactive React Flow canvas and floating toolbars

This commit is contained in:
2026-11-06 00:52:07 +08:00
parent fec69c6e59
commit d4b45c5051
13 changed files with 778 additions and 36 deletions
+103
View File
@@ -0,0 +1,103 @@
import { applyEdgeChanges, applyNodeChanges } from "@xyflow/react";
import type { Edge, EdgeChange, Node, NodeChange, Viewport, XYPosition } from "@xyflow/react";
export type CanvasNode = Node<{
kind: "text" | "image" | "script";
title: string;
text: string;
src?: string;
}, "material">;
export type CanvasDocument = { schema_version: 1; nodes: CanvasNode[]; edges: Edge[]; viewport: Viewport };
export type CanvasHistory = { past: CanvasDocument[]; present: CanvasDocument; future: CanvasDocument[] };
export type CanvasAction =
| { type: "checkpoint" }
| { type: "replace"; document: CanvasDocument }
| { type: "nodes"; changes: NodeChange<CanvasNode>[] }
| { type: "edges"; changes: EdgeChange[] }
| { type: "edit"; id: string; text: string }
| { type: "viewport"; viewport: Viewport }
| { type: "undo" | "redo" };
export function emptyCanvas(): CanvasDocument {
return { schema_version: 1, nodes: [], edges: [], viewport: { x: 0, y: 0, zoom: 1 } };
}
export function canvasStorageKey(workspace: string, board: string) {
return `endoleap_canvas_v1:${workspace}:${board}`;
}
function recorded(state: CanvasHistory, present: CanvasDocument): CanvasHistory {
return { past: [...state.past, state.present].slice(-40), present, future: [] };
}
export function canvasReducer(state: CanvasHistory, action: CanvasAction): CanvasHistory {
switch (action.type) {
case "checkpoint": return recorded(state, state.present);
case "replace": return recorded(state, action.document);
case "nodes": {
const nodes = applyNodeChanges(action.changes, state.present.nodes);
const ids = new Set(nodes.map((node) => node.id));
const present = { ...state.present, nodes, edges: state.present.edges.filter((edge) => ids.has(edge.source) && ids.has(edge.target)) };
return action.changes.some((change) => change.type === "remove" || change.type === "add")
? recorded(state, present) : { ...state, present };
}
case "edges": {
// React Flow may report the same edge removal after its node was removed.
// Keep that notification from consuming a second undo step.
const changes = action.changes.filter((change) => change.type !== "remove" || state.present.edges.some((edge) => edge.id === change.id));
if (!changes.length) return state;
const present = { ...state.present, edges: applyEdgeChanges(changes, state.present.edges) };
return changes.some((change) => change.type === "remove" || change.type === "add")
? recorded(state, present) : { ...state, present };
}
case "edit": return { ...state, future: [], present: { ...state.present, nodes: state.present.nodes.map((node) => node.id === action.id ? { ...node, data: { ...node.data, text: action.text } } : node) } };
case "viewport": return { ...state, present: { ...state.present, viewport: action.viewport } };
case "undo": {
const last = state.past.at(-1);
return last ? { past: state.past.slice(0, -1), present: { ...last, viewport: state.present.viewport }, future: [state.present, ...state.future] } : state;
}
case "redo": {
const next = state.future[0];
return next ? { past: [...state.past, state.present].slice(-40), present: { ...next, viewport: state.present.viewport }, future: state.future.slice(1) } : state;
}
}
}
export function makeCanvasNode(kind: CanvasNode["data"]["kind"], position: XYPosition, src?: string): CanvasNode {
return {
id: crypto.randomUUID(), type: "material", position,
data: { kind, title: kind === "image" ? "视觉参考" : kind === "script" ? "脚本" : "灵感笔记", text: "", ...(src ? { src } : {}) },
};
}
function validImage(src: unknown): src is string {
if (typeof src !== "string") return false;
if (/^data:image\/(png|jpeg|webp|gif);base64,[A-Za-z0-9+/=]+$/.test(src)) return src.length < 3_000_000;
try { const url = new URL(src); return url.protocol === "https:" && !url.username && !url.password && src.length < 2048; }
catch { return false; }
}
export function decodeCanvas(raw: string): CanvasDocument {
if (raw.length > 5_000_000) throw new Error("图谱文件太大,请使用小于 5 MB 的文件。");
const value = JSON.parse(raw);
if (!value || value.schema_version !== 1 || !Array.isArray(value.nodes) || !Array.isArray(value.edges) || value.nodes.length > 500 || value.edges.length > 2000)
throw new Error("不是有效的啡跃图谱文件。");
const ids = new Set<string>();
const nodes: CanvasNode[] = value.nodes.map((node: CanvasNode) => {
if (!node || typeof node.id !== "string" || !/^[\w-]{1,128}$/.test(node.id) || ids.has(node.id) || !node.position ||
![node.position.x, node.position.y].every((n) => Number.isFinite(n) && Math.abs(n) <= 1_000_000) ||
!node.data || !["text", "image", "script"].includes(node.data.kind) || typeof node.data.title !== "string" || node.data.title.length > 300 ||
typeof node.data.text !== "string" || node.data.text.length > 100_000 || (node.data.kind === "image" && !validImage(node.data.src)))
throw new Error("图谱节点格式无效。");
ids.add(node.id);
return { id: node.id, type: "material", position: { x: node.position.x, y: node.position.y }, data: {
kind: node.data.kind, title: node.data.title, text: node.data.text, ...(node.data.kind === "image" ? { src: node.data.src } : {}),
} };
});
const edgeIds = new Set<string>();
const edges: Edge[] = value.edges.map((edge: Edge) => {
if (!edge || typeof edge.id !== "string" || !/^[\w-]{1,128}$/.test(edge.id) || edgeIds.has(edge.id) || !ids.has(edge.source) || !ids.has(edge.target))
throw new Error("图谱连线格式无效。");
edgeIds.add(edge.id);
return { id: edge.id, source: edge.source, target: edge.target };
});
const viewport = value.viewport;
if (!viewport || ![viewport.x, viewport.y, viewport.zoom].every(Number.isFinite) || viewport.zoom < 0.1 || viewport.zoom > 4)
throw new Error("图谱视图格式无效。");
return { schema_version: 1, nodes, edges, viewport: { x: viewport.x, y: viewport.y, zoom: viewport.zoom } };
}