cornfield 0.46.1 → 0.46.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.
Files changed (52) hide show
  1. package/explorer/capped.js +3 -2
  2. package/explorer/engine/feedback.js +49 -0
  3. package/explorer/engine/flow-ui.js +39 -0
  4. package/explorer/engine/fly.js +54 -0
  5. package/explorer/engine/hint.js +97 -0
  6. package/explorer/engine/keyboard.js +156 -0
  7. package/explorer/engine/layers.js +280 -0
  8. package/explorer/engine/layout.js +378 -0
  9. package/explorer/engine/map-style.js +547 -0
  10. package/explorer/engine/piece.js +142 -0
  11. package/explorer/engine/render.js +331 -0
  12. package/explorer/engine/search-status.js +59 -0
  13. package/explorer/engine/selection.js +42 -0
  14. package/explorer/engine/sheets.js +417 -0
  15. package/explorer/engine/strings.js +63 -0
  16. package/explorer/feedback.js +11 -45
  17. package/explorer/flow-style.js +28 -0
  18. package/explorer/flows.js +10 -51
  19. package/explorer/fly.js +11 -43
  20. package/explorer/hint.js +15 -88
  21. package/explorer/index.html +38 -75
  22. package/explorer/input.js +190 -177
  23. package/explorer/instance-state.js +172 -0
  24. package/explorer/instance.js +267 -0
  25. package/explorer/keyboard.js +14 -206
  26. package/explorer/labels.js +3 -3
  27. package/explorer/layers.js +34 -445
  28. package/explorer/layout.js +14 -372
  29. package/explorer/main.js +4 -226
  30. package/explorer/map-style.js +27 -525
  31. package/explorer/mockups-model.js +3 -3
  32. package/explorer/page-pieces.js +61 -0
  33. package/explorer/page.js +232 -0
  34. package/explorer/panels.js +29 -409
  35. package/explorer/pieces/layers.js +325 -0
  36. package/explorer/pieces/node-panel.js +536 -0
  37. package/explorer/pieces/search.js +454 -0
  38. package/explorer/proposal-model.js +1 -1
  39. package/explorer/proposal.js +1 -0
  40. package/explorer/render.js +37 -267
  41. package/explorer/renderer-canvas2d.js +4 -4
  42. package/explorer/renderer-webgl.js +21 -8
  43. package/explorer/renderer.js +11 -9
  44. package/explorer/scene.js +3 -2
  45. package/explorer/search-status.js +8 -47
  46. package/explorer/search.js +26 -324
  47. package/explorer/selection-view.js +17 -19
  48. package/explorer/sheets.js +31 -379
  49. package/explorer/state.js +38 -173
  50. package/explorer/strings.js +17 -60
  51. package/explorer/view.js +4 -5
  52. package/package.json +1 -1
