truemap-cli 0.3.0 → 0.5.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/README.md +31 -27
- package/kit/fonts/OFL-commit-mono.txt +90 -0
- package/kit/fonts/OFL-instrument-sans.txt +93 -0
- package/kit/fonts/commit-mono-latin-400-normal.woff2 +0 -0
- package/kit/fonts/instrument-sans-latin-400-normal.woff2 +0 -0
- package/kit/fonts/instrument-sans-latin-500-normal.woff2 +0 -0
- package/kit/fonts/instrument-sans-latin-600-normal.woff2 +0 -0
- package/kit/truemap.css +179 -62
- package/package.json +1 -1
- package/skills/truemap/SKILL.md +9 -5
- package/src/changes.js +155 -0
- package/src/check.js +98 -7
- package/src/cli.js +178 -13
- package/src/common.js +3 -0
- package/src/export.js +2 -2
- package/src/guide.md +128 -115
- package/src/plan.js +185 -0
- package/src/server.js +188 -11
- package/src/snippet.js +63 -0
- package/src/ui/diagram.js +598 -0
- package/src/ui/sdk.js +271 -39
- package/src/ui/wires.js +114 -22
- package/src/ui/workspace.css +337 -157
- package/src/ui/workspace.html +27 -14
- package/src/ui/workspace.js +503 -57
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
// truemap diagrams: the agent declares groups, nodes and edges; this lays them out (layered, groups as bands)
|
|
2
|
+
// and draws them. Used by the page SDK and by exported pages. Plain script, no dependencies.
|
|
3
|
+
//
|
|
4
|
+
// <figure class="tm-diagram" data-direction="LR">
|
|
5
|
+
// <section class="tm-group" id="core" data-label="Hub core">
|
|
6
|
+
// <div class="tm-node" id="runs" data-name="Runs" data-tag="routing" data-src="server/routing.ts#Runs"
|
|
7
|
+
// data-to="acp:execute store:writes">
|
|
8
|
+
// <div class="tm-detail">…shown in the inspector…</div>
|
|
9
|
+
// </div>
|
|
10
|
+
// </section>
|
|
11
|
+
// <ol class="tm-flows"><li data-path="web api runs acp" data-note="…">A message reaches an agent</li></ol>
|
|
12
|
+
// </figure>
|
|
13
|
+
(() => {
|
|
14
|
+
if (window.truemapDiagram) return;
|
|
15
|
+
const SVGNS = 'http://www.w3.org/2000/svg';
|
|
16
|
+
const viaOf = (s) => s.replace(/\\_/g, '\u0001').replace(/_/g, ' ').replace(/\u0001/g, '_');
|
|
17
|
+
const textOf = (n) => [...n.childNodes].filter((c) => c.nodeType === 3).map((c) => c.textContent).join(' ').replace(/\s+/g, ' ').trim();
|
|
18
|
+
|
|
19
|
+
function nameOf(n) {
|
|
20
|
+
return n.getAttribute('data-name') || n.querySelector(':scope > b, :scope > strong')?.textContent.trim() || textOf(n) || n.id;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// A node element becomes drawable: its face is the name and an optional tag; the detail stays for the inspector.
|
|
24
|
+
function adopt(m, n, groupId) {
|
|
25
|
+
n.dataset.group = groupId || '';
|
|
26
|
+
const name = nameOf(n);
|
|
27
|
+
const detail = n.querySelector(':scope > .tm-detail');
|
|
28
|
+
for (const c of [...n.childNodes]) if (c !== detail) c.remove();
|
|
29
|
+
const face = document.createElement('span'); face.className = 'tm-dg-face';
|
|
30
|
+
face.innerHTML = '<span class="tm-dg-name"></span>';
|
|
31
|
+
face.firstChild.textContent = name;
|
|
32
|
+
if (n.dataset.tag) { const t = document.createElement('span'); t.className = 'tm-dg-tag'; t.textContent = n.dataset.tag; face.appendChild(t); }
|
|
33
|
+
n.insertBefore(face, n.firstChild);
|
|
34
|
+
n.dataset.name = name;
|
|
35
|
+
m.nodes.push(n); m.stage.appendChild(n);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Turn the declared markup into the drawable structure once.
|
|
39
|
+
function prepare(fig) {
|
|
40
|
+
if (fig.__tm) return fig.__tm;
|
|
41
|
+
const stage = document.createElement('div'); stage.className = 'tm-dg-stage';
|
|
42
|
+
const svg = document.createElementNS(SVGNS, 'svg'); svg.setAttribute('class', 'tm-dg-edges'); svg.setAttribute('aria-hidden', 'true');
|
|
43
|
+
stage.appendChild(svg);
|
|
44
|
+
const groups = [];
|
|
45
|
+
const nodes = [];
|
|
46
|
+
const m0 = { stage, nodes };
|
|
47
|
+
const take = (n, group) => adopt(m0, n, group?.id);
|
|
48
|
+
for (const sec of [...fig.querySelectorAll(':scope > .tm-group')]) {
|
|
49
|
+
if (!sec.id) sec.id = `g-${groups.length}`;
|
|
50
|
+
const box = document.createElement('div'); box.className = 'tm-dg-group'; box.dataset.group = sec.id;
|
|
51
|
+
const label = document.createElement('span'); label.className = 'tm-dg-glabel'; label.textContent = sec.getAttribute('data-label') || '';
|
|
52
|
+
box.appendChild(label);
|
|
53
|
+
stage.insertBefore(box, svg.nextSibling);
|
|
54
|
+
groups.push({ id: sec.id, el: box });
|
|
55
|
+
for (const n of [...sec.querySelectorAll(':scope > .tm-node')]) take(n, sec);
|
|
56
|
+
sec.remove();
|
|
57
|
+
}
|
|
58
|
+
for (const n of [...fig.querySelectorAll(':scope > .tm-node')]) take(n, null);
|
|
59
|
+
// flows become tabs above the drawing
|
|
60
|
+
const flowsList = fig.querySelector(':scope > .tm-flows');
|
|
61
|
+
let tabs = null;
|
|
62
|
+
if (flowsList) {
|
|
63
|
+
tabs = document.createElement('div'); tabs.className = 'tm-dg-tabs'; tabs.setAttribute('role', 'tablist');
|
|
64
|
+
for (const li of flowsList.querySelectorAll(':scope > li')) {
|
|
65
|
+
const b = document.createElement('button'); b.type = 'button'; b.className = 'tm-dg-tab'; b.setAttribute('role', 'tab');
|
|
66
|
+
b.setAttribute('aria-selected', 'false');
|
|
67
|
+
b.setAttribute('data-path', li.getAttribute('data-path') || '');
|
|
68
|
+
b.setAttribute('data-note', li.getAttribute('data-note') || '');
|
|
69
|
+
b.textContent = li.getAttribute('data-title') || li.textContent.trim();
|
|
70
|
+
tabs.appendChild(b);
|
|
71
|
+
}
|
|
72
|
+
flowsList.remove();
|
|
73
|
+
}
|
|
74
|
+
if (tabs) fig.appendChild(tabs);
|
|
75
|
+
fig.appendChild(stage);
|
|
76
|
+
fig.__tm = Object.assign(m0, { svg, groups, tabs });
|
|
77
|
+
return fig.__tm;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Orthogonal polyline with rounded corners.
|
|
81
|
+
function roundPath(pts, r = 7) {
|
|
82
|
+
let d = `M${pts[0].x},${pts[0].y}`;
|
|
83
|
+
for (let i = 1; i < pts.length - 1; i++) {
|
|
84
|
+
const a = pts[i - 1]; const b = pts[i]; const c = pts[i + 1];
|
|
85
|
+
const l1 = Math.hypot(b.x - a.x, b.y - a.y); const l2 = Math.hypot(c.x - b.x, c.y - b.y);
|
|
86
|
+
const k = Math.min(r, l1 / 2, l2 / 2);
|
|
87
|
+
if (k < 0.5) { d += ` L${b.x},${b.y}`; continue; }
|
|
88
|
+
const p1 = { x: b.x - ((b.x - a.x) / l1) * k, y: b.y - ((b.y - a.y) / l1) * k };
|
|
89
|
+
const p2 = { x: b.x + ((c.x - b.x) / l2) * k, y: b.y + ((c.y - b.y) / l2) * k };
|
|
90
|
+
d += ` L${p1.x},${p1.y} Q${b.x},${b.y} ${p2.x},${p2.y}`;
|
|
91
|
+
}
|
|
92
|
+
const z = pts[pts.length - 1];
|
|
93
|
+
return `${d} L${z.x},${z.y}`;
|
|
94
|
+
}
|
|
95
|
+
const simplify = (pts) => pts.filter((p, i) => {
|
|
96
|
+
if (i === 0 || i === pts.length - 1) return true;
|
|
97
|
+
const a = pts[i - 1]; const c = pts[i + 1];
|
|
98
|
+
if (Math.abs(a.x - p.x) < 0.5 && Math.abs(a.y - p.y) < 0.5) return false;
|
|
99
|
+
return !((Math.abs(a.x - p.x) < 0.5 && Math.abs(p.x - c.x) < 0.5) || (Math.abs(a.y - p.y) < 0.5 && Math.abs(p.y - c.y) < 0.5));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// The layered layout, computed top to bottom ("rows"); left to right is the same picture turned.
|
|
103
|
+
// - Groups are bands of rows in the author's order: a group never shares a row with another, so groups
|
|
104
|
+
// never overlap or interleave.
|
|
105
|
+
// - Inside a group a box sits one row below the boxes that call it (longest path).
|
|
106
|
+
// - A connection that skips rows reserves a narrow slot in each row it crosses.
|
|
107
|
+
// - Order within rows: barycenter sweeps, keeping the order with the fewest crossings.
|
|
108
|
+
// - Positions: as close to the connected boxes as the ordering and spacing allow (block merging).
|
|
109
|
+
// - Connections run only through the gaps between rows and through their reserved slots, so they
|
|
110
|
+
// never cross a box.
|
|
111
|
+
function layered(m, edges, dir, labelW, room = 860) {
|
|
112
|
+
const lr = dir === 'LR';
|
|
113
|
+
const nodes = m.nodes;
|
|
114
|
+
const idx = new Map(nodes.map((n, i) => [n.id, i]));
|
|
115
|
+
const S = nodes.map((n) => (lr ? { w: n.offsetHeight, h: n.offsetWidth } : { w: n.offsetWidth, h: n.offsetHeight }));
|
|
116
|
+
const NODESEP = lr ? 18 : 24; const SLOT = 12;
|
|
117
|
+
// Top to bottom, groups are swimlanes with their label in a left gutter; left to right, columns with
|
|
118
|
+
// the label above the boxes.
|
|
119
|
+
const gutter = Math.max(72, ...m.groups.map((gr) => (gr.el.firstChild?.offsetWidth || 0) + 30));
|
|
120
|
+
const GAP = lr ? Math.max(60, Math.min(170, labelW + 48)) : (labelW ? 58 : 40);
|
|
121
|
+
const HEAD = 0; const BANDPAD = lr ? 12 : 12; const GAPB = lr ? 14 : (labelW ? 10 : 8);
|
|
122
|
+
const MX = lr ? 34 : gutter; const MY = lr ? 6 : 4;
|
|
123
|
+
const gOf = nodes.map((n) => { const k = m.groups.findIndex((gr) => gr.id === n.dataset.group); return k < 0 ? m.groups.length : k; });
|
|
124
|
+
const E = edges.map((e) => ({ e, a: idx.get(e.back ? e.to : e.from), b: idx.get(e.back ? e.from : e.to) }));
|
|
125
|
+
// 1. rows
|
|
126
|
+
const rank = new Array(nodes.length).fill(0); const localRank = new Array(nodes.length).fill(0);
|
|
127
|
+
const bands = [];
|
|
128
|
+
let base = 0;
|
|
129
|
+
for (const g of [...new Set(gOf)].sort((x, y) => x - y)) {
|
|
130
|
+
const members = nodes.map((_, i) => i).filter((i) => gOf[i] === g);
|
|
131
|
+
const local = new Map(members.map((i) => [i, 0]));
|
|
132
|
+
for (const i of members) for (const x of E) if (x.a === i && gOf[x.b] === g) local.set(x.b, Math.max(local.get(x.b), local.get(i) + 1));
|
|
133
|
+
let top = base;
|
|
134
|
+
const intra = E.filter((x) => gOf[x.a] === g && gOf[x.b] === g);
|
|
135
|
+
// width of the row in its first order: only neighbours that call each other need room for a label
|
|
136
|
+
const inOrder = [...members].sort((u, v) => local.get(u) - local.get(v) || u - v);
|
|
137
|
+
let rowW = inOrder.reduce((n, i) => n + S[i].w, 0) + (members.length - 1) * NODESEP;
|
|
138
|
+
for (let k = 1; k < inOrder.length; k++) {
|
|
139
|
+
const pair = intra.filter((x) => (x.a === inOrder[k - 1] && x.b === inOrder[k]) || (x.b === inOrder[k - 1] && x.a === inOrder[k]));
|
|
140
|
+
if (pair.length) rowW += Math.max(...pair.map((x) => (x.e.lw ? x.e.lw + 30 : 28))) - NODESEP;
|
|
141
|
+
}
|
|
142
|
+
const oneRow = !lr && rowW <= (room - gutter - 24) / 0.82;
|
|
143
|
+
for (const i of members) { rank[i] = oneRow ? base : base + local.get(i); localRank[i] = local.get(i); top = Math.max(top, rank[i]); }
|
|
144
|
+
bands.push({ g, from: base, to: top });
|
|
145
|
+
base = top + 1;
|
|
146
|
+
}
|
|
147
|
+
const R = base;
|
|
148
|
+
const rows = Array.from({ length: R }, () => []);
|
|
149
|
+
const item = nodes.map((_, i) => { const it = { node: i, w: S[i].w, h: S[i].h, order: i }; rows[rank[i]].push(it); return it; });
|
|
150
|
+
for (const x of E) {
|
|
151
|
+
x.chain = [item[x.a]];
|
|
152
|
+
for (let r = rank[x.a] + 1; r < rank[x.b]; r++) { const d = { dummy: x, w: SLOT, h: 0, order: x.a + 0.5, row: r }; rows[r].push(d); x.chain.push(d); }
|
|
153
|
+
x.chain.push(item[x.b]);
|
|
154
|
+
}
|
|
155
|
+
for (let r = 0; r < R; r++) for (const it of rows[r]) it.row = r;
|
|
156
|
+
const up = new Map(); const down = new Map();
|
|
157
|
+
const link = (map, k, v) => { if (!map.has(k)) map.set(k, []); map.get(k).push(v); };
|
|
158
|
+
for (const x of E) x.same = rank[x.a] === rank[x.b];
|
|
159
|
+
for (const x of E) if (!x.same) for (let k = 0; k + 1 < x.chain.length; k++) { link(down, x.chain[k], x.chain[k + 1]); link(up, x.chain[k + 1], x.chain[k]); }
|
|
160
|
+
const sameEdges = E.filter((x) => x.same);
|
|
161
|
+
// inside a row, a call goes left to right: the caller stays left of the callee
|
|
162
|
+
const keepCalls = (row) => {
|
|
163
|
+
for (let pass = 0; pass < 6; pass++) {
|
|
164
|
+
let moved = false;
|
|
165
|
+
for (const x of sameEdges) {
|
|
166
|
+
const a = item[x.a]; const b = item[x.b];
|
|
167
|
+
if (a.row !== row[0]?.row) continue;
|
|
168
|
+
const ia = row.indexOf(a); const ib = row.indexOf(b);
|
|
169
|
+
if (ia > ib) { row.splice(ia, 1); row.splice(ib, 0, a); moved = true; }
|
|
170
|
+
}
|
|
171
|
+
if (!moved) break;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
// 2. order within rows
|
|
175
|
+
const number = () => { for (const row of rows) row.forEach((it, k) => { it.k = k; }); };
|
|
176
|
+
for (const row of rows) row.sort((p, q) => (p.dummy ? 0 : localRank[p.node]) - (q.dummy ? 0 : localRank[q.node]) || p.order - q.order);
|
|
177
|
+
number();
|
|
178
|
+
const crossings = () => {
|
|
179
|
+
let c = 0;
|
|
180
|
+
for (let r = 0; r + 1 < R; r++) {
|
|
181
|
+
const segs = [];
|
|
182
|
+
for (const it of rows[r]) for (const q of down.get(it) || []) segs.push([it.k, q.k]);
|
|
183
|
+
for (let s = 0; s < segs.length; s++) for (let t = s + 1; t < segs.length; t++) if ((segs[s][0] - segs[t][0]) * (segs[s][1] - segs[t][1]) < 0) c++;
|
|
184
|
+
}
|
|
185
|
+
return c;
|
|
186
|
+
};
|
|
187
|
+
let best = rows.map((row) => row.slice()); let bestC = crossings();
|
|
188
|
+
for (let iter = 0; iter < 16 && bestC > 0; iter++) {
|
|
189
|
+
const downward = iter % 2 === 0;
|
|
190
|
+
const seq = downward ? [...rows.keys()].slice(1) : [...rows.keys()].reverse().slice(1);
|
|
191
|
+
for (const r of seq) {
|
|
192
|
+
const nb = downward ? up : down;
|
|
193
|
+
for (const it of rows[r]) { const ns = nb.get(it) || []; it.bc = ns.length ? ns.reduce((s, q) => s + q.k, 0) / ns.length : it.k; }
|
|
194
|
+
rows[r].sort((p, q) => p.bc - q.bc || p.k - q.k);
|
|
195
|
+
keepCalls(rows[r]);
|
|
196
|
+
rows[r].forEach((it, k) => { it.k = k; });
|
|
197
|
+
}
|
|
198
|
+
const c = crossings();
|
|
199
|
+
if (c < bestC) { bestC = c; best = rows.map((row) => row.slice()); }
|
|
200
|
+
}
|
|
201
|
+
for (let r = 0; r < R; r++) rows[r] = best[r];
|
|
202
|
+
number();
|
|
203
|
+
// 3. positions across the rows
|
|
204
|
+
const pairGap = new Map();
|
|
205
|
+
for (const x of sameEdges) { const k = [x.a, x.b].sort((u, v) => u - v).join('|'); pairGap.set(k, Math.max(pairGap.get(k) || 0, x.e.lw ? x.e.lw + 30 : 28)); }
|
|
206
|
+
const sep = (p, q) => (p.dummy || q.dummy ? 10 : Math.max(NODESEP, pairGap.get([p.node, q.node].sort((u, v) => u - v).join('|')) || 0));
|
|
207
|
+
function settle(row, want) {
|
|
208
|
+
const blocks = [];
|
|
209
|
+
row.forEach((it, k) => {
|
|
210
|
+
blocks.push({ items: [k], off: [0], sum: want[k], n: 1, pos: want[k] });
|
|
211
|
+
while (blocks.length > 1) {
|
|
212
|
+
const B = blocks[blocks.length - 1]; const A = blocks[blocks.length - 2];
|
|
213
|
+
const la = row[A.items[A.items.length - 1]]; const fb = row[B.items[0]];
|
|
214
|
+
const gap = la.w / 2 + sep(la, fb) + fb.w / 2;
|
|
215
|
+
const aEnd = A.pos + A.off[A.off.length - 1];
|
|
216
|
+
if (aEnd + gap <= B.pos + 0.01) break;
|
|
217
|
+
const b0 = A.off[A.off.length - 1] + gap;
|
|
218
|
+
for (let j = 0; j < B.items.length; j++) { A.items.push(B.items[j]); A.off.push(b0 + B.off[j]); }
|
|
219
|
+
A.sum += B.sum - B.n * b0; A.n += B.n; A.pos = A.sum / A.n;
|
|
220
|
+
blocks.pop();
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
for (const b of blocks) b.items.forEach((k, j) => { row[k].x = b.pos + b.off[j]; });
|
|
224
|
+
}
|
|
225
|
+
for (const row of rows) { let x = 0; row.forEach((it, k) => { if (k) x += sep(row[k - 1], it); it.x = x + it.w / 2; x += it.w; }); const c = x / 2; for (const it of row) it.x -= c; }
|
|
226
|
+
for (let it = 0; it < 10; it++) {
|
|
227
|
+
const seq = it % 2 === 0 ? [...rows.keys()] : [...rows.keys()].reverse();
|
|
228
|
+
for (const r of seq) {
|
|
229
|
+
const want = rows[r].map((p) => {
|
|
230
|
+
const ns = [...(up.get(p) || []), ...(down.get(p) || [])];
|
|
231
|
+
return ns.length ? ns.reduce((s, q) => s + q.x, 0) / ns.length : p.x;
|
|
232
|
+
});
|
|
233
|
+
settle(rows[r], want);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
let minX = Infinity; let maxX = -Infinity;
|
|
237
|
+
for (const row of rows) for (const it of row) { minX = Math.min(minX, it.x - it.w / 2); maxX = Math.max(maxX, it.x + it.w / 2); }
|
|
238
|
+
for (const row of rows) for (const it of row) it.x += MX - minX;
|
|
239
|
+
const W = maxX - minX + MX + 12;
|
|
240
|
+
const rowH = rows.map((row) => Math.max(1, ...row.map((it) => it.h)));
|
|
241
|
+
const bandOf = (r) => bands.find((b) => r >= b.from && r <= b.to);
|
|
242
|
+
const ys = []; const gapAfter = []; let y = MY + (lr ? 0 : BANDPAD);
|
|
243
|
+
const adjacent = (x) => Math.abs(item[x.a].k - item[x.b].k) === 1;
|
|
244
|
+
const needsBelow = new Set(sameEdges.filter((x) => !adjacent(x)).map((x) => rank[x.a]));
|
|
245
|
+
// a gap grows with the lines that turn in it, so their lanes and labels have room
|
|
246
|
+
const turning = new Array(R).fill(0);
|
|
247
|
+
for (const x of E) {
|
|
248
|
+
if (x.same) { if (!adjacent(x)) turning[rank[x.a]] += 1; continue; }
|
|
249
|
+
for (let k = 0; k + 1 < x.chain.length; k++) if (Math.abs((x.chain[k].x ?? 0) - (x.chain[k + 1].x ?? 0)) > 2) turning[x.chain[k].row] += 1;
|
|
250
|
+
}
|
|
251
|
+
const roomFor = (r, base) => Math.min(lr ? 200 : 110, Math.max(base, 18 + turning[r] * 9));
|
|
252
|
+
for (let r = 0; r < R; r++) {
|
|
253
|
+
const b = bandOf(r);
|
|
254
|
+
if (b.from === r) y += HEAD;
|
|
255
|
+
ys[r] = y + rowH[r] / 2; y += rowH[r];
|
|
256
|
+
if (r + 1 < R) { const g = roomFor(r, bandOf(r + 1) !== b ? BANDPAD * 2 + GAPB : GAP); gapAfter[r] = { top: y, bottom: y + g }; y += g; }
|
|
257
|
+
}
|
|
258
|
+
if (needsBelow.has(R - 1)) { const g = roomFor(R - 1, 30); gapAfter[R - 1] = { top: y, bottom: y + g }; y += g; }
|
|
259
|
+
y += BANDPAD + MY;
|
|
260
|
+
const H = y;
|
|
261
|
+
// 4. routes
|
|
262
|
+
const out = new Map(); const inc = new Map();
|
|
263
|
+
for (const x of E) {
|
|
264
|
+
x.label = x.e.label; x.bundle = `${x.a}|${x.e.back ? '<' : '>'}|${x.label}`;
|
|
265
|
+
if (x.same) continue;
|
|
266
|
+
if (!out.has(x.a)) out.set(x.a, []); out.get(x.a).push(x);
|
|
267
|
+
if (!inc.has(x.b)) inc.set(x.b, []); inc.get(x.b).push(x);
|
|
268
|
+
}
|
|
269
|
+
const portX = (it, list, keyOf, otherX) => {
|
|
270
|
+
const keys = [...new Set(list.map(keyOf))];
|
|
271
|
+
keys.sort((p, q) => otherX(p) - otherX(q));
|
|
272
|
+
const step = Math.min(16, (it.w * 0.7) / Math.max(1, keys.length));
|
|
273
|
+
return new Map(keys.map((k, i) => [k, it.x + (i - (keys.length - 1) / 2) * step]));
|
|
274
|
+
};
|
|
275
|
+
for (const [a, list] of out) {
|
|
276
|
+
const it = item[a];
|
|
277
|
+
const ports = portX(it, list, (x) => x.bundle, (k) => { const x = list.find((z) => z.bundle === k); return x.chain[1].x; });
|
|
278
|
+
for (const x of list) x.exit = ports.get(x.bundle);
|
|
279
|
+
}
|
|
280
|
+
for (const [b, list] of inc) {
|
|
281
|
+
const it = item[b];
|
|
282
|
+
const ports = portX(it, list, (x) => x.e.name, (k) => { const x = list.find((z) => z.e.name === k); return x.chain[x.chain.length - 2].x; });
|
|
283
|
+
for (const x of list) x.entry = ports.get(x.e.name);
|
|
284
|
+
}
|
|
285
|
+
// horizontal runs share a gap: give each its own lane (a bundle shares one)
|
|
286
|
+
const lanes = new Map(); // row -> [{key, lo, hi}]
|
|
287
|
+
const hops = [];
|
|
288
|
+
for (const x of E) {
|
|
289
|
+
const pts = [];
|
|
290
|
+
if (x.same) {
|
|
291
|
+
if (!adjacent(x)) { const a = item[x.a]; const b = item[x.b]; const dir = Math.sign(b.x - a.x); x.below = { px: a.x + dir * a.w / 4, qx: b.x - dir * b.w / 4 }; hops.push({ x, k: 0, row: a.row, px: x.below.px, qx: x.below.qx, key: `${x.e.name}:same` }); }
|
|
292
|
+
x.pts = pts; continue;
|
|
293
|
+
}
|
|
294
|
+
for (let k = 0; k + 1 < x.chain.length; k++) {
|
|
295
|
+
const p = x.chain[k]; const q = x.chain[k + 1];
|
|
296
|
+
const px = k === 0 ? x.exit : p.x; const qx = k + 1 === x.chain.length - 1 ? x.entry : q.x;
|
|
297
|
+
hops.push({ x, k, row: p.row, px, qx, key: k === 0 ? `${x.bundle}` : `${x.e.name}:${k}` });
|
|
298
|
+
}
|
|
299
|
+
x.pts = pts;
|
|
300
|
+
}
|
|
301
|
+
for (const h of hops) {
|
|
302
|
+
if (Math.abs(h.px - h.qx) < 1) continue;
|
|
303
|
+
if (!lanes.has(h.row)) lanes.set(h.row, new Map());
|
|
304
|
+
const L = lanes.get(h.row);
|
|
305
|
+
const cur = L.get(h.key);
|
|
306
|
+
if (cur) { cur.lo = Math.min(cur.lo, h.px, h.qx); cur.hi = Math.max(cur.hi, h.px, h.qx); } else L.set(h.key, { lo: Math.min(h.px, h.qx), hi: Math.max(h.px, h.qx) });
|
|
307
|
+
}
|
|
308
|
+
const laneY = new Map();
|
|
309
|
+
for (const [row, L] of lanes) {
|
|
310
|
+
const g = gapAfter[row]; const list = [...L.entries()].sort((p, q) => p[1].lo - q[1].lo);
|
|
311
|
+
const span = g.bottom - g.top; const step = Math.min(11, Math.max(6, (span - 16) / Math.max(1, list.length)));
|
|
312
|
+
list.forEach(([key], i) => laneY.set(`${row}|${key}`, g.top + span / 2 + (i - (list.length - 1) / 2) * step));
|
|
313
|
+
}
|
|
314
|
+
for (const x of E) {
|
|
315
|
+
if (x.same) {
|
|
316
|
+
const a = item[x.a]; const b = item[x.b]; const r = rank[x.a];
|
|
317
|
+
if (!x.below) {
|
|
318
|
+
const dir = Math.sign(b.x - a.x) || 1;
|
|
319
|
+
const both = sameEdges.some((z) => z.a === x.b && z.b === x.a);
|
|
320
|
+
const yy = ys[r] + (both ? (x.a < x.b ? -6 : 6) : 0);
|
|
321
|
+
x.pts = [{ x: a.x + dir * a.w / 2, y: yy }, { x: b.x - dir * b.w / 2, y: yy }];
|
|
322
|
+
} else {
|
|
323
|
+
const ly = laneY.get(`${r}|${x.e.name}:same`) ?? (gapAfter[r].top + gapAfter[r].bottom) / 2;
|
|
324
|
+
x.pts = simplify([{ x: x.below.px, y: ys[r] + a.h / 2 }, { x: x.below.px, y: ly }, { x: x.below.qx, y: ly }, { x: x.below.qx, y: ys[r] + b.h / 2 }]);
|
|
325
|
+
}
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
const pts = [{ x: x.exit, y: ys[rank[x.a]] + item[x.a].h / 2 }];
|
|
329
|
+
for (const h of hops.filter((z) => z.x === x)) {
|
|
330
|
+
const ly = laneY.get(`${h.row}|${h.key}`) ?? (gapAfter[h.row].top + gapAfter[h.row].bottom) / 2;
|
|
331
|
+
const nextRow = h.row + 1;
|
|
332
|
+
const q = x.chain[h.k + 1];
|
|
333
|
+
pts.push({ x: h.px, y: ly }, { x: h.qx, y: ly });
|
|
334
|
+
if (q.dummy) pts.push({ x: h.qx, y: ys[nextRow] - rowH[nextRow] / 2 }, { x: h.qx, y: ys[nextRow] + rowH[nextRow] / 2 });
|
|
335
|
+
else pts.push({ x: h.qx, y: ys[nextRow] - q.h / 2 });
|
|
336
|
+
}
|
|
337
|
+
x.pts = simplify(pts);
|
|
338
|
+
}
|
|
339
|
+
// 5. back to screen coordinates
|
|
340
|
+
const T = lr ? (p) => ({ x: p.y, y: p.x }) : (p) => p;
|
|
341
|
+
const pos = nodes.map((n, i) => { const c = T({ x: item[i].x, y: ys[rank[i]] }); return { x: c.x, y: c.y, w: n.offsetWidth, h: n.offsetHeight }; });
|
|
342
|
+
const bandBoxes = bands.map((b) => {
|
|
343
|
+
const top = ys[b.from] - rowH[b.from] / 2 - HEAD - (lr ? 0 : BANDPAD) + (lr ? 0 : 0); const bottom = ys[b.to] + rowH[b.to] / 2 + BANDPAD;
|
|
344
|
+
const box = lr ? { l: top, t: 4, w: bottom - top, h: W - 8 } : { l: 4, t: top, w: W - 8, h: bottom - top };
|
|
345
|
+
return { g: b.g, box };
|
|
346
|
+
});
|
|
347
|
+
const routes = new Map(E.map((x) => {
|
|
348
|
+
let pts = x.pts.map(T);
|
|
349
|
+
if (x.e.back) pts = pts.reverse();
|
|
350
|
+
return [x.e.name, pts];
|
|
351
|
+
}));
|
|
352
|
+
return { width: lr ? H : W, height: lr ? W : H, pos, bands: bandBoxes, routes, dir, gutter: lr ? 0 : gutter };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function layout(fig) {
|
|
356
|
+
const m = prepare(fig);
|
|
357
|
+
const byId = new Map(m.nodes.map((n) => [n.id, n]));
|
|
358
|
+
// The author's order is the reading order: groups first to last, boxes inside a group in order.
|
|
359
|
+
// A connection that points back against it is a callback; it never pulls a box forward.
|
|
360
|
+
const gIndex = new Map(m.groups.map((gr, i) => [gr.id, i]));
|
|
361
|
+
const order = (n) => (gIndex.get(n.dataset.group) ?? m.groups.length) * 1000 + m.nodes.indexOf(n);
|
|
362
|
+
const probe = document.createElement('span'); probe.className = 'tm-dg-label tm-dg-probe'; m.stage.appendChild(probe);
|
|
363
|
+
const edges = [];
|
|
364
|
+
let labelW = 0;
|
|
365
|
+
for (const src of m.nodes) {
|
|
366
|
+
for (const spec of (src.getAttribute('data-to') || '').split(/[\s,]+/).filter(Boolean)) {
|
|
367
|
+
const [id, ...lab] = spec.split(':');
|
|
368
|
+
if (!byId.has(id) || id === src.id || edges.some((e) => e.from === src.id && e.to === id)) continue;
|
|
369
|
+
const label = viaOf(lab.join(':'));
|
|
370
|
+
let lw = 0;
|
|
371
|
+
if (label) { probe.textContent = label; lw = probe.offsetWidth; labelW = Math.max(labelW, lw); }
|
|
372
|
+
edges.push({ from: src.id, to: id, label, lw, name: `${src.id}>${id}`, back: order(src) > order(byId.get(id)) });
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
probe.remove();
|
|
376
|
+
// Direction: the author's, or left to right when it fits the column, else top to bottom.
|
|
377
|
+
const room = fig.clientWidth || 860;
|
|
378
|
+
const asked = (fig.getAttribute('data-direction') || '').toUpperCase();
|
|
379
|
+
let L = layered(m, edges, asked === 'TB' ? 'TB' : 'LR', labelW, room);
|
|
380
|
+
if (!asked && L.width > room) L = layered(m, edges, 'TB', labelW, room);
|
|
381
|
+
fig.dataset.laidOut = L.dir;
|
|
382
|
+
if (L.dir === 'TB' && L.width < room) {
|
|
383
|
+
const dx = (room - L.width) / 2;
|
|
384
|
+
for (const v of L.pos) v.x += dx;
|
|
385
|
+
for (const pts of L.routes.values()) for (const p of pts) p.x += dx;
|
|
386
|
+
for (const b of L.bands) b.box.w = room - 8;
|
|
387
|
+
L.width = room;
|
|
388
|
+
}
|
|
389
|
+
const gw = L.width; const gh = L.height;
|
|
390
|
+
m.nodes.forEach((n, i) => { const v = L.pos[i]; n.style.left = `${v.x - v.w / 2}px`; n.style.top = `${v.y - v.h / 2}px`; });
|
|
391
|
+
for (const gr of m.groups) gr.box = null;
|
|
392
|
+
for (const b of L.bands) {
|
|
393
|
+
const gr = m.groups[b.g];
|
|
394
|
+
if (!gr) continue;
|
|
395
|
+
gr.box = b.box;
|
|
396
|
+
}
|
|
397
|
+
for (const gr of m.groups) {
|
|
398
|
+
gr.el.hidden = !gr.box;
|
|
399
|
+
if (gr.box) Object.assign(gr.el.style, { left: `${gr.box.l}px`, top: `${gr.box.t}px`, width: `${gr.box.w}px`, height: `${gr.box.h}px` });
|
|
400
|
+
}
|
|
401
|
+
m.stage.style.width = `${gw}px`; m.stage.style.height = `${gh}px`;
|
|
402
|
+
const svg = m.svg; svg.innerHTML = '';
|
|
403
|
+
svg.setAttribute('width', gw); svg.setAttribute('height', gh); svg.setAttribute('viewBox', `0 0 ${gw} ${gh}`);
|
|
404
|
+
for (const lab of m.stage.querySelectorAll('.tm-dg-label')) lab.remove();
|
|
405
|
+
const isKey = (id) => byId.get(id)?.classList.contains('is-key');
|
|
406
|
+
const firstFlow = (m.tabs?.querySelector('.tm-dg-tab')?.getAttribute('data-path') || '').split(/[\s,]+/).filter(Boolean);
|
|
407
|
+
const mainPairs = new Set(firstFlow.slice(1).flatMap((id, i) => [`${firstFlow[i]}>${id}`, `${id}>${firstFlow[i]}`]));
|
|
408
|
+
const isMain = (e) => (mainPairs.size ? mainPairs.has(`${e.from}>${e.to}`) : isKey(e.from) && isKey(e.to));
|
|
409
|
+
const placed = [];
|
|
410
|
+
for (const e of edges) {
|
|
411
|
+
const pts = L.routes.get(e.name)?.map((p) => ({ ...p }));
|
|
412
|
+
if (!pts || pts.length < 2) continue;
|
|
413
|
+
// stop short of the target so the arrowhead sits outside the box
|
|
414
|
+
const a = pts[pts.length - 2]; const b = pts[pts.length - 1];
|
|
415
|
+
const len = Math.hypot(b.x - a.x, b.y - a.y) || 1;
|
|
416
|
+
const ux = (b.x - a.x) / len; const uy = (b.y - a.y) / len;
|
|
417
|
+
const tip = { x: b.x, y: b.y }; pts[pts.length - 1] = { x: b.x - ux * 7, y: b.y - uy * 7 };
|
|
418
|
+
const key = isMain(e);
|
|
419
|
+
const ext = byId.get(e.from)?.classList.contains('is-ext') || byId.get(e.to)?.classList.contains('is-ext');
|
|
420
|
+
const planned = byId.get(e.from)?.getAttribute('data-plan') === 'new' || byId.get(e.to)?.getAttribute('data-plan') === 'new';
|
|
421
|
+
const path = document.createElementNS(SVGNS, 'path');
|
|
422
|
+
path.setAttribute('d', roundPath(pts));
|
|
423
|
+
path.setAttribute('class', `tm-dg-edge${key ? ' is-main' : ''}${ext ? ' is-ext' : ''}${e.back ? ' is-back' : ''}${planned ? ' is-plan' : ''}`);
|
|
424
|
+
const head = document.createElementNS(SVGNS, 'path');
|
|
425
|
+
head.setAttribute('d', `M${tip.x},${tip.y} L${tip.x - ux * 7 - uy * 4},${tip.y - uy * 7 + ux * 4} L${tip.x - ux * 7 + uy * 4},${tip.y - uy * 7 - ux * 4} Z`);
|
|
426
|
+
head.setAttribute('class', `tm-dg-head${key ? ' is-main' : ''}${planned ? ' is-plan' : ''}`);
|
|
427
|
+
for (const el of [path, head]) { el.dataset.from = e.from; el.dataset.to = e.to; }
|
|
428
|
+
svg.appendChild(path); svg.appendChild(head);
|
|
429
|
+
if (e.label) placed.push({ e, path, key });
|
|
430
|
+
}
|
|
431
|
+
const boxes = L.pos.map((v) => ({ l: v.x - v.w / 2 - 3, r: v.x + v.w / 2 + 3, t: v.y - v.h / 2 - 3, b: v.y + v.h / 2 + 3 }));
|
|
432
|
+
const taken = [];
|
|
433
|
+
// the corner badges (+12 -3, Plan: edit) and the pins sit just above a box's top right
|
|
434
|
+
const badges = L.pos.map((v) => ({ l: v.x + v.w / 2 - 80, r: v.x + v.w / 2 + 12, t: v.y - v.h / 2 - 14, b: v.y - v.h / 2 + 6 }));
|
|
435
|
+
const hits = (a, list) => list.some((o) => a.l < o.r && a.r > o.l && a.t < o.b && a.b > o.t);
|
|
436
|
+
// points along every edge, so a group label can keep off the wires
|
|
437
|
+
const dots = [];
|
|
438
|
+
for (const p of svg.querySelectorAll('.tm-dg-edge')) { const L = p.getTotalLength(); for (let d = 0; d <= L; d += 5) { const q = p.getPointAtLength(d); dots.push({ l: q.x - 1, r: q.x + 1, t: q.y - 1, b: q.y + 1 }); } }
|
|
439
|
+
const crowd = (r) => (hits(r, boxes) ? 100 : 0) + (hits(r, taken) ? 100 : 0) + dots.filter((o) => r.l < o.r && r.r > o.l && r.t < o.b && r.b > o.t).length;
|
|
440
|
+
for (const gr of m.groups) {
|
|
441
|
+
if (!gr.box) continue;
|
|
442
|
+
const lab = gr.el.firstChild; lab.style.left = '0px'; lab.style.top = '0px';
|
|
443
|
+
if (L.dir === 'TB') { lab.style.left = '14px'; lab.style.top = '12px'; continue; }
|
|
444
|
+
const w = lab.offsetWidth + 6; const h = lab.offsetHeight + 4; const { l, t, w: W, h: H } = gr.box;
|
|
445
|
+
const spots = [[l + 10, t + 6], [l + W - 10 - w, t + 6], [l + 10, t + H - 6 - h], [l + W - 10 - w, t + H - 6 - h]];
|
|
446
|
+
let best = null; let bestScore = Infinity;
|
|
447
|
+
for (const [x, y] of spots) { const r = { l: x, r: x + w, t: y, b: y + h }; const sc = crowd(r); if (sc < bestScore) { best = r; bestScore = sc; } if (!sc) break; }
|
|
448
|
+
lab.style.left = `${best.l - l + 3}px`; lab.style.top = `${best.t - t + 2}px`;
|
|
449
|
+
taken.push(best);
|
|
450
|
+
}
|
|
451
|
+
// Edge labels sit on their own edge, at the first spot along it that covers no box and no other label.
|
|
452
|
+
// The same word on several edges from one box is said once, on the most important of them.
|
|
453
|
+
const seen = new Set();
|
|
454
|
+
placed.sort((x, y) => (y.key - x.key));
|
|
455
|
+
for (const pl of placed) { const k = `${pl.e.from}|${pl.e.label}`; pl.dup = seen.has(k); seen.add(k); }
|
|
456
|
+
const bundleSize = new Map();
|
|
457
|
+
for (const pl of placed) { const k = `${pl.e.from}|${pl.e.label}`; bundleSize.set(k, (bundleSize.get(k) || 0) + 1); }
|
|
458
|
+
for (const { e, path, key, dup } of placed) {
|
|
459
|
+
const t = document.createElement('span'); t.className = `tm-dg-label${key ? ' is-main' : ''}${dup ? ' is-dup' : ''}`;
|
|
460
|
+
const many = bundleSize.get(`${e.from}|${e.label}`) || 1;
|
|
461
|
+
t.textContent = !dup && many > 1 ? `${e.label} ×${many}` : e.label;
|
|
462
|
+
t.dataset.from = e.from; t.dataset.to = e.to;
|
|
463
|
+
m.stage.appendChild(t);
|
|
464
|
+
const w = t.offsetWidth; const h = t.offsetHeight; const L = path.getTotalLength();
|
|
465
|
+
let best = null;
|
|
466
|
+
const trunk = bundleSize.get(`${e.from}|${e.label}`) > 1;
|
|
467
|
+
// try every few pixels along the line, nearest the preferred spot first; take the first free one,
|
|
468
|
+
// otherwise the least crowded; a label that would still cover a box only shows when its line is lit
|
|
469
|
+
const pref = trunk ? 0.15 : 0.5;
|
|
470
|
+
const fr = [pref]; for (let d = 0.04; d < 0.96; d += 0.04) { if (pref + d < 0.97) fr.push(pref + d); if (pref - d > 0.03) fr.push(pref - d); }
|
|
471
|
+
let bestCost = Infinity;
|
|
472
|
+
const overlap = (a, list) => list.reduce((n, o) => n + Math.max(0, Math.min(a.r, o.r) - Math.max(a.l, o.l)) * Math.max(0, Math.min(a.b, o.b) - Math.max(a.t, o.t)), 0);
|
|
473
|
+
for (const f of fr) {
|
|
474
|
+
const p = path.getPointAtLength(L * f);
|
|
475
|
+
const r = { l: p.x - w / 2, r: p.x + w / 2, t: p.y - h / 2, b: p.y + h / 2 };
|
|
476
|
+
const cost = overlap(r, boxes) * 4 + overlap(r, badges) * 4 + overlap(r, taken) * 2;
|
|
477
|
+
if (cost < bestCost) { bestCost = cost; best = r; }
|
|
478
|
+
if (!cost) break;
|
|
479
|
+
}
|
|
480
|
+
if (bestCost > 0) t.classList.add('is-crowded');
|
|
481
|
+
if (!dup && !t.classList.contains('is-crowded')) taken.push(best);
|
|
482
|
+
t.style.left = `${(best.l + best.r) / 2}px`; t.style.top = `${(best.t + best.b) / 2}px`;
|
|
483
|
+
}
|
|
484
|
+
// main-path edges on top
|
|
485
|
+
for (const el of [...svg.querySelectorAll('.is-main')]) svg.appendChild(el);
|
|
486
|
+
// Still wider than the column: shrink a little, then scroll rather than make text unreadable.
|
|
487
|
+
const k = Math.max(0.7, Math.min(1, (room - 16) / gw));
|
|
488
|
+
m.stage.style.transform = k < 1 ? `scale(${k})` : '';
|
|
489
|
+
m.stage.style.marginBottom = k < 1 ? `${-gh * (1 - k)}px` : '';
|
|
490
|
+
m.stage.style.marginRight = k < 1 ? `${-gw * (1 - k)}px` : '';
|
|
491
|
+
fig.classList.toggle('is-wide', gw * k > room + 1);
|
|
492
|
+
markFlow(fig);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// A lit flow: numbered steps in order along its edges; the other boxes keep their shape, only their text dims.
|
|
496
|
+
function markFlow(fig) {
|
|
497
|
+
const m = fig.__tm; if (!m) return;
|
|
498
|
+
for (const b of m.stage.querySelectorAll('.tm-dg-step')) b.remove();
|
|
499
|
+
for (const l of m.stage.querySelectorAll('.tm-dg-label[data-step]')) l.removeAttribute('data-step');
|
|
500
|
+
const active = m.tabs?.querySelector('[aria-selected="true"]');
|
|
501
|
+
const ids = active ? active.getAttribute('data-path').split(/[\s,]+/).filter(Boolean) : [];
|
|
502
|
+
fig.toggleAttribute('data-flow', ids.length > 0);
|
|
503
|
+
const on = new Set(ids);
|
|
504
|
+
for (const n of m.nodes) n.toggleAttribute('data-in-flow', on.has(n.id));
|
|
505
|
+
const pairs = new Map(); ids.forEach((id, i) => { if (i < ids.length - 1) pairs.set(`${id}>${ids[i + 1]}`, i + 1); });
|
|
506
|
+
for (const el of m.stage.querySelectorAll('.tm-dg-edge, .tm-dg-head, .tm-dg-label')) {
|
|
507
|
+
const k = `${el.dataset.from}>${el.dataset.to}`;
|
|
508
|
+
el.classList.toggle('is-flow', pairs.has(k));
|
|
509
|
+
}
|
|
510
|
+
// step numbers sit on the edge's midpoint (or on its label)
|
|
511
|
+
for (const [k, n] of pairs) {
|
|
512
|
+
const p = m.svg.querySelector(`.tm-dg-edge[data-from="${CSS.escape(k.split('>')[0])}"][data-to="${CSS.escape(k.split('>')[1])}"]`);
|
|
513
|
+
if (!p) continue;
|
|
514
|
+
const [from, to] = k.split('>');
|
|
515
|
+
const lab = [...m.stage.querySelectorAll('.tm-dg-label')].find((l) => l.dataset.from === from && l.dataset.to === to);
|
|
516
|
+
// a line with a word carries its number inside the label, so the two can never cover each other
|
|
517
|
+
if (lab && !lab.classList.contains('is-dup')) { lab.setAttribute('data-step', String(n)); continue; }
|
|
518
|
+
const s = document.createElement('span'); s.className = 'tm-dg-step'; s.textContent = String(n);
|
|
519
|
+
// the number sits on its own line, just before the arrowhead, so it can't be read as another line's
|
|
520
|
+
const L = p.getTotalLength();
|
|
521
|
+
const labs = [...m.stage.querySelectorAll('.tm-dg-label')].filter((l) => getComputedStyle(l).visibility !== 'hidden').map((l) => { const x = parseFloat(l.style.left); const y = parseFloat(l.style.top); return { l: x - l.offsetWidth / 2 - 3, r: x + l.offsetWidth / 2 + 3, t: y - l.offsetHeight / 2 - 3, b: y + l.offsetHeight / 2 + 3 }; });
|
|
522
|
+
let at = p.getPointAtLength(Math.max(0, L - 20));
|
|
523
|
+
for (let d = 20; d < L - 10; d += 6) {
|
|
524
|
+
const q = p.getPointAtLength(L - d);
|
|
525
|
+
if (!labs.some((o) => q.x + 9 > o.l && q.x - 9 < o.r && q.y + 9 > o.t && q.y - 9 < o.b)) { at = q; break; }
|
|
526
|
+
}
|
|
527
|
+
s.style.left = `${at.x}px`; s.style.top = `${at.y}px`;
|
|
528
|
+
m.stage.appendChild(s);
|
|
529
|
+
}
|
|
530
|
+
const note = m.tabs && fig.querySelector(':scope > .tm-dg-note');
|
|
531
|
+
if (active && active.getAttribute('data-note')) {
|
|
532
|
+
const el = note || document.createElement('p');
|
|
533
|
+
el.className = 'tm-dg-note'; el.textContent = active.getAttribute('data-note');
|
|
534
|
+
if (!note) m.tabs.after(el);
|
|
535
|
+
} else note?.remove();
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function draw() {
|
|
539
|
+
for (const fig of document.querySelectorAll('figure.tm-diagram')) {
|
|
540
|
+
try { layout(fig); } catch (e) { console.warn('truemap diagram:', e); }
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function setFlow(fig, btn) {
|
|
544
|
+
const m = fig.__tm; if (!m?.tabs) return;
|
|
545
|
+
const was = btn?.getAttribute('aria-selected') === 'true';
|
|
546
|
+
for (const b of m.tabs.querySelectorAll('.tm-dg-tab')) b.setAttribute('aria-selected', String(!!btn && b === btn && !was));
|
|
547
|
+
markFlow(fig);
|
|
548
|
+
}
|
|
549
|
+
// Generic hover and tab handling (the SDK adds the inspector; exported pages get this alone).
|
|
550
|
+
function interact() {
|
|
551
|
+
document.addEventListener('click', (e) => {
|
|
552
|
+
const tab = e.target.closest?.('.tm-dg-tab');
|
|
553
|
+
if (tab) { setFlow(tab.closest('figure.tm-diagram'), tab); return; }
|
|
554
|
+
const fig = e.target.closest?.('figure.tm-diagram');
|
|
555
|
+
if (fig && !e.target.closest('.tm-node, .tm-dg-tabs')) setFlow(fig, null); // empty space returns to the plain map
|
|
556
|
+
});
|
|
557
|
+
let hot = null;
|
|
558
|
+
document.addEventListener('mouseover', (e) => {
|
|
559
|
+
const n = e.target.closest?.('figure.tm-diagram .tm-node') || null;
|
|
560
|
+
if (n === hot) return; hot = n;
|
|
561
|
+
for (const el of document.querySelectorAll('.tm-dg-edge, .tm-dg-head, .tm-dg-label')) {
|
|
562
|
+
el.classList.toggle('is-hot', !!n && (el.dataset.from === n.id || el.dataset.to === n.id));
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
// A plan's new boxes, drawn dashed in their group until the plan is built.
|
|
567
|
+
function setGhosts(fig, adds) {
|
|
568
|
+
const m = prepare(fig);
|
|
569
|
+
for (const n of m.nodes.filter((x) => x.dataset.ghost)) n.remove();
|
|
570
|
+
m.nodes = m.nodes.filter((x) => !x.dataset.ghost);
|
|
571
|
+
for (const n of m.nodes) if (n.dataset.toOrig !== undefined) { n.setAttribute('data-to', n.dataset.toOrig); delete n.dataset.toOrig; }
|
|
572
|
+
const groupOf = (g) => {
|
|
573
|
+
const k = String(g || '').toLowerCase();
|
|
574
|
+
return m.groups.find((gr) => gr.id.toLowerCase() === k || (gr.el.firstChild?.textContent || '').toLowerCase() === k) || m.groups[0] || null;
|
|
575
|
+
};
|
|
576
|
+
for (const a of adds || []) {
|
|
577
|
+
if (!a || !a.id || document.getElementById(a.id)) continue;
|
|
578
|
+
const n = document.createElement('div');
|
|
579
|
+
n.className = 'tm-node'; n.id = a.id; n.dataset.ghost = '1'; n.setAttribute('data-plan', 'new'); n.setAttribute('tabindex', '0');
|
|
580
|
+
n.setAttribute('data-name', a.name || a.id);
|
|
581
|
+
if (a.tag) n.setAttribute('data-tag', a.tag);
|
|
582
|
+
if (a.to) n.setAttribute('data-to', a.to);
|
|
583
|
+
const det = document.createElement('div'); det.className = 'tm-detail';
|
|
584
|
+
if (a.why) { const p = document.createElement('p'); p.textContent = a.why; det.appendChild(p); }
|
|
585
|
+
n.appendChild(det);
|
|
586
|
+
adopt(m, n, groupOf(a.group)?.id);
|
|
587
|
+
for (const spec of a.from || []) {
|
|
588
|
+
const [src, ...lab] = String(spec).split(':');
|
|
589
|
+
const from = m.nodes.find((x) => x.id === src);
|
|
590
|
+
if (!from) continue;
|
|
591
|
+
if (from.dataset.toOrig === undefined) from.dataset.toOrig = from.getAttribute('data-to') || '';
|
|
592
|
+
from.setAttribute('data-to', `${from.getAttribute('data-to') || ''} ${a.id}${lab.length ? `:${lab.join(':')}` : ''}`.trim());
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
layout(fig);
|
|
596
|
+
}
|
|
597
|
+
window.truemapDiagram = { draw, interact, setFlow, nameOf, setGhosts };
|
|
598
|
+
})();
|