@popcorn245/cornfield 0.47.0 → 0.49.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.
Files changed (65) hide show
  1. package/core/contrast.js +6 -6
  2. package/core/strings.js +14 -0
  3. package/explorer/check.js +7 -227
  4. package/explorer/connection.js +3 -3
  5. package/explorer/css/account.css +46 -0
  6. package/explorer/css/base.css +159 -0
  7. package/explorer/css/changes.css +160 -0
  8. package/explorer/css/decision.css +68 -0
  9. package/explorer/css/dialogs.css +50 -0
  10. package/explorer/css/header.css +75 -0
  11. package/explorer/css/layers.css +92 -0
  12. package/explorer/css/live.css +48 -0
  13. package/explorer/css/map.css +43 -0
  14. package/explorer/css/node-panel.css +188 -0
  15. package/explorer/css/page.css +83 -0
  16. package/explorer/css/proposal.css +151 -0
  17. package/explorer/css/search.css +84 -0
  18. package/explorer/css/sheets.css +134 -0
  19. package/explorer/css/whatif.css +55 -0
  20. package/explorer/css/work.css +288 -0
  21. package/explorer/engine/checks.js +252 -0
  22. package/explorer/engine/connection.js +36 -0
  23. package/explorer/engine/layers.js +1 -0
  24. package/explorer/engine/map-style.js +2 -2
  25. package/explorer/engine/player.js +489 -0
  26. package/explorer/engine/proposal.js +373 -0
  27. package/explorer/engine/render.js +1 -0
  28. package/explorer/engine/source.js +315 -0
  29. package/explorer/engine/theme-sheet.js +444 -0
  30. package/explorer/focus-room.js +1 -1
  31. package/explorer/header-fit.js +3 -3
  32. package/explorer/house.css +1 -1
  33. package/explorer/index.html +23 -61
  34. package/explorer/instance.js +82 -6
  35. package/explorer/page-address.js +59 -0
  36. package/explorer/page-pieces.js +33 -5
  37. package/explorer/page.js +20 -20
  38. package/explorer/panels.js +1 -3
  39. package/explorer/pieces/changes.js +502 -0
  40. package/explorer/pieces/decision.js +657 -0
  41. package/explorer/pieces/layers.js +4 -0
  42. package/explorer/pieces/map-stage.css +1 -1
  43. package/explorer/pieces/map.js +113 -2
  44. package/explorer/pieces/node-panel.js +34 -1
  45. package/explorer/pieces/proposal.js +488 -0
  46. package/explorer/pieces/scope-link.js +50 -0
  47. package/explorer/pieces/scope.js +247 -0
  48. package/explorer/pieces/status.css +24 -0
  49. package/explorer/pieces/status.js +144 -0
  50. package/explorer/plugins.js +0 -10
  51. package/explorer/proposal.js +64 -697
  52. package/explorer/review-column.js +24 -439
  53. package/explorer/review.js +27 -673
  54. package/explorer/scenario-ui.js +32 -478
  55. package/explorer/sheets.js +8 -7
  56. package/explorer/theme-model.js +1 -1
  57. package/explorer/theme.js +1 -1
  58. package/explorer/tokens.css +2 -2
  59. package/explorer/view.js +6 -5
  60. package/explorer/waiting-line.js +7 -50
  61. package/package.json +1 -1
  62. package/server/components.js +144 -0
  63. package/server/embed.js +3 -1
  64. package/server/server.js +9 -1
  65. package/explorer/explorer.css +0 -1512
