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,68 @@
1
+ import type { FlowEdge, PortDir } from "./FlowEdge";
2
+ import type { ResizeHandle } from "./geometry";
3
+ import type { LinkPreview } from "./overlay";
4
+ import type { Shape } from "./Shape";
5
+ import type { Bounds } from "./snap";
6
+ export declare const CLICK_MOVE_TOLERANCE = 4;
7
+ export declare const CLICK_CREATE_GAP = 80;
8
+ export type Session = {
9
+ kind: "move";
10
+ shape: Shape;
11
+ start: {
12
+ x: number;
13
+ y: number;
14
+ };
15
+ origin: {
16
+ x: number;
17
+ y: number;
18
+ };
19
+ moved: boolean;
20
+ group: Array<{
21
+ shape: Shape;
22
+ x: number;
23
+ y: number;
24
+ }> | null;
25
+ keepMulti?: boolean;
26
+ press?: {
27
+ x: number;
28
+ y: number;
29
+ };
30
+ } | {
31
+ kind: "resize";
32
+ shape: Shape;
33
+ handle: ResizeHandle;
34
+ start: {
35
+ x: number;
36
+ y: number;
37
+ };
38
+ origin: Bounds;
39
+ } | {
40
+ kind: "group-resize";
41
+ handle: ResizeHandle;
42
+ start: {
43
+ x: number;
44
+ y: number;
45
+ };
46
+ origin: Bounds;
47
+ origins: Array<{
48
+ shape: Shape;
49
+ x: number;
50
+ y: number;
51
+ width: number;
52
+ height: number;
53
+ }>;
54
+ } | {
55
+ kind: "link";
56
+ source: Shape;
57
+ sourceDir: PortDir;
58
+ preview: LinkPreview;
59
+ start: {
60
+ x: number;
61
+ y: number;
62
+ };
63
+ armed?: boolean;
64
+ } | {
65
+ kind: "edge-move";
66
+ edge: FlowEdge;
67
+ which: "from" | "to";
68
+ };
@@ -0,0 +1,4 @@
1
+ // 端口点击快速创建:点击(非拖拽)端口时沿该方向自动新建节点并连线;
2
+ // 拖拽仍是手动连线,以按下后的位移区分两种意图
3
+ export const CLICK_MOVE_TOLERANCE = 4; // 判定点击的最大位移(屏幕像素)
4
+ export const CLICK_CREATE_GAP = 80; // 新节点与源节点边缘的间距(世界坐标)
@@ -0,0 +1,17 @@
1
+ import type { App, Plugin } from "@silkor/core";
2
+ import type { Shape, ShapeOptions } from "./Shape";
3
+ export interface ShapeDescriptor {
4
+ kind: string;
5
+ label?: string;
6
+ }
7
+ export interface ShapeRegistryCapability {
8
+ register(kind: string, factory: (options: ShapeOptions) => Shape, meta?: {
9
+ label?: string;
10
+ }): void;
11
+ list(): readonly ShapeDescriptor[];
12
+ create(kind: string, options?: ShapeOptions): Shape | undefined;
13
+ }
14
+ export declare const SHAPE_REGISTRY_CAPABILITY: import("@silkor/core").CapabilityKey<ShapeRegistryCapability>;
15
+ export declare class BuiltinShapes implements Plugin {
16
+ install(app: App): void;
17
+ }
@@ -0,0 +1,36 @@
1
+ import { capabilityKey } from "@silkor/core";
2
+ import { RectShape, Diamond, EllipseShape, Parallelogram, Hexagon, Database, } from "./shapes";
3
+ // 能力键:形状注册表供消费方查询(消费方只认能力名与契约,不认提供方插件)
4
+ export const SHAPE_REGISTRY_CAPABILITY = capabilityKey("flowchart.shape-registry");
5
+ // 注册表实现:Map 保持插入序,list/create 直接映射
6
+ class ShapeRegistry {
7
+ entries = new Map();
8
+ register(kind, factory, meta) {
9
+ this.entries.set(kind, { factory, label: meta?.label });
10
+ }
11
+ list() {
12
+ return [...this.entries].map(([kind, e]) => ({ kind, label: e.label }));
13
+ }
14
+ create(kind, options) {
15
+ const e = this.entries.get(kind);
16
+ return e?.factory(options ?? {});
17
+ }
18
+ }
19
+ // 官方形状插件:内置六种流程图形状,install 时自注册注册表能力。
20
+ // 第三方形状插件按能力键消费同一注册表追加/覆盖条目,不 import 本插件类型
21
+ export class BuiltinShapes {
22
+ install(app) {
23
+ const registry = new ShapeRegistry();
24
+ registry.register("rect", (o) => new RectShape(o), { label: "矩形" });
25
+ registry.register("diamond", (o) => new Diamond(o), { label: "菱形" });
26
+ registry.register("ellipse", (o) => new EllipseShape(o), { label: "椭圆" });
27
+ registry.register("parallelogram", (o) => new Parallelogram(o), {
28
+ label: "平行四边形",
29
+ });
30
+ registry.register("hexagon", (o) => new Hexagon(o), { label: "六边形" });
31
+ registry.register("database", (o) => new Database(o), { label: "数据库" });
32
+ app.provide(SHAPE_REGISTRY_CAPABILITY, registry, {
33
+ provider: "flowchart.builtin-shapes",
34
+ });
35
+ }
36
+ }
@@ -0,0 +1,56 @@
1
+ import { Shape } from "./Shape";
2
+ import type { ShapeOptions } from "./Shape";
3
+ export declare class RectShape extends Shape {
4
+ radius: number;
5
+ protected get textFit(): number;
6
+ constructor(options?: ShapeOptions);
7
+ protected boundaryT(dx: number, dy: number): number;
8
+ protected drawPath(ctx: CanvasRenderingContext2D): void;
9
+ protected contains(x: number, y: number): boolean;
10
+ }
11
+ export declare class Diamond extends Shape {
12
+ protected get textFit(): number;
13
+ constructor(options?: ShapeOptions);
14
+ protected boundaryT(dx: number, dy: number): number;
15
+ protected drawPath(ctx: CanvasRenderingContext2D): void;
16
+ protected contains(x: number, y: number): boolean;
17
+ }
18
+ export declare class EllipseShape extends Shape {
19
+ protected get textFit(): number;
20
+ constructor(options?: ShapeOptions);
21
+ protected boundaryT(dx: number, dy: number): number;
22
+ protected drawPath(ctx: CanvasRenderingContext2D): void;
23
+ protected contains(x: number, y: number): boolean;
24
+ }
25
+ export declare class Parallelogram extends Shape {
26
+ constructor(options?: ShapeOptions);
27
+ private get skew();
28
+ private get poly();
29
+ protected get textFit(): number;
30
+ protected boundaryT(dx: number, dy: number): number;
31
+ protected contains(x: number, y: number): boolean;
32
+ portAt(dir: "top" | "right" | "bottom" | "left"): {
33
+ x: number;
34
+ y: number;
35
+ };
36
+ portStub(dir: "top" | "right" | "bottom" | "left"): number;
37
+ protected drawPath(ctx: CanvasRenderingContext2D): void;
38
+ }
39
+ export declare class Hexagon extends Shape {
40
+ constructor(options?: ShapeOptions);
41
+ private get cut();
42
+ private get poly();
43
+ protected get textFit(): number;
44
+ protected boundaryT(dx: number, dy: number): number;
45
+ protected contains(x: number, y: number): boolean;
46
+ protected drawPath(ctx: CanvasRenderingContext2D): void;
47
+ }
48
+ export declare class Database extends Shape {
49
+ constructor(options?: ShapeOptions);
50
+ private get capRy();
51
+ protected get textFit(): number;
52
+ protected boundaryT(dx: number, dy: number): number;
53
+ protected contains(x: number, y: number): boolean;
54
+ protected drawPath(ctx: CanvasRenderingContext2D): void;
55
+ render(ctx: CanvasRenderingContext2D): void;
56
+ }
package/dist/shapes.js ADDED
@@ -0,0 +1,291 @@
1
+ import { Shape } from "./Shape";
2
+ import { PORT_STUB } from "./FlowEdge";
3
+ // 形状几何(路径构建/命中判定)复用 core 的绘制原语,flowchart 只保留领域逻辑
4
+ // (锚点 boundaryT、端口 portAt、文本内接 textFit);core 的 Parallelogram 与
5
+ // 本包同名类冲突,按别名引入
6
+ import { Rect, Ellipse, Parallelogram as CoreParallelogram } from "@silkor/core";
7
+ // 圆角矩形(流程步骤):默认矩形命中
8
+ export class RectShape extends Shape {
9
+ radius = 8;
10
+ // 矩形本身即最大内接矩形
11
+ get textFit() {
12
+ return 1;
13
+ }
14
+ constructor(options = {}) {
15
+ super("rect", { width: 160, height: 99 }, options);
16
+ }
17
+ boundaryT(dx, dy) {
18
+ // 射线与矩形边界求交:取两个轴向比例中的较小者(轴向分量为 0 时记无穷大)
19
+ const tx = dx === 0 ? Infinity : this.width / 2 / Math.abs(dx);
20
+ const ty = dy === 0 ? Infinity : this.height / 2 / Math.abs(dy);
21
+ return Math.min(tx, ty);
22
+ }
23
+ drawPath(ctx) {
24
+ Rect.buildPath(ctx, this.width, this.height, this.radius);
25
+ }
26
+ contains(x, y) {
27
+ return x >= 0 && x <= this.width && y >= 0 && y <= this.height;
28
+ }
29
+ }
30
+ // 菱形(判断分支)
31
+ export class Diamond extends Shape {
32
+ // 文本盒略宽于最大居中内接矩形(1/2 倍宽高),换取更多横向空间
33
+ get textFit() {
34
+ return 1.5;
35
+ }
36
+ constructor(options = {}) {
37
+ super("diamond", { width: 160, height: 99 }, options);
38
+ }
39
+ // 菱形方程 |dx|/a + |dy|/b = 1,方向系数为该式的倒数
40
+ boundaryT(dx, dy) {
41
+ const a = this.width / 2;
42
+ const b = this.height / 2;
43
+ return 1 / (Math.abs(dx) / a + Math.abs(dy) / b);
44
+ }
45
+ drawPath(ctx) {
46
+ ctx.moveTo(this.width / 2, 0);
47
+ ctx.lineTo(this.width, this.height / 2);
48
+ ctx.lineTo(this.width / 2, this.height);
49
+ ctx.lineTo(0, this.height / 2);
50
+ ctx.closePath();
51
+ }
52
+ contains(x, y) {
53
+ const a = this.width / 2;
54
+ const b = this.height / 2;
55
+ return Math.abs(x - a) / a + Math.abs(y - b) / b <= 1;
56
+ }
57
+ }
58
+ // 椭圆(开始 / 结束)
59
+ export class EllipseShape extends Shape {
60
+ // 椭圆内最大居中内接矩形为 1/√2 倍宽高,取倒数
61
+ get textFit() {
62
+ return Math.SQRT2;
63
+ }
64
+ constructor(options = {}) {
65
+ super("ellipse", { width: 160, height: 99 }, options);
66
+ }
67
+ // 椭圆方程 (dx/a)² + (dy/b)² = 1,方向系数为该式的 -1/2 次幂
68
+ boundaryT(dx, dy) {
69
+ const a = this.width / 2;
70
+ const b = this.height / 2;
71
+ return 1 / Math.sqrt((dx / a) ** 2 + (dy / b) ** 2);
72
+ }
73
+ drawPath(ctx) {
74
+ Ellipse.buildPath(ctx, this.width, this.height);
75
+ }
76
+ contains(x, y) {
77
+ return Ellipse.containsPoint(this.width, this.height, x, y);
78
+ }
79
+ }
80
+ // 凸多边形几何工具:顶点按顺序给出(本地坐标),返回中心射线 (dx, dy) 与
81
+ // 边界的比例 t(中心 + t·方向),无交返回 Infinity(射线方向为零向量时)
82
+ function polygonBoundaryT(poly, cx, cy, dx, dy) {
83
+ let best = Infinity;
84
+ for (let i = 0; i < poly.length; i++) {
85
+ const a = poly[i];
86
+ const b = poly[(i + 1) % poly.length];
87
+ const ex = b.x - a.x;
88
+ const ey = b.y - a.y;
89
+ // 射线 C + t·D 与边 A + s·E 联立求解(克莱姆法则)
90
+ const den = dx * ey - dy * ex;
91
+ if (den === 0)
92
+ continue;
93
+ const px = a.x - cx;
94
+ const py = a.y - cy;
95
+ const t = (px * ey - py * ex) / den;
96
+ const s = (dy * px - dx * py) / den;
97
+ if (t > 0 && s >= 0 && s <= 1 && t < best)
98
+ best = t;
99
+ }
100
+ return best;
101
+ }
102
+ // 平行四边形(输入 / 输出):上边右移、下边左移的斜切四边形
103
+ export class Parallelogram extends Shape {
104
+ constructor(options = {}) {
105
+ super("parallelogram", { width: 160, height: 99 }, options);
106
+ }
107
+ // 斜切量:高度的 30%
108
+ get skew() {
109
+ return this.height * 0.3;
110
+ }
111
+ // 顺时针顶点
112
+ get poly() {
113
+ const k = this.skew;
114
+ const { width: w, height: h } = this;
115
+ return [
116
+ { x: k, y: 0 },
117
+ { x: w, y: 0 },
118
+ { x: w - k, y: h },
119
+ { x: 0, y: h },
120
+ ];
121
+ }
122
+ // 内接矩形宽 ≈ w − 2·skew
123
+ get textFit() {
124
+ return 1.4;
125
+ }
126
+ boundaryT(dx, dy) {
127
+ const t = polygonBoundaryT(this.poly, this.width / 2, this.height / 2, dx, dy);
128
+ return t === Infinity ? 0 : t;
129
+ }
130
+ // 凸多边形内含判定:沿中心 → 点方向的边界比例 t ≥ 1 即在边界内(含边界)
131
+ contains(x, y) {
132
+ const cx = this.width / 2;
133
+ const cy = this.height / 2;
134
+ if (x === cx && y === cy)
135
+ return true;
136
+ return polygonBoundaryT(this.poly, cx, cy, x - cx, y - cy) >= 1;
137
+ }
138
+ // 左右端口补偿到斜边中点(左斜边 x = k/2、右斜边 x = w − k/2),
139
+ // 使连线端点与对接手柄落在可见边界上;上下端口仍在上下边中点
140
+ portAt(dir) {
141
+ const k = this.skew;
142
+ const local = {
143
+ top: { x: this.width / 2, y: 0 },
144
+ right: { x: this.width - k / 2, y: this.height / 2 },
145
+ bottom: { x: this.width / 2, y: this.height },
146
+ left: { x: k / 2, y: this.height / 2 },
147
+ }[dir];
148
+ const p = this.getWorldTransform().transformPoint(local);
149
+ return { x: p.x, y: p.y };
150
+ }
151
+ // 左右端口内收 k/2,直段加上内收量使转折点仍落在包围盒外 PORT_STUB 处,
152
+ // 后续正交段(竖行)不会穿过斜边;上下端口在包围盒边上,用默认值
153
+ portStub(dir) {
154
+ return dir === "left" || dir === "right"
155
+ ? PORT_STUB + this.skew / 2
156
+ : PORT_STUB;
157
+ }
158
+ drawPath(ctx) {
159
+ CoreParallelogram.buildPath(ctx, this.width, this.height, this.skew);
160
+ }
161
+ }
162
+ // 六边形(准备):左右两侧斜切的等边六边形
163
+ export class Hexagon extends Shape {
164
+ constructor(options = {}) {
165
+ super("hexagon", { width: 160, height: 99 }, options);
166
+ }
167
+ // 左右斜切量:宽度的 15%
168
+ get cut() {
169
+ return this.width * 0.15;
170
+ }
171
+ get poly() {
172
+ const n = this.cut;
173
+ const { width: w, height: h } = this;
174
+ return [
175
+ { x: n, y: 0 },
176
+ { x: w - n, y: 0 },
177
+ { x: w, y: h / 2 },
178
+ { x: w - n, y: h },
179
+ { x: n, y: h },
180
+ { x: 0, y: h / 2 },
181
+ ];
182
+ }
183
+ // 内接矩形宽 ≈ w − 2·cut
184
+ get textFit() {
185
+ return 1.35;
186
+ }
187
+ boundaryT(dx, dy) {
188
+ const t = polygonBoundaryT(this.poly, this.width / 2, this.height / 2, dx, dy);
189
+ return t === Infinity ? 0 : t;
190
+ }
191
+ contains(x, y) {
192
+ const cx = this.width / 2;
193
+ const cy = this.height / 2;
194
+ if (x === cx && y === cy)
195
+ return true;
196
+ return polygonBoundaryT(this.poly, cx, cy, x - cx, y - cy) >= 1;
197
+ }
198
+ drawPath(ctx) {
199
+ const [a, b, c, d, e, f] = this.poly;
200
+ ctx.moveTo(a.x, a.y);
201
+ ctx.lineTo(b.x, b.y);
202
+ ctx.lineTo(c.x, c.y);
203
+ ctx.lineTo(d.x, d.y);
204
+ ctx.lineTo(e.x, e.y);
205
+ ctx.lineTo(f.x, f.y);
206
+ ctx.closePath();
207
+ }
208
+ }
209
+ // 数据库(存储):圆柱体,顶帽完整椭圆 + 侧边 + 底帽半椭圆
210
+ export class Database extends Shape {
211
+ constructor(options = {}) {
212
+ super("database", { width: 160, height: 99 }, options);
213
+ }
214
+ // 顶/底帽椭圆的纵向半径:高度的 15%
215
+ get capRy() {
216
+ return this.height * 0.15;
217
+ }
218
+ // 文本落在中段柱体(全宽),仅多行延伸到帽区时略收窄
219
+ get textFit() {
220
+ return 1.15;
221
+ }
222
+ // 侧边(中段有效)与顶/底帽椭圆的射线求交取最小正 t;
223
+ // 帽椭圆解二次方程:t²·A + t·B + C = 0
224
+ boundaryT(dx, dy) {
225
+ const { width: w, height: h } = this;
226
+ const a = w / 2;
227
+ const ry = this.capRy;
228
+ const cx = a;
229
+ const cy = h / 2;
230
+ let best = Infinity;
231
+ // 左右侧边:x = 0 / w,且交点纵坐标落在中段
232
+ if (dx !== 0) {
233
+ const t = (dx > 0 ? w - cx : -cx) / dx;
234
+ const y = cy + t * dy;
235
+ if (t > 0 && y >= ry && y <= h - ry)
236
+ best = t;
237
+ }
238
+ // 顶/底帽:射线起点相对帽心的纵偏移 oy,代入椭圆方程
239
+ for (const ey of [ry, h - ry]) {
240
+ const oy = cy - ey;
241
+ const A = (dx * dx) / (a * a) + (dy * dy) / (ry * ry);
242
+ const B = (2 * oy * dy) / (ry * ry);
243
+ const C = (oy * oy) / (ry * ry) - 1;
244
+ const disc = B * B - 4 * A * C;
245
+ if (disc < 0)
246
+ continue;
247
+ const sq = Math.sqrt(disc);
248
+ for (const t of [(-B - sq) / (2 * A), (-B + sq) / (2 * A)]) {
249
+ if (t > 0 && t < best)
250
+ best = t;
251
+ }
252
+ }
253
+ return best === Infinity ? 0 : best;
254
+ }
255
+ // 中段柱体全宽有效;帽区按对应椭圆方程判定
256
+ contains(x, y) {
257
+ const { width: w, height: h } = this;
258
+ const ry = this.capRy;
259
+ if (x < 0 || x > w)
260
+ return false;
261
+ if (y >= ry && y <= h - ry)
262
+ return true;
263
+ const cy = y < ry ? ry : h - ry;
264
+ const nx = (x - w / 2) / (w / 2);
265
+ const ny = (y - cy) / ry;
266
+ return nx * nx + ny * ny <= 1;
267
+ }
268
+ drawPath(ctx) {
269
+ const { width: w, height: h } = this;
270
+ const ry = this.capRy;
271
+ ctx.moveTo(0, ry);
272
+ ctx.lineTo(0, h - ry);
273
+ // 底帽:经最低点的半椭圆(左 → 下 → 右)
274
+ ctx.ellipse(w / 2, h - ry, w / 2, ry, 0, Math.PI, 0, true);
275
+ ctx.lineTo(w, ry);
276
+ // 顶帽:经最高点的半椭圆(右 → 上 → 左)闭合轮廓
277
+ ctx.ellipse(w / 2, ry, w / 2, ry, 0, 0, Math.PI, true);
278
+ }
279
+ // 轮廓之外补画顶帽椭圆的前缘弧(下半弧):圆柱顶面的透视边缘,体现体积感
280
+ render(ctx) {
281
+ super.render(ctx);
282
+ const { width: w } = this;
283
+ const ry = this.capRy;
284
+ ctx.beginPath();
285
+ // 角度 0 → π(画布 y 轴向下):经最低点的下半弧,左端与轮廓顶弧起点重合
286
+ ctx.ellipse(w / 2, ry, w / 2 - this.style.lineWidth / 2, ry, 0, 0, Math.PI, false);
287
+ ctx.strokeStyle = this.style.stroke;
288
+ ctx.lineWidth = this.style.lineWidth;
289
+ ctx.stroke();
290
+ }
291
+ }
@@ -0,0 +1,23 @@
1
+ import type { App, Plugin } from "@silkor/core";
2
+ import type { Bounds, SnapResult } from "./snap";
3
+ export interface SnapInput {
4
+ box: Bounds;
5
+ candidates: readonly Bounds[];
6
+ scale: number;
7
+ prev?: {
8
+ x: boolean;
9
+ y: boolean;
10
+ };
11
+ }
12
+ export interface SnapCapability {
13
+ snap(input: SnapInput): SnapResult;
14
+ }
15
+ export declare const SNAP_CAPABILITY: import("@silkor/core").CapabilityKey<SnapCapability>;
16
+ export declare class BuiltinSnap implements Plugin {
17
+ private options?;
18
+ constructor(options?: {
19
+ snapTol?: number;
20
+ spacingTol?: number;
21
+ } | undefined);
22
+ install(app: App): void;
23
+ }
@@ -0,0 +1,32 @@
1
+ import { capabilityKey } from "@silkor/core";
2
+ import { computeSnap } from "./snap";
3
+ // 能力键:消费方只认能力名与契约,不认提供方插件
4
+ export const SNAP_CAPABILITY = capabilityKey("flowchart.snap");
5
+ // 官方吸附容差(屏幕像素):对齐吸附阈值 / 等间距吸附阈值
6
+ const DEFAULT_SNAP_TOL = 6;
7
+ const DEFAULT_SPACING_TOL = 4;
8
+ // 官方吸附插件:对齐吸附 + 等间距吸附(算法在 snap.ts 纯函数 computeSnap),
9
+ // install 时自注册 flowchart.snap 能力,容差可按需覆盖
10
+ export class BuiltinSnap {
11
+ options;
12
+ constructor(options) {
13
+ this.options = options;
14
+ }
15
+ install(app) {
16
+ const snapTol = this.options?.snapTol ?? DEFAULT_SNAP_TOL;
17
+ const spacingTol = this.options?.spacingTol ?? DEFAULT_SPACING_TOL;
18
+ const impl = {
19
+ snap: ({ box, candidates, scale, prev }) => {
20
+ const opts = {
21
+ snapTol: snapTol / scale,
22
+ spacingTol: spacingTol / scale,
23
+ prev,
24
+ };
25
+ return computeSnap(box, candidates, opts);
26
+ },
27
+ };
28
+ app.provide(SNAP_CAPABILITY, impl, {
29
+ provider: "flowchart.builtin-snap",
30
+ });
31
+ }
32
+ }
package/dist/snap.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ export type Bounds = {
2
+ x: number;
3
+ y: number;
4
+ width: number;
5
+ height: number;
6
+ };
7
+ export interface SpacingGuide {
8
+ x1: number;
9
+ y1: number;
10
+ x2: number;
11
+ y2: number;
12
+ value: number;
13
+ }
14
+ export interface SnapGuides {
15
+ v: number[];
16
+ h: number[];
17
+ spacing: SpacingGuide[];
18
+ }
19
+ export interface SnapOptions {
20
+ snapTol: number;
21
+ spacingTol: number;
22
+ prev?: {
23
+ x: boolean;
24
+ y: boolean;
25
+ };
26
+ }
27
+ export interface SnapResult {
28
+ dx: number;
29
+ dy: number;
30
+ guides: SnapGuides | null;
31
+ snappedX: boolean;
32
+ snappedY: boolean;
33
+ }
34
+ export declare function computeSnap(box: Bounds, candidates: readonly Bounds[], opts: SnapOptions): SnapResult;