@astl/player 0.3.1 → 0.3.3

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.
@@ -3,12 +3,16 @@
3
3
  * practice that checks one question at a time; without it (or when its chunk fails) the exercise plays without help.
4
4
  *
5
5
  * Under each graded interaction that offers help, and under every one where practice checks one question at a time
6
- * (`checkScope: 'interaction'`), after the block holding it for one in a line of text, a row:
7
- * - "Podpowiedź 1 z 2", one hint at a time from the checker, what the next one costs said before it is used;
6
+ * (`checkScope: 'interaction'`), after the block holding it for one in a line of text, a row, in this order:
8
7
  * - "Sprawdź" for that interaction alone, counted like any check ("Sprawdź pole 1" for a field in a line of text whose
9
8
  * paragraph holds other fields, their rows under each other after it; the end of the name is hidden otherwise);
9
+ * - "Podpowiedź 1 z 2", one hint at a time from the checker, what the next one costs said beside it before it is used,
10
+ * and the hints given; with `hintPosition` `top` these go at the top of the question instead, a "?" at its end;
10
11
  * - the feedback text of the last verdict, in a box of the verdict's colour and edge;
11
12
  * - "Dlaczego?", a disclosure with the explanation.
13
+ * Every control and text of a row has a part of its own (`check-button`, `hint-offer`, `hint-button`, `hint-cost`,
14
+ * `hints`, `help-feedback`, `help-why`, `help-problem`), in a flex row (`help-actions`) inside a flex column (`help`), so
15
+ * a host orders, aligns, spaces or hides them in its own CSS; the DOM goes in the order shown.
12
16
  * A Check element (`astl-check`) checks its questions at once, as one check, from a row where it is, and they have no
13
17
  * Check of their own; so does an automatic one, after the last of the questions of a run of text (paragraphs, lists,
14
18
  * headings ending it) that no Check element has, when there are two or more ("Sprawdź", then "2 z 3 poprawnie").
@@ -92,11 +96,22 @@ export interface HelpHost {
92
96
  */
93
97
  overall(response: CheckResponse | null): string | null;
94
98
  announce(message: string): void;
99
+ /** Where a question's hint goes now (`<astl-player hint-position>`). */
100
+ hintPosition(): HintPosition;
95
101
  }
96
102
 
103
+ /**
104
+ * Where a question's hint goes: `row`, in its row with its Check (the default); `top`, at the top of the question, a
105
+ * "?" at its end with the cost beside it and the hints given under it. A page's hint, and the hint of a question that
106
+ * floats or is placed, stays in its row.
107
+ */
108
+ export type HintPosition = 'row' | 'top';
109
+
97
110
  export interface Help {
98
111
  /** Removes the rows and stops listening. */
99
112
  destroy(): void;
113
+ /** Draws the rows again: the host changed where hints go. */
114
+ refresh(): void;
100
115
  }
101
116
 
102
117
  const COST = /^(?:0|1|0?\.\d{1,6}|1\.0{1,6})$/;
