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.
Files changed (55) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +38 -3
  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 +578 -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 +115 -0
  26. package/dist/flowchart.js +1425 -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 +32 -3
@@ -0,0 +1,636 @@
1
+ import { App, EdgePan } from "@silkor/core";
2
+ import { POINTER_CAPABILITY } from "@silkor/core";
3
+ import { FlowEdge, PORT_STUB } from "./FlowEdge";
4
+ import { LinkPreview } from "./overlay";
5
+ import { Shape } from "./Shape";
6
+ import { applyGroupResize, applyResize, cornerHit, edgeHandleHit, edgeHit, findLinkTarget, hitEdge, hitShape, portHit, previewToDir, unionBounds, } from "./geometry";
7
+ import { groupResizeCommand, moveCommand, nudgeCommand, reconnectCommand, resizeCommand, } from "./commands";
8
+ import { createNodeInDirection } from "./quick-create";
9
+ import { CLICK_MOVE_TOLERANCE } from "./session";
10
+ import { multiBounds, selEdge, selShape, selShapes } from "./selection";
11
+ // 交互会话控制器:指针事件 → 交互策略(点选、拖拽移动/缩放、端口连线、
12
+ // 端点重接、方向键微移)与选中语义;会话/吸附/框选组合框状态归本类所有
13
+ export class FlowInteractions {
14
+ deps;
15
+ // 当前交互会话(拖拽移动/缩放/组合缩放/连线/端点重接,无会话为 null)
16
+ session = null;
17
+ // 连线拖拽吸附状态:接近中的目标节点与吸附中的端口方向
18
+ linkTarget = null;
19
+ linkSnap = null;
20
+ // 节点拖拽对齐参考线(applySnap 更新,拖拽结束清除)
21
+ guides = null;
22
+ // 吸附迟滞状态:上一帧两轴是否处于对齐吸附态(拖拽会话内逐帧回传给
23
+ // computeSnap,会话结束清零),已吸附的轴用加倍的释放容差维持吸附,
24
+ // 光标在阈值边界抖动时吸附不反复进出
25
+ snapPrev = { x: false, y: false };
26
+ // 框选拖拽中的实时组合框(已框中节点的并集 bounds,供覆盖层绘制;松手后清空,
27
+ // 由门面的 selection.onMarqueeMove/onMarqueeEnd 写入)
28
+ marqueeCombo = null;
29
+ // 边缘自动平移(core 机制):节点拖拽移动中指针贴近画布边缘时持续平移视口,
30
+ // 每次平移后按指针屏幕坐标重放移动逻辑(节点跟随鼠标);相机-only 呈现由 core 负责
31
+ edgePan;
32
+ constructor(deps) {
33
+ this.deps = deps;
34
+ this.edgePan = new EdgePan(deps.app, (x, y) => this.onEdgePan(x, y));
35
+ }
36
+ // 边缘平移帧回调:仅节点移动会话跟随(缩放/连线等会话不平移视口)
37
+ onEdgePan(clientX, clientY) {
38
+ const s = this.session;
39
+ if (!s || s.kind !== "move")
40
+ return;
41
+ this.applyMove(s, this.deps.app.camera.toWorld(clientX, clientY));
42
+ }
43
+ // 移动会话帧应用:按世界坐标 p 重放「按下时原始位置 + 本帧位移 + 吸附修正」,
44
+ // mousemove 与边缘自动平移的帧回调共用(平移后无需鼠标再动,节点也要跟随)
45
+ applyMove(s, p) {
46
+ if (s.group) {
47
+ // 组合移动:以整体框(成员并集 bounds)的四边/中线与外部节点对齐吸附,
48
+ // 全部组内成员(含被按住的主节点)不作为参考目标,避免内部基线;
49
+ // 全量拖拽位移 + 吸附修正统一应用到全部成员。
50
+ // 吸附基准框必须由各成员"按下时原始位置 + 本帧原始位移"构成——若混入
51
+ // 已被上一帧吸附修正过的当前位置,上一帧的修正量会泄漏进本帧的吸附
52
+ // 计算,吸附会在开/关之间震荡(参考线闪烁、整体位置抖动)
53
+ const exclude = new Set([s.shape, ...s.group.map((g) => g.shape)]);
54
+ const b0 = unionBounds([
55
+ {
56
+ x: s.origin.x,
57
+ y: s.origin.y,
58
+ width: s.shape.width,
59
+ height: s.shape.height,
60
+ },
61
+ ...s.group.map((g) => ({
62
+ x: g.x,
63
+ y: g.y,
64
+ width: g.shape.width,
65
+ height: g.shape.height,
66
+ })),
67
+ ]);
68
+ b0.x += p.x - s.start.x;
69
+ b0.y += p.y - s.start.y;
70
+ const box = { ...b0 };
71
+ this.applySnap(box, exclude);
72
+ const mx = p.x - s.start.x + (box.x - b0.x);
73
+ const my = p.y - s.start.y + (box.y - b0.y);
74
+ s.shape.x = s.origin.x + mx;
75
+ s.shape.y = s.origin.y + my;
76
+ for (const g of s.group) {
77
+ g.shape.x = g.x + mx;
78
+ g.shape.y = g.y + my;
79
+ }
80
+ }
81
+ else {
82
+ s.shape.x = s.origin.x + (p.x - s.start.x);
83
+ s.shape.y = s.origin.y + (p.y - s.start.y);
84
+ this.applySnap(s.shape);
85
+ }
86
+ this.deps.app.requestRender();
87
+ }
88
+ // 交互会话进行中(undo/redo 拒绝执行的判定)
89
+ get busy() {
90
+ return this.session !== null;
91
+ }
92
+ // 统一指针监听:绑定/撤销与屏幕→世界坐标换算由指针能力承担(机制层),
93
+ // 本场景只声明各事件的交互策略;返回的撤销函数由门面并入 cleanups 统一管理
94
+ bind(canvas) {
95
+ const pointer = this.deps.app.require(POINTER_CAPABILITY);
96
+ return pointer.bind(canvas, {
97
+ onDown: (e) => this.onDown(e),
98
+ onWindowMove: (e) => this.onMove(e),
99
+ onWindowUp: (e) => this.onUp(e),
100
+ onKeydown: (e) => this.onKeyDown(e),
101
+ });
102
+ }
103
+ // 悬浮连线待编辑态:按下时命中已选中的连线(第二次点击,即"双击"语义),
104
+ // 抬起未拖动时进入标签编辑(与节点"单击已选中进入编辑"同一交互模式;
105
+ // 延迟到抬起是为避开编辑覆盖层聚焦与本次点击事件的焦点竞争)
106
+ pendingEdgeEdit = null;
107
+ onDown(e) {
108
+ if (e.button !== 0)
109
+ return;
110
+ const p = this.deps.app.camera.toWorld(e.clientX, e.clientY);
111
+ const scale = this.deps.app.camera.viewport.scale;
112
+ this.pendingEdgeEdit = null;
113
+ // 按下后进入交互会话,悬浮端口提示不再显示
114
+ this.deps.hover.reset();
115
+ // 选中态下按优先级命中:角手柄缩放 → 端口连线 → 边线缩放 → 形状体移动
116
+ const sel = selShape(this.deps.sel);
117
+ if (sel) {
118
+ const corner = cornerHit(sel, p, scale);
119
+ if (corner) {
120
+ this.session = {
121
+ kind: "resize",
122
+ shape: sel,
123
+ handle: corner,
124
+ start: p,
125
+ origin: { x: sel.x, y: sel.y, width: sel.width, height: sel.height },
126
+ };
127
+ return;
128
+ }
129
+ const dir = portHit(sel, p, scale);
130
+ if (dir) {
131
+ // 按下时不创建预览边(此刻无法区分点击/拖拽):位移超过点击阈值
132
+ // 进入拖拽连线时才插入预览并取消选中(见 onMove 的 armed 逻辑)
133
+ const preview = new LinkPreview();
134
+ preview.router = this.deps.router;
135
+ preview.from = sel.portAt(dir);
136
+ preview.fromDir = dir;
137
+ preview.fromStub = sel.portStub(dir);
138
+ preview.to = p;
139
+ preview.toDir = previewToDir(dir, preview.from, p);
140
+ this.session = {
141
+ kind: "link",
142
+ source: sel,
143
+ sourceDir: dir,
144
+ preview,
145
+ start: p,
146
+ };
147
+ this.deps.app.requestRender();
148
+ return;
149
+ }
150
+ const side = edgeHit(sel, p, scale);
151
+ if (side) {
152
+ this.session = {
153
+ kind: "resize",
154
+ shape: sel,
155
+ handle: side,
156
+ start: p,
157
+ origin: { x: sel.x, y: sel.y, width: sel.width, height: sel.height },
158
+ };
159
+ return;
160
+ }
161
+ }
162
+ // 激活连线的首尾手柄:拖拽重接该端(优先于命中其他形状)
163
+ const selEdgeDown = selEdge(this.deps.sel);
164
+ if (selEdgeDown) {
165
+ const edge = selEdgeDown;
166
+ const which = edgeHandleHit(edge, p, scale);
167
+ if (which) {
168
+ // 护住激活覆盖层焦点:mousedown 的默认动作(焦点移向不可聚焦的 canvas,
169
+ // 即回落 body)会 blur 覆盖层 → TextEditor 切回隐藏态,重接结束后打字
170
+ // 不再直接进入标签。preventDefault 只拦焦点移动,拖拽重接不受影响
171
+ e.preventDefault();
172
+ // 直接拖拽原连线:被拖端进入拖拽态,路由以光标为虚拟端口
173
+ const fixedDir = which === "from" ? edge.toDir : edge.fromDir;
174
+ const fixedPort = which === "from"
175
+ ? edge.toNode.portAt(edge.toDir)
176
+ : edge.fromNode.portAt(edge.fromDir);
177
+ edge.dragWhich = which;
178
+ edge.dragPoint = p;
179
+ edge.dragDir = previewToDir(fixedDir, fixedPort, p);
180
+ this.session = { kind: "edge-move", edge, which };
181
+ this.deps.canvas.style.cursor = "crosshair";
182
+ this.deps.app.requestRender();
183
+ return;
184
+ }
185
+ }
186
+ // 多选组合框的手柄/边线:整体缩放(优先于命中形状)
187
+ const mb = multiBounds(this.deps.sel);
188
+ if (mb) {
189
+ const handle = cornerHit(mb, p, scale) ?? edgeHit(mb, p, scale);
190
+ if (handle) {
191
+ this.session = {
192
+ kind: "group-resize",
193
+ handle,
194
+ start: p,
195
+ origin: mb,
196
+ origins: selShapes(this.deps.sel).map((s) => ({
197
+ shape: s,
198
+ x: s.x,
199
+ y: s.y,
200
+ width: s.width,
201
+ height: s.height,
202
+ })),
203
+ };
204
+ return;
205
+ }
206
+ }
207
+ const hit = hitShape(this.deps.app.children, p);
208
+ if (hit) {
209
+ // 护住激活覆盖层焦点:mousedown 默认动作(焦点移向不可聚焦的 canvas,
210
+ // 即回落 body)会 blur 覆盖层 → 切回隐藏态,激活态打字/IME 落空。
211
+ // preventDefault 只拦焦点移动,移动/缩放/连线拖拽不受影响
212
+ e.preventDefault();
213
+ // 按下在已单选节点上:记录屏幕坐标,抬起未拖动时进入编辑(拖拽仍优先)。
214
+ // 多选组内按下不记录(未拖动的抬起塌缩为单选,不进编辑)
215
+ const press = this.deps.sel.items.length === 1 && this.deps.sel.items[0] === hit
216
+ ? { x: e.clientX, y: e.clientY }
217
+ : undefined;
218
+ if (this.deps.sel.items.includes(hit)) {
219
+ // 组内形状:整体移动,保持多选(单击不动则抬起时塌缩为单选)
220
+ this.session = {
221
+ kind: "move",
222
+ shape: hit,
223
+ start: p,
224
+ origin: { x: hit.x, y: hit.y },
225
+ moved: false,
226
+ press,
227
+ group: selShapes(this.deps.sel)
228
+ .filter((s) => s !== hit)
229
+ .map((s) => ({ shape: s, x: s.x, y: s.y })),
230
+ };
231
+ }
232
+ else {
233
+ this.select(hit);
234
+ this.session = {
235
+ kind: "move",
236
+ shape: hit,
237
+ start: p,
238
+ origin: { x: hit.x, y: hit.y },
239
+ moved: false,
240
+ group: null,
241
+ press,
242
+ };
243
+ }
244
+ return;
245
+ }
246
+ // 形状未命中时尝试命中连线(形状层在上,优先选中形状);
247
+ // 已选中的连线再次点击(双击语义)时抬起进入标签编辑
248
+ const hitLine = hitEdge(this.deps.model.edges, p, scale);
249
+ if (hitLine) {
250
+ // 关键:连线激活在 mousedown 内同步执行(select → activateLabel →
251
+ // editor.start 的 el.focus 已在本事件处理器中完成)。若放行 mousedown
252
+ // 默认动作,焦点会在处理器返回后被浏览器夺回 body,覆盖层 blur 切回
253
+ // 隐藏态,激活态打字全部落空。mindmap 在 click(mouseup 后)里激活
254
+ // 无此竞争,此处必须显式 preventDefault 护住覆盖层焦点
255
+ e.preventDefault();
256
+ const already = this.deps.sel.items.length === 1 && this.deps.sel.items[0] === hitLine;
257
+ this.select(hitLine);
258
+ if (already)
259
+ this.pendingEdgeEdit = { edge: hitLine, press: { x: e.clientX, y: e.clientY } };
260
+ this.deps.app.requestRender();
261
+ return;
262
+ }
263
+ // 选中形状(单/多选通用):选框并集内任意位置按下即整体移动(无需按在
264
+ // 节点上,菱形/椭圆空白角落也可拖拽);未拖动时保持原选中态不塌缩
265
+ const selList = selShapes(this.deps.sel);
266
+ if (selList.length) {
267
+ const b = unionBounds(selList);
268
+ if (p.x >= b.x &&
269
+ p.x <= b.x + b.width &&
270
+ p.y >= b.y &&
271
+ p.y <= b.y + b.height) {
272
+ const [first, ...rest] = selList;
273
+ this.session = {
274
+ kind: "move",
275
+ shape: first,
276
+ start: p,
277
+ origin: { x: first.x, y: first.y },
278
+ moved: false,
279
+ group: rest.length
280
+ ? rest.map((s) => ({ shape: s, x: s.x, y: s.y }))
281
+ : null,
282
+ keepMulti: true,
283
+ };
284
+ return;
285
+ }
286
+ }
287
+ // 空白按下:立即取消选中,框选会话由选中插件接管(橡皮筋 + 松手落定多选)
288
+ this.select(null);
289
+ this.deps.sel.beginMarquee(e);
290
+ }
291
+ onMove(e) {
292
+ const s = this.session;
293
+ // 无会话时做悬停反馈:缩放手柄/边线/端口/形状体/连线分别给对应光标。
294
+ // 框选会话中跳过:悬浮命中是 O(n) 全树遍历 + O(m) 连线扫掠(每事件执行),
295
+ // 橡皮筋扫过连线时 hoverEdge 翻转还会触发全量重光栅化——大场景框选卡顿主因
296
+ if (!s) {
297
+ if (!this.deps.sel.marqueeActive)
298
+ this.deps.hover.track(e);
299
+ return;
300
+ }
301
+ const p = this.deps.app.camera.toWorld(e.clientX, e.clientY);
302
+ if (s.kind === "move") {
303
+ s.moved = true;
304
+ // 拖拽即结束进行中的编辑会话:提交文本、文字交还 canvas 渲染(输入框
305
+ // 关闭)。否则输入框滞留在按下时的位置,不跟随被拖节点。结束一次后
306
+ // editing 为 null,后续帧自然跳过
307
+ if (this.deps.editing.editing) {
308
+ this.deps.editing.finishEdit();
309
+ this.deps.editor.hide();
310
+ }
311
+ // 边缘自动平移:记录指针位置并确保会话循环在跑(幂等,会话结束在 onUp 停止)
312
+ this.edgePan.start(e.clientX, e.clientY);
313
+ this.applyMove(s, p);
314
+ }
315
+ else if (s.kind === "resize") {
316
+ // 缩放即结束编辑(同移动:输入框不跟随形状变化)
317
+ if (this.deps.editing.editing) {
318
+ this.deps.editing.finishEdit();
319
+ this.deps.editor.hide();
320
+ }
321
+ applyResize(s.shape, s.handle, s.origin, p.x - s.start.x, p.y - s.start.y);
322
+ this.deps.app.requestRender();
323
+ }
324
+ else if (s.kind === "group-resize") {
325
+ if (this.deps.editing.editing) {
326
+ this.deps.editing.finishEdit();
327
+ this.deps.editor.hide();
328
+ }
329
+ applyGroupResize(s.handle, s.origin, s.origins, p.x - s.start.x, p.y - s.start.y);
330
+ this.deps.app.requestRender();
331
+ }
332
+ else if (s.kind === "link") {
333
+ const scale = this.deps.app.camera.viewport.scale;
334
+ // 位移超过点击阈值才确认是拖拽连线:此时才插入预览边并取消选中
335
+ if (!s.armed) {
336
+ if (Math.hypot(p.x - s.start.x, p.y - s.start.y) <=
337
+ CLICK_MOVE_TOLERANCE / scale)
338
+ return;
339
+ s.armed = true;
340
+ const idx = this.deps.app.children.findIndex((n) => n instanceof Shape);
341
+ this.deps.app.insert(s.preview, idx === -1 ? this.deps.app.children.length : idx);
342
+ this.deps.canvas.style.cursor = "crosshair";
343
+ this.select(null);
344
+ }
345
+ this.updateLinkTarget(p);
346
+ // 吸附时预览以手柄的具体位置与端口方向渲染;自由拖拽时按光标相对
347
+ // 出口端口的位置确定虚拟进入方向
348
+ if (this.linkTarget && this.linkSnap) {
349
+ s.preview.to = this.linkTarget.portAt(this.linkSnap);
350
+ s.preview.toDir = this.linkSnap;
351
+ s.preview.toStub = this.linkTarget.portStub(this.linkSnap);
352
+ }
353
+ else {
354
+ s.preview.to = p;
355
+ s.preview.toStub = PORT_STUB;
356
+ s.preview.toDir = previewToDir(s.sourceDir, s.preview.from, p);
357
+ }
358
+ this.deps.app.requestRender();
359
+ }
360
+ else if (s.kind === "edge-move") {
361
+ // 拖拽重接即结束连线标签编辑(同移动:输入框不跟随连线变化)
362
+ if (this.deps.editing.editing) {
363
+ this.deps.editing.finishEdit();
364
+ this.deps.editor.hide();
365
+ }
366
+ // 原连线被拖端跟随光标:吸附时落在目标手柄上,自由拖拽时按出口方向
367
+ // 与光标位置确定虚拟进入方向。任意节点均可吸附(含本连线两端节点,
368
+ // 可调整接入边或形成自环)
369
+ this.updateLinkTarget(p);
370
+ if (this.linkTarget && this.linkSnap) {
371
+ s.edge.dragPoint = this.linkTarget.portAt(this.linkSnap);
372
+ s.edge.dragDir = this.linkSnap;
373
+ }
374
+ else {
375
+ const fixedDir = s.which === "from" ? s.edge.toDir : s.edge.fromDir;
376
+ const fixedPort = s.which === "from"
377
+ ? s.edge.toNode.portAt(s.edge.toDir)
378
+ : s.edge.fromNode.portAt(s.edge.fromDir);
379
+ s.edge.dragPoint = p;
380
+ s.edge.dragDir = previewToDir(fixedDir, fixedPort, p);
381
+ }
382
+ this.deps.app.requestRender();
383
+ }
384
+ }
385
+ // 连线拖拽中更新吸附目标:光标进入某节点包围盒外扩 LINK_SNAP_MARGIN 范围时,
386
+ // 该节点成为目标(浮出对接手柄),并吸附到距光标最近的端口(任意节点均可,
387
+ // 包括连线/预览自身的节点——支持改接边与自环)
388
+ updateLinkTarget(p) {
389
+ const r = findLinkTarget(this.deps.model.shapes.values(), p, this.deps.app.camera.viewport.scale);
390
+ this.linkTarget = r ? r.target : null;
391
+ this.linkSnap = r ? r.snap : null;
392
+ }
393
+ // 节点拖拽吸附:候选参考目标 = 除被拖载体(单形状)与组内成员(组合拖拽)
394
+ // 之外的全部形状;吸附算法按能力键消费(BuiltinSnap 官方实现,第三方可覆盖),
395
+ // 这里只负责筛选候选、回传迟滞状态、把位移写回载体
396
+ applySnap(shape, exclude = null) {
397
+ const scale = this.deps.app.camera.viewport.scale;
398
+ const candidates = [...this.deps.model.shapes.values()].filter((o) => o !== shape && !(exclude && exclude.has(o)));
399
+ const r = this.deps.snapper.snap({
400
+ box: shape,
401
+ candidates,
402
+ scale,
403
+ prev: this.snapPrev,
404
+ });
405
+ this.snapPrev = { x: r.snappedX, y: r.snappedY };
406
+ shape.x += r.dx;
407
+ shape.y += r.dy;
408
+ this.guides = r.guides;
409
+ }
410
+ onUp(e) {
411
+ const s = this.session;
412
+ if (!s) {
413
+ // 无会话的抬起:命中已选中连线的点击(双击语义)且未拖动时进入标签编辑
414
+ const pend = this.pendingEdgeEdit;
415
+ this.pendingEdgeEdit = null;
416
+ if (pend &&
417
+ Math.hypot(e.clientX - pend.press.x, e.clientY - pend.press.y) <=
418
+ CLICK_MOVE_TOLERANCE)
419
+ this.deps.editing.startEdit(pend.edge, pend.press);
420
+ return;
421
+ }
422
+ this.session = null;
423
+ // 交互会话结束:停止边缘自动平移(仅移动会话启动过,幂等)
424
+ this.edgePan.stop();
425
+ this.deps.canvas.style.cursor = "";
426
+ if (s.kind === "move") {
427
+ this.guides = null;
428
+ this.snapPrev = { x: false, y: false };
429
+ this.deps.app.requestRender(); // 清除参考线后立即重绘,否则画面残留拖拽时最后一帧
430
+ if (s.moved) {
431
+ // 拖拽移动记录为可逆命令(变更已发生):主节点与组内成员合并为一条,
432
+ // undo 一次全回退;位置全部未变(吸附归位)时不记录
433
+ const before = [
434
+ { shape: s.shape, x: s.origin.x, y: s.origin.y },
435
+ ...(s.group ?? []).map((g) => ({ shape: g.shape, x: g.x, y: g.y })),
436
+ ];
437
+ const after = before.map((m) => ({
438
+ shape: m.shape,
439
+ x: m.shape.x,
440
+ y: m.shape.y,
441
+ }));
442
+ if (before.some((m) => m.shape.x !== m.x || m.shape.y !== m.y)) {
443
+ this.deps.history.record(moveCommand(before, after));
444
+ }
445
+ this.deps.onChange?.("move");
446
+ }
447
+ else if (s.press && Math.hypot(e.clientX - s.press.x, e.clientY - s.press.y) <= CLICK_MOVE_TOLERANCE)
448
+ // 未拖动的点击落在已选中节点上:进入编辑(光标聚焦到点击处)
449
+ this.deps.editing.startEdit(s.shape, s.press);
450
+ else if (s.group && !s.keepMulti)
451
+ this.select(s.shape); // 组内单击未拖动:塌缩为单选该形状
452
+ }
453
+ else if (s.kind === "resize") {
454
+ const o = s.origin;
455
+ if (s.shape.x !== o.x ||
456
+ s.shape.y !== o.y ||
457
+ s.shape.width !== o.width ||
458
+ s.shape.height !== o.height) {
459
+ // 缩放记录为可逆命令(变更已发生):before/after 各定格一份 bounds
460
+ this.deps.history.record(resizeCommand(s.shape, { x: o.x, y: o.y, width: o.width, height: o.height }, {
461
+ x: s.shape.x,
462
+ y: s.shape.y,
463
+ width: s.shape.width,
464
+ height: s.shape.height,
465
+ }));
466
+ this.deps.onChange?.("resize");
467
+ }
468
+ }
469
+ else if (s.kind === "group-resize") {
470
+ // 有成员相对初始状态发生变化时通知,并记录为可逆命令(变更已发生)
471
+ if (s.origins.some((g) => g.shape.x !== g.x ||
472
+ g.shape.y !== g.y ||
473
+ g.shape.width !== g.width ||
474
+ g.shape.height !== g.height)) {
475
+ const before = s.origins.map((g) => ({
476
+ shape: g.shape,
477
+ x: g.x,
478
+ y: g.y,
479
+ width: g.width,
480
+ height: g.height,
481
+ }));
482
+ const after = s.origins.map((g) => ({
483
+ shape: g.shape,
484
+ x: g.shape.x,
485
+ y: g.shape.y,
486
+ width: g.shape.width,
487
+ height: g.shape.height,
488
+ }));
489
+ this.deps.history.record(groupResizeCommand(before, after));
490
+ this.deps.onChange?.("resize");
491
+ }
492
+ }
493
+ else if (s.kind === "edge-move") {
494
+ // 端点重接结束:退出拖拽态;吸附目标按其端口重接(任意节点,含本连线
495
+ // 两端节点);未吸附时按落点命中节点重接(主导方向自动选边),纯点击
496
+ // 落回被拖端原节点时视为无操作,连线保持原状
497
+ s.edge.dragWhich = null;
498
+ const target = this.linkTarget;
499
+ const snap = this.linkSnap;
500
+ this.linkTarget = null;
501
+ this.linkSnap = null;
502
+ const hit = target ?? hitShape(this.deps.app.children, this.deps.app.camera.toWorld(e.clientX, e.clientY));
503
+ const cur = s.which === "from" ? s.edge.fromNode : s.edge.toNode;
504
+ if (hit && !(hit === cur && !target)) {
505
+ // 重接前定格原端(节点 + 端口方向),undo/redo 对称重接
506
+ const oldNode = cur;
507
+ const oldDir = s.which === "from" ? s.edge.fromDir : s.edge.toDir;
508
+ s.edge.reattachEnd(s.which, hit, target && snap ? snap : undefined);
509
+ const newNode = s.which === "from" ? s.edge.fromNode : s.edge.toNode;
510
+ const newDir = s.which === "from" ? s.edge.fromDir : s.edge.toDir;
511
+ this.deps.history.record(reconnectCommand(s.edge, s.which, oldNode, oldDir, newNode, newDir));
512
+ this.deps.onChange?.("reconnect");
513
+ }
514
+ this.deps.app.requestRender();
515
+ }
516
+ else {
517
+ // 连线交互结束:移除预览。(源形状在按下时已取消激活)
518
+ // 纯点击端口(位移在阈值内):沿端口方向自动新建节点并连线;
519
+ // 拖拽时吸附中的目标按其端口固定创建连线,否则按落点命中建边
520
+ this.deps.model.removeTop(s.preview);
521
+ const target = this.linkTarget;
522
+ const snap = this.linkSnap;
523
+ this.linkTarget = null;
524
+ this.linkSnap = null;
525
+ const p = this.deps.app.camera.toWorld(e.clientX, e.clientY);
526
+ // 未进入拖拽态(armed)即为纯点击端口:沿端口方向自动新建节点并连线
527
+ if (!s.armed) {
528
+ this.createNodeInDirection(s.source, s.sourceDir);
529
+ }
530
+ else if (target && snap) {
531
+ this.deps.model.addEdgeBetween(s.source, target, true, {
532
+ from: s.sourceDir,
533
+ to: snap,
534
+ });
535
+ }
536
+ else {
537
+ const hit = hitShape(this.deps.app.children, p);
538
+ if (hit && hit !== s.source)
539
+ this.deps.model.addEdgeBetween(s.source, hit, true);
540
+ }
541
+ this.deps.app.requestRender();
542
+ }
543
+ }
544
+ // 端口点击快速创建:委托共享的 createNodeInDirection(参考节点 + 方向 +
545
+ // 类型),与门面/AI 程序化新建走同一条几何与原子命令路径,新节点进入选中态
546
+ createNodeInDirection(source, dir) {
547
+ const shape = createNodeInDirection(this.deps, source, dir);
548
+ this.select(shape);
549
+ this.deps.onChange?.("add");
550
+ }
551
+ onKeyDown(e) {
552
+ // 焦点在原生表单控件(AI 输入框等)或外部 contenteditable 时按键属于
553
+ // 文本输入:唤醒编辑会经 editor.start 的 el.focus 抢走输入框焦点(覆盖层
554
+ // 透明不可见,表现为打字/聚焦 AI 输入框时焦点丢失),方向键也会被劫持去
555
+ // 微移节点。此处直接放行。画布自身激活覆盖层除外:选中即挂载覆盖层并聚焦
556
+ // (editor.hasFocus 为 true),其未进入编辑时按键不属于文本输入,方向键
557
+ // 须落到下方微移选中节点;可打印字符/IME/Delete 已被覆盖层元素级唤醒监听
558
+ // 先行处理,document 级监听随后触发时 editing 已置位,自然被下方守卫短路
559
+ const t = e.target;
560
+ const isFormControl = t instanceof HTMLElement && t.matches("input, textarea, select");
561
+ const isContentEditable = t instanceof HTMLElement && t.isContentEditable;
562
+ if (isFormControl || (isContentEditable && !this.deps.editor.hasFocus()))
563
+ return;
564
+ // 编辑态按键属于输入,交由覆盖层处理(方向键移光标、回车换行)
565
+ if (this.deps.editing.editing)
566
+ return;
567
+ // 撤销/重做/删除已注册为命令并绑定键位(构造器),由命令插件统一分发;
568
+ // 此处只处理领域语义按键(方向键微移选中节点)
569
+ // 选中节点直接打字进入编辑(同连线激活态"打字即编辑"语义):可打印字符/
570
+ // IME 唤醒空缓冲编辑态——不拦截默认插入,按键经 focus 转移落入覆盖层并由
571
+ // input 整体替换节点文本。方向键优先微移不唤醒;修饰组合(撤销/复制等
572
+ // 快捷键)不唤醒;多选(单选模型为空)与选中连线不在此唤醒
573
+ const sel = selShape(this.deps.sel);
574
+ if (sel &&
575
+ !e.ctrlKey &&
576
+ !e.metaKey &&
577
+ !e.altKey &&
578
+ (e.key.length === 1 || e.key === "Process" || e.keyCode === 229))
579
+ this.deps.editing.startEdit(sel);
580
+ // 方向键:微移选中节点(Shift 加速),连线不参与移动
581
+ const dir = {
582
+ ArrowLeft: [-1, 0],
583
+ ArrowRight: [1, 0],
584
+ ArrowUp: [0, -1],
585
+ ArrowDown: [0, 1],
586
+ }[e.key];
587
+ if (!dir)
588
+ return;
589
+ const step = e.shiftKey ? 10 : 1;
590
+ const dx = dir[0] * step;
591
+ const dy = dir[1] * step;
592
+ const targets = selShapes(this.deps.sel);
593
+ if (!targets.length)
594
+ return;
595
+ e.preventDefault();
596
+ // 微移记录为可逆命令(变更已发生):undo 回到按前位置,redo 重放同向位移
597
+ const before = targets.map((s) => ({ shape: s, x: s.x, y: s.y }));
598
+ for (const s of targets) {
599
+ s.x += dx;
600
+ s.y += dy;
601
+ }
602
+ this.deps.history.record(nudgeCommand(targets, dx, dy, before));
603
+ // 节点已移动:重新对齐可能常驻挂载的激活覆盖层(其位置在挂载/输入时对齐)
604
+ this.deps.editor.reposition();
605
+ this.deps.app.requestRender();
606
+ this.deps.onChange?.("move");
607
+ }
608
+ select(sel) {
609
+ // 多选与单选互斥:选中集合整体替换,天然互斥
610
+ // 选中变化后悬浮端口不再有效(可能指向新选中形状的错误方向)
611
+ this.deps.hover.clearPort();
612
+ if (sel) {
613
+ if (this.deps.sel.items.length === 1 && this.deps.sel.items[0] === sel)
614
+ return;
615
+ for (const it of this.deps.sel.items)
616
+ it.selected = false;
617
+ this.deps.sel.set([sel]);
618
+ sel.selected = true;
619
+ // 激活的连线提到连线层最上再渲染,避免激活样式被其他连线覆盖
620
+ if (sel instanceof FlowEdge)
621
+ this.deps.model.raiseEdgeTop(sel);
622
+ // 单选(形状或连线):挂载空缓冲激活覆盖层(隐藏、已聚焦、常驻接收
623
+ // 打字/IME,打字即编辑)。覆盖层须在 IME 组合开始前就位,故选中即挂载
624
+ this.deps.editing.activate(sel);
625
+ }
626
+ else {
627
+ if (!this.deps.sel.items.length)
628
+ return;
629
+ for (const it of this.deps.sel.items)
630
+ it.selected = false;
631
+ this.deps.sel.clear();
632
+ this.deps.editing.deactivate();
633
+ }
634
+ this.deps.app.requestRender();
635
+ }
636
+ }