@popcorn245/cornfield 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,9 +9,11 @@
9
9
  */
10
10
 
11
11
  import { relativeTime } from './review-model.js';
12
- import { runTone, canRetry, aboutWords, runName, statusWords, statusWhy, runMeta, canStop, canAnswer, groupRuns, compareTitle, limitRows, checksWords, timelineWords, nodeLabels, collisionWords, answerWords } from './runs-model.js';
12
+ import { runTone, canRetry, aboutWords, runName, statusWords, statusWhy, runMeta, canStop, canAnswer, compareTitle, limitRows, checksWords, timelineWords, nodeLabels, collisionWords, answerWords } from './runs-model.js';
13
13
  import { cornState, cornStateAttr, stateIconHtml } from './corn-state.js';
14
14
  import { t } from './strings.js';
15
+ import { runGroups } from './waiting-model.js';
16
+ import { runGroupHeadingHtml } from './waiting-view.js';
15
17
 
16
18
 
17
19
  /**
@@ -174,17 +176,18 @@ export function runHtml(ctx, run, all) {
174
176
  }
175
177
 
176
178
  /**
177
- * The list: runs on their own and compares, as the items of the list.
179
+ * The list, in groups (spec 0010 D6): Needs you (oldest question first), In motion, Finished (each newest first), each starting with a heading that
180
+ * says how many; runs on their own and compares are the items. The headings are items of the one flat list, so a run's card is never moved into a section.
178
181
  * @param {ViewContext} ctx
179
182
  * @param {object[]} runs
180
183
  */
181
184
  export function runsHtml(ctx, runs) {
182
- return groupRuns(runs).map((g) => {
185
+ return runGroups(runs).map((group) => runGroupHeadingHtml(group) + group.items.map((g) => {
183
186
  if (g.kind === 'run') return runHtml(ctx, g.run, runs);
184
187
  const id = `cmp-${g.runs[0].runId}`;
185
188
  return `<li class="hx-run-group" data-key="${ctx.esc(id)}"><section class="hx-run-compare" aria-labelledby="${ctx.esc(id)}">
186
189
  <h4 class="hx-run-compare-title" id="${ctx.esc(id)}">${ctx.esc(compareTitle(g.runs))}</h4>
187
190
  <p class="hx-forge-meta hx-faint">The same request under each profile, side by side.</p>
188
191
  <ul class="hx-run-columns" aria-label="${ctx.esc(g.runs.length)} profiles compared">${g.runs.map((r) => runHtml(ctx, r, runs)).join('')}</ul></section></li>`;
189
- }).join('');
192
+ }).join('')).join('');
190
193
  }
@@ -1,47 +1,95 @@
1
1
  /**
2
2
  * @module explorer/sheet-model
3
3
  *
4
- * The state machine of the phone's bottom sheets (TOPO-18), with no DOM: which sheets exist,
5
- * and which one is expanded. Every sheet starts as a peek (a handle and a line or two). At most one
6
- * is expanded at a time: expanding one collapses the others. sheets.js puts this on the page.
4
+ * The state machine of the phone's bottom sheets (TOPO-18, spec 0010 D7), with no DOM: which sheets exist, which are open, and which one is
5
+ * expanded. sheets.js puts this on the page.
6
+ *
7
+ * - Every sheet starts as a **peek** (a handle and a line or two). At most one is **expanded**; expanding one folds the sheets of the *other*
8
+ * group to a **stub** (one 44px row with a title and a badge). Tapping a stub expands it, and the one that was expanded becomes the stub.
9
+ * - **At most two sheets are open**: the *review* (the proposal bar, or the scenario player) and one *other* (Work, or the node panel).
10
+ * Opening Work displaces the node panel, and the reverse (`show` says which sheets it displaced); the node stays selected.
11
+ * - **The map keeps at least 35% of the screen** in every state (`sheetCap`): an expanded sheet's height is capped to leave it.
7
12
  */
8
13
 
