clavarium 0.2.23 → 0.2.24-next.2

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/web/links.js CHANGED
@@ -1,9 +1,10 @@
1
1
  /* THE MAP (once "Who gets what"), AND FLOWS: the first tab of Access, with
2
2
  * its Map / Flows switch (app.js opens, closes and filters Access).
3
3
  *
4
- * Map devices, conversations and assets in columns that each
5
- * scroll on their own; a link is a right, coloured by what
6
- * it gives. At rest no line, a count on each box; a box
4
+ * Map devices, conversations and assets in columns, laid out
5
+ * whole and fitted to the screen, with a zoom of its own
6
+ * (pinch, ctrl + wheel, the buttons; moved by a drag once
7
+ * zoomed in); a link is a right, coloured by what it gives. At rest no line, a count on each box; a box
7
8
  * touched shows its links, greys the rest, and says them in
8
9
  * words. A link is made by dragging from a dot (or a box
9
10
  * touched, then another), changed by the knob on it, taken
@@ -27,8 +28,6 @@
27
28
  /* An icon of that set, or, until it is there, nothing in its place. */
28
29
  const ic = (name, cls = 'lk-svg') => window.clavariumIcons?.icon(name, cls) ?? null;
29
30
  const kindIcon = (kind) => h('span', { class: 'lk-ic', 'aria-hidden': 'true' }, ic(KIND_ICON[kind]));
30
- /* A pill that points up or down: its arrow drawn, then its words. */
31
- const arrowed = (pill, dir, text) => { pill.replaceChildren(...[ic(dir === 'up' ? 'arrow-up' : 'arrow-down', 'lk-arrow')].filter(Boolean), text); return pill; };
32
31
  const KIND_WORD = { device: 'device', conversation: 'conversation', folder: 'folder', git: 'repository', mailbox: 'mailbox', secret: 'secret' };
33
32
  const COLUMN = { device: 'device', conversation: 'conversation', folder: 'asset', git: 'asset', mailbox: 'asset', secret: 'asset' };
34
33
  const TITLES = { device: 'Devices', conversation: 'Conversations', asset: 'Assets' };
@@ -189,7 +188,7 @@
189
188
  legend();
190
189
  const bd = board();
191
190
  bd.className = `lk-board ${VIEWS[view].cols.length === 3 ? 'three' : 'two'}`;
192
- bd.querySelectorAll('.lk-col, .lk-knob, .lk-pop, .lk-edge').forEach((e) => e.remove()); pop = null; popLink = null;
191
+ bd.querySelectorAll('.lk-col, .lk-knob').forEach((e) => e.remove()); closePop();
193
192
  const lists = layout();
194
193
  VIEWS[view].cols.forEach((cols, ci) => {
195
194
  const col = h('div', { class: 'lk-col' });
@@ -200,10 +199,9 @@
200
199
  for (const n of list) col.append(nodeEl(n));
201
200
  if (!list.length) col.append(empty(k));
202
201
  });
203
- col.addEventListener('scroll', () => { place(); draw(); }, { passive: true });
204
202
  bd.append(col);
205
203
  });
206
- draw(); hint(); paintPhone();
204
+ fit(); draw(); hint(); paintPhone();
207
205
  }
208
206
  /* How each kind is made: a device paired, a conversation created, an asset added. The same,
209
207
  from the small round "+" by each column's title (always there, however many it holds)
@@ -220,9 +218,12 @@
220
218
  }
221
219
  function nodeEl(n) {
222
220
  const cnt = graph.links.filter((l) => (l.from === n.id || l.to === n.id) && shown(l)).length;
223
- const e = h('div', { class: `lk-node lk-c-${COLUMN[n.kind]}${n.pending ? ' pending' : ''}`, 'data-id': n.id, 'data-kind': n.kind, tabindex: 0, role: 'button', 'aria-label': `${n.name}, ${n.sub}, ${cnt} link${cnt === 1 ? '' : 's'}` },
221
+ /* One line: the icon says the kind; its words in the title. A conversation says by a dot its chat on, or that it is not ready. */
222
+ const state = n.kind !== 'conversation' ? null : n.phase && n.phase !== 'ready' ? 'off' : n.chat ? 'chat' : null;
223
+ const e = h('div', { class: `lk-node lk-c-${COLUMN[n.kind]}${n.pending ? ' pending' : ''}`, 'data-id': n.id, 'data-kind': n.kind, tabindex: 0, role: 'button', title: `${n.name} · ${n.sub}`, 'aria-label': `${n.name}, ${n.sub}, ${cnt} link${cnt === 1 ? '' : 's'}` },
224
224
  kindIcon(n.kind),
225
- h('span', { class: 'lk-tx' }, h('span', { class: 'lk-nm', text: n.name }), h('span', { class: 'lk-sb', text: n.sub })),
225
+ h('span', { class: 'lk-tx' }, h('span', { class: 'lk-nm', text: n.name })),
226
+ state && h('span', { class: 'lk-st', 'data-state': state, 'aria-hidden': 'true' }),
226
227
  h('span', { class: 'lk-cnt', text: String(cnt), title: `${cnt} link${cnt === 1 ? '' : 's'}` }));
