silkio-flowchart 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -2
  3. package/dist/FlowEdge.d.ts +45 -0
  4. package/dist/FlowEdge.js +390 -0
  5. package/dist/Shape.d.ts +48 -0
  6. package/dist/Shape.js +118 -0
  7. package/dist/ai-protocol.d.ts +146 -0
  8. package/dist/ai-protocol.js +579 -0
  9. package/dist/ai-report.d.ts +3 -0
  10. package/dist/ai-report.js +83 -0
  11. package/dist/ai.d.ts +6 -0
  12. package/dist/ai.js +7 -0
  13. package/dist/auto-layout.d.ts +15 -0
  14. package/dist/auto-layout.js +248 -0
  15. package/dist/commands.d.ts +106 -0
  16. package/dist/commands.js +264 -0
  17. package/dist/data.d.ts +24 -0
  18. package/dist/data.js +1 -0
  19. package/dist/edge-router.d.ts +24 -0
  20. package/dist/edge-router.js +22 -0
  21. package/dist/editing.d.ts +32 -0
  22. package/dist/editing.js +148 -0
  23. package/dist/flow-layout-capability.d.ts +7 -0
  24. package/dist/flow-layout-capability.js +2 -0
  25. package/dist/flowchart.d.ts +118 -0
  26. package/dist/flowchart.js +1446 -0
  27. package/dist/geometry.d.ts +98 -0
  28. package/dist/geometry.js +310 -0
  29. package/dist/history-codec.d.ts +47 -0
  30. package/dist/history-codec.js +410 -0
  31. package/dist/hover.d.ts +22 -0
  32. package/dist/hover.js +96 -0
  33. package/dist/index.d.ts +22 -0
  34. package/dist/index.js +18 -0
  35. package/dist/interactions.d.ts +51 -0
  36. package/dist/interactions.js +636 -0
  37. package/dist/model.d.ts +58 -0
  38. package/dist/model.js +233 -0
  39. package/dist/overlay.d.ts +60 -0
  40. package/dist/overlay.js +242 -0
  41. package/dist/quick-create.d.ts +12 -0
  42. package/dist/quick-create.js +29 -0
  43. package/dist/selection.d.ts +8 -0
  44. package/dist/selection.js +31 -0
  45. package/dist/session.d.ts +68 -0
  46. package/dist/session.js +4 -0
  47. package/dist/shape-registry.d.ts +17 -0
  48. package/dist/shape-registry.js +36 -0
  49. package/dist/shapes.d.ts +56 -0
  50. package/dist/shapes.js +291 -0
  51. package/dist/snap-capability.d.ts +23 -0
  52. package/dist/snap-capability.js +32 -0
  53. package/dist/snap.d.ts +34 -0
  54. package/dist/snap.js +370 -0
  55. package/package.json +36 -3
