silkio-flowchart 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -2
  3. package/dist/FlowEdge.d.ts +45 -0
  4. package/dist/FlowEdge.js +390 -0
  5. package/dist/Shape.d.ts +48 -0
  6. package/dist/Shape.js +118 -0
  7. package/dist/ai-protocol.d.ts +146 -0
  8. package/dist/ai-protocol.js +579 -0
  9. package/dist/ai-report.d.ts +3 -0
  10. package/dist/ai-report.js +83 -0
  11. package/dist/ai.d.ts +6 -0
  12. package/dist/ai.js +7 -0
  13. package/dist/auto-layout.d.ts +15 -0
  14. package/dist/auto-layout.js +248 -0
  15. package/dist/commands.d.ts +106 -0
  16. package/dist/commands.js +264 -0
  17. package/dist/data.d.ts +24 -0
  18. package/dist/data.js +1 -0
  19. package/dist/edge-router.d.ts +24 -0
  20. package/dist/edge-router.js +22 -0
  21. package/dist/editing.d.ts +32 -0
  22. package/dist/editing.js +148 -0
  23. package/dist/flow-layout-capability.d.ts +7 -0
  24. package/dist/flow-layout-capability.js +2 -0
  25. package/dist/flowchart.d.ts +118 -0
  26. package/dist/flowchart.js +1446 -0
  27. package/dist/geometry.d.ts +98 -0
  28. package/dist/geometry.js +310 -0
  29. package/dist/history-codec.d.ts +47 -0
  30. package/dist/history-codec.js +410 -0
  31. package/dist/hover.d.ts +22 -0
  32. package/dist/hover.js +96 -0
  33. package/dist/index.d.ts +22 -0
  34. package/dist/index.js +18 -0
  35. package/dist/interactions.d.ts +51 -0
  36. package/dist/interactions.js +636 -0
  37. package/dist/model.d.ts +58 -0
  38. package/dist/model.js +233 -0
  39. package/dist/overlay.d.ts +60 -0
  40. package/dist/overlay.js +242 -0
  41. package/dist/quick-create.d.ts +12 -0
  42. package/dist/quick-create.js +29 -0
  43. package/dist/selection.d.ts +8 -0
  44. package/dist/selection.js +31 -0
  45. package/dist/session.d.ts +68 -0
  46. package/dist/session.js +4 -0
  47. package/dist/shape-registry.d.ts +17 -0
  48. package/dist/shape-registry.js +36 -0
  49. package/dist/shapes.d.ts +56 -0
  50. package/dist/shapes.js +291 -0
  51. package/dist/snap-capability.d.ts +23 -0
  52. package/dist/snap-capability.js +32 -0
  53. package/dist/snap.d.ts +34 -0
  54. package/dist/snap.js +370 -0
  55. package/package.json +36 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 silkio-flowchart contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,38 @@