14
+ /** The map keeps at least this share of the screen's height (spec 0010 D7) */
15
+ export const MAP_FLOOR = 0.35;
16
+ /** The height of a stub, in px: a row a thumb can hit */
17
+ export const STUB_PX = 44;
18
+
19
+ /** The groups: the sheets of the review, and the one other sheet (open one at a time) */
20
+ export const SHEET_GROUPS = Object.freeze({ proposal: 'review', player: 'review', forge: 'other', drawer: 'other' });
21
+ /** Groups that hold one open sheet at most: opening one displaces the other */
22
+ const EXCLUSIVE = new Set(['other']);
23
+
24
+ /** The tallest an expanded sheet may be, in px, for a screen `viewportPx` tall with `stubs` stubs under or over it: the rest is the map's 35% */
25
+ export const sheetCap = (viewportPx, stubs = 0) => Math.max(0, Math.floor(viewportPx - Math.ceil(viewportPx * MAP_FLOOR) - stubs * STUB_PX));
26
+
27
+ /** The map's smallest share of the screen, in px, for a screen `viewportPx` tall */
28
+ export const mapFloorPx = (viewportPx) => Math.ceil(viewportPx * MAP_FLOOR);
29
+
9
30
  /**
31
+ * @param {Record<string, string>} [groups] the group of each sheet id (a sheet not named is its own group)
10
32
  * @returns {{
11
33
  * add(id: string): void, remove(id: string): void, has(id: string): boolean,
34
+ * show(id: string): string[], hide(id: string): void, isShown(id: string): boolean, shown(): string[],
12
35
  * expand(id: string): boolean, collapse(id: string): boolean, collapseAll(): boolean, toggle(id: string): boolean,
13
- * isExpanded(id: string): boolean, expanded(): string|null, ids(): string[],
14
- * }} every change method answers whether anything changed
36
+ * isExpanded(id: string): boolean, expanded(): string|null, stateOf(id: string): 'expanded'|'peek'|'stub'|'hidden'|undefined, stubs(): number, ids(): string[],
37
+ * }} every change method answers whether anything changed (`show`: the ids it displaced)
15
38
  */
16
- export function createSheetModel() {
39
+ export function createSheetModel(groups = SHEET_GROUPS) {
17
40
  const known = new Set();
18
- let open = null;
41
+ const open = new Set(); // the sheets that are open (a sheet is open from `add` until `hide`)
42
+ let expandedId = null;
43
+ const groupOf = (id) => groups[id] || id;
19
44
  return {
20
- add: (id) => { known.add(id); },
21
- remove: (id) => { known.delete(id); if (open === id) open = null; },
45
+ add: (id) => { known.add(id); open.add(id); },
46
+ remove: (id) => { known.delete(id); open.delete(id); if (expandedId === id) expandedId = null; },
22
47
  has: (id) => known.has(id),
48
+ /** Opens a sheet. In an exclusive group the other open sheet is displaced (closed): its id is returned */
49
+ show(id) {
50
+ if (!known.has(id)) return [];
51
+ open.add(id);
52
+ if (!EXCLUSIVE.has(groupOf(id))) return [];
53
+ const gone = [...open].filter((o) => o !== id && groupOf(o) === groupOf(id));
54
+ for (const g of gone) { open.delete(g); if (expandedId === g) expandedId = null; }
55
+ return gone;
56
+ },
57
+ hide(id) { open.delete(id); if (expandedId === id) expandedId = null; },
58
+ isShown: (id) => open.has(id),
59
+ shown: () => [...open],
23
60
  expand(id) {
24
- if (!known.has(id) || open === id) return false;
25
- open = id;
61
+ if (!known.has(id) || !open.has(id) || expandedId === id) return false;
62
+ expandedId = id;
26
63
  return true;
27
64
  },
28
65
  collapse(id) {
29
- if (open !== id) return false;
30
- open = null;
66
+ if (expandedId !== id) return false;
67
+ expandedId = null;
31
68
  return true;
32
69
  },
33
70
  collapseAll() {
34
- if (open === null) return false;
35
- open = null;
71
+ if (expandedId === null) return false;
72
+ expandedId = null;
36
73
  return true;
37
74
  },
38
75
  toggle(id) {
39
- if (!known.has(id)) return false;
40
- open = open === id ? null : id;
76
+ if (!known.has(id) || !open.has(id)) return false;
77
+ expandedId = expandedId === id ? null : id;
41
78
  return true;
42
79
  },
43
- isExpanded: (id) => open === id,
44
- expanded: () => open,
80
+ isExpanded: (id) => expandedId === id,
81
+ expanded: () => expandedId,
82
+ /** `stub` for an open sheet of the other group while one is expanded; `peek` otherwise; undefined for a sheet that is not known */
83
+ stateOf(id) {
84
+ if (!known.has(id)) return undefined;
85
+ if (!open.has(id)) return 'hidden';
86
+ if (expandedId === id) return 'expanded';
87
+ return expandedId !== null && groupOf(expandedId) !== groupOf(id) ? 'stub' : 'peek';
88
+ },
89
+ /** How many stubs there are now */
90
+ stubs() {
91
+ return [...open].filter((id) => expandedId !== null && id !== expandedId && groupOf(id) !== groupOf(expandedId)).length;
92
+ },
45
93
  ids: () => [...known],
46
94
  };
47
95
  }
@@ -6,7 +6,10 @@
6
6
  * the map (`#sheet-dock`, which is `display: contents` from 768px up, so the desktop layout is untouched).
7
7
  * Each is a **peek** by default (a handle, a title and a line or two) and **expands** to at most half the
8
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).
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.
10
13
  *
