@intentius/behold 0.29.42 → 0.29.43

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intentius/behold",
3
- "version": "0.29.42",
3
+ "version": "0.29.43",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "behold — a live control plane on chant. See your whole estate (every substrate in one graph), coloured by drift; act through delegated, gated Ops.",
package/web/app.js CHANGED
@@ -11,7 +11,7 @@
11
11
  import "./view-repo.js"; // behold#571: first, so every read carries the page's ?repo=
12
12
  import { createRefreshQueue } from "./refresh-queue.js";
13
13
  import { readViewQuery, writeViewQuery, settleView, settlePlace, refusalLine } from "./view-url.js";
14
- import { quietCardLines, readEmbed, selectMessage, viewFromMessage } from "./embed.js";
14
+ import { quietCardLines, readEmbed, selectMessage, viewFromMessage, wheelPx, wheelZoomsBare } from "./embed.js";
15
15
  import { WORDS as POP_WORDS, cardRoot as popCardRoot, placePopover, popReduce, popState, resourcePopover, rootPopover } from "./kiosk-popover.js";
16
16
  import { cssString, hoverCss, hoverFromMessage, hoverLinks, hoverMessage, isLightBg, rootPaint, rootText } from "./kiosk-roots.js";
17
17
  import { gateCards } from "./workspace-gates.js";
@@ -122,7 +122,7 @@ import { shotFromMessage, shotViewBox, tweenViewBox, unionBox, sameViewBox } fro
122
122
  // (/api/terragucci/carve, liveCarveGate below), or when /api/graph's meta
123
123
  // carries `dots` (the local preview server, scripts/dots-preview.ts). With
124
124
  // neither it stays unmounted, and its hooks below are no-ops.
125
- import { mountDots, unmountDots, dotsCamera, dotsZoomOutFactor, dotsCarving } from "./dots.js";
125
+ import { mountDots, unmountDots, dotsCamera, dotsZoomOutFactor, dotsCarving, dotsMinViewWidth } from "./dots.js";
126
126
  import { carveDrawable } from "./dots-live.js";
127
127
  initTheme();
128
128
  // #476: framed in a host pane. The flag is a class on <html>, so the CSS that
