@astl/player 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/player/help.d.ts +16 -2
- package/dist/player/help.d.ts.map +1 -1
- package/dist/player/help.js +119 -45
- package/dist/player/help.js.map +1 -1
- package/dist/player/player.d.ts +6 -0
- package/dist/player/player.d.ts.map +1 -1
- package/dist/player/player.js +58 -1
- package/dist/player/player.js.map +1 -1
- package/dist/player/styles.d.ts.map +1 -1
- package/dist/player/styles.js +22 -0
- package/dist/player/styles.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/player/help.ts +142 -55
- package/src/player/player.ts +61 -2
- package/src/player/styles.ts +22 -0
package/src/player/help.ts
CHANGED
|
@@ -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,6 +181,13 @@ 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;
|
|
170
192
|
/**
|
|
171
193
|
* The end of the check button's name ("to pytanie", "pole 2"), hidden unless `named`; a page's row says "Sprawdź tę
|
|
@@ -241,6 +263,11 @@ const PLACING = [
|
|
|
241
263
|
'margin-inline-end',
|
|
242
264
|
];
|
|
243
265
|
|
|
266
|
+
/** Whether a block floats or is placed (`PLACING`): what goes beside it then stays in its row. */
|
|
267
|
+
function placed(anchor: Element): boolean {
|
|
268
|
+
return anchor instanceof HTMLElement && PLACING.some((name) => anchor.style.getPropertyValue(name) !== '');
|
|
269
|
+
}
|
|
270
|
+
|
|
244
271
|
function place(box: HTMLElement, anchor: Element): void {
|
|
245
272
|
if (!(anchor instanceof HTMLElement)) return;
|
|
246
273
|
for (const name of PLACING) {
|
|
@@ -264,6 +291,8 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
264
291
|
|
|
265
292
|
const rows: Row[] = [];
|
|
266
293
|
const after = new Map<Element, Element>();
|
|
294
|
+
/** The last top box before a block, so the next goes after it (the fields of a line, in order). */
|
|
295
|
+
const before = new Map<Element, Element>();
|
|
267
296
|
const row = (
|
|
268
297
|
owner: Element,
|
|
269
298
|
offer: ElementHelp,
|
|
@@ -279,6 +308,8 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
279
308
|
id: owner.id,
|
|
280
309
|
offer,
|
|
281
310
|
box,
|
|
311
|
+
anchor: null,
|
|
312
|
+
top: null,
|
|
282
313
|
...extra,
|
|
283
314
|
lang: owner.closest('[lang]')?.getAttribute('lang') ?? '',
|
|
284
315
|
dir: owner.closest('[dir]')?.getAttribute('dir') ?? '',
|
|
@@ -366,6 +397,7 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
366
397
|
(after.get(anchor) ?? anchor).after(entry.box);
|
|
367
398
|
after.set(anchor, entry.box);
|
|
368
399
|
place(entry.box, anchor);
|
|
400
|
+
if (offer.hints.length > 0 && !placed(anchor)) entry.anchor = anchor;
|
|
369
401
|
}
|
|
370
402
|
host.pages.forEach((page, index) => {
|
|
371
403
|
if (page.localName !== 'astl-page') return;
|
|
@@ -479,10 +511,11 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
479
511
|
check.box.hidden = controller.readOnly;
|
|
480
512
|
if (controller.readOnly) return nothing;
|
|
481
513
|
const summary = summaryOf(check);
|
|
482
|
-
return html`<div class="help-actions">
|
|
514
|
+
return html`<div class="help-actions" part="help-actions">
|
|
483
515
|
<button
|
|
484
516
|
type="button"
|
|
485
517
|
class="button"
|
|
518
|
+
part="check-button"
|
|
486
519
|
aria-describedby=${summary === null ? nothing : check.summaryId}
|
|
487
520
|
aria-disabled=${controller.canCheck() ? nothing : 'true'}
|
|
488
521
|
@click=${() => {
|
|
@@ -525,11 +558,13 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
525
558
|
} finally {
|
|
526
559
|
entry.pending = false;
|
|
527
560
|
const active = host.root instanceof ShadowRoot ? host.root.activeElement : document.activeElement;
|
|
528
|
-
|
|
561
|
+
// The hint is in its row, or at the top of its question.
|
|
562
|
+
const holder = entry.top !== null && host.hintPosition() === 'top' ? entry.top : entry.box;
|
|
563
|
+
const focused = holder.contains(active);
|
|
529
564
|
update();
|
|
530
565
|
// The last hint takes its button away: the focus goes to that hint, not to the page.
|
|
531
566
|
if (focused && entry.used >= entry.total) {
|
|
532
|
-
|
|
567
|
+
holder.querySelector<HTMLElement>('.help-hints li:last-child')?.focus();
|
|
533
568
|
}
|
|
534
569
|
}
|
|
535
570
|
};
|
|
@@ -539,6 +574,21 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
539
574
|
return message === `error.${code}` ? t('error.internal_error') : message;
|
|
540
575
|
};
|
|
541
576
|
|
|
577
|
+
/** A question's box for its hint at its top: before its block, after the boxes of the fields before it there. */
|
|
578
|
+
const topBox = (entry: Row, anchor: Element): HTMLElement => {
|
|
579
|
+
const top = document.createElement('div');
|
|
580
|
+
top.className = 'astl-help-top';
|
|
581
|
+
top.setAttribute('part', 'help-top');
|
|
582
|
+
top.setAttribute('role', 'group');
|
|
583
|
+
top.setAttribute('aria-label', entry.label);
|
|
584
|
+
top.lang = locale;
|
|
585
|
+
const last = before.get(anchor);
|
|
586
|
+
if (last === undefined) anchor.before(top);
|
|
587
|
+
else last.after(top);
|
|
588
|
+
before.set(anchor, top);
|
|
589
|
+
return top;
|
|
590
|
+
};
|
|
591
|
+
|
|
542
592
|
const view = (entry: Row): TemplateResult | typeof nothing => {
|
|
543
593
|
const open = !controller.readOnly;
|
|
544
594
|
const next = entry.used < entry.total && help.hints && practice && open ? entry.used : null;
|
|
@@ -547,49 +597,70 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
547
597
|
const points = next === null ? undefined : entry.offer.hintPoints?.[next];
|
|
548
598
|
const costId = `astl-help-cost-${rows.indexOf(entry)}`;
|
|
549
599
|
const check = entry.check !== null && open;
|
|
600
|
+
// The hint at the top of the question: its next hint, the hints given and a failed request's problem go there.
|
|
601
|
+
const top = entry.anchor !== null && host.hintPosition() === 'top';
|
|
602
|
+
if (top && entry.top === null) entry.top = topBox(entry, entry.anchor as Element);
|
|
603
|
+
const costView =
|
|
604
|
+
cost > 0
|
|
605
|
+
? html`<span class="help-cost" part="hint-cost" id=${costId}
|
|
606
|
+
>${points === undefined ? t('help.hintCost', { percent: Math.round(cost * 100) }) : t('help.hintPoints', { points })}</span
|
|
607
|
+
>`
|
|
608
|
+
: nothing;
|
|
609
|
+
const label = next === null ? '' : t('help.hint', { n: next + 1, total: entry.total });
|
|
610
|
+
// At the top, a "?" at the end, its cost before it; in the row, the button with its cost after it.
|
|
611
|
+
const offerView =
|
|
612
|
+
next === null
|
|
613
|
+
? nothing
|
|
614
|
+
: html`<span class="help-hint" part="hint-offer"
|
|
615
|
+
>${top ? costView : nothing}<button
|
|
616
|
+
type="button"
|
|
617
|
+
class=${top ? 'button help-hint-compact' : 'button'}
|
|
618
|
+
part="hint-button"
|
|
619
|
+
title=${top ? label : nothing}
|
|
620
|
+
aria-describedby=${cost > 0 ? costId : nothing}
|
|
621
|
+
aria-disabled=${entry.pending ? 'true' : nothing}
|
|
622
|
+
@click=${() => void ask(entry)}
|
|
623
|
+
>
|
|
624
|
+
${top ? html`<span aria-hidden="true">?</span><span class="astl-vh">${label}</span>` : label}</button
|
|
625
|
+
>${top ? nothing : costView}</span
|
|
626
|
+
>`;
|
|
627
|
+
const hintsView =
|
|
628
|
+
entry.hints.length === 0
|
|
629
|
+
? nothing
|
|
630
|
+
: html`<ol class="help-hints" part="hints">
|
|
631
|
+
${entry.hints.map(
|
|
632
|
+
(hint) =>
|
|
633
|
+
html`<li tabindex="-1">
|
|
634
|
+
<span class="help-label">${t('help.hintItem', { n: hint.n })}</span>${hint.content}
|
|
635
|
+
</li>`,
|
|
636
|
+
)}
|
|
637
|
+
</ol>`;
|
|
638
|
+
const problemView =
|
|
639
|
+
entry.problem === null
|
|
640
|
+
? nothing
|
|
641
|
+
: html`<p class="help-problem" part="help-problem" role="alert">${problemText(entry.problem)}</p>`;
|
|
642
|
+
if (entry.top !== null) {
|
|
643
|
+
const shown = top && (next !== null || entry.hints.length > 0 || entry.problem !== null);
|
|
644
|
+
entry.top.hidden = !shown;
|
|
645
|
+
render(shown ? html`${offerView}${hintsView}${problemView}` : nothing, entry.top);
|
|
646
|
+
}
|
|
550
647
|
const shows =
|
|
551
|
-
next !== null ||
|
|
552
648
|
check ||
|
|
553
|
-
entry.hints.length > 0 ||
|
|
554
649
|
entry.feedback !== null ||
|
|
555
|
-
entry.explanation !== null
|
|
556
|
-
|
|
650
|
+
entry.explanation !== null ||
|
|
651
|
+
(!top && (next !== null || entry.hints.length > 0));
|
|
652
|
+
entry.box.hidden = !shows && (top || entry.problem === null);
|
|
557
653
|
if (entry.box.hidden) return nothing;
|
|
558
654
|
return html`${
|
|
559
|
-
next === null
|
|
655
|
+
!check && (top || next === null)
|
|
560
656
|
? 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
|
-
}
|
|
657
|
+
: html`<div class="help-actions" part="help-actions">
|
|
588
658
|
${
|
|
589
659
|
check
|
|
590
660
|
? html`<button
|
|
591
661
|
type="button"
|
|
592
662
|
class="button"
|
|
663
|
+
part="check-button"
|
|
593
664
|
aria-disabled=${controller.canCheck() ? nothing : 'true'}
|
|
594
665
|
@click=${() => {
|
|
595
666
|
if (controller.canCheck()) void host.check(entry.id);
|
|
@@ -604,37 +675,26 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
604
675
|
}
|
|
605
676
|
</button>`
|
|
606
677
|
: nothing
|
|
607
|
-
}
|
|
678
|
+
}${top ? nothing : offerView}
|
|
608
679
|
</div>`
|
|
609
680
|
}
|
|
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
|
-
}
|
|
681
|
+
${top ? nothing : hintsView}
|
|
622
682
|
${
|
|
623
683
|
entry.feedback === null || entry.verdict === null
|
|
624
684
|
? nothing
|
|
625
|
-
: html`<div class="help-feedback" data-verdict=${entry.verdict} ?data-own=${entry.ownVerdict}>
|
|
685
|
+
: html`<div class="help-feedback" part="help-feedback" data-verdict=${entry.verdict} ?data-own=${entry.ownVerdict}>
|
|
626
686
|
${renderVerdict(entry.verdict, t, { lang: locale, hiddenWord: entry.ownVerdict })}${entry.feedback}
|
|
627
687
|
</div>`
|
|
628
688
|
}
|
|
629
689
|
${
|
|
630
690
|
entry.explanation === null
|
|
631
691
|
? nothing
|
|
632
|
-
: html`<details class="help-why">
|
|
692
|
+
: html`<details class="help-why" part="help-why">
|
|
633
693
|
<summary>${t('help.why')}</summary>
|
|
634
694
|
${entry.explanation}
|
|
635
695
|
</details>`
|
|
636
696
|
}
|
|
637
|
-
${
|
|
697
|
+
${top ? nothing : problemView}`;
|
|
638
698
|
};
|
|
639
699
|
|
|
640
700
|
/**
|
|
@@ -745,19 +805,24 @@ export function mountHelp(host: HelpHost): Help {
|
|
|
745
805
|
return {
|
|
746
806
|
destroy() {
|
|
747
807
|
unsubscribe();
|
|
748
|
-
for (const entry of rows)
|
|
808
|
+
for (const entry of rows) {
|
|
809
|
+
entry.box.remove();
|
|
810
|
+
entry.top?.remove();
|
|
811
|
+
}
|
|
749
812
|
for (const check of checkRows) {
|
|
750
813
|
check.box.parentElement?.removeAttribute('data-shown');
|
|
751
814
|
check.box.remove();
|
|
752
815
|
}
|
|
753
816
|
},
|
|
817
|
+
refresh: update,
|
|
754
818
|
};
|
|
755
819
|
}
|
|
756
820
|
|
|
757
821
|
// 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
822
|
// floats. The feedback box has the verdict's colour and edge (solid, double, dashed, dotted), never colour alone;
|
|
759
823
|
// under a block, which says its verdict in words right above, it keeps the icon beside the text, the word hidden.
|
|
760
|
-
//
|
|
824
|
+
// "Check" comes first, then the hint: its cost stays by its button (closer than "Check" is) and wraps under it. At the
|
|
825
|
+
// top of a question the hint is a round "?" at the end of the line, its cost before it, the hints given under them.
|
|
761
826
|
const helpSheet = css`
|
|
762
827
|
.canvas .astl-help {
|
|
763
828
|
display: flex;
|
|
@@ -798,6 +863,28 @@ const helpSheet = css`
|
|
|
798
863
|
color: var(--astl-ink-muted);
|
|
799
864
|
font-size: var(--astl-size-small);
|
|
800
865
|
}
|
|
866
|
+
.canvas .astl-help-top {
|
|
867
|
+
display: flex;
|
|
868
|
+
flex-direction: column;
|
|
869
|
+
align-items: flex-end;
|
|
870
|
+
gap: var(--astl-space-2);
|
|
871
|
+
margin-block: 0 var(--astl-space-2);
|
|
872
|
+
font-size: var(--astl-size-ui);
|
|
873
|
+
line-height: 1.4;
|
|
874
|
+
}
|
|
875
|
+
.canvas .astl-help-top[hidden] {
|
|
876
|
+
display: none;
|
|
877
|
+
}
|
|
878
|
+
.astl-help-top > :is(.help-hints, .help-problem) {
|
|
879
|
+
align-self: stretch;
|
|
880
|
+
}
|
|
881
|
+
.help-hint-compact {
|
|
882
|
+
justify-content: center;
|
|
883
|
+
inline-size: var(--astl-target-min);
|
|
884
|
+
padding-inline: 0;
|
|
885
|
+
border-radius: 50%;
|
|
886
|
+
font-weight: 700;
|
|
887
|
+
}
|
|
801
888
|
.help-hints {
|
|
802
889
|
display: grid;
|
|
803
890
|
gap: var(--astl-space-2);
|
package/src/player/player.ts
CHANGED
|
@@ -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 {
|
|
@@ -130,6 +130,16 @@ function codeOf(error: unknown, fallback: string): string {
|
|
|
130
130
|
return typeof code === 'string' && code !== '' ? code : fallback;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
/**
|
|
134
|
+
* How narrow a player of `width` px is, in the root font size: 1 at 18rem and under, 2 at 12rem and under (about 200%
|
|
135
|
+
* text on a phone), else 0; null when it is not laid out (hidden), which keeps the step it had.
|
|
136
|
+
*/
|
|
137
|
+
function narrowStep(width: number): 0 | 1 | 2 | null {
|
|
138
|
+
if (width === 0) return null;
|
|
139
|
+
const rem = Number.parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
|
|
140
|
+
return width <= 12 * rem ? 2 : width <= 18 * rem ? 1 : 0;
|
|
141
|
+
}
|
|
142
|
+
|
|
133
143
|
function clock(ms: number): string {
|
|
134
144
|
const seconds = Math.ceil(ms / 1000);
|
|
135
145
|
const pad = (n: number) => String(n).padStart(2, '0');
|
|
@@ -148,6 +158,7 @@ export class AstlPlayer extends LitElement {
|
|
|
148
158
|
endpoint: {},
|
|
149
159
|
theme: {},
|
|
150
160
|
styleTemplate: { attribute: STYLE_TEMPLATE_ATTRIBUTE, reflect: true },
|
|
161
|
+
hintPosition: { attribute: 'hint-position', reflect: true },
|
|
151
162
|
persist: { type: Boolean },
|
|
152
163
|
};
|
|
153
164
|
|
|
@@ -174,6 +185,11 @@ export class AstlPlayer extends LitElement {
|
|
|
174
185
|
* `<astl-doc>` names none that this player knows.
|
|
175
186
|
*/
|
|
176
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;
|
|
177
193
|
/** Keep snapshots in localStorage (per view hash) and restore them on load. */
|
|
178
194
|
declare persist: boolean;
|
|
179
195
|
|
|
@@ -203,6 +219,13 @@ export class AstlPlayer extends LitElement {
|
|
|
203
219
|
/** Whether the book shows two pages side by side now: its module says the player is wide enough. */
|
|
204
220
|
#spread = false;
|
|
205
221
|
#resize: ResizeObserver | null = null;
|
|
222
|
+
/** How narrow the player is (`narrowStep`); its padding follows. */
|
|
223
|
+
#narrow: 0 | 1 | 2 = 0;
|
|
224
|
+
/** Watches the mark at the top of the shadow root: as wide as the player, 1rem tall, taking no room. */
|
|
225
|
+
#width: ResizeObserver | null = null;
|
|
226
|
+
/** The mark's width when last seen: 0 while hidden. */
|
|
227
|
+
#widthSeen = 0;
|
|
228
|
+
#widthFrame = 0;
|
|
206
229
|
/** Page headings, null where a page has none (a single page without a label). */
|
|
207
230
|
#headings: (HTMLElement | null)[] = [];
|
|
208
231
|
#sourceChecker: Checker | null = null;
|
|
@@ -237,6 +260,7 @@ export class AstlPlayer extends LitElement {
|
|
|
237
260
|
this.endpoint = null;
|
|
238
261
|
this.theme = 'auto';
|
|
239
262
|
this.styleTemplate = null;
|
|
263
|
+
this.hintPosition = 'row';
|
|
240
264
|
this.persist = false;
|
|
241
265
|
}
|
|
242
266
|
|
|
@@ -388,6 +412,7 @@ export class AstlPlayer extends LitElement {
|
|
|
388
412
|
super.connectedCallback();
|
|
389
413
|
this.#syncClock();
|
|
390
414
|
this.#resize?.observe(this);
|
|
415
|
+
this.#watchWidth();
|
|
391
416
|
void this.#autoload();
|
|
392
417
|
}
|
|
393
418
|
|
|
@@ -405,9 +430,12 @@ export class AstlPlayer extends LitElement {
|
|
|
405
430
|
this.#flushSnapshot();
|
|
406
431
|
this.#syncClock();
|
|
407
432
|
this.#resize?.disconnect();
|
|
433
|
+
this.#width?.disconnect();
|
|
434
|
+
cancelAnimationFrame(this.#widthFrame);
|
|
408
435
|
}
|
|
409
436
|
|
|
410
437
|
protected override firstUpdated(): void {
|
|
438
|
+
this.#watchWidth();
|
|
411
439
|
this.#query('.canvas')?.addEventListener(PlayerEvent.change, (event) => {
|
|
412
440
|
const detail = (event as CustomEvent<ChangeDetail>).detail;
|
|
413
441
|
if (detail?.answers) this.#controller?.setAnswers(detail.answers);
|
|
@@ -417,6 +445,7 @@ export class AstlPlayer extends LitElement {
|
|
|
417
445
|
|
|
418
446
|
protected override updated(changed: PropertyValues<this>): void {
|
|
419
447
|
if (changed.has('theme')) this.dataset.theme = this.#theme();
|
|
448
|
+
if (changed.has('hintPosition')) this.#help?.refresh();
|
|
420
449
|
if (changed.has('view') && this.view !== '' && this.view !== this.#source) {
|
|
421
450
|
void this.#loadView(this.view).catch(() => undefined);
|
|
422
451
|
return;
|
|
@@ -438,13 +467,14 @@ export class AstlPlayer extends LitElement {
|
|
|
438
467
|
const locale = this.#uiLocale();
|
|
439
468
|
const t = this.#i18n().translator(locale, PLAYER_NAMESPACE);
|
|
440
469
|
const controller = this.#controller;
|
|
441
|
-
return html`<div
|
|
470
|
+
return html`<div class="mark"></div><div
|
|
442
471
|
class="root"
|
|
443
472
|
part="player"
|
|
444
473
|
lang=${locale}
|
|
445
474
|
dir=${directionOf(locale)}
|
|
446
475
|
data-style-template=${this.#documentTemplate ?? nothing}
|
|
447
476
|
?data-spread=${this.#spread}
|
|
477
|
+
data-narrow=${this.#narrow === 0 ? nothing : this.#narrow}
|
|
448
478
|
aria-busy=${this.#state === 'loading' ? 'true' : 'false'}
|
|
449
479
|
>
|
|
450
480
|
${this.#state === 'loading' ? html`<p class="status">${t('loading')}</p>` : nothing}
|
|
@@ -1023,6 +1053,7 @@ export class AstlPlayer extends LitElement {
|
|
|
1023
1053
|
return response === null ? null : this.#resultAnnouncement(t, response);
|
|
1024
1054
|
},
|
|
1025
1055
|
announce: (message) => this.#announce(message),
|
|
1056
|
+
hintPosition: () => (this.hintPosition === 'top' ? 'top' : 'row'),
|
|
1026
1057
|
}) ?? null;
|
|
1027
1058
|
const book = pages.length > 1 ? await booking : null;
|
|
1028
1059
|
if (generation !== this.#generation) return;
|
|
@@ -1203,6 +1234,34 @@ export class AstlPlayer extends LitElement {
|
|
|
1203
1234
|
return this.#spread ? this.#shown(page)[0] + 2 * direction : page + direction;
|
|
1204
1235
|
}
|
|
1205
1236
|
|
|
1237
|
+
/**
|
|
1238
|
+
* Follows the player's width (the mark's) without size containment, so in any layout the player takes the width it
|
|
1239
|
+
* had: measured at once when rendered or connected, before it is painted, then on every resize. A resize applies a
|
|
1240
|
+
* frame later, outside the observer's delivery, which a change of size in a box sized by its content would loop;
|
|
1241
|
+
* only a player just shown (from hidden) applies at once, as the mark does not follow the padding.
|
|
1242
|
+
*/
|
|
1243
|
+
#watchWidth(): void {
|
|
1244
|
+
const mark = this.#query('.mark');
|
|
1245
|
+
if (mark === null || typeof ResizeObserver === 'undefined') return;
|
|
1246
|
+
this.#widthSeen = mark.clientWidth;
|
|
1247
|
+
this.#setNarrow(narrowStep(this.#widthSeen));
|
|
1248
|
+
this.#width ??= new ResizeObserver(([entry]) => {
|
|
1249
|
+
const width = entry?.contentRect.width ?? 0;
|
|
1250
|
+
const shown = this.#widthSeen === 0;
|
|
1251
|
+
this.#widthSeen = width;
|
|
1252
|
+
cancelAnimationFrame(this.#widthFrame);
|
|
1253
|
+
if (shown) this.#setNarrow(narrowStep(width));
|
|
1254
|
+
else this.#widthFrame = requestAnimationFrame(() => this.#setNarrow(narrowStep(width)));
|
|
1255
|
+
});
|
|
1256
|
+
this.#width.observe(mark);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
#setNarrow(step: 0 | 1 | 2 | null): void {
|
|
1260
|
+
if (step === null || step === this.#narrow) return;
|
|
1261
|
+
this.#narrow = step;
|
|
1262
|
+
this.requestUpdate();
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1206
1265
|
/** The book's module says whether the player is wide enough for a spread: switching keeps the page, without focus. */
|
|
1207
1266
|
#setSpread(spread: boolean): void {
|
|
1208
1267
|
if (spread === this.#spread) return;
|
package/src/player/styles.ts
CHANGED
|
@@ -154,6 +154,28 @@ export const playerStyles: CSSResultGroup = [
|
|
|
154
154
|
line-height: 1.25;
|
|
155
155
|
}
|
|
156
156
|
`,
|
|
157
|
+
// A narrow player (a small phone, larger text) keeps more of its width for the exercise: less space around the card
|
|
158
|
+
// and inside its sides, in two steps (`data-narrow`, from the player's own width; the second for about 200% text on
|
|
159
|
+
// a phone). A phone of 360 px or more at 100% text keeps the full padding. The mark is what the player measures: as
|
|
160
|
+
// wide as the player and 1rem tall, so a new root font size is a resize too, and it takes no room.
|
|
161
|
+
css`
|
|
162
|
+
.mark {
|
|
163
|
+
block-size: 1rem;
|
|
164
|
+
margin-block-end: -1rem;
|
|
165
|
+
}
|
|
166
|
+
.root[data-narrow] {
|
|
167
|
+
padding: var(--astl-space-2);
|
|
168
|
+
}
|
|
169
|
+
.root[data-narrow] > .canvas {
|
|
170
|
+
padding-inline: var(--astl-space-3);
|
|
171
|
+
}
|
|
172
|
+
.root[data-narrow='2'] {
|
|
173
|
+
padding-inline: 0;
|
|
174
|
+
}
|
|
175
|
+
.root[data-narrow='2'] > .canvas {
|
|
176
|
+
padding-inline: var(--astl-space-1);
|
|
177
|
+
}
|
|
178
|
+
`,
|
|
157
179
|
// A Check element shows only where the help module draws it (practice that checks one question at a time).
|
|
158
180
|
css`
|
|
159
181
|
.canvas astl-check:not([data-shown]) {
|