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.
- package/LICENSE +21 -0
- package/README.md +37 -2
- package/dist/FlowEdge.d.ts +45 -0
- package/dist/FlowEdge.js +390 -0
- package/dist/Shape.d.ts +48 -0
- package/dist/Shape.js +118 -0
- package/dist/ai-protocol.d.ts +146 -0
- package/dist/ai-protocol.js +579 -0
- package/dist/ai-report.d.ts +3 -0
- package/dist/ai-report.js +83 -0
- package/dist/ai.d.ts +6 -0
- package/dist/ai.js +7 -0
- package/dist/auto-layout.d.ts +15 -0
- package/dist/auto-layout.js +248 -0
- package/dist/commands.d.ts +106 -0
- package/dist/commands.js +264 -0
- package/dist/data.d.ts +24 -0
- package/dist/data.js +1 -0
- package/dist/edge-router.d.ts +24 -0
- package/dist/edge-router.js +22 -0
- package/dist/editing.d.ts +32 -0
- package/dist/editing.js +148 -0
- package/dist/flow-layout-capability.d.ts +7 -0
- package/dist/flow-layout-capability.js +2 -0
- package/dist/flowchart.d.ts +118 -0
- package/dist/flowchart.js +1446 -0
- package/dist/geometry.d.ts +98 -0
- package/dist/geometry.js +310 -0
- package/dist/history-codec.d.ts +47 -0
- package/dist/history-codec.js +410 -0
- package/dist/hover.d.ts +22 -0
- package/dist/hover.js +96 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +18 -0
- package/dist/interactions.d.ts +51 -0
- package/dist/interactions.js +636 -0
- package/dist/model.d.ts +58 -0
- package/dist/model.js +233 -0
- package/dist/overlay.d.ts +60 -0
- package/dist/overlay.js +242 -0
- package/dist/quick-create.d.ts +12 -0
- package/dist/quick-create.js +29 -0
- package/dist/selection.d.ts +8 -0
- package/dist/selection.js +31 -0
- package/dist/session.d.ts +68 -0
- package/dist/session.js +4 -0
- package/dist/shape-registry.d.ts +17 -0
- package/dist/shape-registry.js +36 -0
- package/dist/shapes.d.ts +56 -0
- package/dist/shapes.js +291 -0
- package/dist/snap-capability.d.ts +23 -0
- package/dist/snap-capability.js +32 -0
- package/dist/snap.d.ts +34 -0
- package/dist/snap.js +370 -0
- package/package.json +36 -3
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Node } from "@silkor/core";
|
|
2
|
+
import { Shape } from "./Shape";
|
|
3
|
+
import type { FlowEdge, PortDir } from "./FlowEdge";
|
|
4
|
+
import type { Bounds } from "./snap";
|
|
5
|
+
export declare const PORT_HIT_RADIUS = 10;
|
|
6
|
+
export declare const HANDLE_HIT = 8;
|
|
7
|
+
export declare const EDGE_TOLERANCE = 5;
|
|
8
|
+
export declare const EDGE_HANDLE_HIT = 8;
|
|
9
|
+
export declare const MIN_SIZE = 20;
|
|
10
|
+
export declare const PORT_RADIUS = 5;
|
|
11
|
+
export declare const PORT_OFFSET = 24;
|
|
12
|
+
export declare const PORT_HOVER_SCALE = 2.4;
|
|
13
|
+
export declare const LINK_SNAP_MARGIN = 16;
|
|
14
|
+
export type ResizeHandle = "nw" | "n" | "ne" | "e" | "se" | "s" | "sw" | "w";
|
|
15
|
+
export declare function hitShape(children: readonly Node[], p: {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
}): Shape | null;
|
|
19
|
+
export declare function portPos(shape: Shape, dir: "top" | "right" | "bottom" | "left", scale: number): {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
};
|
|
23
|
+
export declare function portHit(shape: Shape, p: {
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
}, scale: number): "top" | "right" | "bottom" | "left" | null;
|
|
27
|
+
export declare function cornerHit(b: Bounds, p: {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
}, scale: number): "nw" | "ne" | "se" | "sw" | null;
|
|
31
|
+
export declare function edgeHit(b: Bounds, p: {
|
|
32
|
+
x: number;
|
|
33
|
+
y: number;
|
|
34
|
+
}, scale: number): "n" | "e" | "s" | "w" | null;
|
|
35
|
+
export declare function hitEdge(edges: FlowEdge[], p: {
|
|
36
|
+
x: number;
|
|
37
|
+
y: number;
|
|
38
|
+
}, scale: number): FlowEdge | null;
|
|
39
|
+
export declare function edgeHandleHit(edge: FlowEdge, p: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
}, scale: number): "from" | "to" | null;
|
|
43
|
+
export declare function hoverCursor(shape: Shape, p: {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
}, scale: number): string | null;
|
|
47
|
+
export declare function previewToDir(exitDir: PortDir, exitPort: {
|
|
48
|
+
x: number;
|
|
49
|
+
y: number;
|
|
50
|
+
}, p: {
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
}): PortDir;
|
|
54
|
+
export declare function unionBounds(shapes: Array<{
|
|
55
|
+
x: number;
|
|
56
|
+
y: number;
|
|
57
|
+
width: number;
|
|
58
|
+
height: number;
|
|
59
|
+
}>): Bounds;
|
|
60
|
+
export declare function marqueeHits(shapes: Iterable<Shape>, r: Bounds): Shape[];
|
|
61
|
+
export declare function findLinkTarget(shapes: Iterable<Shape>, p: {
|
|
62
|
+
x: number;
|
|
63
|
+
y: number;
|
|
64
|
+
}, scale: number): {
|
|
65
|
+
target: Shape;
|
|
66
|
+
snap: PortDir;
|
|
67
|
+
} | null;
|
|
68
|
+
export declare function applyResize(shape: Shape, handle: ResizeHandle, origin: Bounds, dx: number, dy: number): void;
|
|
69
|
+
export declare function applyGroupResize(handle: ResizeHandle, origin: Bounds, origins: Array<{
|
|
70
|
+
shape: Shape;
|
|
71
|
+
x: number;
|
|
72
|
+
y: number;
|
|
73
|
+
width: number;
|
|
74
|
+
height: number;
|
|
75
|
+
}>, dx: number, dy: number): void;
|
|
76
|
+
export declare function placeNodeInDirection(source: {
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
width: number;
|
|
80
|
+
height: number;
|
|
81
|
+
}, dir: PortDir, size: {
|
|
82
|
+
width: number;
|
|
83
|
+
height: number;
|
|
84
|
+
}): {
|
|
85
|
+
x: number;
|
|
86
|
+
y: number;
|
|
87
|
+
};
|
|
88
|
+
export declare function dominantPortDir(from: {
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
width: number;
|
|
92
|
+
height: number;
|
|
93
|
+
}, to: {
|
|
94
|
+
x: number;
|
|
95
|
+
y: number;
|
|
96
|
+
width: number;
|
|
97
|
+
height: number;
|
|
98
|
+
}): PortDir;
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// 交互几何辅助(纯函数):命中测试、端口定位、缩放几何、框选命中与连线吸附
|
|
2
|
+
// 目标查找——不持有画布或交互状态,输入数据、输出结果,供交互策略组合调用;
|
|
3
|
+
// 常量为屏幕像素口径,调用方按视口缩放换算成世界坐标
|
|
4
|
+
import { Shape } from "./Shape";
|
|
5
|
+
import { OPPOSITE_DIR, PORT_DIR_VEC } from "./FlowEdge";
|
|
6
|
+
import { CLICK_CREATE_GAP } from "./session";
|
|
7
|
+
// 端口命中半径(世界坐标):也用作缩放边线对四边中点的避让宽度
|
|
8
|
+
export const PORT_HIT_RADIUS = 10;
|
|
9
|
+
// 命中参数(屏幕像素):手柄、边线、连线折线与端点手柄共用口径
|
|
10
|
+
export const HANDLE_HIT = 8; // 手柄命中半径(屏幕像素)
|
|
11
|
+
export const EDGE_TOLERANCE = 5; // 边线命中容差(屏幕像素)
|
|
12
|
+
export const EDGE_HANDLE_HIT = 8; // 连线端点手柄命中半径(屏幕像素)
|
|
13
|
+
export const MIN_SIZE = 20; // 缩放最小尺寸(世界坐标)
|
|
14
|
+
// 连线端口圆点外观:圆心相对边中点向外的偏移与悬浮放大倍数(屏幕像素)
|
|
15
|
+
export const PORT_RADIUS = 5;
|
|
16
|
+
export const PORT_OFFSET = 24;
|
|
17
|
+
export const PORT_HOVER_SCALE = 2.4;
|
|
18
|
+
// 连线拖拽吸附:光标进入目标节点包围盒外扩该范围时,目标节点浮出对接手柄
|
|
19
|
+
export const LINK_SNAP_MARGIN = 16; // 吸附感应范围(屏幕像素)
|
|
20
|
+
// 命中形状:hitNode 返回最深层(可能是形状内嵌 Text),沿父链回溯到 Shape
|
|
21
|
+
export function hitShape(children, p) {
|
|
22
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
23
|
+
const hit = children[i].hitNode(p.x, p.y);
|
|
24
|
+
if (!hit)
|
|
25
|
+
continue;
|
|
26
|
+
let cur = hit;
|
|
27
|
+
while (cur && !(cur instanceof Shape))
|
|
28
|
+
cur = cur.parent;
|
|
29
|
+
if (cur)
|
|
30
|
+
return cur;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
// 端口显示位置:边中点沿外法向偏移 PORT_OFFSET 屏幕像素(圆点浮在形状外侧)。
|
|
35
|
+
// 节点描边为 border-box(画在尺寸内部、外缘即几何边界),圆点与可见边线的间距恒定
|
|
36
|
+
export function portPos(shape, dir, scale) {
|
|
37
|
+
const port = shape.portAt(dir);
|
|
38
|
+
const off = PORT_OFFSET / scale;
|
|
39
|
+
const d = {
|
|
40
|
+
top: { x: 0, y: -1 },
|
|
41
|
+
right: { x: 1, y: 0 },
|
|
42
|
+
bottom: { x: 0, y: 1 },
|
|
43
|
+
left: { x: -1, y: 0 },
|
|
44
|
+
}[dir];
|
|
45
|
+
return { x: port.x + d.x * off, y: port.y + d.y * off };
|
|
46
|
+
}
|
|
47
|
+
// 端口命中:返回命中的方向,未命中返回 null(按外浮后的圆点位置判定)
|
|
48
|
+
export function portHit(shape, p, scale) {
|
|
49
|
+
// 命中范围 = 悬浮放大后的视觉圆(PORT_RADIUS * PORT_HOVER_SCALE),
|
|
50
|
+
// 进入即触发悬浮/点击,之外不响应
|
|
51
|
+
const r = (PORT_RADIUS * PORT_HOVER_SCALE) / scale;
|
|
52
|
+
for (const dir of ["top", "right", "bottom", "left"]) {
|
|
53
|
+
const port = portPos(shape, dir, scale);
|
|
54
|
+
if (Math.hypot(p.x - port.x, p.y - port.y) <= r)
|
|
55
|
+
return dir;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
// 四角手柄命中(屏幕像素半径换算到世界坐标),形状与框选框通用
|
|
60
|
+
export function cornerHit(b, p, scale) {
|
|
61
|
+
const r = HANDLE_HIT / scale;
|
|
62
|
+
const { x, y, width: w, height: h } = b;
|
|
63
|
+
const corners = [
|
|
64
|
+
["nw", x, y],
|
|
65
|
+
["ne", x + w, y],
|
|
66
|
+
["se", x + w, y + h],
|
|
67
|
+
["sw", x, y + h],
|
|
68
|
+
];
|
|
69
|
+
for (const [handle, cx, cy] of corners) {
|
|
70
|
+
if (Math.hypot(p.x - cx, p.y - cy) <= r)
|
|
71
|
+
return handle;
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
// bounds 边线命中(四边拖拽缩放):距边线 EDGE_TOLERANCE 内命中;
|
|
76
|
+
// 四角让位给角手柄、四边中点让位给连线端口(连接与缩放互不干扰)
|
|
77
|
+
export function edgeHit(b, p, scale) {
|
|
78
|
+
const tol = EDGE_TOLERANCE / scale;
|
|
79
|
+
const nearCorner = HANDLE_HIT / scale + 2;
|
|
80
|
+
const nearPort = PORT_HIT_RADIUS;
|
|
81
|
+
const { x, y, width: w, height: h } = b;
|
|
82
|
+
const sides = [
|
|
83
|
+
["n", { x, y }, { x: x + w, y }],
|
|
84
|
+
["e", { x: x + w, y }, { x: x + w, y: y + h }],
|
|
85
|
+
["s", { x, y: y + h }, { x: x + w, y: y + h }],
|
|
86
|
+
["w", { x, y }, { x, y: y + h }],
|
|
87
|
+
];
|
|
88
|
+
for (const [side, a, b] of sides) {
|
|
89
|
+
const len = Math.hypot(b.x - a.x, b.y - a.y);
|
|
90
|
+
if (len === 0)
|
|
91
|
+
continue;
|
|
92
|
+
// 点到线段的投影参数 t(截断到 0..1)与垂距
|
|
93
|
+
const t = Math.max(0, Math.min(1, ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / (len * len)));
|
|
94
|
+
const proj = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
|
|
95
|
+
if (Math.hypot(p.x - proj.x, p.y - proj.y) > tol)
|
|
96
|
+
continue;
|
|
97
|
+
const along = t * len;
|
|
98
|
+
if (along < nearCorner || len - along < nearCorner)
|
|
99
|
+
continue;
|
|
100
|
+
if (Math.abs(along - len / 2) < nearPort)
|
|
101
|
+
continue;
|
|
102
|
+
return side;
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
// 命中连线:距折线各段容差内命中;可见标签的白底盒同样算连线的一部分
|
|
107
|
+
//(标签盒遮住线体且上下沿超出线宽,仅按线体容差时点击标签上下边缘会落空)
|
|
108
|
+
export function hitEdge(edges, p, scale) {
|
|
109
|
+
const tol = EDGE_TOLERANCE / scale;
|
|
110
|
+
for (const edge of edges) {
|
|
111
|
+
if (edge.hitPoint(p, tol))
|
|
112
|
+
return edge;
|
|
113
|
+
const l = edge.label;
|
|
114
|
+
if (l.visible &&
|
|
115
|
+
p.x >= l.x - tol &&
|
|
116
|
+
p.x <= l.x + l.width + tol &&
|
|
117
|
+
p.y >= l.y - tol &&
|
|
118
|
+
p.y <= l.y + l.height + tol)
|
|
119
|
+
return edge;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
// 连线端点手柄命中:返回命中的端,未命中返回 null
|
|
124
|
+
export function edgeHandleHit(edge, p, scale) {
|
|
125
|
+
const pts = edge.points;
|
|
126
|
+
if (pts.length < 2)
|
|
127
|
+
return null;
|
|
128
|
+
const r = EDGE_HANDLE_HIT / scale;
|
|
129
|
+
if (Math.hypot(p.x - pts[0].x, p.y - pts[0].y) <= r)
|
|
130
|
+
return "from";
|
|
131
|
+
const last = pts[pts.length - 1];
|
|
132
|
+
if (Math.hypot(p.x - last.x, p.y - last.y) <= r)
|
|
133
|
+
return "to";
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
// 悬停光标:手柄/边线给缩放光标(端口与节点本体保持默认光标)
|
|
137
|
+
export function hoverCursor(shape, p, scale) {
|
|
138
|
+
const corner = cornerHit(shape, p, scale);
|
|
139
|
+
if (corner)
|
|
140
|
+
return corner === "nw" || corner === "se" ? "nwse-resize" : "nesw-resize";
|
|
141
|
+
const side = edgeHit(shape, p, scale);
|
|
142
|
+
if (side)
|
|
143
|
+
return side === "n" || side === "s" ? "ns-resize" : "ew-resize";
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
// 自由拖拽时预览的虚拟进入方向:与出口方向同轴,按光标相对出口端口的位置
|
|
147
|
+
// 选边——光标在出口外法向一侧时沿行进方向顺势进入对侧(如从下方出来、
|
|
148
|
+
// 光标在下方则按从上面进入,形成下、右、下的顺向路径),越过出口端口时同侧进入
|
|
149
|
+
export function previewToDir(exitDir, exitPort, p) {
|
|
150
|
+
const v = PORT_DIR_VEC[exitDir];
|
|
151
|
+
const proj = (p.x - exitPort.x) * v.x + (p.y - exitPort.y) * v.y;
|
|
152
|
+
return proj > 0 ? OPPOSITE_DIR[exitDir] : exitDir;
|
|
153
|
+
}
|
|
154
|
+
// 多个形状的并集包围盒(接受任意含 x/y/width/height 的载体,含原始位置快照)
|
|
155
|
+
export function unionBounds(shapes) {
|
|
156
|
+
let x0 = Infinity;
|
|
157
|
+
let y0 = Infinity;
|
|
158
|
+
let x1 = -Infinity;
|
|
159
|
+
let y1 = -Infinity;
|
|
160
|
+
for (const s of shapes) {
|
|
161
|
+
x0 = Math.min(x0, s.x);
|
|
162
|
+
y0 = Math.min(y0, s.y);
|
|
163
|
+
x1 = Math.max(x1, s.x + s.width);
|
|
164
|
+
y1 = Math.max(y1, s.y + s.height);
|
|
165
|
+
}
|
|
166
|
+
return { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
|
|
167
|
+
}
|
|
168
|
+
// 与框选矩形相交的形状(相交即命中)
|
|
169
|
+
export function marqueeHits(shapes, r) {
|
|
170
|
+
return [...shapes].filter((sh) => sh.x < r.x + r.width &&
|
|
171
|
+
sh.x + sh.width > r.x &&
|
|
172
|
+
sh.y < r.y + r.height &&
|
|
173
|
+
sh.y + sh.height > r.y);
|
|
174
|
+
}
|
|
175
|
+
// 连线拖拽吸附目标查找:光标进入某节点包围盒外扩 LINK_SNAP_MARGIN 范围时,
|
|
176
|
+
// 该节点成为目标,并吸附到距光标最近的端口(任意节点均可,包括连线/预览
|
|
177
|
+
// 自身的节点——支持改接边与自环);无节点在感应范围内返回 null
|
|
178
|
+
export function findLinkTarget(shapes, p, scale) {
|
|
179
|
+
const m = LINK_SNAP_MARGIN / scale;
|
|
180
|
+
for (const shape of shapes) {
|
|
181
|
+
if (p.x < shape.x - m ||
|
|
182
|
+
p.x > shape.x + shape.width + m ||
|
|
183
|
+
p.y < shape.y - m ||
|
|
184
|
+
p.y > shape.y + shape.height + m)
|
|
185
|
+
continue;
|
|
186
|
+
let best = Infinity;
|
|
187
|
+
let snap = "top";
|
|
188
|
+
for (const dir of ["top", "right", "bottom", "left"]) {
|
|
189
|
+
const q = portPos(shape, dir, scale);
|
|
190
|
+
const d = Math.hypot(p.x - q.x, p.y - q.y);
|
|
191
|
+
if (d < best) {
|
|
192
|
+
best = d;
|
|
193
|
+
snap = dir;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return { target: shape, snap };
|
|
197
|
+
}
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
// 按手柄方位应用缩放:对侧边/对角锚定,尺寸钳制到 MIN_SIZE
|
|
201
|
+
export function applyResize(shape, handle, origin, dx, dy) {
|
|
202
|
+
let { x, y, width: w, height: h } = origin;
|
|
203
|
+
if (handle.includes("e"))
|
|
204
|
+
w = origin.width + dx;
|
|
205
|
+
if (handle.includes("s"))
|
|
206
|
+
h = origin.height + dy;
|
|
207
|
+
if (handle.includes("w"))
|
|
208
|
+
w = origin.width - dx;
|
|
209
|
+
if (handle.includes("n"))
|
|
210
|
+
h = origin.height - dy;
|
|
211
|
+
if (handle.length === 2) {
|
|
212
|
+
// 角手柄:锁定比例等比缩放,取相对变化更大的轴作为统一缩放比
|
|
213
|
+
// (边手柄仅单轴生效,仍可自由改变比例)
|
|
214
|
+
const r = Math.max(Math.abs(w / origin.width) >= Math.abs(h / origin.height)
|
|
215
|
+
? w / origin.width
|
|
216
|
+
: h / origin.height, MIN_SIZE / origin.width, MIN_SIZE / origin.height);
|
|
217
|
+
w = origin.width * r;
|
|
218
|
+
h = origin.height * r;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
w = Math.max(MIN_SIZE, w);
|
|
222
|
+
h = Math.max(MIN_SIZE, h);
|
|
223
|
+
}
|
|
224
|
+
// 锚点:拖拽方向的对侧边/角固定不动
|
|
225
|
+
if (handle.includes("w"))
|
|
226
|
+
x = origin.x + origin.width - w;
|
|
227
|
+
if (handle.includes("n"))
|
|
228
|
+
y = origin.y + origin.height - h;
|
|
229
|
+
shape.x = x;
|
|
230
|
+
shape.y = y;
|
|
231
|
+
shape.width = w;
|
|
232
|
+
shape.height = h;
|
|
233
|
+
shape.layout();
|
|
234
|
+
}
|
|
235
|
+
// 多选组合缩放:以组合 bounds 的对侧为锚点,按整体缩放比等比变换每个成员
|
|
236
|
+
// 的位置与尺寸;按组内最小形状钳制缩放比,保证任何成员不低于 MIN_SIZE
|
|
237
|
+
export function applyGroupResize(handle, origin, origins, dx, dy) {
|
|
238
|
+
let sx = 1;
|
|
239
|
+
let sy = 1;
|
|
240
|
+
if (handle.includes("e"))
|
|
241
|
+
sx = (origin.width + dx) / origin.width;
|
|
242
|
+
if (handle.includes("w"))
|
|
243
|
+
sx = (origin.width - dx) / origin.width;
|
|
244
|
+
if (handle.includes("s"))
|
|
245
|
+
sy = (origin.height + dy) / origin.height;
|
|
246
|
+
if (handle.includes("n"))
|
|
247
|
+
sy = (origin.height - dy) / origin.height;
|
|
248
|
+
const minW = Math.min(...origins.map((g) => g.width));
|
|
249
|
+
const minH = Math.min(...origins.map((g) => g.height));
|
|
250
|
+
if (handle.length === 2) {
|
|
251
|
+
// 角手柄:锁定比例等比缩放,取相对变化更大的轴作为统一缩放比
|
|
252
|
+
// (边手柄仅单轴生效,仍可自由改变比例)
|
|
253
|
+
const r = Math.max(Math.abs(sx) >= Math.abs(sy) ? sx : sy, MIN_SIZE / minW, MIN_SIZE / minH);
|
|
254
|
+
sx = r;
|
|
255
|
+
sy = r;
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
sx = Math.max(sx, MIN_SIZE / minW);
|
|
259
|
+
sy = Math.max(sy, MIN_SIZE / minH);
|
|
260
|
+
}
|
|
261
|
+
// 锚点:拖拽方向的对侧边/角固定不动
|
|
262
|
+
const ax = handle.includes("w") ? origin.x + origin.width : origin.x;
|
|
263
|
+
const ay = handle.includes("n") ? origin.y + origin.height : origin.y;
|
|
264
|
+
for (const g of origins) {
|
|
265
|
+
g.shape.x = ax + (g.x - ax) * sx;
|
|
266
|
+
g.shape.y = ay + (g.y - ay) * sy;
|
|
267
|
+
g.shape.width = g.width * sx;
|
|
268
|
+
g.shape.height = g.height * sy;
|
|
269
|
+
g.shape.layout();
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// ---- 端口方向快速创建的共享几何 ----
|
|
273
|
+
//
|
|
274
|
+
// 端口手柄点击新建节点(interactions.createNodeInDirection)与 AI 语义图新增节点
|
|
275
|
+
// 落位(ai-protocol.mergeFlowGeometry)共用同一份口径,避免两套几何各自实现。
|
|
276
|
+
// 沿 dir 在源节点外侧放置新节点:边缘间距 CLICK_CREATE_GAP,垂直于方向的
|
|
277
|
+
// 轴线与源节点中心对齐。交互入口传入在位 Shape,程序化入口传入数据快照即可。
|
|
278
|
+
export function placeNodeInDirection(source, dir, size) {
|
|
279
|
+
if (dir === "right") {
|
|
280
|
+
return {
|
|
281
|
+
x: source.x + source.width + CLICK_CREATE_GAP,
|
|
282
|
+
y: source.y + (source.height - size.height) / 2,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
if (dir === "left") {
|
|
286
|
+
return {
|
|
287
|
+
x: source.x - CLICK_CREATE_GAP - size.width,
|
|
288
|
+
y: source.y + (source.height - size.height) / 2,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
if (dir === "bottom") {
|
|
292
|
+
return {
|
|
293
|
+
x: source.x + (source.width - size.width) / 2,
|
|
294
|
+
y: source.y + source.height + CLICK_CREATE_GAP,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
x: source.x + (source.width - size.width) / 2,
|
|
299
|
+
y: source.y - CLICK_CREATE_GAP - size.height,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
// 由两节点中心的主导方向推断端口方向(与 FlowEdge 构造时 hints 缺省的选边口径一致):
|
|
303
|
+
// 语义图不含方向信息,程序据此把「布局里在谁下方/右侧」翻译成手柄方向
|
|
304
|
+
export function dominantPortDir(from, to) {
|
|
305
|
+
const dx = to.x + to.width / 2 - (from.x + from.width / 2);
|
|
306
|
+
const dy = to.y + to.height / 2 - (from.y + from.height / 2);
|
|
307
|
+
if (Math.abs(dx) >= Math.abs(dy))
|
|
308
|
+
return dx >= 0 ? "right" : "left";
|
|
309
|
+
return dy >= 0 ? "bottom" : "top";
|
|
310
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { App, HistoryCommand, SerializedCommand } from "@silkor/core";
|
|
2
|
+
import type { PortDir } from "./FlowEdge";
|
|
3
|
+
import type { FlowNodeData } from "./data";
|
|
4
|
+
import type { EdgeRouterCapability } from "./edge-router";
|
|
5
|
+
import type { FlowModel } from "./model";
|
|
6
|
+
export interface FlowchartCodecDeps {
|
|
7
|
+
model: FlowModel;
|
|
8
|
+
app: App;
|
|
9
|
+
router: EdgeRouterCapability | null;
|
|
10
|
+
}
|
|
11
|
+
export interface AddShapePayload {
|
|
12
|
+
data: FlowNodeData;
|
|
13
|
+
}
|
|
14
|
+
export interface DeletePayload {
|
|
15
|
+
v: "shapes" | "edge";
|
|
16
|
+
items?: Array<{
|
|
17
|
+
data: FlowNodeData;
|
|
18
|
+
childIdx: number;
|
|
19
|
+
edges: Array<{
|
|
20
|
+
id: string;
|
|
21
|
+
fromId: string;
|
|
22
|
+
toId: string;
|
|
23
|
+
fromDir: PortDir;
|
|
24
|
+
toDir: PortDir;
|
|
25
|
+
text?: string;
|
|
26
|
+
order: number;
|
|
27
|
+
childIdx: number;
|
|
28
|
+
}>;
|
|
29
|
+
}>;
|
|
30
|
+
edge?: {
|
|
31
|
+
edgeId: string;
|
|
32
|
+
fromId: string;
|
|
33
|
+
toId: string;
|
|
34
|
+
fromDir: PortDir;
|
|
35
|
+
toDir: PortDir;
|
|
36
|
+
text?: string;
|
|
37
|
+
};
|
|
38
|
+
childIdx?: number;
|
|
39
|
+
listIdx?: number;
|
|
40
|
+
}
|
|
41
|
+
export interface ReplacePayload {
|
|
42
|
+
oldData: FlowNodeData;
|
|
43
|
+
newData: FlowNodeData;
|
|
44
|
+
}
|
|
45
|
+
export declare function createFlowchartCodec(deps: FlowchartCodecDeps): {
|
|
46
|
+
decode: (s: SerializedCommand) => HistoryCommand | null;
|
|
47
|
+
};
|