package/dist/ai.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export { defaultAIFlowPrompt, normalizeAIFlowGraph, normalizeAIFlowOps, parseAIFlowOps, parseAIFlowResponse, inferFlowShape, toSemanticGraph, } from "./ai-protocol";
2
+ export type { AIFlowRole, AIFlowNode, AIFlowEdge, AIFlowGraph, NormalizedFlowNode, NormalizedFlowGraph, AIFlowOp, AIFlowRenderOp, AIFlowCreateNodeOp, AIFlowUpdateEdgeOp, AIFlowUndoOp, AIFlowRedoOp, AIFlowHistoryOp, AIFlowAnswerOp, AIFlowSelection, AIFlowHistoryState, AIFlowStep, AIFlowTurn, AIFlowEditRequest, AIFlowGenerate, AIFlowRunOptions, AIFlowRunResult, } from "./ai-protocol";
3
+ export { layoutFlowGraph } from "./auto-layout";
4
+ export type { FlowLayoutDirection, FlowLayoutOptions } from "./auto-layout";
5
+ export { FLOW_AUTO_LAYOUT_CAPABILITY } from "./flow-layout-capability";
6
+ export type { FlowAutoLayoutCapability } from "./flow-layout-capability";
package/dist/ai.js ADDED
@@ -0,0 +1,7 @@
1
+ // AI 生成模块转发出口:语义图协议(提示词/解析/规整)、操作序列
2
+ // (整图重绘 / 方向性新建 / 撤销 / 重做 / 查历史 / 回答)与确定性自动布局。
3
+ // 领域包不绑定任何 LLM SDK,宿主经 FlowChartOptions.ai 注入生成适配器,
4
+ // 由 FlowChart.generate() 串起「操作序列 → FlowData → 原子整图替换 / 历史驱动」。
5
+ export { defaultAIFlowPrompt, normalizeAIFlowGraph, normalizeAIFlowOps, parseAIFlowOps, parseAIFlowResponse, inferFlowShape, toSemanticGraph, } from "./ai-protocol";
6
+ export { layoutFlowGraph } from "./auto-layout";
7
+ export { FLOW_AUTO_LAYOUT_CAPABILITY } from "./flow-layout-capability";
@@ -0,0 +1,15 @@
1
+ import type { FlowData } from "./data";
2
+ import type { AIFlowGraph } from "./ai-protocol";
3
+ export type FlowLayoutDirection = "TB" | "LR";
4
+ export interface FlowLayoutOptions {
5
+ direction?: FlowLayoutDirection;
6
+ nodeWidth?: number;
7
+ nodeHeight?: number;
8
+ layerGap?: number;
9
+ nodeGap?: number;
10
+ origin?: {
11
+ x: number;
12
+ y: number;
13
+ };
14
+ }
15
+ export declare function layoutFlowGraph(graph: AIFlowGraph, options?: FlowLayoutOptions): FlowData;
@@ -0,0 +1,248 @@
1
+ import { normalizeAIFlowGraph } from "./ai-protocol";
2
+ // 布局器本身是零运行时依赖的纯计算模块(便于直接单测);能力键与契约放在
3
+ // flow-layout-capability.ts,由门面按能力键查找第三方布局实现。
4
+ // PORT_STUB = 24(FlowEdge.ts):层间距至少要容下两端各一段引出直段
5
+ const MIN_LAYER_GAP = 56;
6
+ export function layoutFlowGraph(graph, options = {}) {
7
+ const g = normalizeAIFlowGraph(graph);
8
+ const direction = options.direction ?? "TB";
9
+ const nodeWidth = Math.max(40, options.nodeWidth ?? 160);
10
+ const nodeHeight = Math.max(30, options.nodeHeight ?? 99);
11
+ const layerGap = Math.max(MIN_LAYER_GAP, options.layerGap ?? 120);
12
+ const nodeGap = Math.max(8, options.nodeGap ?? 72);
13
+ const origin = options.origin ?? { x: 80, y: 80 };
14
+ const mainSize = direction === "TB" ? nodeHeight : nodeWidth;
15
+ const crossSize = direction === "TB" ? nodeWidth : nodeHeight;
16
+ const dummySize = Math.min(crossSize, 24);
17
+ const n = g.nodes.length;
18
+ const idToIdx = new Map(g.nodes.map((node, i) => [node.id, i]));
19
+ const edges = [];
20
+ for (const e of g.edges) {
21
+ const from = idToIdx.get(e.from);
22
+ const to = idToIdx.get(e.to);
23
+ if (from === undefined || to === undefined)
24
+ continue;
25
+ edges.push({
26
+ from,
27
+ to,
28
+ ...(e.text ? { text: e.text } : {}),
29
+ ...(e.fromDir ? { fromDir: e.fromDir } : {}),
30
+ ...(e.toDir ? { toDir: e.toDir } : {}),
31
+ });
32
+ }
33
+ const out = g.nodes.map(() => []);
34
+ edges.forEach((e, i) => out[e.from].push(i));
35
+ // ---- 1) 环检测:DFS 中指向「仍在栈上」的节点的边记为回边 ----
36
+ // 迭代 DFS 避免深图递归爆栈;回边不参与分层/排序,最后单独分配绕行端口
37
+ const state = new Uint8Array(n);
38
+ const back = new Set();
39
+ for (let start = 0; start < n; start++) {
40
+ if (state[start] !== 0)
41
+ continue;
42
+ state[start] = 1;
43
+ const stack = [{ u: start, k: 0 }];
44
+ while (stack.length) {
45
+ const top = stack[stack.length - 1];
46
+ if (top.k >= out[top.u].length) {
47
+ state[top.u] = 2;
48
+ stack.pop();
49
+ continue;
50
+ }
51
+ const ei = out[top.u][top.k++];
52
+ const v = edges[ei].to;
53
+ if (state[v] === 1)
54
+ back.add(ei);
55
+ else if (state[v] === 0) {
56
+ state[v] = 1;
57
+ stack.push({ u: v, k: 0 });
58
+ }
59
+ }
60
+ }
61
+ // ---- 2) 最长路径分层(Kahn 拓扑序)----
62
+ const indeg = new Array(n).fill(0);
63
+ edges.forEach((e, i) => {
64
+ if (!back.has(i))
65
+ indeg[e.to]++;
66
+ });
67
+ const layer = new Array(n).fill(0);
68
+ const queue = [];
69
+ for (let i = 0; i < n; i++)
70
+ if (indeg[i] === 0)
71
+ queue.push(i);
72
+ let head = 0;
73
+ while (head < queue.length) {
74
+ const u = queue[head++];
75
+ for (const ei of out[u]) {
76
+ if (back.has(ei))
77
+ continue;
78
+ const v = edges[ei].to;
79
+ layer[v] = Math.max(layer[v], layer[u] + 1);
80
+ indeg[v] = indeg[v] - 1;
81
+ if (indeg[v] === 0)
82
+ queue.push(v);
83
+ }
84
+ }
85
+ const maxLayer = n ? Math.max(...layer) : 0;
86
+ // ---- 3) 层内槽位 + 跨层长边的虚节点链 ----
87
+ const slots = Array.from({ length: maxLayer + 1 }, () => []);
88
+ const slotById = new Map();
89
+ for (let i = 0; i < n; i++) {
90
+ const s = {
91
+ id: g.nodes[i].id,
92
+ layer: layer[i],
93
+ real: true,
94
+ size: crossSize,
95
+ pos: 0,
96
+ };
97
+ slots[s.layer].push(s);
98
+ slotById.set(s.id, s);
99
+ }
100
+ // 无向邻接(虚节点链 + 真实端点),供重心排序查找相邻层邻居
101
+ const adjacency = new Map();
102
+ const link = (a, b) => {
103
+ if (!adjacency.has(a))
104
+ adjacency.set(a, new Set());
105
+ if (!adjacency.has(b))
106
+ adjacency.set(b, new Set());
107
+ adjacency.get(a).add(b);
108
+ adjacency.get(b).add(a);
109
+ };
110
+ let dummySeq = 0;
111
+ edges.forEach((e, i) => {
112
+ if (back.has(i))
113
+ return;
114
+ const lu = layer[e.from];
115
+ const lv = layer[e.to];
116
+ if (lv <= lu)
117
+ return;
118
+ let prev = g.nodes[e.from].id;
119
+ for (let l = lu + 1; l < lv; l++) {
120
+ let id;
121
+ do {
122
+ id = `__layout_dummy_${++dummySeq}`;
123
+ } while (slotById.has(id));
124
+ const dummy = { id, layer: l, real: false, size: dummySize, pos: 0 };
125
+ slots[l].push(dummy);
126
+ slotById.set(id, dummy);
127
+ link(prev, id);
128
+ prev = id;
129
+ }
130
+ link(prev, g.nodes[e.to].id);
131
+ });
132
+ // ---- 4) 重心法层内排序(上下交替若干轮,稳定排序保持模型给出的语义顺序)----
133
+ const position = new Map();
134
+ const refreshLayer = (l) => {
135
+ slots[l].forEach((s, i) => position.set(s.id, i));
136
+ };
137
+ for (let l = 0; l <= maxLayer; l++)
138
+ refreshLayer(l);
139
+ const reorder = (l, neighborLayer) => {
140
+ const list = slots[l];
141
+ if (list.length < 2)
142
+ return;
143
+ const before = new Map(list.map((s, i) => [s.id, i]));
144
+ const scores = new Map();
145
+ for (const s of list) {
146
+ const neighbors = [...(adjacency.get(s.id) ?? [])].filter((id) => slotById.get(id)?.layer === neighborLayer);
147
+ scores.set(s.id, neighbors.length
148
+ ? neighbors.reduce((acc, id) => acc + (position.get(id) ?? 0), 0) /
149
+ neighbors.length
150
+ : before.get(s.id));
151
+ }
152
+ list.sort((a, b) => {
153
+ const d = scores.get(a.id) - scores.get(b.id);
154
+ return Math.abs(d) < 1e-9 ? before.get(a.id) - before.get(b.id) : d;
155
+ });
156
+ refreshLayer(l);
157
+ };
158
+ for (let pass = 0; pass < 6; pass++) {
159
+ for (let l = 1; l <= maxLayer; l++)
160
+ reorder(l, l - 1);
161
+ for (let l = maxLayer - 1; l >= 0; l--)
162
+ reorder(l, l + 1);
163
+ }
164
+ // ---- 5) 坐标分配:各层按自身总宽在全局宽度内居中 ----
165
+ const layerWidths = slots.map((list) => list.reduce((sum, s) => sum + s.size, 0) +
166
+ Math.max(0, list.length - 1) * nodeGap);
167
+ const maxWidth = Math.max(0, ...layerWidths);
168
+ const mainOrigin = direction === "TB" ? origin.y : origin.x;
169
+ const crossOrigin = direction === "TB" ? origin.x : origin.y;
170
+ for (let l = 0; l <= maxLayer; l++) {
171
+ let cursor = crossOrigin + (maxWidth - layerWidths[l]) / 2;
172
+ for (const s of slots[l]) {
173
+ s.pos = cursor + s.size / 2;
174
+ cursor += s.size + nodeGap;
175
+ }
176
+ }
177
+ const nodes = g.nodes.map((node, i) => {
178
+ const s = slotById.get(node.id);
179
+ const main = mainOrigin + s.layer * (mainSize + layerGap);
180
+ return {
181
+ id: node.id,
182
+ shape: node.shape,
183
+ // TB:y 为层起点、x 由副轴中心回退半宽;LR 反之
184
+ x: Math.round(direction === "TB" ? s.pos - nodeWidth / 2 : main),
185
+ y: Math.round(direction === "TB" ? main : s.pos - nodeHeight / 2),
186
+ width: nodeWidth,
187
+ height: nodeHeight,
188
+ text: node.text,
189
+ };
190
+ });
191
+ // ---- 6) 端口方向:全部显式给出(FlowEdge 创建时冻结,事后移动不会重选边)----
192
+ // 前向边沿主方向出入;多分支按目标副轴位置散开到左/右(LR 为上/下)出口,
193
+ // 避免菱形多条边共用一个底部端口;回边统一走外侧同侧出入,由正交路由绕行。
194
+ // 语义边显式给出的方向优先:原样落边,不参与自动散开(同节点其余自动分支
195
+ // 避开已被占用的方向再分配,避免端口相撞)。
196
+ const branchFrom = new Map();
197
+ const forwardOut = new Map();
198
+ edges.forEach((e, i) => {
199
+ if (back.has(i) || layer[e.to] <= layer[e.from])
200
+ return;
201
+ forwardOut.set(e.from, [...(forwardOut.get(e.from) ?? []), i]);
202
+ });
203
+ const sideDirs = {
204
+ TB: { first: "left", mid: "bottom", last: "right" },
205
+ LR: { first: "top", mid: "right", last: "bottom" },
206
+ };
207
+ for (const list of forwardOut.values()) {
208
+ if (list.length < 2)
209
+ continue;
210
+ const auto = list.filter((ei) => !edges[ei].fromDir);
211
+ if (auto.length < 2)
212
+ continue;
213
+ // 已被显式方向占用的出口从候选序列中剔除;全被占满时退回主方向
214
+ const used = new Set(list.map((ei) => edges[ei].fromDir).filter((d) => !!d));
215
+ const dirs = sideDirs[direction];
216
+ const order = [dirs.first, dirs.mid, dirs.last].filter((d) => !used.has(d));
217
+ if (!order.length)
218
+ order.push(dirs.mid);
219
+ auto.sort((a, b) => slotById.get(g.nodes[edges[a].to].id).pos -
220
+ slotById.get(g.nodes[edges[b].to].id).pos);
221
+ auto.forEach((ei, k) => {
222
+ branchFrom.set(ei, k === 0
223
+ ? order[0]
224
+ : k === auto.length - 1
225
+ ? order[order.length - 1]
226
+ : (order[1] ?? order[0]));
227
+ });
228
+ }
229
+ const edgeData = edges.map((e, i) => {
230
+ const backward = back.has(i) || layer[e.to] <= layer[e.from];
231
+ const fromDir = e.fromDir ??
232
+ (backward
233
+ ? direction === "TB" ? "right" : "bottom"
234
+ : branchFrom.get(i) ?? (direction === "TB" ? "bottom" : "right"));
235
+ const toDir = e.toDir ??
236
+ (backward
237
+ ? direction === "TB" ? "right" : "bottom"
238
+ : direction === "TB" ? "top" : "left");
239
+ return {
240
+ from: g.nodes[e.from].id,
241
+ to: g.nodes[e.to].id,
242
+ fromDir,
243
+ toDir,
244
+ ...(e.text ? { text: e.text } : {}),
245
+ };
246
+ });
247
+ return { nodes, edges: edgeData };
248
+ }
@@ -0,0 +1,106 @@
1
+ import type { CommandCapability } from "@silkor/core";
2
+ import type { HistoryCommand } from "@silkor/core";
3
+ import type { FlowEdge, PortDir } from "./FlowEdge";
4
+ import type { Shape } from "./Shape";
5
+ import type { Bounds } from "./snap";
6
+ import type { FlowData, FlowNodeData } from "./data";
7
+ export declare function shapeData(shape: Shape): FlowNodeData;
8
+ export interface MovePayload {
9
+ v: "abs" | "delta";
10
+ before: Array<{
11
+ shapeId: string;
12
+ x: number;
13
+ y: number;
14
+ }>;
15
+ after?: Array<{
16
+ shapeId: string;
17
+ x: number;
18
+ y: number;
19
+ }>;
20
+ targets?: Array<string>;
21
+ dx?: number;
22
+ dy?: number;
23
+ }
24
+ export interface ConnectPayload {
25
+ edgeId: string;
26
+ fromId: string;
27
+ toId: string;
28
+ fromDir: PortDir;
29
+ toDir: PortDir;
30
+ text?: string;
31
+ }
32
+ export interface RenderSnapshot {
33
+ data: FlowData;
34
+ edgeIds: string[];
35
+ }
36
+ export interface RenderPayload {
37
+ before: RenderSnapshot;
38
+ after: RenderSnapshot;
39
+ }
40
+ interface ShapeRectData {
41
+ shapeId: string;
42
+ x: number;
43
+ y: number;
44
+ width: number;
45
+ height: number;
46
+ }
47
+ export type ResizePayload = {
48
+ v: "single";
49
+ shapeId: string;
50
+ before: Bounds;
51
+ after: Bounds;
52
+ } | {
53
+ v: "group";
54
+ before: ShapeRectData[];
55
+ after: ShapeRectData[];
56
+ };
57
+ export interface ReconnectPayload {
58
+ edgeId: string;
59
+ which: "from" | "to";
60
+ oldNodeId: string;
61
+ oldDir: PortDir;
62
+ newNodeId: string;
63
+ newDir: PortDir;
64
+ }
65
+ export interface TextPayload {
66
+ shapeId: string;
67
+ before: string;
68
+ after: string;
69
+ }
70
+ export interface EdgeTextPayload {
71
+ edgeId: string;
72
+ before: string;
73
+ after: string;
74
+ }
75
+ export interface FlowchartCommandHooks {
76
+ editing(): boolean;
77
+ editorFocused(): boolean;
78
+ hasSelection(): boolean;
79
+ undo(): void;
80
+ redo(): void;
81
+ deleteSelected(): void;
82
+ selectAll(): void;
83
+ copySelected(): void;
84
+ paste(): void;
85
+ }
86
+ export declare function bindFlowchartCommands(commands: CommandCapability, hooks: FlowchartCommandHooks): void;
87
+ export interface ShapeMove {
88
+ shape: Shape;
89
+ x: number;
90
+ y: number;
91
+ }
92
+ export interface ShapeRect {
93
+ shape: Shape;
94
+ x: number;
95
+ y: number;
96
+ width: number;
97
+ height: number;
98
+ }
99
+ export declare function moveCommand(before: ShapeMove[], after: ShapeMove[]): HistoryCommand;
100
+ export declare function nudgeCommand(targets: Shape[], dx: number, dy: number, before: ShapeMove[]): HistoryCommand;
101
+ export declare function resizeCommand(shape: Shape, before: Bounds, after: Bounds): HistoryCommand;
102
+ export declare function groupResizeCommand(before: ShapeRect[], after: ShapeRect[]): HistoryCommand;
103
+ export declare function reconnectCommand(edge: FlowEdge, which: "from" | "to", oldNode: Shape, oldDir: PortDir, newNode: Shape, newDir: PortDir): HistoryCommand;
104
+ export declare function textCommand(shape: Shape, before: string, after: string): HistoryCommand;
105
+ export declare function edgeTextCommand(edge: FlowEdge, before: string, after: string): HistoryCommand;
106
+ export {};
@@ -0,0 +1,264 @@
1
+ // 命令装配与可逆命令工厂:undo/redo/delete 注册为命令并绑定默认键位;
2
+ // 各类交互变更(移动/缩放/微调/重接/文本)的 HistoryCommand 在此构造——
3
+ // 只依赖调用方传入的数据快照,不触碰 FlowChart 内部状态
4
+ import { hasDocumentTextSelection } from "@silkor/core";
5
+ // 形状数据快照:id/几何/文本(与 model.toJSON 的节点条目同构,JSON 安全),
6
+ // 供命令载荷内嵌,导入端经形状注册表重建。
7
+ // 导出供 flowchart.ts / interactions.ts 的内联命令与 history-codec 共用
8
+ export function shapeData(shape) {
9
+ return {
10
+ id: shape.id ?? undefined,
11
+ shape: shape.kind,
12
+ x: shape.x,
13
+ y: shape.y,
14
+ width: shape.width,
15
+ height: shape.height,
16
+ text: shape.text.text,
17
+ };
18
+ }
19
+ // 注册流程图命令并绑定默认键位:撤销/重做/删除,document keydown 由命令
20
+ // 插件统一分发;应用层与第三方可按能力键复用命令或重绑键位。
21
+ // 复制绑定 mod+c:命令经剪贴板能力写系统剪贴板(Chrome 无文本选区时 Ctrl+C
22
+ // 不派发 copy 事件,需命令路径兜底);粘贴不绑 mod+v——走原生 paste 事件
23
+ // (Ctrl+V 必发、clipboardData 同步可读、零权限,绑定命令反而 preventDefault
24
+ // 抑制事件)。右键菜单复制粘贴走 copy/paste 事件(Clipboard 插件分发,编辑态
25
+ // 自动放行文本级复制粘贴)。命令 id 同时供工具栏/菜单等程序化入口复用。
26
+ // 分发守卫:编辑会话中按键属于输入,一律不拦;激活态常驻覆盖层持焦(空缓冲
27
+ // 接收打字),无修饰键按键属于打字唤醒交由覆盖层,Ctrl/Cmd/Alt 组合键
28
+ // (撤销/全选等)仍走命令分发
29
+ export function bindFlowchartCommands(commands, hooks) {
30
+ commands.guard = (e) => {
31
+ // 焦点在原生表单控件(AI 输入框、文档重命名框、配置输入框等)时按键属于
32
+ // 文本输入,命令一律不拦截——否则裸键 delete/backspace 会被全局删除命令
33
+ // 劫持(preventDefault 吞掉删字),输入框无法正常编辑
34
+ const t = e.target;
35
+ if (t instanceof HTMLElement && t.matches("input, textarea, select"))
36
+ return false;
37
+ // 文档内存在非折叠文字选区(编辑覆盖层选中文字,或宿主页面其它区域如 AI
38
+ // 对话面板)时,mod+c/mod+x 属于原生复制/剪切,命令不拦截——否则面板里
39
+ // 选中的文字复制出来是画布激活节点(与 mindmap 键盘守卫同语义)
40
+ if ((e.ctrlKey || e.metaKey) &&
41
+ (e.key === "c" || e.key === "C" || e.key === "x" || e.key === "X") &&
42
+ hasDocumentTextSelection())
43
+ return false;
44
+ if (hooks.editing())
45
+ return false;
46
+ if (hooks.editorFocused() && !(e.ctrlKey || e.metaKey || e.altKey))
47
+ return false;
48
+ return true;
49
+ };
50
+ commands.register({
51
+ id: "flowchart.undo",
52
+ title: "撤销",
53
+ run: () => hooks.undo(),
54
+ });
55
+ commands.register({
56
+ id: "flowchart.redo",
57
+ title: "重做",
58
+ run: () => hooks.redo(),
59
+ });
60
+ commands.register({
61
+ id: "flowchart.delete",
62
+ title: "删除选中",
63
+ enabled: () => hooks.hasSelection(),
64
+ run: () => hooks.deleteSelected(),
65
+ });
66
+ commands.register({
67
+ id: "flowchart.selectAll",
68
+ title: "全选",
69
+ run: () => hooks.selectAll(),
70
+ });
71
+ commands.register({
72
+ id: "flowchart.copy",
73
+ title: "复制选中",
74
+ enabled: () => hooks.hasSelection(),
75
+ run: () => hooks.copySelected(),
76
+ });
77
+ commands.register({
78
+ id: "flowchart.paste",
79
+ title: "粘贴",
80
+ run: () => hooks.paste(),
81
+ });
82
+ commands.bind("mod+z", "flowchart.undo");
83
+ commands.bind("mod+shift+z", "flowchart.redo");
84
+ commands.bind("mod+y", "flowchart.redo");
85
+ commands.bind("mod+a", "flowchart.selectAll");
86
+ commands.bind("mod+c", "flowchart.copy");
87
+ commands.bind("delete", "flowchart.delete");
88
+ commands.bind("backspace", "flowchart.delete");
89
+ }
90
+ // 拖拽移动命令:before/after 各定格一份位置,undo 回到按前、redo 重放拖后
91
+ export function moveCommand(before, after) {
92
+ return {
93
+ type: "move",
94
+ redo: () => {
95
+ for (const m of after) {
96
+ m.shape.x = m.x;
97
+ m.shape.y = m.y;
98
+ }
99
+ },
100
+ undo: () => {
101
+ for (const m of before) {
102
+ m.shape.x = m.x;
103
+ m.shape.y = m.y;
104
+ }
105
+ },
106
+ // 序列化载荷:形状按 id 引用 + 绝对位置快照,undo/redo 重放绝对位置
107
+ toJSON: () => ({
108
+ kind: "flowchart.move",
109
+ payload: {
110
+ v: "abs",
111
+ before: before.map((m) => ({ shapeId: m.shape.id, x: m.x, y: m.y })),
112
+ after: after.map((m) => ({ shapeId: m.shape.id, x: m.x, y: m.y })),
113
+ },
114
+ }),
115
+ };
116
+ }
117
+ // 方向键微调命令:redo 重放同向位移,undo 回到按前位置
118
+ export function nudgeCommand(targets, dx, dy, before) {
119
+ return {
120
+ type: "move",
121
+ redo: () => {
122
+ for (const s of targets) {
123
+ s.x += dx;
124
+ s.y += dy;
125
+ }
126
+ },
127
+ undo: () => {
128
+ for (const m of before) {
129
+ m.shape.x = m.x;
130
+ m.shape.y = m.y;
131
+ }
132
+ },
133
+ // 序列化载荷:形状按 id 引用 + 相对位移,保留微调的相对语义
134
+ toJSON: () => ({
135
+ kind: "flowchart.move",
136
+ payload: {
137
+ v: "delta",
138
+ targets: targets.map((s) => s.id),
139
+ dx,
140
+ dy,
141
+ before: before.map((m) => ({ shapeId: m.shape.id, x: m.x, y: m.y })),
142
+ },
143
+ }),
144
+ };
145
+ }
146
+ // 单形状缩放命令:before/after 各定格一份 bounds
147
+ export function resizeCommand(shape, before, after) {
148
+ return {
149
+ type: "resize",
150
+ redo: () => {
151
+ Object.assign(shape, after);
152
+ shape.layout();
153
+ },
154
+ undo: () => {
155
+ Object.assign(shape, before);
156
+ shape.layout();
157
+ },
158
+ // 序列化载荷:形状按 id 引用 + 前后 bounds 快照
159
+ toJSON: () => ({
160
+ kind: "flowchart.resize",
161
+ payload: {
162
+ v: "single",
163
+ shapeId: shape.id,
164
+ before,
165
+ after,
166
+ },
167
+ }),
168
+ };
169
+ }
170
+ // 多选组合缩放命令:逐成员恢复位置与尺寸
171
+ export function groupResizeCommand(before, after) {
172
+ return {
173
+ type: "resize",
174
+ redo: () => {
175
+ for (const m of after) {
176
+ Object.assign(m.shape, {
177
+ x: m.x,
178
+ y: m.y,
179
+ width: m.width,
180
+ height: m.height,
181
+ });
182
+ m.shape.layout();
183
+ }
184
+ },
185
+ undo: () => {
186
+ for (const m of before) {
187
+ Object.assign(m.shape, {
188
+ x: m.x,
189
+ y: m.y,
190
+ width: m.width,
191
+ height: m.height,
192
+ });
193
+ m.shape.layout();
194
+ }
195
+ },
196
+ // 序列化载荷:逐成员按 id 引用 + 前后几何快照
197
+ toJSON: () => ({
198
+ kind: "flowchart.resize",
199
+ payload: {
200
+ v: "group",
201
+ before: before.map((m) => ({ shapeId: m.shape.id, x: m.x, y: m.y, width: m.width, height: m.height })),
202
+ after: after.map((m) => ({ shapeId: m.shape.id, x: m.x, y: m.y, width: m.width, height: m.height })),
203
+ },
204
+ }),
205
+ };
206
+ }
207
+ // 连线端点重接命令:undo/redo 对称重接(节点 + 端口方向)
208
+ export function reconnectCommand(edge, which, oldNode, oldDir, newNode, newDir) {
209
+ return {
210
+ type: "reconnect",
211
+ redo: () => edge.reattachEnd(which, newNode, newDir),
212
+ undo: () => edge.reattachEnd(which, oldNode, oldDir),
213
+ // 序列化载荷:连线与两端节点按 id 引用,前后端口方向为纯数据
214
+ toJSON: () => ({
215
+ kind: "flowchart.reconnect",
216
+ payload: {
217
+ edgeId: edge.id,
218
+ which,
219
+ oldNodeId: oldNode.id,
220
+ oldDir,
221
+ newNodeId: newNode.id,
222
+ newDir,
223
+ },
224
+ }),
225
+ };
226
+ }
227
+ // 文本编辑命令:文字相对会话起点变化时入栈,layout 重测让居中落定
228
+ export function textCommand(shape, before, after) {
229
+ return {
230
+ type: "text",
231
+ redo: () => {
232
+ shape.text.text = after;
233
+ shape.layout();
234
+ },
235
+ undo: () => {
236
+ shape.text.text = before;
237
+ shape.layout();
238
+ },
239
+ // 序列化载荷:形状按 id 引用 + 前后文本快照
240
+ toJSON: () => ({
241
+ kind: "flowchart.text",
242
+ payload: { shapeId: shape.id, before, after },
243
+ }),
244
+ };
245
+ }
246
+ // 连线标签编辑命令:redo/undo 对称改写标签文本并重排(重测 + 折线中点居中)
247
+ export function edgeTextCommand(edge, before, after) {
248
+ return {
249
+ type: "text",
250
+ redo: () => {
251
+ edge.label.text = after;
252
+ edge.layout();
253
+ },
254
+ undo: () => {
255
+ edge.label.text = before;
256
+ edge.layout();
257
+ },
258
+ // 序列化载荷:连线按 id 引用 + 前后文本快照
259
+ toJSON: () => ({
260
+ kind: "flowchart.edgeText",
261
+ payload: { edgeId: edge.id, before, after },
262
+ }),
263
+ };
264
+ }
package/dist/data.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ import type { PortDir } from './FlowEdge';
2
+ export type BuiltinShapeKind = 'rect' | 'diamond' | 'ellipse' | 'parallelogram' | 'hexagon' | 'database';
3
+ export type ShapeKind = BuiltinShapeKind | (string & {});
4
+ export interface FlowNodeData {
5
+ id?: string;
6
+ shape: ShapeKind;
7
+ x: number;
8
+ y: number;
9
+ width?: number;
10
+ height?: number;
11
+ text: string;
12
+ }
13
+ export interface FlowEdgeData {
14
+ from: string;
15
+ to: string;
16
+ fromDir?: PortDir;
17
+ toDir?: PortDir;
18
+ text?: string;
19
+ }
20
+ export interface FlowData {
21
+ nodes: FlowNodeData[];
22
+ edges: FlowEdgeData[];
23
+ }
24
+ export type FlowChangeType = 'add' | 'delete' | 'move' | 'resize' | 'connect' | 'reconnect' | 'text' | 'replace' | 'ai';
package/dist/data.js ADDED
@@ -0,0 +1 @@
1
+ export {};