11
14
  * The sheets cover the bottom of the map, so the map must not fit or follow a step into that part:
12
15
  * `measureCovered()` is the height the dock covers (0 on a desktop), kept in `state.coveredBottom`
@@ -14,7 +17,7 @@
14
17
  */
15
18
 
16
19
  import { state } from './state.js';
17
- import { createSheetModel } from './sheet-model.js';
20
+ import { createSheetModel, sheetCap } from './sheet-model.js';
18
21
  import { freeRect } from './camera.js';
19
22
 
20
23
  /** The width below which the sheets apply (matches the CSS) */
@@ -24,6 +27,9 @@ const SWIPE_PX = 20;
24
27
 
25
28
  const model = createSheetModel();
26
29
  const elements = new Map(); // id -> the sheet's element
30
+ const options = new Map(); // id -> { stub?: () => { title: string, badge?: string, label?: string }, away?: () => void } what the sheet's owner gave it
31
+ const wiredAs = new WeakMap(); // a sheet's element, and its handle -> the id their listeners were bound for
32
+ const away = new Set(); // sheets displaced by the other of their group: open on the page, hidden until opened again
27
33
  const listeners = new Set();
28
34
  let dock = null;
29
35
  let lastCovered = 0;
@@ -34,8 +40,8 @@ export const isPhone = () => typeof window !== 'undefined' && typeof window.matc
34
40
  const $ = (id) => document.getElementById(id);
35
41
  const stageOf = () => (dock && dock.parentElement) || (state.canvas ? state.canvas.parentElement : null);
36
42
 
37
- /** 'expanded' or 'peek' for a registered sheet; undefined for one that is not */
38
- export const sheetState = (id) => (model.has(id) ? (model.isExpanded(id) ? 'expanded' : 'peek') : undefined);
43
+ /** '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 */
44
+ export const sheetState = (id) => { const st = model.stateOf(id); return st === 'hidden' ? 'peek' : st; };
39
45
  /** The id of the expanded sheet, or null */
40
46
  export const expandedSheet = () => model.expanded();
41
47
 
