@hattoriai/poney-layout 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +10 -2
- package/dist/index.d.ts +533 -0
- package/dist/index.js +1267 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1267 @@
|
|
|
1
|
+
import { indexOf, projectArchitecture, refKey, projectDataFlow, projectErd, isDefined } from '@hattoriai/poney-core';
|
|
2
|
+
|
|
3
|
+
// src/geometry.ts
|
|
4
|
+
function roundedPath(points, radius = 8) {
|
|
5
|
+
if (points.length < 2) return "";
|
|
6
|
+
let d = `M ${points[0].x} ${points[0].y}`;
|
|
7
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
8
|
+
const p0 = points[i - 1];
|
|
9
|
+
const p1 = points[i];
|
|
10
|
+
const p2 = points[i + 1];
|
|
11
|
+
const d1 = Math.hypot(p1.x - p0.x, p1.y - p0.y);
|
|
12
|
+
const d2 = Math.hypot(p2.x - p1.x, p2.y - p1.y);
|
|
13
|
+
const r = Math.min(radius, d1 / 2, d2 / 2);
|
|
14
|
+
if (r < 0.5) {
|
|
15
|
+
d += ` L ${p1.x} ${p1.y}`;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const a = { x: p1.x - (p1.x - p0.x) / d1 * r, y: p1.y - (p1.y - p0.y) / d1 * r };
|
|
19
|
+
const b = { x: p1.x + (p2.x - p1.x) / d2 * r, y: p1.y + (p2.y - p1.y) / d2 * r };
|
|
20
|
+
d += ` L ${a.x} ${a.y} Q ${p1.x} ${p1.y} ${b.x} ${b.y}`;
|
|
21
|
+
}
|
|
22
|
+
const last = points[points.length - 1];
|
|
23
|
+
return `${d} L ${last.x} ${last.y}`;
|
|
24
|
+
}
|
|
25
|
+
function trimEnd(points, by) {
|
|
26
|
+
if (points.length < 2 || by <= 0) return points;
|
|
27
|
+
const out = points.slice();
|
|
28
|
+
const a = out[out.length - 2];
|
|
29
|
+
const b = out[out.length - 1];
|
|
30
|
+
const len = Math.hypot(b.x - a.x, b.y - a.y);
|
|
31
|
+
if (len <= by) return out;
|
|
32
|
+
out[out.length - 1] = { x: b.x - (b.x - a.x) / len * by, y: b.y - (b.y - a.y) / len * by };
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
function trimStart(points, by) {
|
|
36
|
+
return trimEnd(points.slice().reverse(), by).reverse();
|
|
37
|
+
}
|
|
38
|
+
function endAngle(points) {
|
|
39
|
+
const a = points[points.length - 2];
|
|
40
|
+
const b = points[points.length - 1];
|
|
41
|
+
return Math.atan2(b.y - a.y, b.x - a.x);
|
|
42
|
+
}
|
|
43
|
+
function startAngle(points) {
|
|
44
|
+
const a = points[1];
|
|
45
|
+
const b = points[0];
|
|
46
|
+
return Math.atan2(b.y - a.y, b.x - a.x);
|
|
47
|
+
}
|
|
48
|
+
function labelPoint(points) {
|
|
49
|
+
let best = 0;
|
|
50
|
+
let bestLen = -1;
|
|
51
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
52
|
+
const len = Math.hypot(points[i + 1].x - points[i].x, points[i + 1].y - points[i].y);
|
|
53
|
+
if (len > bestLen) {
|
|
54
|
+
bestLen = len;
|
|
55
|
+
best = i;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const a = points[best];
|
|
59
|
+
const b = points[best + 1] ?? a;
|
|
60
|
+
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, horizontal: Math.abs(b.x - a.x) >= Math.abs(b.y - a.y) };
|
|
61
|
+
}
|
|
62
|
+
function pointAlong(points, t) {
|
|
63
|
+
const lengths = points.slice(1).map((p, i) => Math.hypot(p.x - points[i].x, p.y - points[i].y));
|
|
64
|
+
let target = lengths.reduce((a, b) => a + b, 0) * t;
|
|
65
|
+
for (let i = 0; i < lengths.length; i++) {
|
|
66
|
+
if (target <= lengths[i]) {
|
|
67
|
+
const f = lengths[i] === 0 ? 0 : target / lengths[i];
|
|
68
|
+
return { x: points[i].x + (points[i + 1].x - points[i].x) * f, y: points[i].y + (points[i + 1].y - points[i].y) * f };
|
|
69
|
+
}
|
|
70
|
+
target -= lengths[i];
|
|
71
|
+
}
|
|
72
|
+
return points[points.length - 1];
|
|
73
|
+
}
|
|
74
|
+
function union(rects) {
|
|
75
|
+
if (rects.length === 0) return { x: 0, y: 0, w: 0, h: 0 };
|
|
76
|
+
const x = Math.min(...rects.map((r) => r.x));
|
|
77
|
+
const y = Math.min(...rects.map((r) => r.y));
|
|
78
|
+
const x2 = Math.max(...rects.map((r) => r.x + r.w));
|
|
79
|
+
const y2 = Math.max(...rects.map((r) => r.y + r.h));
|
|
80
|
+
return { x, y, w: x2 - x, h: y2 - y };
|
|
81
|
+
}
|
|
82
|
+
var center = (r) => ({ x: r.x + r.w / 2, y: r.y + r.h / 2 });
|
|
83
|
+
|
|
84
|
+
// src/text.ts
|
|
85
|
+
var estimateText = (text, size, weight, family) => text.length * size * (family === "mono" ? 0.6 : weight >= 600 ? 0.56 : 0.52);
|
|
86
|
+
var fonts = {
|
|
87
|
+
sans: '"Hanken Grotesk Variable", ui-sans-serif, system-ui, sans-serif',
|
|
88
|
+
mono: '"JetBrains Mono Variable", ui-monospace, monospace'
|
|
89
|
+
};
|
|
90
|
+
function canvasMeasurer() {
|
|
91
|
+
if (typeof document === "undefined") return void 0;
|
|
92
|
+
const ctx = document.createElement("canvas").getContext("2d");
|
|
93
|
+
if (!ctx) return void 0;
|
|
94
|
+
return (text, size, weight, family) => {
|
|
95
|
+
ctx.font = `${weight} ${size}px ${fonts[family]}`;
|
|
96
|
+
return ctx.measureText(text).width;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
var measurer;
|
|
100
|
+
function setTextMeasurer(fn) {
|
|
101
|
+
measurer = fn;
|
|
102
|
+
}
|
|
103
|
+
function setFonts(next) {
|
|
104
|
+
fonts = { ...fonts, ...next };
|
|
105
|
+
measurer = void 0;
|
|
106
|
+
}
|
|
107
|
+
function measure(text, size = 13, weight = 400, family = "sans") {
|
|
108
|
+
measurer ??= canvasMeasurer() ?? estimateText;
|
|
109
|
+
return measurer(text, size, weight, family);
|
|
110
|
+
}
|
|
111
|
+
function wrap(text, width, size = 13, weight = 400, maxLines = 2) {
|
|
112
|
+
const words = text.split(/\s+/).filter(Boolean);
|
|
113
|
+
const lines = [];
|
|
114
|
+
let line = "";
|
|
115
|
+
for (const word of words) {
|
|
116
|
+
const candidate = line ? `${line} ${word}` : word;
|
|
117
|
+
if (measure(candidate, size, weight) <= width || !line) line = candidate;
|
|
118
|
+
else {
|
|
119
|
+
lines.push(line);
|
|
120
|
+
line = word;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (line) lines.push(line);
|
|
124
|
+
if (lines.length <= maxLines) return lines;
|
|
125
|
+
const kept = lines.slice(0, maxLines);
|
|
126
|
+
let last = kept[maxLines - 1];
|
|
127
|
+
while (last.length > 1 && measure(`${last}\u2026`, size, weight) > width) last = last.slice(0, -1).trimEnd();
|
|
128
|
+
kept[maxLines - 1] = `${last}\u2026`;
|
|
129
|
+
return kept;
|
|
130
|
+
}
|
|
131
|
+
function clip(text, width, size = 13, weight = 400, family = "sans") {
|
|
132
|
+
if (measure(text, size, weight, family) <= width) return text;
|
|
133
|
+
let t = text;
|
|
134
|
+
while (t.length > 1 && measure(`${t}\u2026`, size, weight, family) > width) t = t.slice(0, -1);
|
|
135
|
+
return `${t.trimEnd()}\u2026`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/elk.ts
|
|
139
|
+
var engine;
|
|
140
|
+
var elk = () => engine ??= import('elkjs/lib/elk.bundled.js').then((m) => new m.default());
|
|
141
|
+
var BASE_OPTIONS = {
|
|
142
|
+
"elk.algorithm": "layered",
|
|
143
|
+
"elk.edgeRouting": "ORTHOGONAL",
|
|
144
|
+
"elk.layered.nodePlacement.strategy": "NETWORK_SIMPLEX",
|
|
145
|
+
"elk.layered.crossingMinimization.strategy": "LAYER_SWEEP",
|
|
146
|
+
"elk.layered.considerModelOrder.strategy": "NODES_AND_EDGES"
|
|
147
|
+
};
|
|
148
|
+
async function runElk(graph) {
|
|
149
|
+
const out = await (await elk()).layout(graph);
|
|
150
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
151
|
+
const origin = /* @__PURE__ */ new Map([[out.id, { x: 0, y: 0 }]]);
|
|
152
|
+
const visit = (n, ox, oy, parent) => {
|
|
153
|
+
const x = ox + (n.x ?? 0);
|
|
154
|
+
const y = oy + (n.y ?? 0);
|
|
155
|
+
origin.set(n.id, { x, y });
|
|
156
|
+
nodes.set(n.id, { id: n.id, parent, x, y, w: n.width ?? 0, h: n.height ?? 0 });
|
|
157
|
+
n.children?.forEach((c) => visit(c, x, y, n.id));
|
|
158
|
+
};
|
|
159
|
+
out.children?.forEach((c) => visit(c, 0, 0));
|
|
160
|
+
const edges = /* @__PURE__ */ new Map();
|
|
161
|
+
const collect = (n) => {
|
|
162
|
+
n.edges?.forEach((e) => {
|
|
163
|
+
const container = e.container ?? n.id;
|
|
164
|
+
const o = origin.get(container) ?? { x: 0, y: 0 };
|
|
165
|
+
const shift = (p) => ({ x: p.x + o.x, y: p.y + o.y });
|
|
166
|
+
const section = e.sections?.[0];
|
|
167
|
+
const points = section ? [section.startPoint, ...section.bendPoints ?? [], section.endPoint].map(shift) : [];
|
|
168
|
+
const labels = (e.labels ?? []).map((l) => ({ text: l.text ?? "", x: (l.x ?? 0) + o.x, y: (l.y ?? 0) + o.y, w: l.width ?? 0, h: l.height ?? 0 }));
|
|
169
|
+
edges.set(e.id, { id: e.id, points, labels });
|
|
170
|
+
});
|
|
171
|
+
n.children?.forEach(collect);
|
|
172
|
+
};
|
|
173
|
+
collect(out);
|
|
174
|
+
return { width: out.width ?? 0, height: out.height ?? 0, nodes, edges };
|
|
175
|
+
}
|
|
176
|
+
var ARCH_SIZES = {
|
|
177
|
+
card: { w: 248, h: 104 },
|
|
178
|
+
cylinder: { w: 240, h: 112 },
|
|
179
|
+
stack: { w: 240, h: 104 },
|
|
180
|
+
person: { w: 176, h: 120 },
|
|
181
|
+
frame: { w: 330, h: 150 },
|
|
182
|
+
chip: { w: 220, h: 64 },
|
|
183
|
+
group: { w: 248, h: 104 }
|
|
184
|
+
};
|
|
185
|
+
var ARCH_PADDING = { container: 56, group: 38 };
|
|
186
|
+
async function layoutArchitecture(doc, view, options = {}) {
|
|
187
|
+
const idx = indexOf(doc);
|
|
188
|
+
const diagram = projectArchitecture(doc, view, options);
|
|
189
|
+
const toElk = (n) => {
|
|
190
|
+
const compound = n.children.length > 0;
|
|
191
|
+
const shape = n.type === "group" ? "group" : idx.style(n.entityId, view.id).shape;
|
|
192
|
+
const size = n.collapsed && n.type === "entity" ? ARCH_SIZES.frame : ARCH_SIZES[shape];
|
|
193
|
+
const fixed = view.positions[n.id];
|
|
194
|
+
const node = { id: n.id };
|
|
195
|
+
if (compound) {
|
|
196
|
+
const top = n.type === "group" ? ARCH_PADDING.group : ARCH_PADDING.container;
|
|
197
|
+
node.layoutOptions = {
|
|
198
|
+
"elk.padding": `[top=${top},left=24,bottom=24,right=24]`,
|
|
199
|
+
"elk.spacing.nodeNode": "28"
|
|
200
|
+
};
|
|
201
|
+
node.children = n.children.map(toElk);
|
|
202
|
+
} else {
|
|
203
|
+
node.width = size.w;
|
|
204
|
+
node.height = size.h;
|
|
205
|
+
}
|
|
206
|
+
const kind = n.entityId ? idx.entities.get(n.entityId)?.kind : void 0;
|
|
207
|
+
if (!compound && (kind === "person" || kind === "external"))
|
|
208
|
+
node.layoutOptions = { "elk.layered.layering.layerConstraint": kind === "person" ? "FIRST" : "LAST" };
|
|
209
|
+
if (fixed) {
|
|
210
|
+
node.x = fixed.x;
|
|
211
|
+
node.y = fixed.y;
|
|
212
|
+
}
|
|
213
|
+
return node;
|
|
214
|
+
};
|
|
215
|
+
const kindOf = (nodeId) => idx.entities.get(nodeId)?.kind;
|
|
216
|
+
const reversed = new Set(diagram.edges.filter((e) => kindOf(e.source) === "external" && kindOf(e.target) !== "external").map((e) => e.id));
|
|
217
|
+
const graph = {
|
|
218
|
+
id: "root",
|
|
219
|
+
layoutOptions: {
|
|
220
|
+
...BASE_OPTIONS,
|
|
221
|
+
"elk.direction": diagram.direction === "down" ? "DOWN" : "RIGHT",
|
|
222
|
+
"elk.hierarchyHandling": "INCLUDE_CHILDREN",
|
|
223
|
+
"elk.spacing.nodeNode": "36",
|
|
224
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": "84",
|
|
225
|
+
"elk.spacing.edgeNode": "22",
|
|
226
|
+
"elk.spacing.edgeEdge": "14",
|
|
227
|
+
"elk.layered.spacing.edgeNodeBetweenLayers": "26",
|
|
228
|
+
"elk.layered.spacing.edgeEdgeBetweenLayers": "14",
|
|
229
|
+
"elk.padding": "[top=24,left=24,bottom=24,right=24]"
|
|
230
|
+
},
|
|
231
|
+
children: diagram.roots.map(toElk),
|
|
232
|
+
edges: diagram.edges.map((e) => reversed.has(e.id) ? { id: e.id, sources: [e.target], targets: [e.source] } : { id: e.id, sources: [e.source], targets: [e.target] })
|
|
233
|
+
};
|
|
234
|
+
const out = await runElk(graph);
|
|
235
|
+
const meta = /* @__PURE__ */ new Map();
|
|
236
|
+
const depthOf = /* @__PURE__ */ new Map();
|
|
237
|
+
const walk = (n, depth) => {
|
|
238
|
+
meta.set(n.id, n);
|
|
239
|
+
depthOf.set(n.id, depth);
|
|
240
|
+
n.children.forEach((c) => walk(c, depth + 1));
|
|
241
|
+
};
|
|
242
|
+
diagram.roots.forEach((r) => walk(r, 0));
|
|
243
|
+
const nodes = [...out.nodes.values()].map((r) => {
|
|
244
|
+
const m = meta.get(r.id);
|
|
245
|
+
return {
|
|
246
|
+
id: r.id,
|
|
247
|
+
type: m.type,
|
|
248
|
+
entityId: m.entityId,
|
|
249
|
+
groupId: m.groupId,
|
|
250
|
+
parent: m.parent,
|
|
251
|
+
compound: m.children.length > 0,
|
|
252
|
+
collapsed: m.collapsed,
|
|
253
|
+
expandable: m.collapsed || m.children.length > 0,
|
|
254
|
+
hiddenCount: m.hiddenCount,
|
|
255
|
+
depth: depthOf.get(r.id) ?? 0,
|
|
256
|
+
x: r.x,
|
|
257
|
+
y: r.y,
|
|
258
|
+
w: r.w,
|
|
259
|
+
h: r.h
|
|
260
|
+
};
|
|
261
|
+
}).sort((a, b) => a.depth - b.depth);
|
|
262
|
+
const edges = diagram.edges.map((e) => {
|
|
263
|
+
const points = out.edges.get(e.id)?.points ?? [];
|
|
264
|
+
return { ...e, points: reversed.has(e.id) ? points.slice().reverse() : points };
|
|
265
|
+
}).filter((e) => e.points.length >= 2);
|
|
266
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
267
|
+
const anchors = {};
|
|
268
|
+
for (const n of nodes) {
|
|
269
|
+
if (n.entityId) anchors[refKey({ kind: "entity", id: n.entityId })] = n;
|
|
270
|
+
if (n.groupId) anchors[refKey({ kind: "group", view: view.id, id: n.groupId })] = n;
|
|
271
|
+
}
|
|
272
|
+
for (const [entityId, rep] of Object.entries(diagram.representative)) {
|
|
273
|
+
const r = byId.get(rep);
|
|
274
|
+
if (r && !entityId.startsWith("g:")) anchors[refKey({ kind: "entity", id: entityId })] ??= r;
|
|
275
|
+
}
|
|
276
|
+
for (const e of edges) {
|
|
277
|
+
const box = union(e.points.map((p) => ({ x: p.x, y: p.y, w: 1, h: 1 })));
|
|
278
|
+
for (const r of e.relationships) anchors[refKey({ kind: "relationship", id: r })] = box;
|
|
279
|
+
}
|
|
280
|
+
return { width: out.width, height: out.height, nodes, edges, anchors };
|
|
281
|
+
}
|
|
282
|
+
var SEQ = {
|
|
283
|
+
headerH: 64,
|
|
284
|
+
headerGap: 30,
|
|
285
|
+
row: 44,
|
|
286
|
+
selfRow: 58,
|
|
287
|
+
fragmentHeader: 34,
|
|
288
|
+
branchHeader: 30,
|
|
289
|
+
fragmentEnd: 14,
|
|
290
|
+
activationW: 10,
|
|
291
|
+
margin: 32,
|
|
292
|
+
labelSize: 12.5
|
|
293
|
+
};
|
|
294
|
+
function prune(steps, keep) {
|
|
295
|
+
const out = [];
|
|
296
|
+
for (const s of steps) {
|
|
297
|
+
if (s.type === "message") {
|
|
298
|
+
if (keep.has(s.id)) out.push(s);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
const branches = s.branches.map((b) => ({ ...b, steps: prune(b.steps, keep) })).filter((b) => b.steps.length > 0);
|
|
302
|
+
if (branches.length) out.push({ ...s, branches });
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
306
|
+
function layoutSequence(doc, view, options = {}) {
|
|
307
|
+
const idx = indexOf(doc);
|
|
308
|
+
const full = idx.interaction(view.interaction);
|
|
309
|
+
if (!full) throw new Error(`Unknown interaction "${view.interaction}"`);
|
|
310
|
+
const numberOf = new Map(idx.messages(full.id).map((f) => [f.message.id, f.number]));
|
|
311
|
+
const keep = options.only?.length ? new Set(options.only) : void 0;
|
|
312
|
+
const steps = keep ? prune(full.steps, keep) : full.steps;
|
|
313
|
+
const used = /* @__PURE__ */ new Set();
|
|
314
|
+
const collect = (list) => list.forEach((s) => s.type === "message" ? (used.add(s.from), used.add(s.to)) : s.branches.forEach((b) => collect(b.steps)));
|
|
315
|
+
collect(steps);
|
|
316
|
+
const interaction = { ...full, steps, participants: keep ? full.participants.filter((p) => used.has(p)) : full.participants };
|
|
317
|
+
const flat = [];
|
|
318
|
+
const walkFlat = (list) => list.forEach((s) => s.type === "message" ? flat.push({ message: s }) : s.branches.forEach((b) => walkFlat(b.steps)));
|
|
319
|
+
walkFlat(steps);
|
|
320
|
+
const ids = interaction.participants;
|
|
321
|
+
const col = new Map(ids.map((id, i) => [id, i]));
|
|
322
|
+
const compact = !!options.compact;
|
|
323
|
+
const widths = ids.map((id) => Math.max(compact ? 128 : 156, measure(idx.entities.get(id)?.name ?? id, 14.5, 650) + (compact ? 60 : 70)));
|
|
324
|
+
const gaps = ids.map((_, i) => i === 0 ? 0 : widths[i - 1] / 2 + widths[i] / 2 + (compact ? 28 : 36));
|
|
325
|
+
const labelWidth = (m) => (measure(m.label, SEQ.labelSize, 500) + 44 + (compact ? 60 : 0)) * (compact ? 0.62 : 1);
|
|
326
|
+
for (const { message: m } of flat) {
|
|
327
|
+
const a = col.get(m.from);
|
|
328
|
+
const b = col.get(m.to);
|
|
329
|
+
if (a === b) {
|
|
330
|
+
if (a + 1 < ids.length) gaps[a + 1] = Math.max(gaps[a + 1], labelWidth(m) + (compact ? 40 : 56));
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
const [lo, hi] = a < b ? [a, b] : [b, a];
|
|
334
|
+
const span = gaps.slice(lo + 1, hi + 1).reduce((s, g) => s + g, 0);
|
|
335
|
+
const need = labelWidth(m) + 24;
|
|
336
|
+
if (span < need) for (let i = lo + 1; i <= hi; i++) gaps[i] += (need - span) / (hi - lo);
|
|
337
|
+
}
|
|
338
|
+
const xs = [];
|
|
339
|
+
ids.forEach((_, i) => xs.push(i === 0 ? SEQ.margin + 40 + widths[0] / 2 : xs[i - 1] + gaps[i]));
|
|
340
|
+
const xOf = (id) => xs[col.get(id)];
|
|
341
|
+
const messages = [];
|
|
342
|
+
const fragments = [];
|
|
343
|
+
const activations = [];
|
|
344
|
+
const open = new Map(ids.map((id) => [id, []]));
|
|
345
|
+
const depth = (id) => open.get(id).length;
|
|
346
|
+
const barX = (id, d) => xOf(id) - SEQ.activationW / 2 + Math.max(0, d - 1) * 4;
|
|
347
|
+
const edgeX = (id, towardsRight) => {
|
|
348
|
+
const d = depth(id);
|
|
349
|
+
if (d === 0) return xOf(id);
|
|
350
|
+
const bx = barX(id, d);
|
|
351
|
+
return towardsRight ? bx + SEQ.activationW : bx;
|
|
352
|
+
};
|
|
353
|
+
const push = (id, y2) => open.get(id).push(y2);
|
|
354
|
+
const pop = (id, y2) => {
|
|
355
|
+
const stack = open.get(id);
|
|
356
|
+
const start = stack.pop();
|
|
357
|
+
if (start !== void 0) activations.push({ entityId: id, x: barX(id, stack.length + 1), y1: start, y2: Math.max(y2, start + 12) });
|
|
358
|
+
};
|
|
359
|
+
const snapshot = () => new Map([...open].map(([k, v]) => [k, v.length]));
|
|
360
|
+
const restore = (snap, y2) => {
|
|
361
|
+
for (const id of ids) {
|
|
362
|
+
while (depth(id) > 0) pop(id, y2);
|
|
363
|
+
for (let i = 0; i < (snap.get(id) ?? 0); i++) push(id, y2);
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
let y = SEQ.headerH + SEQ.headerGap;
|
|
367
|
+
let maxRight = xs[xs.length - 1] + widths[widths.length - 1] / 2;
|
|
368
|
+
const involved = (steps2, acc = /* @__PURE__ */ new Set()) => {
|
|
369
|
+
for (const s of steps2) {
|
|
370
|
+
if (s.type === "message") {
|
|
371
|
+
acc.add(s.from);
|
|
372
|
+
acc.add(s.to);
|
|
373
|
+
} else s.branches.forEach((b) => involved(b.steps, acc));
|
|
374
|
+
}
|
|
375
|
+
return acc;
|
|
376
|
+
};
|
|
377
|
+
const nesting = (steps2) => Math.max(0, ...steps2.map((s) => s.type === "fragment" ? 1 + Math.max(...s.branches.map((b) => nesting(b.steps))) : 0));
|
|
378
|
+
const walk = (steps2, stackIds, level) => {
|
|
379
|
+
for (const s of steps2) {
|
|
380
|
+
if (s.type === "message") {
|
|
381
|
+
const m = s;
|
|
382
|
+
if (m.kind === "self") {
|
|
383
|
+
const x2 = edgeX(m.from, true);
|
|
384
|
+
push(m.from, y + 6);
|
|
385
|
+
const w = 34;
|
|
386
|
+
messages.push({ id: m.id, number: numberOf.get(m.id), kind: m.kind, from: m.from, to: m.to, label: m.label, y, x1: x2, x2, loop: { x: x2, y1: y - 2, y2: y + 20, w }, fragments: stackIds });
|
|
387
|
+
maxRight = Math.max(maxRight, x2 + w + measure(m.label, SEQ.labelSize, 500) + 48);
|
|
388
|
+
pop(m.from, y + 28);
|
|
389
|
+
y += SEQ.selfRow;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const right2 = xOf(m.to) > xOf(m.from);
|
|
393
|
+
if (m.kind === "call") {
|
|
394
|
+
if (depth(m.from) === 0 && !compact) push(m.from, y - 10);
|
|
395
|
+
const x1 = edgeX(m.from, right2);
|
|
396
|
+
push(m.to, y);
|
|
397
|
+
const x2 = edgeX(m.to, !right2);
|
|
398
|
+
messages.push({ id: m.id, number: numberOf.get(m.id), kind: m.kind, from: m.from, to: m.to, label: m.label, y, x1, x2, fragments: stackIds });
|
|
399
|
+
} else if (m.kind === "return") {
|
|
400
|
+
const x1 = edgeX(m.from, right2);
|
|
401
|
+
pop(m.from, y);
|
|
402
|
+
const x2 = edgeX(m.to, !right2);
|
|
403
|
+
messages.push({ id: m.id, number: numberOf.get(m.id), kind: m.kind, from: m.from, to: m.to, label: m.label, y, x1, x2, fragments: stackIds });
|
|
404
|
+
} else {
|
|
405
|
+
const x1 = edgeX(m.from, right2);
|
|
406
|
+
const x2 = edgeX(m.to, !right2);
|
|
407
|
+
messages.push({ id: m.id, number: numberOf.get(m.id), kind: m.kind, from: m.from, to: m.to, label: m.label, y, x1, x2, fragments: stackIds });
|
|
408
|
+
}
|
|
409
|
+
y += SEQ.row;
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
const f = s;
|
|
413
|
+
const top = y - 18;
|
|
414
|
+
const who = [...involved([f])].map((id) => col.get(id)).sort((a, b) => a - b);
|
|
415
|
+
const pad = 30 + (nesting([f]) - 1) * 12;
|
|
416
|
+
const x = xs[who[0]] - pad;
|
|
417
|
+
let right = xs[who[who.length - 1]] + pad;
|
|
418
|
+
y = top + SEQ.fragmentHeader + 18;
|
|
419
|
+
const branches = [];
|
|
420
|
+
const snap = snapshot();
|
|
421
|
+
const before = messages.length;
|
|
422
|
+
f.branches.forEach((b, i) => {
|
|
423
|
+
if (i > 0) {
|
|
424
|
+
const sepY = y - 18;
|
|
425
|
+
branches.push({ guard: b.guard, y: sepY });
|
|
426
|
+
restore(snap, sepY);
|
|
427
|
+
y = sepY + SEQ.branchHeader + 18;
|
|
428
|
+
} else branches.push({ guard: b.guard, y: top });
|
|
429
|
+
walk(b.steps, [...stackIds, f.id], level + 1);
|
|
430
|
+
});
|
|
431
|
+
for (const m of messages.slice(before)) if (m.loop) right = Math.max(right, m.loop.x + m.loop.w + measure(m.label, SEQ.labelSize, 500) + 48);
|
|
432
|
+
const bottom = y - 18 + SEQ.fragmentEnd;
|
|
433
|
+
fragments.push({ id: f.id, kind: f.kind, label: f.label, depth: level, branches, x, y: top, w: right - x, h: bottom - top });
|
|
434
|
+
maxRight = Math.max(maxRight, right);
|
|
435
|
+
y = bottom + 26;
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
walk(interaction.steps, [], 0);
|
|
439
|
+
const end = y + 4;
|
|
440
|
+
for (const id of ids) while (depth(id) > 0) pop(id, end - 18);
|
|
441
|
+
const participants = ids.map((id, i) => ({
|
|
442
|
+
entityId: id,
|
|
443
|
+
x: xs[i],
|
|
444
|
+
header: { x: xs[i] - widths[i] / 2, y: 0, w: widths[i], h: SEQ.headerH },
|
|
445
|
+
lifeline: { y1: SEQ.headerH, y2: end }
|
|
446
|
+
}));
|
|
447
|
+
const minX = Math.min(0, ...fragments.map((f) => f.x - SEQ.margin));
|
|
448
|
+
const shift = -minX;
|
|
449
|
+
if (shift) {
|
|
450
|
+
participants.forEach((p) => {
|
|
451
|
+
p.x += shift;
|
|
452
|
+
p.header.x += shift;
|
|
453
|
+
});
|
|
454
|
+
messages.forEach((m) => {
|
|
455
|
+
m.x1 += shift;
|
|
456
|
+
m.x2 += shift;
|
|
457
|
+
if (m.loop) m.loop.x += shift;
|
|
458
|
+
});
|
|
459
|
+
activations.forEach((a) => a.x += shift);
|
|
460
|
+
fragments.forEach((f) => f.x += shift);
|
|
461
|
+
maxRight += shift;
|
|
462
|
+
}
|
|
463
|
+
const anchors = {};
|
|
464
|
+
anchors[refKey({ kind: "behavior", id: interaction.id })] = { x: 0, y: 0, w: maxRight + SEQ.margin, h: end };
|
|
465
|
+
for (const p of participants) anchors[refKey({ kind: "entity", id: p.entityId })] = p.header;
|
|
466
|
+
for (const m of messages) {
|
|
467
|
+
const x = Math.min(m.x1, m.x2);
|
|
468
|
+
const w = Math.max(Math.abs(m.x2 - m.x1), m.loop ? m.loop.w + 120 : 0);
|
|
469
|
+
anchors[refKey({ kind: "message", behavior: interaction.id, id: m.id })] = { x, y: m.y - 22, w, h: 32 };
|
|
470
|
+
}
|
|
471
|
+
for (const f of fragments) anchors[refKey({ kind: "fragment", behavior: interaction.id, id: f.id })] = f;
|
|
472
|
+
return { interactionId: interaction.id, width: maxRight + SEQ.margin, height: end + 24, participants, messages, activations, fragments, anchors };
|
|
473
|
+
}
|
|
474
|
+
var SIZES = {
|
|
475
|
+
terminator: { w: 184, h: 64 },
|
|
476
|
+
process: { w: 196, h: 64 },
|
|
477
|
+
store: { w: 204, h: 52 }
|
|
478
|
+
};
|
|
479
|
+
async function layoutDataFlow(doc, view, options = {}) {
|
|
480
|
+
const idx = indexOf(doc);
|
|
481
|
+
const d = projectDataFlow(doc, view, options);
|
|
482
|
+
const constraint = (id) => {
|
|
483
|
+
const role = d.nodes.find((n) => n.id === id)?.role;
|
|
484
|
+
if (role !== "terminator") return {};
|
|
485
|
+
const ins = d.flows.some((f) => f.target === id);
|
|
486
|
+
const outs = d.flows.some((f) => f.source === id);
|
|
487
|
+
const c = !ins ? "FIRST" : !outs ? "LAST" : void 0;
|
|
488
|
+
return c ? { layoutOptions: { "elk.layered.layering.layerConstraint": c } } : {};
|
|
489
|
+
};
|
|
490
|
+
const out = await runElk({
|
|
491
|
+
id: "root",
|
|
492
|
+
layoutOptions: {
|
|
493
|
+
...BASE_OPTIONS,
|
|
494
|
+
"elk.direction": "DOWN",
|
|
495
|
+
"elk.spacing.nodeNode": "48",
|
|
496
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": "48",
|
|
497
|
+
"elk.spacing.edgeNode": "24",
|
|
498
|
+
"elk.spacing.edgeEdge": "16",
|
|
499
|
+
"elk.spacing.edgeLabel": "6",
|
|
500
|
+
"elk.edgeLabels.placement": "CENTER",
|
|
501
|
+
"elk.padding": "[top=32,left=32,bottom=32,right=32]"
|
|
502
|
+
},
|
|
503
|
+
children: d.nodes.map((n) => ({
|
|
504
|
+
id: n.id,
|
|
505
|
+
width: SIZES[n.role].w,
|
|
506
|
+
height: SIZES[n.role].h,
|
|
507
|
+
...view.positions[n.id],
|
|
508
|
+
...constraint(n.id)
|
|
509
|
+
})),
|
|
510
|
+
edges: [
|
|
511
|
+
...d.flows.map((f) => ({
|
|
512
|
+
id: f.id,
|
|
513
|
+
sources: [f.source],
|
|
514
|
+
targets: [f.target],
|
|
515
|
+
labels: [{ text: f.label, width: measure(f.label, 11.5, 500) + 26, height: 22 }]
|
|
516
|
+
})),
|
|
517
|
+
...d.control.map((c) => ({ id: c.id, sources: [c.source], targets: [c.target], layoutOptions: { "elk.layered.priority.direction": "0" } }))
|
|
518
|
+
]
|
|
519
|
+
});
|
|
520
|
+
const nodes = d.nodes.map((n) => ({ ...out.nodes.get(n.id), id: n.id, entityId: n.entityId, role: n.role }));
|
|
521
|
+
const edges = [
|
|
522
|
+
...d.control.map((c) => ({ id: c.id, control: true, source: c.source, target: c.target, points: out.edges.get(c.id)?.points ?? [], data: [] })),
|
|
523
|
+
...d.flows.map((f) => {
|
|
524
|
+
const e = out.edges.get(f.id);
|
|
525
|
+
return { id: f.id, control: false, source: f.source, target: f.target.replace(/~in$/, ""), points: e?.points ?? [], label: e?.labels[0], data: f.data };
|
|
526
|
+
})
|
|
527
|
+
].filter((e) => e.points.length >= 2);
|
|
528
|
+
const anchors = {};
|
|
529
|
+
for (const n of nodes) anchors[refKey({ kind: "entity", id: n.entityId })] ??= n;
|
|
530
|
+
for (const e of edges) {
|
|
531
|
+
const box = union([...e.points.map((p) => ({ x: p.x, y: p.y, w: 1, h: 1 })), ...e.label ? [e.label] : []]);
|
|
532
|
+
anchors[refKey(e.control ? { kind: "relationship", id: e.id } : { kind: "dataFlow", id: e.id })] = box;
|
|
533
|
+
const flow = idx.dataFlows.get(e.id);
|
|
534
|
+
if (flow?.via) anchors[refKey({ kind: "relationship", id: flow.via })] ??= box;
|
|
535
|
+
}
|
|
536
|
+
return { width: out.width, height: out.height, nodes, edges, anchors };
|
|
537
|
+
}
|
|
538
|
+
var ROW = 92;
|
|
539
|
+
var HEADER = 52;
|
|
540
|
+
var TOP = HEADER + 40;
|
|
541
|
+
var CELL = 212;
|
|
542
|
+
function layoutActivity(doc, view) {
|
|
543
|
+
const idx = indexOf(doc);
|
|
544
|
+
const process = idx.process(view.process);
|
|
545
|
+
if (!process) throw new Error(`Unknown process "${view.process}"`);
|
|
546
|
+
const nodes = process.nodes;
|
|
547
|
+
const out = /* @__PURE__ */ new Map();
|
|
548
|
+
const incoming = /* @__PURE__ */ new Map();
|
|
549
|
+
for (const f of process.flows) {
|
|
550
|
+
out.set(f.from, [...out.get(f.from) ?? [], f.to]);
|
|
551
|
+
incoming.set(f.to, [...incoming.get(f.to) ?? [], f.from]);
|
|
552
|
+
}
|
|
553
|
+
const start = nodes.find((n) => n.kind === "start") ?? nodes[0];
|
|
554
|
+
const back = /* @__PURE__ */ new Set();
|
|
555
|
+
const state = /* @__PURE__ */ new Map();
|
|
556
|
+
const dfs = (id) => {
|
|
557
|
+
state.set(id, "open");
|
|
558
|
+
for (const f of process.flows.filter((x) => x.from === id)) {
|
|
559
|
+
const s = state.get(f.to);
|
|
560
|
+
if (s === "open") back.add(f.id);
|
|
561
|
+
else if (!s) dfs(f.to);
|
|
562
|
+
}
|
|
563
|
+
state.set(id, "done");
|
|
564
|
+
};
|
|
565
|
+
dfs(start.id);
|
|
566
|
+
nodes.forEach((n) => !state.has(n.id) && dfs(n.id));
|
|
567
|
+
const forward = process.flows.filter((f) => !back.has(f.id));
|
|
568
|
+
const rank = new Map(nodes.map((n) => [n.id, 0]));
|
|
569
|
+
const indeg = new Map(nodes.map((n) => [n.id, 0]));
|
|
570
|
+
forward.forEach((f) => indeg.set(f.to, (indeg.get(f.to) ?? 0) + 1));
|
|
571
|
+
const queue = nodes.filter((n) => (indeg.get(n.id) ?? 0) === 0).map((n) => n.id);
|
|
572
|
+
while (queue.length) {
|
|
573
|
+
const id = queue.shift();
|
|
574
|
+
for (const f of forward.filter((x) => x.from === id)) {
|
|
575
|
+
rank.set(f.to, Math.max(rank.get(f.to), rank.get(id) + 1));
|
|
576
|
+
indeg.set(f.to, indeg.get(f.to) - 1);
|
|
577
|
+
if (indeg.get(f.to) === 0) queue.push(f.to);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
const performers = [];
|
|
581
|
+
if (view.swimlanes) {
|
|
582
|
+
for (const n of nodes) if (!performers.includes(n.performedBy)) performers.push(n.performedBy);
|
|
583
|
+
}
|
|
584
|
+
if (performers.length === 0) performers.push(void 0);
|
|
585
|
+
const laneOf = (n) => view.swimlanes ? performers.indexOf(n.performedBy) : 0;
|
|
586
|
+
const cells = /* @__PURE__ */ new Map();
|
|
587
|
+
for (const n of nodes) {
|
|
588
|
+
const key = `${laneOf(n)}:${rank.get(n.id)}`;
|
|
589
|
+
cells.set(key, [...cells.get(key) ?? [], n.id]);
|
|
590
|
+
}
|
|
591
|
+
const laneWidth = performers.map((_, li) => Math.max(240, ...[...cells.entries()].filter(([k]) => k.startsWith(`${li}:`)).map(([, v]) => v.length * CELL + 28)));
|
|
592
|
+
const lanes = [];
|
|
593
|
+
let lx = 0;
|
|
594
|
+
performers.forEach((p, i) => {
|
|
595
|
+
lanes.push({ entityId: p, label: p ? idx.entities.get(p)?.name ?? p : view.swimlanes ? "Unassigned" : process.name, x: lx, w: laneWidth[i] });
|
|
596
|
+
lx += laneWidth[i];
|
|
597
|
+
});
|
|
598
|
+
const scene = /* @__PURE__ */ new Map();
|
|
599
|
+
for (const n of nodes) {
|
|
600
|
+
const li = laneOf(n);
|
|
601
|
+
const r = rank.get(n.id);
|
|
602
|
+
const cell = cells.get(`${li}:${r}`);
|
|
603
|
+
const slot = cell.indexOf(n.id);
|
|
604
|
+
const cx2 = lanes[li].x + lanes[li].w / 2 + (slot - (cell.length - 1) / 2) * CELL;
|
|
605
|
+
const cy2 = TOP + r * ROW;
|
|
606
|
+
let w = 0;
|
|
607
|
+
let h = 0;
|
|
608
|
+
let lines = [n.label];
|
|
609
|
+
switch (n.kind) {
|
|
610
|
+
case "start":
|
|
611
|
+
w = h = 22;
|
|
612
|
+
break;
|
|
613
|
+
case "end":
|
|
614
|
+
w = h = 26;
|
|
615
|
+
break;
|
|
616
|
+
case "fork":
|
|
617
|
+
case "join":
|
|
618
|
+
w = 120;
|
|
619
|
+
h = 6;
|
|
620
|
+
break;
|
|
621
|
+
case "decision":
|
|
622
|
+
case "merge":
|
|
623
|
+
w = Math.max(96, measure(n.label, 12.5, 600) + 60);
|
|
624
|
+
h = 54;
|
|
625
|
+
break;
|
|
626
|
+
default:
|
|
627
|
+
lines = wrap(n.label, 156, 13, 500, 2);
|
|
628
|
+
w = Math.min(188, Math.max(124, Math.max(...lines.map((l) => measure(l, 13, 500))) + 32));
|
|
629
|
+
h = lines.length > 1 ? 58 : 42;
|
|
630
|
+
}
|
|
631
|
+
scene.set(n.id, { id: n.id, kind: n.kind, label: n.label, lines, outcome: n.outcome, lane: li, rank: r, x: cx2 - w / 2, y: cy2 - h / 2, w, h });
|
|
632
|
+
}
|
|
633
|
+
for (const n of scene.values()) {
|
|
634
|
+
if (n.kind !== "fork" && n.kind !== "join") continue;
|
|
635
|
+
const others = (n.kind === "fork" ? out.get(n.id) : incoming.get(n.id))?.map((id) => scene.get(id)) ?? [];
|
|
636
|
+
if (others.length < 2) continue;
|
|
637
|
+
const xs = others.map((o) => o.x + o.w / 2);
|
|
638
|
+
const lo = Math.min(...xs, n.x + n.w / 2) - 28;
|
|
639
|
+
const hi = Math.max(...xs, n.x + n.w / 2) + 28;
|
|
640
|
+
n.x = lo;
|
|
641
|
+
n.w = hi - lo;
|
|
642
|
+
}
|
|
643
|
+
const cx = (n) => n.x + n.w / 2;
|
|
644
|
+
const cy = (n) => n.y + n.h / 2;
|
|
645
|
+
const exits = /* @__PURE__ */ new Map();
|
|
646
|
+
for (const n of scene.values()) {
|
|
647
|
+
if (n.kind !== "decision") continue;
|
|
648
|
+
const outs = process.flows.filter((f) => f.from === n.id && !back.has(f.id));
|
|
649
|
+
const sorted = outs.slice().sort((a, b) => cx(scene.get(a.to)) - cx(scene.get(b.to)));
|
|
650
|
+
const free = /* @__PURE__ */ new Set(["left", "bottom", "right"]);
|
|
651
|
+
const want = (f) => {
|
|
652
|
+
const d = cx(scene.get(f.to)) - cx(n);
|
|
653
|
+
return Math.abs(d) < 2 ? "bottom" : d < 0 ? "left" : "right";
|
|
654
|
+
};
|
|
655
|
+
for (const f of sorted) {
|
|
656
|
+
const w = want(f);
|
|
657
|
+
const order = w === "left" ? ["left", "bottom", "right"] : w === "right" ? ["right", "bottom", "left"] : ["bottom", "left", "right"];
|
|
658
|
+
const pick = order.find((o) => free.has(o)) ?? "bottom";
|
|
659
|
+
free.delete(pick);
|
|
660
|
+
exits.set(f.id, pick);
|
|
661
|
+
}
|
|
662
|
+
for (const f of process.flows.filter((x) => x.from === n.id && back.has(x.id))) exits.set(f.id, free.has("right") ? "right" : "bottom");
|
|
663
|
+
}
|
|
664
|
+
const edges = [];
|
|
665
|
+
let channels = 0;
|
|
666
|
+
for (const f of process.flows) {
|
|
667
|
+
const a = scene.get(f.from);
|
|
668
|
+
const b = scene.get(f.to);
|
|
669
|
+
if (back.has(f.id)) {
|
|
670
|
+
const lo = Math.min(b.rank, a.rank);
|
|
671
|
+
const hi = Math.max(b.rank, a.rank);
|
|
672
|
+
const minX = Math.min(a.x, b.x);
|
|
673
|
+
const maxX = Math.max(a.x + a.w, b.x + b.w);
|
|
674
|
+
const between = [...scene.values()].filter((n) => n.rank >= lo && n.rank <= hi && n.x + n.w >= minX - CELL / 2 && n.x <= maxX + CELL / 2);
|
|
675
|
+
const channel = Math.max(...between.map((n) => n.x + n.w)) + 28 + channels++ * 14;
|
|
676
|
+
const gapY = a.y + a.h + 16 + (channels - 1) * 6;
|
|
677
|
+
const start2 = exits.get(f.id) === "right" ? { x: a.x + a.w, y: cy(a) } : { x: cx(a), y: a.y + a.h };
|
|
678
|
+
const points2 = exits.get(f.id) === "right" ? [start2, { x: channel, y: start2.y }, { x: channel, y: cy(b) }, { x: b.x + b.w, y: cy(b) }] : [start2, { x: start2.x, y: gapY }, { x: channel, y: gapY }, { x: channel, y: cy(b) }, { x: b.x + b.w, y: cy(b) }];
|
|
679
|
+
edges.push({ id: f.id, from: f.from, to: f.to, points: points2, guard: f.guard, guardAt: f.guard ? { x: start2.x + 8, y: start2.y + 14, anchor: "start" } : void 0, back: true });
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
const ex = b.kind === "join" ? cx(a) : cx(b);
|
|
683
|
+
const end = { x: ex, y: b.y };
|
|
684
|
+
let points;
|
|
685
|
+
let guardAt;
|
|
686
|
+
const exit = a.kind === "decision" ? exits.get(f.id) : void 0;
|
|
687
|
+
if (exit === "left" || exit === "right") {
|
|
688
|
+
const s = { x: exit === "right" ? a.x + a.w : a.x, y: cy(a) };
|
|
689
|
+
points = Math.abs(ex - s.x) < 1 ? [s, end] : [s, { x: ex, y: s.y }, end];
|
|
690
|
+
guardAt = { x: s.x + (exit === "right" ? 10 : -10), y: s.y - 9, anchor: exit === "right" ? "start" : "end" };
|
|
691
|
+
} else {
|
|
692
|
+
const sx = a.kind === "fork" ? cx(b) : cx(a);
|
|
693
|
+
const s = { x: sx, y: a.y + a.h };
|
|
694
|
+
const midY = end.y - 22;
|
|
695
|
+
points = Math.abs(s.x - end.x) < 1 ? [s, end] : [s, { x: s.x, y: midY }, { x: end.x, y: midY }, end];
|
|
696
|
+
guardAt = { x: s.x + 8, y: s.y + 16, anchor: "start" };
|
|
697
|
+
}
|
|
698
|
+
edges.push({ id: f.id, from: f.from, to: f.to, points, guard: f.guard, guardAt: f.guard ? guardAt : void 0, back: false });
|
|
699
|
+
}
|
|
700
|
+
const all = [...scene.values()];
|
|
701
|
+
const maxRight = Math.max(lx, ...edges.flatMap((e) => e.points.map((p) => p.x + 24)));
|
|
702
|
+
const height = Math.max(...all.map((n) => n.y + n.h)) + 72;
|
|
703
|
+
if (maxRight > lx) lanes[lanes.length - 1].w += maxRight - lx;
|
|
704
|
+
const anchors = {};
|
|
705
|
+
for (const n of all) anchors[refKey({ kind: "activityNode", behavior: process.id, id: n.id })] = n;
|
|
706
|
+
for (const e of edges) anchors[refKey({ kind: "activityFlow", behavior: process.id, id: e.id })] = union(e.points.map((p) => ({ ...p, w: 1, h: 1 })));
|
|
707
|
+
lanes.forEach((l) => {
|
|
708
|
+
if (l.entityId) anchors[refKey({ kind: "entity", id: l.entityId })] = { x: l.x, y: 0, w: l.w, h: HEADER };
|
|
709
|
+
});
|
|
710
|
+
anchors[refKey({ kind: "behavior", id: process.id })] = { x: 0, y: 0, w: maxRight, h: height };
|
|
711
|
+
return { processId: process.id, width: Math.max(lx, maxRight), height, lanes, laneHeader: HEADER, nodes: all, edges, anchors };
|
|
712
|
+
}
|
|
713
|
+
var ERD = { header: 40, row: 26, more: 28, bottom: 6 };
|
|
714
|
+
function tableSize(t) {
|
|
715
|
+
const nameW = measure(t.name, 13.5, 650) + 64;
|
|
716
|
+
const rowW = Math.max(0, ...t.fields.map((f) => measure(f.name, 12.5, 500, "mono") + measure(f.type, 11.5, 400, "mono") + 78));
|
|
717
|
+
const w = Math.ceil(Math.max(232, nameW, rowW));
|
|
718
|
+
const h = ERD.header + t.fields.length * ERD.row + (t.hiddenCount > 0 || t.expanded ? ERD.more : 0) + ERD.bottom;
|
|
719
|
+
return { w, h };
|
|
720
|
+
}
|
|
721
|
+
async function layoutErd(doc, view, options = {}) {
|
|
722
|
+
const d = projectErd(doc, view, options);
|
|
723
|
+
const tableNode = (t) => {
|
|
724
|
+
const s = tableSize(t);
|
|
725
|
+
return { id: t.dataTypeId, width: s.w, height: s.h, ...view.positions[t.dataTypeId] };
|
|
726
|
+
};
|
|
727
|
+
const out = await runElk({
|
|
728
|
+
id: "root",
|
|
729
|
+
layoutOptions: {
|
|
730
|
+
...BASE_OPTIONS,
|
|
731
|
+
"elk.direction": "RIGHT",
|
|
732
|
+
"elk.hierarchyHandling": "INCLUDE_CHILDREN",
|
|
733
|
+
"elk.spacing.nodeNode": "36",
|
|
734
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": "96",
|
|
735
|
+
"elk.spacing.edgeNode": "24",
|
|
736
|
+
"elk.spacing.edgeEdge": "16",
|
|
737
|
+
"elk.padding": "[top=32,left=32,bottom=32,right=32]"
|
|
738
|
+
},
|
|
739
|
+
children: [
|
|
740
|
+
...d.stores.map((s) => ({
|
|
741
|
+
id: `store:${s}`,
|
|
742
|
+
layoutOptions: { "elk.padding": "[top=44,left=20,bottom=20,right=20]", "elk.spacing.nodeNode": "28" },
|
|
743
|
+
children: d.tables.filter((t) => t.store === s).map(tableNode)
|
|
744
|
+
})),
|
|
745
|
+
...d.tables.filter((t) => !t.store).map(tableNode)
|
|
746
|
+
],
|
|
747
|
+
edges: d.relations.map((r) => ({ id: r.id, sources: [r.from], targets: [r.to] }))
|
|
748
|
+
});
|
|
749
|
+
const tables = d.tables.map((t) => {
|
|
750
|
+
const r = out.nodes.get(t.dataTypeId);
|
|
751
|
+
const refs = new Set(d.relations.filter((x) => x.via && (x.from === t.dataTypeId || x.to === t.dataTypeId)).map((x) => x.via));
|
|
752
|
+
return {
|
|
753
|
+
...r,
|
|
754
|
+
dataTypeId: t.dataTypeId,
|
|
755
|
+
name: t.name,
|
|
756
|
+
store: t.store,
|
|
757
|
+
hiddenCount: t.hiddenCount,
|
|
758
|
+
expanded: t.expanded,
|
|
759
|
+
rows: t.fields.map((f, i) => ({ ...f, y: r.y + ERD.header + i * ERD.row, foreign: !!f.references || refs.has(f.name) && !f.primaryKey }))
|
|
760
|
+
};
|
|
761
|
+
});
|
|
762
|
+
const byId = new Map(tables.map((t) => [t.dataTypeId, t]));
|
|
763
|
+
const rowY = (t, via) => {
|
|
764
|
+
const row = t.rows.find((f) => f.name === via) ?? t.rows.find((f) => f.primaryKey);
|
|
765
|
+
return row ? row.y + ERD.row / 2 : void 0;
|
|
766
|
+
};
|
|
767
|
+
const snapEnd = (pts, t, y) => {
|
|
768
|
+
if (y === void 0 || pts.length < 2) return pts;
|
|
769
|
+
const end = pts[pts.length - 1];
|
|
770
|
+
const prev = pts[pts.length - 2];
|
|
771
|
+
if (Math.abs(prev.y - end.y) > 0.5 || Math.abs(end.y - y) < 0.5) return pts;
|
|
772
|
+
const onLeft = Math.abs(end.x - t.x) < 2;
|
|
773
|
+
const jx = end.x + (onLeft ? -18 : 18);
|
|
774
|
+
if (onLeft && prev.x > jx || !onLeft && prev.x < jx) return pts;
|
|
775
|
+
return [...pts.slice(0, -1), { x: jx, y: end.y }, { x: jx, y }, { x: end.x, y }];
|
|
776
|
+
};
|
|
777
|
+
const relations = d.relations.map((r) => {
|
|
778
|
+
let points = out.edges.get(r.id)?.points ?? [];
|
|
779
|
+
const from = byId.get(r.from);
|
|
780
|
+
const to = byId.get(r.to);
|
|
781
|
+
const fromVia = from.rows.some((f) => f.name === r.via) && r.from !== r.to ? r.via : void 0;
|
|
782
|
+
points = snapEnd(points, to, rowY(to, r.via));
|
|
783
|
+
points = snapEnd(points.slice().reverse(), from, rowY(from, fromVia)).reverse();
|
|
784
|
+
return { ...r, points };
|
|
785
|
+
}).filter((r) => r.points.length >= 2);
|
|
786
|
+
const stores = d.stores.map((s) => ({ ...out.nodes.get(`store:${s}`), entityId: s }));
|
|
787
|
+
const anchors = {};
|
|
788
|
+
for (const t of tables) {
|
|
789
|
+
anchors[refKey({ kind: "dataType", id: t.dataTypeId })] = t;
|
|
790
|
+
for (const f of t.rows) anchors[refKey({ kind: "dataType", id: t.dataTypeId, field: f.name })] = { x: t.x, y: f.y, w: t.w, h: ERD.row };
|
|
791
|
+
}
|
|
792
|
+
for (const s of stores) anchors[refKey({ kind: "entity", id: s.entityId })] = s;
|
|
793
|
+
for (const r of relations) anchors[refKey({ kind: "dataRelation", id: r.id })] = union(r.points.map((p) => ({ ...p, w: 1, h: 1 })));
|
|
794
|
+
return { width: out.width, height: out.height, stores, tables, relations, anchors };
|
|
795
|
+
}
|
|
796
|
+
function transitionLabelSize(event, guard) {
|
|
797
|
+
const w = Math.max(measure(event, 11.5, 600), guard ? measure(`[${guard}]`, 10.5, 400) : 0) + 16;
|
|
798
|
+
return { width: Math.ceil(w), height: guard ? 34 : 22 };
|
|
799
|
+
}
|
|
800
|
+
async function layoutState(doc, view, options = {}) {
|
|
801
|
+
const idx = indexOf(doc);
|
|
802
|
+
const base = idx.stateMachine(view.stateMachine);
|
|
803
|
+
if (!base) throw new Error(`Unknown state machine "${view.stateMachine}"`);
|
|
804
|
+
const sm = { ...base, transitions: [...base.transitions, ...options.extraTransitions ?? []] };
|
|
805
|
+
const size = (s) => {
|
|
806
|
+
if (s.kind === "initial") return { w: 22, h: 22 };
|
|
807
|
+
const entry = s.entryActions.length ? `On entry: ${s.entryActions[0]}${s.entryActions.length > 1 ? ` +${s.entryActions.length - 1}` : ""}` : "";
|
|
808
|
+
const w = Math.max(148, measure(s.name, 14, 600) + 52, entry ? measure(entry, 11, 400) + 36 : 0);
|
|
809
|
+
return { w: Math.ceil(w), h: entry ? 64 : 48 };
|
|
810
|
+
};
|
|
811
|
+
const out = await runElk({
|
|
812
|
+
id: "root",
|
|
813
|
+
layoutOptions: {
|
|
814
|
+
...BASE_OPTIONS,
|
|
815
|
+
"elk.direction": "DOWN",
|
|
816
|
+
"elk.spacing.nodeNode": "56",
|
|
817
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": "44",
|
|
818
|
+
"elk.spacing.edgeNode": "22",
|
|
819
|
+
"elk.spacing.edgeEdge": "18",
|
|
820
|
+
"elk.spacing.edgeLabel": "6",
|
|
821
|
+
"elk.edgeLabels.placement": "CENTER",
|
|
822
|
+
"elk.padding": "[top=40,left=40,bottom=40,right=40]"
|
|
823
|
+
},
|
|
824
|
+
children: sm.states.map((s) => ({ id: s.id, width: size(s).w, height: size(s).h, ...view.positions[s.id] })),
|
|
825
|
+
edges: sm.transitions.map((t) => ({ id: t.id, sources: [t.from], targets: [t.to], labels: [{ text: t.event, ...transitionLabelSize(t.event, t.guard) }] }))
|
|
826
|
+
});
|
|
827
|
+
const states = sm.states.map((s) => ({ ...out.nodes.get(s.id), id: s.id, kind: s.kind, name: s.name, outcome: s.outcome, entryActions: s.entryActions }));
|
|
828
|
+
const transitions = sm.transitions.map((t) => {
|
|
829
|
+
const e = out.edges.get(t.id);
|
|
830
|
+
return { id: t.id, from: t.from, to: t.to, event: t.event, guard: t.guard, points: e?.points ?? [], label: e?.labels[0] };
|
|
831
|
+
}).filter((t) => t.points.length >= 2);
|
|
832
|
+
const anchors = {};
|
|
833
|
+
for (const s of states) anchors[refKey({ kind: "state", behavior: sm.id, id: s.id })] = s;
|
|
834
|
+
for (const t of transitions)
|
|
835
|
+
anchors[refKey({ kind: "transition", behavior: sm.id, id: t.id })] = union([...t.points.map((p) => ({ ...p, w: 1, h: 1 })), ...t.label ? [t.label] : []]);
|
|
836
|
+
anchors[refKey({ kind: "behavior", id: sm.id })] = { x: 0, y: 0, w: out.width, h: out.height };
|
|
837
|
+
if (sm.subject) anchors[refKey({ kind: "dataType", id: sm.subject })] = { x: 0, y: 0, w: out.width, h: out.height };
|
|
838
|
+
return { machineId: sm.id, width: out.width, height: out.height, states, transitions, anchors };
|
|
839
|
+
}
|
|
840
|
+
var ACTOR = { w: 168, h: 96 };
|
|
841
|
+
var PILL_H = 40;
|
|
842
|
+
var ROW2 = 52;
|
|
843
|
+
function layoutUseCase(doc, view) {
|
|
844
|
+
const idx = indexOf(doc);
|
|
845
|
+
const caps = view.capabilities.map((id) => idx.capabilities.get(id)).filter(isDefined);
|
|
846
|
+
const areaNames = [];
|
|
847
|
+
for (const c of caps) if (!areaNames.includes(c.area)) areaNames.push(c.area);
|
|
848
|
+
const pillW = Math.min(340, Math.max(240, ...caps.map((c) => measure(c.name, 13.5, 600) + 56)));
|
|
849
|
+
const boundaryX = ACTOR.w + 200;
|
|
850
|
+
const boundaryW = pillW + 140;
|
|
851
|
+
const capX = boundaryX + (boundaryW - pillW) / 2;
|
|
852
|
+
let y = 76;
|
|
853
|
+
const areas = [];
|
|
854
|
+
const capabilities = [];
|
|
855
|
+
for (const area of areaNames) {
|
|
856
|
+
const top = y;
|
|
857
|
+
y += 34;
|
|
858
|
+
for (const c of caps.filter((x) => x.area === area)) {
|
|
859
|
+
capabilities.push({ id: c.id, name: c.name, x: capX, y, w: pillW, h: PILL_H });
|
|
860
|
+
y += ROW2;
|
|
861
|
+
}
|
|
862
|
+
y += 6;
|
|
863
|
+
areas.push({ label: area, x: boundaryX + 16, y: top, w: boundaryW - 32, h: y - top });
|
|
864
|
+
y += 14;
|
|
865
|
+
}
|
|
866
|
+
const boundary = { x: boundaryX, y: 24, w: boundaryW, h: y + 4 - 24, title: doc.project.name };
|
|
867
|
+
const capById = new Map(capabilities.map((c) => [c.id, c]));
|
|
868
|
+
const place = (ids, side) => {
|
|
869
|
+
const x = side === "left" ? 24 : boundaryX + boundaryW + 176;
|
|
870
|
+
const wanted = ids.map((id) => {
|
|
871
|
+
const mine = caps.filter((c) => (side === "left" ? c.actors : c.supporting).includes(id)).map((c) => capById.get(c.id));
|
|
872
|
+
const cy = mine.reduce((s, r) => s + r.y + r.h / 2, 0) / Math.max(1, mine.length);
|
|
873
|
+
return { id, cy };
|
|
874
|
+
});
|
|
875
|
+
wanted.sort((a, b) => a.cy - b.cy);
|
|
876
|
+
let floor = 0;
|
|
877
|
+
return wanted.map(({ id, cy }) => {
|
|
878
|
+
const top = Math.max(floor, cy - ACTOR.h / 2);
|
|
879
|
+
floor = top + ACTOR.h + 28;
|
|
880
|
+
return { entityId: id, side, x, y: top, w: ACTOR.w, h: ACTOR.h };
|
|
881
|
+
});
|
|
882
|
+
};
|
|
883
|
+
const primary = [...new Set(caps.flatMap((c) => c.actors))];
|
|
884
|
+
const supporting = [...new Set(caps.flatMap((c) => c.supporting))].filter((id) => !primary.includes(id));
|
|
885
|
+
const actors = [...place(primary, "left"), ...place(supporting, "right")];
|
|
886
|
+
const actorById = new Map(actors.map((a) => [a.entityId, a]));
|
|
887
|
+
const associations = [];
|
|
888
|
+
for (const c of caps) {
|
|
889
|
+
const r = capById.get(c.id);
|
|
890
|
+
for (const a of c.actors) {
|
|
891
|
+
const box = actorById.get(a);
|
|
892
|
+
if (box) associations.push({ id: `${a}->${c.id}`, actor: a, capability: c.id, side: "left", from: { x: box.x + box.w - 22, y: box.y + 30 }, to: { x: r.x, y: r.y + r.h / 2 } });
|
|
893
|
+
}
|
|
894
|
+
for (const a of c.supporting) {
|
|
895
|
+
const box = actorById.get(a);
|
|
896
|
+
if (box?.side === "right") associations.push({ id: `${a}->${c.id}`, actor: a, capability: c.id, side: "right", from: { x: box.x + 22, y: box.y + 30 }, to: { x: r.x + r.w, y: r.y + r.h / 2 } });
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
const includes = [];
|
|
900
|
+
for (const c of caps)
|
|
901
|
+
for (const inc of c.includes) {
|
|
902
|
+
const a = capById.get(c.id);
|
|
903
|
+
const b = capById.get(inc);
|
|
904
|
+
if (!a || !b) continue;
|
|
905
|
+
includes.push({ id: `${c.id}=>${inc}`, from: c.id, to: inc, a: { x: a.x + a.w - 24, y: a.y + a.h }, b: { x: b.x + b.w - 24, y: b.y }, bulge: 0 });
|
|
906
|
+
}
|
|
907
|
+
const width = Math.max(...actors.map((a) => a.x + a.w), boundary.x + boundary.w) + 32;
|
|
908
|
+
const height = Math.max(boundary.y + boundary.h, ...actors.map((a) => a.y + a.h)) + 32;
|
|
909
|
+
const anchors = {};
|
|
910
|
+
for (const c of capabilities) anchors[refKey({ kind: "capability", id: c.id })] = c;
|
|
911
|
+
for (const a of actors) anchors[refKey({ kind: "entity", id: a.entityId })] = a;
|
|
912
|
+
return { width, height, boundary, areas, capabilities, actors, associations, includes, anchors };
|
|
913
|
+
}
|
|
914
|
+
var BAND_H = 132;
|
|
915
|
+
var LABEL_W = 168;
|
|
916
|
+
var NODE_H = 58;
|
|
917
|
+
var GAP = 28;
|
|
918
|
+
function layoutDependency(doc, view) {
|
|
919
|
+
const idx = indexOf(doc);
|
|
920
|
+
const shown = new Set(view.entities);
|
|
921
|
+
const bandOf = /* @__PURE__ */ new Map();
|
|
922
|
+
view.layers.forEach((l, i) => l.members.forEach((m) => shown.has(m) && bandOf.set(m, i)));
|
|
923
|
+
for (const id of view.entities) if (!bandOf.has(id)) bandOf.set(id, view.layers.length - 1);
|
|
924
|
+
const rels = (view.relationships ?? doc.model.relationships.filter((r) => shown.has(r.source) && shown.has(r.target)).map((r) => r.id)).map((id) => idx.relationships.get(id)).filter((r) => !!r && shown.has(r.source) && shown.has(r.target));
|
|
925
|
+
const width = (id) => Math.ceil(Math.max(184, measure(idx.entities.get(id)?.name ?? id, 13.5, 600) + 70));
|
|
926
|
+
const rows = view.layers.map((_, i) => view.entities.filter((id) => bandOf.get(id) === i));
|
|
927
|
+
const pos = /* @__PURE__ */ new Map();
|
|
928
|
+
const assign = () => rows.forEach((row) => row.forEach((id, i) => pos.set(id, i / Math.max(1, row.length - 1))));
|
|
929
|
+
assign();
|
|
930
|
+
const neighbors = (id) => rels.flatMap((r) => r.source === id ? [r.target] : r.target === id ? [r.source] : []);
|
|
931
|
+
const sweep = (order) => {
|
|
932
|
+
for (const b of order) {
|
|
933
|
+
const row = rows[b];
|
|
934
|
+
const score = new Map(row.map((id) => {
|
|
935
|
+
const ns = neighbors(id).filter((n) => bandOf.get(n) !== b);
|
|
936
|
+
return [id, ns.length ? ns.reduce((s, n) => s + pos.get(n), 0) / ns.length : pos.get(id)];
|
|
937
|
+
}));
|
|
938
|
+
row.sort((a, c) => score.get(a) - score.get(c));
|
|
939
|
+
row.forEach((id, i) => pos.set(id, i / Math.max(1, row.length - 1)));
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
const down = rows.map((_, i) => i);
|
|
943
|
+
sweep(down.slice(1));
|
|
944
|
+
sweep(down.slice(0, -1).reverse());
|
|
945
|
+
sweep(down.slice(1));
|
|
946
|
+
const rowWidth = rows.map((row) => row.reduce((s, id) => s + width(id), 0) + Math.max(0, row.length - 1) * GAP);
|
|
947
|
+
const content = Math.max(...rowWidth, 400);
|
|
948
|
+
const nodes = [];
|
|
949
|
+
rows.forEach((row, b) => {
|
|
950
|
+
let x = LABEL_W + 24 + (content - rowWidth[b]) / 2;
|
|
951
|
+
for (const id of row) {
|
|
952
|
+
nodes.push({ entityId: id, band: b, x, y: b * BAND_H + (BAND_H - NODE_H) / 2, w: width(id), h: NODE_H });
|
|
953
|
+
x += width(id) + GAP;
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
const byId = new Map(nodes.map((n) => [n.entityId, n]));
|
|
957
|
+
const totalW = LABEL_W + 24 + content + 40;
|
|
958
|
+
const bands = view.layers.map((l, i) => ({ id: l.id, label: l.label, description: l.description, x: 0, y: i * BAND_H, w: totalW, h: BAND_H }));
|
|
959
|
+
const ports = /* @__PURE__ */ new Map();
|
|
960
|
+
const portKey = (id, side) => `${id}:${side}`;
|
|
961
|
+
const plan = rels.map((r) => {
|
|
962
|
+
const a = byId.get(r.source);
|
|
963
|
+
const b = byId.get(r.target);
|
|
964
|
+
const direction = b.band > a.band ? "down" : b.band < a.band ? "up" : "across";
|
|
965
|
+
const out = direction === "down" ? "bottom" : "top";
|
|
966
|
+
const inn = direction === "down" ? "top" : direction === "up" ? "bottom" : "top";
|
|
967
|
+
ports.set(portKey(r.source, out), [...ports.get(portKey(r.source, out)) ?? [], r.id]);
|
|
968
|
+
ports.set(portKey(r.target, inn), [...ports.get(portKey(r.target, inn)) ?? [], r.id]);
|
|
969
|
+
return { r, a, b, direction, out, inn };
|
|
970
|
+
});
|
|
971
|
+
const portX = (id, side, relId, other) => {
|
|
972
|
+
const n = byId.get(id);
|
|
973
|
+
const list = (ports.get(portKey(id, side)) ?? []).slice();
|
|
974
|
+
const center2 = (relOther) => {
|
|
975
|
+
const r = idx.relationships.get(relOther);
|
|
976
|
+
const o = byId.get(r.source === id ? r.target : r.source);
|
|
977
|
+
return o.x + o.w / 2;
|
|
978
|
+
};
|
|
979
|
+
list.sort((p, q) => center2(p) - center2(q));
|
|
980
|
+
const i = list.indexOf(relId);
|
|
981
|
+
const span = Math.min(n.w * 0.6, (list.length - 1) * 18);
|
|
982
|
+
return n.x + n.w / 2 - span / 2 + (list.length > 1 ? i / (list.length - 1) * span : 0);
|
|
983
|
+
};
|
|
984
|
+
const edges = plan.map(({ r, a, b, direction, out, inn }) => {
|
|
985
|
+
const p0 = { x: portX(r.source, out, r.id), y: out === "bottom" ? a.y + a.h : a.y };
|
|
986
|
+
const p3 = { x: portX(r.target, inn, r.id), y: inn === "top" ? b.y : b.y + b.h };
|
|
987
|
+
let c1;
|
|
988
|
+
let c2;
|
|
989
|
+
if (direction === "across") {
|
|
990
|
+
const lift = 46;
|
|
991
|
+
c1 = { x: p0.x, y: p0.y - lift };
|
|
992
|
+
c2 = { x: p3.x, y: p3.y - lift };
|
|
993
|
+
} else {
|
|
994
|
+
const k = Math.max(40, Math.abs(p3.y - p0.y) * 0.45);
|
|
995
|
+
const s = direction === "down" ? 1 : -1;
|
|
996
|
+
c1 = { x: p0.x, y: p0.y + k * s };
|
|
997
|
+
c2 = { x: p3.x, y: p3.y - k * s };
|
|
998
|
+
}
|
|
999
|
+
return { id: r.id, source: r.source, target: r.target, kind: r.kind, d: [p0, c1, c2, p3], direction };
|
|
1000
|
+
});
|
|
1001
|
+
const anchors = {};
|
|
1002
|
+
for (const n of nodes) anchors[refKey({ kind: "entity", id: n.entityId })] = n;
|
|
1003
|
+
for (const b of bands) anchors[refKey({ kind: "group", view: view.id, id: b.id })] = b;
|
|
1004
|
+
for (const e of edges) {
|
|
1005
|
+
const xs = e.d.map((p) => p.x);
|
|
1006
|
+
const ys = e.d.map((p) => p.y);
|
|
1007
|
+
anchors[refKey({ kind: "relationship", id: e.id })] = { x: Math.min(...xs), y: Math.min(...ys), w: Math.max(...xs) - Math.min(...xs), h: Math.max(...ys) - Math.min(...ys) };
|
|
1008
|
+
}
|
|
1009
|
+
return { width: totalW, height: view.layers.length * BAND_H, bands, nodes, edges, anchors };
|
|
1010
|
+
}
|
|
1011
|
+
var GROUP = { head: 52, pad: 24 };
|
|
1012
|
+
var MAP = { h: 72, gapX: 120, gapY: 44, minW: 184, maxW: 260 };
|
|
1013
|
+
var LABEL = { size: 12, h: 22, subH: 36 };
|
|
1014
|
+
function mapNodeWidth(name) {
|
|
1015
|
+
return Math.round(Math.min(MAP.maxW, Math.max(MAP.minW, measure(name, 15, 600) + 76)));
|
|
1016
|
+
}
|
|
1017
|
+
function mapLabelSize(text, sub) {
|
|
1018
|
+
const w = Math.max(measure(text, LABEL.size, 500), sub ? measure(sub, 11, 400) : 0) + 18;
|
|
1019
|
+
return { w: Math.ceil(w), h: sub ? LABEL.subH : LABEL.h };
|
|
1020
|
+
}
|
|
1021
|
+
async function layoutMap(doc, view, options = {}) {
|
|
1022
|
+
const idx = indexOf(doc);
|
|
1023
|
+
const extra = new Map((options.extraEntities ?? []).map((e) => [e.id, e]));
|
|
1024
|
+
const entity = (id) => idx.entities.get(id) ?? extra.get(id);
|
|
1025
|
+
const through = new Set(view.passThrough);
|
|
1026
|
+
const ids = [...view.entities.filter((id) => idx.entities.has(id) && !through.has(id)), ...[...extra.keys()].filter((id) => !view.entities.includes(id))];
|
|
1027
|
+
const shown = new Set(ids);
|
|
1028
|
+
const listed = view.relationships ? new Set(view.relationships) : void 0;
|
|
1029
|
+
const all = [...doc.model.relationships, ...options.extraRelationships ?? []];
|
|
1030
|
+
const candidate = (r) => !listed || listed.has(r.id) || (options.extraRelationships ?? []).includes(r);
|
|
1031
|
+
const lines = all.filter((r) => candidate(r) && shown.has(r.source) && shown.has(r.target)).map((r) => ({ id: r.id, relationships: [r.id], source: r.source, target: r.target, text: r.label }));
|
|
1032
|
+
for (const hop of through) {
|
|
1033
|
+
const ins = all.filter((r) => candidate(r) && r.target === hop && shown.has(r.source));
|
|
1034
|
+
const outs = all.filter((r) => candidate(r) && r.source === hop && shown.has(r.target));
|
|
1035
|
+
for (const a of ins)
|
|
1036
|
+
for (const b of outs) lines.push({ id: a.id, relationships: [a.id, b.id], source: a.source, target: b.target, via: hop, text: a.label });
|
|
1037
|
+
}
|
|
1038
|
+
const shownParent = (id) => {
|
|
1039
|
+
let p = entity(id)?.parent;
|
|
1040
|
+
for (let guard = 0; p && !shown.has(p) && guard < 32; guard++) p = entity(p)?.parent;
|
|
1041
|
+
return p && shown.has(p) && p !== id ? p : void 0;
|
|
1042
|
+
};
|
|
1043
|
+
const children = /* @__PURE__ */ new Map();
|
|
1044
|
+
for (const id of ids) {
|
|
1045
|
+
const p = shownParent(id);
|
|
1046
|
+
if (p) children.set(p, [...children.get(p) ?? [], id]);
|
|
1047
|
+
}
|
|
1048
|
+
const closed = new Set((options.collapsed ?? view.collapsed).filter((id) => children.has(id)));
|
|
1049
|
+
const ancestors = (id) => {
|
|
1050
|
+
const out2 = [];
|
|
1051
|
+
for (let p = shownParent(id); p && out2.length < 32 && !out2.includes(p); p = shownParent(p)) out2.push(p);
|
|
1052
|
+
return out2;
|
|
1053
|
+
};
|
|
1054
|
+
const rep = (id) => [id, ...ancestors(id)].filter((a, i) => i === 0 || closed.has(a)).pop();
|
|
1055
|
+
const descendants = (id) => (children.get(id) ?? []).flatMap((c) => [c, ...descendants(c)]);
|
|
1056
|
+
const visible = ids.filter((id) => rep(id) === id);
|
|
1057
|
+
const openGroup = (id) => children.has(id) && !closed.has(id);
|
|
1058
|
+
const merged = /* @__PURE__ */ new Map();
|
|
1059
|
+
for (const l of lines) {
|
|
1060
|
+
const source = rep(l.source);
|
|
1061
|
+
const target = rep(l.target);
|
|
1062
|
+
if (source === target || ancestors(source).includes(target) || ancestors(target).includes(source)) continue;
|
|
1063
|
+
const key = `${source}>${target}`;
|
|
1064
|
+
const m = merged.get(key);
|
|
1065
|
+
if (m) {
|
|
1066
|
+
m.relationships.push(...l.relationships);
|
|
1067
|
+
m.texts.push(l.text);
|
|
1068
|
+
} else merged.set(key, { ...l, source, target, relationships: [...l.relationships], texts: [l.text] });
|
|
1069
|
+
}
|
|
1070
|
+
lines.length = 0;
|
|
1071
|
+
for (const m of merged.values()) {
|
|
1072
|
+
const texts = [...new Set(m.texts)];
|
|
1073
|
+
const joined = texts.join(", ");
|
|
1074
|
+
const text = texts.length === 1 ? texts[0] : joined.length <= 40 ? joined : `${texts[0]} +${texts.length - 1} more`;
|
|
1075
|
+
lines.push({ id: m.id, relationships: m.relationships, source: m.source, target: m.target, via: texts.length > 1 ? void 0 : m.via, text });
|
|
1076
|
+
}
|
|
1077
|
+
const elkEnd = (id) => openGroup(id) ? elkEnd(children.get(id).find((c) => rep(c) === c) ?? id) : id;
|
|
1078
|
+
const elkNode = (id) => {
|
|
1079
|
+
const name = entity(id)?.name ?? id;
|
|
1080
|
+
if (!openGroup(id)) return { id, width: mapNodeWidth(name), height: MAP.h, ...view.positions[id] };
|
|
1081
|
+
const min = Math.ceil(measure(name, 14, 600) + measure(entity(id)?.technology ?? "", 12, 400) + GROUP.pad * 2 + 40);
|
|
1082
|
+
return {
|
|
1083
|
+
id,
|
|
1084
|
+
layoutOptions: {
|
|
1085
|
+
"elk.padding": `[top=${GROUP.head},left=${GROUP.pad},bottom=${GROUP.pad},right=${GROUP.pad}]`,
|
|
1086
|
+
"elk.nodeSize.constraints": "MINIMUM_SIZE",
|
|
1087
|
+
"elk.nodeSize.minimum": `(${min}, ${MAP.h})`
|
|
1088
|
+
},
|
|
1089
|
+
children: children.get(id).filter((c) => rep(c) === c).map(elkNode)
|
|
1090
|
+
};
|
|
1091
|
+
};
|
|
1092
|
+
const down = view.layout.direction === "down";
|
|
1093
|
+
const out = await runElk({
|
|
1094
|
+
id: "root",
|
|
1095
|
+
layoutOptions: {
|
|
1096
|
+
...BASE_OPTIONS,
|
|
1097
|
+
"elk.direction": down ? "DOWN" : "RIGHT",
|
|
1098
|
+
"elk.spacing.nodeNode": String(down ? 56 : MAP.gapY),
|
|
1099
|
+
"elk.layered.spacing.nodeNodeBetweenLayers": String(down ? 84 : MAP.gapX),
|
|
1100
|
+
"elk.layered.nodePlacement.strategy": "BRANDES_KOEPF",
|
|
1101
|
+
"elk.layered.nodePlacement.bk.fixedAlignment": "BALANCED",
|
|
1102
|
+
"elk.padding": "[top=48,left=48,bottom=48,right=48]",
|
|
1103
|
+
...children.size ? { "elk.hierarchyHandling": "INCLUDE_CHILDREN" } : {}
|
|
1104
|
+
},
|
|
1105
|
+
children: visible.filter((id) => !shownParent(id)).map(elkNode),
|
|
1106
|
+
edges: lines.flatMap((l, i) => {
|
|
1107
|
+
const [a, b] = [elkEnd(l.source), elkEnd(l.target)];
|
|
1108
|
+
return a === b ? [] : [{ id: `e${i}`, sources: [a], targets: [b] }];
|
|
1109
|
+
})
|
|
1110
|
+
});
|
|
1111
|
+
const box = (id) => {
|
|
1112
|
+
const n = out.nodes.get(id);
|
|
1113
|
+
return { x: n.x, y: n.y, w: n.w, h: n.h };
|
|
1114
|
+
};
|
|
1115
|
+
const nodes = visible.filter((id) => !openGroup(id)).map((id) => ({ ...box(id), entityId: id, ...closed.has(id) ? { members: descendants(id) } : {} }));
|
|
1116
|
+
const groups = visible.filter(openGroup).map((id) => ({ ...box(id), entityId: id, members: descendants(id), depth: ancestors(id).length })).sort((a, b) => a.depth - b.depth);
|
|
1117
|
+
const byId = new Map([...nodes.map((n) => [n.entityId, n]), ...groups.map((g) => [g.entityId, g])]);
|
|
1118
|
+
const sideOf = (l) => {
|
|
1119
|
+
const a = byId.get(l.source);
|
|
1120
|
+
const b = byId.get(l.target);
|
|
1121
|
+
const gapX = Math.max(b.x - (a.x + a.w), a.x - (b.x + b.w));
|
|
1122
|
+
const gapY = Math.max(b.y - (a.y + a.h), a.y - (b.y + b.h));
|
|
1123
|
+
if (gapX >= gapY && gapX > 0) return b.x > a.x ? { out: "right", in: "left" } : { out: "left", in: "right" };
|
|
1124
|
+
return b.y > a.y ? { out: "bottom", in: "top" } : { out: "top", in: "bottom" };
|
|
1125
|
+
};
|
|
1126
|
+
const ports = /* @__PURE__ */ new Map();
|
|
1127
|
+
const plan = lines.map((l, i) => {
|
|
1128
|
+
const s = sideOf(l);
|
|
1129
|
+
const k1 = `${l.source}:${s.out}`;
|
|
1130
|
+
const k2 = `${l.target}:${s.in}`;
|
|
1131
|
+
ports.set(k1, [...ports.get(k1) ?? [], i]);
|
|
1132
|
+
ports.set(k2, [...ports.get(k2) ?? [], i]);
|
|
1133
|
+
return { l, s, k1, k2, i };
|
|
1134
|
+
});
|
|
1135
|
+
const center2 = (id) => {
|
|
1136
|
+
const n = byId.get(id);
|
|
1137
|
+
return { x: n.x + n.w / 2, y: n.y + n.h / 2 };
|
|
1138
|
+
};
|
|
1139
|
+
const port = (nodeId, side, key, i) => {
|
|
1140
|
+
const n = byId.get(nodeId);
|
|
1141
|
+
const vertical = side === "left" || side === "right";
|
|
1142
|
+
const other = (j) => center2(lines[j].source === nodeId ? lines[j].target : lines[j].source);
|
|
1143
|
+
const list = (ports.get(key) ?? []).slice().sort((p, q) => vertical ? other(p).y - other(q).y : other(p).x - other(q).x);
|
|
1144
|
+
const k = list.indexOf(i);
|
|
1145
|
+
const span = Math.min((vertical ? n.h : n.w) * 0.55, (list.length - 1) * 14);
|
|
1146
|
+
const offset = list.length > 1 ? -span / 2 + k / (list.length - 1) * span : 0;
|
|
1147
|
+
if (side === "right") return { x: n.x + n.w, y: n.y + n.h / 2 + offset };
|
|
1148
|
+
if (side === "left") return { x: n.x, y: n.y + n.h / 2 + offset };
|
|
1149
|
+
if (side === "bottom") return { x: n.x + n.w / 2 + offset, y: n.y + n.h };
|
|
1150
|
+
return { x: n.x + n.w / 2 + offset, y: n.y };
|
|
1151
|
+
};
|
|
1152
|
+
const taken = [...nodes.map((n) => ({ x: n.x - 6, y: n.y - 6, w: n.w + 12, h: n.h + 12 })), ...groups.map((g) => ({ x: g.x, y: g.y, w: g.w, h: GROUP.head - 8 }))];
|
|
1153
|
+
const dirs = { right: [1, 0], left: [-1, 0], bottom: [0, 1], top: [0, -1] };
|
|
1154
|
+
const curveOf = ({ l, s, k1, k2, i }) => {
|
|
1155
|
+
const p0 = port(l.source, s.out, k1, i);
|
|
1156
|
+
const p3 = port(l.target, s.in, k2, i);
|
|
1157
|
+
const horizontal = s.out === "left" || s.out === "right";
|
|
1158
|
+
const pull = horizontal ? Math.max(48, Math.abs(p3.x - p0.x) * 0.45) : Math.max(36, Math.abs(p3.y - p0.y) * 0.45);
|
|
1159
|
+
const [ox, oy] = dirs[s.out];
|
|
1160
|
+
const [ix, iy] = dirs[s.in];
|
|
1161
|
+
return [p0, { x: p0.x + ox * pull, y: p0.y + oy * pull }, { x: p3.x + ix * pull, y: p3.y + iy * pull }, p3];
|
|
1162
|
+
};
|
|
1163
|
+
const curves = plan.map(curveOf);
|
|
1164
|
+
const edges = plan.map(({ l, i }) => {
|
|
1165
|
+
const curve = curves[i];
|
|
1166
|
+
const sub = l.via ? `through ${entity(l.via)?.name ?? l.via}` : void 0;
|
|
1167
|
+
const size = mapLabelSize(l.text, sub);
|
|
1168
|
+
const others = curves.filter((_, j) => j !== i);
|
|
1169
|
+
const cost = (r) => {
|
|
1170
|
+
let c = 0;
|
|
1171
|
+
for (const t of taken) c += 8 * Math.max(0, Math.min(r.x + r.w, t.x + t.w) - Math.max(r.x, t.x)) * Math.max(0, Math.min(r.y + r.h, t.y + t.h) - Math.max(r.y, t.y));
|
|
1172
|
+
for (const o of others) for (let k = 1; k < 24; k++) {
|
|
1173
|
+
const p = bezier(o, k / 24);
|
|
1174
|
+
if (p.x > r.x && p.x < r.x + r.w && p.y > r.y && p.y < r.y + r.h) c += 600;
|
|
1175
|
+
}
|
|
1176
|
+
return c;
|
|
1177
|
+
};
|
|
1178
|
+
let box2;
|
|
1179
|
+
let leader;
|
|
1180
|
+
let best = Infinity;
|
|
1181
|
+
const ts = [0.5, 0.44, 0.56, 0.38, 0.62, 0.32, 0.68, 0.26, 0.74];
|
|
1182
|
+
for (const off of [0, 1, -1, 2, -2, 3, -3, 4, -4]) {
|
|
1183
|
+
for (const t of ts) {
|
|
1184
|
+
const p = bezier(curve, t);
|
|
1185
|
+
const d = tangent(curve, t);
|
|
1186
|
+
const len = Math.hypot(d.x, d.y) || 1;
|
|
1187
|
+
const shift = off * (Math.abs(d.x / len) > 0.7 ? size.h / 2 + 12 : size.w / 2 + 12);
|
|
1188
|
+
const n = { x: -d.y / len, y: d.x / len };
|
|
1189
|
+
const candidate2 = { x: p.x + n.x * shift - size.w / 2, y: p.y + n.y * shift - size.h / 2, w: size.w, h: size.h };
|
|
1190
|
+
const c = cost(candidate2) + Math.abs(t - 0.5) * 40 + Math.abs(off) * 250;
|
|
1191
|
+
if (c < best) {
|
|
1192
|
+
best = c;
|
|
1193
|
+
box2 = candidate2;
|
|
1194
|
+
leader = off ? p : void 0;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
if (best <= 10) break;
|
|
1198
|
+
}
|
|
1199
|
+
box2 ??= { x: 0, y: 0, ...size };
|
|
1200
|
+
taken.push(box2);
|
|
1201
|
+
return { id: l.id, relationships: l.relationships, source: l.source, target: l.target, via: l.via, curve, label: { ...box2, text: l.text, sub }, leader };
|
|
1202
|
+
});
|
|
1203
|
+
const anchors = {};
|
|
1204
|
+
for (const n of nodes) anchors[refKey({ kind: "entity", id: n.entityId })] = n;
|
|
1205
|
+
for (const g of groups) anchors[refKey({ kind: "entity", id: g.entityId })] = g;
|
|
1206
|
+
for (const id of ids) if (rep(id) !== id) anchors[refKey({ kind: "entity", id })] = byId.get(rep(id));
|
|
1207
|
+
for (const e of edges) {
|
|
1208
|
+
const xs = e.curve.map((p) => p.x);
|
|
1209
|
+
const ys = e.curve.map((p) => p.y);
|
|
1210
|
+
const lx = [...xs, e.label.x, e.label.x + e.label.w];
|
|
1211
|
+
const ly = [...ys, e.label.y, e.label.y + e.label.h];
|
|
1212
|
+
const r = { x: Math.min(...lx), y: Math.min(...ly), w: Math.max(...lx) - Math.min(...lx), h: Math.max(...ly) - Math.min(...ly) };
|
|
1213
|
+
for (const rel of e.relationships) anchors[refKey({ kind: "relationship", id: rel })] ??= r;
|
|
1214
|
+
}
|
|
1215
|
+
return { width: out.width, height: out.height, nodes, groups, edges, anchors };
|
|
1216
|
+
}
|
|
1217
|
+
function bezier([p0, p1, p2, p3], t) {
|
|
1218
|
+
const u = 1 - t;
|
|
1219
|
+
return {
|
|
1220
|
+
x: u * u * u * p0.x + 3 * u * u * t * p1.x + 3 * u * t * t * p2.x + t * t * t * p3.x,
|
|
1221
|
+
y: u * u * u * p0.y + 3 * u * u * t * p1.y + 3 * u * t * t * p2.y + t * t * t * p3.y
|
|
1222
|
+
};
|
|
1223
|
+
}
|
|
1224
|
+
function tangent([p0, p1, p2, p3], t) {
|
|
1225
|
+
const u = 1 - t;
|
|
1226
|
+
return {
|
|
1227
|
+
x: 3 * u * u * (p1.x - p0.x) + 6 * u * t * (p2.x - p1.x) + 3 * t * t * (p3.x - p2.x),
|
|
1228
|
+
y: 3 * u * u * (p1.y - p0.y) + 6 * u * t * (p2.y - p1.y) + 3 * t * t * (p3.y - p2.y)
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
// src/scenes.ts
|
|
1233
|
+
var extensionLayouts = /* @__PURE__ */ new Map();
|
|
1234
|
+
function registerExtensionLayout(viewType, layout) {
|
|
1235
|
+
extensionLayouts.set(viewType, layout);
|
|
1236
|
+
}
|
|
1237
|
+
async function layoutView(doc, view, options = {}) {
|
|
1238
|
+
switch (view.type) {
|
|
1239
|
+
case "architecture":
|
|
1240
|
+
return layoutArchitecture(doc, view, options);
|
|
1241
|
+
case "map":
|
|
1242
|
+
return layoutMap(doc, view, options);
|
|
1243
|
+
case "sequence":
|
|
1244
|
+
return layoutSequence(doc, view, options);
|
|
1245
|
+
case "dataflow":
|
|
1246
|
+
return layoutDataFlow(doc, view, options);
|
|
1247
|
+
case "activity":
|
|
1248
|
+
return layoutActivity(doc, view);
|
|
1249
|
+
case "erd":
|
|
1250
|
+
return layoutErd(doc, view, options);
|
|
1251
|
+
case "state":
|
|
1252
|
+
return layoutState(doc, view, options);
|
|
1253
|
+
case "usecase":
|
|
1254
|
+
return layoutUseCase(doc, view);
|
|
1255
|
+
case "dependency":
|
|
1256
|
+
return layoutDependency(doc, view);
|
|
1257
|
+
case "extension": {
|
|
1258
|
+
const layout = extensionLayouts.get(view.extension);
|
|
1259
|
+
if (!layout) throw new Error(`No layout registered for "${view.extension}"`);
|
|
1260
|
+
return layout(doc, view, options);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
export { ARCH_PADDING, ARCH_SIZES, BASE_OPTIONS, ERD, GROUP, LABEL, MAP, SEQ, bezier, center, clip, endAngle, estimateText, labelPoint, layoutActivity, layoutArchitecture, layoutDataFlow, layoutDependency, layoutErd, layoutMap, layoutSequence, layoutState, layoutUseCase, layoutView, mapLabelSize, mapNodeWidth, measure, pointAlong, registerExtensionLayout, roundedPath, runElk, setFonts, setTextMeasurer, startAngle, transitionLabelSize, trimEnd, trimStart, union, wrap };
|
|
1266
|
+
//# sourceMappingURL=index.js.map
|
|
1267
|
+
//# sourceMappingURL=index.js.map
|