@@ -0,0 +1,417 @@
1
+ /**
2
+ * @module explorer/engine/sheets
3
+ *
4
+ * The phone's bottom sheets (spec 0012, TOPO-18). Below 768px the proposal bar, the scenario
5
+ * player, the issues and PRs panel (forge-panel.js) and the node panel are sheets docked at the bottom of
6
+ * the map (`#sheet-dock`, which is `display: contents` from 768px up, so the desktop layout is untouched).
7
+ * Each is a **peek** by default (a handle, a title and a line or two) and **expands** to at most half the
8
+ * screen (60% for the node panel) with its own scroll. A tap or a swipe on the handle expands or collapses it; a tap
9
+ * on the map collapses it. At most one is expanded at a time (sheet-model.js). Spec 0010 D7: the Work panel is a sheet too, with the review
10
+ * (the proposal bar or the scenario player) and one other sheet at most (opening Work displaces the node panel and the reverse); while one is
11
+ * expanded the sheet of the other group folds to a **stub** (one 44px row: its title and its badge; a tap swaps them), and an expanded sheet is
12
+ * capped so the map keeps 35% of the screen (`--hx-sheet-cap`, sheet-model.js `sheetCap`). Escape folds an expanded sheet, then closes.
13
+ *
14
+ * The sheets cover the bottom of the map, so the map must not fit or follow a step into that part:
15
+ * `measureCovered()` is the height the dock covers (0 on a desktop), kept in `state.coveredBottom`
16
+ * and in `--hx-covered` on the stage; the map's fit and the scenario camera read it.
17
+ *
18
+ * It is on an instance (spec 0011 D2): the sheets, the dock and what is watching it are the instance's own (`inst.slot`), and the functions
19
+ * take `inst` first. A node panel registers its own sheet (pieces/node-panel.js); explorer/sheets.js keeps the old names for the page.
20
+ */
21
+
22
+ import { createSheetModel, sheetCap } from '../sheet-model.js';
23
+ import { freeRect } from '../camera.js';
24
+
25
+ /** The width below which the sheets apply (matches the CSS) */
26
+ export const PHONE_QUERY = '(max-width: 767px)';
27
+ /** A drag on the handle longer than this (px) is a swipe, not a tap */
28
+ const SWIPE_PX = 20;
29
+
30
+ /** What the instance keeps of its sheets */
31
+ function sheetsOf(inst) {
32
+ return inst.slot('sheets', () => ({
33
+ model: createSheetModel(),
34
+ elements: new Map(), // id -> the sheet's element
35
+ options: new Map(), // id -> { stub?: () => { title: string, badge?: string, label?: string }, away?: () => void } what the sheet's owner gave it
36
+ offs: new Map(), // id -> how to remove the listeners bound on the sheet and its handle
37
+ away: new Set(), // sheets displaced by the other of their group: open on the page, hidden until opened again
38
+ listeners: new Set(),
39
+ dock: null,
40
+ lastCovered: 0,
41
+ observers: [],
42
+ wiredDock: null,
43
+ documentWired: false,
44
+ }));
45
+ }
46
+
47
+ export const isPhone = (inst) => !!(inst.win && typeof inst.win.matchMedia === 'function' && inst.win.matchMedia(PHONE_QUERY).matches);
48
+
49
+ const stageOf = (inst, S) => (S.dock && S.dock.parentElement) || (inst.state.canvas ? inst.state.canvas.parentElement : null);
50
+
51
+ /** 'expanded', 'stub' or 'peek' for a registered sheet (a sheet that is not open is a peek, as it is when it opens); undefined for one that is not registered */
52
+ export const sheetState = (inst, id) => { const st = sheetsOf(inst).model.stateOf(id); return st === 'hidden' ? 'peek' : st; };
53
+ /** The id of the expanded sheet, or null */
54
+ export const expandedSheet = (inst) => sheetsOf(inst).model.expanded();
55
+
56
+ /** Calls `fn` when the height the sheets cover, or which sheet is expanded, changes. Returns how to stop. */
57
+ export function onCoveredChange(inst, fn) {
58
+ const S = sheetsOf(inst);
59
+ S.listeners.add(fn);
60
+ return () => S.listeners.delete(fn);
61
+ }
62
+
63
+ /** Puts the model on the page: each sheet's attribute and handle, the stage's `data-sheet-open`, the covered height */
64
+ function apply(inst) {
65
+ const S = sheetsOf(inst);
66
+ const { model } = S;
67
+ syncShown(inst);
68
+ for (const [id, el] of S.elements) {
69
+ const expanded = model.isExpanded(id);
70
+ const st = model.stateOf(id);
71
+ el.dataset.sheetState = st === 'hidden' ? 'peek' : st;
72
+ const handle = [...el.children].find((c) => c.classList.contains('hx-sheet-handle'));
73
+ if (handle) {
74
+ handle.setAttribute('aria-expanded', String(expanded));
75
+ handle.setAttribute('aria-label', expanded ? 'Collapse this panel' : 'Expand this panel');
76
+ }
77
+ paintStub(inst, id, el, st === 'stub');
78
+ }
79
+ const stage = stageOf(inst, S);
80
+ if (stage) stage.dataset.sheetOpen = model.expanded() || '';
81
+ // The expanded sheet is capped so the map keeps 35% of the screen: what is left under the stub, if there is one
82
+ if (S.dock && isPhone(inst)) S.dock.style.setProperty('--hx-sheet-cap', `${sheetCap(inst.win.innerHeight || 0, model.stubs())}px`);
83
+ markLast(inst);
84
+ measureCovered(inst);
85
+ S.listeners.forEach((fn) => fn());
86
+ }
87
+
88
+ /** Whether a sheet's element is open on the page (the proposal bar is shown, the Work panel and the node panel carry their open class) */
89
+ function isOpenOnPage(id, el) {
90
+ if (id === 'forge') return el.classList.contains('forge-open');
91
+ if (id === 'drawer') return el.classList.contains('drawer-open');
92
+ return !el.hidden && el.isConnected;
93
+ }
94
+
95
+ /**
96
+ * Brings the model's open sheets in line with the page. A sheet that has just opened beside another of its group displaces it (Work and the node
97
+ * panel take turns): the displaced one is `away` (hidden on a phone, its selection kept) and its owner is told. A sheet that is closed on the page
98
+ * is hidden in the model and is no longer away.
99
+ */
100
+ function syncShown(inst) {
101
+ const S = sheetsOf(inst);
102
+ const { model } = S;
103
+ if (!isPhone(inst)) { // the sheets are panels from 768px: nothing is displaced and nothing is a stub
104
+ S.away.clear();
105
+ for (const el of S.elements.values()) el.removeAttribute('data-sheet-away');
106
+ return;
107
+ }
108
+ for (const [id, el] of S.elements) {
109
+ const open = isOpenOnPage(id, el) && !S.away.has(id);
110
+ if (!open && !isOpenOnPage(id, el)) S.away.delete(id);
111
+ if (open && !model.isShown(id)) {
112
+ for (const gone of model.show(id)) displace(inst, gone);
113
+ } else if (!open && model.isShown(id)) model.hide(id);
114
+ }
115
+ for (const [id, el] of S.elements) {
116
+ const gone = S.away.has(id);
117
+ const was = el.hasAttribute('data-sheet-away');
118
+ el.toggleAttribute('data-sheet-away', gone);
119
+ if (gone && !el.inert) el.inert = true; // out of reach of Tab while it is away
120
+ else if (!gone && was && isOpenOnPage(id, el)) el.inert = false;
121
+ }
122
+ }
123
+
124
+ /** A sheet displaced by the other of its group: hidden, not closed (the node stays selected); Work is closed by its own code */
125
+ function displace(inst, id) {
126
+ const S = sheetsOf(inst);
127
+ const el = S.elements.get(id);
128
+ if (!el) return;
129
+ S.away.add(id);
130
+ const on = S.options.get(id);
131
+ if (on && on.away) on.away();
132
+ }
133
+
134
+ /** The stub's row: made on first use, shown only in the stub state, with the title and the badge the sheet's owner says */
135
+ function paintStub(inst, id, el, on) {
136
+ const S = sheetsOf(inst);
137
+ let stub = [...el.children].find((c) => c.classList.contains('hx-sheet-stub'));
138
+ if (!stub) {
139
+ if (!on) return;
140
+ stub = inst.doc.createElement('button');
141
+ stub.type = 'button';
142
+ stub.className = 'hx-sheet-stub';
143
+ stub.dataset.corn = 'sheet-stub';
144
+ stub.setAttribute('part', 'sheet-stub');
145
+ stub.addEventListener('click', (e) => { e.stopPropagation(); e.preventDefault(); expandSheet(inst, id); const h = [...el.children].find((c) => c.classList.contains('hx-sheet-handle')); if (h && isPhone(inst)) h.focus(); });
146
+ el.append(stub);
147
+ }
148
+ stub.hidden = !on;
149
+ if (!on) return;
150
+ const opt = S.options.get(id);
151
+ const said = (opt && opt.stub ? opt.stub() : null) || { title: el.getAttribute('aria-label') || id };
152
+ const title = String(said.title || '');
153
+ const badge = said.badge ? String(said.badge) : '';
154
+ const key = `${title}\u0000${badge}\u0000${said.label || ''}`;
155
+ if (stub.dataset.key === key) return;
156
+ stub.dataset.key = key;
157
+ stub.replaceChildren();
158
+ const t = inst.doc.createElement('span');
159
+ t.className = 'hx-sheet-stub-title';
160
+ t.textContent = title;
161
+ stub.append(t);
162
+ if (badge) {
163
+ const b = inst.doc.createElement('span');
164
+ b.className = 'hx-waiting-badge';
165
+ b.dataset.corn = 'waiting-badge';
166
+ b.setAttribute('part', 'waiting-badge');
167
+ b.setAttribute('aria-hidden', 'true');
168
+ b.textContent = badge;
169
+ stub.append(b);
170
+ }
171
+ stub.setAttribute('aria-label', said.label || (badge ? `${title}, ${badge}` : title));
172
+ }
173
+
174
+ /** Says a stub's words may have changed (the number waiting, a proposal's title): drawn again, if it is showing */
175
+ export function refreshStubs(inst) {
176
+ const S = sheetsOf(inst);
177
+ for (const [id, el] of S.elements) if (S.model.stateOf(id) === 'stub') paintStub(inst, id, el, true);
178
+ }
179
+
180
+ /**
181
+ * Opens a sheet's place in the dock without expanding it (Work opens as a peek, a node panel opens as a peek): the other sheet of its group is
182
+ * displaced, and this one is no longer away. Called by what opens a sheet (the node panel's code); the Work panel's open class is seen too.
183
+ */
184
+ export function showSheet(inst, id) {
185
+ const S = sheetsOf(inst);
186
+ if (!S.elements.has(id)) return;
187
+ S.away.delete(id);
188
+ apply(inst);
189
+ }
190
+
191
+ /** The lowest visible sheet gets `data-sheet-last`: it carries the bottom safe-area inset */
192
+ function markLast(inst) {
193
+ const { dock } = sheetsOf(inst);
194
+ if (!dock) return;
195
+ let last = null;
196
+ for (const child of dock.children) {
197
+ child.removeAttribute('data-sheet-last');
198
+ const closed = (child.classList.contains('hx-drawer') && !child.classList.contains('drawer-open')) || (child.classList.contains('hx-forge') && !child.classList.contains('forge-open'));
199
+ if (!child.hidden && !closed) last = child;
200
+ }
201
+ if (last) last.setAttribute('data-sheet-last', '');
202
+ }
203
+
204
+ /**
205
+ * The height of the map the sheets hide, in px: the dock's height on a phone, 0 anywhere else.
206
+ * Also written to `state.coveredBottom` and to `--hx-covered` on the stage. Without a dock on the
207
+ * page it returns what `state.coveredBottom` holds.
208
+ */
209
+ export function measureCovered(inst) {
210
+ const S = sheetsOf(inst);
211
+ const { state } = inst;
212
+ if (!S.dock || !S.dock.isConnected) return state.coveredBottom || 0;
213
+ const covered = isPhone(inst) ? Math.round(S.dock.offsetHeight || 0) : 0;
214
+ state.coveredBottom = covered;
215
+ const stage = stageOf(inst, S);
216
+ if (stage) stage.style.setProperty('--hx-covered', `${covered}px`);
217
+ if (covered !== S.lastCovered) {
218
+ S.lastCovered = covered;
219
+ S.listeners.forEach((fn) => fn());
220
+ }
221
+ return covered;
222
+ }
223
+
224
+ /**
225
+ * The part of the map nothing covers, in the canvas's own CSS px: what the camera should frame a node into.
226
+ * On a phone the dock's sheets cover the bottom; from 768px the proposal bar covers the top (and the change list it grows to hold),
227
+ * the node panel the right, the Work panel and the review column the left and the scenario player the bottom. Without a layout (no page) it is the whole canvas.
228
+ * @returns {{ x: number, y: number, width: number, height: number }}
229
+ */
230
+ export function visibleMapRect(inst) {
231
+ const { state } = inst;
232
+ const covers = { top: 0, bottom: 0, left: 0, right: 0 };
233
+ const canvas = state.canvas;
234
+ const shown = (el) => !!el && !el.hidden && el.isConnected;
235
+ if (isPhone(inst)) covers.bottom = measureCovered(inst) + 8;
236
+ else if (canvas && typeof canvas.getBoundingClientRect === 'function') {
237
+ const c = canvas.getBoundingClientRect();
238
+ const bar = inst.el('proposal-bar');
239
+ if (shown(bar)) covers.top = Math.max(0, bar.getBoundingClientRect().bottom - c.top) + 8;
240
+ const drawer = inst.el('spec-drawer');
241
+ if (shown(drawer) && drawer.classList.contains('drawer-open')) covers.right = drawer.offsetWidth; // its width, not its box: it slides in
242
+ const forge = inst.el('forge-panel');
243
+ if (shown(forge) && forge.classList.contains('forge-open')) covers.left = forge.offsetWidth;
244
+ // the review column (review-column.js) is docked on the left under the bar: its right edge is what it covers, past the Work panel's when both are open
245
+ const column = inst.el('review-column');
246
+ if (shown(column)) covers.left = Math.max(covers.left, Math.max(0, column.getBoundingClientRect().right - c.left) + 8);
247
+ const player = inst.root ? inst.root.querySelector('.hx-player') : null;
248
+ if (shown(player)) covers.bottom = Math.max(0, c.bottom - player.getBoundingClientRect().top) + 8;
249
+ }
250
+ return freeRect(state.width || 0, state.height || 0, covers);
251
+ }
252
+
253
+ // ---------------------------------------------------------------------------
254
+ // Registering and changing sheets
255
+ // ---------------------------------------------------------------------------
256
+
257
+ /** Makes `el` a sheet called `id` (a peek until expanded) and gives it a handle if it has none */
258
+ export function registerSheet(inst, id, el, opts = {}) {
259
+ if (!el) return;
260
+ const S = sheetsOf(inst);
261
+ unregisterSheet(inst, id, false);
262
+ S.model.add(id);
263
+ S.elements.set(id, el);
264
+ S.options.set(id, opts);
265
+ el.classList.add('hx-sheet');
266
+ let handle = [...el.children].find((c) => c.classList.contains('hx-sheet-handle'));
267
+ if (!handle) {
268
+ handle = inst.doc.createElement('button');
269
+ handle.type = 'button';
270
+ handle.className = 'hx-sheet-handle';
271
+ handle.dataset.corn = 'sheet-handle';
272
+ handle.setAttribute('part', 'sheet-handle');
273
+ handle.innerHTML = '<span data-corn="sheet-grip" part="sheet-grip"></span>';
274
+ el.prepend(handle);
275
+ }
276
+ // Registered again (a re-init) the old listeners went with `unregisterSheet` above: one set, or a tap toggles twice
277
+ S.offs.set(id, [...bindHandle(inst, id, handle), inst.listen(el, 'click', (e) => peekClick(inst, id, e), true)]);
278
+ apply(inst);
279
+ }
280
+
281
+ /** Forgets a sheet (the player closing, a node panel ending); `redraw` false while it is only being replaced */
282
+ export function unregisterSheet(inst, id, redraw = true) {
283
+ const S = sheetsOf(inst);
284
+ if (!S.model.has(id)) return;
285
+ (S.offs.get(id) || []).forEach((off) => off());
286
+ S.offs.delete(id);
287
+ S.model.remove(id);
288
+ S.elements.delete(id);
289
+ S.options.delete(id);
290
+ S.away.delete(id);
291
+ if (redraw) apply(inst);
292
+ }
293
+
294
+ export function expandSheet(inst, id) { syncShown(inst); if (sheetsOf(inst).model.expand(id)) apply(inst); } // what is open on the page is known first (a bar shown a moment ago)
295
+ export function collapseSheet(inst, id) { if (sheetsOf(inst).model.collapse(id)) apply(inst); }
296
+ export function collapseAllSheets(inst) { if (sheetsOf(inst).model.collapseAll()) apply(inst); }
297
+ export function toggleSheet(inst, id) { syncShown(inst); if (sheetsOf(inst).model.toggle(id)) apply(inst); } // the same: a bar shown since the last change is open
298
+
299
+ // ---------------------------------------------------------------------------
300
+ // Taps and swipes
301
+ // ---------------------------------------------------------------------------
302
+
303
+ /** The handle: a tap toggles, a swipe up expands, a swipe down collapses. Returns how to take its listeners back */
304
+ function bindHandle(inst, id, handle) {
305
+ let startY = null;
306
+ let swiped = false;
307
+ const offs = [];
308
+ offs.push(inst.listen(handle, 'pointerdown', (e) => {
309
+ startY = e.clientY;
310
+ swiped = false;
311
+ if (handle.setPointerCapture && e.pointerId !== undefined) { try { handle.setPointerCapture(e.pointerId); } catch { /* a synthetic event has no pointer to capture */ } }
312
+ }));
313
+ const end = (e) => {
314
+ if (startY === null) return;
315
+ const dy = e.clientY - startY;
316
+ startY = null;
317
+ if (Math.abs(dy) < SWIPE_PX) return;
318
+ swiped = true;
319
+ if (dy < 0) expandSheet(inst, id); else collapseSheet(inst, id);
320
+ };
321
+ offs.push(inst.listen(handle, 'pointerup', end));
322
+ offs.push(inst.listen(handle, 'pointercancel', () => { startY = null; }));
323
+ offs.push(inst.listen(handle, 'click', (e) => {
324
+ e.stopPropagation(); // not a tap on the sheet itself (peekClick)
325
+ if (swiped) { swiped = false; return; }
326
+ toggleSheet(inst, id);
327
+ }));
328
+ return offs;
329
+ }
330
+
331
+ /** A peek sheet expands on a tap anywhere on it, except on its Close button (which still closes) */
332
+ function peekClick(inst, id, e) {
333
+ if (!isPhone(inst) || sheetsOf(inst).model.isExpanded(id)) return;
334
+ const target = e.target;
335
+ if (target.closest && target.closest('.hx-sheet-handle, .hx-sheet-stub')) return; // the handle's own listener, and the stub's (it swaps, and takes the focus to the handle)
336
+ if (target.closest && target.closest('select, input, textarea, [data-peek-ok]')) return; // the speed menu, and the proposal's Send back, Approve and mode switcher, work from the peek
337
+ const action = target.closest && target.closest('[data-action]');
338
+ if (action && /close/.test(action.dataset.action)) return;
339
+ e.preventDefault();
340
+ e.stopPropagation();
341
+ expandSheet(inst, id);
342
+ }
343
+
344
+ /** A touch or click on the map collapses the expanded sheet */
345
+ function onMapDown(inst, e) {
346
+ if (e.target === inst.state.canvas && isPhone(inst)) collapseAllSheets(inst);
347
+ }
348
+
349
+ /** Escape in an expanded sheet folds it to a peek; Escape in a peek is the sheet's own (it closes it): "folds, then closes" (spec 0010 D7) */
350
+ function onEscape(inst, e) {
351
+ if (e.key !== 'Escape' || e.defaultPrevented || !isPhone(inst)) return;
352
+ const S = sheetsOf(inst);
353
+ const id = S.model.expanded();
354
+ const el = id && S.elements.get(id);
355
+ if (!el || !el.contains(e.target)) return;
356
+ if (e.target.closest && e.target.closest('select, input, textarea')) return; // a field's own Escape first
357
+ e.preventDefault();
358
+ e.stopPropagation();
359
+ collapseSheet(inst, id);
360
+ const handle = [...el.children].find((c) => c.classList.contains('hx-sheet-handle'));
361
+ if (handle) handle.focus();
362
+ }
363
+
364
+ /** The dim layer behind an expanded node panel: a tap there collapses the panel to its peek (it no longer closes it) */
365
+ export function onBackdrop(inst, e) {
366
+ if (!isPhone(inst)) return;
367
+ e.stopPropagation();
368
+ collapseAllSheets(inst);
369
+ }
370
+
371
+ // ---------------------------------------------------------------------------
372
+ // Wiring (the page calls this once, after the page's markup is there; a node panel calls it as it mounts)
373
+ // ---------------------------------------------------------------------------
374
+
375
+ /** Watches the instance's dock (`#sheet-dock`) and its root for the taps and keys that fold a sheet; a second call for the same dock does nothing */
376
+ export function initSheets(inst) {
377
+ const S = sheetsOf(inst);
378
+ const found = inst.el('sheet-dock');
379
+ if (!found) return;
380
+ if (found !== S.dock) {
381
+ // A new page (or a test's new markup): forget the old sheets and watch this dock
382
+ S.observers.forEach((o) => o.disconnect());
383
+ S.observers = [];
384
+ S.model.ids().forEach((id) => S.model.remove(id));
385
+ S.elements.clear();
386
+ S.options.clear();
387
+ S.away.clear();
388
+ S.lastCovered = 0;
389
+ S.dock = found;
390
+ }
391
+ if (S.wiredDock !== S.dock) {
392
+ S.wiredDock = S.dock;
393
+ const win = inst.win;
394
+ if (win && typeof win.MutationObserver === 'function') {
395
+ const mo = new win.MutationObserver(() => { markLast(inst); measureCovered(inst); });
396
+ mo.observe(S.dock, { childList: true, subtree: true, attributes: true, attributeFilter: ['hidden', 'class'] });
397
+ S.observers.push(mo);
398
+ inst.own(() => mo.disconnect());
399
+ }
400
+ if (win && typeof win.ResizeObserver === 'function') {
401
+ const ro = new win.ResizeObserver(() => measureCovered(inst));
402
+ ro.observe(S.dock);
403
+ S.observers.push(ro);
404
+ inst.own(() => ro.disconnect());
405
+ }
406
+ }
407
+ if (!S.documentWired) {
408
+ S.documentWired = true;
409
+ for (const type of ['pointerdown', 'touchstart', 'mousedown']) inst.listen(inst.root, type, (e) => onMapDown(inst, e), { passive: true, capture: true });
410
+ inst.listen(inst.root, 'keydown', (e) => onEscape(inst, e), true);
411
+ if (inst.win && typeof inst.win.matchMedia === 'function') {
412
+ const mq = inst.win.matchMedia(PHONE_QUERY);
413
+ if (mq && mq.addEventListener) inst.listen(mq, 'change', () => { collapseAllSheets(inst); measureCovered(inst); });
414
+ }
415
+ }
416
+ apply(inst);
417
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @module explorer/engine/strings
3
+ *
4
+ * The words a repo may say in its own voice (core/strings.js is the table; `strings` in cornfield.config.js the overrides, which
5
+ * `/cornfield/config.json` carries), on an instance: `inst.strings` holds the overrides (key -> text), so two instances on one page can
6
+ * speak in two voices. Every overridable label is read through `t(inst, key)`; the markup of a piece marks what to replace with
7
+ * `data-corn-string` attributes, which `applyStrings` fills once the words are in.
8
+ *
9
+ * data-corn-string="key" the element's text
10
+ * data-corn-string-aria-label="key" its aria-label
11
+ * data-corn-string-placeholder="key" its placeholder
12
+ *
13
+ * The rule for accessible names: a control's name is its visible label. When a key is overridden the visible text and the
14
+ * aria-label carry the same words, and where a name adds a fixed target ("Stop run 7") only the label part is replaced
15
+ * ("Halt run 7"). So a screen reader says what is on the screen.
16
+ *
17
+ * Values are only ever set through textContent and attributes, never as HTML, and a value that fails core/strings.js
18
+ * `stringProblem` (not text, a line break, too long) is ignored here as well, whatever the host sent.
19
+ * explorer/strings.js keeps the old names for the page's instance (a bridge).
20
+ */
21
+ import { STRINGS, isStringKey, stringProblem } from '../../core/strings.js';
22
+
23
+ /** Whether the instance overrides this key with good text */
24
+ export const isOverridden = (inst, key) => isStringKey(key) && Object.hasOwn(inst.strings, key) && !stringProblem(inst.strings[key]);
25
+
26
+ /**
27
+ * Takes the overrides a host carries (`config.strings`) in place of the instance's. Anything that is not a known key with good text is left out.
28
+ * @param {{ strings: Record<string, string> }} inst
29
+ * @param {unknown} strings
30
+ */
31
+ export function setStrings(inst, strings) {
32
+ for (const key of Object.keys(inst.strings)) delete inst.strings[key];
33
+ if (!strings || typeof strings !== 'object' || Array.isArray(strings)) return;
34
+ for (const [key, value] of Object.entries(strings)) if (isStringKey(key) && !stringProblem(value)) inst.strings[key] = value;
35
+ }
36
+
37
+ /** The words for a key: the override, else the default. A key that is not in the table gives the key itself (a test keeps the code to the table). */
38
+ export function t(inst, key) {
39
+ if (isOverridden(inst, key)) return inst.strings[key];
40
+ return isStringKey(key) ? STRINGS[key] : String(key);
41
+ }
42
+
43
+ const TARGETS = [
44
+ ['data-corn-string', (el, text) => { el.textContent = text; }],
45
+ ['data-corn-string-aria-label', (el, text) => el.setAttribute('aria-label', text)],
46
+ ['data-corn-string-placeholder', (el, text) => el.setAttribute('placeholder', text)],
47
+ ];
48
+
49
+ /**
50
+ * Puts the instance's overrides in the markup under `root` (default: the instance's own root). Only an overridden key touches its element,
51
+ * so what the markup says is what shows without one.
52
+ * @param {{ strings: Record<string, string>, root: ParentNode | null }} inst
53
+ * @param {ParentNode} [scope]
54
+ */
55
+ export function applyStrings(inst, scope = inst.root) {
56
+ if (!scope || typeof scope.querySelectorAll !== 'function') return;
57
+ for (const [attr, set] of TARGETS) {
58
+ for (const el of scope.querySelectorAll(`[${attr}]`)) {
59
+ const key = el.getAttribute(attr);
60
+ if (isOverridden(inst, key)) set(el, inst.strings[key]);
61
+ }
62
+ }
63
+ }
@@ -1,49 +1,15 @@
1
1
  /**
2
2
  * @module explorer/feedback
3
3
  *
4
- * One way to say what an action came to: **where the person is looking** (by the control they used) and **to a screen reader** (the live
5
- * region), together. A result is kept under its key (a run's id) until the next action on that key, so a card that is drawn again shows
6
- * it again; a result that has no next action to end it (Copy spec) goes after `RESULT_MS`, which is never less than five seconds.
7
- *
8
- * sayResult(key, words, ok) keep the result for `key` and announce it
9
- * resultOf(key) { text, ok } or null
10
- * clearResult(key) the next action on `key` begins: the old result goes
11
- * showResultIn(el, key) paints the result of `key` into a result element (`data-corn="result"`), hidden when there is none
4
+ * A bridge (spec 0011 D2, step 4): the results of actions now live on the instance (engine/feedback.js). This file keeps the old names for the
5
+ * modules that are not yet on an instance (forge-panel.js), bound to the page's instance. It goes when forge-panel.js is converted.
12
6
  */
13
-
14
- import { announce } from './keyboard.js';
15
-
16
- /** How long a result with no next action to end it stays (ms): at least five seconds */
17
- export const RESULT_MS = 6000;
18
-
19
- const results = new Map();
20
-
21
- /** Keeps the result of an action under `key` and announces it through the live region */
22
- export function sayResult(key, text, ok = true) {
23
- results.set(key, { text, ok });
24
- announce(text);
25
- }
26
-
27
- /** The result kept under `key`, or null */
28
- export function resultOf(key) {
29
- return results.get(key) || null;
30
- }
31
-
32
- /** The next action on `key` begins: its old result goes */
33
- export function clearResult(key) {
34
- results.delete(key);
35
- }
36
-
37
- /** Paints the result of `key` into `el` (a result element), hidden when there is none */
38
- export function showResultIn(el, key) {
39
- if (!el) return;
40
- const r = results.get(key);
41
- el.hidden = !r;
42
- el.textContent = r ? r.text : '';
43
- if (r) el.dataset.cornState = r.ok ? 'success' : 'failure'; else delete el.dataset.cornState;
44
- }
45
-
46
- /** For tests: forget every result */
47
- export function resetFeedbackForTests() {
48
- results.clear();
49
- }
7
+ import { pageInstance } from './state.js';
8
+ import * as F from './engine/feedback.js';
9
+
10
+ export { RESULT_MS } from './engine/feedback.js';
11
+ export const sayResult = (key, text, ok = true) => F.sayResult(pageInstance(), key, text, ok);
12
+ export const resultOf = (key) => F.resultOf(pageInstance(), key);
13
+ export const clearResult = (key) => F.clearResult(pageInstance(), key);
14
+ export const showResultIn = (el, key) => F.showResultIn(pageInstance(), el, key);
15
+ export const resetFeedbackForTests = () => F.resetFeedback(pageInstance());
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @module explorer/flow-style
3
+ *
4
+ * How a flow edge is styled: its colour and label per kind (scene.js turns that into the curve, arrowhead and packet a renderer draws).
5
+ * Pure, so the scene can use it on any instance; flows.js (the legend, the data-flow preset) re-exports it under the old names.
6
+ */
7
+
8
+ export const FLOW_STYLES = {
9
+ outbox: { color: '#f59e0b', label: 'Outbox → API (writes)' },
10
+ changes: { color: '#22d3ee', label: 'Change stream (reads back)' },
11
+ trigger: { color: '#a3e635', label: 'Trigger → audit_log (seq)' },
12
+ sync: { color: '#fb7185', label: 'Background sync (external)' },
13
+ model: { color: '#c084fc', label: 'Model classes feed a tier' },
14
+ rom: { color: '#f0abfc', label: 'ROM: change → re-bake → serve' },
15
+ read: { color: '#93c5fd', label: 'Server reads rows over HTTP' },
16
+ api: { color: '#38bdf8', label: 'App calls the API (derived from its code)' },
17
+ build: { color: '#d6d3d1', label: 'Build script writes files' },
18
+ reads: { color: '#fde047', label: 'Table → page (render reads)' },
19
+ writes: { color: '#f87171', label: 'Page → table (form / action)' },
20
+ uses: { color: '#f472b6', label: 'Stylesheet uses design tokens' },
21
+ extends: { color: '#5eead4', label: 'Component extends a base class' },
22
+ calls: { color: '#fb7185', label: 'Calls (service → service, outside system)' },
23
+ triggers: { color: '#a3e635', label: 'Triggers (cron, webhook, store change)' },
24
+ delivers: { color: '#94a3b8', label: 'Outside system → webhook endpoint' },
25
+ 'runs-on': { color: '#a8a29e', label: 'App → the stack that serves its domain' },
26
+ };
27
+
28
+ export const flowStyleOf = (edge) => FLOW_STYLES[edge.kind === 'flow' ? edge.flow : edge.kind] || FLOW_STYLES.sync;
package/explorer/flows.js CHANGED
@@ -1,10 +1,11 @@
1
1
  /**
2
2
  * @module explorer/flows
3
3
  *
4
- * Data flow on the map:
4
+ * Data flow on the map (a bridge for the legend and the panel's block, which live on the instance in engine/flow-ui.js; they go from here
5
+ * when the last module that is not on an instance stops calling them):
5
6
  * - how a flow edge is styled (its colour and label per kind; scene.js turns that into the curve,
6
7
  * arrowhead and packet a renderer draws),
7
- * - the legend and the "Data flows" block of a node's panel, and
8
+ * - the legend and the "Data flows" block of a node's panel (engine/flow-ui.js), and
8
9
  * - the data-flow preset: the same topology folded into six tiers, left to right,
9
10
  * browser -> app servers -> API -> tables -> services -> outside systems.
10
11
  *
@@ -12,32 +13,11 @@
12
13
  * today, dashed = proposed. Other edge kinds (reads, writes, calls, ...) are styled by kind.
13
14
  */
14
15
 
15
- import { state, nameOf, escHtml } from './state.js';
16
- import { themed } from './theme-model.js';
17
- import { capped } from './capped.js';
18
- import { layersOf } from './layer-model.js';
16
+ import { pageInstance } from './state.js';
17
+ import { FLOW_STYLES } from './flow-style.js';
18
+ import * as F from './engine/flow-ui.js';
19
19
 
20
- export const FLOW_STYLES = {
21
- outbox: { color: '#f59e0b', label: 'Outbox → API (writes)' },
22
- changes: { color: '#22d3ee', label: 'Change stream (reads back)' },
23
- trigger: { color: '#a3e635', label: 'Trigger → audit_log (seq)' },
24
- sync: { color: '#fb7185', label: 'Background sync (external)' },
25
- model: { color: '#c084fc', label: 'Model classes feed a tier' },
26
- rom: { color: '#f0abfc', label: 'ROM: change → re-bake → serve' },
27
- read: { color: '#93c5fd', label: 'Server reads rows over HTTP' },
28
- api: { color: '#38bdf8', label: 'App calls the API (derived from its code)' },
29
- build: { color: '#d6d3d1', label: 'Build script writes files' },
30
- reads: { color: '#fde047', label: 'Table → page (render reads)' },
31
- writes: { color: '#f87171', label: 'Page → table (form / action)' },
32
- uses: { color: '#f472b6', label: 'Stylesheet uses design tokens' },
33
- extends: { color: '#5eead4', label: 'Component extends a base class' },
34
- calls: { color: '#fb7185', label: 'Calls (service → service, outside system)' },
35
- triggers: { color: '#a3e635', label: 'Triggers (cron, webhook, store change)' },
36
- delivers: { color: '#94a3b8', label: 'Outside system → webhook endpoint' },
37
- 'runs-on': { color: '#a8a29e', label: 'App → the stack that serves its domain' },
38
- };
39
-
40
- export const flowStyleOf = (edge) => FLOW_STYLES[edge.kind === 'flow' ? edge.flow : edge.kind] || FLOW_STYLES.sync;
20
+ export { FLOW_STYLES, flowStyleOf } from './flow-style.js';
41
21
 
42
22
  export const flowReduceMotion = typeof window !== 'undefined' && !!(window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches);
43
23
 
@@ -128,28 +108,7 @@ export function buildDataFlow(nodes, edges) {
128
108
  }
129
109
 
130
110
  // ---------------------------------------------------------------------------
131
- // Legend and the panel's "Data flows" block
111
+ // Legend and the panel's "Data flows" block (engine/flow-ui.js, on an instance)
132
112
  // ---------------------------------------------------------------------------
133
- export function renderFlowLegend() {
134
- const el = document.getElementById('flow-legend');
135
- if (!el) return;
136
- const activeLayers = layersOf(state);
137
- if (activeLayers.flow === false && activeLayers.table === false) { el.innerHTML = ''; el.hidden = true; return; }
138
- el.hidden = false;
139
- const row = (swatch, text) => `<div class="hx-legend-row"><span class="hx-legend-swatch">${swatch}</span><span>${text}</span></div>`;
140
- el.innerHTML = `
141
- <div class="hx-menu-section" data-corn="menu-section">
142
- <div class="hx-menu-title" data-corn="menu-title">
143
- <span>Data Flow</span>
144
- </div>
145
- ${Object.values(FLOW_STYLES).map((s) => row(`<span data-corn="swatch" class="hx-swatch-line" style="background:${themed(s.color, state.theme)}"></span>`, s.label)).join('')}
146
- </div>`;
147
- }
148
-
149
- export function flowSummary(node) {
150
- const flows = state.edges.filter((e) => e.kind === 'flow' && (e.from === node.id || e.to === node.id));
151
- const fact = (key, list, out) => (list.length
152
- ? `<div class="hx-fact" data-corn="section"><div class="hx-fact-k">${key}</div><div class="hx-fact-v">${capped(list.map((e) => `<div class="hx-line"><span><span class="hx-name">${escHtml(nameOf(out ? e.to : e.from))}</span> · ${escHtml(e.label)}${e.status === 'live' ? '' : ' <span class="hx-warn">(proposed)</span>'}</span></div>`))}</div></div>`
153
- : '');
154
- return fact('Sends to', flows.filter((e) => e.from === node.id), true) + fact('Gets from', flows.filter((e) => e.to === node.id && e.from !== node.id), false);
155
- }
113
+ export const renderFlowLegend = () => F.renderFlowLegend(pageInstance());
114
+ export const flowSummary = (node) => F.flowSummary(pageInstance(), node);