@@ -47,22 +53,122 @@ export function onCoveredChange(fn) {
47
53
 
48
54
  /** Puts the model on the page: each sheet's attribute and handle, the stage's `data-sheet-open`, the covered height */
49
55
  function apply() {
56
+ syncShown();
50
57
  for (const [id, el] of elements) {
51
58
  const expanded = model.isExpanded(id);
52
- el.dataset.sheetState = expanded ? 'expanded' : 'peek';
53
- const handle = el.querySelector('.hx-sheet-handle');
59
+ const st = model.stateOf(id);
60
+ el.dataset.sheetState = st === 'hidden' ? 'peek' : st;
61
+ const handle = [...el.children].find((c) => c.classList.contains('hx-sheet-handle'));
54
62
  if (handle) {
55
63
  handle.setAttribute('aria-expanded', String(expanded));
56
64
  handle.setAttribute('aria-label', expanded ? 'Collapse this panel' : 'Expand this panel');
57
65
  }
66
+ paintStub(id, el, st === 'stub');
58
67
  }
59
68
  const stage = stageOf();
60
69
  if (stage) stage.dataset.sheetOpen = model.expanded() || '';
70
+ // The expanded sheet is capped so the map keeps 35% of the screen: what is left under the stub, if there is one
71
+ if (dock && isPhone() && typeof window !== 'undefined') dock.style.setProperty('--hx-sheet-cap', `${sheetCap(window.innerHeight || 0, model.stubs())}px`);
61
72
  markLast();
62
73
  measureCovered();
63
74
  listeners.forEach((fn) => fn());
64
75
  }
65
76
 
77
+ /** 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) */
78
+ function isOpenOnPage(id, el) {
79
+ if (id === 'forge') return el.classList.contains('forge-open');
80
+ if (id === 'drawer') return el.classList.contains('drawer-open');
81
+ return !el.hidden && el.isConnected;
82
+ }
83
+
84
+ /**
85
+ * 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
86
+ * 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
87
+ * is hidden in the model and is no longer away.
88
+ */
89
+ function syncShown() {
90
+ if (!isPhone()) { // the sheets are panels from 768px: nothing is displaced and nothing is a stub
91
+ away.clear();
92
+ for (const el of elements.values()) el.removeAttribute('data-sheet-away');
93
+ return;
94
+ }
95
+ for (const [id, el] of elements) {
96
+ const open = isOpenOnPage(id, el) && !away.has(id);
97
+ if (!open && !isOpenOnPage(id, el)) away.delete(id);
98
+ if (open && !model.isShown(id)) {
99
+ for (const gone of model.show(id)) displace(gone);
100
+ } else if (!open && model.isShown(id)) model.hide(id);
101
+ }
102
+ for (const [id, el] of elements) {
103
+ const gone = away.has(id);
104
+ const was = el.hasAttribute('data-sheet-away');
105
+ el.toggleAttribute('data-sheet-away', gone);
106
+ if (gone && !el.inert) el.inert = true; // out of reach of Tab while it is away
107
+ else if (!gone && was && isOpenOnPage(id, el)) el.inert = false;
108
+ }
109
+ }
110
+
111
+ /** A sheet displaced by the other of its group: hidden, not closed (the node stays selected); Work is closed by its own code */
112
+ function displace(id) {
113
+ const el = elements.get(id);
114
+ if (!el) return;
115
+ away.add(id);
116
+ const on = options.get(id);
117
+ if (on && on.away) on.away();
118
+ }
119
+
120
+ /** 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 */
121
+ function paintStub(id, el, on) {
122
+ let stub = [...el.children].find((c) => c.classList.contains('hx-sheet-stub'));
123
+ if (!stub) {
124
+ if (!on) return;
125
+ stub = document.createElement('button');
126
+ stub.type = 'button';
127
+ stub.className = 'hx-sheet-stub';
128
+ stub.dataset.corn = 'sheet-stub';
129
+ stub.addEventListener('click', (e) => { e.stopPropagation(); e.preventDefault(); expandSheet(id); const h = [...el.children].find((c) => c.classList.contains('hx-sheet-handle')); if (h && isPhone()) h.focus(); });
130
+ el.append(stub);
131
+ }
132
+ stub.hidden = !on;
133
+ if (!on) return;
134
+ const opt = options.get(id);
135
+ const said = (opt && opt.stub ? opt.stub() : null) || { title: el.getAttribute('aria-label') || id };
136
+ const title = String(said.title || '');
137
+ const badge = said.badge ? String(said.badge) : '';
138
+ const key = `${title}\u0000${badge}\u0000${said.label || ''}`;
139
+ if (stub.dataset.key === key) return;
140
+ stub.dataset.key = key;
141
+ stub.replaceChildren();
142
+ const t = document.createElement('span');
143
+ t.className = 'hx-sheet-stub-title';
144
+ t.textContent = title;
145
+ stub.append(t);
146
+ if (badge) {
147
+ const b = document.createElement('span');
148
+ b.className = 'hx-waiting-badge';
149
+ b.dataset.corn = 'waiting-badge';
150
+ b.setAttribute('aria-hidden', 'true');
151
+ b.textContent = badge;
152
+ stub.append(b);
153
+ }
154
+ stub.setAttribute('aria-label', said.label || (badge ? `${title}, ${badge}` : title));
155
+ }
156
+
157
+ /** Says a stub's words may have changed (the number waiting, a proposal's title): drawn again, if it is showing */
158
+ export function refreshStubs() {
159
+ for (const [id, el] of elements) if (model.stateOf(id) === 'stub') paintStub(id, el, true);
160
+ }
161
+
162
+ /**
163
+ * 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
164
+ * 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.
165
+ */
166
+ export function showSheet(id) {
167
+ if (!elements.has(id)) return;
168
+ away.delete(id);
169
+ apply();
170
+ }
171
+
66
172
  /** The lowest visible sheet gets `data-sheet-last`: it carries the bottom safe-area inset */
67
173
  function markLast() {
68
174
  if (!dock) return;
@@ -123,11 +229,12 @@ export function visibleMapRect() {
123
229
  // ---------------------------------------------------------------------------
124
230
 
125
231
  /** Makes `el` a sheet called `id` (a peek until expanded) and gives it a handle if it has none */
126
- export function registerSheet(id, el) {
232
+ export function registerSheet(id, el, opts = {}) {
127
233
  if (!el) return;
128
234
  unregisterSheet(id, false);
129
235
  model.add(id);
130
236
  elements.set(id, el);
237
+ options.set(id, opts);
131
238
  el.classList.add('hx-sheet');
132
239
  let handle = [...el.children].find((c) => c.classList.contains('hx-sheet-handle'));
133
240
  if (!handle) {
@@ -138,8 +245,8 @@ export function registerSheet(id, el) {
138
245
  handle.innerHTML = '<span data-corn="sheet-grip"></span>';
139
246
  el.prepend(handle);
140
247
  }
141
- bindHandle(id, handle);
142
- el.addEventListener('click', (e) => peekClick(id, e), true);
248
+ if (wiredAs.get(handle) !== id) { wiredAs.set(handle, id); bindHandle(id, handle); } // registered again (a re-init): one listener, or a tap toggles twice
249
+ if (wiredAs.get(el) !== id) { wiredAs.set(el, id); el.addEventListener('click', (e) => peekClick(id, e), true); }
143
250
  apply();
144
251
  }
145
252
 
@@ -148,10 +255,12 @@ export function unregisterSheet(id, redraw = true) {
148
255
  if (!model.has(id)) return;
149
256
  model.remove(id);
150
257
  elements.delete(id);
258
+ options.delete(id);
259
+ away.delete(id);
151
260
  if (redraw) apply();
152
261
  }
153
262
 
154
- export function expandSheet(id) { if (model.expand(id)) apply(); }
263
+ export function expandSheet(id) { syncShown(); if (model.expand(id)) apply(); } // what is open on the page is known first (a bar shown a moment ago)
155
264
  export function collapseSheet(id) { if (model.collapse(id)) apply(); }
156
265
  export function collapseAllSheets() { if (model.collapseAll()) apply(); }
157
266
  export function toggleSheet(id) { if (model.toggle(id)) apply(); }
@@ -190,7 +299,7 @@ function bindHandle(id, handle) {
190
299
  function peekClick(id, e) {
191
300
  if (!isPhone() || model.isExpanded(id)) return;
192
301
  const target = e.target;
193
- if (target.closest && target.closest('.hx-sheet-handle')) return; // the handle's own listener
302
+ 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)
194
303
  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
195
304
  const action = target.closest && target.closest('[data-action]');
196
305
  if (action && /close/.test(action.dataset.action)) return;
@@ -204,6 +313,20 @@ function onMapDown(e) {
204
313
  if (e.target === state.canvas && isPhone()) collapseAllSheets();
205
314
  }
206
315
 
316
+ /** 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) */
317
+ function onEscape(e) {
318
+ if (e.key !== 'Escape' || e.defaultPrevented || !isPhone()) return;
319
+ const id = model.expanded();
320
+ const el = id && elements.get(id);
321
+ if (!el || !el.contains(e.target)) return;
322
+ if (e.target.closest && e.target.closest('select, input, textarea')) return; // a field's own Escape first
323
+ e.preventDefault();
324
+ e.stopPropagation();
325
+ collapseSheet(id);
326
+ const handle = [...el.children].find((c) => c.classList.contains('hx-sheet-handle'));
327
+ if (handle) handle.focus();
328
+ }
329
+
207
330
  /** The dim layer behind an expanded node panel: a tap there collapses the panel to its peek (it no longer closes it) */
208
331
  function onBackdrop(e) {
209
332
  if (!isPhone()) return;
@@ -225,11 +348,13 @@ export function initSheets() {
225
348
  observers = [];
226
349
  model.ids().forEach((id) => model.remove(id));
227
350
  elements.clear();
351
+ options.clear();
352
+ away.clear();
228
353
  lastCovered = 0;
229
354
  dock = found;
230
355
  }
231
- registerSheet('proposal', $('proposal-bar'));
232
- registerSheet('drawer', $('spec-drawer'));
356
+ registerSheet('proposal', $('proposal-bar'), { stub: () => ({ title: ($('proposal-title') && $('proposal-title').textContent) || 'Proposal' }) });
357
+ registerSheet('drawer', $('spec-drawer'), { stub: () => ({ title: ($('drawer-title') && $('drawer-title').textContent) || 'Node' }) });
233
358
  if (wiredDock !== dock) {
234
359
  wiredDock = dock;
235
360
  const backdrop = $('drawer-backdrop');
@@ -248,6 +373,7 @@ export function initSheets() {
248
373
  if (!documentWired) {
249
374
  documentWired = true;
250
375
  for (const type of ['pointerdown', 'touchstart', 'mousedown']) document.addEventListener(type, onMapDown, { passive: true, capture: true });
376
+ document.addEventListener('keydown', onEscape, true);
251
377
  if (typeof window !== 'undefined' && typeof window.matchMedia === 'function') {
252
378
  const mq = window.matchMedia(PHONE_QUERY);
253
379
  if (mq && mq.addEventListener) mq.addEventListener('change', () => { collapseAllSheets(); measureCovered(); });
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @module explorer/waiting-line
3
+ *
4
+ * The one line under the proposal bar's buttons when a run is waiting on the open proposal (spec 0010 D2): "A run is waiting on you: Plan
5
+ * ready: approve to build, … · Answer". Answer opens Work on Runs at that run with its reply field focused (forge-panel.js). The line is its
6
+ * own element, added after the bar's head once, so the bar's own markup is not touched. The runner's words are set as text, never as HTML.
7
+ */
8
+
9
+ import { waitingLineWords } from './waiting-model.js';
10
+ import { t } from './strings.js';
11
+ import { announce } from './keyboard.js';
12
+
13
+ let line = null;
14
+ let saidFor = null;
15
+
16
+ /** The line, made on first use and kept after the bar's head; null when the bar is not on the page */
17
+ function mount() {
18
+ const body = document.getElementById('proposal-body');
19
+ if (!body) return null;
20
+ if (line && line.isConnected) return line;
21
+ line = document.createElement('p');
22
+ line.className = 'hx-waiting-line';
23
+ line.dataset.corn = 'waiting-line';
24
+ line.dataset.cornState = 'waiting';
25
+ line.hidden = true;
26
+ line.innerHTML = '<span data-waiting-words></span> <button type="button" class="hx-btn hx-forge-act" data-waiting-answer data-peek-ok data-corn-kind="secondary"></button>';
27
+ const head = body.querySelector('.hx-proposal-head');
28
+ if (head) head.after(line); else body.append(line);
29
+ return line;
30
+ }
31
+
32
+ /**
33
+ * Shows the line for `run` (the run waiting on the open proposal), or hides it for null. The first time a run is shown for a proposal it is said
34
+ * through the live region once; a repaint of the same run says nothing.
35
+ * @param {{ runId: string, report?: { note?: string } | null } | null} run
36
+ * @param {string | null} key what the line is about (the proposal), so a run is announced once for it
37
+ */
38
+ export function paintWaitingLine(run, key) {
39
+ const el = mount();
40
+ if (!el) return;
41
+ el.hidden = !run;
42
+ if (!run) { saidFor = null; return; }
43
+ const words = waitingLineWords(run);
44
+ el.querySelector('[data-waiting-words]').textContent = words;
45
+ const button = el.querySelector('[data-waiting-answer]');
46
+ button.textContent = t('answer');
47
+ button.dataset.waitingAnswer = run.runId;
48
+ button.setAttribute('aria-label', `${t('answer')}: ${words}`);
49
+ const said = `${key}|${run.runId}`;
50
+ if (saidFor !== said) { saidFor = said; announce(words); }
51
+ }
52
+
53
+ /** For tests: forget the line */
54
+ export function resetWaitingLineForTests() {
55
+ line = null;
56
+ saidFor = null;
57
+ }