227
228
  if (VIEWS[view].sources.includes(n.kind) && !n.pending) {
228
229
  const dot = h('span', { class: 'lk-handle', title: 'Drag to what it gets', 'aria-hidden': 'true' });
@@ -236,14 +237,15 @@
236
237
  return e;
237
238
  }
238
239
  const el = (id) => board().querySelector(`.lk-node[data-id="${CSS.escape(id)}"]`);
239
- /* Where a link meets a box; a box scrolled out of its column is met at the column's edge. */
240
+ /* A box's place on the board, in the board's own terms (before its zoom). */
241
+ function local(e) {
242
+ const r = e.getBoundingClientRect(); const b = board().getBoundingClientRect(); const k = zoom.k;
243
+ return { left: (r.left - b.left) / k, right: (r.right - b.left) / k, top: (r.top - b.top) / k, bottom: (r.bottom - b.top) / k };
244
+ }
245
+ /* Where a link meets a box. */
240
246
  function anchor(id, side) {
241
- const n = el(id); const col = n.closest('.lk-col');
242
- const r = n.getBoundingClientRect(); const c = col.getBoundingClientRect(); const b = board().getBoundingClientRect();
243
- const top = c.top + 34; const bottom = c.bottom - 2;
244
- let y = r.top + r.height / 2; let away = 0;
245
- if (y < top) { y = top; away = -1; } else if (y > bottom) { y = bottom; away = 1; }
246
- return { x: (side === 'out' ? r.right : r.left) - b.left, y: y - b.top, away, col };
247
+ const r = local(el(id));
248
+ return { x: side === 'out' ? r.right : r.left, y: (r.top + r.bottom) / 2 };
247
249
  }
248
250
  /* Everything a box reaches or is reached by, in this view. */
249
251
  function related(id) {
@@ -282,16 +284,14 @@
282
284
  return { d: `M${a.x},${a.y} L${t},${a.y} C${t + g * 0.35},${a.y} ${z.x - g * 0.3},${z.y} ${z.x},${z.y}` };
283
285
  }
284
286
  /* The free gaps of the middle column, top to bottom, in the board's terms. */