@@ -166,7 +181,19 @@ interface Row {
166
181
  readonly id: string;
167
182
  readonly offer: ElementHelp;
168
183
  readonly box: HTMLElement;
184
+ /**
185
+ * The block its hint goes before at the top of its question (`hintPosition` `top`); null where it has no hints, for a
186
+ * page, and where that block floats or is placed.
187
+ */
188
+ anchor: Element | null;
189
+ /** Its hint's box there, made the first time it is needed. */
190
+ top: HTMLElement | null;
169
191
  readonly label: string;
192
+ /**
193
+ * The name of its hint's box at the top of its question ("Podpowiedzi do tego pytania"), not the row's: a screen
194
+ * reader hears two groups there. Null for a page, whose hint stays in its row.
195
+ */
196
+ readonly topLabel: string | null;
170
197
  /**
171
198
  * The end of the check button's name ("to pytanie", "pole 2"), hidden unless `named`; a page's row says "Sprawdź tę
172
199
  * stronę" instead. Null where the row has no Check.
@@ -241,6 +268,11 @@ const PLACING = [
241
268
  'margin-inline-end',
242
269
  ];
243
270
 
271
+ /** Whether a block floats or is placed (`PLACING`): what goes beside it then stays in its row. */
272
+ function placed(anchor: Element): boolean {
273
+ return anchor instanceof HTMLElement && PLACING.some((name) => anchor.style.getPropertyValue(name) !== '');
274
+ }
275
+
244
276
  function place(box: HTMLElement, anchor: Element): void {
245
277
  if (!(anchor instanceof HTMLElement)) return;
246
278
  for (const name of PLACING) {
@@ -264,10 +296,12 @@ export function mountHelp(host: HelpHost): Help {
264
296
 
265
297
  const rows: Row[] = [];
266
298
  const after = new Map<Element, Element>();
299
+ /** The last top box before a block, so the next goes after it (the fields of a line, in order). */
300
+ const before = new Map<Element, Element>();
267
301
  const row = (
268
302
  owner: Element,
269
303
  offer: ElementHelp,
270
- extra: Pick<Row, 'label' | 'check' | 'named' | 'ownVerdict' | 'groups' | 'slots'>,
304
+ extra: Pick<Row, 'label' | 'topLabel' | 'check' | 'named' | 'ownVerdict' | 'groups' | 'slots'>,
271
305
  ) => {
272
306
  const box = document.createElement('div');
273
307
  box.className = 'astl-help';
@@ -279,6 +313,8 @@ export function mountHelp(host: HelpHost): Help {
279
313
  id: owner.id,
280
314
  offer,
281
315
  box,
316
+ anchor: null,
317
+ top: null,
282
318
  ...extra,
283
319
  lang: owner.closest('[lang]')?.getAttribute('lang') ?? '',
284
320
  dir: owner.closest('[dir]')?.getAttribute('dir') ?? '',
@@ -352,6 +388,7 @@ export function mountHelp(host: HelpHost): Help {
352
388
  const anchor = anchorOf(element);
353
389
  const entry = row(element, offer, {
354
390
  label: inline ? t('help.labelField', { n }) : t('help.label'),
391
+ topLabel: inline ? t('help.topLabelField', { n }) : t('help.topLabel'),
355
392
  check:
356
393
  each && graded && !covered.has(element.id)
357
394
  ? inline
@@ -366,6 +403,7 @@ export function mountHelp(host: HelpHost): Help {
366
403
  (after.get(anchor) ?? anchor).after(entry.box);
367
404
  after.set(anchor, entry.box);
368
405
  place(entry.box, anchor);
406
+ if (offer.hints.length > 0 && !placed(anchor)) entry.anchor = anchor;
369
407
  }
370
408
  host.pages.forEach((page, index) => {
371
409
  if (page.localName !== 'astl-page') return;
@@ -375,6 +413,7 @@ export function mountHelp(host: HelpHost): Help {
375
413
  if (offer === null && !checked) return;
376
414
  const entry = row(page, offer ?? NO_HELP, {
377
415
  label: t('help.labelPage'),
416
+ topLabel: null,
378
417
  check: checked ? '' : null,
379
418
  named: false,
380
419
  ownVerdict: false,
@@ -479,10 +518,11 @@ export function mountHelp(host: HelpHost): Help {
479
518
  check.box.hidden = controller.readOnly;
480
519
  if (controller.readOnly) return nothing;
481
520
  const summary = summaryOf(check);
482
- return html`<div class="help-actions">
521
+ return html`<div class="help-actions" part="help-actions">
483
522
  <button
484
523
  type="button"
485
524
  class="button"
525
+ part="check-button"
486
526
  aria-describedby=${summary === null ? nothing : check.summaryId}
487
527
  aria-disabled=${controller.canCheck() ? nothing : 'true'}
488
528
  @click=${() => {
@@ -525,11 +565,13 @@ export function mountHelp(host: HelpHost): Help {
525
565
  } finally {
526
566
  entry.pending = false;
527
567
  const active = host.root instanceof ShadowRoot ? host.root.activeElement : document.activeElement;
528
- const focused = entry.box.contains(active);
568
+ // The hint is in its row, or at the top of its question.
569
+ const holder = entry.top !== null && host.hintPosition() === 'top' ? entry.top : entry.box;
570
+ const focused = holder.contains(active);
529
571
  update();
530
572
  // The last hint takes its button away: the focus goes to that hint, not to the page.
531
573
  if (focused && entry.used >= entry.total) {
532
- entry.box.querySelector<HTMLElement>('.help-hints li:last-child')?.focus();
574
+ holder.querySelector<HTMLElement>('.help-hints li:last-child')?.focus();
533
575
  }
534
576
  }
535
577
  };
@@ -539,6 +581,21 @@ export function mountHelp(host: HelpHost): Help {
539
581
  return message === `error.${code}` ? t('error.internal_error') : message;
540
582
  };
541
583
 
584
+ /** A question's box for its hint at its top: before its block, after the boxes of the fields before it there. */
585
+ const topBox = (entry: Row, anchor: Element): HTMLElement => {
586
+ const top = document.createElement('div');
587
+ top.className = 'astl-help-top';
588
+ top.setAttribute('part', 'help-top');
589
+ top.setAttribute('role', 'group');
590
+ top.setAttribute('aria-label', entry.topLabel ?? entry.label);
591
+ top.lang = locale;
592
+ const last = before.get(anchor);
593
+ if (last === undefined) anchor.before(top);
594
+ else last.after(top);
595
+ before.set(anchor, top);
596
+ return top;
597
+ };
598
+
542
599
  const view = (entry: Row): TemplateResult | typeof nothing => {
543
600
  const open = !controller.readOnly;
544
601
  const next = entry.used < entry.total && help.hints && practice && open ? entry.used : null;
@@ -547,49 +604,70 @@ export function mountHelp(host: HelpHost): Help {
547
604
  const points = next === null ? undefined : entry.offer.hintPoints?.[next];
548
605
  const costId = `astl-help-cost-${rows.indexOf(entry)}`;
549
606
  const check = entry.check !== null && open;
607
+ // The hint at the top of the question: its next hint, the hints given and a failed request's problem go there.
608
+ const top = entry.anchor !== null && host.hintPosition() === 'top';
609
+ if (top && entry.top === null) entry.top = topBox(entry, entry.anchor as Element);
610
+ const costView =
611
+ cost > 0
612
+ ? html`<span class="help-cost" part="hint-cost" id=${costId}
613
+ >${points === undefined ? t('help.hintCost', { percent: Math.round(cost * 100) }) : t('help.hintPoints', { points })}</span
614
+ >`
615
+ : nothing;
616
+ const label = next === null ? '' : t('help.hint', { n: next + 1, total: entry.total });
617
+ // At the top, a "?" at the end, its cost before it; in the row, the button with its cost after it.
618
+ const offerView =
619
+ next === null
620
+ ? nothing
621
+ : html`<span class="help-hint" part="hint-offer"
622
+ >${top ? costView : nothing}<button
623
+ type="button"
624
+ class=${top ? 'button help-hint-compact' : 'button'}
625
+ part="hint-button"
626
+ title=${top ? label : nothing}
627
+ aria-describedby=${cost > 0 ? costId : nothing}
628
+ aria-disabled=${entry.pending ? 'true' : nothing}
629
+ @click=${() => void ask(entry)}
630
+ >
631
+ ${top ? html`<span aria-hidden="true">?</span><span class="astl-vh">${label}</span>` : label}</button
632
+ >${top ? nothing : costView}</span
633
+ >`;
634
+ const hintsView =
635
+ entry.hints.length === 0
636
+ ? nothing
637
+ : html`<ol class="help-hints" part="hints">
638
+ ${entry.hints.map(
639
+ (hint) =>
640
+ html`<li tabindex="-1">
641
+ <span class="help-label">${t('help.hintItem', { n: hint.n })}</span>${hint.content}
642
+ </li>`,
643
+ )}
644
+ </ol>`;
645
+ const problemView =
646
+ entry.problem === null
647
+ ? nothing
648
+ : html`<p class="help-problem" part="help-problem" role="alert">${problemText(entry.problem)}</p>`;
649
+ if (entry.top !== null) {
650
+ const shown = top && (next !== null || entry.hints.length > 0 || entry.problem !== null);
651
+ entry.top.hidden = !shown;
652
+ render(shown ? html`${offerView}${hintsView}${problemView}` : nothing, entry.top);
653
+ }
550
654
  const shows =
551
- next !== null ||
552
655
  check ||
553
- entry.hints.length > 0 ||
554
656
  entry.feedback !== null ||
555
- entry.explanation !== null;
556
- entry.box.hidden = !shows && entry.problem === null;
657
+ entry.explanation !== null ||
658
+ (!top && (next !== null || entry.hints.length > 0));
659
+ entry.box.hidden = !shows && (top || entry.problem === null);
557
660
  if (entry.box.hidden) return nothing;
558
661
  return html`${
559
- next === null && !check
662
+ !check && (top || next === null)
560
663
  ? nothing
561
- : html`<div class="help-actions">
562
- ${
563
- next === null
564
- ? nothing
565
- : html`<span class="help-hint"
566
- ><button
567
- type="button"
568
- class="button"
569
- aria-describedby=${cost > 0 ? costId : nothing}
570
- aria-disabled=${entry.pending ? 'true' : nothing}
571
- @click=${() => void ask(entry)}
572
- >
573
- ${t('help.hint', { n: next + 1, total: entry.total })}
574
- </button>
575
- ${
576
- cost > 0
577
- ? html`<span class="help-cost" id=${costId}
578
- >${
579
- points === undefined
580
- ? t('help.hintCost', { percent: Math.round(cost * 100) })
581
- : t('help.hintPoints', { points })
582
- }</span
583
- >`
584
- : nothing
585
- }</span
586
- >`
587
- }
664
+ : html`<div class="help-actions" part="help-actions">
588
665
  ${
589
666
  check
590
667
  ? html`<button
591
668
  type="button"
592
669
  class="button"
670
+ part="check-button"
593
671
  aria-disabled=${controller.canCheck() ? nothing : 'true'}
594
672
  @click=${() => {
595
673
  if (controller.canCheck()) void host.check(entry.id);
@@ -604,37 +682,26 @@ export function mountHelp(host: HelpHost): Help {
604
682
  }
605
683
  </button>`
606
684
  : nothing
607
- }
685
+ }${top ? nothing : offerView}
608
686
  </div>`
609
687
  }
610
- ${
611
- entry.hints.length === 0
612
- ? nothing
613
- : html`<ol class="help-hints">
614
- ${entry.hints.map(
615
- (hint) =>
616
- html`<li tabindex="-1">
617
- <span class="help-label">${t('help.hintItem', { n: hint.n })}</span>${hint.content}
618
- </li>`,
619
- )}
620
- </ol>`
621
- }
688
+ ${top ? nothing : hintsView}
622
689
  ${
623
690
  entry.feedback === null || entry.verdict === null
624
691
  ? nothing
625
- : html`<div class="help-feedback" data-verdict=${entry.verdict} ?data-own=${entry.ownVerdict}>
692
+ : html`<div class="help-feedback" part="help-feedback" data-verdict=${entry.verdict} ?data-own=${entry.ownVerdict}>
626
693
  ${renderVerdict(entry.verdict, t, { lang: locale, hiddenWord: entry.ownVerdict })}${entry.feedback}
627
694
  </div>`
628
695
  }
629
696
  ${
630
697
  entry.explanation === null
631
698
  ? nothing
632
- : html`<details class="help-why">
699
+ : html`<details class="help-why" part="help-why">
633
700
  <summary>${t('help.why')}</summary>
634
701
  ${entry.explanation}
635
702
  </details>`
636
703
  }
637
- ${entry.problem === null ? nothing : html`<p class="help-problem" role="alert">${problemText(entry.problem)}</p>`}`;
704
+ ${top ? nothing : problemView}`;
638
705
  };
639
706
 
640
707
  /**
@@ -745,19 +812,26 @@ export function mountHelp(host: HelpHost): Help {
745
812
  return {
746
813
  destroy() {
747
814
  unsubscribe();
748
- for (const entry of rows) entry.box.remove();
815
+ for (const entry of rows) {
816
+ entry.box.remove();
817
+ entry.top?.remove();
818
+ }
749
819
  for (const check of checkRows) {
750
820
  check.box.parentElement?.removeAttribute('data-shown');
751
821
  check.box.remove();
752
822
  }
753
823
  },
824
+ refresh: update,
754
825
  };
755
826
  }
756
827
 
757
828
  // 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
758
829
  // floats. The feedback box has the verdict's colour and edge (solid, double, dashed, dotted), never colour alone;
759
830
  // under a block, which says its verdict in words right above, it keeps the icon beside the text, the word hidden.
760
- // A hint's cost stays by its button (closer than the next action), and wraps under it, never beside "Check".
831
+ // "Check" comes first, then the hint: its cost stays by its button (closer than "Check" is) and wraps under it. At the
832
+ // top of a question the hint is a round "?" at the end of the line, its cost before it, the hints given under them.
833
+ // The "?" is as wide as a button of one line is tall, so it stays round with larger text; when its cost wraps, the
834
+ // cost's lines and the "?" keep to the end.
761
835
  const helpSheet = css`
762
836
  .canvas .astl-help {
763
837
  display: flex;
@@ -798,6 +872,33 @@ const helpSheet = css`
798
872
  color: var(--astl-ink-muted);
799
873
  font-size: var(--astl-size-small);
800
874
  }
875
+ .canvas .astl-help-top {
876
+ display: flex;
877
+ flex-direction: column;
878
+ align-items: flex-end;
879
+ gap: var(--astl-space-2);
880
+ margin-block: 0 var(--astl-space-2);
881
+ font-size: var(--astl-size-ui);
882
+ line-height: 1.4;
883
+ }
884
+ .canvas .astl-help-top[hidden] {
885
+ display: none;
886
+ }
887
+ .astl-help-top > :is(.help-hints, .help-problem) {
888
+ align-self: stretch;
889
+ }
890
+ .help-hint-compact {
891
+ justify-content: center;
892
+ inline-size: max(var(--astl-target-min), calc(1lh + 2 * var(--astl-space-2) + 2 * var(--astl-outline-width)));
893
+ block-size: max(var(--astl-target-min), calc(1lh + 2 * var(--astl-space-2) + 2 * var(--astl-outline-width)));
894
+ padding-inline: 0;
895
+ border-radius: 50%;
896
+ font-weight: 700;
897
+ }
898
+ .astl-help-top .help-hint {
899
+ justify-content: flex-end;
900
+ text-align: end;
901
+ }
801
902
  .help-hints {
802
903
  display: grid;
803
904
  gap: var(--astl-space-2);
@@ -57,7 +57,7 @@ import { type AttemptChange, AttemptController } from '../attempt/controller.js'
57
57
  import { httpChecker } from '../checker/http.js';
58
58
  import { elementTranslator, PLAYER_NAMESPACE, playerMessages } from '../messages.js';
59
59
  import { loadElements, runtime } from '../runtime.js';
60
- import type { Help } from './help.js';
60
+ import type { Help, HintPosition } from './help.js';
61
61
  import { playerStyles } from './styles.js';
62
62
  import { refreshStyleTemplateSheet, styleTemplateSheet } from './templates.js';
63
63
  import {
@@ -158,6 +158,7 @@ export class AstlPlayer extends LitElement {
158
158
  endpoint: {},
159
159
  theme: {},
160
160
  styleTemplate: { attribute: STYLE_TEMPLATE_ATTRIBUTE, reflect: true },
161
+ hintPosition: { attribute: 'hint-position', reflect: true },
161
162
  persist: { type: Boolean },
162
163
  };
163
164
 
@@ -184,6 +185,11 @@ export class AstlPlayer extends LitElement {
184
185
  * `<astl-doc>` names none that this player knows.
185
186
  */
186
187
  declare styleTemplate: string | null;
188
+ /**
189
+ * Where a question's hint goes (`hint-position`): `row`, in its help row after its Check (the default), or `top`, a
190
+ * "?" at the top of the question with its cost beside it and the hints given under it. A change applies at once.
191
+ */
192
+ declare hintPosition: HintPosition;
187
193
  /** Keep snapshots in localStorage (per view hash) and restore them on load. */
188
194
  declare persist: boolean;
189
195
 
@@ -254,6 +260,7 @@ export class AstlPlayer extends LitElement {
254
260
  this.endpoint = null;
255
261
  this.theme = 'auto';
256
262
  this.styleTemplate = null;
263
+ this.hintPosition = 'row';
257
264
  this.persist = false;
258
265
  }
259
266
 
@@ -438,6 +445,7 @@ export class AstlPlayer extends LitElement {
438
445
 
439
446
  protected override updated(changed: PropertyValues<this>): void {
440
447
  if (changed.has('theme')) this.dataset.theme = this.#theme();
448
+ if (changed.has('hintPosition')) this.#help?.refresh();
441
449
  if (changed.has('view') && this.view !== '' && this.view !== this.#source) {
442
450
  void this.#loadView(this.view).catch(() => undefined);
443
451
  return;
@@ -1045,6 +1053,7 @@ export class AstlPlayer extends LitElement {
1045
1053
  return response === null ? null : this.#resultAnnouncement(t, response);
1046
1054
  },
1047
1055
  announce: (message) => this.#announce(message),
1056
+ hintPosition: () => (this.hintPosition === 'top' ? 'top' : 'row'),
1048
1057
  }) ?? null;
1049
1058
  const book = pages.length > 1 ? await booking : null;
1050
1059
  if (generation !== this.#generation) return;