1
- # Temporary Holding Version
1
+ # flowchart
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 基于 [silkor](../) 画布内核的 流程图 场景包,包名 `silkio-flowchart`。
4
+
5
+ ## 安装
6
+
7
+ ```sh
8
+ pnpm add silkio-flowchart
9
+ ```
10
+
11
+ `@silkor/core` 与 `@silkor/preset` 会作为依赖一并安装。
12
+
13
+ ## 使用
14
+
15
+ 便捷模式:场景内部通过 `@silkor/preset` 的 `officialPlugins()` 自动完成插件装配,开箱即用;也支持宿主自建 `App` 的显式装配。
16
+
17
+ 对外导出见 `src/index.ts`。`examples/demo-flowchart` 是可运行的 Astro + Vue 演示。
18
+
19
+ ## 开发
20
+
21
+ ```sh
22
+ pnpm install
23
+ pnpm check:deps # 边界守护:禁止直连插件、禁止依赖另一个场景仓
24
+ pnpm build
25
+ pnpm test
26
+ ```
27
+
28
+ `pnpm check:deps` 会把「场景包只依赖 core + preset」这条规则机械化。官方插件清单以 `@silkor/preset` 的 `officialPlugins()` 为单一事实来源,本仓不得直接依赖 `@silkor/plugin-*`。
29
+
30
+ > 依赖的 `@silkor/core` / `@silkor/preset` 固定从 npm 安装正式版本,不再链接本地源码。需要改内核时在 `silkor` 仓开发并发布,再升级本仓的依赖版本号。
31
+
32
+ ## 版本约定
33
+
34
+ 依赖的 `@silkor/core` / `@silkor/preset` 版本号必须与 `silkor` 仓当前发布的版本族一致。能力键常量在 core、实现在插件,版本错配会到 `app.require(...)` 运行时才暴露。
35
+
36
+ ## License
37
+
38
+ MIT
@@ -0,0 +1,45 @@
1
+ import { Edge, Text } from '@silkor/core';
2
+ import type { Bounds, EdgePoint } from '@silkor/core';
3
+ import type { EdgeRouterCapability } from './edge-router';
4
+ import type { Shape } from './Shape';
5
+ export type PortDir = 'top' | 'right' | 'bottom' | 'left';
6
+ export interface FlowEdgeHints {
7
+ from?: PortDir;
8
+ to?: PortDir;
9
+ }
10
+ export declare class FlowEdge extends Edge {
11
+ fromNode: Shape;
12
+ toNode: Shape;
13
+ fromDir: PortDir;
14
+ toDir: PortDir;
15
+ selected: boolean;
16
+ hovered: boolean;
17
+ points: EdgePoint[];
18
+ router: EdgeRouterCapability | null;
19
+ label: Text;
20
+ get text(): Text;
21
+ constructor(from: Shape, to: Shape, hints?: FlowEdgeHints);
22
+ reattachEnd(which: 'from' | 'to', node: Shape, dir?: PortDir): void;
23
+ update(): void;
24
+ private labelSynced;
25
+ layout(): void;
26
+ private positionLabel;
27
+ render(ctx: CanvasRenderingContext2D): void;
28
+ hitPoint(p: {
29
+ x: number;
30
+ y: number;
31
+ }, tolerance: number): boolean;
32
+ dragWhich: 'from' | 'to' | null;
33
+ dragPoint: EdgePoint;
34
+ dragDir: PortDir;
35
+ getBounds(): Bounds;
36
+ protected route(): EdgePoint[];
37
+ static routeBetween(s: EdgePoint, fromDir: PortDir, t: EdgePoint, toDir: PortDir, fromStub?: number, toStub?: number): EdgePoint[];
38
+ }
39
+ export declare function renderOrthoPolyline(ctx: CanvasRenderingContext2D, pts: EdgePoint[], color: string, lineWidth: number, cornerRadius: number): void;
40
+ export declare function drawEdgeArrow(ctx: CanvasRenderingContext2D, pts: EdgePoint[], color: string): void;
41
+ export declare function renderEdgePreview(ctx: CanvasRenderingContext2D, pts: EdgePoint[], color?: string): void;
42
+ export declare const PORT_DIR_VEC: Record<PortDir, EdgePoint>;
43
+ export declare const OPPOSITE_DIR: Record<PortDir, PortDir>;
44
+ export declare function trimEdgeEnd(pts: EdgePoint[]): EdgePoint[];
45
+ export declare const PORT_STUB = 24;
@@ -0,0 +1,390 @@
1
+ import { Edge, Text, pointsBounds } from '@silkor/core';
2
+ // 流程图连线:横平竖直的正交折线,拐点用小圆角过渡,末端绘制实心箭头。
3
+ // 路由规则:连线从源节点四向端口(上/右/下/左边中点)之一引出,接入目标节点四向端口之一;
4
+ // 两端端口在创建时固定(显式指定或按当时主导方向自动选边),此后节点移动/缩放
5
+ // 只改变连线途经的折点位置,不再改变出入口端口。
6
+ // 出口/入口段锁定端口方向:从端口沿外法向先引出一段固定长度直段
7
+ // (下方出口第一段必为向下竖线,上方入口最后一段必为竖线),再正交连接两端。
8
+ // 中段形态由两端端口方向组合与相对位置决定,均保证首段沿出口、末段沿入口且无折返:
9
+ // 同轴顺畅(右出左入等)为三段 Z 形;同侧出入或目标偏居一侧时取三段 L 形
10
+ // (如右出右入为右、上/下、左);仅面对面且目标在源后方时走五段绕行
11
+ // (如下出上入但目标在源上方:下、右、上、右、下);横竖混搭为 L 形,
12
+ // 转折顺序按出口延伸与进入衔接自动选择。
13
+ export class FlowEdge extends Edge {
14
+ fromNode;
15
+ toNode;
16
+ // 两端固定端口方向(创建时冻结,节点移动不再改变)
17
+ fromDir;
18
+ toDir;
19
+ // 选中态标志(由 FlowChart 维护;选中视觉——线色高亮——由渲染处理)
20
+ selected = false;
21
+ // 悬浮态标志:浅蓝色高亮(优先级低于选中)
22
+ hovered = false;
23
+ // 最近一次路由的折线点(渲染时更新,供命中检测使用)
24
+ points = [];
25
+ // 边路由能力(FlowChart 装配时注入,第三方路由插件经能力键覆盖):
26
+ // null 时回退到内置正交路由(本类的静态方法与渲染辅助),保持独立可用
27
+ router = null;
28
+ // 连线标签(内嵌 Text 子节点):双击连线进入编辑(复用文本编辑器能力),
29
+ // 渲染时定位在路由折线的中点,白底圆角盒遮住下方线体;空文本不显示
30
+ label;
31
+ // 文本载体(编辑会话与文本编辑器按 .text 统一取文本节点,与 Shape 同构):即标签
32
+ get text() {
33
+ return this.label;
34
+ }
35
+ constructor(from, to, hints = {}) {
36
+ super({ color: '#94a3b8', lineWidth: 2, cornerRadius: 8 });
37
+ this.fromNode = from;
38
+ this.toNode = to;
39
+ // 创建时冻结两端端口:未显式指定的端按当前中心连线主导方向选边,
40
+ // 之后节点位置变化只影响折线路径,不再改变出入口
41
+ const sc = from.center();
42
+ const tc = to.center();
43
+ const dx = tc.x - sc.x;
44
+ const dy = tc.y - sc.y;
45
+ const horizontal = Math.abs(dx) >= Math.abs(dy);
46
+ this.fromDir = hints.from ?? (horizontal ? (dx >= 0 ? 'right' : 'left') : (dy >= 0 ? 'bottom' : 'top'));
47
+ this.toDir = hints.to ?? (horizontal ? (dx >= 0 ? 'left' : 'right') : (dy >= 0 ? 'top' : 'bottom'));
48
+ // 标签:自动定宽文本(上限 200,超出自动换行),白底盒遮线;子节点随本节点
49
+ // 渲染(本节点变换恒为单位阵,子节点本地坐标即世界坐标)。空文本置 visible=false
50
+ // 不画白盒。widthRange 同步约束编辑覆盖层宽度(TextEditorView 按其设 min/max-width)
51
+ this.label = new Text({
52
+ text: '',
53
+ fill: '#475569',
54
+ fontSize: 12,
55
+ fontFamily: 'sans-serif',
56
+ align: 'center',
57
+ lineHeight: 1.4,
58
+ padding: [2, 6],
59
+ widthRange: [0, 200],
60
+ boxStyle: { fill: '#ffffff', radius: 4 },
61
+ });
62
+ this.label.visible = false;
63
+ this.add(this.label);
64
+ }
65
+ // 重接一端:更换该端节点并重选端口方向(dir 缺省时按与另一端的主导方向自动选边)
66
+ reattachEnd(which, node, dir) {
67
+ const other = which === 'from' ? this.toNode : this.fromNode;
68
+ const oc = other.center();
69
+ const nc = node.center();
70
+ // 主导方向以“源→目标”为正方向计算,which 为 from 时源是 node、目标是 other,反之亦然
71
+ const dx = which === 'from' ? oc.x - nc.x : nc.x - oc.x;
72
+ const dy = which === 'from' ? oc.y - nc.y : nc.y - oc.y;
73
+ const horizontal = Math.abs(dx) >= Math.abs(dy);
74
+ if (which === 'from') {
75
+ this.fromNode = node;
76
+ this.fromDir = dir ?? (horizontal ? (dx >= 0 ? 'right' : 'left') : (dy >= 0 ? 'bottom' : 'top'));
77
+ }
78
+ else {
79
+ this.toNode = node;
80
+ this.toDir = dir ?? (horizontal ? (dx >= 0 ? 'left' : 'right') : (dy >= 0 ? 'top' : 'bottom'));
81
+ }
82
+ }
83
+ // 渲染前重算路由(也刷新 from/to 供箭头取末端方向),并把标签定位到折线中点
84
+ update() {
85
+ const pts = this.route();
86
+ this.points = pts;
87
+ this.from = pts[0];
88
+ this.to = pts[pts.length - 1];
89
+ // 程序化设置 label.text(建模特载/AI 渲染/粘贴/历史重放)可能未经 layout():
90
+ // 渲染前按文本变化自愈重排,避免文字已改而标签仍隐藏(白盒靠 visible 门控)
91
+ if (this.labelSynced !== this.label.text) {
92
+ this.labelSynced = this.label.text;
93
+ this.layout();
94
+ }
95
+ else {
96
+ this.positionLabel();
97
+ }
98
+ }
99
+ // 上一次已完成重排的标签文本(自愈同步的游标)
100
+ labelSynced = '';
101
+ // 标签重排(文本变化后调用,与 Shape.layout 同语义):重测尺寸后按折线中点
102
+ // 居中定位;空文本(无可视字符)隐藏,避免白底空盒遮线
103
+ layout() {
104
+ this.label.measure();
105
+ this.positionLabel();
106
+ this.label.visible = /\S/.test(this.label.text);
107
+ }
108
+ // 把标签居中定位到折线弧长中点:沿线走行至总长一半处(label 为子节点,
109
+ // 本节点变换恒为单位阵,本地坐标即世界坐标)
110
+ positionLabel() {
111
+ const pts = this.points;
112
+ if (pts.length < 2)
113
+ return;
114
+ let total = 0;
115
+ for (let i = 1; i < pts.length; i++)
116
+ total += Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y);
117
+ let half = total / 2;
118
+ let p = pts[pts.length - 1];
119
+ for (let i = 1; i < pts.length; i++) {
120
+ const len = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y);
121
+ if (half <= len) {
122
+ const t = len === 0 ? 0 : half / len;
123
+ const a = pts[i - 1];
124
+ p = { x: a.x + (pts[i].x - a.x) * t, y: a.y + (pts[i].y - a.y) * t };
125
+ break;
126
+ }
127
+ half -= len;
128
+ }
129
+ this.label.x = p.x - this.label.width / 2;
130
+ this.label.y = p.y - this.label.height / 2;
131
+ }
132
+ render(ctx) {
133
+ this.update();
134
+ const pts = this.points;
135
+ // 拖拽端点期间不显示高亮,保持常态线色;选中 > 悬浮 > 默认
136
+ const color = this.selected && !this.dragWhich
137
+ ? SELECT_COLOR
138
+ : this.hovered && !this.selected
139
+ ? HOVER_COLOR
140
+ : this.color;
141
+ // 路由能力被覆盖时由实现绘制(含箭头等自带装饰);否则走内置正交渲染
142
+ if (this.router) {
143
+ this.router.render(ctx, pts, {
144
+ color,
145
+ lineWidth: this.lineWidth,
146
+ cornerRadius: this.cornerRadius,
147
+ });
148
+ return;
149
+ }
150
+ // 线体末端回缩一个箭头长度再绘制,避免线头从三角箭头下方露出
151
+ renderOrthoPolyline(ctx, trimEdgeEnd(pts), color, this.lineWidth, this.cornerRadius);
152
+ drawEdgeArrow(ctx, pts, color);
153
+ }
154
+ // 命中检测:点到折线各段的最近距离是否在容差内(世界坐标)
155
+ hitPoint(p, tolerance) {
156
+ const pts = this.points;
157
+ if (pts.length < 2)
158
+ return false;
159
+ for (let i = 1; i < pts.length; i++) {
160
+ if (distSeg(p, pts[i - 1], pts[i]) <= tolerance)
161
+ return true;
162
+ }
163
+ return false;
164
+ }
165
+ // 端点拖拽态:拖拽中的端以 dragPoint(光标或吸附端口)替代原端口参与路由,
166
+ // dragDir 为该端的虚拟方向(随光标位置变化);null 表示未在拖拽
167
+ dragWhich = null;
168
+ dragPoint = { x: 0, y: 0 };
169
+ dragDir = 'top';
170
+ // 世界包围盒:按当前路由折线计算(绕行路线的途经点不止首尾两端点),
171
+ // 箭头尖端即末端口点、线体末端回缩,半线宽外扩已足够覆盖
172
+ getBounds() {
173
+ return pointsBounds(this.route(), this.lineWidth / 2, this.getWorldTransform());
174
+ }
175
+ // 生成路径点(相邻重复点已剔除)。拖拽端点时该端以虚拟端口(dragPoint/dragDir,
176
+ // 直段取默认 PORT_STUB)参与路由;路由委托给注入的路由能力,未注入时走
177
+ // 内置正交路由。首段沿源端口外法向、末段沿目标端口外法向反向进入,方向锁定
178
+ route() {
179
+ let s;
180
+ let sd;
181
+ let ss;
182
+ let t;
183
+ let td;
184
+ let ts;
185
+ if (this.dragWhich === 'from') {
186
+ s = this.dragPoint;
187
+ sd = this.dragDir;
188
+ ss = PORT_STUB;
189
+ t = this.toNode.portAt(this.toDir);
190
+ td = this.toDir;
191
+ ts = this.toNode.portStub(this.toDir);
192
+ }
193
+ else if (this.dragWhich === 'to') {
194
+ s = this.fromNode.portAt(this.fromDir);
195
+ sd = this.fromDir;
196
+ ss = this.fromNode.portStub(this.fromDir);
197
+ t = this.dragPoint;
198
+ td = this.dragDir;
199
+ ts = PORT_STUB;
200
+ }
201
+ else {
202
+ s = this.fromNode.portAt(this.fromDir);
203
+ sd = this.fromDir;
204
+ ss = this.fromNode.portStub(this.fromDir);
205
+ t = this.toNode.portAt(this.toDir);
206
+ td = this.toDir;
207
+ ts = this.toNode.portStub(this.toDir);
208
+ }
209
+ return this.router
210
+ ? this.router.route({ from: s, fromDir: sd, to: t, toDir: td, fromStub: ss, toStub: ts })
211
+ : FlowEdge.routeBetween(s, sd, t, td, ss, ts);
212
+ }
213
+ // 两端口间的正交路由:实例连线与拖拽预览共用。
214
+ // 首段沿 fromDir 外法向引出 fromStub 直段、末段沿 toDir 外法向反向进入,
215
+ // 方向锁定;直段长度由两端节点的 portStub 决定(默认 PORT_STUB,
216
+ // 端口内收的形状覆写以把转折点推到包围盒外)
217
+ static routeBetween(s, fromDir, t, toDir, fromStub = PORT_STUB, toStub = PORT_STUB) {
218
+ const fv = PORT_DIR_VEC[fromDir];
219
+ const tv = PORT_DIR_VEC[toDir];
220
+ // 沿外法向引出固定长度直段,保证出口/入口段方向始终与端口方向一致
221
+ const s2 = { x: s.x + fv.x * fromStub, y: s.y + fv.y * fromStub };
222
+ const t2 = { x: t.x + tv.x * toStub, y: t.y + tv.y * toStub };
223
+ const fh = fv.x !== 0;
224
+ const th = tv.x !== 0;
225
+ if (fh && th) {
226
+ // 横出横入:右→左 / 左→右且整体前进方向一致时走竖向中转 Z 形;
227
+ // 否则优先三段 L 形(先沿出口方向横行再竖折,或先竖折再沿进入方向横行),
228
+ // 两侧 L 均不成立(如右出左入但目标在源后方)才走横向中转的五段绕行
229
+ const d = t2.x - s2.x;
230
+ if (d * fv.x >= 0 && d * -tv.x >= 0) {
231
+ const mx = (s2.x + t2.x) / 2;
232
+ return dedupe([s, s2, { x: mx, y: s2.y }, { x: mx, y: t2.y }, t2, t]);
233
+ }
234
+ if (d * fv.x >= 0) {
235
+ // 先横后竖:沿出口方向横行至目标 x,竖行对齐后水平进入(如右出右入:右、上/下、左)
236
+ return dedupe([s, s2, { x: t2.x, y: s2.y }, t2, t]);
237
+ }
238
+ if (d * -tv.x >= 0) {
239
+ // 先竖后横:竖行至进入高度,再沿进入方向水平进入
240
+ return dedupe([s, s2, { x: s2.x, y: t2.y }, t2, t]);
241
+ }
242
+ const my = (s2.y + t2.y) / 2;
243
+ return dedupe([s, s2, { x: s2.x, y: my }, { x: t2.x, y: my }, t2, t]);
244
+ }
245
+ if (!fh && !th) {
246
+ // 竖出竖入:下→上 / 上→下且整体前进方向一致时走横向中转 Z 形;
247
+ // 否则优先三段 L 形,均不成立(如下出上入但目标在源上方)才走竖向中转的
248
+ // 五段 S 形绕行(下、右、上、右、下),不产生折返锯齿
249
+ const d = t2.y - s2.y;
250
+ if (d * fv.y >= 0 && d * -tv.y >= 0) {
251
+ const my = (s2.y + t2.y) / 2;
252
+ return dedupe([s, s2, { x: s2.x, y: my }, { x: t2.x, y: my }, t2, t]);
253
+ }
254
+ if (d * fv.y >= 0) {
255
+ // 先竖后横:沿出口方向竖行至目标 y,横行对齐后竖直进入
256
+ return dedupe([s, s2, { x: s2.x, y: t2.y }, t2, t]);
257
+ }
258
+ if (d * -tv.y >= 0) {
259
+ // 先横后竖:横行至进入侧 x,再沿进入方向竖直进入
260
+ return dedupe([s, s2, { x: t2.x, y: s2.y }, t2, t]);
261
+ }
262
+ const mx = (s2.x + t2.x) / 2;
263
+ return dedupe([s, s2, { x: mx, y: s2.y }, { x: mx, y: t2.y }, t2, t]);
264
+ }
265
+ // 横竖混搭:L 形,转折顺序需同时满足——折线延续出口 stub 方向、
266
+ // 末段与进入方向同向衔接;一侧不满足时换另一侧(另一侧恒无折返),
267
+ // 例如下出左入但入口位高于出口位时为:下、右、上、右
268
+ if (fh) {
269
+ const ok = (t2.x - s2.x) * fv.x >= 0 && (t2.y - s2.y) * -tv.y >= 0;
270
+ return dedupe(ok
271
+ ? [s, s2, { x: t2.x, y: s2.y }, t2, t] // 先横后竖
272
+ : [s, s2, { x: s2.x, y: t2.y }, t2, t]); // 先竖后横(恒安全)
273
+ }
274
+ const ok = (t2.y - s2.y) * fv.y >= 0 && (t2.x - s2.x) * -tv.x >= 0;
275
+ return dedupe(ok
276
+ ? [s, s2, { x: s2.x, y: t2.y }, t2, t] // 先竖后横
277
+ : [s, s2, { x: t2.x, y: s2.y }, t2, t]); // 先横后竖(恒安全)
278
+ }
279
+ }
280
+ // 正交折线渲染:中间每个拐点用 arcTo 做四分之一圆弧(半径 cornerRadius,
281
+ // 自动收缩到相邻边长一半以内),其余为水平/垂直直线段
282
+ //(导出供官方边路由实现 BuiltinEdgeRouter 复用)
283
+ export function renderOrthoPolyline(ctx, pts, color, lineWidth, cornerRadius) {
284
+ const r = cornerRadius;
285
+ ctx.strokeStyle = color;
286
+ ctx.lineWidth = lineWidth;
287
+ ctx.beginPath();
288
+ ctx.moveTo(pts[0].x, pts[0].y);
289
+ for (let i = 1; i < pts.length - 1; i++) {
290
+ const prev = pts[i - 1];
291
+ const cur = pts[i];
292
+ const next = pts[i + 1];
293
+ const d1 = Math.hypot(cur.x - prev.x, cur.y - prev.y);
294
+ const d2 = Math.hypot(next.x - cur.x, next.y - cur.y);
295
+ if (d1 === 0 || d2 === 0) {
296
+ ctx.lineTo(cur.x, cur.y);
297
+ continue;
298
+ }
299
+ const rr = Math.min(r, d1 / 2, d2 / 2);
300
+ // 直线走到圆弧起点(沿 cur→prev 方向回退 rr),再经拐点圆弧切向接入下一段
301
+ ctx.lineTo(cur.x + ((prev.x - cur.x) / d1) * rr, cur.y + ((prev.y - cur.y) / d1) * rr);
302
+ ctx.arcTo(cur.x, cur.y, next.x, next.y, rr);
303
+ }
304
+ const last = pts[pts.length - 1];
305
+ ctx.lineTo(last.x, last.y);
306
+ ctx.stroke();
307
+ }
308
+ // 实心三角箭头:平移到终点并按最后一段直线方向旋转后绘制
309
+ //(导出供官方边路由实现 BuiltinEdgeRouter 复用)
310
+ export function drawEdgeArrow(ctx, pts, color) {
311
+ const to = pts[pts.length - 1];
312
+ const prev = pts[pts.length - 2];
313
+ if (prev.x === to.x && prev.y === to.y)
314
+ return;
315
+ ctx.save();
316
+ ctx.translate(to.x, to.y);
317
+ ctx.rotate(Math.atan2(to.y - prev.y, to.x - prev.x));
318
+ ctx.beginPath();
319
+ ctx.moveTo(0, 0);
320
+ ctx.lineTo(-ARROW_LEN, 4);
321
+ ctx.lineTo(-ARROW_LEN, -4);
322
+ ctx.closePath();
323
+ ctx.fillStyle = color;
324
+ ctx.fill();
325
+ ctx.restore();
326
+ }
327
+ // 拖拽预览渲染:与正式连线同款式的正交折线(末端回缩)+ 箭头
328
+ export function renderEdgePreview(ctx, pts, color = '#3b82f6') {
329
+ renderOrthoPolyline(ctx, trimEdgeEnd(pts), color, 2, 8);
330
+ drawEdgeArrow(ctx, pts, color);
331
+ }
332
+ // 选中态线色(与选中框同色系)
333
+ const SELECT_COLOR = '#3b82f6';
334
+ // 悬浮态线色(更浅的蓝,比选中淡、比默认亮)
335
+ const HOVER_COLOR = '#93c5fd';
336
+ // 箭头长度(三角沿行进方向的底边到尖端距离),线体末端按此回缩
337
+ const ARROW_LEN = 10;
338
+ // 端口外法向单位向量(top 向上、right 向右、bottom 向下、left 向左)
339
+ export const PORT_DIR_VEC = {
340
+ top: { x: 0, y: -1 },
341
+ right: { x: 1, y: 0 },
342
+ bottom: { x: 0, y: 1 },
343
+ left: { x: -1, y: 0 },
344
+ };
345
+ // 端口方向的相反方向
346
+ export const OPPOSITE_DIR = {
347
+ top: 'bottom',
348
+ bottom: 'top',
349
+ left: 'right',
350
+ right: 'left',
351
+ };
352
+ // 返回末端沿最后一段回缩 ARROW_LEN 的折线副本(回缩量不超过该段长度)
353
+ //(导出供官方边路由实现 BuiltinEdgeRouter 复用)
354
+ export function trimEdgeEnd(pts) {
355
+ const n = pts.length;
356
+ if (n < 2)
357
+ return pts;
358
+ const to = pts[n - 1];
359
+ const prev = pts[n - 2];
360
+ const dx = to.x - prev.x;
361
+ const dy = to.y - prev.y;
362
+ const len = Math.hypot(dx, dy);
363
+ if (len === 0)
364
+ return pts;
365
+ const back = Math.min(ARROW_LEN, len);
366
+ const out = pts.slice();
367
+ out[n - 1] = { x: to.x - (dx / len) * back, y: to.y - (dy / len) * back };
368
+ return out;
369
+ }
370
+ // 出口/入口直段长度(世界坐标):端口沿外法向引出的锁定方向段;
371
+ // 端口位于包围盒边上时转折点落在包围盒外该长度处(Shape.portStub 默认值)
372
+ export const PORT_STUB = 24;
373
+ // 点到线段的最短距离
374
+ function distSeg(p, a, b) {
375
+ const dx = b.x - a.x;
376
+ const dy = b.y - a.y;
377
+ const len2 = dx * dx + dy * dy;
378
+ const t = len2 === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2));
379
+ return Math.hypot(p.x - (a.x + dx * t), p.y - (a.y + dy * t));
380
+ }
381
+ // 剔除相邻重复点(锚点恰好落在转折中点上时会出现),避免零长度段干扰圆弧
382
+ function dedupe(pts) {
383
+ const out = [];
384
+ for (const p of pts) {
385
+ const q = out[out.length - 1];
386
+ if (!q || q.x !== p.x || q.y !== p.y)
387
+ out.push(p);
388
+ }
389
+ return out;
390
+ }
@@ -0,0 +1,48 @@
1
+ import { Node, Text } from '@silkor/core';
2
+ import type { NodeOptions } from '@silkor/core';
3
+ import type { ShapeKind } from './data';
4
+ export interface ShapeStyle {
5
+ fill: string;
6
+ stroke: string;
7
+ lineWidth: number;
8
+ color: string;
9
+ fontSize: number;
10
+ fontFamily: string;
11
+ }
12
+ export interface ShapeOptions extends NodeOptions {
13
+ text?: string;
14
+ style?: Partial<ShapeStyle>;
15
+ }
16
+ export declare abstract class Shape extends Node {
17
+ readonly kind: ShapeKind;
18
+ style: ShapeStyle;
19
+ text: Text;
20
+ selected: boolean;
21
+ constructor(kind: ShapeKind, defaults: {
22
+ width: number;
23
+ height: number;
24
+ }, options?: ShapeOptions);
25
+ layout(): void;
26
+ protected abstract boundaryT(dx: number, dy: number): number;
27
+ protected abstract get textFit(): number;
28
+ anchor(target: {
29
+ x: number;
30
+ y: number;
31
+ }): {
32
+ x: number;
33
+ y: number;
34
+ };
35
+ center(): {
36
+ x: number;
37
+ y: number;
38
+ };
39
+ portStub(_dir: 'top' | 'right' | 'bottom' | 'left'): number;
40
+ portAt(dir: 'top' | 'right' | 'bottom' | 'left'): {
41
+ x: number;
42
+ y: number;
43
+ };
44
+ hitTest(worldX: number, worldY: number): boolean;
45
+ protected abstract contains(x: number, y: number): boolean;
46
+ protected abstract drawPath(ctx: CanvasRenderingContext2D): void;
47
+ render(ctx: CanvasRenderingContext2D): void;
48
+ }
package/dist/Shape.js ADDED
@@ -0,0 +1,118 @@
1
+ import { Node, Text } from '@silkor/core';
2
+ import { PORT_STUB } from './FlowEdge';
3
+ const defaultStyle = {
4
+ fill: '#ffffff',
5
+ stroke: '#334155',
6
+ lineWidth: 1.5,
7
+ color: '#1f2937',
8
+ fontSize: 14,
9
+ fontFamily: 'sans-serif',
10
+ };
11
+ // 文本到形状边界的单侧留白(内容盒 = 形状宽度 - 左右留白)
12
+ const TEXT_PAD = 16;
13
+ // 多行文本行高
14
+ const TEXT_LINE_HEIGHT = 1.4;
15
+ // 流程图形状基类:容器 Node,内嵌居中 Text 子节点。
16
+ // 子类只负责四件事:drawPath(边界路径)、contains(命中判定)、
17
+ // boundaryT(中心到边界的方向系数)、textFit(文本盒内接换算系数),
18
+ // 锚点/端口/选中态/文字居中由基类统一处理。
19
+ // 形状尺寸完全由外部决定(默认值/拖拽/序列化),文本改动只重排居中,不改尺寸。
20
+ export class Shape extends Node {
21
+ kind;
22
+ style;
23
+ text;
24
+ // 选中态标志(由 FlowChart 维护;选中视觉——bounds 框/手柄/端口——由其覆盖层绘制)
25
+ selected = false;
26
+ constructor(kind, defaults, options = {}) {
27
+ super(options);
28
+ this.kind = kind;
29
+ this.style = { ...defaultStyle, ...options.style };
30
+ // 形状尺寸固定:未显式给宽高时套用该形状的默认尺寸(Node 默认的 autoWidth 关闭)
31
+ if (options.width === undefined) {
32
+ this.autoWidth = false;
33
+ this.width = defaults.width;
34
+ }
35
+ if (options.height === undefined) {
36
+ this.autoHeight = false;
37
+ this.height = defaults.height;
38
+ }
39
+ // 文本不自动定宽:换行宽度始终跟随形状宽度,打字不改节点尺寸;多行水平居中
40
+ this.text = new Text({
41
+ text: options.text ?? '',
42
+ fill: this.style.color,
43
+ fontSize: this.style.fontSize,
44
+ fontFamily: this.style.fontFamily,
45
+ wrap: true,
46
+ align: 'center',
47
+ lineHeight: TEXT_LINE_HEIGHT,
48
+ });
49
+ this.text.autoWidth = false;
50
+ this.add(this.text);
51
+ this.layout();
52
+ }
53
+ // 文本居中:换行宽度同步为形状内接内容盒宽度并重测,再居中(不改形状尺寸)
54
+ layout() {
55
+ this.text.width = Math.max(0, this.width / this.textFit - TEXT_PAD * 2);
56
+ this.text.measure();
57
+ this.text.x = Math.round((this.width - this.text.width) / 2);
58
+ this.text.y = Math.round((this.height - this.text.height) / 2);
59
+ }
60
+ // 朝目标点方向的边界锚点(世界坐标):中心连线与自身边界求交
61
+ anchor(target) {
62
+ const c = this.center();
63
+ const dx = target.x - c.x;
64
+ const dy = target.y - c.y;
65
+ if (dx === 0 && dy === 0)
66
+ return { x: c.x, y: c.y };
67
+ const t = this.boundaryT(dx, dy);
68
+ return { x: c.x + dx * t, y: c.y + dy * t };
69
+ }
70
+ // 世界坐标中心
71
+ center() {
72
+ const p = this.getWorldTransform().transformPoint({ x: this.width / 2, y: this.height / 2 });
73
+ return { x: p.x, y: p.y };
74
+ }
75
+ // 连线出口/入口直段长度:端口沿外法向引出的锁定方向段(世界坐标)。
76
+ // 默认端口位于包围盒边上,转折点落在包围盒外 PORT_STUB 处;端口被补偿
77
+ // 内收的形状(如平行四边形斜边中点)覆写 portAt 时需同时覆写此方法
78
+ // 加上内收量,保证转折点仍在包围盒外,后续正交段不穿过形状
79
+ portStub(_dir) {
80
+ return PORT_STUB;
81
+ }
82
+ // 四向端口(边界中点,世界坐标)
83
+ portAt(dir) {
84
+ const local = { top: { x: this.width / 2, y: 0 }, right: { x: this.width, y: this.height / 2 }, bottom: { x: this.width / 2, y: this.height }, left: { x: 0, y: this.height / 2 } }[dir];
85
+ const p = this.getWorldTransform().transformPoint(local);
86
+ return { x: p.x, y: p.y };
87
+ }
88
+ //#endregion
89
+ //#region 命中
90
+ // 按形状几何判定(菱形/椭圆的矩形角落在形状外),逆变换到本地坐标后交给子类
91
+ hitTest(worldX, worldY) {
92
+ const local = this.localTransform.inverse().transformPoint({ x: worldX, y: worldY });
93
+ return this.contains(local.x, local.y);
94
+ }
95
+ render(ctx) {
96
+ const { fill, stroke, lineWidth } = this.style;
97
+ ctx.beginPath();
98
+ this.drawPath(ctx);
99
+ if (fill) {
100
+ ctx.fillStyle = fill;
101
+ ctx.fill();
102
+ }
103
+ // border-box 语义:描边完全画在节点尺寸内部,宽度不改变节点整体大小
104
+ //(裁剪到形状路径后以 2 倍线宽描边,外半被裁掉,可见部分恰好是内侧 lineWidth)
105
+ if (stroke) {
106
+ ctx.save();
107
+ ctx.beginPath();
108
+ this.drawPath(ctx);
109
+ ctx.clip();
110
+ ctx.beginPath();
111
+ this.drawPath(ctx);
112
+ ctx.strokeStyle = stroke;
113
+ ctx.lineWidth = lineWidth * 2;
114
+ ctx.stroke();
115
+ ctx.restore();
116
+ }
117
+ }
118
+ }