truemap-cli 0.3.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ui/wires.js CHANGED
@@ -54,7 +54,7 @@
54
54
  if (!dst || dst === src || !map.contains(dst) || !rectOf.has(src) || !rectOf.has(dst)) continue;
55
55
  const a = rectOf.get(src); const b = rectOf.get(dst);
56
56
  const ra = rowOf(a); const rb = rowOf(b);
57
- edges.push({ src, dst, a, b, ra, rb, dir: rb > ra ? 'down' : rb < ra ? 'up' : 'same', label: labelParts.join(':').replace(/_/g, ' ') });
57
+ edges.push({ src, dst, a, b, ra, rb, dir: rb > ra ? 'down' : rb < ra ? 'up' : 'same', label: labelParts.join(':').replace(/\\_/g, '\u0001').replace(/_/g, ' ').replace(/\u0001/g, '_') });
58
58
  }
59
59
  }
60
60
  const group = (key, pick) => {
@@ -181,12 +181,18 @@
181
181
  const svg = document.createElementNS(SVGNS, 'svg');
182
182
  svg.setAttribute('class', 'tm-wires'); svg.setAttribute('aria-hidden', 'true');
183
183
  svg.setAttribute('width', w); svg.setAttribute('height', h); svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
184
- const placed = []; const labelsBy = new Map();
184
+ const segsOf = new Map(); // plan -> its straight segments, for label placement
185
+ for (const p of plans) {
186
+ const pts = p.pts.map(([x, y]) => [x, typeof y === 'object' ? y.y : y]);
187
+ p.resolved = pts;
188
+ segsOf.set(p, pts.slice(1).map((q, i) => [pts[i][0], pts[i][1], q[0], q[1]]));
189
+ }
190
+ const isKey = (p) => p.e.src.classList.contains('is-key') && p.e.dst.classList.contains('is-key');
185
191
  for (const p of plans) {
186
192
  const { e } = p;
187
- const key = e.src.classList.contains('is-key') && e.dst.classList.contains('is-key');
193
+ const key = isKey(p);
188
194
  const ext = e.dst.classList.contains('is-ext') || e.src.classList.contains('is-ext');
189
- const pts = p.pts.map(([x, y]) => [x, typeof y === 'object' ? y.y : y]);
195
+ const pts = p.resolved;
190
196
  const path = document.createElementNS(SVGNS, 'path');
191
197
  path.setAttribute('d', ortho(pts)); path.setAttribute('class', `tm-wire${key ? ' is-key' : ''}${ext ? ' is-ext' : ''}`);
192
198
  const { tx, ty } = p;
@@ -194,31 +200,89 @@
194
200
  right: `M${tx - 1},${ty - 4.5} L${tx - 1},${ty + 4.5} L${tx + 6},${ty} Z`, left: `M${tx + 1},${ty - 4.5} L${tx + 1},${ty + 4.5} L${tx - 6},${ty} Z` }[p.head];
195
201
  const arrow = document.createElementNS(SVGNS, 'path');
196
202
  arrow.setAttribute('d', head); arrow.setAttribute('class', `tm-wire-head${key ? ' is-key' : ''}`);
203
+ for (const el of [path, arrow]) { el.dataset.from = e.src.id; el.dataset.to = e.dst.id; }
197
204
  svg.appendChild(path); svg.appendChild(arrow);
198
- // Labels sit by the box the wire leaves; one per text per box; never on another label or a box.
199
- const used = labelsBy.get(e.src) || labelsBy.set(e.src, { texts: [], x: null }).get(e.src);
200
- if (e.label && !used.texts.includes(e.label)) {
201
- const lw = e.label.length * 6.4; const y = p.label.y;
202
- let x = p.label.middle ? p.label.x - lw / 2 : Math.max(p.label.x, used.x ?? -Infinity);
203
- const hits = (lx) => placed.some((q) => lx < q.r + 8 && lx + lw > q.l - 8 && Math.abs(q.y - y) < 12)
204
- || rects.some((o) => lx < o.r && lx + lw > o.l && y > o.t && y - 10 < o.b);
205
- for (let i = 0; i < 40 && hits(x); i++) x += 12;
206
- const t = document.createElementNS(SVGNS, 'text');
207
- t.setAttribute('class', 'tm-wire-label'); t.setAttribute('x', x); t.setAttribute('y', y);
208
- t.textContent = e.label; svg.appendChild(t);
209
- placed.push({ l: x, r: x + lw, y });
210
- if (!p.label.middle) used.x = x + lw + 12;
211
- used.texts.push(e.label);
212
- }
213
205
  }