@@ -0,0 +1,252 @@
1
+ /**
2
+ * @module explorer/engine/checks
3
+ *
4
+ * Checked scenarios on an instance (spec 0029 sections 3 and 5, PHY-10; the pure half is check-model.js). When a scenario opens, every check of every
5
+ * step goes to the checker (a plugin's adapter, handed to the piece by the host) in ONE batch; each step then shows its state on the player bar, and on
6
+ * the phone's sheet: a pill in the bar's head ("✕ Refused", seen on the peek too) and a line under the step's text ("Refused: Only an adult can change
7
+ * the household's rooms (ADULT_ONLY_WRITES)"). The current step's node or edge is drawn red, green or amber (scene.js calls `paintVerdict` with the
8
+ * verdict this module puts on the player's snapshot). The **As** menu (the personas on the map, what-if ones too) acts every step as one persona and
9
+ * asks again; **Check again** asks again as it is.
10
+ *
11
+ * createChecks(inst) -> {
12
+ * setChecker(checker) hands over the checker; none means every checked step reads "Not checked"
13
+ * open(scenario, onChange) a scenario opened: ask its batch
14
+ * decorate(snap, scenario) every sync: puts `verdict` (and the As actor) on the snapshot; asks again when what is asked changed
15
+ * paint(bar, snap) every bar update (the controls go on the bar at its first paint)
16
+ * close() the scenario closed: cancel what is in flight
17
+ * version() / checkedScenarioId() / verdicts() / chooseActor(id) / recheck()
18
+ * }
19
+ *
20
+ * A scenario that names an actor turns the People layer on, so its personas are on the map. Nothing is stored: the answers live in memory for the
21
+ * scenario on the map, and go with it.
22
+ */
23
+
24
+ import { escapeHtml as escHtml } from '../../core/text.js';
25
+ import { setLayer } from './layers.js';
26
+ import { layersOf } from '../layer-model.js';
27
+ import { mapIndex, personaLabel, planChecks, runChecks, stepVerdict, verdictWords } from '../check-model.js';
28
+ import { cornState } from '../corn-state.js';
29
+ import { addParts } from './piece.js';
30
+
31
+ const namesActor = (scenario) => !!scenario && (!!scenario.actor || (Array.isArray(scenario.steps) && scenario.steps.some((s) => s && s.actor)));
32
+
33
+ /**
34
+ * @param {ReturnType<import('../instance.js').createInstance>} inst
35
+ * @param {{ checker?: object | null }} [opts]
36
+ */
37
+ export function createChecks(inst, opts = {}) {
38
+ const { state } = inst;
39
+ let checker = opts.checker || null;
40
+ let current = null; // { scenario, as, nodes, plan, answer, controller, onChange }
41
+ let version = 0;
42
+ /** The words last written into each player's check line (paint) */
43
+ const lineHtml = new WeakMap();
44
+ /** What takes the listeners off the controls on the bar (the bar goes with them, but a listener is held until it is taken back) */
45
+ let controlOffs = [];
46
+
47
+ function close() {
48
+ if (current && current.controller) current.controller.abort();
49
+ current = null;
50
+ version++;
51
+ }
52
+
53
+ function setChecker(next) {
54
+ next = next || null;
55
+ if (next !== checker) close(); // answers asked of another checker are not this one's
56
+ checker = next;
57
+ }
58
+
59
+ /** Asks the scenario's one batch; an answer that arrives for a batch no longer on the map is dropped */
60
+ function ask() {
61
+ const c = current;
62
+ if (!c) return;
63
+ if (c.controller) c.controller.abort();
64
+ const items = c.plan.items;
65
+ if (items.length === 0) { c.answer = { status: 'ok', results: new Map(), bad: new Map() }; version++; return; }
66
+ const controller = new AbortController();
67
+ c.controller = controller;
68
+ c.answer = { status: 'pending' };
69
+ version++;
70
+ runChecks(checker, items, { signal: controller.signal }).then(
71
+ ({ results, bad }) => settle(c, controller, { status: 'ok', results, bad }),
72
+ (error) => { if (!controller.signal.aborted) settle(c, controller, { status: 'failed', error }); },
73
+ );
74
+ }
75
+
76
+ function settle(c, controller, answer) {
77
+ if (current !== c || c.controller !== controller) return;
78
+ c.controller = null;
79
+ c.answer = answer;
80
+ version++;
81
+ c.onChange();
82
+ }
83
+
84
+ /** Plans the checks again when the scenario, the As persona or the map changed, and asks again when what is asked changed */
85
+ function replan(scenario, force = false) {
86
+ const c = current;
87
+ if (!c) return;
88
+ if (!force && c.scenario === scenario && c.nodes === state.nodes && c.plan) return;
89
+ const before = c.plan ? c.plan.key : null;
90
+ c.scenario = scenario;
91
+ c.nodes = state.nodes;
92
+ c.plan = planChecks(scenario, mapIndex(state.nodes, state.edges), checker, { as: c.as });
93
+ version++;
94
+ if (force || c.plan.key !== before) ask();
95
+ }
96
+
97
+ /**
98
+ * A scenario opened: its batch is asked.
99
+ * @param {object} scenario
100
+ * @param {() => void} onChange called when an answer arrives or the As menu moved (the player syncs and redraws)
101
+ */
102
+ function open(scenario, onChange = () => {}) {
103
+ // the scenario that is already being checked (opened in the list, now played): its answers stay, only who is told changes
104
+ if (current && current.scenario && scenario && current.scenario.id === scenario.id) { current.onChange = onChange; replan(scenario); return; }
105
+ close();
106
+ current = { scenario: null, as: null, nodes: null, plan: null, answer: { status: 'idle' }, controller: null, onChange };
107
+ if (namesActor(scenario) && layersOf(state).persona !== true) setLayer(inst, 'persona', true, { scenario: true });
108
+ replan(scenario);
109
+ }
110
+
111
+ /** Puts the current step's verdict on the player's snapshot (`snap.verdict`, which scene.js draws) and, while the As menu overrides it, the persona acting the step */
112
+ function decorate(snap, scenario) {
113
+ if (!current) return snap;
114
+ replan(scenario);
115
+ const plan = current.plan.steps[snap.index];
116
+ snap.verdict = stepVerdict(plan, current.answer);
117
+ if (current.as && plan && plan.actor) snap.current.actor = plan.actor;
118
+ return snap;
119
+ }
120
+
121
+ /** The verdict for each step of the scenario on the map (for a test or a caller that wants the whole run) */
122
+ function verdicts() {
123
+ if (!current || !current.plan) return [];
124
+ return current.plan.steps.map((p) => stepVerdict(p, current.answer));
125
+ }
126
+
127
+ /** The As menu moved: every step acts as that persona ('' = as written), and the batch is asked again */
128
+ function chooseActor(id) {
129
+ if (!current) return;
130
+ current.as = id || null;
131
+ replan(current.scenario, true);
132
+ current.onChange();
133
+ }
134
+
135
+ /** Check again: the same batch, asked again */
136
+ function recheck() {
137
+ if (!current) return;
138
+ replan(current.scenario, true);
139
+ current.onChange();
140
+ }
141
+
142
+ /** The pill in the head, the line under the text, and the As row under the controls: only on the bar of a scenario that names an actor, so any other bar is as it was */
143
+ function mountControls(bar) {
144
+ const doc = inst.doc;
145
+ // the pill ends the step's line; the reason is a line of its own under the bar's row, with the As row (the player is one bar: spec 0006 D6)
146
+ const pill = doc.createElement('span');
147
+ pill.className = 'hx-player-verdict';
148
+ pill.dataset.corn = 'state-pill';
149
+ pill.hidden = true;
150
+ const text = bar.querySelector('.hx-player-text');
151
+ if (text) text.append(' ', pill); else bar.prepend(pill);
152
+
153
+ const line = doc.createElement('p');
154
+ line.className = 'hx-player-check';
155
+ line.dataset.corn = 'scenario-check';
156
+ line.hidden = true;
157
+
158
+ const row = doc.createElement('div');
159
+ row.className = 'hx-player-as';
160
+ row.innerHTML = `
161
+ <label class="hx-player-as-label">As <select data-corn="control" class="hx-player-speed hx-player-as-menu" data-check="as" aria-label="Act every step as"></select></label>
162
+ <button data-corn-kind="secondary" type="button" class="hx-btn" data-check="recheck">Check again</button>
163
+ <span class="hx-player-as-sum hx-faint"></span>`;
164
+ const track = bar.querySelector('.hx-player-track');
165
+ if (track) { track.before(line); track.before(row); } else { bar.appendChild(line); bar.appendChild(row); }
166
+ for (const el of [pill, line, row]) { if (el.hasAttribute('data-corn')) el.setAttribute('part', el.getAttribute('data-corn')); }
167
+ addParts(row);
168
+
169
+ const menu = row.querySelector('[data-check=as]');
170
+ const again = row.querySelector('[data-check=recheck]');
171
+ const onMenu = (e) => chooseActor(e.target.value);
172
+ menu.addEventListener('change', onMenu);
173
+ again.addEventListener('click', recheck);
174
+ controlOffs.push(() => { menu.removeEventListener('change', onMenu); again.removeEventListener('click', recheck); });
175
+ }
176
+
177
+ /** The bar goes: its controls let go of their listeners */
178
+ function unmount() {
179
+ const offs = controlOffs;
180
+ controlOffs = [];
181
+ offs.forEach((off) => off());
182
+ }
183
+
184
+ /** The personas on the map, what-if ones included, as the As menu's options */
185
+ function fillMenu(menu) {
186
+ const personas = state.nodes.filter((n) => n.type === 'persona');
187
+ const options = [{ value: '', label: 'As written' }, ...personas.map((p) => ({ value: p.id.replace(/^persona-/, ''), label: personaLabel(p) }))];
188
+ const key = options.map((o) => `${o.value}\u0000${o.label}`).join('\u0001');
189
+ if (menu.dataset.key !== key) {
190
+ menu.innerHTML = options.map((o) => `<option value="${escHtml(o.value)}">${escHtml(o.label)}</option>`).join('');
191
+ menu.dataset.key = key;
192
+ }
193
+ menu.value = current && current.as ? current.as : '';
194
+ }
195
+
196
+ /** What the batch found, in a few words: "4 checks · 1 refused" */
197
+ function summary() {
198
+ const all = verdicts();
199
+ const asked = all.reduce((n, v) => n + v.checks.length, 0);
200
+ if (!current || current.answer.status === 'pending') return asked ? `Checking ${asked}…` : '';
201
+ if (current.answer.status === 'failed') return '';
202
+ const refused = all.filter((v) => v.state === 'refused').length;
203
+ const amber = all.filter((v) => v.mismatch).length;
204
+ const parts = [`${asked} ${asked === 1 ? 'check' : 'checks'}`];
205
+ if (refused) parts.push(`${refused} refused`);
206
+ if (amber) parts.push(`${amber} not as expected`);
207
+ return asked ? parts.join(' · ') : 'No permission questions';
208
+ }
209
+
210
+ /** Paints the step's state on the bar: called with every bar update */
211
+ function paint(bar, snap) {
212
+ if (!bar || !current) return;
213
+ if (!bar.querySelector('.hx-player-as')) {
214
+ if (!namesActor(current.scenario)) return;
215
+ mountControls(bar);
216
+ }
217
+ const pill = bar.querySelector('.hx-player-verdict');
218
+ const line = bar.querySelector('.hx-player-check');
219
+ const row = bar.querySelector('.hx-player-as');
220
+ if (!pill || !line || !row) return;
221
+ const words = snap.done ? null : verdictWords(snap.verdict); // at the end there is no step to say a verdict of
222
+ pill.hidden = !words;
223
+ line.hidden = !words;
224
+ if (words) {
225
+ pill.dataset.tone = words.tone;
226
+ if (cornState(words.tone)) pill.dataset.cornState = cornState(words.tone); else delete pill.dataset.cornState;
227
+ pill.textContent = words.pill;
228
+ pill.title = words.line;
229
+ line.dataset.tone = words.tone;
230
+ // painted every frame: the live line is rewritten only when its words change, so a verdict is said once
231
+ const html = `${escHtml(words.line)}${words.detail ? ` <span class="hx-player-check-detail">${escHtml(words.detail)}</span>` : ''}`;
232
+ if (lineHtml.get(line) !== html) { lineHtml.set(line, html); line.innerHTML = html; }
233
+ }
234
+ fillMenu(row.querySelector('[data-check=as]'));
235
+ row.querySelector('[data-check=recheck]').disabled = current.answer.status === 'pending';
236
+ row.querySelector('.hx-player-as-sum').textContent = summary();
237
+ }
238
+
239
+ return {
240
+ setChecker,
241
+ open,
242
+ close,
243
+ decorate,
244
+ paint,
245
+ unmount,
246
+ verdicts,
247
+ chooseActor,
248
+ recheck,
249
+ version: () => version,
250
+ checkedScenarioId: () => (current && current.scenario ? current.scenario.id : null),
251
+ };
252
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @module explorer/engine/connection
3
+ *
4
+ * The words for a failure that has no answer from the server, for an instance (spec 0011 D2): offline, it is "You are offline", not "cornfield
5
+ * could not be reached", and an action says it was not sent and keeps what the person typed. The banner that says it when the browser goes
6
+ * offline or comes back is the page's (explorer/connection.js); a piece only needs the words.
7
+ */
8
+
9
+ /** The words for an action that got no answer: offline, it was not sent (and what was typed is still there); online, the server did not answer */
10
+ export function notSentWords(inst, what) {
11
+ return inst.offline() ? `You are offline. ${what} was not sent; what you typed is kept. Try again when you are back.` : `cornfield did not answer. ${what} was not sent; what you typed is kept.`;
12
+ }
13
+
14
+ /**
15
+ * `fetch` for a read or a write that carries the maintainer's session: on the instance's own origin it is exactly `inst.fetch`; against a base
16
+ * on another origin (a host app, docs/embedding.md) the request is credentialed, which the server answers for a listed origin. A URL on another
17
+ * origin than the base's (an absolute URL, a `//host` path) is fetched without credentials.
18
+ */
19
+ export function credentialed(inst) {
20
+ return (path, init) => inst.fetch(path, sessionInit(inst, path, init));
21
+ }
22
+
23
+ /**
24
+ * The one rule for the maintainer's session (credentialed() here, and engine/source.js's reads of the config and the map): the fetch options for
25
+ * `path`, with `credentials: 'include'` added when the instance has a base and `path` is on the base's own origin, else `init` as it was
26
+ * (`undefined` stays `undefined`, so a request of the page's own is unchanged). What the caller set itself wins.
27
+ */
28
+ export function sessionInit(inst, path, init) {
29
+ return inst.base && underBase(inst, path) ? { credentials: 'include', ...init } : init;
30
+ }
31
+
32
+ /** Whether `path`, made absolute against the instance's base (as `inst.fetch` makes it), is on the base's origin: only those carry the maintainer's session */
33
+ export function underBase(inst, path) {
34
+ if (!inst.base) return false;
35
+ try { return new URL(inst.url(String(path)), inst.base).origin === new URL(inst.base).origin; } catch { return false; }
36
+ }
@@ -178,6 +178,7 @@ function changed(inst) {
178
178
  inst.touchScene();
179
179
  inst.emit('layers');
180
180
  redrawLayers(inst);
181
+ if (!inst.map) return; // a scope sets the project's layers before the map piece is there; it draws them when it comes
181
182
  inst.map.render();
182
183
  inst.map.ensureFlowLoop();
183
184
  }
@@ -12,7 +12,7 @@
12
12
  * --corn-map-added|changed|removed|edit the diff: a proposal's marks on nodes and edges
13
13
  * --corn-map-selected|match the halo of the selected node, and of a search hit
14
14
  * --corn-map-lit the lines of the focus: the links the selection (or a walk's step) lights
15
- * --corn-map-collision the collision mark of two runs on one node (a DOM mark: explorer.css reads it)
15
+ * --corn-map-collision the collision mark of two runs on one node (a DOM mark: css/map.css reads it)
16
16
  * --corn-map-font the font list of every label on the map (a list of families, `auto` for the map's own)
17
17
  * --corn-map-edge-width the width of an ordinary line, in map px (`auto` keeps each line's own; the focus's and the marks' keep their proportion)
18
18
  * --corn-map-label-min the smallest a label is drawn on screen, in px (11 by default; below 9px it is ignored; a hub's title is 2 more)
@@ -361,7 +361,7 @@ export function createMapStyle(inst) {
361
361
 
362
362
  /**
363
363
  * The config's part of the map theme once the page has it: loads the icons, sets the pixelated flag, and marks the root element
364
- * `data-pixelated` (for the page, <html>) so a stylesheet can sample any DOM image without smoothing too (`image-rendering: pixelated`, explorer.css).
364
+ * `data-pixelated` (for the page, <html>) so a stylesheet can sample any DOM image without smoothing too (`image-rendering`, css/base.css, from page.css's `--hx-pixelated`).
365
365
  * @param {{icons?: Record<string, string>, pixelated?: boolean} | null | undefined} theme `config.theme` of /cornfield/config.json
366
366
  */
367
367
  applyTheme(theme, opts = {}) {