@vanduo-oss/vd3-cbun 1.0.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.
- package/CHANGELOG.md +50 -0
- package/LICENSE +22 -0
- package/README.md +160 -0
- package/SKILL.md +119 -0
- package/dist/charts/core.d.ts +273 -0
- package/dist/charts/index.cjs +1828 -0
- package/dist/charts/index.cjs.map +7 -0
- package/dist/charts/index.d.ts +65 -0
- package/dist/charts/index.js +1805 -0
- package/dist/charts/index.js.map +7 -0
- package/dist/charts/vd3-charts.css +51 -0
- package/dist/charts/vue.d.ts +86 -0
- package/dist/flowchart/core.d.ts +288 -0
- package/dist/flowchart/index.cjs +3447 -0
- package/dist/flowchart/index.cjs.map +7 -0
- package/dist/flowchart/index.d.ts +54 -0
- package/dist/flowchart/index.js +3424 -0
- package/dist/flowchart/index.js.map +7 -0
- package/dist/flowchart/vd3-flowchart.css +600 -0
- package/dist/flowchart/vue.d.ts +66 -0
- package/dist/hex-grid/core.d.ts +200 -0
- package/dist/hex-grid/hex-math.cjs +162 -0
- package/dist/hex-grid/hex-math.cjs.map +7 -0
- package/dist/hex-grid/hex-math.d.ts +119 -0
- package/dist/hex-grid/hex-math.js +141 -0
- package/dist/hex-grid/hex-math.js.map +7 -0
- package/dist/hex-grid/index.cjs +915 -0
- package/dist/hex-grid/index.cjs.map +7 -0
- package/dist/hex-grid/index.d.ts +15 -0
- package/dist/hex-grid/index.js +894 -0
- package/dist/hex-grid/index.js.map +7 -0
- package/dist/hex-grid/vue.d.ts +14 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +7 -0
- package/dist/meta.json +551 -0
- package/dist/music-player/core.d.ts +88 -0
- package/dist/music-player/index.cjs +1227 -0
- package/dist/music-player/index.cjs.map +7 -0
- package/dist/music-player/index.d.ts +12 -0
- package/dist/music-player/index.js +1204 -0
- package/dist/music-player/index.js.map +7 -0
- package/dist/music-player/vd3-music-player.css +829 -0
- package/dist/music-player/vue.d.ts +32 -0
- package/package.json +105 -0
|
@@ -0,0 +1,3424 @@
|
|
|
1
|
+
// src/flowchart/vue.js
|
|
2
|
+
import { defineComponent, h, ref, onMounted, onBeforeUnmount, watch } from "vue";
|
|
3
|
+
|
|
4
|
+
// src/flowchart/layout.js
|
|
5
|
+
var DIRECTIONS = {
|
|
6
|
+
right: { main: "x", sign: 1 },
|
|
7
|
+
left: { main: "x", sign: -1 },
|
|
8
|
+
down: { main: "y", sign: 1 },
|
|
9
|
+
up: { main: "y", sign: -1 }
|
|
10
|
+
};
|
|
11
|
+
function num(value, fallback) {
|
|
12
|
+
const next = Number(value);
|
|
13
|
+
return Number.isFinite(next) ? next : fallback;
|
|
14
|
+
}
|
|
15
|
+
function buildGraph(nodes, edges) {
|
|
16
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
17
|
+
const children = new Map(nodes.map((node) => [node.id, []]));
|
|
18
|
+
const indegree = new Map(nodes.map((node) => [node.id, 0]));
|
|
19
|
+
edges.forEach((edge) => {
|
|
20
|
+
const from = edge?.from?.nodeId;
|
|
21
|
+
const to = edge?.to?.nodeId;
|
|
22
|
+
if (from === to || !byId.has(from) || !byId.has(to)) return;
|
|
23
|
+
children.get(from).push(to);
|
|
24
|
+
indegree.set(to, indegree.get(to) + 1);
|
|
25
|
+
});
|
|
26
|
+
return { byId, children, indegree };
|
|
27
|
+
}
|
|
28
|
+
function pickRoots(nodes, indegree, optRoot) {
|
|
29
|
+
if (optRoot && indegree.has(optRoot)) return [optRoot];
|
|
30
|
+
const roots = nodes.filter((node) => indegree.get(node.id) === 0).map((node) => node.id);
|
|
31
|
+
return roots.length ? roots : nodes.length ? [nodes[0].id] : [];
|
|
32
|
+
}
|
|
33
|
+
function spanningForest(allIds, roots, children) {
|
|
34
|
+
const visited = /* @__PURE__ */ new Set();
|
|
35
|
+
const depth = /* @__PURE__ */ new Map();
|
|
36
|
+
const tree = /* @__PURE__ */ new Map();
|
|
37
|
+
const forestRoots = [];
|
|
38
|
+
const walk = (id, d) => {
|
|
39
|
+
visited.add(id);
|
|
40
|
+
depth.set(id, d);
|
|
41
|
+
const kids = [];
|
|
42
|
+
(children.get(id) || []).forEach((child) => {
|
|
43
|
+
if (!visited.has(child)) {
|
|
44
|
+
kids.push(child);
|
|
45
|
+
walk(child, d + 1);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
tree.set(id, kids);
|
|
49
|
+
};
|
|
50
|
+
roots.forEach((root) => {
|
|
51
|
+
if (!visited.has(root)) {
|
|
52
|
+
forestRoots.push(root);
|
|
53
|
+
walk(root, 0);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
allIds.forEach((id) => {
|
|
57
|
+
if (!visited.has(id)) {
|
|
58
|
+
forestRoots.push(id);
|
|
59
|
+
walk(id, 0);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return { depth, tree, forestRoots };
|
|
63
|
+
}
|
|
64
|
+
function treeCenters(forestRoots, tree, depth, sizeOf, options) {
|
|
65
|
+
const dir = DIRECTIONS[options.direction] || DIRECTIONS.right;
|
|
66
|
+
const levelGap = num(options.levelGap, 220);
|
|
67
|
+
const siblingGap = num(options.siblingGap, 140);
|
|
68
|
+
const crossOf = /* @__PURE__ */ new Map();
|
|
69
|
+
let cursor = 0;
|
|
70
|
+
const place = (id) => {
|
|
71
|
+
const kids = tree.get(id) || [];
|
|
72
|
+
if (!kids.length) {
|
|
73
|
+
crossOf.set(id, cursor);
|
|
74
|
+
cursor += siblingGap;
|
|
75
|
+
return crossOf.get(id);
|
|
76
|
+
}
|
|
77
|
+
const childCenters = kids.map(place);
|
|
78
|
+
const center = (childCenters[0] + childCenters[childCenters.length - 1]) / 2;
|
|
79
|
+
crossOf.set(id, center);
|
|
80
|
+
return center;
|
|
81
|
+
};
|
|
82
|
+
forestRoots.forEach((root) => {
|
|
83
|
+
place(root);
|
|
84
|
+
cursor += siblingGap;
|
|
85
|
+
});
|
|
86
|
+
const centers = /* @__PURE__ */ new Map();
|
|
87
|
+
crossOf.forEach((cross, id) => {
|
|
88
|
+
const main = (depth.get(id) || 0) * levelGap * dir.sign;
|
|
89
|
+
centers.set(
|
|
90
|
+
id,
|
|
91
|
+
dir.main === "x" ? { centerX: main, centerY: cross } : { centerX: cross, centerY: main }
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
return centers;
|
|
95
|
+
}
|
|
96
|
+
function radialCenters(forestRoots, tree, depth, options) {
|
|
97
|
+
const ringGap = num(options.radius, 220);
|
|
98
|
+
const leaves = [];
|
|
99
|
+
const collect = (id) => {
|
|
100
|
+
const kids = tree.get(id) || [];
|
|
101
|
+
if (!kids.length) leaves.push(id);
|
|
102
|
+
else kids.forEach(collect);
|
|
103
|
+
};
|
|
104
|
+
forestRoots.forEach(collect);
|
|
105
|
+
const total = Math.max(1, leaves.length);
|
|
106
|
+
const leafAngle = new Map(leaves.map((id, index) => [id, (index + 0.5) / total * Math.PI * 2]));
|
|
107
|
+
const angleOf = /* @__PURE__ */ new Map();
|
|
108
|
+
const resolveAngle = (id) => {
|
|
109
|
+
const kids = tree.get(id) || [];
|
|
110
|
+
if (!kids.length) {
|
|
111
|
+
angleOf.set(id, leafAngle.get(id));
|
|
112
|
+
return angleOf.get(id);
|
|
113
|
+
}
|
|
114
|
+
const childAngles = kids.map(resolveAngle);
|
|
115
|
+
const mean = childAngles.reduce((sum, value) => sum + value, 0) / childAngles.length;
|
|
116
|
+
angleOf.set(id, mean);
|
|
117
|
+
return mean;
|
|
118
|
+
};
|
|
119
|
+
forestRoots.forEach(resolveAngle);
|
|
120
|
+
const centers = /* @__PURE__ */ new Map();
|
|
121
|
+
angleOf.forEach((angle, id) => {
|
|
122
|
+
const radius = (depth.get(id) || 0) * ringGap;
|
|
123
|
+
centers.set(id, { centerX: Math.cos(angle) * radius, centerY: Math.sin(angle) * radius });
|
|
124
|
+
});
|
|
125
|
+
return centers;
|
|
126
|
+
}
|
|
127
|
+
function gridCenters(nodes, sizeOf, options) {
|
|
128
|
+
const gap = num(options.gap, 48);
|
|
129
|
+
const columns = Math.max(
|
|
130
|
+
1,
|
|
131
|
+
Math.floor(num(options.columns, Math.ceil(Math.sqrt(nodes.length || 1))))
|
|
132
|
+
);
|
|
133
|
+
let cellW = 0;
|
|
134
|
+
let cellH = 0;
|
|
135
|
+
nodes.forEach((node) => {
|
|
136
|
+
const size = sizeOf(node.id);
|
|
137
|
+
cellW = Math.max(cellW, size.width);
|
|
138
|
+
cellH = Math.max(cellH, size.height);
|
|
139
|
+
});
|
|
140
|
+
cellW += gap;
|
|
141
|
+
cellH += gap;
|
|
142
|
+
const sorted = [...nodes].sort((a, b) => a.y - b.y || a.x - b.x);
|
|
143
|
+
const centers = /* @__PURE__ */ new Map();
|
|
144
|
+
sorted.forEach((node, index) => {
|
|
145
|
+
const col = index % columns;
|
|
146
|
+
const row = Math.floor(index / columns);
|
|
147
|
+
centers.set(node.id, { centerX: col * cellW, centerY: row * cellH });
|
|
148
|
+
});
|
|
149
|
+
return centers;
|
|
150
|
+
}
|
|
151
|
+
function computeLayout(documentData, mode = "tree", options = {}) {
|
|
152
|
+
const nodes = Array.isArray(documentData?.nodes) ? documentData.nodes : [];
|
|
153
|
+
const edges = Array.isArray(documentData?.edges) ? documentData.edges : [];
|
|
154
|
+
if (!nodes.length) return /* @__PURE__ */ new Map();
|
|
155
|
+
const { byId, children, indegree } = buildGraph(nodes, edges);
|
|
156
|
+
const sizeOf = (id) => {
|
|
157
|
+
const node = byId.get(id);
|
|
158
|
+
return { width: num(node?.width, 160), height: num(node?.height, 96) };
|
|
159
|
+
};
|
|
160
|
+
const allIds = nodes.map((node) => node.id);
|
|
161
|
+
let centers;
|
|
162
|
+
let anchorId = nodes[0].id;
|
|
163
|
+
if (mode === "grid") {
|
|
164
|
+
centers = gridCenters(nodes, sizeOf, options);
|
|
165
|
+
} else {
|
|
166
|
+
const rootOpt = options.root == null ? null : String(options.root).trim();
|
|
167
|
+
const roots = pickRoots(nodes, indegree, rootOpt);
|
|
168
|
+
anchorId = roots[0] || anchorId;
|
|
169
|
+
const { depth, tree, forestRoots } = spanningForest(allIds, roots, children);
|
|
170
|
+
centers = mode === "radial" ? radialCenters(forestRoots, tree, depth, options) : treeCenters(forestRoots, tree, depth, sizeOf, options);
|
|
171
|
+
}
|
|
172
|
+
const anchorNode = byId.get(anchorId);
|
|
173
|
+
const anchorCenter = centers.get(anchorId) || { centerX: 0, centerY: 0 };
|
|
174
|
+
const dx = anchorNode ? anchorNode.x + anchorNode.width / 2 - anchorCenter.centerX : 0;
|
|
175
|
+
const dy = anchorNode ? anchorNode.y + anchorNode.height / 2 - anchorCenter.centerY : 0;
|
|
176
|
+
const result = /* @__PURE__ */ new Map();
|
|
177
|
+
centers.forEach((center, id) => {
|
|
178
|
+
const size = sizeOf(id);
|
|
179
|
+
result.set(id, {
|
|
180
|
+
x: Math.round((center.centerX + dx - size.width / 2) * 100) / 100,
|
|
181
|
+
y: Math.round((center.centerY + dy - size.height / 2) * 100) / 100
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
var LAYOUT_MODES = ["tree", "radial", "grid"];
|
|
187
|
+
|
|
188
|
+
// src/flowchart/core.js
|
|
189
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
190
|
+
var DEFAULT_GRID_SIZE = 24;
|
|
191
|
+
var MIN_SCALE = 0.35;
|
|
192
|
+
var MAX_SCALE = 3;
|
|
193
|
+
var MIN_NODE_SIZE = 56;
|
|
194
|
+
var MAX_NODE_SIZE = 420;
|
|
195
|
+
var WORLD_EXTENT = 12e3;
|
|
196
|
+
var RESIZE_HANDLES = ["n", "ne", "e", "se", "s", "sw", "w", "nw"];
|
|
197
|
+
var DEFAULT_EDGE_STROKE_WIDTH = 2.25;
|
|
198
|
+
var MIN_EDGE_STROKE_WIDTH = 1.25;
|
|
199
|
+
var MAX_EDGE_STROKE_WIDTH = 6;
|
|
200
|
+
var CONNECTION_PORT_RADIUS = 6;
|
|
201
|
+
var CONNECTION_PORT_HIT_RADIUS = 14;
|
|
202
|
+
var RECONNECT_ENDPOINT_RADIUS = 7;
|
|
203
|
+
var RECONNECT_ENDPOINT_HIT_RADIUS = 12;
|
|
204
|
+
var EDGE_HIT_STROKE_MIN = 16;
|
|
205
|
+
var CONNECTION_SNAP_PADDING = 36;
|
|
206
|
+
var CONNECTION_HYSTERESIS = 16;
|
|
207
|
+
var CONNECTION_CENTER_LOCK_RADIUS = 18;
|
|
208
|
+
var RESIZE_PORT_GAP = 34;
|
|
209
|
+
var EDGE_STROKE_PRESETS = [
|
|
210
|
+
{ id: "thin", label: "Thin", width: 1.75 },
|
|
211
|
+
{ id: "medium", label: "Medium", width: DEFAULT_EDGE_STROKE_WIDTH },
|
|
212
|
+
{ id: "bold", label: "Bold", width: 3.5 }
|
|
213
|
+
];
|
|
214
|
+
var VD_FLOWCHART_VERSION = "1.2.0";
|
|
215
|
+
var FLOWCHART_NODE_TYPES = [
|
|
216
|
+
"rounded-rect",
|
|
217
|
+
"rect",
|
|
218
|
+
"diamond",
|
|
219
|
+
"circle",
|
|
220
|
+
"textbox",
|
|
221
|
+
"label",
|
|
222
|
+
"junction"
|
|
223
|
+
];
|
|
224
|
+
var FLOWCHART_PORTS = ["top", "right", "bottom", "left"];
|
|
225
|
+
var FLOWCHART_EDGE_MARKERS = ["none", "arrow", "dot"];
|
|
226
|
+
var FLOWCHART_EDGE_ROUTES = ["curve", "straight", "orthogonal"];
|
|
227
|
+
var COALESCING_REASONS = /* @__PURE__ */ new Set(["node:update", "edge:update"]);
|
|
228
|
+
var DEFAULT_EDGE_ROUTE = "curve";
|
|
229
|
+
var ORTHOGONAL_STUB_LENGTH = 32;
|
|
230
|
+
var ORTHOGONAL_CORNER_RADIUS = 12;
|
|
231
|
+
var ORTHOGONAL_CLEARANCE = 16;
|
|
232
|
+
var EDGE_CURVATURE = 0.5;
|
|
233
|
+
var MIN_CURVE_ARM = 22;
|
|
234
|
+
var MAX_CURVE_ARM = 260;
|
|
235
|
+
var FLOWCHART_EDGE_ROUTE_LABELS = {
|
|
236
|
+
curve: "Curve",
|
|
237
|
+
straight: "Straight",
|
|
238
|
+
orthogonal: "Stepped orthogonal"
|
|
239
|
+
};
|
|
240
|
+
var DEFAULT_NODE_SPECS = {
|
|
241
|
+
"rounded-rect": { width: 180, height: 96, text: "Step" },
|
|
242
|
+
rect: { width: 180, height: 96, text: "Process" },
|
|
243
|
+
diamond: { width: 184, height: 120, text: "Decision" },
|
|
244
|
+
circle: { width: 128, height: 128, text: "Start" },
|
|
245
|
+
textbox: { width: 240, height: 144, text: "Notes" },
|
|
246
|
+
label: { width: 180, height: 72, text: "Label" },
|
|
247
|
+
junction: {
|
|
248
|
+
width: 28,
|
|
249
|
+
height: 28,
|
|
250
|
+
text: "",
|
|
251
|
+
minWidth: 28,
|
|
252
|
+
minHeight: 28,
|
|
253
|
+
maxWidth: 28,
|
|
254
|
+
maxHeight: 28,
|
|
255
|
+
resizable: false,
|
|
256
|
+
textEditable: false
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
var FLOWCHART_PALETTE_ITEMS = [
|
|
260
|
+
{ kind: "tool", tool: "arrow", label: "arrow" },
|
|
261
|
+
{ kind: "node", type: "rounded-rect", label: "rounded rect" },
|
|
262
|
+
{ kind: "node", type: "rect", label: "rect" },
|
|
263
|
+
{ kind: "node", type: "diamond", label: "diamond" },
|
|
264
|
+
{ kind: "node", type: "circle", label: "circle" },
|
|
265
|
+
{ kind: "node", type: "junction", label: "junction" },
|
|
266
|
+
{ kind: "node", type: "textbox", label: "textbox" },
|
|
267
|
+
{ kind: "node", type: "label", label: "label" }
|
|
268
|
+
];
|
|
269
|
+
var flowchartId = 0;
|
|
270
|
+
function nextId(prefix) {
|
|
271
|
+
flowchartId += 1;
|
|
272
|
+
return `${prefix}-${flowchartId}`;
|
|
273
|
+
}
|
|
274
|
+
function hasWindow() {
|
|
275
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
276
|
+
}
|
|
277
|
+
function isElement(value) {
|
|
278
|
+
return hasWindow() && value instanceof Element;
|
|
279
|
+
}
|
|
280
|
+
function isPlainObject(value) {
|
|
281
|
+
return Boolean(value) && Object.prototype.toString.call(value) === "[object Object]";
|
|
282
|
+
}
|
|
283
|
+
function clamp(value, min, max) {
|
|
284
|
+
return Math.min(max, Math.max(min, value));
|
|
285
|
+
}
|
|
286
|
+
function toFiniteNumber(value, fallback) {
|
|
287
|
+
const next = Number(value);
|
|
288
|
+
return Number.isFinite(next) ? next : fallback;
|
|
289
|
+
}
|
|
290
|
+
function deepClone(value) {
|
|
291
|
+
return JSON.parse(JSON.stringify(value));
|
|
292
|
+
}
|
|
293
|
+
function formatNumber(value) {
|
|
294
|
+
return Number(value.toFixed(2));
|
|
295
|
+
}
|
|
296
|
+
function sanitizeId(value) {
|
|
297
|
+
if (value == null) return "";
|
|
298
|
+
return String(value).trim();
|
|
299
|
+
}
|
|
300
|
+
function resolveElement(target) {
|
|
301
|
+
if (!hasWindow()) {
|
|
302
|
+
throw new Error("Vanduo Flowchart requires a browser DOM target.");
|
|
303
|
+
}
|
|
304
|
+
if (typeof target === "string") {
|
|
305
|
+
const el = document.querySelector(target);
|
|
306
|
+
if (!el) throw new Error(`Flowchart target not found: ${target}`);
|
|
307
|
+
return el;
|
|
308
|
+
}
|
|
309
|
+
if (isElement(target)) return target;
|
|
310
|
+
throw new Error("Flowchart target must be an Element or selector string.");
|
|
311
|
+
}
|
|
312
|
+
function createElement(tagName, options = {}) {
|
|
313
|
+
const element = document.createElement(tagName);
|
|
314
|
+
if (options.className) element.className = options.className;
|
|
315
|
+
if (options.text != null) element.textContent = String(options.text);
|
|
316
|
+
if (options.type) element.type = options.type;
|
|
317
|
+
if (options.value != null) element.value = String(options.value);
|
|
318
|
+
if (options.placeholder != null) element.placeholder = String(options.placeholder);
|
|
319
|
+
if (options.title != null) element.title = String(options.title);
|
|
320
|
+
if (options.rows != null) element.rows = Number(options.rows);
|
|
321
|
+
if (options.disabled) element.disabled = true;
|
|
322
|
+
if (options.tabIndex != null) element.tabIndex = Number(options.tabIndex);
|
|
323
|
+
return element;
|
|
324
|
+
}
|
|
325
|
+
function svgEl(name, attrs = {}) {
|
|
326
|
+
const element = document.createElementNS(SVG_NS, name);
|
|
327
|
+
Object.entries(attrs).forEach(([key, value]) => {
|
|
328
|
+
if (value != null) {
|
|
329
|
+
element.setAttribute(key, String(value));
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
return element;
|
|
333
|
+
}
|
|
334
|
+
function clearChildren(element) {
|
|
335
|
+
while (element.firstChild) {
|
|
336
|
+
element.removeChild(element.firstChild);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
function ensureUniqueId(preferred, prefix, usedIds) {
|
|
340
|
+
let candidate = sanitizeId(preferred) || nextId(prefix);
|
|
341
|
+
while (usedIds.has(candidate)) {
|
|
342
|
+
candidate = nextId(prefix);
|
|
343
|
+
}
|
|
344
|
+
usedIds.add(candidate);
|
|
345
|
+
return candidate;
|
|
346
|
+
}
|
|
347
|
+
function normalizeNodeType(type) {
|
|
348
|
+
return FLOWCHART_NODE_TYPES.includes(type) ? type : "rounded-rect";
|
|
349
|
+
}
|
|
350
|
+
function getNodeSpec(type) {
|
|
351
|
+
return DEFAULT_NODE_SPECS[normalizeNodeType(type)];
|
|
352
|
+
}
|
|
353
|
+
function getNodeSizeBounds(type) {
|
|
354
|
+
const spec = getNodeSpec(type);
|
|
355
|
+
return {
|
|
356
|
+
minWidth: spec.minWidth ?? MIN_NODE_SIZE,
|
|
357
|
+
minHeight: spec.minHeight ?? MIN_NODE_SIZE,
|
|
358
|
+
maxWidth: spec.maxWidth ?? MAX_NODE_SIZE,
|
|
359
|
+
maxHeight: spec.maxHeight ?? MAX_NODE_SIZE
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
function clampNodeWidth(type, width, fallback) {
|
|
363
|
+
const bounds = getNodeSizeBounds(type);
|
|
364
|
+
return clamp(toFiniteNumber(width, fallback), bounds.minWidth, bounds.maxWidth);
|
|
365
|
+
}
|
|
366
|
+
function clampNodeHeight(type, height, fallback) {
|
|
367
|
+
const bounds = getNodeSizeBounds(type);
|
|
368
|
+
return clamp(toFiniteNumber(height, fallback), bounds.minHeight, bounds.maxHeight);
|
|
369
|
+
}
|
|
370
|
+
function isNodeResizable(nodeOrType) {
|
|
371
|
+
const spec = typeof nodeOrType === "string" ? getNodeSpec(nodeOrType) : getNodeSpec(nodeOrType?.type);
|
|
372
|
+
return spec.resizable !== false;
|
|
373
|
+
}
|
|
374
|
+
function isNodeTextEditable(nodeOrType) {
|
|
375
|
+
const spec = typeof nodeOrType === "string" ? getNodeSpec(nodeOrType) : getNodeSpec(nodeOrType?.type);
|
|
376
|
+
return spec.textEditable !== false;
|
|
377
|
+
}
|
|
378
|
+
function normalizeEdgeStrokeWidth(value) {
|
|
379
|
+
const next = Number(value);
|
|
380
|
+
if (!Number.isFinite(next)) return DEFAULT_EDGE_STROKE_WIDTH;
|
|
381
|
+
return formatNumber(clamp(next, MIN_EDGE_STROKE_WIDTH, MAX_EDGE_STROKE_WIDTH));
|
|
382
|
+
}
|
|
383
|
+
function getStrokePresetId(strokeWidth) {
|
|
384
|
+
const match = EDGE_STROKE_PRESETS.find((preset) => Math.abs(preset.width - strokeWidth) < 0.01);
|
|
385
|
+
return match?.id || "medium";
|
|
386
|
+
}
|
|
387
|
+
function getStrokePresetWidth(presetId) {
|
|
388
|
+
const preset = EDGE_STROKE_PRESETS.find((item) => item.id === presetId);
|
|
389
|
+
return preset?.width ?? DEFAULT_EDGE_STROKE_WIDTH;
|
|
390
|
+
}
|
|
391
|
+
function normalizeViewport(viewport) {
|
|
392
|
+
return {
|
|
393
|
+
x: toFiniteNumber(viewport?.x, 0),
|
|
394
|
+
y: toFiniteNumber(viewport?.y, 0),
|
|
395
|
+
scale: clamp(toFiniteNumber(viewport?.scale, 1), MIN_SCALE, MAX_SCALE)
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function normalizeNode(rawNode, index, usedIds) {
|
|
399
|
+
const type = normalizeNodeType(rawNode?.type);
|
|
400
|
+
const spec = getNodeSpec(type);
|
|
401
|
+
return {
|
|
402
|
+
id: ensureUniqueId(rawNode?.id, "node", usedIds),
|
|
403
|
+
type,
|
|
404
|
+
x: toFiniteNumber(rawNode?.x, index * 28),
|
|
405
|
+
y: toFiniteNumber(rawNode?.y, index * 18),
|
|
406
|
+
width: clampNodeWidth(type, rawNode?.width, spec.width),
|
|
407
|
+
height: clampNodeHeight(type, rawNode?.height, spec.height),
|
|
408
|
+
text: rawNode?.text == null ? spec.text : String(rawNode.text),
|
|
409
|
+
data: isPlainObject(rawNode?.data) ? deepClone(rawNode.data) : {}
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function normalizeEndpoint(rawEndpoint, fallbackPort) {
|
|
413
|
+
return {
|
|
414
|
+
nodeId: sanitizeId(rawEndpoint?.nodeId),
|
|
415
|
+
port: FLOWCHART_PORTS.includes(rawEndpoint?.port) ? rawEndpoint.port : fallbackPort
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function normalizeEdgeMarker(value) {
|
|
419
|
+
return FLOWCHART_EDGE_MARKERS.includes(value) ? value : null;
|
|
420
|
+
}
|
|
421
|
+
function normalizeEdgeRoute(value) {
|
|
422
|
+
return FLOWCHART_EDGE_ROUTES.includes(value) ? value : DEFAULT_EDGE_ROUTE;
|
|
423
|
+
}
|
|
424
|
+
function syncEdgeKind(edge) {
|
|
425
|
+
edge.kind = edge.startMarker === "none" && edge.endMarker === "none" ? "line" : "arrow";
|
|
426
|
+
}
|
|
427
|
+
function normalizeEdge(rawEdge, index, nodeIds, usedIds) {
|
|
428
|
+
const from = normalizeEndpoint(rawEdge?.from, "right");
|
|
429
|
+
const to = normalizeEndpoint(rawEdge?.to, "left");
|
|
430
|
+
if (!from.nodeId || !to.nodeId) return null;
|
|
431
|
+
if (!nodeIds.has(from.nodeId) || !nodeIds.has(to.nodeId)) return null;
|
|
432
|
+
if (!FLOWCHART_PORTS.includes(from.port) || !FLOWCHART_PORTS.includes(to.port)) return null;
|
|
433
|
+
const legacyKind = rawEdge?.kind === "line" ? "line" : "arrow";
|
|
434
|
+
const startMarker = normalizeEdgeMarker(rawEdge?.startMarker) ?? (legacyKind === "line" ? "none" : "none");
|
|
435
|
+
const endMarker = normalizeEdgeMarker(rawEdge?.endMarker) ?? (legacyKind === "line" ? "none" : "arrow");
|
|
436
|
+
const edge = {
|
|
437
|
+
id: ensureUniqueId(rawEdge?.id, "edge", usedIds),
|
|
438
|
+
from,
|
|
439
|
+
to,
|
|
440
|
+
kind: legacyKind,
|
|
441
|
+
startMarker,
|
|
442
|
+
endMarker,
|
|
443
|
+
strokeWidth: normalizeEdgeStrokeWidth(rawEdge?.strokeWidth),
|
|
444
|
+
route: normalizeEdgeRoute(rawEdge?.route),
|
|
445
|
+
label: rawEdge?.label == null ? "" : String(rawEdge.label),
|
|
446
|
+
data: isPlainObject(rawEdge?.data) ? deepClone(rawEdge.data) : {}
|
|
447
|
+
};
|
|
448
|
+
syncEdgeKind(edge);
|
|
449
|
+
return edge;
|
|
450
|
+
}
|
|
451
|
+
function normalizeDocument(input) {
|
|
452
|
+
let source = input;
|
|
453
|
+
if (typeof source === "string") {
|
|
454
|
+
try {
|
|
455
|
+
source = JSON.parse(source);
|
|
456
|
+
} catch {
|
|
457
|
+
source = {};
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (!isPlainObject(source)) {
|
|
461
|
+
source = {};
|
|
462
|
+
}
|
|
463
|
+
const usedNodeIds = /* @__PURE__ */ new Set();
|
|
464
|
+
const nodes = (Array.isArray(source.nodes) ? source.nodes : []).map(
|
|
465
|
+
(node, index) => normalizeNode(node, index, usedNodeIds)
|
|
466
|
+
);
|
|
467
|
+
const nodeIds = new Set(nodes.map((node) => node.id));
|
|
468
|
+
const usedEdgeIds = /* @__PURE__ */ new Set();
|
|
469
|
+
const edges = (Array.isArray(source.edges) ? source.edges : []).map((edge, index) => normalizeEdge(edge, index, nodeIds, usedEdgeIds)).filter(Boolean);
|
|
470
|
+
return {
|
|
471
|
+
version: VD_FLOWCHART_VERSION,
|
|
472
|
+
viewport: normalizeViewport(source.viewport),
|
|
473
|
+
nodes,
|
|
474
|
+
edges
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
function splitLongToken(token, maxChars) {
|
|
478
|
+
const result = [];
|
|
479
|
+
let index = 0;
|
|
480
|
+
while (index < token.length) {
|
|
481
|
+
result.push(token.slice(index, index + maxChars));
|
|
482
|
+
index += maxChars;
|
|
483
|
+
}
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
function wrapText(text, maxChars) {
|
|
487
|
+
const safeMaxChars = Math.max(6, maxChars);
|
|
488
|
+
const lines = [];
|
|
489
|
+
String(text || "").split(/\r?\n/).forEach((paragraph) => {
|
|
490
|
+
const trimmed = paragraph.trim();
|
|
491
|
+
if (!trimmed) {
|
|
492
|
+
lines.push("");
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
let current = "";
|
|
496
|
+
trimmed.split(/\s+/).forEach((word) => {
|
|
497
|
+
if (word.length > safeMaxChars) {
|
|
498
|
+
if (current) {
|
|
499
|
+
lines.push(current);
|
|
500
|
+
current = "";
|
|
501
|
+
}
|
|
502
|
+
splitLongToken(word, safeMaxChars).forEach((chunk) => lines.push(chunk));
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
const next = current ? `${current} ${word}` : word;
|
|
506
|
+
if (next.length <= safeMaxChars) {
|
|
507
|
+
current = next;
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (current) lines.push(current);
|
|
511
|
+
current = word;
|
|
512
|
+
});
|
|
513
|
+
if (current) lines.push(current);
|
|
514
|
+
});
|
|
515
|
+
if (!lines.length) return [""];
|
|
516
|
+
if (lines.length <= 6) return lines;
|
|
517
|
+
const clipped = lines.slice(0, 6);
|
|
518
|
+
clipped[5] = clipped[5].length > safeMaxChars - 3 ? `${clipped[5].slice(0, safeMaxChars - 3)}...` : `${clipped[5]}...`;
|
|
519
|
+
return clipped;
|
|
520
|
+
}
|
|
521
|
+
function estimateChars(width) {
|
|
522
|
+
return Math.max(8, Math.floor((width - 24) / 7));
|
|
523
|
+
}
|
|
524
|
+
function getPortPosition(node, port) {
|
|
525
|
+
switch (port) {
|
|
526
|
+
case "top":
|
|
527
|
+
return { x: node.x + node.width / 2, y: node.y };
|
|
528
|
+
case "right":
|
|
529
|
+
return { x: node.x + node.width, y: node.y + node.height / 2 };
|
|
530
|
+
case "bottom":
|
|
531
|
+
return { x: node.x + node.width / 2, y: node.y + node.height };
|
|
532
|
+
case "left":
|
|
533
|
+
default:
|
|
534
|
+
return { x: node.x, y: node.y + node.height / 2 };
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function getNearestPort(node, point) {
|
|
538
|
+
return FLOWCHART_PORTS.reduce((best, port) => {
|
|
539
|
+
const portPoint = getPortPosition(node, port);
|
|
540
|
+
const distance = Math.hypot(point.x - portPoint.x, point.y - portPoint.y);
|
|
541
|
+
if (!best || distance < best.distance) {
|
|
542
|
+
return { port, point: portPoint, distance };
|
|
543
|
+
}
|
|
544
|
+
return best;
|
|
545
|
+
}, null);
|
|
546
|
+
}
|
|
547
|
+
function getPortNormal(port) {
|
|
548
|
+
switch (port) {
|
|
549
|
+
case "top":
|
|
550
|
+
return { x: 0, y: -1 };
|
|
551
|
+
case "right":
|
|
552
|
+
return { x: 1, y: 0 };
|
|
553
|
+
case "bottom":
|
|
554
|
+
return { x: 0, y: 1 };
|
|
555
|
+
case "left":
|
|
556
|
+
default:
|
|
557
|
+
return { x: -1, y: 0 };
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
function isPointInsideNode(node, point) {
|
|
561
|
+
return point.x >= node.x && point.x <= node.x + node.width && point.y >= node.y && point.y <= node.y + node.height;
|
|
562
|
+
}
|
|
563
|
+
function getPortByDirection(node, point) {
|
|
564
|
+
const centerX = node.x + node.width / 2;
|
|
565
|
+
const centerY = node.y + node.height / 2;
|
|
566
|
+
const deltaX = point.x - centerX;
|
|
567
|
+
const deltaY = point.y - centerY;
|
|
568
|
+
const port = Math.abs(deltaX) > Math.abs(deltaY) ? deltaX > 0 ? "right" : "left" : deltaY > 0 ? "bottom" : "top";
|
|
569
|
+
const portPoint = getPortPosition(node, port);
|
|
570
|
+
return {
|
|
571
|
+
port,
|
|
572
|
+
point: portPoint,
|
|
573
|
+
distance: Math.hypot(point.x - portPoint.x, point.y - portPoint.y)
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function pickPortForNode(node, point, referencePoint = null) {
|
|
577
|
+
if (isPointInsideNode(node, point)) {
|
|
578
|
+
const centerX = node.x + node.width / 2;
|
|
579
|
+
const centerY = node.y + node.height / 2;
|
|
580
|
+
const distanceFromCenter = Math.hypot(point.x - centerX, point.y - centerY);
|
|
581
|
+
if (referencePoint && distanceFromCenter <= CONNECTION_CENTER_LOCK_RADIUS) {
|
|
582
|
+
return getPortByDirection(node, referencePoint);
|
|
583
|
+
}
|
|
584
|
+
return getPortByDirection(node, point);
|
|
585
|
+
}
|
|
586
|
+
return getNearestPort(node, point);
|
|
587
|
+
}
|
|
588
|
+
function getDistanceToNodeBounds(node, point) {
|
|
589
|
+
const left = node.x;
|
|
590
|
+
const right = node.x + node.width;
|
|
591
|
+
const top = node.y;
|
|
592
|
+
const bottom = node.y + node.height;
|
|
593
|
+
const dx = point.x < left ? left - point.x : point.x > right ? point.x - right : 0;
|
|
594
|
+
const dy = point.y < top ? top - point.y : point.y > bottom ? point.y - bottom : 0;
|
|
595
|
+
return Math.hypot(dx, dy);
|
|
596
|
+
}
|
|
597
|
+
function offsetPoint(point, normal, distance) {
|
|
598
|
+
return {
|
|
599
|
+
x: point.x + normal.x * distance,
|
|
600
|
+
y: point.y + normal.y * distance
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function isHorizontalPort(port) {
|
|
604
|
+
return port === "left" || port === "right";
|
|
605
|
+
}
|
|
606
|
+
function addDistinctPoint(points, point) {
|
|
607
|
+
const previous = points[points.length - 1];
|
|
608
|
+
if (!previous || previous.x !== point.x || previous.y !== point.y) {
|
|
609
|
+
points.push(point);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function buildPathFromPoints(points) {
|
|
613
|
+
return points.map((point, index) => {
|
|
614
|
+
const command = index === 0 ? "M" : "L";
|
|
615
|
+
return `${command} ${formatNumber(point.x)} ${formatNumber(point.y)}`;
|
|
616
|
+
}).join(" ");
|
|
617
|
+
}
|
|
618
|
+
function buildRoundedPath(points, radius) {
|
|
619
|
+
if (points.length < 3 || radius <= 0) {
|
|
620
|
+
return buildPathFromPoints(points);
|
|
621
|
+
}
|
|
622
|
+
let d = `M ${formatNumber(points[0].x)} ${formatNumber(points[0].y)}`;
|
|
623
|
+
for (let index = 1; index < points.length - 1; index += 1) {
|
|
624
|
+
const prev = points[index - 1];
|
|
625
|
+
const corner = points[index];
|
|
626
|
+
const next = points[index + 1];
|
|
627
|
+
const lenIn = Math.hypot(corner.x - prev.x, corner.y - prev.y);
|
|
628
|
+
const lenOut = Math.hypot(next.x - corner.x, next.y - corner.y);
|
|
629
|
+
const r = Math.min(radius, lenIn / 2, lenOut / 2);
|
|
630
|
+
if (!(r > 0)) {
|
|
631
|
+
d += ` L ${formatNumber(corner.x)} ${formatNumber(corner.y)}`;
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
const approach = {
|
|
635
|
+
x: corner.x - (corner.x - prev.x) / lenIn * r,
|
|
636
|
+
y: corner.y - (corner.y - prev.y) / lenIn * r
|
|
637
|
+
};
|
|
638
|
+
const departure = {
|
|
639
|
+
x: corner.x + (next.x - corner.x) / lenOut * r,
|
|
640
|
+
y: corner.y + (next.y - corner.y) / lenOut * r
|
|
641
|
+
};
|
|
642
|
+
d += ` L ${formatNumber(approach.x)} ${formatNumber(approach.y)}`;
|
|
643
|
+
d += ` Q ${formatNumber(corner.x)} ${formatNumber(corner.y)} ${formatNumber(departure.x)} ${formatNumber(departure.y)}`;
|
|
644
|
+
}
|
|
645
|
+
const last = points[points.length - 1];
|
|
646
|
+
d += ` L ${formatNumber(last.x)} ${formatNumber(last.y)}`;
|
|
647
|
+
return d;
|
|
648
|
+
}
|
|
649
|
+
function getPolylineLabelPoint(points) {
|
|
650
|
+
let totalLength = 0;
|
|
651
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
652
|
+
totalLength += Math.hypot(
|
|
653
|
+
points[index].x - points[index - 1].x,
|
|
654
|
+
points[index].y - points[index - 1].y
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
if (!totalLength) {
|
|
658
|
+
const first = points[0] || { x: 0, y: 0 };
|
|
659
|
+
return { x: formatNumber(first.x), y: formatNumber(first.y) };
|
|
660
|
+
}
|
|
661
|
+
const halfway = totalLength / 2;
|
|
662
|
+
let covered = 0;
|
|
663
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
664
|
+
const start = points[index - 1];
|
|
665
|
+
const end = points[index];
|
|
666
|
+
const segmentLength = Math.hypot(end.x - start.x, end.y - start.y);
|
|
667
|
+
if (!segmentLength) continue;
|
|
668
|
+
if (covered + segmentLength >= halfway) {
|
|
669
|
+
const ratio = (halfway - covered) / segmentLength;
|
|
670
|
+
return {
|
|
671
|
+
x: formatNumber(start.x + (end.x - start.x) * ratio),
|
|
672
|
+
y: formatNumber(start.y + (end.y - start.y) * ratio)
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
covered += segmentLength;
|
|
676
|
+
}
|
|
677
|
+
const last = points[points.length - 1] || { x: 0, y: 0 };
|
|
678
|
+
return { x: formatNumber(last.x), y: formatNumber(last.y) };
|
|
679
|
+
}
|
|
680
|
+
function curveControlPoint(point, port, target, curvature) {
|
|
681
|
+
const normal = getPortNormal(port);
|
|
682
|
+
const dx = target.x - point.x;
|
|
683
|
+
const dy = target.y - point.y;
|
|
684
|
+
const distance = Math.hypot(dx, dy) || 1;
|
|
685
|
+
const align = (normal.x * dx + normal.y * dy) / distance;
|
|
686
|
+
const ease = 0.55 + 0.45 * align;
|
|
687
|
+
const arm = clamp(distance * curvature * ease, MIN_CURVE_ARM, MAX_CURVE_ARM);
|
|
688
|
+
return { x: point.x + normal.x * arm, y: point.y + normal.y * arm };
|
|
689
|
+
}
|
|
690
|
+
function buildCurvePath(fromPoint, toPoint, fromPort = "right", toPort = "left") {
|
|
691
|
+
const controlA = curveControlPoint(fromPoint, fromPort, toPoint, EDGE_CURVATURE);
|
|
692
|
+
const controlB = curveControlPoint(toPoint, toPort, fromPoint, EDGE_CURVATURE);
|
|
693
|
+
const labelX = 0.125 * fromPoint.x + 0.375 * controlA.x + 0.375 * controlB.x + 0.125 * toPoint.x;
|
|
694
|
+
const labelY = 0.125 * fromPoint.y + 0.375 * controlA.y + 0.375 * controlB.y + 0.125 * toPoint.y;
|
|
695
|
+
return {
|
|
696
|
+
d: `M ${formatNumber(fromPoint.x)} ${formatNumber(fromPoint.y)} C ${formatNumber(controlA.x)} ${formatNumber(controlA.y)} ${formatNumber(controlB.x)} ${formatNumber(controlB.y)} ${formatNumber(toPoint.x)} ${formatNumber(toPoint.y)}`,
|
|
697
|
+
labelX: formatNumber(labelX),
|
|
698
|
+
labelY: formatNumber(labelY)
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
function buildStraightPath(fromPoint, toPoint) {
|
|
702
|
+
return {
|
|
703
|
+
d: buildPathFromPoints([fromPoint, toPoint]),
|
|
704
|
+
labelX: formatNumber((fromPoint.x + toPoint.x) / 2),
|
|
705
|
+
labelY: formatNumber((fromPoint.y + toPoint.y) / 2)
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
function getNodeRectBounds(node, pad = 0) {
|
|
709
|
+
if (!node) return null;
|
|
710
|
+
return {
|
|
711
|
+
left: node.x - pad,
|
|
712
|
+
top: node.y - pad,
|
|
713
|
+
right: node.x + node.width + pad,
|
|
714
|
+
bottom: node.y + node.height + pad
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
function segmentIntersectsRect(a, b, rect) {
|
|
718
|
+
if (!rect) return false;
|
|
719
|
+
const minX = Math.min(a.x, b.x);
|
|
720
|
+
const maxX = Math.max(a.x, b.x);
|
|
721
|
+
const minY = Math.min(a.y, b.y);
|
|
722
|
+
const maxY = Math.max(a.y, b.y);
|
|
723
|
+
return minX < rect.right && maxX > rect.left && minY < rect.bottom && maxY > rect.top;
|
|
724
|
+
}
|
|
725
|
+
function pickOrthogonalChannel(from, to, low, high) {
|
|
726
|
+
const costLow = Math.abs(low - from) + Math.abs(low - to);
|
|
727
|
+
const costHigh = Math.abs(high - from) + Math.abs(high - to);
|
|
728
|
+
if (costLow < costHigh) return low;
|
|
729
|
+
if (costHigh < costLow) return high;
|
|
730
|
+
return to >= from ? high : low;
|
|
731
|
+
}
|
|
732
|
+
function getOrthogonalMidpoints(fromStub, toStub, fromPort, toPort) {
|
|
733
|
+
const sourceHorizontal = isHorizontalPort(fromPort);
|
|
734
|
+
const targetHorizontal = isHorizontalPort(toPort);
|
|
735
|
+
if (sourceHorizontal && targetHorizontal) {
|
|
736
|
+
const midX = (fromStub.x + toStub.x) / 2;
|
|
737
|
+
return [
|
|
738
|
+
{ x: midX, y: fromStub.y },
|
|
739
|
+
{ x: midX, y: toStub.y }
|
|
740
|
+
];
|
|
741
|
+
}
|
|
742
|
+
if (!sourceHorizontal && !targetHorizontal) {
|
|
743
|
+
const midY = (fromStub.y + toStub.y) / 2;
|
|
744
|
+
return [
|
|
745
|
+
{ x: fromStub.x, y: midY },
|
|
746
|
+
{ x: toStub.x, y: midY }
|
|
747
|
+
];
|
|
748
|
+
}
|
|
749
|
+
if (sourceHorizontal) {
|
|
750
|
+
return [{ x: toStub.x, y: fromStub.y }];
|
|
751
|
+
}
|
|
752
|
+
return [{ x: fromStub.x, y: toStub.y }];
|
|
753
|
+
}
|
|
754
|
+
function getOrthogonalPoints(fromPoint, toPoint, fromPort, toPort, fromRect, toRect) {
|
|
755
|
+
const fromNormal = getPortNormal(fromPort);
|
|
756
|
+
const toNormal = getPortNormal(toPort);
|
|
757
|
+
const fromStub = offsetPoint(fromPoint, fromNormal, ORTHOGONAL_STUB_LENGTH);
|
|
758
|
+
const toStub = offsetPoint(toPoint, toNormal, ORTHOGONAL_STUB_LENGTH);
|
|
759
|
+
const sourceHorizontal = isHorizontalPort(fromPort);
|
|
760
|
+
const targetHorizontal = isHorizontalPort(toPort);
|
|
761
|
+
const mids = [];
|
|
762
|
+
if (sourceHorizontal && targetHorizontal) {
|
|
763
|
+
const facing = fromNormal.x === -toNormal.x;
|
|
764
|
+
const hasRoom = fromNormal.x > 0 ? fromStub.x <= toStub.x : fromStub.x >= toStub.x;
|
|
765
|
+
if (facing && hasRoom) {
|
|
766
|
+
const midX = (fromStub.x + toStub.x) / 2;
|
|
767
|
+
mids.push({ x: midX, y: fromStub.y }, { x: midX, y: toStub.y });
|
|
768
|
+
} else {
|
|
769
|
+
const top = Math.min(fromRect.top, toRect.top);
|
|
770
|
+
const bottom = Math.max(fromRect.bottom, toRect.bottom);
|
|
771
|
+
const yChannel = pickOrthogonalChannel(fromStub.y, toStub.y, top, bottom);
|
|
772
|
+
mids.push({ x: fromStub.x, y: yChannel }, { x: toStub.x, y: yChannel });
|
|
773
|
+
}
|
|
774
|
+
} else if (!sourceHorizontal && !targetHorizontal) {
|
|
775
|
+
const facing = fromNormal.y === -toNormal.y;
|
|
776
|
+
const hasRoom = fromNormal.y > 0 ? fromStub.y <= toStub.y : fromStub.y >= toStub.y;
|
|
777
|
+
if (facing && hasRoom) {
|
|
778
|
+
const midY = (fromStub.y + toStub.y) / 2;
|
|
779
|
+
mids.push({ x: fromStub.x, y: midY }, { x: toStub.x, y: midY });
|
|
780
|
+
} else {
|
|
781
|
+
const left = Math.min(fromRect.left, toRect.left);
|
|
782
|
+
const right = Math.max(fromRect.right, toRect.right);
|
|
783
|
+
const xChannel = pickOrthogonalChannel(fromStub.x, toStub.x, left, right);
|
|
784
|
+
mids.push({ x: xChannel, y: fromStub.y }, { x: xChannel, y: toStub.y });
|
|
785
|
+
}
|
|
786
|
+
} else {
|
|
787
|
+
const cleanCorner = sourceHorizontal ? { x: toStub.x, y: fromStub.y } : { x: fromStub.x, y: toStub.y };
|
|
788
|
+
const altCorner = sourceHorizontal ? { x: fromStub.x, y: toStub.y } : { x: toStub.x, y: fromStub.y };
|
|
789
|
+
const legsClear = (corner) => !segmentIntersectsRect(fromStub, corner, fromRect) && !segmentIntersectsRect(fromStub, corner, toRect) && !segmentIntersectsRect(corner, toStub, fromRect) && !segmentIntersectsRect(corner, toStub, toRect);
|
|
790
|
+
mids.push(legsClear(cleanCorner) || !legsClear(altCorner) ? cleanCorner : altCorner);
|
|
791
|
+
}
|
|
792
|
+
const points = [];
|
|
793
|
+
[fromPoint, fromStub, ...mids, toStub, toPoint].forEach(
|
|
794
|
+
(point) => addDistinctPoint(points, point)
|
|
795
|
+
);
|
|
796
|
+
return points;
|
|
797
|
+
}
|
|
798
|
+
function buildOrthogonalPath(fromPoint, toPoint, fromPort = "right", toPort = "left", fromRect = null, toRect = null) {
|
|
799
|
+
let points;
|
|
800
|
+
if (fromRect && toRect) {
|
|
801
|
+
points = getOrthogonalPoints(fromPoint, toPoint, fromPort, toPort, fromRect, toRect);
|
|
802
|
+
} else {
|
|
803
|
+
const fromStub = offsetPoint(fromPoint, getPortNormal(fromPort), ORTHOGONAL_STUB_LENGTH);
|
|
804
|
+
const toStub = offsetPoint(toPoint, getPortNormal(toPort), ORTHOGONAL_STUB_LENGTH);
|
|
805
|
+
points = [];
|
|
806
|
+
[
|
|
807
|
+
fromPoint,
|
|
808
|
+
fromStub,
|
|
809
|
+
...getOrthogonalMidpoints(fromStub, toStub, fromPort, toPort),
|
|
810
|
+
toStub,
|
|
811
|
+
toPoint
|
|
812
|
+
].forEach((point) => addDistinctPoint(points, point));
|
|
813
|
+
}
|
|
814
|
+
const label = getPolylineLabelPoint(points);
|
|
815
|
+
return {
|
|
816
|
+
d: buildRoundedPath(points, ORTHOGONAL_CORNER_RADIUS),
|
|
817
|
+
labelX: label.x,
|
|
818
|
+
labelY: label.y
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
function buildEdgePath(edge, fromNode = null, toNode = null) {
|
|
822
|
+
const fromPort = edge?.from?.port || edge?.fromPort || "right";
|
|
823
|
+
const toPort = edge?.to?.port || edge?.toPort || "left";
|
|
824
|
+
const fromPoint = edge?.fromPoint || (fromNode ? getPortPosition(fromNode, fromPort) : null);
|
|
825
|
+
const toPoint = edge?.toPoint || (toNode ? getPortPosition(toNode, toPort) : null);
|
|
826
|
+
const route = normalizeEdgeRoute(edge?.route);
|
|
827
|
+
if (!fromPoint || !toPoint) {
|
|
828
|
+
return { d: "", labelX: 0, labelY: 0 };
|
|
829
|
+
}
|
|
830
|
+
if (route === "straight") {
|
|
831
|
+
return buildStraightPath(fromPoint, toPoint);
|
|
832
|
+
}
|
|
833
|
+
if (route === "orthogonal") {
|
|
834
|
+
const fromRect = !edge?.fromPoint && fromNode ? getNodeRectBounds(fromNode, ORTHOGONAL_CLEARANCE) : null;
|
|
835
|
+
const toRect = !edge?.toPoint && toNode ? getNodeRectBounds(toNode, ORTHOGONAL_CLEARANCE) : null;
|
|
836
|
+
return buildOrthogonalPath(fromPoint, toPoint, fromPort, toPort, fromRect, toRect);
|
|
837
|
+
}
|
|
838
|
+
return buildCurvePath(fromPoint, toPoint, fromPort, toPort);
|
|
839
|
+
}
|
|
840
|
+
function createArrowMarker(id, strokeWidth, reversed = false) {
|
|
841
|
+
const size = formatNumber(Math.max(9, strokeWidth * 4.4));
|
|
842
|
+
const refInset = formatNumber(Math.max(1.5, strokeWidth * 0.85));
|
|
843
|
+
const marker = svgEl("marker", {
|
|
844
|
+
id,
|
|
845
|
+
markerWidth: size,
|
|
846
|
+
markerHeight: size,
|
|
847
|
+
refX: reversed ? refInset : formatNumber(size - refInset),
|
|
848
|
+
refY: formatNumber(size / 2),
|
|
849
|
+
orient: "auto",
|
|
850
|
+
markerUnits: "userSpaceOnUse"
|
|
851
|
+
});
|
|
852
|
+
marker.appendChild(
|
|
853
|
+
svgEl("path", {
|
|
854
|
+
d: reversed ? `M ${formatNumber(size)} 0 L 0 ${formatNumber(size / 2)} L ${formatNumber(size)} ${formatNumber(size)} z` : `M 0 0 L ${formatNumber(size)} ${formatNumber(size / 2)} L 0 ${formatNumber(size)} z`,
|
|
855
|
+
fill: "var(--vd-flowchart-accent)"
|
|
856
|
+
})
|
|
857
|
+
);
|
|
858
|
+
return marker;
|
|
859
|
+
}
|
|
860
|
+
function createDotMarker(id, strokeWidth) {
|
|
861
|
+
const size = formatNumber(Math.max(8, strokeWidth * 3.3));
|
|
862
|
+
const radius = formatNumber(Math.max(2.75, strokeWidth * 1.45));
|
|
863
|
+
const marker = svgEl("marker", {
|
|
864
|
+
id,
|
|
865
|
+
markerWidth: size,
|
|
866
|
+
markerHeight: size,
|
|
867
|
+
refX: formatNumber(size / 2),
|
|
868
|
+
refY: formatNumber(size / 2),
|
|
869
|
+
orient: "auto",
|
|
870
|
+
markerUnits: "userSpaceOnUse"
|
|
871
|
+
});
|
|
872
|
+
marker.appendChild(
|
|
873
|
+
svgEl("circle", {
|
|
874
|
+
cx: formatNumber(size / 2),
|
|
875
|
+
cy: formatNumber(size / 2),
|
|
876
|
+
r: radius,
|
|
877
|
+
fill: "var(--vd-flowchart-accent)"
|
|
878
|
+
})
|
|
879
|
+
);
|
|
880
|
+
return marker;
|
|
881
|
+
}
|
|
882
|
+
function getNodeFontMetrics(node) {
|
|
883
|
+
if (node.type === "label") {
|
|
884
|
+
return { fontSize: 18, lineHeight: 20 };
|
|
885
|
+
}
|
|
886
|
+
if (node.type === "textbox") {
|
|
887
|
+
return { fontSize: 13, lineHeight: 18 };
|
|
888
|
+
}
|
|
889
|
+
return { fontSize: 14, lineHeight: 18 };
|
|
890
|
+
}
|
|
891
|
+
function getResizeHandlePosition(node, handle) {
|
|
892
|
+
const middleX = node.width / 2;
|
|
893
|
+
const middleY = node.height / 2;
|
|
894
|
+
const x = handle.includes("w") ? 0 : handle.includes("e") ? node.width : middleX;
|
|
895
|
+
const y = handle.includes("n") ? 0 : handle.includes("s") ? node.height : middleY;
|
|
896
|
+
return { x, y };
|
|
897
|
+
}
|
|
898
|
+
function getResizeCursor(handle) {
|
|
899
|
+
if (handle === "n" || handle === "s") return "ns-resize";
|
|
900
|
+
if (handle === "e" || handle === "w") return "ew-resize";
|
|
901
|
+
if (handle === "ne" || handle === "sw") return "nesw-resize";
|
|
902
|
+
return "nwse-resize";
|
|
903
|
+
}
|
|
904
|
+
function getBounds(nodes) {
|
|
905
|
+
if (!nodes.length) {
|
|
906
|
+
return { left: 0, top: 0, right: 0, bottom: 0 };
|
|
907
|
+
}
|
|
908
|
+
return nodes.reduce(
|
|
909
|
+
(accumulator, node) => ({
|
|
910
|
+
left: Math.min(accumulator.left, node.x),
|
|
911
|
+
top: Math.min(accumulator.top, node.y),
|
|
912
|
+
right: Math.max(accumulator.right, node.x + node.width),
|
|
913
|
+
bottom: Math.max(accumulator.bottom, node.y + node.height)
|
|
914
|
+
}),
|
|
915
|
+
{
|
|
916
|
+
left: Number.POSITIVE_INFINITY,
|
|
917
|
+
top: Number.POSITIVE_INFINITY,
|
|
918
|
+
right: Number.NEGATIVE_INFINITY,
|
|
919
|
+
bottom: Number.NEGATIVE_INFINITY
|
|
920
|
+
}
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
function createField(labelText, control) {
|
|
924
|
+
const wrapper = createElement("div", { className: "vd-flowchart-field" });
|
|
925
|
+
const label = createElement("label", { text: labelText });
|
|
926
|
+
wrapper.appendChild(label);
|
|
927
|
+
wrapper.appendChild(control);
|
|
928
|
+
return wrapper;
|
|
929
|
+
}
|
|
930
|
+
function createPalettePreview(type) {
|
|
931
|
+
const preview = svgEl("svg", {
|
|
932
|
+
class: `vd-flowchart-palette-preview vd-flowchart-palette-preview--${type}`,
|
|
933
|
+
viewBox: "0 0 72 44",
|
|
934
|
+
"aria-hidden": "true",
|
|
935
|
+
focusable: "false"
|
|
936
|
+
});
|
|
937
|
+
const baseClass = `vd-flowchart-palette-shape vd-flowchart-palette-shape--${type}`;
|
|
938
|
+
if (type === "arrow") {
|
|
939
|
+
preview.appendChild(
|
|
940
|
+
svgEl("path", {
|
|
941
|
+
class: "vd-flowchart-palette-arrow",
|
|
942
|
+
d: "M 12 30 C 28 10 44 10 60 22"
|
|
943
|
+
})
|
|
944
|
+
);
|
|
945
|
+
preview.appendChild(
|
|
946
|
+
svgEl("path", {
|
|
947
|
+
class: "vd-flowchart-palette-arrowhead",
|
|
948
|
+
d: "M 53 16 L 64 22 L 52 27 z"
|
|
949
|
+
})
|
|
950
|
+
);
|
|
951
|
+
return preview;
|
|
952
|
+
}
|
|
953
|
+
if (type === "rounded-rect") {
|
|
954
|
+
preview.appendChild(
|
|
955
|
+
svgEl("rect", {
|
|
956
|
+
class: baseClass,
|
|
957
|
+
x: 10,
|
|
958
|
+
y: 10,
|
|
959
|
+
width: 52,
|
|
960
|
+
height: 24,
|
|
961
|
+
rx: 8,
|
|
962
|
+
ry: 8
|
|
963
|
+
})
|
|
964
|
+
);
|
|
965
|
+
return preview;
|
|
966
|
+
}
|
|
967
|
+
if (type === "rect") {
|
|
968
|
+
preview.appendChild(
|
|
969
|
+
svgEl("rect", {
|
|
970
|
+
class: baseClass,
|
|
971
|
+
x: 10,
|
|
972
|
+
y: 10,
|
|
973
|
+
width: 52,
|
|
974
|
+
height: 24,
|
|
975
|
+
rx: 1,
|
|
976
|
+
ry: 1
|
|
977
|
+
})
|
|
978
|
+
);
|
|
979
|
+
return preview;
|
|
980
|
+
}
|
|
981
|
+
if (type === "diamond") {
|
|
982
|
+
preview.appendChild(
|
|
983
|
+
svgEl("polygon", {
|
|
984
|
+
class: baseClass,
|
|
985
|
+
points: "36,6 64,22 36,38 8,22"
|
|
986
|
+
})
|
|
987
|
+
);
|
|
988
|
+
return preview;
|
|
989
|
+
}
|
|
990
|
+
if (type === "circle") {
|
|
991
|
+
preview.appendChild(
|
|
992
|
+
svgEl("ellipse", {
|
|
993
|
+
class: baseClass,
|
|
994
|
+
cx: 36,
|
|
995
|
+
cy: 22,
|
|
996
|
+
rx: 18,
|
|
997
|
+
ry: 18
|
|
998
|
+
})
|
|
999
|
+
);
|
|
1000
|
+
return preview;
|
|
1001
|
+
}
|
|
1002
|
+
if (type === "junction") {
|
|
1003
|
+
preview.appendChild(
|
|
1004
|
+
svgEl("path", {
|
|
1005
|
+
class: "vd-flowchart-palette-junction-lines",
|
|
1006
|
+
d: "M 12 22 H 27 M 45 22 H 60 M 36 8 V 14 M 36 30 V 36"
|
|
1007
|
+
})
|
|
1008
|
+
);
|
|
1009
|
+
preview.appendChild(
|
|
1010
|
+
svgEl("circle", {
|
|
1011
|
+
class: baseClass,
|
|
1012
|
+
cx: 36,
|
|
1013
|
+
cy: 22,
|
|
1014
|
+
r: 8
|
|
1015
|
+
})
|
|
1016
|
+
);
|
|
1017
|
+
return preview;
|
|
1018
|
+
}
|
|
1019
|
+
if (type === "textbox") {
|
|
1020
|
+
preview.appendChild(
|
|
1021
|
+
svgEl("rect", {
|
|
1022
|
+
class: baseClass,
|
|
1023
|
+
x: 10,
|
|
1024
|
+
y: 8,
|
|
1025
|
+
width: 52,
|
|
1026
|
+
height: 28,
|
|
1027
|
+
rx: 6,
|
|
1028
|
+
ry: 6
|
|
1029
|
+
})
|
|
1030
|
+
);
|
|
1031
|
+
preview.appendChild(
|
|
1032
|
+
svgEl("path", {
|
|
1033
|
+
class: "vd-flowchart-palette-lines",
|
|
1034
|
+
d: "M 20 18 H 52 M 20 25 H 46"
|
|
1035
|
+
})
|
|
1036
|
+
);
|
|
1037
|
+
return preview;
|
|
1038
|
+
}
|
|
1039
|
+
const label = svgEl("text", {
|
|
1040
|
+
class: "vd-flowchart-palette-label-mark",
|
|
1041
|
+
x: 36,
|
|
1042
|
+
y: 24,
|
|
1043
|
+
"text-anchor": "middle",
|
|
1044
|
+
"dominant-baseline": "middle"
|
|
1045
|
+
});
|
|
1046
|
+
label.textContent = "Aa";
|
|
1047
|
+
preview.appendChild(label);
|
|
1048
|
+
return preview;
|
|
1049
|
+
}
|
|
1050
|
+
var VdFlowchart = class {
|
|
1051
|
+
constructor(options = {}) {
|
|
1052
|
+
this.element = resolveElement(options.element || options.target);
|
|
1053
|
+
this.readonly = Boolean(options.readonly);
|
|
1054
|
+
this.gridSize = clamp(toFiniteNumber(options.gridSize, DEFAULT_GRID_SIZE), 12, 64);
|
|
1055
|
+
this.documentData = normalizeDocument(options.data || {});
|
|
1056
|
+
this.listeners = {};
|
|
1057
|
+
this.selection = null;
|
|
1058
|
+
this.interaction = null;
|
|
1059
|
+
this.activeTool = null;
|
|
1060
|
+
this.paletteSerial = 0;
|
|
1061
|
+
this.destroyed = false;
|
|
1062
|
+
this.lastNodePointer = null;
|
|
1063
|
+
this.reconnectEdgeId = null;
|
|
1064
|
+
this.clipboard = null;
|
|
1065
|
+
this.gridPatternId = nextId("flowchart-grid");
|
|
1066
|
+
this.markerIds = /* @__PURE__ */ new Map();
|
|
1067
|
+
this.textEditor = null;
|
|
1068
|
+
this.historyEnabled = options.history !== false;
|
|
1069
|
+
this.historyLimit = Math.max(1, Math.floor(toFiniteNumber(options.historyLimit, 100)));
|
|
1070
|
+
this.history = [];
|
|
1071
|
+
this.historyIndex = -1;
|
|
1072
|
+
this.isApplyingHistory = false;
|
|
1073
|
+
this.autoFit = Boolean(options.autoFit);
|
|
1074
|
+
this.readyEmitted = false;
|
|
1075
|
+
this.resizeObserver = null;
|
|
1076
|
+
this.handleToolbarClick = this.handleToolbarClick.bind(this);
|
|
1077
|
+
this.handleArrangeChange = this.handleArrangeChange.bind(this);
|
|
1078
|
+
this.handlePaletteClick = this.handlePaletteClick.bind(this);
|
|
1079
|
+
this.handlePointerDown = this.handlePointerDown.bind(this);
|
|
1080
|
+
this.handlePointerMove = this.handlePointerMove.bind(this);
|
|
1081
|
+
this.handlePointerUp = this.handlePointerUp.bind(this);
|
|
1082
|
+
this.handleClick = this.handleClick.bind(this);
|
|
1083
|
+
this.handleDoubleClick = this.handleDoubleClick.bind(this);
|
|
1084
|
+
this.handleWheel = this.handleWheel.bind(this);
|
|
1085
|
+
this.handleKeyDown = this.handleKeyDown.bind(this);
|
|
1086
|
+
this.handleSelectionFieldInput = this.handleSelectionFieldInput.bind(this);
|
|
1087
|
+
this.handleSelectionFieldChange = this.handleSelectionFieldChange.bind(this);
|
|
1088
|
+
this.handleJsonActionClick = this.handleJsonActionClick.bind(this);
|
|
1089
|
+
this.handleResize = this.handleResize.bind(this);
|
|
1090
|
+
this.buildShell();
|
|
1091
|
+
this.bindEvents();
|
|
1092
|
+
this.render();
|
|
1093
|
+
this.seedHistory();
|
|
1094
|
+
this.updateHistoryButtons();
|
|
1095
|
+
this.scheduleReady();
|
|
1096
|
+
}
|
|
1097
|
+
// --- Readiness -----------------------------------------------------------
|
|
1098
|
+
// The editor builds synchronously, but fitView() needs real canvas
|
|
1099
|
+
// dimensions, which only exist after the host is laid out. Emit `ready` once
|
|
1100
|
+
// the canvas reports a non-zero size (the old code fell back to an 800x560
|
|
1101
|
+
// guess); consumers can then fitView() without nextTick/setTimeout. The emit
|
|
1102
|
+
// is always deferred at least a microtask so a listener attached right after
|
|
1103
|
+
// construction — e.g. `new VdFlowchart(...).on('ready', ...)` or the Vue
|
|
1104
|
+
// wrapper — is registered before it fires.
|
|
1105
|
+
scheduleReady() {
|
|
1106
|
+
if (this.readyEmitted || this.destroyed || !hasWindow()) return;
|
|
1107
|
+
const tryEmit = () => {
|
|
1108
|
+
if (this.readyEmitted || this.destroyed) return true;
|
|
1109
|
+
const ready = this.canvasEl.clientWidth > 0 && this.canvasEl.clientHeight > 0;
|
|
1110
|
+
if (!ready) return false;
|
|
1111
|
+
this.readyEmitted = true;
|
|
1112
|
+
if (this.autoFit) this.fitView();
|
|
1113
|
+
this.emit("ready", this);
|
|
1114
|
+
return true;
|
|
1115
|
+
};
|
|
1116
|
+
const startObserving = () => {
|
|
1117
|
+
if (this.readyEmitted || this.destroyed || tryEmit()) return;
|
|
1118
|
+
if (typeof ResizeObserver === "function") {
|
|
1119
|
+
this.resizeObserver = new ResizeObserver(() => {
|
|
1120
|
+
if (tryEmit()) {
|
|
1121
|
+
this.resizeObserver?.disconnect();
|
|
1122
|
+
this.resizeObserver = null;
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
this.resizeObserver.observe(this.canvasEl);
|
|
1126
|
+
}
|
|
1127
|
+
const raf = typeof requestAnimationFrame === "function" ? requestAnimationFrame : (cb) => setTimeout(cb, 16);
|
|
1128
|
+
raf(() => tryEmit());
|
|
1129
|
+
};
|
|
1130
|
+
if (typeof queueMicrotask === "function") {
|
|
1131
|
+
queueMicrotask(startObserving);
|
|
1132
|
+
} else {
|
|
1133
|
+
Promise.resolve().then(startObserving);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
buildShell() {
|
|
1137
|
+
this.element.innerHTML = "";
|
|
1138
|
+
this.element.classList.add("vd-flowchart-host");
|
|
1139
|
+
this.root = createElement("div", {
|
|
1140
|
+
className: `vd-flowchart-shell${this.readonly ? " vd-flowchart-readonly" : ""}`
|
|
1141
|
+
});
|
|
1142
|
+
this.toolbar = createElement("div", { className: "vd-flowchart-toolbar" });
|
|
1143
|
+
const toolbarLeft = createElement("div", { className: "vd-flowchart-toolbar-group" });
|
|
1144
|
+
const toolbarRight = createElement("div", { className: "vd-flowchart-toolbar-group" });
|
|
1145
|
+
this.zoomOutButton = createElement("button", { className: "vd-flowchart-btn", text: "-" });
|
|
1146
|
+
this.zoomOutButton.setAttribute("data-flowchart-action", "zoom-out");
|
|
1147
|
+
this.zoomOutButton.setAttribute("type", "button");
|
|
1148
|
+
this.zoomInButton = createElement("button", { className: "vd-flowchart-btn", text: "+" });
|
|
1149
|
+
this.zoomInButton.setAttribute("data-flowchart-action", "zoom-in");
|
|
1150
|
+
this.zoomInButton.setAttribute("type", "button");
|
|
1151
|
+
this.resetViewButton = createElement("button", {
|
|
1152
|
+
className: "vd-flowchart-btn",
|
|
1153
|
+
text: "Reset"
|
|
1154
|
+
});
|
|
1155
|
+
this.resetViewButton.setAttribute("data-flowchart-action", "reset-view");
|
|
1156
|
+
this.resetViewButton.setAttribute("type", "button");
|
|
1157
|
+
this.fitViewButton = createElement("button", { className: "vd-flowchart-btn", text: "Fit" });
|
|
1158
|
+
this.fitViewButton.setAttribute("data-flowchart-action", "fit-view");
|
|
1159
|
+
this.fitViewButton.setAttribute("type", "button");
|
|
1160
|
+
this.undoButton = createElement("button", {
|
|
1161
|
+
className: "vd-flowchart-btn",
|
|
1162
|
+
text: "\u21B6",
|
|
1163
|
+
title: "Undo",
|
|
1164
|
+
disabled: true
|
|
1165
|
+
});
|
|
1166
|
+
this.undoButton.setAttribute("data-flowchart-action", "undo");
|
|
1167
|
+
this.undoButton.setAttribute("type", "button");
|
|
1168
|
+
this.undoButton.setAttribute("aria-label", "Undo");
|
|
1169
|
+
this.redoButton = createElement("button", {
|
|
1170
|
+
className: "vd-flowchart-btn",
|
|
1171
|
+
text: "\u21B7",
|
|
1172
|
+
title: "Redo",
|
|
1173
|
+
disabled: true
|
|
1174
|
+
});
|
|
1175
|
+
this.redoButton.setAttribute("data-flowchart-action", "redo");
|
|
1176
|
+
this.redoButton.setAttribute("type", "button");
|
|
1177
|
+
this.redoButton.setAttribute("aria-label", "Redo");
|
|
1178
|
+
this.arrangeSelect = createElement("select", {
|
|
1179
|
+
className: "vd-flowchart-btn vd-flowchart-arrange",
|
|
1180
|
+
disabled: this.readonly
|
|
1181
|
+
});
|
|
1182
|
+
this.arrangeSelect.setAttribute("aria-label", "Auto-arrange layout");
|
|
1183
|
+
this.arrangeSelect.setAttribute("data-flowchart-arrange", "");
|
|
1184
|
+
[
|
|
1185
|
+
{ value: "", label: "Arrange \u25BE" },
|
|
1186
|
+
{ value: "tree", label: "Tree" },
|
|
1187
|
+
{ value: "radial", label: "Radial" },
|
|
1188
|
+
{ value: "grid", label: "Grid" }
|
|
1189
|
+
].forEach((item, index) => {
|
|
1190
|
+
const option = createElement("option", { value: item.value, text: item.label });
|
|
1191
|
+
option.value = item.value;
|
|
1192
|
+
if (index === 0) option.disabled = true;
|
|
1193
|
+
this.arrangeSelect.appendChild(option);
|
|
1194
|
+
});
|
|
1195
|
+
this.arrangeSelect.selectedIndex = 0;
|
|
1196
|
+
this.clearButton = createElement("button", {
|
|
1197
|
+
className: "vd-flowchart-btn",
|
|
1198
|
+
text: "Clear",
|
|
1199
|
+
disabled: this.readonly
|
|
1200
|
+
});
|
|
1201
|
+
this.clearButton.setAttribute("data-flowchart-action", "clear");
|
|
1202
|
+
this.clearButton.setAttribute("type", "button");
|
|
1203
|
+
this.zoomLabel = createElement("span", {
|
|
1204
|
+
className: "vd-flowchart-toolbar-label",
|
|
1205
|
+
text: "100%"
|
|
1206
|
+
});
|
|
1207
|
+
toolbarLeft.appendChild(this.zoomOutButton);
|
|
1208
|
+
toolbarLeft.appendChild(this.zoomInButton);
|
|
1209
|
+
toolbarLeft.appendChild(this.resetViewButton);
|
|
1210
|
+
toolbarLeft.appendChild(this.fitViewButton);
|
|
1211
|
+
toolbarLeft.appendChild(this.undoButton);
|
|
1212
|
+
toolbarLeft.appendChild(this.redoButton);
|
|
1213
|
+
toolbarLeft.appendChild(this.arrangeSelect);
|
|
1214
|
+
toolbarLeft.appendChild(this.clearButton);
|
|
1215
|
+
toolbarRight.appendChild(this.zoomLabel);
|
|
1216
|
+
this.toolbar.appendChild(toolbarLeft);
|
|
1217
|
+
this.toolbar.appendChild(toolbarRight);
|
|
1218
|
+
this.body = createElement("div", { className: "vd-flowchart-body" });
|
|
1219
|
+
this.palettePanel = createElement("aside", {
|
|
1220
|
+
className: "vd-flowchart-panel vd-flowchart-panel--palette"
|
|
1221
|
+
});
|
|
1222
|
+
this.palettePanel.appendChild(
|
|
1223
|
+
createElement("h4", { className: "vd-flowchart-panel-title", text: "Shapes & tools" })
|
|
1224
|
+
);
|
|
1225
|
+
this.paletteGrid = createElement("div", { className: "vd-flowchart-palette" });
|
|
1226
|
+
FLOWCHART_PALETTE_ITEMS.forEach((item) => {
|
|
1227
|
+
const button = createElement("button", {
|
|
1228
|
+
className: "vd-flowchart-palette-btn"
|
|
1229
|
+
});
|
|
1230
|
+
button.setAttribute("type", "button");
|
|
1231
|
+
if (item.kind === "tool") {
|
|
1232
|
+
button.setAttribute("data-tool", item.tool);
|
|
1233
|
+
button.setAttribute("aria-label", `Use ${item.label}`);
|
|
1234
|
+
} else {
|
|
1235
|
+
button.setAttribute("data-node-type", item.type);
|
|
1236
|
+
button.setAttribute("aria-label", `Add ${item.label}`);
|
|
1237
|
+
}
|
|
1238
|
+
button.appendChild(createPalettePreview(item.tool || item.type));
|
|
1239
|
+
button.appendChild(
|
|
1240
|
+
createElement("span", {
|
|
1241
|
+
className: "vd-flowchart-palette-label",
|
|
1242
|
+
text: item.label
|
|
1243
|
+
})
|
|
1244
|
+
);
|
|
1245
|
+
this.paletteGrid.appendChild(button);
|
|
1246
|
+
});
|
|
1247
|
+
this.palettePanel.appendChild(this.paletteGrid);
|
|
1248
|
+
this.canvasEl = createElement("div", { className: "vd-flowchart-canvas", tabIndex: 0 });
|
|
1249
|
+
this.svg = svgEl("svg", {
|
|
1250
|
+
class: "vd-flowchart-svg",
|
|
1251
|
+
role: "img",
|
|
1252
|
+
"aria-label": "Vanduo Flowchart editor"
|
|
1253
|
+
});
|
|
1254
|
+
const defs = svgEl("defs");
|
|
1255
|
+
this.markerDefs = svgEl("g");
|
|
1256
|
+
defs.appendChild(this.markerDefs);
|
|
1257
|
+
const pattern = svgEl("pattern", {
|
|
1258
|
+
id: this.gridPatternId,
|
|
1259
|
+
width: this.gridSize,
|
|
1260
|
+
height: this.gridSize,
|
|
1261
|
+
patternUnits: "userSpaceOnUse"
|
|
1262
|
+
});
|
|
1263
|
+
pattern.appendChild(
|
|
1264
|
+
svgEl("path", {
|
|
1265
|
+
d: `M ${this.gridSize} 0 L 0 0 0 ${this.gridSize}`,
|
|
1266
|
+
fill: "none",
|
|
1267
|
+
stroke: "var(--vd-flowchart-border)",
|
|
1268
|
+
"stroke-opacity": 0.55,
|
|
1269
|
+
"stroke-width": 1
|
|
1270
|
+
})
|
|
1271
|
+
);
|
|
1272
|
+
defs.appendChild(pattern);
|
|
1273
|
+
this.svg.appendChild(defs);
|
|
1274
|
+
this.world = svgEl("g", { class: "vd-flowchart-world" });
|
|
1275
|
+
this.gridRect = svgEl("rect", {
|
|
1276
|
+
class: "vd-flowchart-grid",
|
|
1277
|
+
x: -WORLD_EXTENT / 2,
|
|
1278
|
+
y: -WORLD_EXTENT / 2,
|
|
1279
|
+
width: WORLD_EXTENT,
|
|
1280
|
+
height: WORLD_EXTENT,
|
|
1281
|
+
fill: `url(#${this.gridPatternId})`
|
|
1282
|
+
});
|
|
1283
|
+
this.edgesLayer = svgEl("g", { class: "vd-flowchart-edges" });
|
|
1284
|
+
this.previewLayer = svgEl("g", { class: "vd-flowchart-preview" });
|
|
1285
|
+
this.nodesLayer = svgEl("g", { class: "vd-flowchart-nodes" });
|
|
1286
|
+
this.overlayLayer = svgEl("g", { class: "vd-flowchart-overlay" });
|
|
1287
|
+
this.world.appendChild(this.gridRect);
|
|
1288
|
+
this.world.appendChild(this.edgesLayer);
|
|
1289
|
+
this.world.appendChild(this.previewLayer);
|
|
1290
|
+
this.world.appendChild(this.nodesLayer);
|
|
1291
|
+
this.world.appendChild(this.overlayLayer);
|
|
1292
|
+
this.svg.appendChild(this.world);
|
|
1293
|
+
this.canvasEl.appendChild(this.svg);
|
|
1294
|
+
this.inspectorPanel = createElement("aside", {
|
|
1295
|
+
className: "vd-flowchart-panel vd-flowchart-panel--inspector"
|
|
1296
|
+
});
|
|
1297
|
+
this.inspectorPanel.appendChild(
|
|
1298
|
+
createElement("h4", { className: "vd-flowchart-panel-title", text: "Inspector" })
|
|
1299
|
+
);
|
|
1300
|
+
this.selectionMeta = createElement("div", { className: "vd-flowchart-selection-meta" });
|
|
1301
|
+
this.selectionFields = createElement("div", { className: "vd-flowchart-fields" });
|
|
1302
|
+
this.deleteButton = createElement("button", {
|
|
1303
|
+
className: "vd-flowchart-btn vd-flowchart-delete",
|
|
1304
|
+
text: "Delete",
|
|
1305
|
+
disabled: true
|
|
1306
|
+
});
|
|
1307
|
+
this.deleteButton.setAttribute("type", "button");
|
|
1308
|
+
this.inspectorPanel.appendChild(this.selectionMeta);
|
|
1309
|
+
this.inspectorPanel.appendChild(this.selectionFields);
|
|
1310
|
+
this.inspectorPanel.appendChild(this.deleteButton);
|
|
1311
|
+
this.inspectorPanel.appendChild(
|
|
1312
|
+
createElement("h4", { className: "vd-flowchart-panel-title", text: "JSON" })
|
|
1313
|
+
);
|
|
1314
|
+
this.jsonPanel = createElement("div", { className: "vd-flowchart-json" });
|
|
1315
|
+
this.jsonTextarea = createElement("textarea", { rows: 18 });
|
|
1316
|
+
this.jsonActions = createElement("div", { className: "vd-flowchart-json-actions" });
|
|
1317
|
+
this.refreshJsonButton = createElement("button", {
|
|
1318
|
+
className: "vd-flowchart-json-btn",
|
|
1319
|
+
text: "Refresh"
|
|
1320
|
+
});
|
|
1321
|
+
this.refreshJsonButton.setAttribute("type", "button");
|
|
1322
|
+
this.refreshJsonButton.setAttribute("data-json-action", "refresh");
|
|
1323
|
+
this.loadJsonButton = createElement("button", {
|
|
1324
|
+
className: "vd-flowchart-json-btn",
|
|
1325
|
+
text: "Load",
|
|
1326
|
+
disabled: this.readonly
|
|
1327
|
+
});
|
|
1328
|
+
this.loadJsonButton.setAttribute("type", "button");
|
|
1329
|
+
this.loadJsonButton.setAttribute("data-json-action", "load");
|
|
1330
|
+
this.jsonActions.appendChild(this.refreshJsonButton);
|
|
1331
|
+
this.jsonActions.appendChild(this.loadJsonButton);
|
|
1332
|
+
this.jsonPanel.appendChild(this.jsonTextarea);
|
|
1333
|
+
this.jsonPanel.appendChild(this.jsonActions);
|
|
1334
|
+
this.inspectorPanel.appendChild(this.jsonPanel);
|
|
1335
|
+
this.body.appendChild(this.palettePanel);
|
|
1336
|
+
this.body.appendChild(this.canvasEl);
|
|
1337
|
+
this.body.appendChild(this.inspectorPanel);
|
|
1338
|
+
this.root.appendChild(this.toolbar);
|
|
1339
|
+
this.root.appendChild(this.body);
|
|
1340
|
+
this.element.appendChild(this.root);
|
|
1341
|
+
}
|
|
1342
|
+
bindEvents() {
|
|
1343
|
+
this.toolbar.addEventListener("click", this.handleToolbarClick);
|
|
1344
|
+
this.arrangeSelect.addEventListener("change", this.handleArrangeChange);
|
|
1345
|
+
this.paletteGrid.addEventListener("click", this.handlePaletteClick);
|
|
1346
|
+
this.deleteButton.addEventListener("click", () => this.deleteSelection());
|
|
1347
|
+
this.selectionFields.addEventListener("input", this.handleSelectionFieldInput);
|
|
1348
|
+
this.selectionFields.addEventListener("change", this.handleSelectionFieldChange);
|
|
1349
|
+
this.jsonActions.addEventListener("click", this.handleJsonActionClick);
|
|
1350
|
+
this.canvasEl.addEventListener("pointerdown", this.handlePointerDown);
|
|
1351
|
+
this.canvasEl.addEventListener("pointermove", this.handlePointerMove);
|
|
1352
|
+
this.canvasEl.addEventListener("pointerup", this.handlePointerUp);
|
|
1353
|
+
this.canvasEl.addEventListener("pointercancel", this.handlePointerUp);
|
|
1354
|
+
this.canvasEl.addEventListener("click", this.handleClick);
|
|
1355
|
+
this.canvasEl.addEventListener("dblclick", this.handleDoubleClick);
|
|
1356
|
+
this.canvasEl.addEventListener("wheel", this.handleWheel, { passive: false });
|
|
1357
|
+
this.root.addEventListener("keydown", this.handleKeyDown, true);
|
|
1358
|
+
window.addEventListener("pointerup", this.handlePointerUp);
|
|
1359
|
+
window.addEventListener("resize", this.handleResize);
|
|
1360
|
+
}
|
|
1361
|
+
unbindEvents() {
|
|
1362
|
+
this.toolbar.removeEventListener("click", this.handleToolbarClick);
|
|
1363
|
+
this.arrangeSelect.removeEventListener("change", this.handleArrangeChange);
|
|
1364
|
+
this.paletteGrid.removeEventListener("click", this.handlePaletteClick);
|
|
1365
|
+
this.selectionFields.removeEventListener("input", this.handleSelectionFieldInput);
|
|
1366
|
+
this.selectionFields.removeEventListener("change", this.handleSelectionFieldChange);
|
|
1367
|
+
this.jsonActions.removeEventListener("click", this.handleJsonActionClick);
|
|
1368
|
+
this.canvasEl.removeEventListener("pointerdown", this.handlePointerDown);
|
|
1369
|
+
this.canvasEl.removeEventListener("pointermove", this.handlePointerMove);
|
|
1370
|
+
this.canvasEl.removeEventListener("pointerup", this.handlePointerUp);
|
|
1371
|
+
this.canvasEl.removeEventListener("pointercancel", this.handlePointerUp);
|
|
1372
|
+
this.canvasEl.removeEventListener("click", this.handleClick);
|
|
1373
|
+
this.canvasEl.removeEventListener("dblclick", this.handleDoubleClick);
|
|
1374
|
+
this.canvasEl.removeEventListener("wheel", this.handleWheel);
|
|
1375
|
+
this.root.removeEventListener("keydown", this.handleKeyDown, true);
|
|
1376
|
+
window.removeEventListener("pointerup", this.handlePointerUp);
|
|
1377
|
+
window.removeEventListener("resize", this.handleResize);
|
|
1378
|
+
}
|
|
1379
|
+
handleResize() {
|
|
1380
|
+
if (this.destroyed) return;
|
|
1381
|
+
this.render({ inspector: false, json: false });
|
|
1382
|
+
}
|
|
1383
|
+
updatePaletteState() {
|
|
1384
|
+
const arrowArmed = this.activeTool === "arrow";
|
|
1385
|
+
this.paletteGrid.querySelectorAll(".vd-flowchart-palette-btn").forEach((button) => {
|
|
1386
|
+
const tool = button.getAttribute("data-tool");
|
|
1387
|
+
button.classList.toggle("is-active", Boolean(tool) && tool === this.activeTool);
|
|
1388
|
+
});
|
|
1389
|
+
this.canvasEl.classList.toggle("is-arrow-tool", arrowArmed);
|
|
1390
|
+
}
|
|
1391
|
+
setActiveTool(tool) {
|
|
1392
|
+
this.activeTool = tool || null;
|
|
1393
|
+
this.updatePaletteState();
|
|
1394
|
+
this.render({ scene: true, inspector: false, json: false });
|
|
1395
|
+
}
|
|
1396
|
+
handleToolbarClick(event) {
|
|
1397
|
+
const actionButton = event.target.closest("[data-flowchart-action]");
|
|
1398
|
+
if (!actionButton) return;
|
|
1399
|
+
const action = actionButton.getAttribute("data-flowchart-action");
|
|
1400
|
+
if (action === "zoom-in") this.zoomIn();
|
|
1401
|
+
if (action === "zoom-out") this.zoomOut();
|
|
1402
|
+
if (action === "reset-view") this.resetView();
|
|
1403
|
+
if (action === "fit-view") this.fitView();
|
|
1404
|
+
if (action === "undo") this.undo();
|
|
1405
|
+
if (action === "redo") this.redo();
|
|
1406
|
+
if (action === "clear" && !this.readonly) this.clear();
|
|
1407
|
+
}
|
|
1408
|
+
handleArrangeChange(event) {
|
|
1409
|
+
if (this.readonly) return;
|
|
1410
|
+
const mode = event.target.value;
|
|
1411
|
+
if (mode) this.layout(mode);
|
|
1412
|
+
event.target.selectedIndex = 0;
|
|
1413
|
+
}
|
|
1414
|
+
handlePaletteClick(event) {
|
|
1415
|
+
if (this.readonly) return;
|
|
1416
|
+
const toolButton = event.target.closest("[data-tool]");
|
|
1417
|
+
if (toolButton) {
|
|
1418
|
+
const tool = toolButton.getAttribute("data-tool");
|
|
1419
|
+
this.setActiveTool(this.activeTool === tool ? null : tool);
|
|
1420
|
+
return;
|
|
1421
|
+
}
|
|
1422
|
+
const button = event.target.closest("[data-node-type]");
|
|
1423
|
+
if (!button) return;
|
|
1424
|
+
this.setActiveTool(null);
|
|
1425
|
+
this.addNode({ type: button.getAttribute("data-node-type") });
|
|
1426
|
+
}
|
|
1427
|
+
handleJsonActionClick(event) {
|
|
1428
|
+
const button = event.target.closest("[data-json-action]");
|
|
1429
|
+
if (!button) return;
|
|
1430
|
+
const action = button.getAttribute("data-json-action");
|
|
1431
|
+
if (action === "refresh") {
|
|
1432
|
+
this.syncJsonTextarea(true);
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
if (action === "load" && !this.readonly) {
|
|
1436
|
+
this.load(this.jsonTextarea.value);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
handleSelectionFieldInput(event) {
|
|
1440
|
+
const field = event.target.getAttribute("data-field");
|
|
1441
|
+
if (!field) return;
|
|
1442
|
+
if (field === "node-text") {
|
|
1443
|
+
this.updateNode(
|
|
1444
|
+
this.selection?.id,
|
|
1445
|
+
{ text: event.target.value },
|
|
1446
|
+
{ inspector: false, reason: "node:update" }
|
|
1447
|
+
);
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
if (field === "edge-label") {
|
|
1451
|
+
this.updateEdge(
|
|
1452
|
+
this.selection?.id,
|
|
1453
|
+
{ label: event.target.value },
|
|
1454
|
+
{ inspector: false, reason: "edge:update" }
|
|
1455
|
+
);
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1458
|
+
if (field === "node-width" || field === "node-height") {
|
|
1459
|
+
const patch = field === "node-width" ? { width: toFiniteNumber(event.target.value, void 0) } : { height: toFiniteNumber(event.target.value, void 0) };
|
|
1460
|
+
this.updateNode(this.selection?.id, patch, { inspector: false, reason: "node:update" });
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
handleSelectionFieldChange(event) {
|
|
1464
|
+
const field = event.target.getAttribute("data-field");
|
|
1465
|
+
if (!field) return;
|
|
1466
|
+
if (field === "node-type") {
|
|
1467
|
+
this.updateNode(
|
|
1468
|
+
this.selection?.id,
|
|
1469
|
+
{ type: event.target.value },
|
|
1470
|
+
{ inspector: true, reason: "node:update" }
|
|
1471
|
+
);
|
|
1472
|
+
return;
|
|
1473
|
+
}
|
|
1474
|
+
if (field === "edge-start-marker") {
|
|
1475
|
+
this.updateEdge(
|
|
1476
|
+
this.selection?.id,
|
|
1477
|
+
{ startMarker: event.target.value },
|
|
1478
|
+
{ inspector: true, reason: "edge:update" }
|
|
1479
|
+
);
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
if (field === "edge-end-marker") {
|
|
1483
|
+
this.updateEdge(
|
|
1484
|
+
this.selection?.id,
|
|
1485
|
+
{ endMarker: event.target.value },
|
|
1486
|
+
{ inspector: true, reason: "edge:update" }
|
|
1487
|
+
);
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1490
|
+
if (field === "edge-route") {
|
|
1491
|
+
this.updateEdge(
|
|
1492
|
+
this.selection?.id,
|
|
1493
|
+
{ route: event.target.value },
|
|
1494
|
+
{ inspector: true, reason: "edge:update" }
|
|
1495
|
+
);
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
if (field === "edge-stroke-preset") {
|
|
1499
|
+
this.updateEdge(
|
|
1500
|
+
this.selection?.id,
|
|
1501
|
+
{ strokeWidth: getStrokePresetWidth(event.target.value) },
|
|
1502
|
+
{ inspector: true, reason: "edge:update" }
|
|
1503
|
+
);
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
handleKeyDown(event) {
|
|
1507
|
+
if (this.readonly) return;
|
|
1508
|
+
if (event.target && (event.target.tagName === "INPUT" || event.target.tagName === "TEXTAREA" || event.target.tagName === "SELECT")) {
|
|
1509
|
+
return;
|
|
1510
|
+
}
|
|
1511
|
+
if (event.key === "Escape" && this.activeTool) {
|
|
1512
|
+
event.preventDefault();
|
|
1513
|
+
this.setActiveTool(null);
|
|
1514
|
+
return;
|
|
1515
|
+
}
|
|
1516
|
+
const modKey = event.metaKey || event.ctrlKey;
|
|
1517
|
+
if (modKey && (event.key === "z" || event.key === "Z")) {
|
|
1518
|
+
event.preventDefault();
|
|
1519
|
+
if (event.shiftKey) {
|
|
1520
|
+
this.redo();
|
|
1521
|
+
} else {
|
|
1522
|
+
this.undo();
|
|
1523
|
+
}
|
|
1524
|
+
return;
|
|
1525
|
+
}
|
|
1526
|
+
if (modKey && (event.key === "y" || event.key === "Y")) {
|
|
1527
|
+
event.preventDefault();
|
|
1528
|
+
this.redo();
|
|
1529
|
+
return;
|
|
1530
|
+
}
|
|
1531
|
+
if (modKey && event.key === "c") {
|
|
1532
|
+
if (this.selection) {
|
|
1533
|
+
event.preventDefault();
|
|
1534
|
+
this.copySelection();
|
|
1535
|
+
}
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
if (modKey && event.key === "v") {
|
|
1539
|
+
if (this.clipboard) {
|
|
1540
|
+
event.preventDefault();
|
|
1541
|
+
this.pasteClipboard();
|
|
1542
|
+
}
|
|
1543
|
+
return;
|
|
1544
|
+
}
|
|
1545
|
+
if (modKey && event.key === "x") {
|
|
1546
|
+
if (this.selection) {
|
|
1547
|
+
event.preventDefault();
|
|
1548
|
+
this.cutSelection();
|
|
1549
|
+
}
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
if (!this.selection) return;
|
|
1553
|
+
if (event.key === "Backspace" || event.key === "Delete") {
|
|
1554
|
+
event.preventDefault();
|
|
1555
|
+
this.deleteSelection();
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
copySelection() {
|
|
1559
|
+
if (!this.selection) return false;
|
|
1560
|
+
if (this.selection.kind === "node") {
|
|
1561
|
+
const node = this.findNode(this.selection.id);
|
|
1562
|
+
if (!node) return false;
|
|
1563
|
+
this.clipboard = { kind: "node", data: deepClone(node) };
|
|
1564
|
+
return true;
|
|
1565
|
+
}
|
|
1566
|
+
const edge = this.findEdge(this.selection.id);
|
|
1567
|
+
if (!edge) return false;
|
|
1568
|
+
this.clipboard = { kind: "edge", data: deepClone(edge) };
|
|
1569
|
+
return true;
|
|
1570
|
+
}
|
|
1571
|
+
pasteClipboard() {
|
|
1572
|
+
if (!this.clipboard || this.readonly) return false;
|
|
1573
|
+
if (this.clipboard.kind === "node") {
|
|
1574
|
+
const usedIds2 = new Set(this.documentData.nodes.map((node2) => node2.id));
|
|
1575
|
+
const node = normalizeNode(
|
|
1576
|
+
{
|
|
1577
|
+
...this.clipboard.data,
|
|
1578
|
+
id: void 0,
|
|
1579
|
+
x: this.clipboard.data.x + 24,
|
|
1580
|
+
y: this.clipboard.data.y + 24
|
|
1581
|
+
},
|
|
1582
|
+
this.documentData.nodes.length,
|
|
1583
|
+
usedIds2
|
|
1584
|
+
);
|
|
1585
|
+
this.documentData.nodes.push(node);
|
|
1586
|
+
this.select({ kind: "node", id: node.id });
|
|
1587
|
+
this.emitChange("node:add", { node: deepClone(node) });
|
|
1588
|
+
return true;
|
|
1589
|
+
}
|
|
1590
|
+
const usedIds = new Set(this.documentData.edges.map((edge2) => edge2.id));
|
|
1591
|
+
const nodeIds = new Set(this.documentData.nodes.map((node) => node.id));
|
|
1592
|
+
const edge = normalizeEdge(
|
|
1593
|
+
{
|
|
1594
|
+
...this.clipboard.data,
|
|
1595
|
+
id: void 0
|
|
1596
|
+
},
|
|
1597
|
+
this.documentData.edges.length,
|
|
1598
|
+
nodeIds,
|
|
1599
|
+
usedIds
|
|
1600
|
+
);
|
|
1601
|
+
if (!edge) return false;
|
|
1602
|
+
this.documentData.edges.push(edge);
|
|
1603
|
+
this.select({ kind: "edge", id: edge.id });
|
|
1604
|
+
this.emitChange("edge:add", { edge: deepClone(edge) });
|
|
1605
|
+
return true;
|
|
1606
|
+
}
|
|
1607
|
+
cutSelection() {
|
|
1608
|
+
if (!this.copySelection()) return false;
|
|
1609
|
+
return this.deleteSelection();
|
|
1610
|
+
}
|
|
1611
|
+
handleDoubleClick(event) {
|
|
1612
|
+
if (this.readonly) return;
|
|
1613
|
+
const edgeTarget = event.target.closest("[data-edge-id]");
|
|
1614
|
+
if (edgeTarget && !event.target.closest("[data-edge-endpoint]")) {
|
|
1615
|
+
const edgeId = edgeTarget.getAttribute("data-edge-id");
|
|
1616
|
+
if (edgeId && this.findEdge(edgeId)) {
|
|
1617
|
+
this.reconnectEdgeId = edgeId;
|
|
1618
|
+
this.select({ kind: "edge", id: edgeId });
|
|
1619
|
+
event.preventDefault();
|
|
1620
|
+
event.stopPropagation();
|
|
1621
|
+
return;
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
const nodeTarget = event.target.closest("[data-node-id]");
|
|
1625
|
+
if (!nodeTarget || event.target.closest("[data-edge-id]")) return;
|
|
1626
|
+
const nodeId = nodeTarget.getAttribute("data-node-id");
|
|
1627
|
+
if (this.startTextEdit(nodeId)) {
|
|
1628
|
+
event.preventDefault();
|
|
1629
|
+
event.stopPropagation();
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
handleClick(event) {
|
|
1633
|
+
if (this.readonly || event.detail < 2) return;
|
|
1634
|
+
const nodeTarget = event.target.closest("[data-node-id]");
|
|
1635
|
+
if (!nodeTarget || event.target.closest("[data-port]") || event.target.closest("[data-resize-handle]"))
|
|
1636
|
+
return;
|
|
1637
|
+
if (this.startTextEdit(nodeTarget.getAttribute("data-node-id"))) {
|
|
1638
|
+
event.preventDefault();
|
|
1639
|
+
event.stopPropagation();
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
handlePointerDown(event) {
|
|
1643
|
+
if (this.destroyed || event.button != null && event.button !== 0) return;
|
|
1644
|
+
if (this.textEditor && !event.target.closest(".vd-flowchart-text-editor")) {
|
|
1645
|
+
this.stopTextEdit({ commit: true });
|
|
1646
|
+
}
|
|
1647
|
+
this.canvasEl.focus();
|
|
1648
|
+
const nodeTarget = event.target.closest("[data-node-id]");
|
|
1649
|
+
const portTarget = event.target.closest("[data-port]");
|
|
1650
|
+
const resizeTarget = event.target.closest("[data-resize-handle]");
|
|
1651
|
+
const edgeTarget = event.target.closest("[data-edge-id]");
|
|
1652
|
+
const endpointTarget = event.target.closest("[data-edge-endpoint]");
|
|
1653
|
+
if (endpointTarget && !this.readonly) {
|
|
1654
|
+
const edgeId = endpointTarget.getAttribute("data-edge-id");
|
|
1655
|
+
const endpoint = endpointTarget.getAttribute("data-edge-endpoint");
|
|
1656
|
+
const edge = this.findEdge(edgeId);
|
|
1657
|
+
if (!edge || endpoint !== "from" && endpoint !== "to") return;
|
|
1658
|
+
const fromNode = this.findNode(edge.from.nodeId);
|
|
1659
|
+
const toNode = this.findNode(edge.to.nodeId);
|
|
1660
|
+
if (!fromNode || !toNode) return;
|
|
1661
|
+
this.reconnectEdgeId = edgeId;
|
|
1662
|
+
this.select({ kind: "edge", id: edgeId });
|
|
1663
|
+
this.interaction = {
|
|
1664
|
+
kind: "reconnect",
|
|
1665
|
+
pointerId: event.pointerId,
|
|
1666
|
+
edgeId,
|
|
1667
|
+
endpoint,
|
|
1668
|
+
target: null,
|
|
1669
|
+
previousSnap: null,
|
|
1670
|
+
fromPoint: getPortPosition(fromNode, edge.from.port),
|
|
1671
|
+
toPoint: getPortPosition(toNode, edge.to.port),
|
|
1672
|
+
fromPort: edge.from.port,
|
|
1673
|
+
toPort: edge.to.port,
|
|
1674
|
+
route: edge.route,
|
|
1675
|
+
strokeWidth: edge.strokeWidth
|
|
1676
|
+
};
|
|
1677
|
+
this.capturePointer(event.pointerId);
|
|
1678
|
+
this.syncConnectingState();
|
|
1679
|
+
this.render({ inspector: false, json: false });
|
|
1680
|
+
event.preventDefault();
|
|
1681
|
+
return;
|
|
1682
|
+
}
|
|
1683
|
+
if (nodeTarget && !portTarget && !resizeTarget && !edgeTarget && !this.readonly) {
|
|
1684
|
+
if (this.activeTool === "arrow") {
|
|
1685
|
+
this.select({ kind: "node", id: nodeTarget.getAttribute("data-node-id") });
|
|
1686
|
+
event.preventDefault();
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
const nodeId = nodeTarget.getAttribute("data-node-id");
|
|
1690
|
+
const now = Date.now();
|
|
1691
|
+
const repeatedNodeClick = this.lastNodePointer && this.lastNodePointer.nodeId === nodeId && now - this.lastNodePointer.time <= 420;
|
|
1692
|
+
this.lastNodePointer = { nodeId, time: now };
|
|
1693
|
+
if ((event.detail >= 2 || repeatedNodeClick) && this.startTextEdit(nodeId)) {
|
|
1694
|
+
event.preventDefault();
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
} else {
|
|
1698
|
+
this.lastNodePointer = null;
|
|
1699
|
+
}
|
|
1700
|
+
if (portTarget && !this.readonly) {
|
|
1701
|
+
const nodeId = portTarget.getAttribute("data-node-id");
|
|
1702
|
+
const port = portTarget.getAttribute("data-port");
|
|
1703
|
+
const node = this.findNode(nodeId);
|
|
1704
|
+
if (!node) return;
|
|
1705
|
+
const sourcePoint = getPortPosition(node, port);
|
|
1706
|
+
this.interaction = {
|
|
1707
|
+
kind: "connect",
|
|
1708
|
+
pointerId: event.pointerId,
|
|
1709
|
+
source: { nodeId, port },
|
|
1710
|
+
target: null,
|
|
1711
|
+
previousSnap: null,
|
|
1712
|
+
fromPoint: sourcePoint,
|
|
1713
|
+
toPoint: sourcePoint,
|
|
1714
|
+
fromPort: port,
|
|
1715
|
+
toPort: "left",
|
|
1716
|
+
route: DEFAULT_EDGE_ROUTE,
|
|
1717
|
+
strokeWidth: DEFAULT_EDGE_STROKE_WIDTH
|
|
1718
|
+
};
|
|
1719
|
+
this.select({ kind: "node", id: nodeId });
|
|
1720
|
+
this.capturePointer(event.pointerId);
|
|
1721
|
+
this.syncConnectingState();
|
|
1722
|
+
this.render({ inspector: false, json: false });
|
|
1723
|
+
event.preventDefault();
|
|
1724
|
+
return;
|
|
1725
|
+
}
|
|
1726
|
+
if (resizeTarget && !this.readonly) {
|
|
1727
|
+
const nodeId = resizeTarget.getAttribute("data-node-id");
|
|
1728
|
+
const handle = resizeTarget.getAttribute("data-resize-handle");
|
|
1729
|
+
const node = this.findNode(nodeId);
|
|
1730
|
+
if (!node || !RESIZE_HANDLES.includes(handle)) return;
|
|
1731
|
+
this.select({ kind: "node", id: nodeId });
|
|
1732
|
+
this.interaction = {
|
|
1733
|
+
kind: "resize-node",
|
|
1734
|
+
pointerId: event.pointerId,
|
|
1735
|
+
nodeId,
|
|
1736
|
+
handle,
|
|
1737
|
+
startWorld: this.clientToWorld(event.clientX, event.clientY),
|
|
1738
|
+
original: deepClone(node),
|
|
1739
|
+
moved: false
|
|
1740
|
+
};
|
|
1741
|
+
this.capturePointer(event.pointerId);
|
|
1742
|
+
event.preventDefault();
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
if (edgeTarget) {
|
|
1746
|
+
const edgeId = edgeTarget.getAttribute("data-edge-id");
|
|
1747
|
+
if (!this.readonly) {
|
|
1748
|
+
this.reconnectEdgeId = edgeId;
|
|
1749
|
+
} else if (this.reconnectEdgeId && this.reconnectEdgeId !== edgeId) {
|
|
1750
|
+
this.reconnectEdgeId = null;
|
|
1751
|
+
}
|
|
1752
|
+
this.select({ kind: "edge", id: edgeId });
|
|
1753
|
+
event.preventDefault();
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
if (nodeTarget) {
|
|
1757
|
+
this.reconnectEdgeId = null;
|
|
1758
|
+
const nodeId = nodeTarget.getAttribute("data-node-id");
|
|
1759
|
+
this.select({ kind: "node", id: nodeId });
|
|
1760
|
+
if (!this.readonly) {
|
|
1761
|
+
const node = this.findNode(nodeId);
|
|
1762
|
+
const world = this.clientToWorld(event.clientX, event.clientY);
|
|
1763
|
+
this.interaction = {
|
|
1764
|
+
kind: "drag-node",
|
|
1765
|
+
pointerId: event.pointerId,
|
|
1766
|
+
nodeId,
|
|
1767
|
+
offsetX: world.x - node.x,
|
|
1768
|
+
offsetY: world.y - node.y,
|
|
1769
|
+
moved: false
|
|
1770
|
+
};
|
|
1771
|
+
this.capturePointer(event.pointerId);
|
|
1772
|
+
}
|
|
1773
|
+
return;
|
|
1774
|
+
}
|
|
1775
|
+
this.select(null);
|
|
1776
|
+
this.reconnectEdgeId = null;
|
|
1777
|
+
this.interaction = {
|
|
1778
|
+
kind: "pan",
|
|
1779
|
+
pointerId: event.pointerId,
|
|
1780
|
+
startClientX: event.clientX,
|
|
1781
|
+
startClientY: event.clientY,
|
|
1782
|
+
startViewportX: this.documentData.viewport.x,
|
|
1783
|
+
startViewportY: this.documentData.viewport.y,
|
|
1784
|
+
moved: false
|
|
1785
|
+
};
|
|
1786
|
+
this.capturePointer(event.pointerId);
|
|
1787
|
+
event.preventDefault();
|
|
1788
|
+
}
|
|
1789
|
+
handlePointerMove(event) {
|
|
1790
|
+
if (!this.interaction || this.interaction.pointerId !== event.pointerId) return;
|
|
1791
|
+
if (this.interaction.kind === "drag-node") {
|
|
1792
|
+
const node = this.findNode(this.interaction.nodeId);
|
|
1793
|
+
if (!node) return;
|
|
1794
|
+
const world = this.clientToWorld(event.clientX, event.clientY);
|
|
1795
|
+
const nextX = formatNumber(world.x - this.interaction.offsetX);
|
|
1796
|
+
const nextY = formatNumber(world.y - this.interaction.offsetY);
|
|
1797
|
+
if (nextX !== node.x || nextY !== node.y) {
|
|
1798
|
+
node.x = nextX;
|
|
1799
|
+
node.y = nextY;
|
|
1800
|
+
this.interaction.moved = true;
|
|
1801
|
+
this.render({ inspector: false, json: false });
|
|
1802
|
+
}
|
|
1803
|
+
return;
|
|
1804
|
+
}
|
|
1805
|
+
if (this.interaction.kind === "resize-node") {
|
|
1806
|
+
const node = this.findNode(this.interaction.nodeId);
|
|
1807
|
+
if (!node) return;
|
|
1808
|
+
const world = this.clientToWorld(event.clientX, event.clientY);
|
|
1809
|
+
const deltaX = world.x - this.interaction.startWorld.x;
|
|
1810
|
+
const deltaY = world.y - this.interaction.startWorld.y;
|
|
1811
|
+
const next = this.getResizedNodeBounds(
|
|
1812
|
+
this.interaction.original,
|
|
1813
|
+
this.interaction.handle,
|
|
1814
|
+
deltaX,
|
|
1815
|
+
deltaY
|
|
1816
|
+
);
|
|
1817
|
+
if (next.x !== node.x || next.y !== node.y || next.width !== node.width || next.height !== node.height) {
|
|
1818
|
+
node.x = next.x;
|
|
1819
|
+
node.y = next.y;
|
|
1820
|
+
node.width = next.width;
|
|
1821
|
+
node.height = next.height;
|
|
1822
|
+
this.interaction.moved = true;
|
|
1823
|
+
this.render({ inspector: false, json: false });
|
|
1824
|
+
}
|
|
1825
|
+
return;
|
|
1826
|
+
}
|
|
1827
|
+
if (this.interaction.kind === "pan") {
|
|
1828
|
+
const deltaX = event.clientX - this.interaction.startClientX;
|
|
1829
|
+
const deltaY = event.clientY - this.interaction.startClientY;
|
|
1830
|
+
this.documentData.viewport.x = formatNumber(this.interaction.startViewportX + deltaX);
|
|
1831
|
+
this.documentData.viewport.y = formatNumber(this.interaction.startViewportY + deltaY);
|
|
1832
|
+
this.interaction.moved = true;
|
|
1833
|
+
this.render({ inspector: false, json: false });
|
|
1834
|
+
return;
|
|
1835
|
+
}
|
|
1836
|
+
if (this.interaction.kind === "connect") {
|
|
1837
|
+
const world = this.clientToWorld(event.clientX, event.clientY);
|
|
1838
|
+
const snapTarget = this.findConnectionTarget(
|
|
1839
|
+
world,
|
|
1840
|
+
this.interaction.source.nodeId,
|
|
1841
|
+
this.interaction.previousSnap,
|
|
1842
|
+
this.interaction.fromPoint
|
|
1843
|
+
);
|
|
1844
|
+
if (snapTarget) {
|
|
1845
|
+
this.interaction.target = { nodeId: snapTarget.node.id, port: snapTarget.port };
|
|
1846
|
+
this.interaction.toPoint = snapTarget.point;
|
|
1847
|
+
this.interaction.toPort = snapTarget.port;
|
|
1848
|
+
this.interaction.previousSnap = { nodeId: snapTarget.node.id, port: snapTarget.port };
|
|
1849
|
+
this.render({ inspector: false, json: false });
|
|
1850
|
+
return;
|
|
1851
|
+
}
|
|
1852
|
+
this.interaction.target = null;
|
|
1853
|
+
this.interaction.toPoint = world;
|
|
1854
|
+
this.interaction.toPort = this.interaction.toPort || "left";
|
|
1855
|
+
this.interaction.previousSnap = null;
|
|
1856
|
+
this.render({ inspector: false, json: false });
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
if (this.interaction.kind === "reconnect") {
|
|
1860
|
+
const edge = this.findEdge(this.interaction.edgeId);
|
|
1861
|
+
if (!edge) return;
|
|
1862
|
+
const world = this.clientToWorld(event.clientX, event.clientY);
|
|
1863
|
+
const snapTarget = this.findConnectionTarget(
|
|
1864
|
+
world,
|
|
1865
|
+
null,
|
|
1866
|
+
this.interaction.previousSnap,
|
|
1867
|
+
this.interaction.endpoint === "from" ? this.interaction.toPoint : this.interaction.fromPoint
|
|
1868
|
+
);
|
|
1869
|
+
if (this.interaction.endpoint === "from") {
|
|
1870
|
+
const toNode = this.findNode(edge.to.nodeId);
|
|
1871
|
+
if (!toNode) return;
|
|
1872
|
+
this.interaction.toPoint = getPortPosition(toNode, edge.to.port);
|
|
1873
|
+
this.interaction.toPort = edge.to.port;
|
|
1874
|
+
if (snapTarget) {
|
|
1875
|
+
this.interaction.target = { nodeId: snapTarget.node.id, port: snapTarget.port };
|
|
1876
|
+
this.interaction.fromPoint = snapTarget.point;
|
|
1877
|
+
this.interaction.fromPort = snapTarget.port;
|
|
1878
|
+
this.interaction.previousSnap = { nodeId: snapTarget.node.id, port: snapTarget.port };
|
|
1879
|
+
} else {
|
|
1880
|
+
this.interaction.target = null;
|
|
1881
|
+
this.interaction.fromPoint = world;
|
|
1882
|
+
this.interaction.fromPort = this.interaction.fromPort || edge.from.port;
|
|
1883
|
+
this.interaction.previousSnap = null;
|
|
1884
|
+
}
|
|
1885
|
+
} else {
|
|
1886
|
+
const fromNode = this.findNode(edge.from.nodeId);
|
|
1887
|
+
if (!fromNode) return;
|
|
1888
|
+
this.interaction.fromPoint = getPortPosition(fromNode, edge.from.port);
|
|
1889
|
+
this.interaction.fromPort = edge.from.port;
|
|
1890
|
+
if (snapTarget) {
|
|
1891
|
+
this.interaction.target = { nodeId: snapTarget.node.id, port: snapTarget.port };
|
|
1892
|
+
this.interaction.toPoint = snapTarget.point;
|
|
1893
|
+
this.interaction.toPort = snapTarget.port;
|
|
1894
|
+
this.interaction.previousSnap = { nodeId: snapTarget.node.id, port: snapTarget.port };
|
|
1895
|
+
} else {
|
|
1896
|
+
this.interaction.target = null;
|
|
1897
|
+
this.interaction.toPoint = world;
|
|
1898
|
+
this.interaction.toPort = this.interaction.toPort || edge.to.port;
|
|
1899
|
+
this.interaction.previousSnap = null;
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
this.render({ inspector: false, json: false });
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
handlePointerUp(event) {
|
|
1906
|
+
if (!this.interaction || event.pointerId != null && this.interaction.pointerId !== event.pointerId) {
|
|
1907
|
+
return;
|
|
1908
|
+
}
|
|
1909
|
+
const interaction = this.interaction;
|
|
1910
|
+
this.interaction = null;
|
|
1911
|
+
this.releasePointerCapture(interaction.pointerId);
|
|
1912
|
+
this.syncConnectingState();
|
|
1913
|
+
if (interaction.kind === "resize-node") {
|
|
1914
|
+
if (interaction.moved) {
|
|
1915
|
+
const node = this.findNode(interaction.nodeId);
|
|
1916
|
+
this.render({ inspector: true, json: true });
|
|
1917
|
+
this.emitChange("node:resize", { node: deepClone(node) });
|
|
1918
|
+
} else {
|
|
1919
|
+
this.render({ inspector: false, json: false });
|
|
1920
|
+
}
|
|
1921
|
+
return;
|
|
1922
|
+
}
|
|
1923
|
+
if (interaction.kind === "drag-node") {
|
|
1924
|
+
if (interaction.moved) {
|
|
1925
|
+
this.render({ inspector: true, json: true });
|
|
1926
|
+
this.emitChange("node:move", { node: deepClone(this.findNode(interaction.nodeId)) });
|
|
1927
|
+
} else {
|
|
1928
|
+
this.render({ inspector: false, json: false });
|
|
1929
|
+
}
|
|
1930
|
+
return;
|
|
1931
|
+
}
|
|
1932
|
+
if (interaction.kind === "pan") {
|
|
1933
|
+
if (interaction.moved) {
|
|
1934
|
+
this.render({ inspector: false, json: true });
|
|
1935
|
+
this.emitViewportChange("viewport:pan");
|
|
1936
|
+
} else {
|
|
1937
|
+
this.render({ inspector: false, json: false });
|
|
1938
|
+
}
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
if (interaction.kind === "connect") {
|
|
1942
|
+
const world = this.clientToWorld(event.clientX || 0, event.clientY || 0);
|
|
1943
|
+
const snapTarget = interaction.target ? { node: this.findNode(interaction.target.nodeId), port: interaction.target.port } : this.findConnectionTarget(
|
|
1944
|
+
world,
|
|
1945
|
+
interaction.source.nodeId,
|
|
1946
|
+
interaction.previousSnap,
|
|
1947
|
+
interaction.fromPoint
|
|
1948
|
+
);
|
|
1949
|
+
if (snapTarget && snapTarget.node) {
|
|
1950
|
+
const edge = this.addEdge({
|
|
1951
|
+
from: interaction.source,
|
|
1952
|
+
to: {
|
|
1953
|
+
nodeId: snapTarget.node.id,
|
|
1954
|
+
port: snapTarget.port
|
|
1955
|
+
},
|
|
1956
|
+
strokeWidth: interaction.strokeWidth,
|
|
1957
|
+
endMarker: "arrow"
|
|
1958
|
+
});
|
|
1959
|
+
if (edge) {
|
|
1960
|
+
if (this.activeTool === "arrow") this.setActiveTool(null);
|
|
1961
|
+
this.syncConnectingState();
|
|
1962
|
+
return;
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
if (this.activeTool === "arrow") this.setActiveTool(null);
|
|
1966
|
+
this.syncConnectingState();
|
|
1967
|
+
this.render({ inspector: false, json: false });
|
|
1968
|
+
return;
|
|
1969
|
+
}
|
|
1970
|
+
if (interaction.kind === "reconnect") {
|
|
1971
|
+
const edge = this.findEdge(interaction.edgeId);
|
|
1972
|
+
if (edge && interaction.target) {
|
|
1973
|
+
const patch = interaction.endpoint === "from" ? { from: { nodeId: interaction.target.nodeId, port: interaction.target.port } } : { to: { nodeId: interaction.target.nodeId, port: interaction.target.port } };
|
|
1974
|
+
const nextFrom = patch.from || edge.from;
|
|
1975
|
+
const nextTo = patch.to || edge.to;
|
|
1976
|
+
if (!(nextFrom.nodeId === nextTo.nodeId && nextFrom.port === nextTo.port)) {
|
|
1977
|
+
this.updateEdge(interaction.edgeId, patch, { inspector: true, reason: "edge:reconnect" });
|
|
1978
|
+
} else {
|
|
1979
|
+
this.render({ inspector: false, json: false });
|
|
1980
|
+
}
|
|
1981
|
+
} else {
|
|
1982
|
+
this.render({ inspector: false, json: false });
|
|
1983
|
+
}
|
|
1984
|
+
this.reconnectEdgeId = interaction.edgeId;
|
|
1985
|
+
this.syncConnectingState();
|
|
1986
|
+
return;
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
handleWheel(event) {
|
|
1990
|
+
event.preventDefault();
|
|
1991
|
+
const local = this.clientToLocal(event.clientX, event.clientY);
|
|
1992
|
+
const factor = event.deltaY > 0 ? 1 / 1.12 : 1.12;
|
|
1993
|
+
this.scaleAround(factor, local.x, local.y, "viewport:zoom");
|
|
1994
|
+
}
|
|
1995
|
+
capturePointer(pointerId) {
|
|
1996
|
+
if (typeof this.canvasEl.setPointerCapture !== "function") return;
|
|
1997
|
+
try {
|
|
1998
|
+
this.canvasEl.setPointerCapture(pointerId);
|
|
1999
|
+
} catch {
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
releasePointerCapture(pointerId) {
|
|
2003
|
+
if (typeof this.canvasEl.releasePointerCapture !== "function") return;
|
|
2004
|
+
try {
|
|
2005
|
+
this.canvasEl.releasePointerCapture(pointerId);
|
|
2006
|
+
} catch {
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
syncConnectingState() {
|
|
2010
|
+
const connecting = this.interaction?.kind === "connect" || this.interaction?.kind === "reconnect";
|
|
2011
|
+
this.canvasEl.classList.toggle("is-connecting", connecting);
|
|
2012
|
+
}
|
|
2013
|
+
shouldShowNodePorts(node) {
|
|
2014
|
+
if (this.readonly) return false;
|
|
2015
|
+
if (this.interaction?.kind === "connect" || this.interaction?.kind === "reconnect") return true;
|
|
2016
|
+
if (this.activeTool === "arrow") return true;
|
|
2017
|
+
return this.selection?.kind === "node" && this.selection.id === node.id;
|
|
2018
|
+
}
|
|
2019
|
+
getMarkerId(markerType, position, strokeWidth) {
|
|
2020
|
+
const width = normalizeEdgeStrokeWidth(strokeWidth);
|
|
2021
|
+
const key = `${markerType}:${position}:${width}`;
|
|
2022
|
+
if (this.markerIds.has(key)) {
|
|
2023
|
+
return this.markerIds.get(key);
|
|
2024
|
+
}
|
|
2025
|
+
const markerId = nextId(`flowchart-${markerType}-${position}`);
|
|
2026
|
+
const marker = markerType === "dot" ? createDotMarker(markerId, width) : createArrowMarker(markerId, width, position === "start");
|
|
2027
|
+
this.markerDefs.appendChild(marker);
|
|
2028
|
+
this.markerIds.set(key, markerId);
|
|
2029
|
+
return markerId;
|
|
2030
|
+
}
|
|
2031
|
+
getEdgeStrokeWidth(edge) {
|
|
2032
|
+
return normalizeEdgeStrokeWidth(edge?.strokeWidth);
|
|
2033
|
+
}
|
|
2034
|
+
getEdgeHitStrokeWidth(edge) {
|
|
2035
|
+
return formatNumber(Math.max(EDGE_HIT_STROKE_MIN, this.getEdgeStrokeWidth(edge) + 12));
|
|
2036
|
+
}
|
|
2037
|
+
findConnectionTarget(worldPoint, excludeNodeId, previousSnap = null, referencePoint = null) {
|
|
2038
|
+
const snapPadding = CONNECTION_SNAP_PADDING / this.documentData.viewport.scale;
|
|
2039
|
+
const hysteresisMargin = CONNECTION_HYSTERESIS / this.documentData.viewport.scale;
|
|
2040
|
+
const best = this.documentData.nodes.reduce((candidate, node) => {
|
|
2041
|
+
if (node.id === excludeNodeId) return candidate;
|
|
2042
|
+
const distanceToBounds2 = getDistanceToNodeBounds(node, worldPoint);
|
|
2043
|
+
if (distanceToBounds2 > snapPadding) return candidate;
|
|
2044
|
+
const nearest = pickPortForNode(node, worldPoint, referencePoint);
|
|
2045
|
+
if (!nearest) return candidate;
|
|
2046
|
+
const score = distanceToBounds2 * 1e3 + nearest.distance;
|
|
2047
|
+
if (!candidate || score < candidate.score) {
|
|
2048
|
+
return {
|
|
2049
|
+
node,
|
|
2050
|
+
port: nearest.port,
|
|
2051
|
+
point: nearest.point,
|
|
2052
|
+
score
|
|
2053
|
+
};
|
|
2054
|
+
}
|
|
2055
|
+
return candidate;
|
|
2056
|
+
}, null);
|
|
2057
|
+
if (!previousSnap || !best) return best;
|
|
2058
|
+
const previousNode = this.findNode(previousSnap.nodeId);
|
|
2059
|
+
if (!previousNode || previousNode.id === excludeNodeId) return best;
|
|
2060
|
+
const distanceToBounds = getDistanceToNodeBounds(previousNode, worldPoint);
|
|
2061
|
+
if (distanceToBounds > snapPadding) return best;
|
|
2062
|
+
if (best.node.id === previousSnap.nodeId && best.port === previousSnap.port) {
|
|
2063
|
+
return best;
|
|
2064
|
+
}
|
|
2065
|
+
const previousPoint = getPortPosition(previousNode, previousSnap.port);
|
|
2066
|
+
const previousScore = distanceToBounds * 1e3 + Math.hypot(worldPoint.x - previousPoint.x, worldPoint.y - previousPoint.y);
|
|
2067
|
+
if (best.score + hysteresisMargin >= previousScore) {
|
|
2068
|
+
return {
|
|
2069
|
+
node: previousNode,
|
|
2070
|
+
port: previousSnap.port,
|
|
2071
|
+
point: previousPoint,
|
|
2072
|
+
score: previousScore
|
|
2073
|
+
};
|
|
2074
|
+
}
|
|
2075
|
+
return best;
|
|
2076
|
+
}
|
|
2077
|
+
getResizedNodeBounds(original, handle, deltaX, deltaY) {
|
|
2078
|
+
const nextType = original.type;
|
|
2079
|
+
if (!isNodeResizable(nextType)) {
|
|
2080
|
+
return {
|
|
2081
|
+
x: formatNumber(original.x),
|
|
2082
|
+
y: formatNumber(original.y),
|
|
2083
|
+
width: formatNumber(original.width),
|
|
2084
|
+
height: formatNumber(original.height)
|
|
2085
|
+
};
|
|
2086
|
+
}
|
|
2087
|
+
let x = original.x;
|
|
2088
|
+
let y = original.y;
|
|
2089
|
+
let width = original.width;
|
|
2090
|
+
let height = original.height;
|
|
2091
|
+
if (handle.includes("e")) {
|
|
2092
|
+
width = clampNodeWidth(nextType, original.width + deltaX, original.width);
|
|
2093
|
+
}
|
|
2094
|
+
if (handle.includes("s")) {
|
|
2095
|
+
height = clampNodeHeight(nextType, original.height + deltaY, original.height);
|
|
2096
|
+
}
|
|
2097
|
+
if (handle.includes("w")) {
|
|
2098
|
+
width = clampNodeWidth(nextType, original.width - deltaX, original.width);
|
|
2099
|
+
x = original.x + original.width - width;
|
|
2100
|
+
}
|
|
2101
|
+
if (handle.includes("n")) {
|
|
2102
|
+
height = clampNodeHeight(nextType, original.height - deltaY, original.height);
|
|
2103
|
+
y = original.y + original.height - height;
|
|
2104
|
+
}
|
|
2105
|
+
return {
|
|
2106
|
+
x: formatNumber(x),
|
|
2107
|
+
y: formatNumber(y),
|
|
2108
|
+
width: formatNumber(width),
|
|
2109
|
+
height: formatNumber(height)
|
|
2110
|
+
};
|
|
2111
|
+
}
|
|
2112
|
+
clientToLocal(clientX, clientY) {
|
|
2113
|
+
const rect = this.canvasEl.getBoundingClientRect();
|
|
2114
|
+
return {
|
|
2115
|
+
x: clientX - rect.left,
|
|
2116
|
+
y: clientY - rect.top
|
|
2117
|
+
};
|
|
2118
|
+
}
|
|
2119
|
+
localToWorld(localX, localY) {
|
|
2120
|
+
const viewport = this.documentData.viewport;
|
|
2121
|
+
return {
|
|
2122
|
+
x: (localX - viewport.x) / viewport.scale,
|
|
2123
|
+
y: (localY - viewport.y) / viewport.scale
|
|
2124
|
+
};
|
|
2125
|
+
}
|
|
2126
|
+
clientToWorld(clientX, clientY) {
|
|
2127
|
+
const local = this.clientToLocal(clientX, clientY);
|
|
2128
|
+
return this.localToWorld(local.x, local.y);
|
|
2129
|
+
}
|
|
2130
|
+
getViewportCenter() {
|
|
2131
|
+
const width = this.canvasEl.clientWidth || 800;
|
|
2132
|
+
const height = this.canvasEl.clientHeight || 560;
|
|
2133
|
+
return this.localToWorld(width / 2, height / 2);
|
|
2134
|
+
}
|
|
2135
|
+
scaleAround(factor, localX, localY, reason) {
|
|
2136
|
+
const viewport = this.documentData.viewport;
|
|
2137
|
+
const anchor = this.localToWorld(localX, localY);
|
|
2138
|
+
const nextScale = clamp(formatNumber(viewport.scale * factor), MIN_SCALE, MAX_SCALE);
|
|
2139
|
+
if (nextScale === viewport.scale) return;
|
|
2140
|
+
viewport.scale = nextScale;
|
|
2141
|
+
viewport.x = formatNumber(localX - anchor.x * nextScale);
|
|
2142
|
+
viewport.y = formatNumber(localY - anchor.y * nextScale);
|
|
2143
|
+
this.render({ inspector: false, json: true });
|
|
2144
|
+
this.emitViewportChange(reason);
|
|
2145
|
+
}
|
|
2146
|
+
updateToolbarLabel() {
|
|
2147
|
+
this.zoomLabel.textContent = `${Math.round(this.documentData.viewport.scale * 100)}%`;
|
|
2148
|
+
}
|
|
2149
|
+
startTextEdit(nodeId) {
|
|
2150
|
+
if (this.readonly) return false;
|
|
2151
|
+
const node = this.findNode(nodeId);
|
|
2152
|
+
if (!node || !isNodeTextEditable(node)) return false;
|
|
2153
|
+
if (this.textEditor?.nodeId === node.id) {
|
|
2154
|
+
this.positionTextEditor();
|
|
2155
|
+
this.textEditor.textarea.focus();
|
|
2156
|
+
this.textEditor.textarea.select();
|
|
2157
|
+
return true;
|
|
2158
|
+
}
|
|
2159
|
+
this.stopTextEdit({ commit: true });
|
|
2160
|
+
this.select({ kind: "node", id: node.id });
|
|
2161
|
+
const textarea = createElement("textarea", {
|
|
2162
|
+
className: `vd-flowchart-text-editor vd-flowchart-text-editor--${node.type}`,
|
|
2163
|
+
value: node.text,
|
|
2164
|
+
rows: 1
|
|
2165
|
+
});
|
|
2166
|
+
textarea.setAttribute("data-node-id", node.id);
|
|
2167
|
+
textarea.setAttribute("aria-label", "Edit node text");
|
|
2168
|
+
textarea.spellcheck = false;
|
|
2169
|
+
textarea.addEventListener("input", () => this.positionTextEditor());
|
|
2170
|
+
textarea.addEventListener("pointerdown", (event) => event.stopPropagation());
|
|
2171
|
+
textarea.addEventListener("dblclick", (event) => event.stopPropagation());
|
|
2172
|
+
textarea.addEventListener("keydown", (event) => {
|
|
2173
|
+
if (event.key === "Escape") {
|
|
2174
|
+
event.preventDefault();
|
|
2175
|
+
this.stopTextEdit({ commit: false });
|
|
2176
|
+
return;
|
|
2177
|
+
}
|
|
2178
|
+
if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
|
|
2179
|
+
event.preventDefault();
|
|
2180
|
+
this.stopTextEdit({ commit: true });
|
|
2181
|
+
}
|
|
2182
|
+
});
|
|
2183
|
+
textarea.addEventListener("blur", () => {
|
|
2184
|
+
if (this.textEditor?.textarea === textarea) {
|
|
2185
|
+
this.stopTextEdit({ commit: true });
|
|
2186
|
+
}
|
|
2187
|
+
});
|
|
2188
|
+
this.textEditor = {
|
|
2189
|
+
nodeId: node.id,
|
|
2190
|
+
previousText: node.text,
|
|
2191
|
+
textarea
|
|
2192
|
+
};
|
|
2193
|
+
this.canvasEl.appendChild(textarea);
|
|
2194
|
+
this.render({ inspector: true, json: false });
|
|
2195
|
+
window.requestAnimationFrame(() => {
|
|
2196
|
+
if (this.textEditor?.textarea === textarea) {
|
|
2197
|
+
textarea.focus();
|
|
2198
|
+
textarea.select();
|
|
2199
|
+
}
|
|
2200
|
+
});
|
|
2201
|
+
return true;
|
|
2202
|
+
}
|
|
2203
|
+
stopTextEdit(options = {}) {
|
|
2204
|
+
if (!this.textEditor) return false;
|
|
2205
|
+
const commit = options.commit !== false;
|
|
2206
|
+
const editor = this.textEditor;
|
|
2207
|
+
const nextText = editor.textarea.value;
|
|
2208
|
+
this.textEditor = null;
|
|
2209
|
+
editor.textarea.remove();
|
|
2210
|
+
if (commit && !this.readonly && nextText !== editor.previousText) {
|
|
2211
|
+
this.updateNode(
|
|
2212
|
+
editor.nodeId,
|
|
2213
|
+
{ text: nextText },
|
|
2214
|
+
{ inspector: true, reason: "node:update" }
|
|
2215
|
+
);
|
|
2216
|
+
} else {
|
|
2217
|
+
this.render({ scene: true, inspector: false, json: false });
|
|
2218
|
+
}
|
|
2219
|
+
return true;
|
|
2220
|
+
}
|
|
2221
|
+
positionTextEditor() {
|
|
2222
|
+
if (!this.textEditor) return;
|
|
2223
|
+
const node = this.findNode(this.textEditor.nodeId);
|
|
2224
|
+
if (!node) {
|
|
2225
|
+
this.stopTextEdit({ commit: false });
|
|
2226
|
+
return;
|
|
2227
|
+
}
|
|
2228
|
+
const viewport = this.documentData.viewport;
|
|
2229
|
+
const scale = viewport.scale;
|
|
2230
|
+
const textarea = this.textEditor.textarea;
|
|
2231
|
+
const width = node.width * scale;
|
|
2232
|
+
const height = node.height * scale;
|
|
2233
|
+
const metrics = getNodeFontMetrics(node);
|
|
2234
|
+
const lineHeight = metrics.lineHeight * scale;
|
|
2235
|
+
const lineCount = Math.max(1, textarea.value.split(/\r?\n/).length);
|
|
2236
|
+
const horizontalPadding = (node.type === "textbox" ? 16 : 12) * scale;
|
|
2237
|
+
const verticalPadding = node.type === "textbox" ? 12 * scale : Math.max(4, (height - lineCount * lineHeight) / 2);
|
|
2238
|
+
textarea.style.left = `${formatNumber(viewport.x + node.x * scale)}px`;
|
|
2239
|
+
textarea.style.top = `${formatNumber(viewport.y + node.y * scale)}px`;
|
|
2240
|
+
textarea.style.width = `${formatNumber(width)}px`;
|
|
2241
|
+
textarea.style.height = `${formatNumber(height)}px`;
|
|
2242
|
+
textarea.style.fontSize = `${formatNumber(metrics.fontSize * scale)}px`;
|
|
2243
|
+
textarea.style.lineHeight = `${formatNumber(lineHeight)}px`;
|
|
2244
|
+
textarea.style.padding = `${formatNumber(verticalPadding)}px ${formatNumber(horizontalPadding)}px`;
|
|
2245
|
+
}
|
|
2246
|
+
render(options = {}) {
|
|
2247
|
+
if (this.destroyed) return;
|
|
2248
|
+
const shouldRenderScene = options.scene !== false;
|
|
2249
|
+
const shouldRenderInspector = options.inspector !== false;
|
|
2250
|
+
const shouldRenderJson = options.json !== false;
|
|
2251
|
+
if (shouldRenderScene) this.renderScene();
|
|
2252
|
+
if (shouldRenderInspector) this.renderSelectionPanel();
|
|
2253
|
+
if (shouldRenderJson) this.syncJsonTextarea();
|
|
2254
|
+
this.updateToolbarLabel();
|
|
2255
|
+
this.updatePaletteState();
|
|
2256
|
+
this.positionTextEditor();
|
|
2257
|
+
}
|
|
2258
|
+
renderScene() {
|
|
2259
|
+
const viewport = this.documentData.viewport;
|
|
2260
|
+
this.world.setAttribute(
|
|
2261
|
+
"transform",
|
|
2262
|
+
`matrix(${viewport.scale} 0 0 ${viewport.scale} ${viewport.x} ${viewport.y})`
|
|
2263
|
+
);
|
|
2264
|
+
clearChildren(this.edgesLayer);
|
|
2265
|
+
clearChildren(this.previewLayer);
|
|
2266
|
+
clearChildren(this.nodesLayer);
|
|
2267
|
+
clearChildren(this.overlayLayer);
|
|
2268
|
+
const nodeMap = new Map(this.documentData.nodes.map((node) => [node.id, node]));
|
|
2269
|
+
this.documentData.edges.forEach((edge) => {
|
|
2270
|
+
const edgeElement = this.renderEdge(edge, nodeMap);
|
|
2271
|
+
if (edgeElement) this.edgesLayer.appendChild(edgeElement);
|
|
2272
|
+
});
|
|
2273
|
+
if (this.interaction?.kind === "connect" || this.interaction?.kind === "reconnect") {
|
|
2274
|
+
this.previewLayer.appendChild(this.renderPreviewEdge(this.interaction));
|
|
2275
|
+
}
|
|
2276
|
+
this.documentData.nodes.forEach((node) => {
|
|
2277
|
+
this.nodesLayer.appendChild(this.renderNode(node));
|
|
2278
|
+
});
|
|
2279
|
+
if (this.reconnectEdgeId && !this.readonly) {
|
|
2280
|
+
const reconnectEdge = this.findEdge(this.reconnectEdgeId);
|
|
2281
|
+
if (reconnectEdge) {
|
|
2282
|
+
const fromNode = nodeMap.get(reconnectEdge.from.nodeId);
|
|
2283
|
+
const toNode = nodeMap.get(reconnectEdge.to.nodeId);
|
|
2284
|
+
if (fromNode && toNode) {
|
|
2285
|
+
this.overlayLayer.appendChild(
|
|
2286
|
+
this.renderReconnectEndpoints(reconnectEdge, fromNode, toNode)
|
|
2287
|
+
);
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
this.syncConnectingState();
|
|
2292
|
+
}
|
|
2293
|
+
applyEdgeMarkers(pathElement, edge) {
|
|
2294
|
+
if (edge.startMarker && edge.startMarker !== "none") {
|
|
2295
|
+
pathElement.setAttribute(
|
|
2296
|
+
"marker-start",
|
|
2297
|
+
`url(#${this.getMarkerId(edge.startMarker, "start", this.getEdgeStrokeWidth(edge))})`
|
|
2298
|
+
);
|
|
2299
|
+
}
|
|
2300
|
+
if (edge.endMarker && edge.endMarker !== "none") {
|
|
2301
|
+
pathElement.setAttribute(
|
|
2302
|
+
"marker-end",
|
|
2303
|
+
`url(#${this.getMarkerId(edge.endMarker, "end", this.getEdgeStrokeWidth(edge))})`
|
|
2304
|
+
);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
renderEdge(edge, nodeMap) {
|
|
2308
|
+
const fromNode = nodeMap.get(edge.from.nodeId);
|
|
2309
|
+
const toNode = nodeMap.get(edge.to.nodeId);
|
|
2310
|
+
if (!fromNode || !toNode) return null;
|
|
2311
|
+
const edgePath = buildEdgePath(edge, fromNode, toNode);
|
|
2312
|
+
const selected = this.selection?.kind === "edge" && this.selection.id === edge.id;
|
|
2313
|
+
const inReconnectMode = this.reconnectEdgeId === edge.id;
|
|
2314
|
+
const group = svgEl("g", {
|
|
2315
|
+
class: `vd-flowchart-edge${selected ? " is-selected" : ""}${inReconnectMode ? " is-reconnecting" : ""}`,
|
|
2316
|
+
"data-edge-id": edge.id
|
|
2317
|
+
});
|
|
2318
|
+
const strokeWidth = this.getEdgeStrokeWidth(edge);
|
|
2319
|
+
if (selected) {
|
|
2320
|
+
group.appendChild(
|
|
2321
|
+
svgEl("path", {
|
|
2322
|
+
class: "vd-flowchart-edge-selection",
|
|
2323
|
+
d: edgePath.d,
|
|
2324
|
+
"stroke-width": formatNumber(strokeWidth + 7)
|
|
2325
|
+
})
|
|
2326
|
+
);
|
|
2327
|
+
}
|
|
2328
|
+
const visiblePath = svgEl("path", {
|
|
2329
|
+
class: "vd-flowchart-edge-path",
|
|
2330
|
+
d: edgePath.d,
|
|
2331
|
+
"stroke-width": strokeWidth,
|
|
2332
|
+
"data-edge-id": edge.id
|
|
2333
|
+
});
|
|
2334
|
+
this.applyEdgeMarkers(visiblePath, edge);
|
|
2335
|
+
const hitPath = svgEl("path", {
|
|
2336
|
+
class: "vd-flowchart-edge-hit",
|
|
2337
|
+
d: edgePath.d,
|
|
2338
|
+
"stroke-width": this.getEdgeHitStrokeWidth(edge),
|
|
2339
|
+
"data-edge-id": edge.id
|
|
2340
|
+
});
|
|
2341
|
+
group.appendChild(visiblePath);
|
|
2342
|
+
group.appendChild(hitPath);
|
|
2343
|
+
if (edge.label) {
|
|
2344
|
+
const label = svgEl("text", {
|
|
2345
|
+
class: "vd-flowchart-edge-label",
|
|
2346
|
+
x: edgePath.labelX,
|
|
2347
|
+
y: edgePath.labelY - 8,
|
|
2348
|
+
"text-anchor": "middle",
|
|
2349
|
+
"data-edge-id": edge.id
|
|
2350
|
+
});
|
|
2351
|
+
label.textContent = edge.label;
|
|
2352
|
+
group.appendChild(label);
|
|
2353
|
+
}
|
|
2354
|
+
return group;
|
|
2355
|
+
}
|
|
2356
|
+
renderReconnectEndpoints(edge, fromNode, toNode) {
|
|
2357
|
+
const scale = this.documentData.viewport.scale || 1;
|
|
2358
|
+
const group = svgEl("g", { class: "vd-flowchart-edge-endpoints" });
|
|
2359
|
+
[
|
|
2360
|
+
{ endpoint: "from", point: getPortPosition(fromNode, edge.from.port) },
|
|
2361
|
+
{ endpoint: "to", point: getPortPosition(toNode, edge.to.port) }
|
|
2362
|
+
].forEach(({ endpoint, point }) => {
|
|
2363
|
+
group.appendChild(
|
|
2364
|
+
svgEl("circle", {
|
|
2365
|
+
class: "vd-flowchart-edge-endpoint-hit",
|
|
2366
|
+
cx: point.x,
|
|
2367
|
+
cy: point.y,
|
|
2368
|
+
r: RECONNECT_ENDPOINT_HIT_RADIUS / scale,
|
|
2369
|
+
"data-edge-id": edge.id,
|
|
2370
|
+
"data-edge-endpoint": endpoint
|
|
2371
|
+
})
|
|
2372
|
+
);
|
|
2373
|
+
group.appendChild(
|
|
2374
|
+
svgEl("circle", {
|
|
2375
|
+
class: "vd-flowchart-edge-endpoint",
|
|
2376
|
+
cx: point.x,
|
|
2377
|
+
cy: point.y,
|
|
2378
|
+
r: RECONNECT_ENDPOINT_RADIUS / scale,
|
|
2379
|
+
"data-edge-id": edge.id,
|
|
2380
|
+
"data-edge-endpoint": endpoint
|
|
2381
|
+
})
|
|
2382
|
+
);
|
|
2383
|
+
});
|
|
2384
|
+
return group;
|
|
2385
|
+
}
|
|
2386
|
+
renderPreviewEdge(interaction) {
|
|
2387
|
+
const fromPort = interaction.fromPort || interaction.source?.port || "right";
|
|
2388
|
+
const toPort = interaction.toPort || interaction.target?.port || "left";
|
|
2389
|
+
const edgePath = buildEdgePath({
|
|
2390
|
+
strokeWidth: interaction.strokeWidth,
|
|
2391
|
+
route: interaction.route,
|
|
2392
|
+
fromPoint: interaction.fromPoint,
|
|
2393
|
+
toPoint: interaction.toPoint,
|
|
2394
|
+
from: { port: fromPort },
|
|
2395
|
+
to: { port: toPort }
|
|
2396
|
+
});
|
|
2397
|
+
return svgEl("path", {
|
|
2398
|
+
class: "vd-flowchart-preview-path",
|
|
2399
|
+
d: edgePath.d,
|
|
2400
|
+
"stroke-width": this.getEdgeStrokeWidth(interaction)
|
|
2401
|
+
});
|
|
2402
|
+
}
|
|
2403
|
+
renderNode(node) {
|
|
2404
|
+
const selected = this.selection?.kind === "node" && this.selection.id === node.id;
|
|
2405
|
+
const dragging = this.interaction?.kind === "drag-node" && this.interaction.nodeId === node.id;
|
|
2406
|
+
const resizing = this.interaction?.kind === "resize-node" && this.interaction.nodeId === node.id;
|
|
2407
|
+
const editing = this.textEditor?.nodeId === node.id;
|
|
2408
|
+
const group = svgEl("g", {
|
|
2409
|
+
class: `vd-flowchart-node${selected ? " is-selected" : ""}${dragging ? " is-dragging" : ""}${resizing ? " is-resizing" : ""}${editing ? " is-editing" : ""}`,
|
|
2410
|
+
transform: `translate(${formatNumber(node.x)} ${formatNumber(node.y)})`,
|
|
2411
|
+
"data-node-id": node.id
|
|
2412
|
+
});
|
|
2413
|
+
const portsVisible = this.shouldShowNodePorts(node);
|
|
2414
|
+
const hitbox = svgEl("rect", {
|
|
2415
|
+
x: 0,
|
|
2416
|
+
y: 0,
|
|
2417
|
+
width: node.width,
|
|
2418
|
+
height: node.height,
|
|
2419
|
+
rx: node.type === "rounded-rect" || node.type === "textbox" ? 16 : node.type === "junction" ? node.width / 2 : 6,
|
|
2420
|
+
fill: "transparent",
|
|
2421
|
+
"data-node-id": node.id
|
|
2422
|
+
});
|
|
2423
|
+
group.appendChild(hitbox);
|
|
2424
|
+
group.appendChild(this.renderNodeShape(node));
|
|
2425
|
+
group.appendChild(this.renderNodeText(node));
|
|
2426
|
+
if (selected && !this.readonly && isNodeResizable(node)) {
|
|
2427
|
+
group.appendChild(this.renderResizeControls(node));
|
|
2428
|
+
}
|
|
2429
|
+
const scale = this.documentData.viewport.scale || 1;
|
|
2430
|
+
FLOWCHART_PORTS.forEach((port) => {
|
|
2431
|
+
const position = getPortPosition({ ...node, x: 0, y: 0 }, port);
|
|
2432
|
+
const portGroup = svgEl("g", {
|
|
2433
|
+
class: `vd-flowchart-port-group${portsVisible ? " is-visible" : ""}`,
|
|
2434
|
+
"data-node-id": node.id,
|
|
2435
|
+
"data-port": port
|
|
2436
|
+
});
|
|
2437
|
+
portGroup.appendChild(
|
|
2438
|
+
svgEl("circle", {
|
|
2439
|
+
class: "vd-flowchart-port-hit",
|
|
2440
|
+
cx: position.x,
|
|
2441
|
+
cy: position.y,
|
|
2442
|
+
r: CONNECTION_PORT_HIT_RADIUS / scale
|
|
2443
|
+
})
|
|
2444
|
+
);
|
|
2445
|
+
portGroup.appendChild(
|
|
2446
|
+
svgEl("circle", {
|
|
2447
|
+
class: "vd-flowchart-port",
|
|
2448
|
+
cx: position.x,
|
|
2449
|
+
cy: position.y,
|
|
2450
|
+
r: CONNECTION_PORT_RADIUS / scale
|
|
2451
|
+
})
|
|
2452
|
+
);
|
|
2453
|
+
group.appendChild(portGroup);
|
|
2454
|
+
});
|
|
2455
|
+
return group;
|
|
2456
|
+
}
|
|
2457
|
+
renderResizeControls(node) {
|
|
2458
|
+
const scale = this.documentData.viewport.scale || 1;
|
|
2459
|
+
const zone = 14 / scale;
|
|
2460
|
+
const cornerZone = 22 / scale;
|
|
2461
|
+
const handleSize = 9 / scale;
|
|
2462
|
+
const gap = RESIZE_PORT_GAP / scale;
|
|
2463
|
+
const group = svgEl("g", { class: "vd-flowchart-resize-controls" });
|
|
2464
|
+
const zones = {
|
|
2465
|
+
n: { x: 0, y: -zone / 2, width: node.width, height: zone },
|
|
2466
|
+
e: { x: node.width - zone / 2, y: 0, width: zone, height: node.height },
|
|
2467
|
+
s: { x: 0, y: node.height - zone / 2, width: node.width, height: zone },
|
|
2468
|
+
w: { x: -zone / 2, y: 0, width: zone, height: node.height },
|
|
2469
|
+
ne: {
|
|
2470
|
+
x: node.width - cornerZone / 2,
|
|
2471
|
+
y: -cornerZone / 2,
|
|
2472
|
+
width: cornerZone,
|
|
2473
|
+
height: cornerZone
|
|
2474
|
+
},
|
|
2475
|
+
se: {
|
|
2476
|
+
x: node.width - cornerZone / 2,
|
|
2477
|
+
y: node.height - cornerZone / 2,
|
|
2478
|
+
width: cornerZone,
|
|
2479
|
+
height: cornerZone
|
|
2480
|
+
},
|
|
2481
|
+
sw: {
|
|
2482
|
+
x: -cornerZone / 2,
|
|
2483
|
+
y: node.height - cornerZone / 2,
|
|
2484
|
+
width: cornerZone,
|
|
2485
|
+
height: cornerZone
|
|
2486
|
+
},
|
|
2487
|
+
nw: { x: -cornerZone / 2, y: -cornerZone / 2, width: cornerZone, height: cornerZone }
|
|
2488
|
+
};
|
|
2489
|
+
RESIZE_HANDLES.forEach((handle) => {
|
|
2490
|
+
const zoneRect = zones[handle];
|
|
2491
|
+
const position = getResizeHandlePosition(node, handle);
|
|
2492
|
+
const dotOffset = 8 / scale;
|
|
2493
|
+
if (handle.includes("e")) position.x += dotOffset;
|
|
2494
|
+
if (handle.includes("w")) position.x -= dotOffset;
|
|
2495
|
+
if (handle.includes("n")) position.y -= dotOffset;
|
|
2496
|
+
if (handle.includes("s")) position.y += dotOffset;
|
|
2497
|
+
const cursor = getResizeCursor(handle);
|
|
2498
|
+
const zoneSegments = [];
|
|
2499
|
+
if (handle === "e" || handle === "w") {
|
|
2500
|
+
const upperHeight = Math.max(0, node.height / 2 - gap / 2);
|
|
2501
|
+
const lowerY = node.height / 2 + gap / 2;
|
|
2502
|
+
const lowerHeight = Math.max(0, node.height - lowerY);
|
|
2503
|
+
zoneSegments.push(
|
|
2504
|
+
{ x: zoneRect.x, y: zoneRect.y, width: zoneRect.width, height: upperHeight },
|
|
2505
|
+
{ x: zoneRect.x, y: lowerY, width: zoneRect.width, height: lowerHeight }
|
|
2506
|
+
);
|
|
2507
|
+
} else if (handle === "n" || handle === "s") {
|
|
2508
|
+
const leftWidth = Math.max(0, node.width / 2 - gap / 2);
|
|
2509
|
+
const rightX = node.width / 2 + gap / 2;
|
|
2510
|
+
const rightWidth = Math.max(0, node.width - rightX);
|
|
2511
|
+
zoneSegments.push(
|
|
2512
|
+
{ x: zoneRect.x, y: zoneRect.y, width: leftWidth, height: zoneRect.height },
|
|
2513
|
+
{ x: rightX, y: zoneRect.y, width: rightWidth, height: zoneRect.height }
|
|
2514
|
+
);
|
|
2515
|
+
} else {
|
|
2516
|
+
zoneSegments.push(zoneRect);
|
|
2517
|
+
}
|
|
2518
|
+
zoneSegments.filter((segment) => segment.width > 0 && segment.height > 0).forEach((segment) => {
|
|
2519
|
+
const hit = svgEl("rect", {
|
|
2520
|
+
class: "vd-flowchart-resize-zone",
|
|
2521
|
+
x: segment.x,
|
|
2522
|
+
y: segment.y,
|
|
2523
|
+
width: segment.width,
|
|
2524
|
+
height: segment.height,
|
|
2525
|
+
"data-node-id": node.id,
|
|
2526
|
+
"data-resize-handle": handle,
|
|
2527
|
+
style: `cursor: ${cursor}`
|
|
2528
|
+
});
|
|
2529
|
+
group.appendChild(hit);
|
|
2530
|
+
});
|
|
2531
|
+
const dot = svgEl("rect", {
|
|
2532
|
+
class: "vd-flowchart-resize-handle",
|
|
2533
|
+
x: position.x - handleSize / 2,
|
|
2534
|
+
y: position.y - handleSize / 2,
|
|
2535
|
+
width: handleSize,
|
|
2536
|
+
height: handleSize,
|
|
2537
|
+
rx: handleSize / 3,
|
|
2538
|
+
ry: handleSize / 3,
|
|
2539
|
+
"data-node-id": node.id,
|
|
2540
|
+
"data-resize-handle": handle,
|
|
2541
|
+
style: `cursor: ${cursor}`
|
|
2542
|
+
});
|
|
2543
|
+
group.appendChild(dot);
|
|
2544
|
+
});
|
|
2545
|
+
return group;
|
|
2546
|
+
}
|
|
2547
|
+
renderNodeShape(node) {
|
|
2548
|
+
const baseClass = `vd-flowchart-node-shape vd-flowchart-node-shape--${node.type}`;
|
|
2549
|
+
if (node.type === "rounded-rect") {
|
|
2550
|
+
return svgEl("rect", {
|
|
2551
|
+
class: baseClass,
|
|
2552
|
+
x: 0,
|
|
2553
|
+
y: 0,
|
|
2554
|
+
width: node.width,
|
|
2555
|
+
height: node.height,
|
|
2556
|
+
rx: 18,
|
|
2557
|
+
ry: 18
|
|
2558
|
+
});
|
|
2559
|
+
}
|
|
2560
|
+
if (node.type === "rect") {
|
|
2561
|
+
return svgEl("rect", {
|
|
2562
|
+
class: baseClass,
|
|
2563
|
+
x: 0,
|
|
2564
|
+
y: 0,
|
|
2565
|
+
width: node.width,
|
|
2566
|
+
height: node.height,
|
|
2567
|
+
rx: 2,
|
|
2568
|
+
ry: 2
|
|
2569
|
+
});
|
|
2570
|
+
}
|
|
2571
|
+
if (node.type === "diamond") {
|
|
2572
|
+
return svgEl("polygon", {
|
|
2573
|
+
class: baseClass,
|
|
2574
|
+
points: `${node.width / 2},0 ${node.width},${node.height / 2} ${node.width / 2},${node.height} 0,${node.height / 2}`
|
|
2575
|
+
});
|
|
2576
|
+
}
|
|
2577
|
+
if (node.type === "circle") {
|
|
2578
|
+
return svgEl("ellipse", {
|
|
2579
|
+
class: baseClass,
|
|
2580
|
+
cx: node.width / 2,
|
|
2581
|
+
cy: node.height / 2,
|
|
2582
|
+
rx: node.width / 2,
|
|
2583
|
+
ry: node.height / 2
|
|
2584
|
+
});
|
|
2585
|
+
}
|
|
2586
|
+
if (node.type === "junction") {
|
|
2587
|
+
return svgEl("circle", {
|
|
2588
|
+
class: baseClass,
|
|
2589
|
+
cx: node.width / 2,
|
|
2590
|
+
cy: node.height / 2,
|
|
2591
|
+
r: Math.min(node.width, node.height) / 2
|
|
2592
|
+
});
|
|
2593
|
+
}
|
|
2594
|
+
if (node.type === "textbox") {
|
|
2595
|
+
return svgEl("rect", {
|
|
2596
|
+
class: baseClass,
|
|
2597
|
+
x: 0,
|
|
2598
|
+
y: 0,
|
|
2599
|
+
width: node.width,
|
|
2600
|
+
height: node.height,
|
|
2601
|
+
rx: 14,
|
|
2602
|
+
ry: 14
|
|
2603
|
+
});
|
|
2604
|
+
}
|
|
2605
|
+
return svgEl("rect", {
|
|
2606
|
+
class: baseClass,
|
|
2607
|
+
x: 0,
|
|
2608
|
+
y: 0,
|
|
2609
|
+
width: node.width,
|
|
2610
|
+
height: node.height,
|
|
2611
|
+
rx: 0,
|
|
2612
|
+
ry: 0
|
|
2613
|
+
});
|
|
2614
|
+
}
|
|
2615
|
+
renderNodeText(node) {
|
|
2616
|
+
const textElement = svgEl("text", {
|
|
2617
|
+
class: `vd-flowchart-node-text vd-flowchart-node-text--${node.type}`,
|
|
2618
|
+
"data-node-id": node.id
|
|
2619
|
+
});
|
|
2620
|
+
if (node.type === "junction" || !node.text) {
|
|
2621
|
+
return textElement;
|
|
2622
|
+
}
|
|
2623
|
+
if (node.type === "textbox") {
|
|
2624
|
+
const lines2 = wrapText(node.text, estimateChars(node.width - 32));
|
|
2625
|
+
const { lineHeight: lineHeight2 } = getNodeFontMetrics(node);
|
|
2626
|
+
lines2.forEach((line, index) => {
|
|
2627
|
+
const span = svgEl("tspan", {
|
|
2628
|
+
x: 16,
|
|
2629
|
+
y: 28 + index * lineHeight2
|
|
2630
|
+
});
|
|
2631
|
+
span.textContent = line;
|
|
2632
|
+
textElement.appendChild(span);
|
|
2633
|
+
});
|
|
2634
|
+
return textElement;
|
|
2635
|
+
}
|
|
2636
|
+
const lines = wrapText(node.text, estimateChars(node.width));
|
|
2637
|
+
const { lineHeight } = getNodeFontMetrics(node);
|
|
2638
|
+
const totalHeight = (lines.length - 1) * lineHeight;
|
|
2639
|
+
const startY = node.height / 2 - totalHeight / 2;
|
|
2640
|
+
textElement.setAttribute("text-anchor", "middle");
|
|
2641
|
+
textElement.setAttribute("dominant-baseline", "middle");
|
|
2642
|
+
lines.forEach((line, index) => {
|
|
2643
|
+
const span = svgEl("tspan", {
|
|
2644
|
+
x: node.width / 2,
|
|
2645
|
+
y: startY + index * lineHeight,
|
|
2646
|
+
"text-anchor": "middle",
|
|
2647
|
+
"dominant-baseline": "middle"
|
|
2648
|
+
});
|
|
2649
|
+
span.textContent = line;
|
|
2650
|
+
textElement.appendChild(span);
|
|
2651
|
+
});
|
|
2652
|
+
return textElement;
|
|
2653
|
+
}
|
|
2654
|
+
renderSelectionPanel() {
|
|
2655
|
+
clearChildren(this.selectionFields);
|
|
2656
|
+
this.deleteButton.disabled = this.readonly || !this.selection;
|
|
2657
|
+
if (!this.selection) {
|
|
2658
|
+
this.selectionMeta.textContent = "Nothing selected";
|
|
2659
|
+
this.selectionFields.appendChild(
|
|
2660
|
+
createElement("p", {
|
|
2661
|
+
className: "vd-flowchart-selection-empty",
|
|
2662
|
+
text: "Select a node or edge to edit it."
|
|
2663
|
+
})
|
|
2664
|
+
);
|
|
2665
|
+
return;
|
|
2666
|
+
}
|
|
2667
|
+
if (this.selection.kind === "node") {
|
|
2668
|
+
const node = this.findNode(this.selection.id);
|
|
2669
|
+
if (!node) {
|
|
2670
|
+
this.selection = null;
|
|
2671
|
+
this.renderSelectionPanel();
|
|
2672
|
+
return;
|
|
2673
|
+
}
|
|
2674
|
+
this.selectionMeta.textContent = `Node ${node.id} \xB7 ${node.type} \xB7 ${Math.round(node.x)}, ${Math.round(node.y)}`;
|
|
2675
|
+
const typeSelect = createElement("select");
|
|
2676
|
+
typeSelect.setAttribute("data-field", "node-type");
|
|
2677
|
+
FLOWCHART_NODE_TYPES.forEach((type) => {
|
|
2678
|
+
const option = createElement("option", { value: type, text: type.replace("-", " ") });
|
|
2679
|
+
option.value = type;
|
|
2680
|
+
option.selected = type === node.type;
|
|
2681
|
+
typeSelect.appendChild(option);
|
|
2682
|
+
});
|
|
2683
|
+
const textArea = createElement("textarea", { value: node.text, rows: 5 });
|
|
2684
|
+
textArea.setAttribute("data-field", "node-text");
|
|
2685
|
+
const widthInput = createElement("input", { value: node.width, type: "number" });
|
|
2686
|
+
widthInput.setAttribute("data-field", "node-width");
|
|
2687
|
+
widthInput.setAttribute("min", String(MIN_NODE_SIZE));
|
|
2688
|
+
widthInput.setAttribute("max", String(MAX_NODE_SIZE));
|
|
2689
|
+
const heightInput = createElement("input", { value: node.height, type: "number" });
|
|
2690
|
+
heightInput.setAttribute("data-field", "node-height");
|
|
2691
|
+
heightInput.setAttribute("min", String(MIN_NODE_SIZE));
|
|
2692
|
+
heightInput.setAttribute("max", String(MAX_NODE_SIZE));
|
|
2693
|
+
this.selectionFields.appendChild(createField("Type", typeSelect));
|
|
2694
|
+
if (isNodeTextEditable(node)) {
|
|
2695
|
+
this.selectionFields.appendChild(createField("Text", textArea));
|
|
2696
|
+
}
|
|
2697
|
+
if (isNodeResizable(node)) {
|
|
2698
|
+
const sizeGrid = createElement("div", { className: "vd-flowchart-field-grid" });
|
|
2699
|
+
sizeGrid.appendChild(createField("Width", widthInput));
|
|
2700
|
+
sizeGrid.appendChild(createField("Height", heightInput));
|
|
2701
|
+
this.selectionFields.appendChild(sizeGrid);
|
|
2702
|
+
} else {
|
|
2703
|
+
this.selectionFields.appendChild(
|
|
2704
|
+
createElement("p", {
|
|
2705
|
+
className: "vd-flowchart-selection-empty",
|
|
2706
|
+
text: "Junctions stay fixed-size and do not carry inline text."
|
|
2707
|
+
})
|
|
2708
|
+
);
|
|
2709
|
+
}
|
|
2710
|
+
return;
|
|
2711
|
+
}
|
|
2712
|
+
const edge = this.findEdge(this.selection.id);
|
|
2713
|
+
if (!edge) {
|
|
2714
|
+
this.selection = null;
|
|
2715
|
+
this.renderSelectionPanel();
|
|
2716
|
+
return;
|
|
2717
|
+
}
|
|
2718
|
+
this.selectionMeta.textContent = `Edge ${edge.id} \xB7 ${edge.route} \xB7 ${formatNumber(edge.strokeWidth)}px \xB7 ${edge.startMarker} \u2192 ${edge.endMarker}`;
|
|
2719
|
+
const routeSelect = createElement("select");
|
|
2720
|
+
routeSelect.setAttribute("data-field", "edge-route");
|
|
2721
|
+
FLOWCHART_EDGE_ROUTES.forEach((route) => {
|
|
2722
|
+
const option = createElement("option", {
|
|
2723
|
+
value: route,
|
|
2724
|
+
text: FLOWCHART_EDGE_ROUTE_LABELS[route]
|
|
2725
|
+
});
|
|
2726
|
+
option.value = route;
|
|
2727
|
+
option.selected = route === edge.route;
|
|
2728
|
+
routeSelect.appendChild(option);
|
|
2729
|
+
});
|
|
2730
|
+
const startSelect = createElement("select");
|
|
2731
|
+
startSelect.setAttribute("data-field", "edge-start-marker");
|
|
2732
|
+
FLOWCHART_EDGE_MARKERS.forEach((marker) => {
|
|
2733
|
+
const option = createElement("option", { value: marker, text: marker });
|
|
2734
|
+
option.value = marker;
|
|
2735
|
+
option.selected = marker === edge.startMarker;
|
|
2736
|
+
startSelect.appendChild(option);
|
|
2737
|
+
});
|
|
2738
|
+
const endSelect = createElement("select");
|
|
2739
|
+
endSelect.setAttribute("data-field", "edge-end-marker");
|
|
2740
|
+
FLOWCHART_EDGE_MARKERS.forEach((marker) => {
|
|
2741
|
+
const option = createElement("option", { value: marker, text: marker });
|
|
2742
|
+
option.value = marker;
|
|
2743
|
+
option.selected = marker === edge.endMarker;
|
|
2744
|
+
endSelect.appendChild(option);
|
|
2745
|
+
});
|
|
2746
|
+
const widthSelect = createElement("select");
|
|
2747
|
+
widthSelect.setAttribute("data-field", "edge-stroke-preset");
|
|
2748
|
+
EDGE_STROKE_PRESETS.forEach((preset) => {
|
|
2749
|
+
const option = createElement("option", { value: preset.id, text: preset.label });
|
|
2750
|
+
option.value = preset.id;
|
|
2751
|
+
option.selected = preset.id === getStrokePresetId(edge.strokeWidth);
|
|
2752
|
+
widthSelect.appendChild(option);
|
|
2753
|
+
});
|
|
2754
|
+
const labelInput = createElement("textarea", { value: edge.label, rows: 4 });
|
|
2755
|
+
labelInput.setAttribute("data-field", "edge-label");
|
|
2756
|
+
const markerGrid = createElement("div", { className: "vd-flowchart-field-grid" });
|
|
2757
|
+
markerGrid.appendChild(createField("Route", routeSelect));
|
|
2758
|
+
markerGrid.appendChild(createField("Weight", widthSelect));
|
|
2759
|
+
markerGrid.appendChild(createField("Start", startSelect));
|
|
2760
|
+
markerGrid.appendChild(createField("End", endSelect));
|
|
2761
|
+
this.selectionFields.appendChild(markerGrid);
|
|
2762
|
+
this.selectionFields.appendChild(createField("Label", labelInput));
|
|
2763
|
+
}
|
|
2764
|
+
syncJsonTextarea(force = false) {
|
|
2765
|
+
if (!force && document.activeElement === this.jsonTextarea) {
|
|
2766
|
+
return;
|
|
2767
|
+
}
|
|
2768
|
+
this.jsonTextarea.value = JSON.stringify(this.toJSON(), null, 2);
|
|
2769
|
+
}
|
|
2770
|
+
getSelectionSnapshot() {
|
|
2771
|
+
if (!this.selection) return null;
|
|
2772
|
+
if (this.selection.kind === "node") {
|
|
2773
|
+
const node = this.findNode(this.selection.id);
|
|
2774
|
+
return node ? { kind: "node", id: node.id, node: deepClone(node) } : null;
|
|
2775
|
+
}
|
|
2776
|
+
const edge = this.findEdge(this.selection.id);
|
|
2777
|
+
return edge ? { kind: "edge", id: edge.id, edge: deepClone(edge) } : null;
|
|
2778
|
+
}
|
|
2779
|
+
select(selection) {
|
|
2780
|
+
const next = selection && selection.id && selection.kind ? { kind: selection.kind, id: selection.id } : null;
|
|
2781
|
+
const previousKey = this.selection ? `${this.selection.kind}:${this.selection.id}` : "";
|
|
2782
|
+
const nextKey = next ? `${next.kind}:${next.id}` : "";
|
|
2783
|
+
this.selection = next;
|
|
2784
|
+
if (previousKey !== nextKey) {
|
|
2785
|
+
this.render({ scene: true, inspector: true, json: false });
|
|
2786
|
+
this.emit("select", { selection: this.getSelectionSnapshot() });
|
|
2787
|
+
return;
|
|
2788
|
+
}
|
|
2789
|
+
this.render({ scene: true, inspector: false, json: false });
|
|
2790
|
+
}
|
|
2791
|
+
selectNode(nodeId) {
|
|
2792
|
+
this.select({ kind: "node", id: sanitizeId(nodeId) });
|
|
2793
|
+
return this;
|
|
2794
|
+
}
|
|
2795
|
+
selectEdge(edgeId) {
|
|
2796
|
+
this.select({ kind: "edge", id: sanitizeId(edgeId) });
|
|
2797
|
+
return this;
|
|
2798
|
+
}
|
|
2799
|
+
deselect() {
|
|
2800
|
+
this.select(null);
|
|
2801
|
+
return this;
|
|
2802
|
+
}
|
|
2803
|
+
resolvePreservedSelection(previousSelection, preserve) {
|
|
2804
|
+
if (!preserve || !previousSelection) return null;
|
|
2805
|
+
const exists = previousSelection.kind === "node" ? Boolean(this.findNode(previousSelection.id)) : Boolean(this.findEdge(previousSelection.id));
|
|
2806
|
+
return exists ? { kind: previousSelection.kind, id: previousSelection.id } : null;
|
|
2807
|
+
}
|
|
2808
|
+
findNode(nodeId) {
|
|
2809
|
+
return this.documentData.nodes.find((node) => node.id === nodeId) || null;
|
|
2810
|
+
}
|
|
2811
|
+
findEdge(edgeId) {
|
|
2812
|
+
return this.documentData.edges.find((edge) => edge.id === edgeId) || null;
|
|
2813
|
+
}
|
|
2814
|
+
emit(eventName, payload) {
|
|
2815
|
+
const listeners = this.listeners[eventName];
|
|
2816
|
+
if (!listeners || !listeners.size) return;
|
|
2817
|
+
listeners.forEach((listener) => listener(payload));
|
|
2818
|
+
}
|
|
2819
|
+
emitChange(reason, extra = {}) {
|
|
2820
|
+
if (this.historyEnabled && !this.isApplyingHistory) {
|
|
2821
|
+
this.recordHistory(reason);
|
|
2822
|
+
}
|
|
2823
|
+
this.syncJsonTextarea();
|
|
2824
|
+
this.emit("change", {
|
|
2825
|
+
reason,
|
|
2826
|
+
document: this.toJSON(),
|
|
2827
|
+
...extra
|
|
2828
|
+
});
|
|
2829
|
+
}
|
|
2830
|
+
emitViewportChange(reason) {
|
|
2831
|
+
const payload = {
|
|
2832
|
+
reason,
|
|
2833
|
+
viewport: deepClone(this.documentData.viewport),
|
|
2834
|
+
document: this.toJSON()
|
|
2835
|
+
};
|
|
2836
|
+
this.syncJsonTextarea();
|
|
2837
|
+
this.emit("viewport", payload);
|
|
2838
|
+
this.emit("change", payload);
|
|
2839
|
+
}
|
|
2840
|
+
on(eventName, callback) {
|
|
2841
|
+
if (!this.listeners[eventName]) {
|
|
2842
|
+
this.listeners[eventName] = /* @__PURE__ */ new Set();
|
|
2843
|
+
}
|
|
2844
|
+
this.listeners[eventName].add(callback);
|
|
2845
|
+
return this;
|
|
2846
|
+
}
|
|
2847
|
+
off(eventName, callback) {
|
|
2848
|
+
this.listeners[eventName]?.delete(callback);
|
|
2849
|
+
return this;
|
|
2850
|
+
}
|
|
2851
|
+
// --- History (undo / redo) ----------------------------------------------
|
|
2852
|
+
// Every document mutation funnels through emitChange(), so recording there
|
|
2853
|
+
// captures all of them with exactly one entry per committed gesture (live
|
|
2854
|
+
// drag/resize only render()). Viewport pan/zoom go through emitViewportChange
|
|
2855
|
+
// and are intentionally not recorded — undo is for content, not the camera.
|
|
2856
|
+
seedHistory() {
|
|
2857
|
+
if (!this.historyEnabled) return;
|
|
2858
|
+
this.history = [{ reason: "init", targetKey: "", snapshot: this.toJSON() }];
|
|
2859
|
+
this.historyIndex = 0;
|
|
2860
|
+
}
|
|
2861
|
+
recordHistory(reason) {
|
|
2862
|
+
const snapshot = this.toJSON();
|
|
2863
|
+
const targetKey = this.selection ? `${this.selection.kind}:${this.selection.id}` : "";
|
|
2864
|
+
const top = this.history[this.historyIndex];
|
|
2865
|
+
const canCoalesce = Boolean(top) && this.historyIndex === this.history.length - 1 && COALESCING_REASONS.has(reason) && top.reason === reason && top.targetKey === targetKey;
|
|
2866
|
+
if (canCoalesce) {
|
|
2867
|
+
top.snapshot = snapshot;
|
|
2868
|
+
return;
|
|
2869
|
+
}
|
|
2870
|
+
if (this.historyIndex < this.history.length - 1) {
|
|
2871
|
+
this.history.length = this.historyIndex + 1;
|
|
2872
|
+
}
|
|
2873
|
+
this.history.push({ reason, targetKey, snapshot });
|
|
2874
|
+
if (this.history.length > this.historyLimit) {
|
|
2875
|
+
this.history.shift();
|
|
2876
|
+
}
|
|
2877
|
+
this.historyIndex = this.history.length - 1;
|
|
2878
|
+
this.emitHistoryState(reason);
|
|
2879
|
+
}
|
|
2880
|
+
applyHistorySnapshot(snapshot, reason) {
|
|
2881
|
+
this.isApplyingHistory = true;
|
|
2882
|
+
this.stopTextEdit({ commit: false });
|
|
2883
|
+
this.activeTool = null;
|
|
2884
|
+
this.reconnectEdgeId = null;
|
|
2885
|
+
const currentViewport = deepClone(this.documentData.viewport);
|
|
2886
|
+
const previousSelection = this.selection;
|
|
2887
|
+
this.documentData = normalizeDocument(snapshot);
|
|
2888
|
+
this.documentData.viewport = normalizeViewport(currentViewport);
|
|
2889
|
+
this.selection = this.resolvePreservedSelection(previousSelection, true);
|
|
2890
|
+
this.render();
|
|
2891
|
+
this.emitChange(reason);
|
|
2892
|
+
this.isApplyingHistory = false;
|
|
2893
|
+
this.emitHistoryState(reason);
|
|
2894
|
+
}
|
|
2895
|
+
emitHistoryState(reason) {
|
|
2896
|
+
this.updateHistoryButtons();
|
|
2897
|
+
this.emit("history", { reason, canUndo: this.canUndo(), canRedo: this.canRedo() });
|
|
2898
|
+
}
|
|
2899
|
+
canUndo() {
|
|
2900
|
+
return this.historyEnabled && this.historyIndex > 0;
|
|
2901
|
+
}
|
|
2902
|
+
canRedo() {
|
|
2903
|
+
return this.historyEnabled && this.historyIndex < this.history.length - 1;
|
|
2904
|
+
}
|
|
2905
|
+
undo() {
|
|
2906
|
+
if (!this.canUndo()) return this;
|
|
2907
|
+
this.historyIndex -= 1;
|
|
2908
|
+
this.applyHistorySnapshot(this.history[this.historyIndex].snapshot, "undo");
|
|
2909
|
+
return this;
|
|
2910
|
+
}
|
|
2911
|
+
redo() {
|
|
2912
|
+
if (!this.canRedo()) return this;
|
|
2913
|
+
this.historyIndex += 1;
|
|
2914
|
+
this.applyHistorySnapshot(this.history[this.historyIndex].snapshot, "redo");
|
|
2915
|
+
return this;
|
|
2916
|
+
}
|
|
2917
|
+
clearHistory() {
|
|
2918
|
+
this.seedHistory();
|
|
2919
|
+
this.emitHistoryState("history:clear");
|
|
2920
|
+
return this;
|
|
2921
|
+
}
|
|
2922
|
+
updateHistoryButtons() {
|
|
2923
|
+
if (this.undoButton) this.undoButton.disabled = this.readonly || !this.canUndo();
|
|
2924
|
+
if (this.redoButton) this.redoButton.disabled = this.readonly || !this.canRedo();
|
|
2925
|
+
}
|
|
2926
|
+
// Resolve a `relativeTo` anchor into a top-left position for a new node of the
|
|
2927
|
+
// given spec. Accepts a bare node id or `{ node, direction, distance, angle }`;
|
|
2928
|
+
// `direction` (right/down/left/up) is sugar for an angle in screen space
|
|
2929
|
+
// (y-down). Returns null when the anchor node can't be found.
|
|
2930
|
+
resolveRelativePosition(relativeTo, spec) {
|
|
2931
|
+
const ref2 = typeof relativeTo === "string" ? { node: relativeTo } : relativeTo || {};
|
|
2932
|
+
const anchor = this.findNode(sanitizeId(ref2.node ?? ref2.nodeId ?? ref2.id));
|
|
2933
|
+
if (!anchor) return null;
|
|
2934
|
+
const directionAngles = { right: 0, down: 90, left: 180, up: -90 };
|
|
2935
|
+
const angleDeg = ref2.angle != null ? toFiniteNumber(ref2.angle, 0) : directionAngles[ref2.direction] ?? 0;
|
|
2936
|
+
const distance = Math.max(0, toFiniteNumber(ref2.distance, 220));
|
|
2937
|
+
const radians = angleDeg * Math.PI / 180;
|
|
2938
|
+
const centerX = anchor.x + anchor.width / 2 + Math.cos(radians) * distance;
|
|
2939
|
+
const centerY = anchor.y + anchor.height / 2 + Math.sin(radians) * distance;
|
|
2940
|
+
return {
|
|
2941
|
+
x: formatNumber(centerX - spec.width / 2),
|
|
2942
|
+
y: formatNumber(centerY - spec.height / 2)
|
|
2943
|
+
};
|
|
2944
|
+
}
|
|
2945
|
+
addNode(partialNode = {}) {
|
|
2946
|
+
const type = normalizeNodeType(partialNode.type);
|
|
2947
|
+
const spec = DEFAULT_NODE_SPECS[type];
|
|
2948
|
+
const relative = partialNode.relativeTo != null ? this.resolveRelativePosition(partialNode.relativeTo, spec) : null;
|
|
2949
|
+
const center = this.getViewportCenter();
|
|
2950
|
+
const offset = this.paletteSerial % 6 * 26;
|
|
2951
|
+
this.paletteSerial += 1;
|
|
2952
|
+
const fallbackX = relative ? relative.x : formatNumber(center.x - spec.width / 2 + offset);
|
|
2953
|
+
const fallbackY = relative ? relative.y : formatNumber(center.y - spec.height / 2 + offset);
|
|
2954
|
+
const rest = { ...partialNode };
|
|
2955
|
+
delete rest.relativeTo;
|
|
2956
|
+
const usedIds = new Set(this.documentData.nodes.map((node2) => node2.id));
|
|
2957
|
+
const node = normalizeNode(
|
|
2958
|
+
{
|
|
2959
|
+
...rest,
|
|
2960
|
+
type,
|
|
2961
|
+
x: partialNode.x == null ? fallbackX : partialNode.x,
|
|
2962
|
+
y: partialNode.y == null ? fallbackY : partialNode.y
|
|
2963
|
+
},
|
|
2964
|
+
this.documentData.nodes.length,
|
|
2965
|
+
usedIds
|
|
2966
|
+
);
|
|
2967
|
+
this.documentData.nodes.push(node);
|
|
2968
|
+
this.select({ kind: "node", id: node.id });
|
|
2969
|
+
this.syncJsonTextarea();
|
|
2970
|
+
this.emitChange("node:add", { node: deepClone(node) });
|
|
2971
|
+
return deepClone(node);
|
|
2972
|
+
}
|
|
2973
|
+
// Convenience: place a child node relative to a parent and connect them with
|
|
2974
|
+
// an auto-ported arrow in one call. Returns { node, edge } (edge may be null
|
|
2975
|
+
// if the connection is rejected). The new child is left selected.
|
|
2976
|
+
addChildNode(parentId, options = {}) {
|
|
2977
|
+
if (this.readonly) return null;
|
|
2978
|
+
const parent = this.findNode(sanitizeId(parentId));
|
|
2979
|
+
if (!parent) return null;
|
|
2980
|
+
const { direction = "right", distance, angle, edge: edgeOptions, ...nodeOptions } = options;
|
|
2981
|
+
const node = this.addNode({
|
|
2982
|
+
...nodeOptions,
|
|
2983
|
+
relativeTo: { node: parent.id, direction, distance, angle }
|
|
2984
|
+
});
|
|
2985
|
+
const edge = this.addEdge({
|
|
2986
|
+
from: parent.id,
|
|
2987
|
+
to: node.id,
|
|
2988
|
+
autoPort: true,
|
|
2989
|
+
endMarker: "arrow",
|
|
2990
|
+
...isPlainObject(edgeOptions) ? edgeOptions : {}
|
|
2991
|
+
});
|
|
2992
|
+
this.select({ kind: "node", id: node.id });
|
|
2993
|
+
return { node, edge };
|
|
2994
|
+
}
|
|
2995
|
+
// Arrange nodes with a built-in layout. Positions are computed by the pure
|
|
2996
|
+
// computeLayout() module, then applied in place (NOT via load(), which would
|
|
2997
|
+
// wipe selection/viewport) so the result is one undoable, change-emitting step.
|
|
2998
|
+
layout(mode = "tree", options = {}) {
|
|
2999
|
+
if (this.readonly) return this;
|
|
3000
|
+
const resolvedMode = LAYOUT_MODES.includes(mode) ? mode : "tree";
|
|
3001
|
+
const positions = computeLayout(this.documentData, resolvedMode, options);
|
|
3002
|
+
if (!positions.size) return this;
|
|
3003
|
+
let changed = false;
|
|
3004
|
+
this.documentData.nodes.forEach((node) => {
|
|
3005
|
+
const next = positions.get(node.id);
|
|
3006
|
+
if (!next) return;
|
|
3007
|
+
const x = formatNumber(next.x);
|
|
3008
|
+
const y = formatNumber(next.y);
|
|
3009
|
+
if (x !== node.x || y !== node.y) {
|
|
3010
|
+
node.x = x;
|
|
3011
|
+
node.y = y;
|
|
3012
|
+
changed = true;
|
|
3013
|
+
}
|
|
3014
|
+
});
|
|
3015
|
+
const rerouted = options.reroutePorts === false ? false : this.rerouteEdgePorts();
|
|
3016
|
+
if (!changed && !rerouted) return this;
|
|
3017
|
+
this.render();
|
|
3018
|
+
this.emitChange("layout", { mode: resolvedMode });
|
|
3019
|
+
if (options.fit) this.fitView();
|
|
3020
|
+
return this;
|
|
3021
|
+
}
|
|
3022
|
+
autoArrange(options = {}) {
|
|
3023
|
+
return this.layout("grid", options);
|
|
3024
|
+
}
|
|
3025
|
+
// Re-pick each edge's from/to port from the nodes' current centers, so a
|
|
3026
|
+
// freshly laid-out graph attaches connectors on sensible sides. Returns
|
|
3027
|
+
// whether any port actually changed.
|
|
3028
|
+
rerouteEdgePorts() {
|
|
3029
|
+
let changed = false;
|
|
3030
|
+
this.documentData.edges.forEach((edge) => {
|
|
3031
|
+
const fromNode = this.findNode(edge.from.nodeId);
|
|
3032
|
+
const toNode = this.findNode(edge.to.nodeId);
|
|
3033
|
+
if (!fromNode || !toNode) return;
|
|
3034
|
+
const fromCenter = {
|
|
3035
|
+
x: fromNode.x + fromNode.width / 2,
|
|
3036
|
+
y: fromNode.y + fromNode.height / 2
|
|
3037
|
+
};
|
|
3038
|
+
const toCenter = { x: toNode.x + toNode.width / 2, y: toNode.y + toNode.height / 2 };
|
|
3039
|
+
const nextFrom = getPortByDirection(fromNode, toCenter).port;
|
|
3040
|
+
const nextTo = getPortByDirection(toNode, fromCenter).port;
|
|
3041
|
+
if (nextFrom !== edge.from.port || nextTo !== edge.to.port) {
|
|
3042
|
+
edge.from.port = nextFrom;
|
|
3043
|
+
edge.to.port = nextTo;
|
|
3044
|
+
changed = true;
|
|
3045
|
+
}
|
|
3046
|
+
});
|
|
3047
|
+
return changed;
|
|
3048
|
+
}
|
|
3049
|
+
applyNodePatch(node, patch = {}) {
|
|
3050
|
+
const nextType = normalizeNodeType(patch.type ?? node.type);
|
|
3051
|
+
node.type = nextType;
|
|
3052
|
+
node.x = patch.x == null ? node.x : formatNumber(toFiniteNumber(patch.x, node.x));
|
|
3053
|
+
node.y = patch.y == null ? node.y : formatNumber(toFiniteNumber(patch.y, node.y));
|
|
3054
|
+
node.text = isNodeTextEditable(nextType) ? patch.text == null ? node.text : String(patch.text) : getNodeSpec(nextType).text;
|
|
3055
|
+
node.width = clampNodeWidth(nextType, patch.width, node.width);
|
|
3056
|
+
node.height = clampNodeHeight(nextType, patch.height, node.height);
|
|
3057
|
+
if (isPlainObject(patch.data)) {
|
|
3058
|
+
node.data = deepClone(patch.data);
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
updateNode(nodeId, patch = {}, options = {}) {
|
|
3062
|
+
const node = this.findNode(nodeId);
|
|
3063
|
+
if (!node) return null;
|
|
3064
|
+
const previousType = node.type;
|
|
3065
|
+
const previousSpec = DEFAULT_NODE_SPECS[previousType];
|
|
3066
|
+
this.applyNodePatch(node, patch);
|
|
3067
|
+
if (patch.type && previousType !== node.type) {
|
|
3068
|
+
const nextSpec = DEFAULT_NODE_SPECS[node.type];
|
|
3069
|
+
if (patch.width == null && node.width === previousSpec.width) {
|
|
3070
|
+
node.width = nextSpec.width;
|
|
3071
|
+
}
|
|
3072
|
+
if (patch.height == null && node.height === previousSpec.height) {
|
|
3073
|
+
node.height = nextSpec.height;
|
|
3074
|
+
}
|
|
3075
|
+
if (node.text === DEFAULT_NODE_SPECS[previousType].text) {
|
|
3076
|
+
node.text = nextSpec.text;
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
this.render({
|
|
3080
|
+
scene: true,
|
|
3081
|
+
inspector: options.inspector !== false,
|
|
3082
|
+
json: true
|
|
3083
|
+
});
|
|
3084
|
+
this.emitChange(options.reason || "node:update", { node: deepClone(node) });
|
|
3085
|
+
return deepClone(node);
|
|
3086
|
+
}
|
|
3087
|
+
// Walk outgoing edges (from.nodeId -> to.nodeId) breadth-first to collect a
|
|
3088
|
+
// node and every descendant reachable through the directed graph. The visited
|
|
3089
|
+
// Set doubles as a cycle guard, so a diamond or loop is safe (each node once).
|
|
3090
|
+
collectDescendants(rootId) {
|
|
3091
|
+
const visited = /* @__PURE__ */ new Set([rootId]);
|
|
3092
|
+
const queue = [rootId];
|
|
3093
|
+
while (queue.length) {
|
|
3094
|
+
const current = queue.shift();
|
|
3095
|
+
this.documentData.edges.forEach((edge) => {
|
|
3096
|
+
if (edge.from.nodeId === current && !visited.has(edge.to.nodeId)) {
|
|
3097
|
+
visited.add(edge.to.nodeId);
|
|
3098
|
+
queue.push(edge.to.nodeId);
|
|
3099
|
+
}
|
|
3100
|
+
});
|
|
3101
|
+
}
|
|
3102
|
+
return visited;
|
|
3103
|
+
}
|
|
3104
|
+
removeNode(nodeId, options = {}) {
|
|
3105
|
+
const id = sanitizeId(nodeId);
|
|
3106
|
+
if (!this.findNode(id)) return false;
|
|
3107
|
+
const ids = options.cascade ? this.collectDescendants(id) : /* @__PURE__ */ new Set([id]);
|
|
3108
|
+
return this.removeNodeIds(ids, options.reason || "node:remove", id);
|
|
3109
|
+
}
|
|
3110
|
+
removeNodeIds(ids, reason = "node:remove", primaryId = null) {
|
|
3111
|
+
const idSet = ids instanceof Set ? ids : new Set(ids);
|
|
3112
|
+
const removed = this.documentData.nodes.filter((node) => idSet.has(node.id)).map((node) => node.id);
|
|
3113
|
+
if (!removed.length) return false;
|
|
3114
|
+
if (this.textEditor && idSet.has(this.textEditor.nodeId)) {
|
|
3115
|
+
this.stopTextEdit({ commit: false });
|
|
3116
|
+
}
|
|
3117
|
+
this.documentData.nodes = this.documentData.nodes.filter((node) => !idSet.has(node.id));
|
|
3118
|
+
this.documentData.edges = this.documentData.edges.filter(
|
|
3119
|
+
(edge) => !idSet.has(edge.from.nodeId) && !idSet.has(edge.to.nodeId)
|
|
3120
|
+
);
|
|
3121
|
+
if (this.selection?.kind === "node" && idSet.has(this.selection.id)) {
|
|
3122
|
+
this.selection = null;
|
|
3123
|
+
}
|
|
3124
|
+
this.render();
|
|
3125
|
+
this.emitChange(reason, { nodeId: primaryId ?? removed[0], nodeIds: removed });
|
|
3126
|
+
return true;
|
|
3127
|
+
}
|
|
3128
|
+
updateEdge(edgeId, patch = {}, options = {}) {
|
|
3129
|
+
const edge = this.findEdge(edgeId);
|
|
3130
|
+
if (!edge) return null;
|
|
3131
|
+
const nodeIds = new Set(this.documentData.nodes.map((node) => node.id));
|
|
3132
|
+
const nextFrom = patch.from ? normalizeEndpoint(patch.from, edge.from.port) : edge.from;
|
|
3133
|
+
const nextTo = patch.to ? normalizeEndpoint(patch.to, edge.to.port) : edge.to;
|
|
3134
|
+
if (patch.from != null || patch.to != null) {
|
|
3135
|
+
if (!nodeIds.has(nextFrom.nodeId) || !nodeIds.has(nextTo.nodeId)) return null;
|
|
3136
|
+
if (!FLOWCHART_PORTS.includes(nextFrom.port) || !FLOWCHART_PORTS.includes(nextTo.port))
|
|
3137
|
+
return null;
|
|
3138
|
+
if (nextFrom.nodeId === nextTo.nodeId && nextFrom.port === nextTo.port) return null;
|
|
3139
|
+
edge.from = nextFrom;
|
|
3140
|
+
edge.to = nextTo;
|
|
3141
|
+
}
|
|
3142
|
+
if (patch.kind != null) {
|
|
3143
|
+
edge.kind = patch.kind === "line" ? "line" : "arrow";
|
|
3144
|
+
if (patch.startMarker == null && patch.endMarker == null) {
|
|
3145
|
+
if (edge.kind === "line") {
|
|
3146
|
+
edge.startMarker = "none";
|
|
3147
|
+
edge.endMarker = "none";
|
|
3148
|
+
} else if (edge.endMarker === "none" && edge.startMarker === "none") {
|
|
3149
|
+
edge.endMarker = "arrow";
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
if (patch.startMarker != null) {
|
|
3154
|
+
edge.startMarker = normalizeEdgeMarker(patch.startMarker) || "none";
|
|
3155
|
+
}
|
|
3156
|
+
if (patch.endMarker != null) {
|
|
3157
|
+
edge.endMarker = normalizeEdgeMarker(patch.endMarker) || "none";
|
|
3158
|
+
}
|
|
3159
|
+
if (patch.route != null) {
|
|
3160
|
+
edge.route = normalizeEdgeRoute(patch.route);
|
|
3161
|
+
}
|
|
3162
|
+
if (patch.strokeWidth != null) {
|
|
3163
|
+
edge.strokeWidth = normalizeEdgeStrokeWidth(patch.strokeWidth);
|
|
3164
|
+
}
|
|
3165
|
+
if (patch.label != null) {
|
|
3166
|
+
edge.label = String(patch.label);
|
|
3167
|
+
}
|
|
3168
|
+
if (isPlainObject(patch.data)) {
|
|
3169
|
+
edge.data = deepClone(patch.data);
|
|
3170
|
+
}
|
|
3171
|
+
syncEdgeKind(edge);
|
|
3172
|
+
this.render({
|
|
3173
|
+
scene: true,
|
|
3174
|
+
inspector: options.inspector !== false,
|
|
3175
|
+
json: true
|
|
3176
|
+
});
|
|
3177
|
+
this.emitChange(options.reason || "edge:update", { edge: deepClone(edge) });
|
|
3178
|
+
return deepClone(edge);
|
|
3179
|
+
}
|
|
3180
|
+
// Accept `from`/`to` as a bare node id or `{ nodeId, port }`. When `autoPort`
|
|
3181
|
+
// is set, fill any omitted port by aiming each endpoint at the other node's
|
|
3182
|
+
// center via getPortByDirection — the same geometry the live connect tool
|
|
3183
|
+
// uses, so a programmatic edge picks the same side a dragged one would.
|
|
3184
|
+
resolveEdgeEndpoints(partialEdge) {
|
|
3185
|
+
const toEndpoint = (value) => {
|
|
3186
|
+
if (typeof value === "string") return { nodeId: value };
|
|
3187
|
+
if (isPlainObject(value)) return { ...value };
|
|
3188
|
+
return {};
|
|
3189
|
+
};
|
|
3190
|
+
const from = toEndpoint(partialEdge.from);
|
|
3191
|
+
const to = toEndpoint(partialEdge.to);
|
|
3192
|
+
if (partialEdge.autoPort) {
|
|
3193
|
+
const fromNode = this.findNode(sanitizeId(from.nodeId));
|
|
3194
|
+
const toNode = this.findNode(sanitizeId(to.nodeId));
|
|
3195
|
+
if (fromNode && toNode) {
|
|
3196
|
+
const fromCenter = {
|
|
3197
|
+
x: fromNode.x + fromNode.width / 2,
|
|
3198
|
+
y: fromNode.y + fromNode.height / 2
|
|
3199
|
+
};
|
|
3200
|
+
const toCenter = { x: toNode.x + toNode.width / 2, y: toNode.y + toNode.height / 2 };
|
|
3201
|
+
if (!FLOWCHART_PORTS.includes(from.port))
|
|
3202
|
+
from.port = getPortByDirection(fromNode, toCenter).port;
|
|
3203
|
+
if (!FLOWCHART_PORTS.includes(to.port))
|
|
3204
|
+
to.port = getPortByDirection(toNode, fromCenter).port;
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
const rest = { ...partialEdge };
|
|
3208
|
+
delete rest.autoPort;
|
|
3209
|
+
return { ...rest, from, to };
|
|
3210
|
+
}
|
|
3211
|
+
addEdge(partialEdge = {}) {
|
|
3212
|
+
if (this.readonly) return null;
|
|
3213
|
+
const input = this.resolveEdgeEndpoints(partialEdge);
|
|
3214
|
+
const nodeIds = new Set(this.documentData.nodes.map((node) => node.id));
|
|
3215
|
+
const usedIds = new Set(this.documentData.edges.map((edge2) => edge2.id));
|
|
3216
|
+
const edge = normalizeEdge(input, this.documentData.edges.length, nodeIds, usedIds);
|
|
3217
|
+
if (!edge) return null;
|
|
3218
|
+
if (edge.from.nodeId === edge.to.nodeId && edge.from.port === edge.to.port) return null;
|
|
3219
|
+
this.documentData.edges.push(edge);
|
|
3220
|
+
this.select({ kind: "edge", id: edge.id });
|
|
3221
|
+
this.syncJsonTextarea();
|
|
3222
|
+
this.emit("connect", { edge: deepClone(edge) });
|
|
3223
|
+
this.emitChange("edge:add", { edge: deepClone(edge) });
|
|
3224
|
+
return deepClone(edge);
|
|
3225
|
+
}
|
|
3226
|
+
removeEdge(edgeId) {
|
|
3227
|
+
const edgeIndex = this.documentData.edges.findIndex((edge) => edge.id === edgeId);
|
|
3228
|
+
if (edgeIndex === -1) return false;
|
|
3229
|
+
this.documentData.edges.splice(edgeIndex, 1);
|
|
3230
|
+
if (this.selection?.kind === "edge" && this.selection.id === edgeId) {
|
|
3231
|
+
this.selection = null;
|
|
3232
|
+
}
|
|
3233
|
+
this.render();
|
|
3234
|
+
this.emitChange("edge:remove", { edgeId });
|
|
3235
|
+
return true;
|
|
3236
|
+
}
|
|
3237
|
+
deleteSelection() {
|
|
3238
|
+
if (!this.selection || this.readonly) return false;
|
|
3239
|
+
if (this.selection.kind === "node") return this.removeNode(this.selection.id);
|
|
3240
|
+
return this.removeEdge(this.selection.id);
|
|
3241
|
+
}
|
|
3242
|
+
setViewport(viewport) {
|
|
3243
|
+
this.documentData.viewport = normalizeViewport(viewport);
|
|
3244
|
+
this.render({ inspector: false, json: true });
|
|
3245
|
+
this.emitViewportChange("viewport:set");
|
|
3246
|
+
return this;
|
|
3247
|
+
}
|
|
3248
|
+
zoomIn() {
|
|
3249
|
+
const width = this.canvasEl.clientWidth || 800;
|
|
3250
|
+
const height = this.canvasEl.clientHeight || 560;
|
|
3251
|
+
this.scaleAround(1.12, width / 2, height / 2, "viewport:zoom");
|
|
3252
|
+
return this;
|
|
3253
|
+
}
|
|
3254
|
+
zoomOut() {
|
|
3255
|
+
const width = this.canvasEl.clientWidth || 800;
|
|
3256
|
+
const height = this.canvasEl.clientHeight || 560;
|
|
3257
|
+
this.scaleAround(1 / 1.12, width / 2, height / 2, "viewport:zoom");
|
|
3258
|
+
return this;
|
|
3259
|
+
}
|
|
3260
|
+
resetView() {
|
|
3261
|
+
this.documentData.viewport = normalizeViewport({ x: 0, y: 0, scale: 1 });
|
|
3262
|
+
this.render({ inspector: false, json: true });
|
|
3263
|
+
this.emitViewportChange("viewport:reset");
|
|
3264
|
+
return this;
|
|
3265
|
+
}
|
|
3266
|
+
fitView() {
|
|
3267
|
+
if (!this.documentData.nodes.length) {
|
|
3268
|
+
return this.resetView();
|
|
3269
|
+
}
|
|
3270
|
+
const bounds = getBounds(this.documentData.nodes);
|
|
3271
|
+
const width = this.canvasEl.clientWidth || 800;
|
|
3272
|
+
const height = this.canvasEl.clientHeight || 560;
|
|
3273
|
+
const padding = 80;
|
|
3274
|
+
const contentWidth = Math.max(1, bounds.right - bounds.left);
|
|
3275
|
+
const contentHeight = Math.max(1, bounds.bottom - bounds.top);
|
|
3276
|
+
const scale = clamp(
|
|
3277
|
+
Math.min((width - padding * 2) / contentWidth, (height - padding * 2) / contentHeight),
|
|
3278
|
+
MIN_SCALE,
|
|
3279
|
+
MAX_SCALE
|
|
3280
|
+
);
|
|
3281
|
+
this.documentData.viewport = {
|
|
3282
|
+
x: formatNumber(width / 2 - (bounds.left + bounds.right) / 2 * scale),
|
|
3283
|
+
y: formatNumber(height / 2 - (bounds.top + bounds.bottom) / 2 * scale),
|
|
3284
|
+
scale: formatNumber(scale)
|
|
3285
|
+
};
|
|
3286
|
+
this.render({ inspector: false, json: true });
|
|
3287
|
+
this.emitViewportChange("viewport:fit");
|
|
3288
|
+
return this;
|
|
3289
|
+
}
|
|
3290
|
+
clear() {
|
|
3291
|
+
this.stopTextEdit({ commit: false });
|
|
3292
|
+
this.activeTool = null;
|
|
3293
|
+
this.reconnectEdgeId = null;
|
|
3294
|
+
this.clipboard = null;
|
|
3295
|
+
this.documentData = normalizeDocument({
|
|
3296
|
+
nodes: [],
|
|
3297
|
+
edges: [],
|
|
3298
|
+
viewport: { x: 0, y: 0, scale: 1 }
|
|
3299
|
+
});
|
|
3300
|
+
this.selection = null;
|
|
3301
|
+
this.render();
|
|
3302
|
+
this.emitChange("clear");
|
|
3303
|
+
return this;
|
|
3304
|
+
}
|
|
3305
|
+
load(data, options = {}) {
|
|
3306
|
+
this.stopTextEdit({ commit: false });
|
|
3307
|
+
this.activeTool = null;
|
|
3308
|
+
this.reconnectEdgeId = null;
|
|
3309
|
+
const previousSelection = this.selection;
|
|
3310
|
+
this.documentData = normalizeDocument(data);
|
|
3311
|
+
this.selection = this.resolvePreservedSelection(previousSelection, options.preserveSelection);
|
|
3312
|
+
this.render();
|
|
3313
|
+
this.emitChange("load");
|
|
3314
|
+
return this;
|
|
3315
|
+
}
|
|
3316
|
+
toJSON() {
|
|
3317
|
+
return deepClone({
|
|
3318
|
+
version: VD_FLOWCHART_VERSION,
|
|
3319
|
+
viewport: this.documentData.viewport,
|
|
3320
|
+
nodes: this.documentData.nodes,
|
|
3321
|
+
edges: this.documentData.edges
|
|
3322
|
+
});
|
|
3323
|
+
}
|
|
3324
|
+
destroy() {
|
|
3325
|
+
if (this.destroyed) return;
|
|
3326
|
+
this.destroyed = true;
|
|
3327
|
+
this.activeTool = null;
|
|
3328
|
+
this.stopTextEdit({ commit: false });
|
|
3329
|
+
this.resizeObserver?.disconnect();
|
|
3330
|
+
this.resizeObserver = null;
|
|
3331
|
+
this.unbindEvents();
|
|
3332
|
+
this.element.innerHTML = "";
|
|
3333
|
+
this.element.classList.remove("vd-flowchart-host");
|
|
3334
|
+
}
|
|
3335
|
+
};
|
|
3336
|
+
|
|
3337
|
+
// src/flowchart/vue.js
|
|
3338
|
+
var FORWARDED_EVENTS = ["change", "select", "viewport", "connect", "ready"];
|
|
3339
|
+
var VdFlowchart2 = defineComponent({
|
|
3340
|
+
name: "VdFlowchart",
|
|
3341
|
+
props: {
|
|
3342
|
+
/** Flowchart document — `{ nodes, edges }`. */
|
|
3343
|
+
data: { type: Object, default: () => ({}) },
|
|
3344
|
+
/** Render as a non-editable viewer. */
|
|
3345
|
+
readonly: { type: Boolean, default: false },
|
|
3346
|
+
/** Background grid size in px. */
|
|
3347
|
+
gridSize: { type: Number, default: void 0 },
|
|
3348
|
+
/** Keep the current selection across `data`-driven reloads when possible. */
|
|
3349
|
+
preserveSelection: { type: Boolean, default: false },
|
|
3350
|
+
/** Fit the view to content once the editor reports a measurable size. */
|
|
3351
|
+
autoFit: { type: Boolean, default: false },
|
|
3352
|
+
/** Enable the built-in undo/redo history (default true). */
|
|
3353
|
+
history: { type: Boolean, default: true },
|
|
3354
|
+
/** Maximum number of history entries to retain. */
|
|
3355
|
+
historyLimit: { type: Number, default: void 0 }
|
|
3356
|
+
},
|
|
3357
|
+
emits: ["change", "select", "viewport", "connect", "ready"],
|
|
3358
|
+
setup(props, { emit, expose }) {
|
|
3359
|
+
const el = ref(null);
|
|
3360
|
+
let instance = null;
|
|
3361
|
+
const create = () => {
|
|
3362
|
+
instance = new VdFlowchart({
|
|
3363
|
+
element: el.value,
|
|
3364
|
+
data: props.data,
|
|
3365
|
+
readonly: props.readonly,
|
|
3366
|
+
gridSize: props.gridSize,
|
|
3367
|
+
autoFit: props.autoFit,
|
|
3368
|
+
history: props.history,
|
|
3369
|
+
historyLimit: props.historyLimit
|
|
3370
|
+
});
|
|
3371
|
+
FORWARDED_EVENTS.forEach((name) => {
|
|
3372
|
+
instance.on(name, (payload) => emit(name, payload));
|
|
3373
|
+
});
|
|
3374
|
+
};
|
|
3375
|
+
onMounted(() => {
|
|
3376
|
+
if (typeof window === "undefined" || !el.value) return;
|
|
3377
|
+
create();
|
|
3378
|
+
});
|
|
3379
|
+
watch(
|
|
3380
|
+
() => props.data,
|
|
3381
|
+
(next) => {
|
|
3382
|
+
if (instance && typeof instance.load === "function") {
|
|
3383
|
+
instance.load(next, { preserveSelection: props.preserveSelection });
|
|
3384
|
+
}
|
|
3385
|
+
},
|
|
3386
|
+
{ deep: true }
|
|
3387
|
+
);
|
|
3388
|
+
watch(
|
|
3389
|
+
() => [props.readonly, props.gridSize, props.autoFit, props.history, props.historyLimit],
|
|
3390
|
+
() => {
|
|
3391
|
+
if (!instance) return;
|
|
3392
|
+
instance.destroy();
|
|
3393
|
+
create();
|
|
3394
|
+
}
|
|
3395
|
+
);
|
|
3396
|
+
onBeforeUnmount(() => {
|
|
3397
|
+
if (instance) {
|
|
3398
|
+
instance.destroy();
|
|
3399
|
+
instance = null;
|
|
3400
|
+
}
|
|
3401
|
+
});
|
|
3402
|
+
expose({
|
|
3403
|
+
getInstance: () => instance,
|
|
3404
|
+
undo: () => instance?.undo(),
|
|
3405
|
+
redo: () => instance?.redo(),
|
|
3406
|
+
canUndo: () => Boolean(instance?.canUndo()),
|
|
3407
|
+
canRedo: () => Boolean(instance?.canRedo()),
|
|
3408
|
+
layout: (mode, options) => instance?.layout(mode, options)
|
|
3409
|
+
});
|
|
3410
|
+
return () => h("div", { ref: el, class: "vd-flowchart" });
|
|
3411
|
+
}
|
|
3412
|
+
});
|
|
3413
|
+
export {
|
|
3414
|
+
FLOWCHART_EDGE_MARKERS,
|
|
3415
|
+
FLOWCHART_EDGE_ROUTES,
|
|
3416
|
+
FLOWCHART_NODE_TYPES,
|
|
3417
|
+
FLOWCHART_PORTS,
|
|
3418
|
+
LAYOUT_MODES,
|
|
3419
|
+
VD_FLOWCHART_VERSION,
|
|
3420
|
+
VdFlowchart2 as VdFlowchart,
|
|
3421
|
+
VdFlowchart as VdFlowchartCore,
|
|
3422
|
+
computeLayout
|
|
3423
|
+
};
|
|
3424
|
+
//# sourceMappingURL=index.js.map
|