@astl/player 0.0.0 → 0.1.1

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 (107) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/LICENSE +202 -0
  3. package/README.md +37 -0
  4. package/dist/a11y.d.ts +41 -0
  5. package/dist/a11y.d.ts.map +1 -0
  6. package/dist/a11y.js +101 -0
  7. package/dist/a11y.js.map +1 -0
  8. package/dist/attempt/controller.d.ts +104 -0
  9. package/dist/attempt/controller.d.ts.map +1 -0
  10. package/dist/attempt/controller.js +325 -0
  11. package/dist/attempt/controller.js.map +1 -0
  12. package/dist/checker/http.d.ts +17 -0
  13. package/dist/checker/http.d.ts.map +1 -0
  14. package/dist/checker/http.js +115 -0
  15. package/dist/checker/http.js.map +1 -0
  16. package/dist/define.d.ts +35 -0
  17. package/dist/define.d.ts.map +1 -0
  18. package/dist/define.js +84 -0
  19. package/dist/define.js.map +1 -0
  20. package/dist/element/base.d.ts +83 -0
  21. package/dist/element/base.d.ts.map +1 -0
  22. package/dist/element/base.js +239 -0
  23. package/dist/element/base.js.map +1 -0
  24. package/dist/index.d.ts +14 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +8 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/interaction/index.d.ts +12 -0
  29. package/dist/interaction/index.d.ts.map +1 -0
  30. package/dist/interaction/index.js +4 -0
  31. package/dist/interaction/index.js.map +1 -0
  32. package/dist/interaction/keys.d.ts +11 -0
  33. package/dist/interaction/keys.d.ts.map +1 -0
  34. package/dist/interaction/keys.js +13 -0
  35. package/dist/interaction/keys.js.map +1 -0
  36. package/dist/interaction/pick-and-place.d.ts +120 -0
  37. package/dist/interaction/pick-and-place.d.ts.map +1 -0
  38. package/dist/interaction/pick-and-place.js +395 -0
  39. package/dist/interaction/pick-and-place.js.map +1 -0
  40. package/dist/interaction/roving-focus.d.ts +44 -0
  41. package/dist/interaction/roving-focus.d.ts.map +1 -0
  42. package/dist/interaction/roving-focus.js +115 -0
  43. package/dist/interaction/roving-focus.js.map +1 -0
  44. package/dist/messages/en.json +53 -0
  45. package/dist/messages/help/en.json +14 -0
  46. package/dist/messages/help/pl.json +14 -0
  47. package/dist/messages/pl.json +53 -0
  48. package/dist/messages.d.ts +7 -0
  49. package/dist/messages.d.ts.map +1 -0
  50. package/dist/messages.js +18 -0
  51. package/dist/messages.js.map +1 -0
  52. package/dist/player/book.d.ts +13 -0
  53. package/dist/player/book.d.ts.map +1 -0
  54. package/dist/player/book.js +65 -0
  55. package/dist/player/book.js.map +1 -0
  56. package/dist/player/help.d.ts +55 -0
  57. package/dist/player/help.d.ts.map +1 -0
  58. package/dist/player/help.js +484 -0
  59. package/dist/player/help.js.map +1 -0
  60. package/dist/player/player.d.ts +87 -0
  61. package/dist/player/player.d.ts.map +1 -0
  62. package/dist/player/player.js +1229 -0
  63. package/dist/player/player.js.map +1 -0
  64. package/dist/player/styled.d.ts +10 -0
  65. package/dist/player/styled.d.ts.map +1 -0
  66. package/dist/player/styled.js +213 -0
  67. package/dist/player/styled.js.map +1 -0
  68. package/dist/player/styles.d.ts +3 -0
  69. package/dist/player/styles.d.ts.map +1 -0
  70. package/dist/player/styles.js +355 -0
  71. package/dist/player/styles.js.map +1 -0
  72. package/dist/player/templates.d.ts +25 -0
  73. package/dist/player/templates.d.ts.map +1 -0
  74. package/dist/player/templates.js +101 -0
  75. package/dist/player/templates.js.map +1 -0
  76. package/dist/player/view.d.ts +44 -0
  77. package/dist/player/view.d.ts.map +1 -0
  78. package/dist/player/view.js +231 -0
  79. package/dist/player/view.js.map +1 -0
  80. package/dist/runtime.d.ts +45 -0
  81. package/dist/runtime.d.ts.map +1 -0
  82. package/dist/runtime.js +48 -0
  83. package/dist/runtime.js.map +1 -0
  84. package/package.json +57 -4
  85. package/src/a11y.ts +134 -0
  86. package/src/attempt/controller.ts +381 -0
  87. package/src/checker/http.ts +149 -0
  88. package/src/define.ts +114 -0
  89. package/src/element/base.ts +310 -0
  90. package/src/index.ts +18 -0
  91. package/src/interaction/index.ts +11 -0
  92. package/src/interaction/keys.ts +16 -0
  93. package/src/interaction/pick-and-place.ts +464 -0
  94. package/src/interaction/roving-focus.ts +133 -0
  95. package/src/messages/en.json +53 -0
  96. package/src/messages/help/en.json +14 -0
  97. package/src/messages/help/pl.json +14 -0
  98. package/src/messages/pl.json +53 -0
  99. package/src/messages.ts +21 -0
  100. package/src/player/book.ts +70 -0
  101. package/src/player/help.ts +585 -0
  102. package/src/player/player.ts +1368 -0
  103. package/src/player/styled.ts +247 -0
  104. package/src/player/styles.ts +355 -0
  105. package/src/player/templates.ts +142 -0
  106. package/src/player/view.ts +294 -0
  107. package/src/runtime.ts +95 -0
