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
@@ -0,0 +1,58 @@
1
+ import { App, Node } from "@silkor/core";
2
+ import type { HistoryCapability, HistoryCommand } from "@silkor/core";
3
+ import { FlowEdge } from "./FlowEdge";
4
+ import type { FlowEdgeHints } from "./FlowEdge";
5
+ import { Shape } from "./Shape";
6
+ import type { ShapeOptions } from "./Shape";
7
+ import type { FlowChangeType, FlowData, ShapeKind } from "./data";
8
+ import type { EdgeRouterCapability } from "./edge-router";
9
+ import type { ShapeRegistryCapability } from "./shape-registry";
10
+ export interface FlowModelDeps {
11
+ app: App;
12
+ registry: ShapeRegistryCapability;
13
+ router: EdgeRouterCapability;
14
+ history: HistoryCapability;
15
+ onChange?: (type: FlowChangeType) => void;
16
+ onDetach: (shape: Shape) => void;
17
+ onSelect: (item: Shape | FlowEdge | null) => void;
18
+ }
19
+ export declare function captureRemoval(children: readonly Node[], edges: FlowEdge[], shape: Shape): {
20
+ shape: Shape;
21
+ childIdx: number;
22
+ edges: {
23
+ edge: FlowEdge;
24
+ order: number;
25
+ childIdx: number;
26
+ }[];
27
+ };
28
+ export declare class FlowModel {
29
+ private deps;
30
+ overlay: Node;
31
+ shapes: Map<string, Shape>;
32
+ edges: FlowEdge[];
33
+ constructor(deps: FlowModelDeps);
34
+ load(data: FlowData): void;
35
+ replaceAll(shapes: Shape[], edges: FlowEdge[]): void;
36
+ createShape(kind: ShapeKind, options: ShapeOptions): Shape;
37
+ registerShape(shape: Shape): void;
38
+ addEdgeBetween(from: Shape, to: Shape, notify: boolean, hints?: FlowEdgeHints): FlowEdge;
39
+ edgeAddRemove(edge: FlowEdge): HistoryCommand;
40
+ insertEdge(edge: FlowEdge): void;
41
+ removeShape(shape: Shape): void;
42
+ removeTop(node: Node): void;
43
+ insertAt(node: Node, idx: number): void;
44
+ captureShapeRemoval(shape: Shape): {
45
+ shape: Shape;
46
+ childIdx: number;
47
+ edges: {
48
+ edge: FlowEdge;
49
+ order: number;
50
+ childIdx: number;
51
+ }[];
52
+ };
53
+ applyShapeRemoval(c: ReturnType<FlowModel["captureShapeRemoval"]>): void;
54
+ restoreShapeRemoval(c: ReturnType<FlowModel["captureShapeRemoval"]>): void;
55
+ swapShape(a: Shape, b: Shape): void;
56
+ raiseEdgeTop(edge: FlowEdge): void;
57
+ toJSON(): FlowData;
58
+ }
package/dist/model.js ADDED
@@ -0,0 +1,233 @@
1
+ import { App, Node } from "@silkor/core";
2
+ import { FlowEdge } from "./FlowEdge";
3
+ import { Shape } from "./Shape";
4
+ // 删除捕获的索引运算(纯函数,自 FlowModel.captureShapeRemoval 抽出便于单测):
5
+ // 形状 + 原 z 序索引 + 关联连线(各自的连线序与 z 序索引)。
6
+ // 撤销删除时按索引插回,恢复原 z 序(连线层在形状层之下)
7
+ export function captureRemoval(children, edges, shape) {
8
+ return {
9
+ shape,
10
+ childIdx: children.indexOf(shape),
11
+ edges: edges
12
+ .map((edge, order) => ({
13
+ edge,
14
+ order,
15
+ childIdx: children.indexOf(edge),
16
+ }))
17
+ .filter((c) => c.edge.fromNode === shape || c.edge.toNode === shape),
18
+ };
19
+ }
20
+ // 流程图模型:形状表与连线表的唯一属主,负责建载/增删/顶替/删除捕获恢复
21
+ // 与 z 序维护(连线层在形状层之下;覆盖层置顶由 App.addOverlay 自动保持),不做命中与交互
22
+ export class FlowModel {
23
+ deps;
24
+ // 选中框覆盖层引用(门面创建后注入):raiseEdgeTop 以它为连线层上界
25
+ overlay;
26
+ shapes = new Map();
27
+ edges = [];
28
+ constructor(deps) {
29
+ this.deps = deps;
30
+ }
31
+ load(data) {
32
+ for (const n of data.nodes) {
33
+ // 数据中的 kind 可能来自未装配的第三方形状插件(Extension 缺失允许降级):
34
+ // 跳过该节点,其关联连线因端点缺失在下方一并跳过
35
+ const shape = this.deps.registry.create(n.shape, {
36
+ id: n.id,
37
+ x: n.x,
38
+ y: n.y,
39
+ width: n.width,
40
+ height: n.height,
41
+ text: n.text,
42
+ });
43
+ if (!shape)
44
+ continue;
45
+ this.registerShape(shape);
46
+ }
47
+ for (const e of data.edges) {
48
+ const from = this.shapes.get(e.from);
49
+ const to = this.shapes.get(e.to);
50
+ if (!from || !to)
51
+ continue;
52
+ const edge = this.addEdgeBetween(from, to, false, { from: e.fromDir, to: e.toDir });
53
+ if (e.text)
54
+ edge.label.text = e.text;
55
+ }
56
+ this.deps.app.requestRender();
57
+ }
58
+ // 整图替换:摘除当前全部形状与连线,挂上给定实例集合。
59
+ // 供 AI 生成/程序化重排的原子命令使用——保留实例引用,undo 后旧历史命令
60
+ // 仍能命中同一对象;连线经 insertEdge 保证画在形状层之下
61
+ replaceAll(shapes, edges) {
62
+ for (const edge of this.edges)
63
+ this.removeTop(edge);
64
+ for (const shape of this.shapes.values())
65
+ this.removeTop(shape);
66
+ this.edges = [];
67
+ this.shapes.clear();
68
+ for (const shape of shapes)
69
+ this.registerShape(shape);
70
+ for (const edge of edges) {
71
+ this.edges.push(edge);
72
+ this.insertEdge(edge);
73
+ }
74
+ this.deps.app.requestRender();
75
+ }
76
+ // 按注册表创建形状:编程路径(addNode/replaceSelected/快速建节点)的 kind
77
+ // 来自类型化 API 或既有节点,未注册属于编程错误,fail fast
78
+ createShape(kind, options) {
79
+ const shape = this.deps.registry.create(kind, options);
80
+ if (!shape)
81
+ throw new Error(`Unknown shape kind: "${kind}"`);
82
+ return shape;
83
+ }
84
+ registerShape(shape) {
85
+ this.shapes.set(shape.id, shape);
86
+ this.deps.app.add(shape);
87
+ }
88
+ // 新建连线:两节点间可存在多条连线,不做去重限制;
89
+ // 插入到形状层之下保证连线画在节点下方;
90
+ // hints 指定两端固定端口(拖拽吸附创建时传入),缺省按主导方向自动选边。
91
+ // notify 时创建记录为可逆命令(变更已发生,record 仅入栈)
92
+ addEdgeBetween(from, to, notify, hints) {
93
+ const edge = new FlowEdge(from, to, hints);
94
+ edge.router = this.deps.router;
95
+ this.edges.push(edge);
96
+ this.insertEdge(edge);
97
+ if (notify) {
98
+ this.deps.history.record({
99
+ type: "connect",
100
+ ...this.edgeAddRemove(edge),
101
+ });
102
+ this.deps.onChange?.("connect");
103
+ }
104
+ return edge;
105
+ }
106
+ // 连线增删的可逆对:redo 压回连线表并插入形状层之下,undo 摘除。
107
+ // 供 connect 命令直接入栈,也供组合命令复用。
108
+ // 序列化载荷:连线与两端节点按 id 引用,端口方向为纯数据
109
+ edgeAddRemove(edge) {
110
+ return {
111
+ redo: () => {
112
+ this.edges.push(edge);
113
+ this.insertEdge(edge);
114
+ },
115
+ undo: () => {
116
+ this.edges = this.edges.filter((e) => e !== edge);
117
+ this.removeTop(edge);
118
+ },
119
+ toJSON: () => ({
120
+ kind: "flowchart.connect",
121
+ payload: {
122
+ edgeId: edge.id,
123
+ fromId: edge.fromNode.id,
124
+ toId: edge.toNode.id,
125
+ fromDir: edge.fromDir,
126
+ toDir: edge.toDir,
127
+ text: edge.label.text || undefined,
128
+ },
129
+ }),
130
+ };
131
+ }
132
+ // 连线插入到形状层之下(首个 Shape 之前),保证画在节点下方
133
+ insertEdge(edge) {
134
+ const idx = this.deps.app.children.findIndex((n) => n instanceof Shape);
135
+ this.deps.app.insert(edge, idx === -1 ? this.deps.app.children.length : idx);
136
+ }
137
+ removeShape(shape) {
138
+ this.deps.onDetach(shape);
139
+ const related = this.edges.filter((e) => e.fromNode === shape || e.toNode === shape);
140
+ this.edges = this.edges.filter((e) => !related.includes(e));
141
+ related.forEach((e) => this.removeTop(e));
142
+ this.removeTop(shape);
143
+ this.shapes.delete(shape.id);
144
+ }
145
+ removeTop(node) {
146
+ this.deps.app.remove(node);
147
+ }
148
+ // 按 z 序索引插回画布(undo/redo 重放同一索引,不依赖恢复时的树状态)
149
+ insertAt(node, idx) {
150
+ this.deps.app.insert(node, idx);
151
+ }
152
+ // 删除捕获:形状 + 原 z 序索引 + 关联连线(各自的连线序与 z 序索引)。
153
+ // 撤销删除时按索引插回,恢复原 z 序(连线层在形状层之下)
154
+ captureShapeRemoval(shape) {
155
+ return captureRemoval(this.deps.app.children, this.edges, shape);
156
+ }
157
+ // 应用删除:先摘关联连线再摘形状(removeShape 内含编辑态清理与取消选中)
158
+ applyShapeRemoval(c) {
159
+ for (const { edge } of c.edges) {
160
+ this.edges = this.edges.filter((e) => e !== edge);
161
+ this.removeTop(edge);
162
+ }
163
+ this.removeShape(c.shape);
164
+ }
165
+ // 恢复删除:按记录的连线序与 z 序索引插回连线与形状
166
+ restoreShapeRemoval(c) {
167
+ for (const { edge, order, childIdx } of c.edges) {
168
+ this.edges.splice(Math.min(order, this.edges.length), 0, edge);
169
+ this.insertAt(edge, childIdx);
170
+ }
171
+ this.insertAt(c.shape, c.childIdx);
172
+ this.shapes.set(c.shape.id, c.shape);
173
+ }
174
+ // 把 b 顶替 a:重绑全部连线两端引用、占据 a 的 z 序位置并更新形状表;
175
+ // b 为替身形状(预先创建),replaceSelected 的 undo/redo 共用
176
+ swapShape(a, b) {
177
+ this.deps.onDetach(a);
178
+ for (const e of this.edges) {
179
+ if (e.fromNode === a)
180
+ e.fromNode = b;
181
+ if (e.toNode === a)
182
+ e.toNode = b;
183
+ }
184
+ const idx = this.deps.app.children.indexOf(a);
185
+ this.removeTop(a);
186
+ this.shapes.delete(a.id);
187
+ if (idx !== -1) {
188
+ this.deps.app.insert(b, idx);
189
+ }
190
+ else {
191
+ this.deps.app.add(b);
192
+ }
193
+ this.shapes.set(b.id, b);
194
+ this.deps.onSelect(b);
195
+ }
196
+ // 把连线移到连线层最上(保持在形状层/覆盖层之下)
197
+ raiseEdgeTop(edge) {
198
+ const children = this.deps.app.children;
199
+ const from = children.indexOf(edge);
200
+ if (from === -1)
201
+ return;
202
+ let to = from;
203
+ for (let j = from + 1; j < children.length; j++) {
204
+ const c = children[j];
205
+ if (c instanceof Shape || c === this.overlay)
206
+ break;
207
+ to = j;
208
+ }
209
+ if (to !== from)
210
+ this.deps.app.reorder(edge, to);
211
+ }
212
+ // 导出当前数据(位置为最新拖拽结果)
213
+ toJSON() {
214
+ return {
215
+ nodes: [...this.shapes.values()].map((s) => ({
216
+ id: s.id ?? undefined,
217
+ shape: s.kind,
218
+ x: s.x,
219
+ y: s.y,
220
+ width: s.width,
221
+ height: s.height,
222
+ text: s.text.text,
223
+ })),
224
+ edges: this.edges.map((e) => ({
225
+ from: e.fromNode.id,
226
+ to: e.toNode.id,
227
+ fromDir: e.fromDir,
228
+ toDir: e.toDir,
229
+ text: e.label.text || undefined,
230
+ })),
231
+ };
232
+ }
233
+ }
@@ -0,0 +1,60 @@
1
+ import { Node } from "@silkor/core";
2
+ import type { PortDir } from "./FlowEdge";
3
+ import type { EdgeRouterCapability } from "./edge-router";
4
+ import type { Shape } from "./Shape";
5
+ import type { Bounds, SnapGuides } from "./snap";
6
+ export declare class LinkPreview extends Node {
7
+ from: {
8
+ x: number;
9
+ y: number;
10
+ };
11
+ to: {
12
+ x: number;
13
+ y: number;
14
+ };
15
+ fromDir: PortDir;
16
+ toDir: PortDir;
17
+ fromStub: number;
18
+ toStub: number;
19
+ router: EdgeRouterCapability;
20
+ render(ctx: CanvasRenderingContext2D): void;
21
+ hitTest(): boolean;
22
+ }
23
+ export declare class SelectionOverlay extends Node {
24
+ private getState;
25
+ constructor(getState: () => {
26
+ shape: Shape | null;
27
+ scale: number;
28
+ ports: Array<{
29
+ x: number;
30
+ y: number;
31
+ }>;
32
+ hoverPort: PortDir | null;
33
+ edgeHandles: Array<{
34
+ x: number;
35
+ y: number;
36
+ }>;
37
+ edgeLabel: {
38
+ x: number;
39
+ y: number;
40
+ width: number;
41
+ height: number;
42
+ radius: number;
43
+ } | null;
44
+ link: {
45
+ ports: Array<{
46
+ x: number;
47
+ y: number;
48
+ }>;
49
+ active: {
50
+ x: number;
51
+ y: number;
52
+ } | null;
53
+ } | null;
54
+ guides: SnapGuides | null;
55
+ marquee: Bounds | null;
56
+ });
57
+ render(ctx: CanvasRenderingContext2D): void;
58
+ private drawPorts;
59
+ hitTest(): boolean;
60
+ }
@@ -0,0 +1,242 @@
1
+ // 画布覆盖层:连线拖拽预览与选中态渲染——只负责"画",交互状态由 FlowChart
2
+ // 经 getState 回调按帧提供,覆盖层自身不持有交互状态
3
+ import { Node } from "@silkor/core";
4
+ import { PORT_DIR_VEC, PORT_STUB } from "./FlowEdge";
5
+ import { PORT_HOVER_SCALE, PORT_RADIUS } from "./geometry";
6
+ // 选中框(bounds)外观参数:尺寸用屏幕像素,除以视口缩放换算成世界坐标
7
+ const SELECT_COLOR = "#3b82f6";
8
+ const HANDLE_SIZE = 9; // 四角手柄边长(屏幕像素)
9
+ // 节点拖拽吸附:对齐参考线颜色(吸附算法与容差在吸附能力实现内,此处只负责渲染)
10
+ const ALIGN_COLOR = "#eea5ffff";
11
+ // 连线拖拽预览:与正式连线同款式的折线 + 箭头
12
+ const LINK_PREVIEW_COLOR = "#3b82f6";
13
+ // 连线拖拽预览:与正式连线同款式的折线 + 箭头(from 为固定端端口及其方向,
14
+ // to 为光标或吸附端端口;toDir 随光标相对出口端口的位置实时变化)。
15
+ // 路由与渲染按能力键消费边路由能力(FlowChart 装配时注入)
16
+ export class LinkPreview extends Node {
17
+ from = { x: 0, y: 0 };
18
+ to = { x: 0, y: 0 };
19
+ fromDir = "right";
20
+ toDir = "left";
21
+ // 两端直段长度:源端在创建时按其 portStub 设置,目标端吸附时跟随目标节点
22
+ fromStub = PORT_STUB;
23
+ toStub = PORT_STUB;
24
+ router;
25
+ render(ctx) {
26
+ ctx.save();
27
+ const pts = this.router.route({
28
+ from: this.from,
29
+ fromDir: this.fromDir,
30
+ to: this.to,
31
+ toDir: this.toDir,
32
+ fromStub: this.fromStub,
33
+ toStub: this.toStub,
34
+ });
35
+ this.router.render(ctx, pts, {
36
+ color: LINK_PREVIEW_COLOR,
37
+ lineWidth: 2,
38
+ cornerRadius: 8,
39
+ });
40
+ ctx.restore();
41
+ }
42
+ hitTest() {
43
+ return false;
44
+ }
45
+ }
46
+ // 选中框覆盖层:为选中形状绘制 bounds 边框、四角缩放手柄与外浮的连线端口圆点,
47
+ // 连线拖拽时为目标节点绘制对接手柄并高亮吸附端口,
48
+ // 尺寸按视口缩放反算,保证任何缩放级别下都是恒定屏幕像素大小
49
+ export class SelectionOverlay extends Node {
50
+ getState;
51
+ constructor(getState) {
52
+ super();
53
+ this.getState = getState;
54
+ }
55
+ render(ctx) {
56
+ const { shape, scale, ports, hoverPort, edgeHandles, edgeLabel, link, guides, marquee, } = this.getState();
57
+ const k = 1 / scale;
58
+ ctx.save();
59
+ // 框选框 / 多选组合框:与单选框同样式(边框 + 四角手柄),无连线端口
60
+ if (marquee) {
61
+ ctx.strokeStyle = SELECT_COLOR;
62
+ ctx.lineWidth = 1.5 * k;
63
+ ctx.strokeRect(marquee.x, marquee.y, marquee.width, marquee.height);
64
+ const s = HANDLE_SIZE * k;
65
+ const { x, y, width: w, height: h } = marquee;
66
+ for (const [cx, cy] of [
67
+ [x, y],
68
+ [x + w, y],
69
+ [x + w, y + h],
70
+ [x, y + h],
71
+ ]) {
72
+ ctx.beginPath();
73
+ ctx.rect(cx - s / 2, cy - s / 2, s, s);
74
+ ctx.fillStyle = "#ffffff";
75
+ ctx.fill();
76
+ ctx.stroke();
77
+ }
78
+ }
79
+ if (shape) {
80
+ const { x, y, width: w, height: h } = shape;
81
+ // bounds 边框
82
+ ctx.strokeStyle = SELECT_COLOR;
83
+ ctx.lineWidth = 1.5 * k;
84
+ ctx.strokeRect(x, y, w, h);
85
+ // 四角手柄(白底蓝边方块)
86
+ const s = HANDLE_SIZE * k;
87
+ for (const [cx, cy] of [
88
+ [x, y],
89
+ [x + w, y],
90
+ [x + w, y + h],
91
+ [x, y + h],
92
+ ]) {
93
+ ctx.beginPath();
94
+ ctx.rect(cx - s / 2, cy - s / 2, s, s);
95
+ ctx.fillStyle = "#ffffff";
96
+ ctx.fill();
97
+ ctx.stroke();
98
+ }
99
+ // 四向连线端口圆点(浮在形状外侧),悬浮端口放大并显示方向箭头
100
+ const dirs = ["top", "right", "bottom", "left"];
101
+ this.drawPorts(ctx, ports, k, null, hoverPort ? dirs.indexOf(hoverPort) : -1, hoverPort);
102
+ }
103
+ // 激活连线的首尾端点手柄(白底蓝边圆形)
104
+ if (edgeHandles.length) {
105
+ ctx.strokeStyle = SELECT_COLOR;
106
+ ctx.lineWidth = 1.5 * k;
107
+ const r = (HANDLE_SIZE / 2) * k;
108
+ for (const h of edgeHandles) {
109
+ ctx.beginPath();
110
+ ctx.arc(h.x, h.y, r, 0, Math.PI * 2);
111
+ ctx.fillStyle = "#ffffff";
112
+ ctx.fill();
113
+ ctx.stroke();
114
+ }
115
+ }
116
+ // 激活连线的标签激活框:紧贴标签盒的蓝色圆角描边(类似思维导图节点高亮环),
117
+ // 圆角沿用标签自身圆角;标签随连线重定位,此处逐帧读状态自动跟随
118
+ if (edgeLabel) {
119
+ ctx.strokeStyle = SELECT_COLOR;
120
+ ctx.lineWidth = 1.5 * k;
121
+ ctx.beginPath();
122
+ ctx.roundRect(edgeLabel.x, edgeLabel.y, edgeLabel.width, edgeLabel.height, edgeLabel.radius);
123
+ ctx.stroke();
124
+ }
125
+ // 连线拖拽目标:浮出对接手柄,吸附中的手柄实心高亮
126
+ if (link)
127
+ this.drawPorts(ctx, link.ports, k, link.active);
128
+ // 对齐参考线:贯穿画布的虚线
129
+ if (guides && (guides.v.length || guides.h.length)) {
130
+ ctx.strokeStyle = ALIGN_COLOR;
131
+ ctx.lineWidth = 1 * k;
132
+ ctx.setLineDash([4 * k, 4 * k]);
133
+ ctx.beginPath();
134
+ for (const x of guides.v) {
135
+ ctx.moveTo(x, -10000);
136
+ ctx.lineTo(x, 10000);
137
+ }
138
+ for (const y of guides.h) {
139
+ ctx.moveTo(-10000, y);
140
+ ctx.lineTo(10000, y);
141
+ }
142
+ ctx.stroke();
143
+ ctx.setLineDash([]);
144
+ }
145
+ // 等间距测量线:虚线段 + 两端刻度 + 间距数值标签
146
+ if (guides && guides.spacing.length) {
147
+ ctx.strokeStyle = ALIGN_COLOR;
148
+ ctx.lineWidth = 1 * k;
149
+ ctx.setLineDash([4 * k, 4 * k]);
150
+ ctx.beginPath();
151
+ for (const g of guides.spacing) {
152
+ ctx.moveTo(g.x1, g.y1);
153
+ ctx.lineTo(g.x2, g.y2);
154
+ }
155
+ ctx.stroke();
156
+ ctx.setLineDash([]);
157
+ // 两端垂直于线段的刻度
158
+ const t = 3 * k;
159
+ ctx.beginPath();
160
+ for (const g of guides.spacing) {
161
+ if (g.y1 === g.y2) {
162
+ ctx.moveTo(g.x1, g.y1 - t);
163
+ ctx.lineTo(g.x1, g.y1 + t);
164
+ ctx.moveTo(g.x2, g.y2 - t);
165
+ ctx.lineTo(g.x2, g.y2 + t);
166
+ }
167
+ else {
168
+ ctx.moveTo(g.x1 - t, g.y1);
169
+ ctx.lineTo(g.x1 + t, g.y1);
170
+ ctx.moveTo(g.x2 - t, g.y2);
171
+ ctx.lineTo(g.x2 + t, g.y2);
172
+ }
173
+ }
174
+ ctx.stroke();
175
+ // 数值标签(圆角底 + 白字),缩放级别恒定屏幕像素大小
176
+ ctx.font = `${10 * k}px system-ui, sans-serif`;
177
+ ctx.textAlign = "center";
178
+ ctx.textBaseline = "middle";
179
+ const padX = 3 * k;
180
+ const h = 14 * k;
181
+ for (const g of guides.spacing) {
182
+ const label = String(Math.round(g.value));
183
+ const w = ctx.measureText(label).width;
184
+ const mx = (g.x1 + g.x2) / 2;
185
+ const my = (g.y1 + g.y2) / 2;
186
+ ctx.beginPath();
187
+ ctx.roundRect(mx - w / 2 - padX, my - h / 2, w + padX * 2, h, h / 2);
188
+ ctx.fillStyle = ALIGN_COLOR;
189
+ ctx.fill();
190
+ ctx.fillStyle = "#ffffff";
191
+ ctx.fillText(label, mx, my);
192
+ }
193
+ }
194
+ ctx.restore();
195
+ }
196
+ // 绘制端口圆点:active 非空时该位置的圆点改为实心蓝(吸附高亮);
197
+ // hoverIdx/hoverDir 指向悬浮端口:放大、实心蓝并在内部绘制指向外侧的方向箭头
198
+ // (提示点击可沿该方向快速新建节点)
199
+ drawPorts(ctx, pts, k, active, hoverIdx = -1, hoverDir) {
200
+ ctx.strokeStyle = SELECT_COLOR;
201
+ pts.forEach((p, i) => {
202
+ const isActive = active && active.x === p.x && active.y === p.y;
203
+ const isHover = i === hoverIdx && hoverDir;
204
+ const r = (isHover ? PORT_RADIUS * PORT_HOVER_SCALE : PORT_RADIUS) * k;
205
+ ctx.beginPath();
206
+ ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
207
+ if (isActive || isHover) {
208
+ ctx.fillStyle = SELECT_COLOR;
209
+ ctx.fill();
210
+ }
211
+ else {
212
+ ctx.fillStyle = "#ffffff";
213
+ ctx.fill();
214
+ // 显式恢复描边样式:悬浮端口的白色箭头会改写 strokeStyle,
215
+ // 不重置会让其后绘制的端口用白色描边(视觉上"样式被改变")
216
+ ctx.strokeStyle = SELECT_COLOR;
217
+ ctx.lineWidth = 1.5 * k;
218
+ ctx.stroke();
219
+ }
220
+ // 悬浮箭头:白色雪佛龙指向端口外法向
221
+ if (isHover && hoverDir) {
222
+ const v = PORT_DIR_VEC[hoverDir];
223
+ const n = { x: -v.y, y: v.x };
224
+ const tip = { x: p.x + v.x * r * 0.45, y: p.y + v.y * r * 0.45 };
225
+ const base = { x: p.x - v.x * r * 0.2, y: p.y - v.y * r * 0.2 };
226
+ const w = r * 0.42;
227
+ ctx.strokeStyle = "#ffffff";
228
+ ctx.lineWidth = 1.5 * k;
229
+ ctx.lineCap = "round";
230
+ ctx.beginPath();
231
+ ctx.moveTo(base.x + n.x * w, base.y + n.y * w);
232
+ ctx.lineTo(tip.x, tip.y);
233
+ ctx.lineTo(base.x - n.x * w, base.y - n.y * w);
234
+ ctx.stroke();
235
+ ctx.lineCap = "butt";
236
+ }
237
+ });
238
+ }
239
+ hitTest() {
240
+ return false;
241
+ }
242
+ }
@@ -0,0 +1,12 @@
1
+ import type { HistoryCapability } from "@silkor/core";
2
+ import type { PortDir } from "./FlowEdge";
3
+ import type { ShapeKind } from "./data";
4
+ import type { EdgeRouterCapability } from "./edge-router";
5
+ import type { FlowModel } from "./model";
6
+ import type { Shape } from "./Shape";
7
+ export interface QuickCreateDeps {
8
+ model: FlowModel;
9
+ router: EdgeRouterCapability;
10
+ history: HistoryCapability;
11
+ }
12
+ export declare function createNodeInDirection(deps: QuickCreateDeps, source: Shape, dir: PortDir, kind?: ShapeKind, text?: string): Shape;
@@ -0,0 +1,29 @@
1
+ import { compose } from "@silkor/core";
2
+ import { FlowEdge, OPPOSITE_DIR } from "./FlowEdge";
3
+ import { shapeData } from "./commands";
4
+ import { placeNodeInDirection } from "./geometry";
5
+ export function createNodeInDirection(deps, source, dir, kind = source.kind, text = "") {
6
+ const shape = deps.model.createShape(kind, {
7
+ width: source.width,
8
+ height: source.height,
9
+ text,
10
+ });
11
+ const pos = placeNodeInDirection(source, dir, shape);
12
+ shape.x = pos.x;
13
+ shape.y = pos.y;
14
+ const edge = new FlowEdge(source, shape, {
15
+ from: dir,
16
+ to: OPPOSITE_DIR[dir],
17
+ });
18
+ edge.router = deps.router;
19
+ // 不预先注册,避免 exec 重放时重复入列;undo 逆序先摘连线再摘节点
20
+ deps.history.exec(compose("add", {
21
+ redo: () => deps.model.registerShape(shape),
22
+ undo: () => deps.model.removeShape(shape),
23
+ toJSON: () => ({
24
+ kind: "flowchart.add-shape",
25
+ payload: { data: shapeData(shape) },
26
+ }),
27
+ }, deps.model.edgeAddRemove(edge)));
28
+ return shape;
29
+ }
@@ -0,0 +1,8 @@
1
+ import type { SelectionCapability } from "@silkor/core";
2
+ import { FlowEdge } from "./FlowEdge";
3
+ import { Shape } from "./Shape";
4
+ import type { Bounds } from "./snap";
5
+ export declare function selShape(sel: SelectionCapability<Shape | FlowEdge>): Shape | null;
6
+ export declare function selEdge(sel: SelectionCapability<Shape | FlowEdge>): FlowEdge | null;
7
+ export declare function selShapes(sel: SelectionCapability<Shape | FlowEdge>): Shape[];
8
+ export declare function multiBounds(sel: SelectionCapability<Shape | FlowEdge>): Bounds | null;
@@ -0,0 +1,31 @@
1
+ import { FlowEdge } from "./FlowEdge";
2
+ import { Shape } from "./Shape";
3
+ import { unionBounds } from "./geometry";
4
+ // 选中集合的派生读取(纯函数):单选形状 / 单选连线 / 选中形状列表 / 多选组合
5
+ // 包围盒。选中模型统一存放在 Selection 能力里,这里只做类型收窄与派生计算,
6
+ // 不持有状态
7
+ // 选中的形状(选中连线或无选中时为 null)
8
+ export function selShape(sel) {
9
+ const items = sel.items;
10
+ return items.length === 1 && items[0] instanceof Shape
11
+ ? items[0]
12
+ : null;
13
+ }
14
+ // 选中的连线(选中形状或无选中时为 null)
15
+ export function selEdge(sel) {
16
+ const items = sel.items;
17
+ return items.length === 1 && items[0] instanceof FlowEdge
18
+ ? items[0]
19
+ : null;
20
+ }
21
+ // 当前选中的形状列表(多选组合或单选形状;选中连线时为空)——单/多选共用一套命中与移动逻辑
22
+ export function selShapes(sel) {
23
+ return sel.items.filter((s) => s instanceof Shape);
24
+ }
25
+ // 多选组合的包围盒(少于 2 个形状时视为无组合)
26
+ export function multiBounds(sel) {
27
+ const shapes = selShapes(sel);
28
+ if (shapes.length < 2)
29
+ return null;
30
+ return unionBounds(shapes);
31
+ }