silkio-flowchart 0.0.0-stage → 0.1.0
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.
- package/LICENSE +15 -0
- package/README.md +38 -3
- package/dist/FlowEdge.d.ts +45 -0
- package/dist/FlowEdge.js +390 -0
- package/dist/Shape.d.ts +48 -0
- package/dist/Shape.js +118 -0
- package/dist/ai-protocol.d.ts +146 -0
- package/dist/ai-protocol.js +578 -0
- package/dist/ai-report.d.ts +3 -0
- package/dist/ai-report.js +83 -0
- package/dist/ai.d.ts +6 -0
- package/dist/ai.js +7 -0
- package/dist/auto-layout.d.ts +15 -0
- package/dist/auto-layout.js +248 -0
- package/dist/commands.d.ts +106 -0
- package/dist/commands.js +264 -0
- package/dist/data.d.ts +24 -0
- package/dist/data.js +1 -0
- package/dist/edge-router.d.ts +24 -0
- package/dist/edge-router.js +22 -0
- package/dist/editing.d.ts +32 -0
- package/dist/editing.js +148 -0
- package/dist/flow-layout-capability.d.ts +7 -0
- package/dist/flow-layout-capability.js +2 -0
- package/dist/flowchart.d.ts +115 -0
- package/dist/flowchart.js +1425 -0
- package/dist/geometry.d.ts +98 -0
- package/dist/geometry.js +310 -0
- package/dist/history-codec.d.ts +47 -0
- package/dist/history-codec.js +410 -0
- package/dist/hover.d.ts +22 -0
- package/dist/hover.js +96 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +18 -0
- package/dist/interactions.d.ts +51 -0
- package/dist/interactions.js +636 -0
- package/dist/model.d.ts +58 -0
- package/dist/model.js +233 -0
- package/dist/overlay.d.ts +60 -0
- package/dist/overlay.js +242 -0
- package/dist/quick-create.d.ts +12 -0
- package/dist/quick-create.js +29 -0
- package/dist/selection.d.ts +8 -0
- package/dist/selection.js +31 -0
- package/dist/session.d.ts +68 -0
- package/dist/session.js +4 -0
- package/dist/shape-registry.d.ts +17 -0
- package/dist/shape-registry.js +36 -0
- package/dist/shapes.d.ts +56 -0
- package/dist/shapes.js +291 -0
- package/dist/snap-capability.d.ts +23 -0
- package/dist/snap-capability.js +32 -0
- package/dist/snap.d.ts +34 -0
- package/dist/snap.js +370 -0
- package/package.json +32 -3
|
@@ -0,0 +1,1425 @@
|
|
|
1
|
+
import { App, VIEWPORT_CAPABILITY, TEXT_EDITOR_CAPABILITY } from "@silkor/core";
|
|
2
|
+
import { selectionCapabilityKey, HISTORY_CAPABILITY } from "@silkor/core";
|
|
3
|
+
import { EXPORT_CAPABILITY, COMMAND_CAPABILITY, CANVAS_CLICK_CAPABILITY } from "@silkor/core";
|
|
4
|
+
import { compose } from "@silkor/core";
|
|
5
|
+
import { CLIPBOARD_CAPABILITY, hasDocumentTextSelection } from "@silkor/core";
|
|
6
|
+
import { officialPlugins } from "@silkor/preset";
|
|
7
|
+
import { BuiltinShapes, SHAPE_REGISTRY_CAPABILITY } from "./shape-registry";
|
|
8
|
+
import { FlowEdge, OPPOSITE_DIR } from "./FlowEdge";
|
|
9
|
+
import { mergeFlowGeometry, parseAIFlowOps, toSemanticGraph } from "./ai-protocol";
|
|
10
|
+
import { createNodeInDirection } from "./quick-create";
|
|
11
|
+
import { describeHistoryCommand } from "./ai-report";
|
|
12
|
+
import { layoutFlowGraph } from "./auto-layout";
|
|
13
|
+
import { FLOW_AUTO_LAYOUT_CAPABILITY } from "./flow-layout-capability";
|
|
14
|
+
import { BuiltinSnap, SNAP_CAPABILITY } from "./snap-capability";
|
|
15
|
+
import { BuiltinEdgeRouter, EDGE_ROUTER_CAPABILITY } from "./edge-router";
|
|
16
|
+
import { SelectionOverlay } from "./overlay";
|
|
17
|
+
import { dominantPortDir, marqueeHits, portPos, unionBounds } from "./geometry";
|
|
18
|
+
import { bindFlowchartCommands, edgeTextCommand, reconnectCommand, shapeData, textCommand } from "./commands";
|
|
19
|
+
import { createFlowchartCodec } from "./history-codec";
|
|
20
|
+
import { FlowModel } from "./model";
|
|
21
|
+
import { FlowEditing } from "./editing";
|
|
22
|
+
import { HoverFeedback } from "./hover";
|
|
23
|
+
import { FlowInteractions } from "./interactions";
|
|
24
|
+
import { multiBounds as multiBoundsOf, selEdge as selEdgeOf, selShape as selShapeOf, selShapes as selShapesOf, } from "./selection";
|
|
25
|
+
// 流程图场景门面:组装视口 / 文本编辑插件与各内聚模块(模型/编辑/悬浮/交互),
|
|
26
|
+
// 持有装配与选中语义,内置交互:点选、拖拽移动、单击已选中节点进入编辑
|
|
27
|
+
//(按点击位置聚焦光标)、端口拖拽连线、Delete 删除。
|
|
28
|
+
// 依赖单向:门面 → interactions/editing/hover → model → Shape/FlowEdge/core
|
|
29
|
+
export class FlowChart {
|
|
30
|
+
options;
|
|
31
|
+
// 装配与渲染属主:私有——外部经门面语义 API(screenToWorld/fit/toImage…)消费。
|
|
32
|
+
// 默认由场景自建(便捷模式),显式装配时经 FlowChartOptions.app 传入;
|
|
33
|
+
// 外部不深入内部状态(如 camera.viewport)
|
|
34
|
+
app;
|
|
35
|
+
// 便捷模式下场景自建宿主并安装的官方预设插件:destroy 时对称卸载;
|
|
36
|
+
// 显式装配下为空(宿主插件属宿主所有,保持在线可复用)
|
|
37
|
+
hostPlugins = [];
|
|
38
|
+
ownsApp = false;
|
|
39
|
+
editor;
|
|
40
|
+
// 模型:形状表与连线表的唯一属主(建载/增删/顶替/z 序维护)
|
|
41
|
+
model;
|
|
42
|
+
// 选中能力(Selection 插件 install 时注册):单选/多选模型统一存取,
|
|
43
|
+
// items 为形状或连线(多选组合只含形状);selected 高亮与置顶联动由本场景维护
|
|
44
|
+
sel;
|
|
45
|
+
// 文本编辑会话(编辑态与会话起点快照)
|
|
46
|
+
editing;
|
|
47
|
+
// 悬浮反馈(悬浮端口/悬浮连线状态)
|
|
48
|
+
hover;
|
|
49
|
+
// 交互会话控制器(会话/吸附/框选组合框状态属主)
|
|
50
|
+
interactions;
|
|
51
|
+
// 选中框覆盖层(常驻顶层,App.addOverlay 注册)
|
|
52
|
+
overlay;
|
|
53
|
+
// 构造期安装的插件(官方形状/吸附/边路由 + 应用层第三方):destroy 时对称卸载
|
|
54
|
+
scenePlugins = [];
|
|
55
|
+
onChange;
|
|
56
|
+
// 命令模式历史记录:实现在 @silkor/plugin-history,构造器内安装插件后按能力键消费;
|
|
57
|
+
// 各变更入口把"改模型"抽成可逆命令入栈,undo/redo 时重放/逆放
|
|
58
|
+
history;
|
|
59
|
+
// 形状注册表(BuiltinShapes 插件 install 时注册,第三方形状插件可追加/覆盖条目):
|
|
60
|
+
// 按 kind 创建形状,注册表缺失属于装配错误(require 兜底),未知 kind 数据按降级跳过
|
|
61
|
+
registry;
|
|
62
|
+
// 吸附能力(BuiltinSnap 插件 install 时注册,第三方吸附插件可同名覆盖):
|
|
63
|
+
// 拖拽对齐/等间距吸附,契约输入载体与候选 bounds + 视口缩放,输出位移与参考线
|
|
64
|
+
snapper;
|
|
65
|
+
// 边路由能力(BuiltinEdgeRouter 插件 install 时注册,第三方路由插件可同名覆盖):
|
|
66
|
+
// 创建连线时注入每个 FlowEdge,路由与渲染均按契约消费
|
|
67
|
+
router;
|
|
68
|
+
cleanups = [];
|
|
69
|
+
// 已拆卸标志:构造器排入的初始 fit 帧回调在 destroy 后不再执行
|
|
70
|
+
disposed = false;
|
|
71
|
+
// 剪贴板能力(Clipboard 插件 install 时注册,未装配时为 null——复制/粘贴降级为
|
|
72
|
+
// 无操作):场景以 consumer 身份参与原生 copy/paste 事件分发
|
|
73
|
+
clipboard;
|
|
74
|
+
// 连续粘贴错开计数(复制时归零):多次粘贴逐次偏移,避免完全重叠
|
|
75
|
+
pasteCount = 0;
|
|
76
|
+
constructor(options) {
|
|
77
|
+
this.options = options;
|
|
78
|
+
this.onChange = options.onChange;
|
|
79
|
+
if (options.app) {
|
|
80
|
+
// 显式装配(高级用法):复用宿主自建并装配好官方插件的 App,
|
|
81
|
+
// 本场景只按能力键复用能力、补齐自有插件,装配缺口由 require 兜底报错
|
|
82
|
+
this.app = options.app;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// 便捷模式(默认):场景自建 App 并按官方预设装配,使用方无需接触
|
|
86
|
+
// @silkor/core / @silkor/preset;场景只拥有自己创建的插件与宿主
|
|
87
|
+
this.app = new App(options.canvas);
|
|
88
|
+
this.ownsApp = true;
|
|
89
|
+
this.hostPlugins = officialPlugins();
|
|
90
|
+
for (const p of this.hostPlugins)
|
|
91
|
+
this.app.use(p);
|
|
92
|
+
}
|
|
93
|
+
this.history = this.app.require(HISTORY_CAPABILITY);
|
|
94
|
+
// 安装官方形状/吸附/边路由插件(自注册各自能力),再装配应用层传入的第三方
|
|
95
|
+
// 插件(可注册/覆盖形状、同名覆盖吸附策略与边路由,初始数据 load 前生效);
|
|
96
|
+
// 此后仅按能力键消费。安装清单留存,destroy 时对称卸载
|
|
97
|
+
this.scenePlugins = [
|
|
98
|
+
new BuiltinShapes(),
|
|
99
|
+
new BuiltinSnap(),
|
|
100
|
+
new BuiltinEdgeRouter(),
|
|
101
|
+
...(options.plugins ?? []),
|
|
102
|
+
];
|
|
103
|
+
for (const p of this.scenePlugins)
|
|
104
|
+
this.app.use(p);
|
|
105
|
+
this.registry = this.app.require(SHAPE_REGISTRY_CAPABILITY);
|
|
106
|
+
this.snapper = this.app.require(SNAP_CAPABILITY);
|
|
107
|
+
this.router = this.app.require(EDGE_ROUTER_CAPABILITY);
|
|
108
|
+
// 显式声明对视口能力的依赖:命中换算与覆盖层绘制都按视口缩放反算
|
|
109
|
+
this.app.require(VIEWPORT_CAPABILITY);
|
|
110
|
+
// 显式声明对点击分发能力的依赖:节点 onClick 与空白点击均由其分发
|
|
111
|
+
// (显式装配模式下由宿主负责安装 Clicker)
|
|
112
|
+
this.app.require(CANVAS_CLICK_CAPABILITY);
|
|
113
|
+
// 撤销/重做/删除/复制/粘贴注册为命令:撤销/删除经 document keydown 由命令
|
|
114
|
+
// 插件统一分发;复制/粘贴走原生剪贴板事件(键位不绑定,命令供工具栏等
|
|
115
|
+
// 程序化入口复用);应用层与第三方可按能力键复用命令或重绑键位
|
|
116
|
+
bindFlowchartCommands(this.app.require(COMMAND_CAPABILITY), {
|
|
117
|
+
editing: () => this.editing.editing !== null,
|
|
118
|
+
editorFocused: () => this.editor.hasFocus(),
|
|
119
|
+
hasSelection: () => this.sel.items.length > 0,
|
|
120
|
+
undo: () => this.undo(),
|
|
121
|
+
redo: () => this.redo(),
|
|
122
|
+
deleteSelected: () => this.deleteSelected(),
|
|
123
|
+
selectAll: () => this.selectAll(),
|
|
124
|
+
copySelected: () => this.copySelected(),
|
|
125
|
+
paste: () => this.paste(),
|
|
126
|
+
});
|
|
127
|
+
// 剪贴板接线:场景以 consumer 身份注册到 Clipboard 插件(原生 copy/paste 事件
|
|
128
|
+
// 分发;未装配插件时 clipboard 为 null,复制/粘贴降级为无操作)
|
|
129
|
+
this.clipboard = this.app.capability(CLIPBOARD_CAPABILITY) ?? null;
|
|
130
|
+
if (this.clipboard) {
|
|
131
|
+
this.cleanups.push(this.clipboard.register({
|
|
132
|
+
kind: "flowchart",
|
|
133
|
+
// copy 全权决策(场景 UX,机制层不窥探选区/编辑状态),选区语义与
|
|
134
|
+
// mindmap 收口一致:"有选区即原生,无选区画布接管"——文档内存在非折叠
|
|
135
|
+
// 文字选区(编辑覆盖层内选中文字,或宿主页面其它区域如 AI 对话面板)→
|
|
136
|
+
// 原生复制;无选区(激活态/光标折叠的编辑态)→ 序列化选中形状
|
|
137
|
+
onCopy: () => {
|
|
138
|
+
if (hasDocumentTextSelection())
|
|
139
|
+
return "native";
|
|
140
|
+
return this.serializeSelection();
|
|
141
|
+
},
|
|
142
|
+
deserialize: (entry) => this.pasteFromEntry(entry),
|
|
143
|
+
// 纯文本粘贴全权决策:编辑会话/交互进行中放行(编辑覆盖层的粘贴归
|
|
144
|
+
// TextEditor 元素级处理),其余在视口中心新建带文本矩形
|
|
145
|
+
onPasteText: ({ text }) => {
|
|
146
|
+
if (!text || this.editing.editing || this.interactions.busy)
|
|
147
|
+
return "native";
|
|
148
|
+
this.pasteText(text);
|
|
149
|
+
return "handled";
|
|
150
|
+
},
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
this.editor = this.setupEditor();
|
|
154
|
+
this.editing = new FlowEditing({
|
|
155
|
+
app: this.app,
|
|
156
|
+
editor: this.editor,
|
|
157
|
+
history: this.history,
|
|
158
|
+
onChange: this.onChange,
|
|
159
|
+
// 激活态连线标签上按 Delete/Backspace:覆盖层接管按键后转回门面删除
|
|
160
|
+
onDelete: () => this.deleteSelected(),
|
|
161
|
+
});
|
|
162
|
+
this.model = new FlowModel({
|
|
163
|
+
app: this.app,
|
|
164
|
+
registry: this.registry,
|
|
165
|
+
router: this.router,
|
|
166
|
+
history: this.history,
|
|
167
|
+
onChange: this.onChange,
|
|
168
|
+
// 形状脱离模型(删除/顶替)时:结束其编辑态并取消选中,防止悬空
|
|
169
|
+
// 编辑会话与悬空选中(原 removeShape/swapShape 内联副作用,集中在此接线)
|
|
170
|
+
onDetach: (shape) => {
|
|
171
|
+
this.editing.cancelFor(shape);
|
|
172
|
+
this.interactions.select(null);
|
|
173
|
+
},
|
|
174
|
+
onSelect: (item) => this.interactions.select(item),
|
|
175
|
+
});
|
|
176
|
+
this.sel = this.setupSelection();
|
|
177
|
+
this.hover = new HoverFeedback({
|
|
178
|
+
app: this.app,
|
|
179
|
+
canvas: options.canvas,
|
|
180
|
+
edges: () => this.model.edges,
|
|
181
|
+
sel: this.sel,
|
|
182
|
+
onRaiseEdgeTop: (edge) => this.model.raiseEdgeTop(edge),
|
|
183
|
+
});
|
|
184
|
+
this.interactions = new FlowInteractions({
|
|
185
|
+
app: this.app,
|
|
186
|
+
canvas: options.canvas,
|
|
187
|
+
model: this.model,
|
|
188
|
+
sel: this.sel,
|
|
189
|
+
editing: this.editing,
|
|
190
|
+
hover: this.hover,
|
|
191
|
+
snapper: this.snapper,
|
|
192
|
+
router: this.router,
|
|
193
|
+
editor: this.editor,
|
|
194
|
+
history: this.history,
|
|
195
|
+
onChange: this.onChange,
|
|
196
|
+
});
|
|
197
|
+
this.model.load(options.data);
|
|
198
|
+
// 选中框覆盖层置于最上层(registerShape 追加新节点后需 raiseOverlay 保持)
|
|
199
|
+
this.overlay = new SelectionOverlay(() => {
|
|
200
|
+
const scale = this.app.camera.viewport.scale;
|
|
201
|
+
const dirs = ["top", "right", "bottom", "left"];
|
|
202
|
+
const selShape = this.selShape;
|
|
203
|
+
const selEdge = this.selEdge;
|
|
204
|
+
return {
|
|
205
|
+
shape: selShape,
|
|
206
|
+
scale,
|
|
207
|
+
ports: selShape
|
|
208
|
+
? dirs.map((dir) => portPos(selShape, dir, scale))
|
|
209
|
+
: [],
|
|
210
|
+
hoverPort: this.hover.port,
|
|
211
|
+
// 激活连线的首尾端点手柄(端点拖拽期间隐藏)
|
|
212
|
+
edgeHandles: selEdge && !selEdge.dragWhich && selEdge.points.length >= 2
|
|
213
|
+
? [selEdge.points[0], selEdge.points[selEdge.points.length - 1]]
|
|
214
|
+
: [],
|
|
215
|
+
// 激活连线的标签激活框(空白标签不可见时不画):标签为子节点且连线
|
|
216
|
+
// 变换恒为单位阵,本地坐标即世界坐标
|
|
217
|
+
edgeLabel: selEdge && !selEdge.dragWhich && selEdge.label.visible
|
|
218
|
+
? {
|
|
219
|
+
x: selEdge.label.x,
|
|
220
|
+
y: selEdge.label.y,
|
|
221
|
+
width: selEdge.label.width,
|
|
222
|
+
height: selEdge.label.height,
|
|
223
|
+
radius: edgeLabelRadius(selEdge.label.boxStyle.radius),
|
|
224
|
+
}
|
|
225
|
+
: null,
|
|
226
|
+
link: this.interactions.linkTarget
|
|
227
|
+
? {
|
|
228
|
+
// 对接手柄位于节点边缘的实际连接点(与连线端口/预览吸附一致)
|
|
229
|
+
ports: dirs.map((dir) => this.interactions.linkTarget.portAt(dir)),
|
|
230
|
+
active: this.interactions.linkSnap
|
|
231
|
+
? this.interactions.linkTarget.portAt(this.interactions.linkSnap)
|
|
232
|
+
: null,
|
|
233
|
+
}
|
|
234
|
+
: null,
|
|
235
|
+
guides: this.interactions.guides,
|
|
236
|
+
// 框选拖拽中:实时显示已框中节点的整体选框(随拖拽更新,未框中节点时
|
|
237
|
+
// 不显示),橡皮筋矩形由选中插件绘制;落定后为多选组合 bounds
|
|
238
|
+
marquee: this.interactions.marqueeCombo ?? this.multiBounds,
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
this.model.overlay = this.overlay;
|
|
242
|
+
// 选中框覆盖层注册为常驻顶层:新增形状/连线等结构变更由 App 自动保持其置顶
|
|
243
|
+
this.app.addOverlay(this.overlay);
|
|
244
|
+
this.cleanups.push(this.interactions.bind(options.canvas));
|
|
245
|
+
// 初始数据载入后自动居中(只平移不改缩放,节点视觉大小不变;对齐 mindmap
|
|
246
|
+
// 的居中观感)。延迟到下一帧执行:等 canvas 完成首次布局,getBoundingClientRect
|
|
247
|
+
// 才有真实尺寸;空数据(无形状)时 center 内部跳过
|
|
248
|
+
requestAnimationFrame(() => {
|
|
249
|
+
if (!this.disposed)
|
|
250
|
+
this.center();
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
// 装配文本编辑器:按能力键取回宿主装配(通常为官方预设)的实例并接线场景回调
|
|
254
|
+
setupEditor() {
|
|
255
|
+
const editor = this.app.require(TEXT_EDITOR_CAPABILITY);
|
|
256
|
+
// 输入实时同步:文本重测后重新居中(不改形状尺寸),覆盖层跟随新位置
|
|
257
|
+
editor.onInput = () => {
|
|
258
|
+
const shape = this.editing.editing;
|
|
259
|
+
if (!shape)
|
|
260
|
+
return;
|
|
261
|
+
shape.layout();
|
|
262
|
+
this.app.requestRender();
|
|
263
|
+
this.editor.reposition();
|
|
264
|
+
};
|
|
265
|
+
// 编辑结束(失焦):文字居中落定并通知外部
|
|
266
|
+
editor.onEditEnd = () => this.editing.finishEdit();
|
|
267
|
+
return editor;
|
|
268
|
+
}
|
|
269
|
+
// 装配选中插件:单选/多选模型与框选会话(机制),选中语义(selected 高亮、
|
|
270
|
+
// 连线置顶等)由本场景定义。按能力键取回官方预设(或宿主显式装配)的实例。
|
|
271
|
+
// 框选命中规则 = 与矩形相交的形状
|
|
272
|
+
setupSelection() {
|
|
273
|
+
const selection = this.app.require(selectionCapabilityKey());
|
|
274
|
+
selection.hitRect = (rect) => marqueeHits(this.model.shapes.values(), rect);
|
|
275
|
+
// 拖框中实时显示已框中节点的整体选框(随拖拽更新,未框中节点时不显示)
|
|
276
|
+
selection.onMarqueeMove = (_rect, hits) => {
|
|
277
|
+
this.interactions.marqueeCombo = hits.length ? unionBounds(hits) : null;
|
|
278
|
+
};
|
|
279
|
+
// 框选落定:单个退化为普通单选,多个进入组合选中,落空保持无选中
|
|
280
|
+
selection.onMarqueeEnd = (_rect, hits) => {
|
|
281
|
+
this.interactions.marqueeCombo = null;
|
|
282
|
+
if (hits.length === 1)
|
|
283
|
+
this.interactions.select(hits[0]);
|
|
284
|
+
else if (hits.length > 1) {
|
|
285
|
+
selection.set(hits);
|
|
286
|
+
// 框选结果为形状组合:摘除可能常驻的连线标签激活覆盖层
|
|
287
|
+
this.editing.deactivate();
|
|
288
|
+
this.app.requestRender();
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
return selection;
|
|
292
|
+
}
|
|
293
|
+
//#region 公开 API
|
|
294
|
+
// 新增节点(默认居中样式),返回节点 id
|
|
295
|
+
addNode(input) {
|
|
296
|
+
const shape = this.model.createShape(input.shape, {
|
|
297
|
+
x: input.x,
|
|
298
|
+
y: input.y,
|
|
299
|
+
width: input.width,
|
|
300
|
+
height: input.height,
|
|
301
|
+
text: input.text,
|
|
302
|
+
});
|
|
303
|
+
// 注册动作本身作为可逆命令:redo 完成注册,undo 摘除节点及其关联连线。
|
|
304
|
+
// 序列化载荷:内嵌形状数据快照,导入端经形状注册表重建
|
|
305
|
+
this.execCmd({
|
|
306
|
+
type: "add",
|
|
307
|
+
redo: () => this.model.registerShape(shape),
|
|
308
|
+
undo: () => this.model.removeShape(shape),
|
|
309
|
+
toJSON: () => ({
|
|
310
|
+
kind: "flowchart.add-shape",
|
|
311
|
+
payload: { data: shapeData(shape) },
|
|
312
|
+
}),
|
|
313
|
+
});
|
|
314
|
+
this.app.requestRender();
|
|
315
|
+
this.onChange?.("add");
|
|
316
|
+
return shape.id;
|
|
317
|
+
}
|
|
318
|
+
// 按 id 或唯一文字解析形状:门面程序化入口(新建/换边)共用
|
|
319
|
+
resolveShape(ref) {
|
|
320
|
+
const byId = this.model.shapes.get(ref);
|
|
321
|
+
if (byId)
|
|
322
|
+
return byId;
|
|
323
|
+
const hits = [...this.model.shapes.values()].filter((s) => s.text.text === ref);
|
|
324
|
+
return hits.length === 1 ? hits[0] : undefined;
|
|
325
|
+
}
|
|
326
|
+
// 在参考节点的指定方向新建节点并自动连边:与点击端口手柄完全同一条路径
|
|
327
|
+
//(共享 createNodeInDirection:同尺寸、CLICK_CREATE_GAP 间距、中轴对齐、
|
|
328
|
+
// 「注册节点 + 创建连线」一条原子命令)。ref 为参考节点 id 或唯一文字;
|
|
329
|
+
// kind 省略时与参考节点同类型;text 可为空(无文字节点合法)。
|
|
330
|
+
// 返回新节点 id;参考节点不存在或画布交互/编辑进行中返回 null。
|
|
331
|
+
// 程序化入口不改变选中态与视口(点击手柄的选中/激活由交互层负责),
|
|
332
|
+
// 因此 AI 调用它不会重置缩放、平移或用户的激活态。
|
|
333
|
+
createNodeFrom(ref, dir, kind, text = "") {
|
|
334
|
+
if (this.interactions.busy || this.editing.editing)
|
|
335
|
+
return null;
|
|
336
|
+
const source = this.resolveShape(ref);
|
|
337
|
+
if (!source)
|
|
338
|
+
return null;
|
|
339
|
+
const shape = createNodeInDirection({ model: this.model, router: this.router, history: this.aiHistory() }, source, dir, kind ?? source.kind, text);
|
|
340
|
+
this.app.requestRender();
|
|
341
|
+
this.onChange?.("add");
|
|
342
|
+
return shape.id;
|
|
343
|
+
}
|
|
344
|
+
// 更新一条边的首尾与分支标签:可改两端节点(from/to)、两端端口方向
|
|
345
|
+
// (fromDir/toDir)与分支标签(text,空字符串清除标签),省略的字段保持
|
|
346
|
+
// 原值;未显式给方向时按最终拓扑的主导方向自动选边。
|
|
347
|
+
// ref 为边 id,或语义定位 { from, to, text? }(同对节点多条分支边用标签区分)。
|
|
348
|
+
// 与拖拽端点重接/标签编辑同源(reattachEnd + reconnectCommand / edgeTextCommand),
|
|
349
|
+
// 全部子命令合成为一条原子命令,一次 Ctrl+Z 整体回退;不动任何节点几何、
|
|
350
|
+
// 视口与选中态。返回是否生效。
|
|
351
|
+
updateEdge(ref, patch) {
|
|
352
|
+
if (this.interactions.busy || this.editing.editing)
|
|
353
|
+
return false;
|
|
354
|
+
const edge = typeof ref === "string"
|
|
355
|
+
? this.model.edges.find((e) => e.id === ref)
|
|
356
|
+
: this.model.edges.find((e) => e.fromNode.id === ref.from &&
|
|
357
|
+
e.toNode.id === ref.to &&
|
|
358
|
+
(ref.text === undefined || e.label.text === ref.text));
|
|
359
|
+
if (!edge)
|
|
360
|
+
return false;
|
|
361
|
+
const oldFromNode = edge.fromNode;
|
|
362
|
+
const oldFromDir = edge.fromDir;
|
|
363
|
+
const oldToNode = edge.toNode;
|
|
364
|
+
const oldToDir = edge.toDir;
|
|
365
|
+
const oldText = edge.label.text;
|
|
366
|
+
const newFromNode = patch.from ? this.resolveShape(patch.from) : oldFromNode;
|
|
367
|
+
const newToNode = patch.to ? this.resolveShape(patch.to) : oldToNode;
|
|
368
|
+
if (!newFromNode || !newToNode)
|
|
369
|
+
return false;
|
|
370
|
+
const touchFrom = patch.from !== undefined || patch.fromDir !== undefined;
|
|
371
|
+
const touchTo = patch.to !== undefined || patch.toDir !== undefined;
|
|
372
|
+
// 标签:text 给出即改(空字符串清除),与旧值相同视为无变化
|
|
373
|
+
const newText = patch.text;
|
|
374
|
+
const touchText = newText !== undefined && newText !== oldText;
|
|
375
|
+
// 按最终两端拓扑算主导方向,避免「先改 from 再改 to」时自动选边参考到旧端点
|
|
376
|
+
const autoDir = dominantPortDir(newFromNode, newToNode);
|
|
377
|
+
const finalFromDir = touchFrom ? (patch.fromDir ?? autoDir) : oldFromDir;
|
|
378
|
+
const finalToDir = touchTo
|
|
379
|
+
? (patch.toDir ?? OPPOSITE_DIR[autoDir])
|
|
380
|
+
: oldToDir;
|
|
381
|
+
if (newFromNode === oldFromNode &&
|
|
382
|
+
newToNode === oldToNode &&
|
|
383
|
+
finalFromDir === oldFromDir &&
|
|
384
|
+
finalToDir === oldToDir &&
|
|
385
|
+
!touchText) {
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
// 变更已发生 → record 只入栈;各子命令合成为一条,undo 逆序回退
|
|
389
|
+
edge.reattachEnd("from", newFromNode, finalFromDir);
|
|
390
|
+
edge.reattachEnd("to", newToNode, finalToDir);
|
|
391
|
+
if (touchText) {
|
|
392
|
+
edge.label.text = newText;
|
|
393
|
+
edge.layout();
|
|
394
|
+
}
|
|
395
|
+
const cmds = [];
|
|
396
|
+
if (newFromNode !== oldFromNode ||
|
|
397
|
+
finalFromDir !== oldFromDir) {
|
|
398
|
+
cmds.push(reconnectCommand(edge, "from", oldFromNode, oldFromDir, newFromNode, finalFromDir));
|
|
399
|
+
}
|
|
400
|
+
if (newToNode !== oldToNode || finalToDir !== oldToDir) {
|
|
401
|
+
cmds.push(reconnectCommand(edge, "to", oldToNode, oldToDir, newToNode, finalToDir));
|
|
402
|
+
}
|
|
403
|
+
if (touchText) {
|
|
404
|
+
cmds.push(edgeTextCommand(edge, oldText, newText));
|
|
405
|
+
}
|
|
406
|
+
this.recordCmd(cmds.length === 1
|
|
407
|
+
? cmds[0]
|
|
408
|
+
: compose(cmds.every((c) => c.type === "reconnect") ? "reconnect" : "text", ...cmds));
|
|
409
|
+
this.app.requestRender();
|
|
410
|
+
this.onChange?.("reconnect");
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
413
|
+
// 全选:全部形状进入多选组合态(整体移动/批量删除/批量替换可用);多选组合
|
|
414
|
+
// 只含形状(与框选语义一致,关联连线随形状删除,无需单独选中)。
|
|
415
|
+
// 编辑中不响应(mod+a 属输入框原生全选,守卫已放行)
|
|
416
|
+
selectAll() {
|
|
417
|
+
if (this.editing.editing)
|
|
418
|
+
return;
|
|
419
|
+
const shapes = [...this.model.shapes.values()];
|
|
420
|
+
if (!shapes.length)
|
|
421
|
+
return;
|
|
422
|
+
for (const it of this.sel.items)
|
|
423
|
+
it.selected = false;
|
|
424
|
+
this.sel.set(shapes);
|
|
425
|
+
this.hover.clearPort();
|
|
426
|
+
// 全选为形状组合:摘除可能常驻的连线标签激活覆盖层
|
|
427
|
+
this.editing.deactivate();
|
|
428
|
+
this.app.requestRender();
|
|
429
|
+
}
|
|
430
|
+
// 复制当前选中的形状(命令/程序化入口):载荷经 Clipboard 插件写入系统剪贴板
|
|
431
|
+
copySelected() {
|
|
432
|
+
const entry = this.serializeSelection();
|
|
433
|
+
if (entry)
|
|
434
|
+
void this.clipboard?.write(entry);
|
|
435
|
+
}
|
|
436
|
+
// 选中形状序列化为剪贴板载荷(多选整组入档):组内连线(两端都在复制集内)
|
|
437
|
+
// 一并快照——粘贴时按组重建,连线随两端节点恢复。选中连线时忽略(连线随
|
|
438
|
+
// 两端节点重建,单独复制无意义);无选中返回 null(copy 事件不拦截)
|
|
439
|
+
serializeSelection() {
|
|
440
|
+
const shapes = this.selShapes;
|
|
441
|
+
if (!shapes.length)
|
|
442
|
+
return null;
|
|
443
|
+
this.pasteCount = 0;
|
|
444
|
+
const set = new Set(shapes);
|
|
445
|
+
return {
|
|
446
|
+
kind: "flowchart",
|
|
447
|
+
payload: {
|
|
448
|
+
nodes: shapes.map((s) => shapeData(s)),
|
|
449
|
+
edges: this.model.edges
|
|
450
|
+
.filter((e) => set.has(e.fromNode) && set.has(e.toNode))
|
|
451
|
+
.map((e) => ({
|
|
452
|
+
from: shapes.indexOf(e.fromNode),
|
|
453
|
+
to: shapes.indexOf(e.toNode),
|
|
454
|
+
fromDir: e.fromDir,
|
|
455
|
+
toDir: e.toDir,
|
|
456
|
+
text: e.label.text || undefined,
|
|
457
|
+
})),
|
|
458
|
+
},
|
|
459
|
+
text: shapes.map((s) => s.text.text).filter(Boolean).join("\n"),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
// 粘贴:从剪贴板能力读取 flowchart 载荷(原生 paste 事件优先,程序化读取
|
|
463
|
+
// 兜底)后还原图组
|
|
464
|
+
paste() {
|
|
465
|
+
void this.clipboard?.read("flowchart").then((entry) => {
|
|
466
|
+
if (entry)
|
|
467
|
+
this.pasteFromEntry(entry);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
// 还原剪贴板载荷:组内形状按相对排布整体复制到视口中心(连续粘贴逐次错开避免
|
|
471
|
+
// 完全重叠),粘贴出的节点分配全新 id,组内连线按快照的端口方向重建。
|
|
472
|
+
// 整次粘贴为一条原子组合命令——子命令复用 add-shape/connect 的可逆对与
|
|
473
|
+
// 序列化载荷(既有 kind,编解码器无需新分支),undo/redo 整组回退/重放;
|
|
474
|
+
// 粘贴后整组进入多选态,可整体拖动或直接再次复制
|
|
475
|
+
pasteFromEntry(entry) {
|
|
476
|
+
const clip = entry.payload;
|
|
477
|
+
if (!clip?.nodes.length || this.editing.editing || this.interactions.busy)
|
|
478
|
+
return;
|
|
479
|
+
// 锚点 = 视口中心(世界坐标),组包围盒中心对齐锚点;错开量随连续粘贴递增
|
|
480
|
+
const rect = this.options.canvas.getBoundingClientRect();
|
|
481
|
+
const anchor = this.app.camera.toWorld(rect.left + rect.width / 2, rect.top + rect.height / 2);
|
|
482
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
483
|
+
for (const n of clip.nodes) {
|
|
484
|
+
minX = Math.min(minX, n.x);
|
|
485
|
+
minY = Math.min(minY, n.y);
|
|
486
|
+
maxX = Math.max(maxX, n.x + (n.width ?? 0));
|
|
487
|
+
maxY = Math.max(maxY, n.y + (n.height ?? 0));
|
|
488
|
+
}
|
|
489
|
+
const drift = this.pasteCount++ * 24;
|
|
490
|
+
const dx = anchor.x - (minX + maxX) / 2 + drift;
|
|
491
|
+
const dy = anchor.y - (minY + maxY) / 2 + drift;
|
|
492
|
+
// 粘贴实例(不传 id,Node 自动生成全新 id):先建形状再建组内连线(注入
|
|
493
|
+
// 边路由能力,与在位连线同一路由行为)
|
|
494
|
+
const shapes = clip.nodes.map((n) => this.model.createShape(n.shape, {
|
|
495
|
+
x: n.x + dx,
|
|
496
|
+
y: n.y + dy,
|
|
497
|
+
width: n.width,
|
|
498
|
+
height: n.height,
|
|
499
|
+
text: n.text,
|
|
500
|
+
}));
|
|
501
|
+
const edges = clip.edges.map(({ from, to, fromDir, toDir, text }) => {
|
|
502
|
+
const edge = new FlowEdge(shapes[from], shapes[to], {
|
|
503
|
+
from: fromDir,
|
|
504
|
+
to: toDir,
|
|
505
|
+
});
|
|
506
|
+
edge.router = this.router;
|
|
507
|
+
if (text)
|
|
508
|
+
edge.label.text = text;
|
|
509
|
+
return edge;
|
|
510
|
+
});
|
|
511
|
+
this.execCmd(compose("add", ...shapes.map((s) => ({
|
|
512
|
+
type: "add",
|
|
513
|
+
redo: () => this.model.registerShape(s),
|
|
514
|
+
undo: () => this.model.removeShape(s),
|
|
515
|
+
toJSON: () => ({
|
|
516
|
+
kind: "flowchart.add-shape",
|
|
517
|
+
payload: { data: shapeData(s) },
|
|
518
|
+
}),
|
|
519
|
+
})), ...edges.map((e) => this.model.edgeAddRemove(e))));
|
|
520
|
+
// 整组进入多选态(与框选落定同一入口,整体移动/批量删除/再复制可用)
|
|
521
|
+
for (const it of this.sel.items)
|
|
522
|
+
it.selected = false;
|
|
523
|
+
this.sel.set(shapes);
|
|
524
|
+
this.hover.clearPort();
|
|
525
|
+
this.editing.deactivate();
|
|
526
|
+
this.app.requestRender();
|
|
527
|
+
this.onChange?.("add");
|
|
528
|
+
}
|
|
529
|
+
// 粘贴外部纯文本:视口中心新建带文本矩形承载(对齐 Figma/Miro 的跨应用粘贴
|
|
530
|
+
// 行为),复用 addNode 的可逆命令路径,新建后进入选中态。编辑会话/交互进行中
|
|
531
|
+
// 与空文本忽略
|
|
532
|
+
pasteText(text) {
|
|
533
|
+
if (!text || this.editing.editing || this.interactions.busy)
|
|
534
|
+
return;
|
|
535
|
+
const rect = this.options.canvas.getBoundingClientRect();
|
|
536
|
+
const anchor = this.app.camera.toWorld(rect.left + rect.width / 2, rect.top + rect.height / 2);
|
|
537
|
+
// 默认形状尺寸 160×99:锚定视口中心(左上角回退半宽半高)
|
|
538
|
+
const id = this.addNode({
|
|
539
|
+
shape: "rect",
|
|
540
|
+
text,
|
|
541
|
+
x: anchor.x - 80,
|
|
542
|
+
y: anchor.y - 50,
|
|
543
|
+
});
|
|
544
|
+
const shape = this.model.shapes.get(id);
|
|
545
|
+
if (shape) {
|
|
546
|
+
for (const it of this.sel.items)
|
|
547
|
+
it.selected = false;
|
|
548
|
+
this.sel.set([shape]);
|
|
549
|
+
this.app.requestRender();
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
// 删除当前选中(形状连同其关联连线,或单条连线;多选时删除全部成员)。
|
|
553
|
+
// 删除记录为可逆命令:undo 按原 z 序索引插回,redo 重放摘除
|
|
554
|
+
deleteSelected() {
|
|
555
|
+
if (this.sel.items.length > 1) {
|
|
556
|
+
const list = [...this.selShapes];
|
|
557
|
+
const captured = list.map((s) => this.model.captureShapeRemoval(s));
|
|
558
|
+
this.sel.clear();
|
|
559
|
+
list.forEach((s) => this.model.removeShape(s));
|
|
560
|
+
this.app.requestRender();
|
|
561
|
+
this.execCmd({
|
|
562
|
+
type: "delete",
|
|
563
|
+
redo: () => {
|
|
564
|
+
for (const c of captured)
|
|
565
|
+
this.model.applyShapeRemoval(c);
|
|
566
|
+
},
|
|
567
|
+
undo: () => {
|
|
568
|
+
for (const c of [...captured].reverse())
|
|
569
|
+
this.model.restoreShapeRemoval(c);
|
|
570
|
+
},
|
|
571
|
+
// 序列化载荷:被删形状内嵌数据快照(脱离树后无从按 id 解析),关联连线
|
|
572
|
+
// 内嵌连线数据;order=连线表序、childIdx=z 序,两个索引都要定格
|
|
573
|
+
toJSON: () => ({
|
|
574
|
+
kind: "flowchart.delete",
|
|
575
|
+
payload: {
|
|
576
|
+
v: "shapes",
|
|
577
|
+
items: captured.map((c) => ({
|
|
578
|
+
data: shapeData(c.shape),
|
|
579
|
+
childIdx: c.childIdx,
|
|
580
|
+
edges: c.edges.map(({ edge, order, childIdx }) => ({
|
|
581
|
+
id: edge.id,
|
|
582
|
+
fromId: edge.fromNode.id,
|
|
583
|
+
toId: edge.toNode.id,
|
|
584
|
+
fromDir: edge.fromDir,
|
|
585
|
+
toDir: edge.toDir,
|
|
586
|
+
text: edge.label.text || undefined,
|
|
587
|
+
order,
|
|
588
|
+
childIdx,
|
|
589
|
+
})),
|
|
590
|
+
})),
|
|
591
|
+
},
|
|
592
|
+
}),
|
|
593
|
+
});
|
|
594
|
+
this.onChange?.("delete");
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
const sel = this.sel.items[0];
|
|
598
|
+
if (!sel)
|
|
599
|
+
return;
|
|
600
|
+
if (sel instanceof FlowEdge) {
|
|
601
|
+
this.execRemoveEdge(sel);
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
this.execRemoveShape(sel);
|
|
605
|
+
}
|
|
606
|
+
this.onChange?.("delete");
|
|
607
|
+
}
|
|
608
|
+
// 摘除一个节点并记为可逆删除命令(deleteSelected 单选分支与 AI removeNode
|
|
609
|
+
// 共用):节点连同关联连线一起删除,undo 按原 z 序索引插回
|
|
610
|
+
execRemoveShape(shape) {
|
|
611
|
+
const captured = this.model.captureShapeRemoval(shape);
|
|
612
|
+
this.model.removeShape(shape);
|
|
613
|
+
this.app.requestRender();
|
|
614
|
+
this.execCmd({
|
|
615
|
+
type: "delete",
|
|
616
|
+
redo: () => this.model.applyShapeRemoval(captured),
|
|
617
|
+
undo: () => this.model.restoreShapeRemoval(captured),
|
|
618
|
+
// 序列化载荷:被删形状内嵌数据快照(脱离树后无从按 id 解析),关联连线
|
|
619
|
+
// 内嵌连线数据;order=连线表序、childIdx=z 序,两个索引都要定格
|
|
620
|
+
toJSON: () => ({
|
|
621
|
+
kind: "flowchart.delete",
|
|
622
|
+
payload: {
|
|
623
|
+
v: "shapes",
|
|
624
|
+
items: [
|
|
625
|
+
{
|
|
626
|
+
data: shapeData(captured.shape),
|
|
627
|
+
childIdx: captured.childIdx,
|
|
628
|
+
edges: captured.edges.map(({ edge, order, childIdx }) => ({
|
|
629
|
+
id: edge.id,
|
|
630
|
+
fromId: edge.fromNode.id,
|
|
631
|
+
toId: edge.toNode.id,
|
|
632
|
+
fromDir: edge.fromDir,
|
|
633
|
+
toDir: edge.toDir,
|
|
634
|
+
text: edge.label.text || undefined,
|
|
635
|
+
order,
|
|
636
|
+
childIdx,
|
|
637
|
+
})),
|
|
638
|
+
},
|
|
639
|
+
],
|
|
640
|
+
},
|
|
641
|
+
}),
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
// 摘除一条连线并记为可逆删除命令(deleteSelected 单选分支与 AI removeEdge
|
|
645
|
+
// 共用):undo 按原连线表序与 z 序插回;仅在连线处于选中态时清空选中
|
|
646
|
+
execRemoveEdge(edge) {
|
|
647
|
+
const captured = {
|
|
648
|
+
edge,
|
|
649
|
+
childIdx: this.app.children.indexOf(edge),
|
|
650
|
+
listIdx: this.model.edges.indexOf(edge),
|
|
651
|
+
};
|
|
652
|
+
if (this.sel.items.includes(edge))
|
|
653
|
+
this.interactions.select(null);
|
|
654
|
+
this.model.removeTop(edge);
|
|
655
|
+
this.model.edges = this.model.edges.filter((e) => e !== edge);
|
|
656
|
+
this.app.requestRender();
|
|
657
|
+
this.execCmd({
|
|
658
|
+
type: "delete",
|
|
659
|
+
redo: () => {
|
|
660
|
+
this.model.removeTop(captured.edge);
|
|
661
|
+
this.model.edges = this.model.edges.filter((e) => e !== captured.edge);
|
|
662
|
+
},
|
|
663
|
+
undo: () => {
|
|
664
|
+
this.model.edges.splice(Math.min(captured.listIdx, this.model.edges.length), 0, captured.edge);
|
|
665
|
+
this.model.insertAt(captured.edge, captured.childIdx);
|
|
666
|
+
},
|
|
667
|
+
// 序列化载荷:单边删除——内嵌连线两端 id 与端口方向(脱离连线表后
|
|
668
|
+
// 无从按 id 解析,须可完整重建)+ 两个插入索引
|
|
669
|
+
toJSON: () => ({
|
|
670
|
+
kind: "flowchart.delete",
|
|
671
|
+
payload: {
|
|
672
|
+
v: "edge",
|
|
673
|
+
edge: {
|
|
674
|
+
edgeId: captured.edge.id,
|
|
675
|
+
fromId: captured.edge.fromNode.id,
|
|
676
|
+
toId: captured.edge.toNode.id,
|
|
677
|
+
fromDir: captured.edge.fromDir,
|
|
678
|
+
toDir: captured.edge.toDir,
|
|
679
|
+
text: captured.edge.label.text || undefined,
|
|
680
|
+
},
|
|
681
|
+
childIdx: captured.childIdx,
|
|
682
|
+
listIdx: captured.listIdx,
|
|
683
|
+
},
|
|
684
|
+
}),
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
// 删除一个节点(程序化入口,AI removeNode 操作复用):节点连同其关联连线
|
|
688
|
+
// 一起删除,undo 按原 z 序索引整体恢复。ref 为节点 id 或唯一文字。
|
|
689
|
+
// 返回是否生效。
|
|
690
|
+
removeNode(ref) {
|
|
691
|
+
if (this.interactions.busy || this.editing.editing)
|
|
692
|
+
return false;
|
|
693
|
+
const shape = this.resolveShape(ref);
|
|
694
|
+
if (!shape)
|
|
695
|
+
return false;
|
|
696
|
+
this.execRemoveShape(shape);
|
|
697
|
+
this.onChange?.("delete");
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
// 删除一条连线(程序化入口,AI removeEdge 操作复用):只摘除这一条连线,
|
|
701
|
+
// 两端节点与其它连线不动,undo 按原序插回。ref 为连线 id,或语义定位
|
|
702
|
+
// { from, to, text? }(同对节点多条分支边用标签区分)。返回是否生效。
|
|
703
|
+
removeEdge(ref) {
|
|
704
|
+
if (this.interactions.busy || this.editing.editing)
|
|
705
|
+
return false;
|
|
706
|
+
const edge = typeof ref === "string"
|
|
707
|
+
? this.model.edges.find((e) => e.id === ref)
|
|
708
|
+
: this.model.edges.find((e) => e.fromNode.id === ref.from &&
|
|
709
|
+
e.toNode.id === ref.to &&
|
|
710
|
+
(ref.text === undefined || e.label.text === ref.text));
|
|
711
|
+
if (!edge)
|
|
712
|
+
return false;
|
|
713
|
+
this.execRemoveEdge(edge);
|
|
714
|
+
this.onChange?.("delete");
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
// 替换当前选中形状的类型:保留位置、尺寸、文本、id 与全部连线
|
|
718
|
+
// (连线两端引用直接重绑到新形状,端口方向维持创建时冻结的值不重选边),
|
|
719
|
+
// 新形状占据原形状的 z 序位置。单选/多选均生效——类型相同的成员跳过,
|
|
720
|
+
// 多个替换组合为一条原子命令(一次 undo 撤销整批)
|
|
721
|
+
replaceSelected(kind) {
|
|
722
|
+
const targets = this.selShapes.filter((s) => s.kind !== kind);
|
|
723
|
+
if (!targets.length)
|
|
724
|
+
return;
|
|
725
|
+
const pairs = targets.map((old) => ({
|
|
726
|
+
old,
|
|
727
|
+
shape: this.model.createShape(kind, {
|
|
728
|
+
id: old.id,
|
|
729
|
+
x: old.x,
|
|
730
|
+
y: old.y,
|
|
731
|
+
width: old.width,
|
|
732
|
+
height: old.height,
|
|
733
|
+
text: old.text.text,
|
|
734
|
+
}),
|
|
735
|
+
}));
|
|
736
|
+
// 替换记录为可逆命令:undo/redo 对称地把旧/新形状顶替回去。
|
|
737
|
+
// 序列化载荷:新旧形状都内嵌数据——旧形状脱离树后不在任何表里,
|
|
738
|
+
// 导入端经会话注册表按 id 保证 undo/redo 顶替的是同一实例
|
|
739
|
+
if (pairs.length === 1) {
|
|
740
|
+
const p = pairs[0];
|
|
741
|
+
this.execCmd({
|
|
742
|
+
type: "replace",
|
|
743
|
+
redo: () => this.model.swapShape(p.old, p.shape),
|
|
744
|
+
undo: () => this.model.swapShape(p.shape, p.old),
|
|
745
|
+
toJSON: () => ({
|
|
746
|
+
kind: "flowchart.replace",
|
|
747
|
+
payload: { oldData: shapeData(p.old), newData: shapeData(p.shape) },
|
|
748
|
+
}),
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
// 多选批量替换:序列化为组合命令(子命令与单选 replace 载荷同构,
|
|
753
|
+
// 编解码器复用现有分支);swapShape 内逐个 onSelect 会把选中塌缩成
|
|
754
|
+
// 单选,redo 收尾恢复整组选中(undo/redo 按钮路径会另行清空选中)
|
|
755
|
+
this.execCmd({
|
|
756
|
+
type: "replace",
|
|
757
|
+
redo: () => {
|
|
758
|
+
for (const p of pairs)
|
|
759
|
+
this.model.swapShape(p.old, p.shape);
|
|
760
|
+
for (const it of this.sel.items)
|
|
761
|
+
it.selected = false;
|
|
762
|
+
this.sel.set(pairs.map((p) => p.shape));
|
|
763
|
+
},
|
|
764
|
+
undo: () => {
|
|
765
|
+
for (const p of [...pairs].reverse())
|
|
766
|
+
this.model.swapShape(p.shape, p.old);
|
|
767
|
+
},
|
|
768
|
+
toJSON: () => ({
|
|
769
|
+
kind: "composite",
|
|
770
|
+
payload: {
|
|
771
|
+
type: "replace",
|
|
772
|
+
children: pairs.map((p) => ({
|
|
773
|
+
kind: "flowchart.replace",
|
|
774
|
+
payload: {
|
|
775
|
+
oldData: shapeData(p.old),
|
|
776
|
+
newData: shapeData(p.shape),
|
|
777
|
+
},
|
|
778
|
+
})),
|
|
779
|
+
},
|
|
780
|
+
}),
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
this.app.requestRender();
|
|
784
|
+
this.onChange?.("replace");
|
|
785
|
+
}
|
|
786
|
+
// 原地替换一个节点的形状(程序化入口,AI replaceShape 操作复用):与
|
|
787
|
+
// replaceSelected 单选路径同一 swapShape 语义——保留位置、尺寸、文本、id
|
|
788
|
+
// 与全部连线(两端重绑、端口方向维持冻结值),一条可逆命令。ref 为节点 id
|
|
789
|
+
// 或唯一文字;目标类型与当前相同视为无变化(不记命令)。返回是否生效。
|
|
790
|
+
replaceShape(ref, kind) {
|
|
791
|
+
if (this.interactions.busy || this.editing.editing)
|
|
792
|
+
return false;
|
|
793
|
+
const old = this.resolveShape(ref);
|
|
794
|
+
if (!old || old.kind === kind)
|
|
795
|
+
return false;
|
|
796
|
+
const shape = this.model.createShape(kind, {
|
|
797
|
+
id: old.id,
|
|
798
|
+
x: old.x,
|
|
799
|
+
y: old.y,
|
|
800
|
+
width: old.width,
|
|
801
|
+
height: old.height,
|
|
802
|
+
text: old.text.text,
|
|
803
|
+
});
|
|
804
|
+
this.execCmd({
|
|
805
|
+
type: "replace",
|
|
806
|
+
redo: () => this.model.swapShape(old, shape),
|
|
807
|
+
undo: () => this.model.swapShape(shape, old),
|
|
808
|
+
toJSON: () => ({
|
|
809
|
+
kind: "flowchart.replace",
|
|
810
|
+
payload: { oldData: shapeData(old), newData: shapeData(shape) },
|
|
811
|
+
}),
|
|
812
|
+
});
|
|
813
|
+
this.app.requestRender();
|
|
814
|
+
this.onChange?.("replace");
|
|
815
|
+
return true;
|
|
816
|
+
}
|
|
817
|
+
// 修改一个节点的文字(程序化入口,AI setNodeText 操作复用):与双击编辑
|
|
818
|
+
// 结束同一 textCommand 历史路径——只改文字并重排居中,位置/尺寸/连线全部
|
|
819
|
+
// 不动,一条可逆命令。ref 为节点 id 或唯一文字(可按改前文字定位);文字
|
|
820
|
+
// 未变化视为无变化(不记命令)。返回是否生效。
|
|
821
|
+
setNodeText(ref, text) {
|
|
822
|
+
if (this.interactions.busy || this.editing.editing)
|
|
823
|
+
return false;
|
|
824
|
+
const shape = this.resolveShape(ref);
|
|
825
|
+
if (!shape || shape.text.text === text)
|
|
826
|
+
return false;
|
|
827
|
+
this.execCmd(textCommand(shape, shape.text.text, text));
|
|
828
|
+
this.app.requestRender();
|
|
829
|
+
this.onChange?.("text");
|
|
830
|
+
return true;
|
|
831
|
+
}
|
|
832
|
+
// 当前选中快照(语义形态,只读):选中形状按模型形状表取,连线以两端节点
|
|
833
|
+
// id + 分支标签 + 当前出入端口方向表达(方向缺失会导致模型重绘时接错侧)。
|
|
834
|
+
// 供 AI 上下文与宿主 UI 复用,不暴露 Shape/FlowEdge 实例
|
|
835
|
+
getSelection() {
|
|
836
|
+
const ids = new Set();
|
|
837
|
+
for (const item of this.sel.items)
|
|
838
|
+
if (item.id)
|
|
839
|
+
ids.add(item.id);
|
|
840
|
+
const nodes = [];
|
|
841
|
+
for (const shape of this.model.shapes.values()) {
|
|
842
|
+
if (shape.id && ids.has(shape.id))
|
|
843
|
+
nodes.push({ id: shape.id, shape: shape.kind, text: shape.text.text });
|
|
844
|
+
}
|
|
845
|
+
const edges = [];
|
|
846
|
+
for (const edge of this.model.edges) {
|
|
847
|
+
if (!edge.id || !ids.has(edge.id))
|
|
848
|
+
continue;
|
|
849
|
+
const from = edge.fromNode.id;
|
|
850
|
+
const to = edge.toNode.id;
|
|
851
|
+
if (!from || !to)
|
|
852
|
+
continue;
|
|
853
|
+
const text = edge.label.text;
|
|
854
|
+
edges.push({
|
|
855
|
+
from,
|
|
856
|
+
to,
|
|
857
|
+
...(text ? { text } : {}),
|
|
858
|
+
fromDir: edge.fromDir,
|
|
859
|
+
toDir: edge.toDir,
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
return { nodes, edges };
|
|
863
|
+
}
|
|
864
|
+
// 历史栈可读描述:把撤销/重做双栈的序列化命令转为文案(AI history 操作的
|
|
865
|
+
// 返回内容,宿主也可直接展示)。撤销栈最近在前,重做栈按重放次序
|
|
866
|
+
historyReport() {
|
|
867
|
+
const snap = this.history.exportJSON?.();
|
|
868
|
+
if (!snap)
|
|
869
|
+
return "历史记录暂不可读(包含无法序列化的命令)";
|
|
870
|
+
const describe = (list) => list
|
|
871
|
+
.map((s, i) => `${i + 1}. ${describeHistoryCommand(s, this.model)}`)
|
|
872
|
+
.join("\n");
|
|
873
|
+
// 导出按栈底→栈顶排列:撤销栈反转后最近变更在前,重做栈保持重放次序
|
|
874
|
+
const undoDesc = describe([...snap.undo].reverse());
|
|
875
|
+
const redoDesc = describe(snap.redo);
|
|
876
|
+
return [
|
|
877
|
+
undoDesc
|
|
878
|
+
? `可撤销 ${snap.undo.length} 条(最近在前):\n${undoDesc}`
|
|
879
|
+
: "没有可撤销的变更",
|
|
880
|
+
redoDesc
|
|
881
|
+
? `可重做 ${snap.redo.length} 条(按重放次序):\n${redoDesc}`
|
|
882
|
+
: "没有可重做的变更",
|
|
883
|
+
].join("\n");
|
|
884
|
+
}
|
|
885
|
+
// 历史栈状态(AI 上下文):能否撤销/重做 + 条数,模型据此决定输出 undo/redo
|
|
886
|
+
aiHistoryState() {
|
|
887
|
+
const snap = this.history.exportJSON?.();
|
|
888
|
+
return {
|
|
889
|
+
canUndo: this.history.canUndo,
|
|
890
|
+
canRedo: this.history.canRedo,
|
|
891
|
+
undoCount: this.history.undoCount ?? snap?.undo.length ?? 0,
|
|
892
|
+
redoCount: this.history.redoCount ?? snap?.redo.length ?? 0,
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
// AI 批量执行收集器:一次 AI run 的全部增量 op(新建/换边/换形状/改文字/删除)
|
|
896
|
+
// 合并为一条 "ai" 组合命令入栈——无论执行多少步(如批量删除几十个节点),
|
|
897
|
+
// 撤销历史只多一条记录(与 mindmap ai-controller 的 compose("ai", ...) 同一语义)。
|
|
898
|
+
// 批量期间 exec 先 redo 立即生效(与 History.exec 一致)但暂缓入栈,run 结束
|
|
899
|
+
// 统一 compose 入栈;record(变更已发生)仅收集。run 结束时最后一次
|
|
900
|
+
// record 仍会清空重做分支,"新变更使既有重做分支失效"的语义保持不变
|
|
901
|
+
aiBatch = null;
|
|
902
|
+
// 批量收集期间的 exec:立即生效但暂缓入栈
|
|
903
|
+
execCmd(cmd) {
|
|
904
|
+
if (this.aiBatch) {
|
|
905
|
+
cmd.redo();
|
|
906
|
+
this.aiBatch.push(cmd);
|
|
907
|
+
}
|
|
908
|
+
else {
|
|
909
|
+
this.history.exec(cmd);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
// 批量收集期间的 record(变更已发生的场景):仅收集
|
|
913
|
+
recordCmd(cmd) {
|
|
914
|
+
if (this.aiBatch)
|
|
915
|
+
this.aiBatch.push(cmd);
|
|
916
|
+
else
|
|
917
|
+
this.history.record(cmd);
|
|
918
|
+
}
|
|
919
|
+
// 批量期间注入复用路径(createNodeInDirection 等)的 history 替身:
|
|
920
|
+
// exec/record 转入收集器,其余透传;未批量时与 this.history 等价
|
|
921
|
+
aiHistory() {
|
|
922
|
+
const self = this;
|
|
923
|
+
return {
|
|
924
|
+
exec: (cmd) => self.execCmd(cmd),
|
|
925
|
+
record: (cmd) => self.recordCmd(cmd),
|
|
926
|
+
undo: () => self.history.undo(),
|
|
927
|
+
redo: () => self.history.redo(),
|
|
928
|
+
get canUndo() {
|
|
929
|
+
return self.history.canUndo;
|
|
930
|
+
},
|
|
931
|
+
get canRedo() {
|
|
932
|
+
return self.history.canRedo;
|
|
933
|
+
},
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
// AI 生成:宿主适配器 → 操作序列 → 逐条执行。改图有两条路径:
|
|
937
|
+
// render(语义图 → 程序布局 → 整图原子替换)与 createNode(参考节点 + 方向 +
|
|
938
|
+
// 类型 → 复用端口手柄快速创建,纯增量、不动其他节点)。模型绝不接触 x/y 与
|
|
939
|
+
// 端口方向;undo/redo 直接驱动既有历史链(与手动 Ctrl+Z 同源),history 只读
|
|
940
|
+
// 历史并入回答,回答类指令只返回文本、不改画布。提示词用
|
|
941
|
+
// defaultAIFlowPrompt(req) 渲染即可。
|
|
942
|
+
async generate(prompt, options = {}) {
|
|
943
|
+
const generate = this.options.ai;
|
|
944
|
+
if (!generate) {
|
|
945
|
+
throw new Error("未配置 AI 生成适配器(FlowChartOptions.ai)");
|
|
946
|
+
}
|
|
947
|
+
if (this.editing.editing) {
|
|
948
|
+
throw new Error("请先结束当前编辑再执行 AI 操作");
|
|
949
|
+
}
|
|
950
|
+
if (this.interactions.busy) {
|
|
951
|
+
throw new Error("画布交互进行中,请稍后重试");
|
|
952
|
+
}
|
|
953
|
+
const graph = toSemanticGraph(this.model.toJSON());
|
|
954
|
+
const req = {
|
|
955
|
+
prompt,
|
|
956
|
+
graph,
|
|
957
|
+
selection: this.getSelection(),
|
|
958
|
+
historyState: this.aiHistoryState(),
|
|
959
|
+
history: options.history ?? [],
|
|
960
|
+
};
|
|
961
|
+
const { ops, answer } = parseAIFlowOps(await generate(req));
|
|
962
|
+
// undo/redo 与改图混用即整体拒绝:否则"撤销后在同一批里改图"的时序歧义
|
|
963
|
+
// 会让结果取决于执行顺序,且一次 Ctrl+Z 无法整体回退
|
|
964
|
+
const hasControl = ops.some((o) => o.op === "undo" || o.op === "redo");
|
|
965
|
+
const hasRender = ops.some((o) => o.op === "render");
|
|
966
|
+
const hasIncremental = ops.some((o) => o.op === "createNode" ||
|
|
967
|
+
o.op === "updateEdge" ||
|
|
968
|
+
o.op === "replaceShape" ||
|
|
969
|
+
o.op === "setNodeText" ||
|
|
970
|
+
o.op === "removeNode" ||
|
|
971
|
+
o.op === "removeEdge");
|
|
972
|
+
if (hasControl && (hasRender || hasIncremental)) {
|
|
973
|
+
throw new Error("AI 的 undo/redo 操作不能与改图操作混用,请分开执行");
|
|
974
|
+
}
|
|
975
|
+
// 增量操作都以「当前画布」为基准,与整图重绘混用会让结果取决于执行顺序
|
|
976
|
+
// (重绘会覆盖此前的增量变更),整体拒绝
|
|
977
|
+
if (hasIncremental && hasRender) {
|
|
978
|
+
throw new Error("AI 的增量操作(新建/换边/换形状/改文字/删除)不能与整图重绘混用,请分开执行");
|
|
979
|
+
}
|
|
980
|
+
// 生成期间用户可能开始拖拽/编辑,执行前复查(与入口守卫对称)
|
|
981
|
+
if (this.editing.editing || this.interactions.busy) {
|
|
982
|
+
throw new Error("AI 操作被画布交互打断,请重试");
|
|
983
|
+
}
|
|
984
|
+
const steps = [];
|
|
985
|
+
const answers = answer ? [answer] : [];
|
|
986
|
+
let applied = 0;
|
|
987
|
+
// 本次生效的语义图:无 render 时为执行前的快照(undo/redo 后重新导出当前图)
|
|
988
|
+
let resultGraph = graph;
|
|
989
|
+
// 顺序执行:history 反映此前操作的效果;反应答只增删文本,不动画布。
|
|
990
|
+
// 全部增量 op 收进 aiBatch,run 结束合并为一条 "ai" 组合命令入栈——一次
|
|
991
|
+
// AI 操作对撤销历史只是一条记录;异常中断时 finally 兜底入栈,已生效的
|
|
992
|
+
// 变更仍然可撤销
|
|
993
|
+
this.aiBatch = [];
|
|
994
|
+
try {
|
|
995
|
+
for (const op of ops) {
|
|
996
|
+
if (op.op === "undo") {
|
|
997
|
+
const ok = this.undo();
|
|
998
|
+
if (ok)
|
|
999
|
+
applied++;
|
|
1000
|
+
steps.push({
|
|
1001
|
+
op,
|
|
1002
|
+
label: ok ? "撤销最近一次变更" : "没有可撤销的变更,已跳过",
|
|
1003
|
+
status: ok ? "applied" : "skipped",
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
else if (op.op === "redo") {
|
|
1007
|
+
const ok = this.redo();
|
|
1008
|
+
if (ok)
|
|
1009
|
+
applied++;
|
|
1010
|
+
steps.push({
|
|
1011
|
+
op,
|
|
1012
|
+
label: ok ? "重做最近一次被撤销的变更" : "没有可重做的变更,已跳过",
|
|
1013
|
+
status: ok ? "applied" : "skipped",
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
else if (op.op === "history") {
|
|
1017
|
+
answers.push(this.historyReport());
|
|
1018
|
+
steps.push({ op, label: "读取历史记录", status: "applied" });
|
|
1019
|
+
}
|
|
1020
|
+
else if (op.op === "render") {
|
|
1021
|
+
resultGraph = op.graph;
|
|
1022
|
+
this.renderAIGraph(op.graph, options.layout);
|
|
1023
|
+
applied++;
|
|
1024
|
+
steps.push({
|
|
1025
|
+
op,
|
|
1026
|
+
label: `按语义图重绘(${op.graph.nodes.length} 个节点、${op.graph.edges.length} 条连线)`,
|
|
1027
|
+
status: "applied",
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
else if (op.op === "createNode") {
|
|
1031
|
+
// 直接调用门面「参考节点 + 方向 + 类型」快速创建:与点击端口手柄同一路径,
|
|
1032
|
+
// 已有节点几何、缩放/平移、激活态全部不受影响
|
|
1033
|
+
const id = this.createNodeFrom(op.ref, op.dir, op.shape, op.text);
|
|
1034
|
+
if (id)
|
|
1035
|
+
applied++;
|
|
1036
|
+
const dirLabel = {
|
|
1037
|
+
top: "上方",
|
|
1038
|
+
bottom: "下方",
|
|
1039
|
+
left: "左侧",
|
|
1040
|
+
right: "右侧",
|
|
1041
|
+
};
|
|
1042
|
+
steps.push({
|
|
1043
|
+
op,
|
|
1044
|
+
label: id
|
|
1045
|
+
? `在「${op.ref}」${dirLabel[op.dir]}新建节点「${op.text}」`
|
|
1046
|
+
: `参考节点「${op.ref}」不存在或画布正忙,已跳过`,
|
|
1047
|
+
status: id ? "applied" : "skipped",
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
else if (op.op === "updateEdge") {
|
|
1051
|
+
// 直接调用门面「更新一条边的首尾」:只重接这一条边,节点几何、
|
|
1052
|
+
// 缩放/平移、选中态全部不受影响
|
|
1053
|
+
const ok = this.updateEdge(op.ref, {
|
|
1054
|
+
from: op.from,
|
|
1055
|
+
fromDir: op.fromDir,
|
|
1056
|
+
to: op.to,
|
|
1057
|
+
toDir: op.toDir,
|
|
1058
|
+
text: op.text,
|
|
1059
|
+
});
|
|
1060
|
+
if (ok)
|
|
1061
|
+
applied++;
|
|
1062
|
+
steps.push({
|
|
1063
|
+
op,
|
|
1064
|
+
label: ok
|
|
1065
|
+
? op.text !== undefined
|
|
1066
|
+
? `更新「${op.ref.from} → ${op.ref.to}」这条连线的首尾与标签`
|
|
1067
|
+
: `更新「${op.ref.from} → ${op.ref.to}」这条连线的首尾`
|
|
1068
|
+
: `未找到「${op.ref.from} → ${op.ref.to}」这条连线或画布正忙,已跳过`,
|
|
1069
|
+
status: ok ? "applied" : "skipped",
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
else if (op.op === "replaceShape") {
|
|
1073
|
+
// 直接调用门面「原地替换形状」:只换这一个节点的形状,位置/尺寸/
|
|
1074
|
+
// 文字/连线全部保留,不重排整张图
|
|
1075
|
+
const ok = this.replaceShape(op.ref, op.shape);
|
|
1076
|
+
if (ok)
|
|
1077
|
+
applied++;
|
|
1078
|
+
steps.push({
|
|
1079
|
+
op,
|
|
1080
|
+
label: ok
|
|
1081
|
+
? `把「${op.ref}」替换为 ${op.shape} 形状`
|
|
1082
|
+
: `节点「${op.ref}」不存在、已是该形状或画布正忙,已跳过`,
|
|
1083
|
+
status: ok ? "applied" : "skipped",
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
else if (op.op === "setNodeText") {
|
|
1087
|
+
// 直接调用门面「改节点文字」:只改这一个节点的文字并重排居中,
|
|
1088
|
+
// 几何与连线全部不动
|
|
1089
|
+
const ok = this.setNodeText(op.ref, op.text);
|
|
1090
|
+
if (ok)
|
|
1091
|
+
applied++;
|
|
1092
|
+
steps.push({
|
|
1093
|
+
op,
|
|
1094
|
+
label: ok
|
|
1095
|
+
? `把「${op.ref}」的文字改为「${op.text}」`
|
|
1096
|
+
: `节点「${op.ref}」不存在、文字未变化或画布正忙,已跳过`,
|
|
1097
|
+
status: ok ? "applied" : "skipped",
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
else if (op.op === "removeNode") {
|
|
1101
|
+
// 直接调用门面「删除节点」:节点连同关联连线一起删除,一条可逆命令
|
|
1102
|
+
const ok = this.removeNode(op.ref);
|
|
1103
|
+
if (ok)
|
|
1104
|
+
applied++;
|
|
1105
|
+
steps.push({
|
|
1106
|
+
op,
|
|
1107
|
+
label: ok
|
|
1108
|
+
? `删除节点「${op.ref}」及其关联连线`
|
|
1109
|
+
: `节点「${op.ref}」不存在或画布正忙,已跳过`,
|
|
1110
|
+
status: ok ? "applied" : "skipped",
|
|
1111
|
+
});
|
|
1112
|
+
}
|
|
1113
|
+
else if (op.op === "removeEdge") {
|
|
1114
|
+
// 直接调用门面「删除连线」:只摘除这一条连线,两端节点不动
|
|
1115
|
+
const ok = this.removeEdge(op.ref);
|
|
1116
|
+
if (ok)
|
|
1117
|
+
applied++;
|
|
1118
|
+
const refLabel = typeof op.ref === "string" ? op.ref : `${op.ref.from} → ${op.ref.to}`;
|
|
1119
|
+
steps.push({
|
|
1120
|
+
op,
|
|
1121
|
+
label: ok
|
|
1122
|
+
? `删除连线「${refLabel}」`
|
|
1123
|
+
: `未找到「${refLabel}」这条连线或画布正忙,已跳过`,
|
|
1124
|
+
status: ok ? "applied" : "skipped",
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
finally {
|
|
1130
|
+
// run 结束:收集到的全部增量命令合并为一条 "ai" 组合命令入栈
|
|
1131
|
+
//(无改图动作则不产生记录;仅一条时直接入栈不包壳,栈形态与单步
|
|
1132
|
+
// 操作一致)
|
|
1133
|
+
const aiCmds = this.aiBatch;
|
|
1134
|
+
this.aiBatch = null;
|
|
1135
|
+
if (aiCmds.length === 1)
|
|
1136
|
+
this.history.record(aiCmds[0]);
|
|
1137
|
+
else if (aiCmds.length > 1)
|
|
1138
|
+
this.history.record(compose("ai", ...aiCmds));
|
|
1139
|
+
}
|
|
1140
|
+
// undo/redo / createNode 后语义图已变:重新导出,保证返回值与画布一致
|
|
1141
|
+
if (applied && !hasRender)
|
|
1142
|
+
resultGraph = toSemanticGraph(this.model.toJSON());
|
|
1143
|
+
return {
|
|
1144
|
+
graph: resultGraph,
|
|
1145
|
+
data: this.model.toJSON(),
|
|
1146
|
+
answer: answers.filter(Boolean).join("\n\n") || undefined,
|
|
1147
|
+
applied,
|
|
1148
|
+
steps,
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
// 语义图 → 画布数据:按能力键允许第三方布局器覆盖官方 DAG 分层布局。
|
|
1152
|
+
// 几何部分合并:已有节点沿用当前坐标/尺寸/端口方向(仅套用形状/文字变更),
|
|
1153
|
+
// 新增节点经锚点平移落进既有版式、新增连线按实际位置自动选边——无论改形状
|
|
1154
|
+
// 还是增删节点,用户手动排好的版式都不会被程序分层布局整体打回。默认不动
|
|
1155
|
+
// 视口(缩放/平移保持原样);「把一张语义图渲染出来」的全新画布场景(无共有
|
|
1156
|
+
// 节点可对位)可显式传 { fit: true } 落定后适应视口。
|
|
1157
|
+
renderAIGraph(graph, options, viewport) {
|
|
1158
|
+
const custom = this.app.capability(FLOW_AUTO_LAYOUT_CAPABILITY);
|
|
1159
|
+
const data = custom
|
|
1160
|
+
? custom.layout(graph, options ?? {})
|
|
1161
|
+
: layoutFlowGraph(graph, options);
|
|
1162
|
+
const merged = mergeFlowGeometry(this.model.toJSON(), data);
|
|
1163
|
+
this.setData(merged);
|
|
1164
|
+
if (viewport?.fit)
|
|
1165
|
+
this.fit();
|
|
1166
|
+
return merged;
|
|
1167
|
+
}
|
|
1168
|
+
// 整图替换(原子撤销):先按数据建好新形状/连线实例(端口方向在创建时
|
|
1169
|
+
// 冻结,故必须显式传入),再以一条 flowchart.render 命令整体顶替。
|
|
1170
|
+
// 旧实例留在 undo 闭包内,撤销后此前历史命令引用的对象仍然有效。
|
|
1171
|
+
setData(data) {
|
|
1172
|
+
if (this.interactions.busy)
|
|
1173
|
+
return;
|
|
1174
|
+
const before = this.model.toJSON();
|
|
1175
|
+
const beforeEdgeIds = this.model.edges.map((e) => e.id);
|
|
1176
|
+
const oldShapes = [...this.model.shapes.values()];
|
|
1177
|
+
const oldEdges = [...this.model.edges];
|
|
1178
|
+
const shapes = data.nodes.map((n) => this.model.createShape(n.shape, {
|
|
1179
|
+
id: n.id,
|
|
1180
|
+
x: n.x,
|
|
1181
|
+
y: n.y,
|
|
1182
|
+
width: n.width,
|
|
1183
|
+
height: n.height,
|
|
1184
|
+
text: n.text,
|
|
1185
|
+
}));
|
|
1186
|
+
const byId = new Map(shapes.map((s) => [s.id, s]));
|
|
1187
|
+
const edges = [];
|
|
1188
|
+
const afterEdges = [];
|
|
1189
|
+
for (const e of data.edges) {
|
|
1190
|
+
const from = byId.get(e.from);
|
|
1191
|
+
const to = byId.get(e.to);
|
|
1192
|
+
if (!from || !to)
|
|
1193
|
+
continue;
|
|
1194
|
+
const edge = new FlowEdge(from, to, { from: e.fromDir, to: e.toDir });
|
|
1195
|
+
edge.router = this.router;
|
|
1196
|
+
if (e.text)
|
|
1197
|
+
edge.label.text = e.text;
|
|
1198
|
+
edges.push(edge);
|
|
1199
|
+
afterEdges.push({
|
|
1200
|
+
from: e.from,
|
|
1201
|
+
to: e.to,
|
|
1202
|
+
fromDir: edge.fromDir,
|
|
1203
|
+
toDir: edge.toDir,
|
|
1204
|
+
...(e.text ? { text: e.text } : {}),
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
const payload = {
|
|
1208
|
+
before: { data: before, edgeIds: beforeEdgeIds },
|
|
1209
|
+
after: {
|
|
1210
|
+
data: { nodes: shapes.map((s) => shapeData(s)), edges: afterEdges },
|
|
1211
|
+
edgeIds: edges.map((e) => e.id),
|
|
1212
|
+
},
|
|
1213
|
+
};
|
|
1214
|
+
// 清掉选中框/端口/编辑覆盖层,避免它们指向被换出的旧实例;先按
|
|
1215
|
+
// id/两端节点记录选中项,替换后对上同源新实例再选回(AI 改图不丢激活态)
|
|
1216
|
+
const selShapeIds = selShapesOf(this.sel).map((s) => s.id);
|
|
1217
|
+
const selEdgeBefore = selEdgeOf(this.sel);
|
|
1218
|
+
this.interactions.select(null);
|
|
1219
|
+
this.editing.deactivate();
|
|
1220
|
+
if (this.editing.editing)
|
|
1221
|
+
this.editing.cancelFor(this.editing.editing);
|
|
1222
|
+
this.hover.clearPort();
|
|
1223
|
+
this.execCmd({
|
|
1224
|
+
type: "ai",
|
|
1225
|
+
redo: () => this.model.replaceAll(shapes, edges),
|
|
1226
|
+
undo: () => this.model.replaceAll(oldShapes, oldEdges),
|
|
1227
|
+
toJSON: () => ({ kind: "flowchart.render", payload }),
|
|
1228
|
+
});
|
|
1229
|
+
// 选回同源实例:形状按 id 对上;连线实例重建、id 不稳定,按两端节点对上
|
|
1230
|
+
const restoredShapes = selShapeIds
|
|
1231
|
+
.map((id) => byId.get(id))
|
|
1232
|
+
.filter((s) => !!s);
|
|
1233
|
+
if (selEdgeBefore) {
|
|
1234
|
+
const edge = edges.find((e) => e.fromNode.id === selEdgeBefore.fromNode.id &&
|
|
1235
|
+
e.toNode.id === selEdgeBefore.toNode.id);
|
|
1236
|
+
if (edge)
|
|
1237
|
+
this.interactions.select(edge);
|
|
1238
|
+
}
|
|
1239
|
+
else if (restoredShapes.length === 1) {
|
|
1240
|
+
this.interactions.select(restoredShapes[0]);
|
|
1241
|
+
}
|
|
1242
|
+
else if (restoredShapes.length) {
|
|
1243
|
+
for (const it of restoredShapes)
|
|
1244
|
+
it.selected = true;
|
|
1245
|
+
this.sel.set(restoredShapes);
|
|
1246
|
+
}
|
|
1247
|
+
this.app.requestRender();
|
|
1248
|
+
this.onChange?.("ai");
|
|
1249
|
+
}
|
|
1250
|
+
// 导出当前数据(位置为最新拖拽结果)
|
|
1251
|
+
toJSON() {
|
|
1252
|
+
return this.model.toJSON();
|
|
1253
|
+
}
|
|
1254
|
+
// 导出历史栈为可序列化快照(撤销栈在先、重做栈在后):栈内含不可序列化命令
|
|
1255
|
+
// (如三方闭包命令)时返回 null,拒绝部分导出
|
|
1256
|
+
exportHistory() {
|
|
1257
|
+
return this.history.exportJSON?.() ?? null;
|
|
1258
|
+
}
|
|
1259
|
+
// 导入历史快照替换当前撤销/重做栈:命令按 kind 经领域编解码器重建,形状/连线
|
|
1260
|
+
// 引用按 id 解析——数据 id 须与导出时同源(用带 id 的 toJSON() 往返),任一
|
|
1261
|
+
// 命令重建失败返回 false 且原栈不变
|
|
1262
|
+
importHistory(data) {
|
|
1263
|
+
const codec = createFlowchartCodec({
|
|
1264
|
+
model: this.model,
|
|
1265
|
+
app: this.app,
|
|
1266
|
+
router: this.router,
|
|
1267
|
+
});
|
|
1268
|
+
return this.history.importJSON?.(data, (s) => codec.decode(s)) ?? false;
|
|
1269
|
+
}
|
|
1270
|
+
// 屏幕坐标 → 世界坐标:clientX/Y 为浏览器事件坐标口径(与指针事件一致,
|
|
1271
|
+
// 画布页面偏移由相机自行扣除);新增节点落在视口某屏幕位置等场景使用
|
|
1272
|
+
screenToWorld(clientX, clientY) {
|
|
1273
|
+
return this.app.camera.toWorld(clientX, clientY);
|
|
1274
|
+
}
|
|
1275
|
+
// 当前缩放比例(只读),写入经 setZoom
|
|
1276
|
+
get zoom() {
|
|
1277
|
+
return this.app.camera.viewport.scale;
|
|
1278
|
+
}
|
|
1279
|
+
// 订阅视口变化(滚轮缩放 / 拖拽平移 / setZoom 等):回调收到最新缩放比例,
|
|
1280
|
+
// 返回解绑函数,页面销毁时应撤销以免宿主闭包被相机长期持有
|
|
1281
|
+
onViewportChange(listener) {
|
|
1282
|
+
return this.app.camera.onViewportChange(() => listener(this.app.camera.viewport.scale));
|
|
1283
|
+
}
|
|
1284
|
+
// 以画布中心为锚点设置缩放:缩放前后中心对应的世界坐标保持不动。
|
|
1285
|
+
// 工具栏滑块的语义入口,外部无需接触 camera.viewport 与渲染请求
|
|
1286
|
+
setZoom(scale) {
|
|
1287
|
+
const rect = this.options.canvas.getBoundingClientRect();
|
|
1288
|
+
const vp = this.app.camera.viewport;
|
|
1289
|
+
const wx = (rect.width / 2 - vp.x) / vp.scale;
|
|
1290
|
+
const wy = (rect.height / 2 - vp.y) / vp.scale;
|
|
1291
|
+
this.app.camera.setViewport({
|
|
1292
|
+
x: rect.width / 2 - wx * scale,
|
|
1293
|
+
y: rect.height / 2 - wy * scale,
|
|
1294
|
+
scale,
|
|
1295
|
+
});
|
|
1296
|
+
// setViewport 只更新相机状态,需显式触发相机-only 重渲染
|
|
1297
|
+
this.app.requestCameraRender();
|
|
1298
|
+
}
|
|
1299
|
+
// 清空撤销/重做栈:切换或新建文档时避免一次 Ctrl+Z 退回上一份文档
|
|
1300
|
+
clearHistory() {
|
|
1301
|
+
this.history.clear?.();
|
|
1302
|
+
}
|
|
1303
|
+
// 适应视图:按全部形状包围盒拟合视口(视口能力的语义化入口,不暴露内部状态)
|
|
1304
|
+
fit() {
|
|
1305
|
+
const shapes = [...this.model.shapes.values()];
|
|
1306
|
+
if (!shapes.length)
|
|
1307
|
+
return;
|
|
1308
|
+
const b = unionBounds(shapes);
|
|
1309
|
+
this.app
|
|
1310
|
+
.require(VIEWPORT_CAPABILITY)
|
|
1311
|
+
.fit({ minX: b.x, minY: b.y, maxX: b.x + b.width, maxY: b.y + b.height });
|
|
1312
|
+
this.app.requestRender();
|
|
1313
|
+
}
|
|
1314
|
+
// 居中视图:保持当前缩放不变,仅平移视口使全部形状包围盒中心对齐画布中心
|
|
1315
|
+
//(节点视觉大小不变;初始化时调用,避免 fit 的缩放改变节点观感)
|
|
1316
|
+
center() {
|
|
1317
|
+
const shapes = [...this.model.shapes.values()];
|
|
1318
|
+
if (!shapes.length)
|
|
1319
|
+
return;
|
|
1320
|
+
const b = unionBounds(shapes);
|
|
1321
|
+
this.app
|
|
1322
|
+
.require(VIEWPORT_CAPABILITY)
|
|
1323
|
+
.center({ minX: b.x, minY: b.y, maxX: b.x + b.width, maxY: b.y + b.height });
|
|
1324
|
+
}
|
|
1325
|
+
// 导出全部形状与连线为图片(返回 Blob):委托导出能力离屏渲染,与当前视口无关;
|
|
1326
|
+
// 显式传入形状与连线(不含选中框等覆盖层),绕行连线按路由折线并入包围盒
|
|
1327
|
+
async toImage(options) {
|
|
1328
|
+
return this.app.require(EXPORT_CAPABILITY).export([...this.model.shapes.values(), ...this.model.edges], options);
|
|
1329
|
+
}
|
|
1330
|
+
// 撤销:编辑与拖拽会话中不响应(避免与进行中的变更互踩);回退前先清空选中,
|
|
1331
|
+
// 防止悬空引用(选中框/端口/编辑态指向已摘除的形状或连线)。
|
|
1332
|
+
// 返回是否实际撤销了一条命令(AI 的 undo 操作据此生成执行反馈)
|
|
1333
|
+
undo() {
|
|
1334
|
+
if (this.editing.editing || this.interactions.busy)
|
|
1335
|
+
return false;
|
|
1336
|
+
const cmd = this.history.undo();
|
|
1337
|
+
if (!cmd)
|
|
1338
|
+
return false;
|
|
1339
|
+
this.interactions.select(null);
|
|
1340
|
+
this.app.requestRender();
|
|
1341
|
+
// undo/redo 不产生新变更,仅通知宿主数据已回退(type 为产生该变更的命令类型)
|
|
1342
|
+
this.onChange?.(cmd.type);
|
|
1343
|
+
return true;
|
|
1344
|
+
}
|
|
1345
|
+
// 重做:流程与 undo 对称,方向相反
|
|
1346
|
+
redo() {
|
|
1347
|
+
if (this.editing.editing || this.interactions.busy)
|
|
1348
|
+
return false;
|
|
1349
|
+
const cmd = this.history.redo();
|
|
1350
|
+
if (!cmd)
|
|
1351
|
+
return false;
|
|
1352
|
+
this.interactions.select(null);
|
|
1353
|
+
this.app.requestRender();
|
|
1354
|
+
this.onChange?.(cmd.type);
|
|
1355
|
+
return true;
|
|
1356
|
+
}
|
|
1357
|
+
get canUndo() {
|
|
1358
|
+
return this.history.canUndo;
|
|
1359
|
+
}
|
|
1360
|
+
get canRedo() {
|
|
1361
|
+
return this.history.canRedo;
|
|
1362
|
+
}
|
|
1363
|
+
// 完整拆卸(幂等,页面级销毁或显式装配下重建场景用):
|
|
1364
|
+
// 1) 撤销交互监听;2) 复位接线在宿主插件上的场景回调(编辑器/选中可能为
|
|
1365
|
+
// 显式装配的宿主所有,避免已销毁门面被插件回调持有);
|
|
1366
|
+
// 3) 注销场景命令(键位随命令一并移除)并复位分发守卫;
|
|
1367
|
+
// 4) 移除覆盖层节点(常驻置顶注册随之注销);
|
|
1368
|
+
// 5) 卸载构造期安装的插件(能力注册表沿 previous 链撤销/恢复)。
|
|
1369
|
+
// 便捷模式下自建宿主上的预设插件随场景卸载(宿主随门面一起废弃);
|
|
1370
|
+
// 显式装配下宿主 App 与官方插件不受影响,可继续复用
|
|
1371
|
+
destroy() {
|
|
1372
|
+
this.disposed = true;
|
|
1373
|
+
// 历史栈随场景废弃:栈内命令闭包引用本场景形状/模型,场景销毁后命令失效
|
|
1374
|
+
this.history.clear?.();
|
|
1375
|
+
this.cleanups.forEach((fn) => fn());
|
|
1376
|
+
this.cleanups = [];
|
|
1377
|
+
this.editor.onInput = undefined;
|
|
1378
|
+
this.editor.onEditEnd = undefined;
|
|
1379
|
+
// 结束进行中的编辑会话(回调已复位,stop 触发的 onEditEnd 无副作用)
|
|
1380
|
+
this.editor.stop();
|
|
1381
|
+
this.sel.hitRect = () => [];
|
|
1382
|
+
this.sel.onMarqueeMove = null;
|
|
1383
|
+
this.sel.onMarqueeEnd = null;
|
|
1384
|
+
const commands = this.app.require(COMMAND_CAPABILITY);
|
|
1385
|
+
for (const id of ["flowchart.undo", "flowchart.redo", "flowchart.delete", "flowchart.selectAll", "flowchart.copy", "flowchart.paste"]) {
|
|
1386
|
+
commands.unregister(id);
|
|
1387
|
+
}
|
|
1388
|
+
commands.guard = null;
|
|
1389
|
+
this.app.remove(this.overlay);
|
|
1390
|
+
for (const p of this.scenePlugins)
|
|
1391
|
+
this.app.uninstall(p);
|
|
1392
|
+
this.scenePlugins = [];
|
|
1393
|
+
// 便捷模式:自建宿主上的官方预设插件也随场景卸载(撤销 DOM 事件监听、
|
|
1394
|
+
// 编辑器元素等);显式装配下的宿主插件属宿主所有,保持在线可复用
|
|
1395
|
+
if (this.ownsApp) {
|
|
1396
|
+
for (const p of this.hostPlugins)
|
|
1397
|
+
this.app.uninstall(p);
|
|
1398
|
+
this.hostPlugins = [];
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
//#endregion
|
|
1402
|
+
// 选中的形状(选中连线或无选中时为 null)
|
|
1403
|
+
get selShape() {
|
|
1404
|
+
return selShapeOf(this.sel);
|
|
1405
|
+
}
|
|
1406
|
+
// 选中的连线(选中形状或无选中时为 null)
|
|
1407
|
+
get selEdge() {
|
|
1408
|
+
return selEdgeOf(this.sel);
|
|
1409
|
+
}
|
|
1410
|
+
// 当前选中的形状列表(多选组合或单选形状;选中连线时为空)
|
|
1411
|
+
get selShapes() {
|
|
1412
|
+
return selShapesOf(this.sel);
|
|
1413
|
+
}
|
|
1414
|
+
// 多选组合的包围盒(少于 2 个形状时视为无组合)
|
|
1415
|
+
get multiBounds() {
|
|
1416
|
+
return multiBoundsOf(this.sel);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
// 标签盒圆角归一化:radius 支持单值或四角数组,激活框描边取单值圆角
|
|
1420
|
+
//(连线标签固定用单值圆角,四角数组走首角兜底)
|
|
1421
|
+
function edgeLabelRadius(radius) {
|
|
1422
|
+
if (radius === undefined)
|
|
1423
|
+
return 0;
|
|
1424
|
+
return typeof radius === "number" ? radius : radius[0];
|
|
1425
|
+
}
|