@@ -0,0 +1,585 @@
1
+ /**
2
+ * Help for students in the player, in a module of its own: only a view that offers help (`hasHelp`) loads it, and
3
+ * without it (or when its chunk fails) the exercise plays without help.
4
+ *
5
+ * Under each graded interaction that offers help (after the block holding it, for one in a line of text), and at the
6
+ * foot of each page with help of its own, a row:
7
+ * - "Podpowiedź (1/2)", one hint at a time from the checker, the cost of the next one said before it is used;
8
+ * - "Sprawdź" for that interaction alone (practice with `checkScope: 'interaction'`), counted like any check;
9
+ * - the feedback text of the last verdict, in a box of the verdict's colour and edge;
10
+ * - "Dlaczego?", a disclosure with the explanation.
11
+ * The texts come with check and submit responses as the policy says (`helpShown` in the engine); the view only says
12
+ * what there is (`HELP_ATTRIBUTES`). A row next to a floated or placed interaction floats with it, below it.
13
+ *
14
+ * What a checker sends is read as hostile, like a view: counts are checked and the HTML is cleaned like view content.
15
+ */
16
+ import {
17
+ type AttemptPolicy,
18
+ type Checker,
19
+ CheckerError,
20
+ type CheckResponse,
21
+ type ComponentManifest,
22
+ createI18n,
23
+ type ElementHelp,
24
+ FEEDBACK_VARIANTS,
25
+ type Feedback,
26
+ type GradeResult,
27
+ type GroupVerdict,
28
+ helpPolicy,
29
+ inertStyles,
30
+ type MessageCatalog,
31
+ type PlayerElementApi,
32
+ type SubmitResponse,
33
+ type Translate,
34
+ type UrlPolicy,
35
+ } from '@astl/core';
36
+ import { css, html, nothing, render, type TemplateResult } from 'lit';
37
+ import { renderVerdict } from '../a11y.js';
38
+ import type { AttemptController } from '../attempt/controller.js';
39
+ import en from '../messages/help/en.json' with { type: 'json' };
40
+ import pl from '../messages/help/pl.json' with { type: 'json' };
41
+ import { PLAYER_NAMESPACE, playerMessages } from '../messages.js';
42
+ import { runtime } from '../runtime.js';
43
+ import { cleanContent } from './view.js';
44
+
45
+ /** The help strings of the player namespace: in this module, so a view without help never loads them. */
46
+ export const helpMessages: readonly MessageCatalog[] = [
47
+ { namespace: PLAYER_NAMESPACE, locale: 'en', messages: en },
48
+ { namespace: PLAYER_NAMESPACE, locale: 'pl', messages: pl },
49
+ ];
50
+
51
+ /** What the player gives the help module: the mounted attempt, and how to check, announce and show problems. */
52
+ export interface HelpHost {
53
+ readonly controller: AttemptController;
54
+ /** The mounted elements in view order, with their page index. */
55
+ readonly mounted: readonly {
56
+ readonly element: HTMLElement & PlayerElementApi;
57
+ readonly manifest: ComponentManifest;
58
+ readonly page: number;
59
+ }[];
60
+ /** The page elements, in order. */
61
+ readonly pages: readonly HTMLElement[];
62
+ /** The UI locale. */
63
+ readonly locale: string;
64
+ readonly urlPolicy: UrlPolicy;
65
+ /** The player's shadow root, for the module's stylesheet. */
66
+ readonly root: Node;
67
+ checker(): Checker | null;
68
+ /** Checks one interaction through the player, which shows a failure; resolves either way. */
69
+ check(id: string): Promise<unknown>;
70
+ announce(message: string): void;
71
+ }
72
+
73
+ export interface Help {
74
+ /** Removes the rows and stops listening. */
75
+ destroy(): void;
76
+ }
77
+
78
+ const COST = /^(?:0|1|0?\.\d{1,6}|1\.0{1,6})$/;
79
+
80
+ /** What a view element offers, from its `HELP_ATTRIBUTES`; null when nothing. Costs outside 0..1 are left out. */
81
+ export function readHelp(el: Element): ElementHelp | null {
82
+ const words = (name: string) => (el.getAttribute(name) ?? '').split(' ').filter(Boolean);
83
+ const hints = words('hints').flatMap((w) => (COST.test(w) ? [Number(w)] : []));
84
+ const written = new Set(words('feedback'));
85
+ const feedback = FEEDBACK_VARIANTS.filter((v) => written.has(v));
86
+ const explanation = el.hasAttribute('explanation');
87
+ return hints.length === 0 && feedback.length === 0 && !explanation
88
+ ? null
89
+ : { hints, explanation, feedback };
90
+ }
91
+
92
+ /**
93
+ * Help content from a checker (rich text of format 1) as clean live DOM in a `div`: cleaned in an inert document like
94
+ * a view's content, without components or other custom elements, and without styles (the engine writes none into
95
+ * help; a `style` from elsewhere is parsed under its inert name, so a strict CSP reports nothing, and goes).
96
+ */
97
+ export function readContent(content: string, urlPolicy: UrlPolicy): HTMLElement {
98
+ const template = document.createElement('template');
99
+ template.innerHTML = inertStyles(content);
100
+ const holder = template.content.ownerDocument.createElement('div');
101
+ holder.append(template.content);
102
+ cleanContent(holder, { manifests: new Map(), urlPolicy }, false, false);
103
+ for (const el of Array.from(holder.querySelectorAll('*'))) if (el.localName.includes('-')) el.remove();
104
+ const live = document.importNode(holder, true);
105
+ live.className = 'help-content';
106
+ return live;
107
+ }
108
+
109
+ const isFeedback = (v: unknown): v is Feedback =>
110
+ typeof v === 'object' &&
111
+ v !== null &&
112
+ typeof (v as Feedback).html === 'string' &&
113
+ FEEDBACK_VARIANTS.includes((v as Feedback).variant);
114
+
115
+ const own = <T>(record: Readonly<Record<string, T>> | undefined, id: string): T | undefined =>
116
+ typeof record === 'object' && record !== null && Object.hasOwn(record, id) ? record[id] : undefined;
117
+
118
+ /** The engine's rule for several groups: nothing answered is empty, all right correct, some right partial. */
119
+ function overall(verdicts: readonly GroupVerdict[]): GroupVerdict | null {
120
+ if (verdicts.length === 0) return null;
121
+ if (verdicts.every((v) => v === 'empty')) return 'empty';
122
+ if (verdicts.every((v) => v === 'correct')) return 'correct';
123
+ return verdicts.some((v) => v === 'correct' || v === 'partial') ? 'partial' : 'incorrect';
124
+ }
125
+
126
+ const isCount = (v: unknown): v is number => Number.isInteger(v) && (v as number) >= 0;
127
+
128
+ interface Row {
129
+ /** View element or page id: what the checker's help is keyed by. */
130
+ readonly id: string;
131
+ readonly offer: ElementHelp;
132
+ readonly box: HTMLElement;
133
+ readonly label: string;
134
+ /** The hidden end of the check button's name ("to pytanie"); null where there is no check of one interaction. */
135
+ readonly check: string | null;
136
+ /**
137
+ * Its element says its verdict in words itself (a block, right above): the feedback box then keeps the icon beside
138
+ * its text and leaves the word to assistive tech. A field in a line of text shows an icon only, a page nothing.
139
+ */
140
+ readonly ownVerdict: boolean;
141
+ /** Its groups in a result (an element's own, or those it is graded in); empty for a page. */
142
+ readonly groups: readonly string[];
143
+ /** The slots whose change makes its feedback stale. */
144
+ readonly slots: ReadonlySet<string>;
145
+ /** The language and direction of its element: its help is content in that language, the row's labels UI text. */
146
+ readonly lang: string;
147
+ readonly dir: string;
148
+ hints: { readonly n: number; readonly content: HTMLElement }[];
149
+ /** Hints given so far, and how many there are (what the checker says once it was asked). */
150
+ used: number;
151
+ total: number;
152
+ pending: boolean;
153
+ /** Problem code of the last hint request. */
154
+ problem: string | null;
155
+ verdict: GroupVerdict | null;
156
+ feedback: HTMLElement | null;
157
+ explanation: HTMLElement | null;
158
+ }
159
+
160
+ /** Where an element's row goes: after the block of the page (or column) that holds it. */
161
+ function anchorOf(element: Element): Element {
162
+ let anchor = element;
163
+ while (anchor.parentElement !== null && !/^astl-(page|column|doc)$/.test(anchor.parentElement.localName)) {
164
+ anchor = anchor.parentElement;
165
+ }
166
+ return anchor;
167
+ }
168
+
169
+ /**
170
+ * What a floated or placed block writes of its place: its row takes the same, and clears the float. `margin` brings a
171
+ * centring an author wrote as `margin: 0 auto`, which the style keeps as the four sides.
172
+ */
173
+ const PLACING = [
174
+ 'float',
175
+ 'width',
176
+ 'inline-size',
177
+ 'max-width',
178
+ 'max-inline-size',
179
+ 'margin',
180
+ 'margin-inline',
181
+ 'margin-inline-start',
182
+ 'margin-inline-end',
183
+ ];
184
+
185
+ function place(box: HTMLElement, anchor: Element): void {
186
+ if (!(anchor instanceof HTMLElement)) return;
187
+ for (const name of PLACING) {
188
+ const value = anchor.style.getPropertyValue(name);
189
+ if (value !== '') box.style.setProperty(name, value);
190
+ }
191
+ const float = anchor.style.getPropertyValue('float');
192
+ if (float !== '' && float !== 'none') box.style.setProperty('clear', float);
193
+ }
194
+
195
+ /** Mounts the rows into a mounted view and keeps them in step with the attempt. */
196
+ export function mountHelp(host: HelpHost): Help {
197
+ const { controller, locale } = host;
198
+ const policy: AttemptPolicy = controller.policy;
199
+ const help = helpPolicy(policy);
200
+ const practice = policy.mode === 'practice';
201
+ const t: Translate = createI18n({
202
+ catalogs: [...playerMessages, ...helpMessages, ...runtime.catalogs],
203
+ }).translator(locale, PLAYER_NAMESPACE);
204
+ adoptHelpSheet(host.root);
205
+
206
+ const rows: Row[] = [];
207
+ const after = new Map<Element, Element>();
208
+ const row = (
209
+ owner: Element,
210
+ offer: ElementHelp,
211
+ extra: Pick<Row, 'label' | 'check' | 'ownVerdict' | 'groups' | 'slots'>,
212
+ ) => {
213
+ const box = document.createElement('div');
214
+ box.className = 'astl-help';
215
+ box.setAttribute('part', 'help');
216
+ box.setAttribute('role', 'group');
217
+ box.setAttribute('aria-label', extra.label);
218
+ box.lang = locale;
219
+ const entry: Row = {
220
+ id: owner.id,
221
+ offer,
222
+ box,
223
+ ...extra,
224
+ lang: owner.closest('[lang]')?.getAttribute('lang') ?? '',
225
+ dir: owner.closest('[dir]')?.getAttribute('dir') ?? '',
226
+ hints: [],
227
+ used: 0,
228
+ total: offer.hints.length,
229
+ pending: false,
230
+ problem: null,
231
+ verdict: null,
232
+ feedback: null,
233
+ explanation: null,
234
+ };
235
+ rows.push(entry);
236
+ return entry;
237
+ };
238
+
239
+ let field = 0;
240
+ for (const { element, manifest } of host.mounted) {
241
+ const inline = manifest.display === 'inline';
242
+ if (inline) field++;
243
+ const offer = manifest.graded ? readHelp(element) : null;
244
+ if (offer === null) continue;
245
+ const n = field;
246
+ const entry = row(element, offer, {
247
+ label: inline ? t('help.labelField', { n }) : t('help.label'),
248
+ check:
249
+ practice && help.checkScope === 'interaction'
250
+ ? inline
251
+ ? t('help.checkField', { n })
252
+ : t('help.check')
253
+ : null,
254
+ ownVerdict: !inline,
255
+ groups: element.groupIds ?? [element.id],
256
+ slots: new Set(element.slotIds),
257
+ });
258
+ const anchor = anchorOf(element);
259
+ (after.get(anchor) ?? anchor).after(entry.box);
260
+ after.set(anchor, entry.box);
261
+ place(entry.box, anchor);
262
+ }
263
+ host.pages.forEach((page, index) => {
264
+ const offer = page.localName === 'astl-page' ? readHelp(page) : null;
265
+ if (offer === null) return;
266
+ const entry = row(page, offer, {
267
+ label: t('help.labelPage'),
268
+ check: null,
269
+ ownVerdict: false,
270
+ groups: [],
271
+ slots: new Set(controller.pages[index]?.slotIds ?? []),
272
+ });
273
+ entry.box.dataset.page = '';
274
+ page.append(entry.box);
275
+ });
276
+
277
+ const content = (value: unknown, entry: Row): HTMLElement | null => {
278
+ if (typeof value !== 'string') return null;
279
+ const shown = readContent(value, host.urlPolicy);
280
+ if (entry.lang) shown.lang = entry.lang;
281
+ if (entry.dir) shown.dir = entry.dir;
282
+ return shown;
283
+ };
284
+
285
+ /** Takes in what a response says of the rows it graded (`only`: a check of one interaction). */
286
+ const apply = (response: CheckResponse | SubmitResponse, only: string | null): void => {
287
+ const result: GradeResult | null = response.result;
288
+ if (result === null) return;
289
+ for (const entry of rows) {
290
+ if (only !== null && entry.id !== only) continue;
291
+ const verdicts = result.groups.filter((g) => entry.groups.includes(g.id)).map((g) => g.verdict);
292
+ const graded = entry.groups.length === 0 ? Object.hasOwn(result.pages, entry.id) : verdicts.length > 0;
293
+ if (!graded) continue;
294
+ const feedback = own(response.feedback, entry.id);
295
+ entry.verdict = isFeedback(feedback)
296
+ ? feedback.variant === 'omitted'
297
+ ? 'empty'
298
+ : feedback.variant
299
+ : overall(verdicts);
300
+ entry.feedback = isFeedback(feedback) ? content(feedback.html, entry) : null;
301
+ entry.explanation = content(own(response.explanations, entry.id), entry) ?? entry.explanation;
302
+ }
303
+ };
304
+
305
+ const update = (): void => {
306
+ for (const entry of rows) render(view(entry), entry.box);
307
+ };
308
+
309
+ const ask = async (entry: Row): Promise<void> => {
310
+ const checker = host.checker();
311
+ if (entry.pending) return;
312
+ entry.pending = true;
313
+ entry.problem = null;
314
+ update();
315
+ try {
316
+ if (checker?.hint === undefined) throw checker === null ? 'no_checker' : 'hints_not_allowed';
317
+ const hint = await checker.hint({ element: entry.id });
318
+ const index = isCount(hint.index) ? hint.index : entry.used;
319
+ const shown = content(hint.html, entry) as HTMLElement;
320
+ entry.hints = [...entry.hints, { n: index + 1, content: shown }];
321
+ entry.used = index + 1;
322
+ entry.total = isCount(hint.left) ? entry.used + hint.left : Math.max(entry.total, entry.used);
323
+ host.announce(t('help.hintAnnounce', { n: index + 1, text: shown.textContent?.trim() ?? '' }));
324
+ } catch (error) {
325
+ const code =
326
+ error instanceof CheckerError
327
+ ? error.problem.code
328
+ : typeof error === 'string'
329
+ ? error
330
+ : 'internal_error';
331
+ if (code === 'hints_exhausted') entry.total = entry.used;
332
+ entry.problem = code;
333
+ host.announce(problemText(code));
334
+ } finally {
335
+ entry.pending = false;
336
+ const active = host.root instanceof ShadowRoot ? host.root.activeElement : document.activeElement;
337
+ const focused = entry.box.contains(active);
338
+ update();
339
+ // The last hint takes its button away: the focus goes to that hint, not to the page.
340
+ if (focused && entry.used >= entry.total) {
341
+ entry.box.querySelector<HTMLElement>('.help-hints li:last-child')?.focus();
342
+ }
343
+ }
344
+ };
345
+
346
+ const problemText = (code: string): string => {
347
+ const message = t(`error.${code}`);
348
+ return message === `error.${code}` ? t('error.internal_error') : message;
349
+ };
350
+
351
+ const view = (entry: Row): TemplateResult | typeof nothing => {
352
+ const open = !controller.readOnly;
353
+ const next = entry.used < entry.total && help.hints && practice && open ? entry.used : null;
354
+ const cost = next === null ? 0 : (entry.offer.hints[next] ?? 0);
355
+ const costId = `astl-help-cost-${rows.indexOf(entry)}`;
356
+ const check = entry.check !== null && open;
357
+ const shows =
358
+ next !== null ||
359
+ check ||
360
+ entry.hints.length > 0 ||
361
+ entry.feedback !== null ||
362
+ entry.explanation !== null;
363
+ entry.box.hidden = !shows && entry.problem === null;
364
+ if (entry.box.hidden) return nothing;
365
+ return html`${
366
+ next === null && !check
367
+ ? nothing
368
+ : html`<div class="help-actions">
369
+ ${
370
+ next === null
371
+ ? nothing
372
+ : html`<span class="help-hint"
373
+ ><button
374
+ type="button"
375
+ class="button"
376
+ aria-describedby=${cost > 0 ? costId : nothing}
377
+ aria-disabled=${entry.pending ? 'true' : nothing}
378
+ @click=${() => void ask(entry)}
379
+ >
380
+ ${t('help.hint', { n: next + 1, total: entry.total })}
381
+ </button>
382
+ ${
383
+ cost > 0
384
+ ? html`<span class="help-cost" id=${costId}
385
+ >${t('help.hintCost', { percent: Math.round(cost * 100) })}</span
386
+ >`
387
+ : nothing
388
+ }</span
389
+ >`
390
+ }
391
+ ${
392
+ check
393
+ ? html`<button
394
+ type="button"
395
+ class="button"
396
+ aria-disabled=${controller.canCheck() ? nothing : 'true'}
397
+ @click=${() => {
398
+ if (controller.canCheck()) void host.check(entry.id);
399
+ }}
400
+ >
401
+ ${t('action.check')}<span class="astl-vh"> ${entry.check}</span>
402
+ </button>`
403
+ : nothing
404
+ }
405
+ </div>`
406
+ }
407
+ ${
408
+ entry.hints.length === 0
409
+ ? nothing
410
+ : html`<ol class="help-hints">
411
+ ${entry.hints.map(
412
+ (hint) =>
413
+ html`<li tabindex="-1">
414
+ <span class="help-label">${t('help.hintItem', { n: hint.n })}</span>${hint.content}
415
+ </li>`,
416
+ )}
417
+ </ol>`
418
+ }
419
+ ${
420
+ entry.feedback === null || entry.verdict === null
421
+ ? nothing
422
+ : html`<div class="help-feedback" data-verdict=${entry.verdict} ?data-own=${entry.ownVerdict}>
423
+ ${renderVerdict(entry.verdict, t, { lang: locale, hiddenWord: entry.ownVerdict })}${entry.feedback}
424
+ </div>`
425
+ }
426
+ ${
427
+ entry.explanation === null
428
+ ? nothing
429
+ : html`<details class="help-why">
430
+ <summary>${t('help.why')}</summary>
431
+ ${entry.explanation}
432
+ </details>`
433
+ }
434
+ ${entry.problem === null ? nothing : html`<p class="help-problem" role="alert">${problemText(entry.problem)}</p>`}`;
435
+ };
436
+
437
+ const unsubscribe = controller.subscribe((change) => {
438
+ if (change.type === 'result') apply(change.response, null);
439
+ else if (change.type === 'element') {
440
+ apply(change.response, change.id);
441
+ const entry = rows.find((r) => r.id === change.id);
442
+ if (entry?.verdict != null) {
443
+ host.announce(
444
+ [t(`verdict.${entry.verdict}`), entry.feedback?.textContent?.trim()].filter(Boolean).join('. '),
445
+ );
446
+ }
447
+ } else if (change.type === 'answers') {
448
+ const changed = Object.keys(change.answers);
449
+ for (const entry of rows) {
450
+ if (changed.some((id) => entry.slots.has(id))) {
451
+ entry.verdict = null;
452
+ entry.feedback = null;
453
+ }
454
+ }
455
+ } else if (change.type === 'reset') {
456
+ for (const entry of rows) {
457
+ entry.verdict = null;
458
+ entry.feedback = null;
459
+ entry.explanation = null;
460
+ }
461
+ }
462
+ update();
463
+ });
464
+ update();
465
+
466
+ return {
467
+ destroy() {
468
+ unsubscribe();
469
+ for (const entry of rows) entry.box.remove();
470
+ },
471
+ };
472
+ }
473
+
474
+ // Rows sit in the content's flow, in the UI's type size. A page's row opens with a line across the page, under its
475
+ // floats. The feedback box has the verdict's colour and edge (solid, double, dashed, dotted), never colour alone;
476
+ // under a block, which says its verdict in words right above, it keeps the icon beside the text, the word hidden.
477
+ // A hint's cost stays by its button (closer than the next action), and wraps under it, never beside "Check".
478
+ const helpSheet = css`
479
+ .canvas .astl-help {
480
+ display: flex;
481
+ flex-direction: column;
482
+ align-items: flex-start;
483
+ gap: var(--astl-space-2);
484
+ margin-block: 0 var(--astl-space-4);
485
+ font-size: var(--astl-size-ui);
486
+ line-height: 1.4;
487
+ }
488
+ .canvas .astl-help[hidden] {
489
+ display: none;
490
+ }
491
+ .canvas .astl-help[data-page] {
492
+ clear: both;
493
+ padding-block-start: var(--astl-space-3);
494
+ border-block-start: var(--astl-outline-width) solid var(--astl-line);
495
+ }
496
+ .help-actions {
497
+ display: flex;
498
+ flex-wrap: wrap;
499
+ align-items: center;
500
+ gap: var(--astl-space-2) var(--astl-space-5);
501
+ }
502
+ .help-hint {
503
+ display: inline-flex;
504
+ flex-wrap: wrap;
505
+ align-items: center;
506
+ gap: var(--astl-space-1) var(--astl-space-2);
507
+ }
508
+ .help-cost {
509
+ color: var(--astl-ink-muted);
510
+ font-size: var(--astl-size-small);
511
+ }
512
+ .help-hints {
513
+ display: grid;
514
+ gap: var(--astl-space-2);
515
+ margin: 0;
516
+ padding: 0;
517
+ list-style: none;
518
+ }
519
+ .help-hints li,
520
+ .help-feedback,
521
+ .help-why {
522
+ box-sizing: border-box;
523
+ max-inline-size: 100%;
524
+ padding-block: var(--astl-space-2);
525
+ padding-inline: var(--astl-space-3);
526
+ border: var(--astl-outline-width) solid var(--astl-line);
527
+ border-radius: var(--astl-radius-m);
528
+ background: var(--astl-surface-raised);
529
+ color: var(--astl-ink);
530
+ }
531
+ .help-label {
532
+ display: block;
533
+ color: var(--astl-ink-muted);
534
+ font-size: var(--astl-size-small);
535
+ font-weight: 600;
536
+ }
537
+ .help-feedback {
538
+ border-style: var(--astl-verdict-border);
539
+ border-color: var(--astl-verdict-color);
540
+ background: var(--astl-verdict-soft);
541
+ }
542
+ .help-feedback[data-own] {
543
+ display: flex;
544
+ align-items: flex-start;
545
+ gap: var(--astl-space-2);
546
+ }
547
+ .help-feedback[data-own] > .astl-verdict {
548
+ block-size: 1lh;
549
+ }
550
+ .help-feedback[data-own] > .help-content {
551
+ flex: 1;
552
+ min-inline-size: 0;
553
+ }
554
+ .help-content > :first-child {
555
+ margin-block-start: 0;
556
+ }
557
+ .help-content > :last-child {
558
+ margin-block-end: 0;
559
+ }
560
+ .help-why {
561
+ padding-block: 0;
562
+ }
563
+ .help-why summary {
564
+ display: flex;
565
+ align-items: center;
566
+ min-block-size: var(--astl-target-min);
567
+ min-inline-size: var(--astl-target-min);
568
+ cursor: pointer;
569
+ font-weight: 600;
570
+ }
571
+ .help-why[open] {
572
+ padding-block-end: var(--astl-space-2);
573
+ }
574
+ .help-problem {
575
+ margin: 0;
576
+ color: var(--astl-ink);
577
+ }
578
+ `.styleSheet;
579
+
580
+ /** Adopts the rows' stylesheet in a player's root, once. */
581
+ function adoptHelpSheet(root: Node): void {
582
+ if (helpSheet !== undefined && root instanceof ShadowRoot && !root.adoptedStyleSheets.includes(helpSheet)) {
583
+ root.adoptedStyleSheets = [...root.adoptedStyleSheets, helpSheet];
584
+ }
585
+ }