214
- // key wires on top, labels above every wire
206
+ // key wires on top
215
207
  [...svg.querySelectorAll('.tm-wire.is-key, .tm-wire-head.is-key')].forEach((el) => svg.appendChild(el));
216
- [...svg.querySelectorAll('.tm-wire-label')].forEach((el) => svg.appendChild(el));
217
208
  map.insertBefore(svg, map.firstChild);
209
+ map.__tm = { plans, segsOf, rects, w, h, svg, isKey };
210
+ placeLabels(map, labelState);
218
211
  markScroll(map);
219
212
  return false;
220
213
  }
221
214
 
215
+ // ---------- labels: placed for what is visible right now ----------
216
+ // Only some labels show at a time: the main path's by default; a hovered box's own wires; a lit path's wires.
217
+ // Each time that changes, the visible labels are placed afresh. A label goes on its own wire, at the first spot
218
+ // (walking from where the wire leaves its box) that touches no box, no other shown label and no wire that is
219
+ // drawn strongly at that moment. With no such spot the label is left off the map; the box's peek still lists it.
220
+ let labelState = {};
221
+ const segHits = ([x1, y1, x2, y2], b) => (x1 === x2
222
+ ? x1 > b.l - 2 && x1 < b.r + 2 && Math.max(y1, y2) > b.t && Math.min(y1, y2) < b.b
223
+ : y1 > b.t - 2 && y1 < b.b + 2 && Math.max(x1, x2) > b.l && Math.min(x1, x2) < b.r);
224
+ function placeLabels(map, state) {
225
+ const m = map.__tm; if (!m) return;
226
+ const { plans, segsOf, rects, w, h, svg, isKey } = m;
227
+ for (const el of svg.querySelectorAll('.tm-wire-label, .tm-wire-plate')) el.remove();
228
+ const hot = state.hot || null; const focus = state.focus && state.focus.size ? state.focus : null;
229
+ const isHot = (p) => !!hot && (p.e.src.id === hot || p.e.dst.id === hot);
230
+ const isFocus = (p) => !!focus && focus.has(p.e.src.id) && focus.has(p.e.dst.id);
231
+ const shown = (p) => (focus ? isFocus(p) || isHot(p) : isKey(p) || isHot(p));
232
+ // Wires a label must keep clear of: the ones drawn strongly right now. Faint wires pass under the label's
233
+ // paper plate, which interrupts them the way a map interrupts a road under a place name.
234
+ const strong = (p) => (focus ? isFocus(p) || isHot(p) : isKey(p) || isHot(p));
235
+ const order = focus ? [...plans.filter(isFocus), ...plans.filter((p) => !isFocus(p) && isHot(p))]
236
+ : [...plans.filter(isKey), ...plans.filter((p) => !isKey(p) && isHot(p))];
237
+ const placed = []; const used = new Map();
238
+ for (const p of order) {
239
+ const { e } = p;
240
+ const texts = used.get(e.src) || used.set(e.src, []).get(e.src);
241
+ if (!e.label || texts.includes(e.label)) continue;
242
+ const lw = e.label.length * 6.4;
243
+ const cands = [];
244
+ for (const [x1, y1, x2, y2] of segsOf.get(p)) {
245
+ if (x1 === x2) {
246
+ const lo = Math.min(y1, y2); const hi = Math.max(y1, y2);
247
+ for (let y = lo + 15; y <= hi - 1; y += 7) cands.push({ x: x1 + 6, y, anchor: 'start' }, { x: x1 - 6, y, anchor: 'end' });
248
+ } else {
249
+ const lo = Math.min(x1, x2); const hi = Math.max(x1, x2);
250
+ for (let x = lo + 6; x + lw <= hi - 2; x += 12) cands.push({ x, y: y1 - 5, anchor: 'start' }, { x, y: y1 + 14, anchor: 'start' });
251
+ }
252
+ }
253
+ const boxOf = (c) => { const l = c.anchor === 'end' ? c.x - lw : c.x; return { l, r: l + lw, t: c.y - 10, b: c.y + 3 }; };
254
+ const free = (c) => {
255
+ const b = boxOf(c);
256
+ if (b.l < 2 || b.r > w - 2 || b.t < 0 || b.b > h) return false;
257
+ if (rects.some((o) => b.l < o.r + 3 && b.r > o.l - 3 && b.t < o.b + 3 && b.b > o.t - 3)) return false;
258
+ if (placed.some((q) => b.l < q.r + 6 && b.r > q.l - 6 && b.t < q.b + 2 && b.b > q.t - 2)) return false;
259
+ for (const [q, segs] of segsOf) if (q !== p && strong(q) && segs.some((sg) => segHits(sg, b))) return false;
260
+ return true;
261
+ };
262
+ const c = cands.find(free);
263
+ if (!c) continue;
264
+ placed.push(boxOf(c));
265
+ const cls = `${isKey(p) ? ' is-key' : ''}${isHot(p) ? ' is-hot' : ''}${isFocus(p) ? ' is-focus' : ''}`;
266
+ const t = document.createElementNS(SVGNS, 'text');
267
+ t.setAttribute('class', `tm-wire-label${cls}`);
268
+ t.setAttribute('x', c.x); t.setAttribute('y', c.y);
269
+ if (c.anchor !== 'start') t.setAttribute('text-anchor', c.anchor);
270
+ t.dataset.from = e.src.id; t.dataset.to = e.dst.id;
271
+ t.textContent = e.label;
272
+ // the paper plate under the label: faint wires stop at its edge instead of running through the words
273
+ const bb = boxOf(c); const plate = document.createElementNS(SVGNS, 'rect');
274
+ plate.setAttribute('class', `tm-wire-plate${cls}`);
275
+ plate.setAttribute('x', bb.l - 3); plate.setAttribute('y', bb.t - 1); plate.setAttribute('width', bb.r - bb.l + 6); plate.setAttribute('height', bb.b - bb.t + 3); plate.setAttribute('rx', 3);
276
+ plate.dataset.from = e.src.id; plate.dataset.to = e.dst.id;
277
+ svg.appendChild(plate); svg.appendChild(t);
278
+ texts.push(e.label);
279
+ }
280
+ }
281
+ function labels(state = {}) {
282
+ labelState = { hot: state.hot || null, focus: state.focus ? new Set(state.focus) : null };
283
+ for (const map of document.querySelectorAll('.tm-map, .tm-blueprint')) placeLabels(map, labelState);
284
+ }
285
+
222
286
  function drawWires() {
223
287
  document.documentElement.style.setProperty('--tm-vw', `${document.documentElement.clientWidth}px`);
224
288
  for (const map of document.querySelectorAll('.tm-map, .tm-blueprint')) {
@@ -233,5 +297,33 @@
233
297
  map.classList.toggle('tm-scrolls', map.scrollWidth - map.clientWidth - map.scrollLeft > 4);
234
298
  if (!map.__tmScroll) { map.__tmScroll = true; map.addEventListener('scroll', () => markScroll(map), { passive: true }); }
235
299
  }
236
- window.truemapWires = { draw: drawWires };
300
+ // Hover and path buttons without the truemap workspace (used by exported pages; the SDK has its own).
301
+ function interact() {
302
+ let hot = null; let lit = [];
303
+ const mark = () => {
304
+ const ids = new Set(lit);
305
+ for (const el of document.querySelectorAll('.tm-wire, .tm-wire-head, .tm-wire-label')) {
306
+ el.classList.toggle('is-hot', !!hot && (el.dataset.from === hot.id || el.dataset.to === hot.id));
307
+ el.classList.toggle('is-focus', ids.has(el.dataset.from) && ids.has(el.dataset.to));
308
+ }
309
+ labels({ hot: hot?.id || null, focus: lit });
310
+ };
311
+ document.addEventListener('mousemove', (e) => {
312
+ const box = e.target.closest?.('.tm-map .tm-node, .tm-blueprint .tm-node') || null;
313
+ if (box !== hot) { hot = box; mark(); }
314
+ });
315
+ document.addEventListener('click', (e) => {
316
+ const btn = e.target.closest?.('[data-tm-flow]'); if (!btn) return;
317
+ const ids = btn.getAttribute('data-tm-flow').split(/[\s,]+/).filter(Boolean);
318
+ const same = lit.length && ids.join() === lit.join();
319
+ lit = same ? [] : ids;
320
+ document.documentElement.toggleAttribute('data-tm-focusing', !!lit.length);
321
+ for (const el of document.querySelectorAll('[data-tm-focus]')) el.removeAttribute('data-tm-focus');
322
+ for (const id of lit) document.getElementById(id)?.setAttribute('data-tm-focus', '');
323
+ for (const b of document.querySelectorAll('[data-tm-flow]')) b.setAttribute('aria-pressed', String(!same && b === btn));
324
+ mark();
325
+ });
326
+ window.truemapWires.mark = mark;
327
+ }
328
+ window.truemapWires = { draw: drawWires, interact, labels };
237
329
  })();