285
- function gaps(col, b) {
286
- const c = col.getBoundingClientRect(); const out = [];
287
- let at = c.top + 34 - b.top;
288
- for (const n of [...col.querySelectorAll('.lk-node')].map((x) => x.getBoundingClientRect()).sort((x, y) => x.top - y.top)) {
289
- if (n.bottom - b.top < at) continue;
290
- if (n.top - b.top > at + 2) out.push([at, Math.min(n.top - b.top, c.bottom - b.top)]);
291
- at = Math.max(at, n.bottom - b.top);
292
- if (at > c.bottom - b.top) break;
287
+ function gaps(col) {
288
+ const c = local(col); const out = [];
289
+ let at = c.top + 34;
290
+ for (const n of [...col.querySelectorAll('.lk-node')].map(local).sort((x, y) => x.top - y.top)) {
291
+ if (n.top > at + 2) out.push([at, n.top]);
292
+ at = Math.max(at, n.bottom);
293
293
  }
294
- if (at < c.bottom - b.top - 2) out.push([at, c.bottom - b.top]);
294
+ if (at < c.bottom - 2) out.push([at, c.bottom]);
295
295
  return out;
296
296
  }
297
297
  function draw() {
@@ -302,14 +302,13 @@
302
302
  const many = allLines && !drag && ls.length > MANY;
303
303
  bd.classList.toggle('simple', !allLines && !drag);
304
304
  bd.classList.toggle('many', many);
305
- const b = bd.getBoundingClientRect();
306
- const colEls = [...bd.querySelectorAll('.lk-col')]; const cols = colEls.map((c) => c.getBoundingClientRect());
307
- const middle = cols.length === 3 ? { left: cols[1].left - b.left, right: cols[1].right - b.left, free: gaps(colEls[1], b) } : null;
308
- svg.setAttribute('viewBox', `0 0 ${b.width} ${b.height}`);
305
+ const colEls = [...bd.querySelectorAll('.lk-col')]; const cols = colEls.map(local);
306
+ const middle = cols.length === 3 ? { left: cols[1].left, right: cols[1].right, free: gaps(colEls[1]) } : null;
307
+ svg.setAttribute('viewBox', `0 0 ${bd.offsetWidth} ${bd.offsetHeight}`);
309
308
  /* No arrowheads: the colour of a line says the right. */
310
309
  svg.replaceChildren();
311
- bd.querySelectorAll('.lk-knob, .lk-edge, .lk-hook').forEach((c) => c.remove());
312
- const edges = new Map(); const knobs = []; const wanted = []; const hooks = new Map();
310
+ bd.querySelectorAll('.lk-knob').forEach((c) => c.remove());
311
+ const knobs = []; const wanted = [];
313
312
  /* The pale ones first: the links in colour are drawn over them. */
314
313
  const layers = [s('g', { class: 'lk-pale' }), s('g')];
315
314
  svg.append(...layers);
@@ -318,26 +317,13 @@
318
317
  const off = rel && !(rel.has(l.from) && rel.has(l.to));
319
318
  const on = rel && !off;
320
319
  const pale = many && !on;
321
- const away = a.away || z.away;
322
- for (const [p, end] of [[a, l.from], [z, l.to]]) if (p.away && !off) { const e = edges.get(p.col) ?? { up: [], down: [] }; (p.away < 0 ? e.up : e.down).push(end); edges.set(p.col, e); }
323
- /* Gathered, a link to a box out of sight is a short hook at the end in sight, bent the way the
324
- other end is; the pill at the column's edge says how many. */
325
- if (pale && away) {
326
- /* Its other end hidden on the left: counted by the pill of that column only. */
327
- if (a.away || off) continue;
328
- const out = Boolean(z.away); const dir = a.away || z.away; const at = out ? a : z;
329
- const key = `${out ? l.from : l.to}|${dir}`;
330
- const g = hooks.get(key) ?? { box: out ? l.from : l.to, out, dir, x: at.x, y: at.y, room: z.x - a.x, k, ids: [] };
331
- g.ids.push(out ? l.to : l.from); hooks.set(key, g);
332
- continue;
333
- }
334
320
  const dash = node(l.from).kind === 'device' && COLUMN[node(l.to).kind] === 'asset';
335
- const over = dash && middle && !away ? middle : null;
321
+ const over = dash && middle ? middle : null;
336
322
  const r = route(a, z, over, many);
337
- const p = s('path', { d: r.d, class: [dash ? 'dash' : '', away ? 'away' : '', off ? 'dim' : '', on ? 'on' : '', pale ? 'pale' : '', l.pending ? 'later' : ''].join(' '), 'data-from': l.from, 'data-to': l.to, 'data-kind': k });
323
+ const p = s('path', { d: r.d, class: [dash ? 'dash' : '', off ? 'dim' : '', on ? 'on' : '', pale ? 'pale' : '', l.pending ? 'later' : ''].join(' '), 'data-from': l.from, 'data-to': l.to, 'data-kind': k });
338
324
  p.style.stroke = css(k);
339
325
  layers[pale ? 0 : 1].append(p);
340
- if (away || pale || off) continue;
326
+ if (pale || off) continue;
341
327
  const hit = s('path', { d: r.d, class: 'hit' }); hit.addEventListener('click', () => openPop(l)); layers[1].append(hit);
342
328
  const spots = (r.knob ? [0.5, 0.3, 0.7, 0.2, 0.82].map(r.knob) : [0.68, 0.55, 0.8, 0.45, 0.88, 0.38].map((f) => p.getPointAtLength(p.getTotalLength() * f)));
343
329
  wanted.push({ l, k, off, on, spots, late: Boolean(r.knob) });
@@ -353,45 +339,6 @@
353
339
  kb.addEventListener('click', () => openPop(l));
354
340
  bd.append(kb);
355
341
  }
356
- /* A box whose links go to boxes out of sight: a short hook bent the way they are, and a small
357
- pill naming them; touched, it brings the first into sight. */
358
- const taken = [];
359
- for (const g of hooks.values()) {
360
- const len = Math.min(40, g.room * 0.3); const names = [...new Set(g.ids)].map((id) => node(id).name);
361
- const d = g.out ? `M${g.x},${g.y} L${g.x + len * 0.5},${g.y} Q${g.x + len},${g.y} ${g.x + len},${g.y + g.dir * 12}` : `M${g.x - len},${g.y + g.dir * 12} Q${g.x - len},${g.y} ${g.x - len * 0.5},${g.y} L${g.x},${g.y}`;
362
- const hook = s('path', { d, class: 'pale hook' }); hook.style.stroke = css(g.k); layers[0].append(hook);
363
- const pill = h('button', { type: 'button', class: `lk-hook ${g.out ? 'out' : 'in'}`, title: `${g.dir < 0 ? 'Above' : 'Below'}: ${names.join(', ')}`,
364
- style: `${g.out ? `left:${g.x + 4}px` : `left:${g.x - 4}px`};top:${g.y + g.dir * 22}px;max-width:${Math.max(48, Math.min(170, g.room - 24))}px` });
365
- arrowed(pill, g.dir < 0 ? 'up' : 'down', `${names[0]}${names.length > 1 ? ` +${names.length - 1}` : ''}`);
366
- pill.addEventListener('click', () => { el([...g.ids].find((id) => el(id)))?.scrollIntoView({ block: 'center', behavior: 'smooth' }); });
367
- bd.append(pill);
368
- /* A name cut short says nothing: where it does not fit, the count, the names in its title. */
369
- if (pill.scrollWidth > pill.clientWidth + 1) {
370
- /* No room in the gap: inside its box, at the end of its second line, where there is. */
371
- const r = el(g.box)?.getBoundingClientRect();
372
- if (r) { pill.classList.add('inbox'); pill.style.left = `${r.right - b.left - 20}px`; pill.style.top = `${r.bottom - b.top - 13}px`; pill.style.maxWidth = `${Math.max(48, r.width - 100)}px`; }
373
- if (pill.scrollWidth > pill.clientWidth + 1) arrowed(pill, g.dir < 0 ? 'up' : 'down', String(names.length));
374
- }
375
- /* Never over another pill or a knob: moved further the same way, or left out (the hook stays). */
376
- const clear = () => { const r = pill.getBoundingClientRect(); return [...taken, ...knobs.map((q) => ({ left: q.x + b.left - 11, right: q.x + b.left + 11, top: q.y + b.top - 11, bottom: q.y + b.top + 11 }))].every((o) => r.right < o.left || r.left > o.right || r.bottom < o.top || r.top > o.bottom); };
377
- let tries = 0;
378
- while (!clear() && tries < 2) { tries += 1; pill.style.top = `${g.y + g.dir * (22 + tries * 20)}px`; }
379
- if (clear()) taken.push(pill.getBoundingClientRect()); else pill.remove();
380
- }
381
- /* Links to boxes scrolled away: a pill says how many, and brings the first into sight.
382
- Above: in the column's title band; below: in the strip under the column. Never over a name. */
383
- for (const [col, e] of edges) {
384
- const c = col.getBoundingClientRect();
385
- for (const [dir, ids] of [['up', e.up], ['down', e.down]]) {
386
- if (!ids.length) continue;
387
- const n = new Set(ids).size;
388
- const pill = h('button', { type: 'button', class: `lk-edge ${dir}`, title: `${n} linked box${n === 1 ? '' : 'es'} ${dir === 'up' ? 'above' : 'below'}: bring into sight`,
389
- style: dir === 'up' ? `left:${c.right - b.left - 14}px;top:${c.top - b.top + 8}px` : `left:${c.left - b.left + c.width / 2}px;top:${c.bottom - b.top + 4}px` });
390
- arrowed(pill, dir, `${n} more linked`);
391
- pill.addEventListener('click', () => { el(ids[0])?.scrollIntoView({ block: 'center', behavior: 'smooth' }); });
392
- bd.append(pill);
393
- }
394
- }
395
342
  if (drag?.at) svg.append(s('path', { d: curve(anchor(drag.from.id, 'out'), drag.at), class: 'temp' }));
396
343
  const src = picked || drag?.from;
397
344
  bd.querySelectorAll('.lk-node').forEach((e) => {
@@ -410,29 +357,127 @@
410
357
  const again = pinned === n.id;
411
358
  pinned = again ? null : n.id;
412
359
  picked = !again && VIEWS[view].sources.includes(n.kind) && !n.pending ? n : null;
413
- closePop(); hint(); reveal(); draw();
360
+ closePop(); hint(); draw(); reveal();
414
361
  }
415
- /* A box touched: what it is linked to, scrolled out of sight in another column, is brought into it. */
362
+ /* A box touched: it and what it is linked to are brought into the map's window (its card above takes room). */
416
363
  function reveal() {
417
364
  if (!pinned || phone() || !el(pinned)) return;
418
- const rel = related(pinned); const mine = el(pinned).closest('.lk-col');
419
- for (const col of board().querySelectorAll('.lk-col')) {
420
- if (col === mine) continue;
421
- const ns = [...col.querySelectorAll('.lk-node')].filter((e) => rel.has(e.dataset.id));
422
- if (!ns.length) continue;
423
- const c = col.getBoundingClientRect(); const rs = ns.map((e) => e.getBoundingClientRect());
424
- if (rs.every((r) => r.top >= c.top + 30 && r.bottom <= c.bottom)) continue;
425
- const top = Math.min(...rs.map((r) => r.top)); const bottom = Math.max(...rs.map((r) => r.bottom));
426
- col.scrollTop += bottom - top < c.height - 50 ? (top + bottom) / 2 - (c.top + c.bottom) / 2 : top - c.top - 40;
365
+ const rel = related(pinned); const sc = scroller(); const H = sc.clientHeight - BAR_H; const W = sc.clientWidth;
366
+ const rs = [...board().querySelectorAll('.lk-node')].filter((e) => rel.has(e.dataset.id)).map(local);
367
+ const me = local(el(pinned));
368
+ const top = Math.min(...rs.map((r) => r.top)) * zoom.k; const bottom = Math.max(...rs.map((r) => r.bottom)) * zoom.k;
369
+ const left = Math.min(...rs.map((r) => r.left)) * zoom.k; const right = Math.max(...rs.map((r) => r.right)) * zoom.k;
370
+ const inY = (y) => top + y >= 0 && bottom + y <= H; const inX = (x) => left + x >= 0 && right + x <= W;
371
+ if (!inY(zoom.y)) zoom.y = bottom - top <= H ? 8 - top + (H - 16 - (bottom - top)) / 2 : 24 - me.top * zoom.k;
372
+ if (!inX(zoom.x)) zoom.x = right - left <= W ? (W - (right - left)) / 2 - left : 24 - me.left * zoom.k;
373
+ apply();
374
+ }
375
+ /* ---------- the map's own zoom: a transform of the board alone, never the page's text size ---------- */
376
+ /* `fit`: the whole board in sight, as at first; zoomed or moved by hand, it stays as it was put. */
377
+ const zoom = { k: 1, x: 0, y: 0, fit: true, min: 1 };
378
+ const MAX_ZOOM = 2.5;
379
+ const scroller = () => $('links-scroller');
380
+ /* Natural width: columns of their own width; wider when the screen has the room. */
381
+ const COL_W = 236; const COL_GAP = 96;
382
+ /* Room kept under the board for the zoom's buttons, fitted. */
383
+ const BAR_H = 46;
384
+ /* Moved or zoomed, kept inside its window: smaller than it, at its top, centred across. */
385
+ function apply() {
386
+ const sc = scroller(); const bd = board(); const W = sc.clientWidth; const H = sc.clientHeight;
387
+ zoom.k = Math.min(MAX_ZOOM, Math.max(zoom.min, zoom.k));
388
+ const w = bd.offsetWidth * zoom.k; const hh = bd.offsetHeight * zoom.k;
389
+ zoom.x = w <= W ? (W - w) / 2 : Math.min(0, Math.max(W - w, zoom.x));
390
+ zoom.y = hh <= H ? 0 : Math.min(0, Math.max(H - hh, zoom.y));
391
+ bd.style.transform = `translate(${zoom.x}px, ${zoom.y}px) scale(${zoom.k})`;
392
+ /* The dots to drag from and the knobs keep their size on the screen, zoomed out. */
393
+ bd.style.setProperty('--lk-inv', String(Math.min(2, Math.max(1, 1 / zoom.k))));
394
+ sc.classList.toggle('zoomed', w > W + 1 || hh > H + 1);
395
+ const bar = sc.querySelector('.lk-zoom');
396
+ if (bar) {
397
+ bar.querySelector('[data-zoom="out"]').disabled = zoom.k <= zoom.min + 0.001;
398
+ bar.querySelector('[data-zoom="in"]').disabled = zoom.k >= MAX_ZOOM - 0.001;
399
+ bar.querySelector('[data-zoom="fit"]').disabled = zoom.fit;
427
400
  }
401
+ place();
428
402
  }
429
- /* The board takes the height left on the screen: as many boxes in sight as can be. */
403
+ /* Zoomed by `f` around a point of the window (its middle by default). */
404
+ function zoomBy(f, cx, cy) {
405
+ const sc = scroller(); const x = cx ?? sc.clientWidth / 2; const y = cy ?? sc.clientHeight / 2;
406
+ const k = Math.min(MAX_ZOOM, Math.max(zoom.min, zoom.k * f));
407
+ if (k === zoom.k) return;
408
+ zoom.x = x - ((x - zoom.x) * k) / zoom.k; zoom.y = y - ((y - zoom.y) * k) / zoom.k; zoom.k = k; zoom.fit = false;
409
+ apply();
410
+ }
411
+ /* The board takes the height left on the screen, and the whole of it is fitted there. */
430
412
  function fit() {
431
- const bd = board(); const a = $('access');
413
+ const sc = scroller(); const bd = board(); const a = $('access');
432
414
  if (phone() || !shownNow()) return;
433
- const top = bd.getBoundingClientRect().top - a.getBoundingClientRect().top + a.scrollTop;
434
- const want = `${Math.round(Math.max(320, a.clientHeight - top - 20))}px`;
435
- if (bd.style.height !== want) { bd.style.height = want; requestAnimationFrame(() => draw()); }
415
+ const n = VIEWS[view].cols.length;
416
+ const natural = n * COL_W + (n - 1) * COL_GAP + 36;
417
+ const top = sc.getBoundingClientRect().top - a.getBoundingClientRect().top + a.scrollTop;
418
+ const room = Math.round(Math.max(320, a.clientHeight - top - 20));
419
+ /* The card of a box touched takes room above: the scale stays the one without it, the map moves under it. */
420
+ const card = $('links-card'); const cardH = card.hidden ? 0 : card.offsetHeight + 10;
421
+ const W = sc.clientWidth;
422
+ bd.style.width = `${natural}px`;
423
+ const bh = bd.offsetHeight;
424
+ const kh = Math.min(1, (room + cardH - BAR_H) / bh);
425
+ const width = Math.max(natural, W / kh);
426
+ bd.style.width = `${Math.floor(width)}px`;
427
+ const k = Math.min(kh, W / width);
428
+ zoom.min = Math.min(k, 1);
429
+ sc.style.height = `${Math.max(160, Math.min(room, Math.ceil(bh * k) + BAR_H))}px`;
430
+ if (zoom.fit) { zoom.k = k; zoom.x = 0; zoom.y = 0; }
431
+ apply();
432
+ }
433
+ function zoomBar() {
434
+ const b = (z, text, label, onclick) => h('button', { type: 'button', 'data-zoom': z, id: `links-zoom-${z}`, 'aria-label': label, title: label, text, onclick });
435
+ return h('div', { class: 'lk-zoom', role: 'group', 'aria-label': 'Zoom of the map' },
436
+ b('out', '−', 'Zoom out', () => zoomBy(1 / 1.25)), b('in', '+', 'Zoom in', () => zoomBy(1.25)),
437
+ b('fit', 'Fit', 'See everything', () => { zoom.fit = true; fit(); }));
438
+ }
439
+ /* A finger or the mouse moves the map (zoomed in), two fingers zoom it; a box or a link touched without moving is a tap. */
440
+ function gestures() {
441
+ const sc = scroller(); const pts = new Map(); let pan = null; let pinch = null; let moved = false;
442
+ const at = (e) => { const r = sc.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; };
443
+ sc.prepend(zoomBar());
444
+ sc.addEventListener('pointerdown', (e) => {
445
+ if (e.button > 0 || e.target.closest('.lk-handle, .lk-pop, .lk-zoom')) return;
446
+ pts.set(e.pointerId, at(e));
447
+ if (pts.size === 1) { moved = false; pan = { ...at(e), x0: zoom.x, y0: zoom.y }; pinch = null; }
448
+ if (pts.size === 2) {
449
+ const [p, q] = [...pts.values()];
450
+ pinch = { d: Math.hypot(p.x - q.x, p.y - q.y) || 1, k: zoom.k, mx: (p.x + q.x) / 2, my: (p.y + q.y) / 2, x0: zoom.x, y0: zoom.y }; pan = null; moved = true;
451
+ }
452
+ });
453
+ sc.addEventListener('pointermove', (e) => {
454
+ if (!pts.has(e.pointerId) || drag) return;
455
+ const p = at(e); pts.set(e.pointerId, p);
456
+ if (pinch && pts.size >= 2) {
457
+ const [a, b] = [...pts.values()];
458
+ const k = Math.min(MAX_ZOOM, Math.max(zoom.min, (pinch.k * Math.hypot(a.x - b.x, a.y - b.y)) / pinch.d));
459
+ const mx = (a.x + b.x) / 2; const my = (a.y + b.y) / 2;
460
+ zoom.x = mx - ((pinch.mx - pinch.x0) * k) / pinch.k; zoom.y = my - ((pinch.my - pinch.y0) * k) / pinch.k; zoom.k = k; zoom.fit = false;
461
+ apply(); return;
462
+ }
463
+ if (!pan) return;
464
+ if (!moved && Math.hypot(p.x - pan.x, p.y - pan.y) < 6) return;
465
+ if (!moved) { moved = true; sc.setPointerCapture?.(e.pointerId); sc.classList.add('panning'); }
466
+ zoom.x = pan.x0 + p.x - pan.x; zoom.y = pan.y0 + p.y - pan.y; apply();
467
+ });
468
+ const up = (e) => { pts.delete(e.pointerId); if (!pts.size) { pan = null; pinch = null; sc.classList.remove('panning'); } };
469
+ sc.addEventListener('pointerup', up); sc.addEventListener('pointercancel', up);
470
+ /* Moved, not tapped: the click that follows is not a tap on a box. */
471
+ sc.addEventListener('click', (e) => { if (moved) { moved = false; e.stopPropagation(); e.preventDefault(); } }, true);
472
+ /* Ctrl (or cmd) + wheel, and a pinch on a trackpad: the map's zoom. The wheel alone moves it, zoomed in. */
473
+ sc.addEventListener('wheel', (e) => {
474
+ if (phone() || e.target.closest('.lk-pop')) return;
475
+ if (e.ctrlKey || e.metaKey) { e.preventDefault(); const p = at(e); zoomBy(Math.exp(-Math.max(-50, Math.min(50, e.deltaY)) * 0.01), p.x, p.y); return; }
476
+ if (!sc.classList.contains('zoomed')) return;
477
+ e.preventDefault(); zoom.x -= e.shiftKey ? e.deltaY : e.deltaX; zoom.y -= e.shiftKey ? 0 : e.deltaY; zoom.fit = false; apply();
478
+ }, { passive: false });
479
+ /* Never scrolled (a box reached with Tab would scroll it): the zoom alone moves the board. */
480
+ sc.addEventListener('scroll', () => { sc.scrollTop = 0; sc.scrollLeft = 0; });
436
481
  }
437
482
  function hint() {
438
483
  card();
@@ -492,17 +537,20 @@
492
537
  try { await request('/setup/drop', { id }); pinned = null; await changed(); } catch (e) { err(e.message); }
493
538
  }
494
539
 
495
- /* Dragging near a column's top or bottom scrolls it, so a target out of sight can be reached. */
540
+ /* Dragging near an edge of the map's window, zoomed in, moves the map, so a target out of sight can be reached. */
496
541
  function startDrag(ev, n) {
497
542
  ev.preventDefault(); ev.stopPropagation(); closePop(); picked = null;
498
543
  drag = { from: n, at: null, x: 0, y: 0 };
544
+ const follow = () => { const b = board().getBoundingClientRect(); drag.at = { x: (drag.x - b.left) / zoom.k, y: (drag.y - b.top) / zoom.k }; };
499
545
  const tick = () => {
500
546
  if (!drag) return;
501
- const over = document.elementFromPoint(drag.x, drag.y)?.closest('.lk-col');
502
- if (over) { const c = over.getBoundingClientRect(); if (drag.y < c.top + 50) over.scrollTop -= 8; else if (drag.y > c.bottom - 40) over.scrollTop += 8; }
547
+ const c = scroller().getBoundingClientRect(); const step = 10;
548
+ const dx = drag.x < c.left + 40 ? step : drag.x > c.right - 40 ? -step : 0;
549
+ const dy = drag.y < c.top + 40 ? step : drag.y > c.bottom - 40 ? -step : 0;
550
+ if (drag.at && (dx || dy)) { const x = zoom.x; const y = zoom.y; zoom.x += dx; zoom.y += dy; apply(); if (x !== zoom.x || y !== zoom.y) { follow(); draw(); } }
503
551
  requestAnimationFrame(tick);
504
552
  };
505
- const move = (e) => { const b = board().getBoundingClientRect(); drag.x = e.clientX; drag.y = e.clientY; drag.at = { x: e.clientX - b.left, y: e.clientY - b.top }; draw(); };
553
+ const move = (e) => { drag.x = e.clientX; drag.y = e.clientY; follow(); draw(); };
506
554
  const up = (e) => {
507
555
  removeEventListener('pointermove', move); removeEventListener('pointerup', up);
508
556
  const t = document.elementFromPoint(e.clientX, e.clientY)?.closest('.lk-node');
@@ -584,16 +632,18 @@
584
632
  if (l.pending) rm.hidden = true;
585
633
  row.append(rm, h('button', { type: 'button', class: 'primary', text: 'Done', onclick: () => { closePop(); draw(); } }));
586
634
  pop.append(row);
587
- board().append(pop); place();
635
+ /* Over the map's window, not in the board: its size is the page's, whatever the zoom. */
636
+ scroller().append(pop); place();
588
637
  /* Whole in sight: the board may reach below the screen. */
589
638
  pop.scrollIntoView({ block: 'nearest' });
590
639
  pop.querySelector('input')?.focus({ preventScroll: true });
591
640
  }
592
641
  function place() {
593
642
  if (!pop || !popLink || !el(popLink.from) || !el(popLink.to)) return;
594
- const a = anchor(popLink.from, 'out'); const z = anchor(popLink.to, 'in'); const b = board().getBoundingClientRect();
595
- const x = Math.min(Math.max(8, (a.x + z.x) / 2 - pop.offsetWidth / 2), b.width - pop.offsetWidth - 8);
596
- const y = Math.min(Math.max(8, (a.y + z.y) / 2 + 16), Math.max(8, b.height - pop.offsetHeight - 8));
643
+ const a = anchor(popLink.from, 'out'); const z = anchor(popLink.to, 'in'); const sc = scroller();
644
+ const mx = ((a.x + z.x) / 2) * zoom.k + zoom.x; const my = ((a.y + z.y) / 2) * zoom.k + zoom.y;
645
+ const x = Math.min(Math.max(8, mx - pop.offsetWidth / 2), sc.clientWidth - pop.offsetWidth - 8);
646
+ const y = Math.min(Math.max(8, my + 16), Math.max(8, sc.clientHeight - pop.offsetHeight - 8));
597
647
  pop.style.left = `${x}px`; pop.style.top = `${y}px`;
598
648
  }
599
649
 
@@ -991,7 +1041,7 @@
991
1041
  err(''); picked = null; ph.screen = 'list';
992
1042
  showPane('links');
993
1043
  await load();
994
- pinned = pin && byId.has(pin) ? pin : null;
1044
+ pinned = pin && byId.has(pin) ? pin : null; zoom.fit = true;
995
1045
  if (pinned && phone()) { ph.kind = COLUMN[node(pinned).kind]; ph.sheet = pinned; }
996
1046
  paint();
997
1047
  if (document.fonts?.ready) document.fonts.ready.then(() => draw());
@@ -1003,7 +1053,7 @@
1003
1053
  traceOf = of ?? '*';
1004
1054
  paintFlows();
1005
1055
  }
1006
- for (const b of document.querySelectorAll('#links-tabs [data-view]')) b.addEventListener('click', () => { view = b.dataset.view; picked = null; pinned = null; try { localStorage.setItem('clavarium-links-view', view); } catch { /* not kept */ } paint(); });
1056
+ for (const b of document.querySelectorAll('#links-tabs [data-view]')) b.addEventListener('click', () => { view = b.dataset.view; picked = null; pinned = null; zoom.fit = true; try { localStorage.setItem('clavarium-links-view', view); } catch { /* not kept */ } paint(); });
1007
1057
  $('links-all').addEventListener('change', () => { allLines = $('links-all').checked; try { localStorage.setItem('clavarium-links-all', allLines ? '1' : '0'); } catch { /* not kept */ } draw(); });
1008
1058
  $('flows-of').addEventListener('change', (e) => { traceOf = e.target.value; paintFlows(); });
1009
1059
  $('flows-export').addEventListener('click', () => { void exportFile(); });
@@ -1038,7 +1088,7 @@
1038
1088
  /* A click away from it closes the knob's card (a button it just replaced is not "away"). */
1039
1089
  document.addEventListener('click', (e) => { if (pop && e.target.isConnected && !e.target.closest('.lk-pop, .lk-knob, .lk-node, svg')) { closePop(); draw(); } });
1040
1090
  addEventListener('resize', () => { if (shownNow()) { legend(); paintPhone(); fit(); draw(); place(); } });
1041
- $('links-scroller').addEventListener('scroll', () => draw(), { passive: true });
1091
+ gestures();
1042
1092
  return { open, openFlows, close, changed };
1043
1093
  };
1044
1094
  })();