@@ -4713,7 +4713,9 @@ function wireKioskFrame() {
4713
4713
  // behold#571: a click on the graph holds the kiosk's shot where the viewer is reading.
4714
4714
  if (card && kioskShot) kioskViewerMoved();
4715
4715
  // behold#571: a click on a wave's box (not a card in it) asks the kiosk for that wave's trace, or its log.
4716
- if (!card && !panMoved && embedOpts.host && window.parent !== window) {
4716
+ // While the dots layer has the SVG out of the paint, its wave boxes are not on screen: a tap there is on a dot or a card.
4717
+ const svgShown = !(currentSvg() && currentSvg().style.visibility === "hidden");
4718
+ if (!card && !panMoved && svgShown && embedOpts.host && window.parent !== window) {
4717
4719
  const box = e.target.closest && (e.target.closest("rect[data-group-id]") || e.target.closest("[data-group-id]"));
4718
4720
  let n = box ? tgWaveOfKey(box.getAttribute("data-group-id")) : null;
4719
4721
  if (n === null) {
@@ -7150,16 +7152,18 @@ function ensureZoomControls(host) {
7150
7152
  "wheel",
7151
7153
  (e) => {
7152
7154
  // Trackpad pinch fires wheel+ctrlKey; ⌘/Ctrl+scroll is the explicit gesture.
7153
- // Plain scroll is left alone (nothing to scroll when fit).
7154
- if (!vb || !(e.ctrlKey || e.metaKey)) return;
7155
+ // Plain scroll is left alone (nothing to scroll when fit), except in the kiosk's
7156
+ // bare frame: the graph is the whole stage there, so a mouse wheel zooms it, as a map's does.
7157
+ if (!vb || !(e.ctrlKey || e.metaKey || wheelZoomsBare(embedOpts.bare, e))) return;
7155
7158
  e.preventDefault();
7156
7159
  const s = currentSvg();
7157
7160
  if (!s) return;
7158
7161
  const r = s.getBoundingClientRect();
7159
7162
  const cx = vb[0] + ((e.clientX - r.left) / r.width) * vb[2];
7160
7163
  const cy = vb[1] + ((e.clientY - r.top) / r.height) * vb[3];
7161
- const f = Math.exp(e.deltaY * 0.0025); // scroll up → f<1 → zoom in
7162
- const minW = vbInit[2] / 60;
7164
+ const f = Math.exp(wheelPx(e) * 0.0025); // scroll up → f<1 → zoom in
7165
+ // While the dots layer draws the roster's cards, no further in than a little past them: past that is an empty stage.
7166
+ const minW = Math.max(vbInit[2] / 60, dotsMinViewWidth() ?? 0);
7163
7167
  const maxW = vbInit[2] * (dotsZoomOutFactor() ?? 3); // #712: the dots preview zooms out further
7164
7168
  const nw = Math.min(maxW, Math.max(minW, vb[2] * f));
7165
7169
  const nh = nw * (vb[3] / vb[2]);
@@ -7200,7 +7204,7 @@ function ensureZoomControls(host) {
7200
7204
  const m0 = mid(touch.pts);
7201
7205
  const cx = vb[0] + ((m.x - r.left) / r.width) * vb[2];
7202
7206
  const cy = vb[1] + ((m.y - r.top) / r.height) * vb[3];
7203
- const nw = Math.min(vbInit[2] * (dotsZoomOutFactor() ?? 3), Math.max(vbInit[2] / 60, vb[2] * f));
7207
+ const nw = Math.min(vbInit[2] * (dotsZoomOutFactor() ?? 3), Math.max(vbInit[2] / 60, dotsMinViewWidth() ?? 0, vb[2] * f));
7204
7208
  const nh = nw * (vb[3] / vb[2]);
7205
7209
  vb[0] = cx - ((cx - vb[0]) * nw) / vb[2] - ((m.x - m0.x) / r.width) * nw;
7206
7210
  vb[1] = cy - ((cy - vb[1]) * nh) / vb[3] - ((m.y - m0.y) / r.height) * nh;
@@ -0,0 +1,362 @@
1
+ // The dots layer's resource cards from the roster: the card level where the
2
+ // page's SVG has no card for a resource. A live carve framed at the kiosk's
3
+ // Components zoom draws one SVG card per root, never one per resource, so the
4
+ // cards a dot grows into are drawn on the canvas instead, from the roster row
5
+ // alone: its address, its type and the estate that owns it now.
6
+ //
7
+ // A dot's slot leaves about one pitch round it, far too little for a card that
8
+ // can hold a name. So at the card level each estate's resources move from
9
+ // their slots into a grid of cards that fills the estate's own box, in unit
10
+ // order: a dot grows into its card as it slides to its cell, and the boxes
11
+ // never move. One card size serves every estate and every moment of the show,
12
+ // so a card never changes size as the carve moves resources about. Pure: rows,
13
+ // layouts and sizes in, plain data out.
14
+
15
+ import { unitOf, parseAddress } from "./dots-layout.js";
16
+ import { BOX } from "./dots-scene.js";
17
+ import { LOD, lerp, smooth } from "./dots-lod.js";
18
+
19
+ /**
20
+ * A grid cell is `aspect` cell heights wide; the card leaves `gap` cell
21
+ * heights to its neighbours. `maxK` caps a cell's height in pitches, so a box
22
+ * with few resources does not get giant cards.
23
+ */
24
+ export const GRID = { aspect: 3.2, gap: 0.12, maxK: 3 };
25
+
26
+ /**
27
+ * The label's faces, in CSS px: the name over the type, growing with the card
28
+ * up to `max`; `pad` and `bar` (the estate's colour down the left) in px.
29
+ * `need` is clamped to [minW, maxW]: the card width at which the card level
30
+ * starts.
31
+ */
32
+ export const CARD_TEXT = { name: 11.5, type: 10.5, max: 15, pad: 7, bar: 3, minW: 90, maxW: 230 };
33
+
34
+ /** The largest cell height (in the box's units) at which `n` cells `aspect` heights wide fit a W x H box in rows. */
35
+ export function bestFit(W, H, n, aspect = GRID.aspect) {
36
+ if (!(n > 0) || !(W > 0) || !(H > 0)) return 0;
37
+ let best = 0;
38
+ for (let c = 1; c <= n; c++) {
39
+ const k = Math.min(W / (c * aspect), H / Math.ceil(n / c));
40
+ if (k > best) best = k;
41
+ }
42
+ return best;
43
+ }
44
+
45
+ /** A box's inner rect, where its resources go: inside its padding, under its title row. */
46
+ export function innerOf(b) {
47
+ return { x: b.x + BOX.pad, y: b.y + BOX.pad + BOX.title, w: Math.max(0, b.w - 2 * BOX.pad), h: Math.max(0, b.h - 2 * BOX.pad - BOX.title) };
48
+ }
49
+
50
+ /** Each estate's resources in a layout, in unit order (web/dots-layout.js), so a unit's cards sit together. */
51
+ export function membersOf(layout, keyOf = (a) => a) {
52
+ const by = new Map();
53
+ for (const a of layout.slots.keys()) {
54
+ const e = layout.owners.get(a);
55
+ if (!layout.boxes.has(e)) continue;
56
+ if (!by.has(e)) by.set(e, []);
57
+ by.get(e).push(a);
58
+ }
59
+ for (const list of by.values()) list.sort((x, y) => {
60
+ const kx = keyOf(x);
61
+ const ky = keyOf(y);
62
+ return kx < ky ? -1 : kx > ky ? 1 : x < y ? -1 : x > y ? 1 : 0;
63
+ });
64
+ return by;
65
+ }
66
+
67
+ /**
68
+ * The one cell height (in pitches) every estate's cards fit at, across the
69
+ * given layouts: the tightest box's best fit, capped at GRID.maxK.
70
+ */
71
+ export function gridScale(layouts, keyOf, { aspect = GRID.aspect, maxK = GRID.maxK } = {}) {
72
+ let k = maxK;
73
+ for (const L of layouts) {
74
+ for (const [e, list] of membersOf(L, keyOf)) {
75
+ const r = innerOf(L.boxes.get(e));
76
+ const f = bestFit(r.w, r.h, list.length, aspect);
77
+ if (f > 0) k = Math.min(k, f);
78
+ }
79
+ }
80
+ return k;
81
+ }
82
+
83
+ /** A card's size in pitches for cell height `k`. */
84
+ export function cardSize(k, { aspect = GRID.aspect, gap = GRID.gap } = {}) {
85
+ return { w: k * (aspect - gap), h: k * (1 - gap) };
86
+ }
87
+
88
+ /**
89
+ * Each resource's card centre in a layout, in pitches: its estate's
90
+ * resources in rows of cells `aspect * k` wide and `k` high, centred in the
91
+ * box's inner rect, in unit order.
92
+ */
93
+ export function gridSlots(layout, k, keyOf, { aspect = GRID.aspect } = {}) {
94
+ const out = new Map();
95
+ if (!(k > 0)) return out;
96
+ const cw = aspect * k;
97
+ for (const [e, list] of membersOf(layout, keyOf)) {
98
+ const r = innerOf(layout.boxes.get(e));
99
+ const n = list.length;
100
+ const cols = Math.max(1, Math.min(n, Math.floor(r.w / cw + 1e-9)));
101
+ const rows = Math.ceil(n / cols);
102
+ const x0 = r.x + (r.w - cols * cw) / 2;
103
+ const y0 = r.y + Math.max(0, (r.h - rows * k) / 2);
104
+ list.forEach((a, i) => out.set(a, { x: x0 + ((i % cols) + 0.5) * cw, y: y0 + (Math.floor(i / cols) + 0.5) * k }));
105
+ }
106
+ return out;
107
+ }
108
+
109
+ /** The unit-order key of a roster row, for {@link membersOf}. */
110
+ export function unitKeyOf(rosterBy) {
111
+ return (a) => {
112
+ const r = rosterBy.get(a);
113
+ return unitOf(r ? r.address : a, r ? r.type : "");
114
+ };
115
+ }
116
+
117
+ /**
118
+ * The type as a card shows it: without its provider's prefix
119
+ * (`aws_iam_role` is `iam_role`), since every card on the stage is the same
120
+ * provider's. `terraform_data` and the like keep theirs, and a type that is
121
+ * all prefix stays whole.
122
+ */
123
+ export function shortType(type) {
124
+ const t = String(type || "");
125
+ const m = /^([a-z0-9]+)_(.+)$/.exec(t);
126
+ if (!m || m[1] === "terraform") return t;
127
+ return m[2];
128
+ }
129
+
130
+ /**
131
+ * What a roster row's card says: the short address (the resource's name and
132
+ * index, without its module path or type) and the type without its
133
+ * provider's prefix. A live dot's id may carry behold's own `#2` suffix (two
134
+ * rows with one address); that is not the roster's, so it is not shown.
135
+ */
136
+ export function cardLabel(row) {
137
+ const address = String((row && row.address) || "").replace(/#\d+$/, "");
138
+ const parts = parseAddress(address);
139
+ const name = parts.name ? `${parts.name}${parts.index}` : address;
140
+ return { name, type: shortType((row && row.type) || (parts.name ? parts.type : "")), address };
141
+ }
142
+
143
+ /**
144
+ * The card width, in CSS px, at which the card level starts: wide enough for
145
+ * the longest name at the name's face and the longest type at the type's,
146
+ * clamped to [minW, maxW]. A name still wider than that is cut from the front.
147
+ * `measure(text, px, weight)` is the text's width.
148
+ */
149
+ export function cardNeed(labels, measure) {
150
+ const t = CARD_TEXT;
151
+ let w = 0;
152
+ for (const l of labels) {
153
+ w = Math.max(w, measure(l.name, t.name, 600));
154
+ if (l.type) w = Math.max(w, measure(l.type, t.type, 400));
155
+ }
156
+ return Math.max(t.minW, Math.min(t.maxW, Math.ceil(w + t.pad * 2 + t.bar + 2)));
157
+ }
158
+
159
+ /** The level of detail for roster cards: a card at `need` px, a dot at the same share of it a card's dot is. */
160
+ export function rosterLod(need) {
161
+ return { ...LOD, cardPx: need, dotPx: (need * LOD.dotPx) / LOD.cardPx };
162
+ }
163
+
164
+ /**
165
+ * `text` cut from the front until `measure(text)` fits `room`, with an
166
+ * ellipsis where the cut is: the end of a name is the part that tells two
167
+ * apart. Returns "" when not even the ellipsis and one character fit.
168
+ */
169
+ export function fitFront(text, room, measure) {
170
+ const s = String(text || "");
171
+ if (!s || !(room > 0)) return "";
172
+ if (measure(s) <= room) return s;
173
+ let lo = 1;
174
+ let hi = s.length - 1;
175
+ let best = "";
176
+ while (lo <= hi) {
177
+ const mid = (lo + hi) >> 1;
178
+ const cand = `…${s.slice(s.length - mid)}`;
179
+ if (measure(cand) <= room) {
180
+ best = cand;
181
+ lo = mid + 1;
182
+ } else hi = mid - 1;
183
+ }
184
+ return best;
185
+ }
186
+
187
+ /**
188
+ * A type too wide for its line, on two lines broken after an underscore,
189
+ * each fitting `room`; null when no break fits.
190
+ */
191
+ export function wrapType(text, room, measure) {
192
+ const s = String(text || "");
193
+ let best = null;
194
+ for (let i = s.indexOf("_"); i > 0; i = s.indexOf("_", i + 1)) {
195
+ const a = s.slice(0, i + 1);
196
+ const b = s.slice(i + 1);
197
+ if (!b || measure(a) > room || measure(b) > room) continue;
198
+ // The break nearest the middle.
199
+ if (!best || Math.abs(a.length - b.length) < Math.abs(best[0].length - best[1].length)) best = [a, b];
200
+ }
201
+ return best;
202
+ }
203
+
204
+ /**
205
+ * The form a resource is drawn in, from `u`: 0 is its dot, 1 its card. The
206
+ * dot's disc grows into the card's rounded rect about the same centre. Sizes
207
+ * are in screen px.
208
+ */
209
+ export function cardShape(cx, cy, u, { dotR, cardW, cardH }) {
210
+ const k = Math.max(0, Math.min(1, u));
211
+ const w = lerp(dotR * 2, Math.max(dotR * 2, cardW), k);
212
+ const h = lerp(dotR * 2, Math.max(dotR * 2, cardH), k);
213
+ const r = lerp(dotR, Math.min(6, h * 0.22), k);
214
+ return { x: cx - w / 2, y: cy - h / 2, w, h, r };
215
+ }
216
+
217
+ /** How far a card's words have faded in at morph `u`: none until most of the way, so a growing dot never shows half-sized text. */
218
+ export function cardTextAlpha(u) {
219
+ return smooth((u - 0.6) / 0.35);
220
+ }
221
+
222
+ /**
223
+ * The card's text, laid out for a card `w` x `h` px whose card level starts
224
+ * at `need` px: the name, then the type in muted ink. The faces are their
225
+ * sizes at `need` and grow with the card, so whatever fits at `need` fits at
226
+ * every size past it. A type too wide goes onto two lines when the card is
227
+ * tall enough, and is cut from the front when it is not; a name too wide is
228
+ * cut from the front. The words never shrink below their faces: a line the
229
+ * card has no room for is left out. `measure(text, px, weight)` is the
230
+ * text's width.
231
+ */
232
+ export function cardText(label, { w, h, need = w }, measure) {
233
+ const t = CARD_TEXT;
234
+ const grow = Math.max(1, w / Math.max(1, need));
235
+ const fs = Math.min(t.max, t.name * grow);
236
+ const ft = Math.min(t.max - 1, t.type * grow);
237
+ const room = w - t.pad * 2 - t.bar;
238
+ const lines = [];
239
+ if (room < fs * 2 || h < fs * 1.3 + 2) return lines;
240
+ const name = fitFront(label.name, room, (s) => measure(s, fs, 600));
241
+ if (name) lines.push({ text: name, size: fs, weight: 600, ink: "fg" });
242
+ const lineH = (n) => (fs + ft * n) * 1.25 + 6;
243
+ if (label.type && h >= lineH(1)) {
244
+ const m = (s) => measure(s, ft, 400);
245
+ const two = m(label.type) > room && h >= lineH(2) ? wrapType(label.type, room, m) : null;
246
+ if (two) for (const part of two) lines.push({ text: part, size: ft, weight: 400, ink: "muted" });
247
+ else {
248
+ const type = fitFront(label.type, room, m);
249
+ if (type) lines.push({ text: type, size: ft, weight: 400, ink: "muted" });
250
+ }
251
+ }
252
+ return lines;
253
+ }
254
+
255
+ /**
256
+ * Which drawn mark a point is on: a card when the point is inside its rect
257
+ * (with `slack` px round it), else the nearest dot within `within` px. `pw`
258
+ * and `ph` are each mark's half width and height on screen, 0 for a dot.
259
+ * Returns the index, or -1.
260
+ */
261
+ export function pickMark(x, y, { px, py, pvis, pw, ph }, { within = 10, slack = 2 } = {}) {
262
+ let best = -1;
263
+ let bd = within * within;
264
+ let inside = -1;
265
+ let insideD = Infinity;
266
+ for (let i = 0; i < px.length; i++) {
267
+ if (!pvis[i]) continue;
268
+ const dx = px[i] - x;
269
+ const dy = py[i] - y;
270
+ const d = dx * dx + dy * dy;
271
+ if (pw && pw[i] > 0 && Math.abs(dx) <= pw[i] + slack && Math.abs(dy) <= ph[i] + slack) {
272
+ // Two cards that meet: the one whose centre is nearer.
273
+ if (d < insideD) {
274
+ insideD = d;
275
+ inside = i;
276
+ }
277
+ continue;
278
+ }
279
+ if (d < bd) {
280
+ bd = d;
281
+ best = i;
282
+ }
283
+ }
284
+ return inside >= 0 ? inside : best;
285
+ }
286
+
287
+ /**
288
+ * The rail's Cards mark: how many cards can draw at the card level, and its
289
+ * words. `own`: the canvas draws the roster's cards (the page's SVG has none
290
+ * for these resources), so every resource has one. Else the cards are the
291
+ * SVG's: the resources it has a card for (`seeded`), or, when it has a card
292
+ * for none of them, the cards it draws of its own (`svgCards`, a live carve's
293
+ * roots).
294
+ */
295
+ export function cardsMark({ resources = 0, seeded = 0, svgCards = 0, own = false } = {}) {
296
+ const count = own ? resources : seeded > 0 ? seeded : svgCards;
297
+ const n = count.toLocaleString("en-US");
298
+ return { count, words: count === 0 ? "no cards" : `${n} card${count === 1 ? "" : "s"}`, enabled: count > 0 };
299
+ }
300
+
301
+ /**
302
+ * Which estate the Cards mark lands on: the director's estate when it holds
303
+ * resources, else the source estate when it does, else the estate holding
304
+ * the most. null when no estate holds any.
305
+ */
306
+ export function cardsEstate(focusEstate, source, counts) {
307
+ if (focusEstate && (counts.get(focusEstate) ?? 0) > 0) return focusEstate;
308
+ if ((counts.get(source) ?? 0) > 0) return source;
309
+ let best = null;
310
+ for (const [e, n] of counts) if (n > 0 && (best === null || n > counts.get(best))) best = e;
311
+ return best;
312
+ }
313
+
314
+ /** The card nearest the middle of a set of cards: where the Cards mark centres, so it always lands on one. */
315
+ export function middleCard(points) {
316
+ if (!points.length) return null;
317
+ let cx = 0;
318
+ let cy = 0;
319
+ for (const p of points) {
320
+ cx += p.x;
321
+ cy += p.y;
322
+ }
323
+ cx /= points.length;
324
+ cy /= points.length;
325
+ let best = points[0];
326
+ let bd = Infinity;
327
+ for (const p of points) {
328
+ const d = (p.x - cx) ** 2 + (p.y - cy) ** 2;
329
+ if (d < bd) {
330
+ bd = d;
331
+ best = p;
332
+ }
333
+ }
334
+ return best;
335
+ }
336
+
337
+ /** A viewBox `w` wide centred on a point, at the pane's aspect (height / width). */
338
+ export function cardsFrame(at, w, aspect) {
339
+ const h = w * aspect;
340
+ return [at.x - w / 2, at.y - h / 2, w, h];
341
+ }
342
+
343
+ /**
344
+ * How far in the zoom may go while the canvas draws the roster's cards: a
345
+ * viewBox no narrower than the one where a card is `past` times the width at
346
+ * which it is fully drawn. `paneW` px, `cardW` in viewBox units.
347
+ */
348
+ export function minViewWidth(paneW, cardW, need, past = 1.8) {
349
+ return (paneW * cardW) / (need * past);
350
+ }
351
+
352
+ /**
353
+ * Where a tip goes beside a point: right of and below it, kept inside the
354
+ * pane less the rail on its right (`rightInset`), and over the mark when the
355
+ * pane's foot leaves no room under it.
356
+ */
357
+ export function tipSpot(x, y, { tipW, tipH, paneW, paneH, rightInset = 0, gap = 12, below = 14, above = y }) {
358
+ const left = Math.max(4, Math.min(x + gap, paneW - rightInset - tipW - 4));
359
+ // No room under the mark: over it, from `above` (the mark's top edge).
360
+ const top = y + below + tipH > paneH - 4 ? Math.max(4, above - 6 - tipH) : y + below;
361
+ return { left, top };
362
+ }