@qti-components/test 1.7.0 → 1.8.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.
- package/dist/components/qti-assessment-item-ref/qti-assessment-item-ref.d.ts +8 -0
- package/dist/components/qti-assessment-item-ref/qti-assessment-item-ref.js +45 -5
- package/dist/components/qti-assessment-item-ref/qti-assessment-item-ref.js.map +1 -1
- package/dist/components/styles.d.ts +3 -0
- package/dist/components/styles.js +29 -4
- package/dist/components/styles.js.map +1 -1
- package/dist/components/test-check-item/test-check-item.js +6 -0
- package/dist/components/test-check-item/test-check-item.js.map +1 -1
- package/dist/components/test-end-attempt/test-end-attempt.js +6 -0
- package/dist/components/test-end-attempt/test-end-attempt.js.map +1 -1
- package/dist/components/test-item-link/test-item-link.js +6 -0
- package/dist/components/test-item-link/test-item-link.js.map +1 -1
- package/dist/components/test-item-to-speech/test-item-to-speech.d.ts +82 -11
- package/dist/components/test-item-to-speech/test-item-to-speech.js +429 -62
- package/dist/components/test-item-to-speech/test-item-to-speech.js.map +1 -1
- package/dist/components/test-next/test-next.js +6 -0
- package/dist/components/test-next/test-next.js.map +1 -1
- package/dist/components/test-prev/test-prev.js +6 -0
- package/dist/components/test-prev/test-prev.js.map +1 -1
- package/dist/components/test-section-link/test-section-link.js +6 -0
- package/dist/components/test-section-link/test-section-link.js.map +1 -1
- package/dist/elements.d.ts +5 -2
- package/dist/elements.js +3 -2
- package/dist/elements.js.map +1 -1
- package/package.json +1 -1
|
@@ -7,7 +7,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
import { html, LitElement, css } from 'lit';
|
|
8
8
|
import { consume, provide, createContext } from '@lit/context';
|
|
9
9
|
import { property, state } from 'lit/decorators.js';
|
|
10
|
+
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
10
11
|
import { sessionContext } from '@qti-components/base';
|
|
12
|
+
import * as styles from '../styles';
|
|
11
13
|
// ─── CSS Custom Highlight ─────────────────────────────────────────────────────
|
|
12
14
|
const HIGHLIGHT_ELEMENT = 'tts-element'; // navigation cursor — whole element
|
|
13
15
|
const HIGHLIGHT_WORD = 'tts-word'; // word boundary during speech
|
|
@@ -24,6 +26,21 @@ function getHighlightSheet() {
|
|
|
24
26
|
return highlightSheet;
|
|
25
27
|
}
|
|
26
28
|
export const ttsContext = createContext(Symbol('tts-context'));
|
|
29
|
+
// ─── Icons ────────────────────────────────────────────────────────────────────
|
|
30
|
+
// Plain geometric glyphs on a 24-unit grid, painted in currentColor. SVG rather than text so
|
|
31
|
+
// every button gets the same box: text glyphs (◀◀ ▶ ⏸ ■ ☞) differ in width, and some fall back
|
|
32
|
+
// to an emoji font with different line metrics.
|
|
33
|
+
const ICONS = {
|
|
34
|
+
play: 'M8 5v14l11-7z',
|
|
35
|
+
pause: 'M6 5h4v14H6zM14 5h4v14h-4z',
|
|
36
|
+
stop: 'M6 6h12v12H6z',
|
|
37
|
+
prev: 'M6 5h2v14H6zM20 5v14L9 12z',
|
|
38
|
+
next: 'M16 5h2v14h-2zM4 5v14l11-7z',
|
|
39
|
+
pick: 'M5 3l13 7-5.5 1.6 3.4 6.6-2.4 1.3-3.4-6.7L6 17z'
|
|
40
|
+
};
|
|
41
|
+
/** Default button content: the icon, plus a visually hidden label as the accessible name. */
|
|
42
|
+
const iconContent = (icon, label) => html `<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d=${ICONS[icon]}></path></svg
|
|
43
|
+
><span class="label">${label}</span>`;
|
|
27
44
|
// ─── Shared base for child button elements ────────────────────────────────────
|
|
28
45
|
/**
|
|
29
46
|
* Base class shared by all TTS control elements. Consumes `ttsContext` and
|
|
@@ -33,25 +50,68 @@ export const ttsContext = createContext(Symbol('tts-context'));
|
|
|
33
50
|
class TtsButtonBase extends LitElement {
|
|
34
51
|
static { this.styles = css `
|
|
35
52
|
:host {
|
|
36
|
-
display: inline-
|
|
53
|
+
display: inline-flex;
|
|
37
54
|
}
|
|
38
55
|
button {
|
|
39
|
-
|
|
56
|
+
${styles.btn};
|
|
57
|
+
min-inline-size: var(--test-button-size, 2.25rem);
|
|
58
|
+
padding-inline: 0.5rem;
|
|
59
|
+
}
|
|
60
|
+
button:hover:not(:disabled) {
|
|
61
|
+
${styles.btnInteractive};
|
|
62
|
+
}
|
|
63
|
+
button:focus-visible {
|
|
64
|
+
${styles.focusRing};
|
|
40
65
|
}
|
|
41
66
|
button:disabled {
|
|
42
|
-
|
|
43
|
-
|
|
67
|
+
${styles.dis};
|
|
68
|
+
}
|
|
69
|
+
button[aria-pressed='true'],
|
|
70
|
+
button[aria-pressed='true']:hover {
|
|
71
|
+
background-color: var(--test-button-selected-background-color, var(--qti-selected-bg, #0175aa));
|
|
72
|
+
color: var(--test-button-selected-color, var(--qti-selected-color, #fff));
|
|
73
|
+
}
|
|
74
|
+
svg,
|
|
75
|
+
::slotted(svg) {
|
|
76
|
+
flex: none;
|
|
77
|
+
inline-size: var(--test-tts-icon-size, 1.125rem);
|
|
78
|
+
block-size: var(--test-tts-icon-size, 1.125rem);
|
|
79
|
+
}
|
|
80
|
+
svg {
|
|
81
|
+
fill: currentColor;
|
|
82
|
+
}
|
|
83
|
+
/* The default content is icon-only; this keeps the button's accessible name. */
|
|
84
|
+
.label {
|
|
85
|
+
position: absolute;
|
|
86
|
+
inline-size: 1px;
|
|
87
|
+
block-size: 1px;
|
|
88
|
+
overflow: hidden;
|
|
89
|
+
clip-path: inset(50%);
|
|
90
|
+
white-space: nowrap;
|
|
44
91
|
}
|
|
45
92
|
`; }
|
|
46
93
|
#internals = this.attachInternals();
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
94
|
+
#mirroredState;
|
|
95
|
+
#mirroredPicking = false;
|
|
96
|
+
updated(_changed) {
|
|
97
|
+
// `_tts` is a context field, not a reactive property, so it never shows up in `changed`:
|
|
98
|
+
// compare against what was last mirrored instead.
|
|
99
|
+
const state = this._tts?.state;
|
|
100
|
+
const picking = this._tts?.picking ?? false;
|
|
101
|
+
if (state === this.#mirroredState && picking === this.#mirroredPicking)
|
|
102
|
+
return;
|
|
103
|
+
this.#mirroredState = state;
|
|
104
|
+
this.#mirroredPicking = picking;
|
|
105
|
+
this.#internals.states.clear();
|
|
106
|
+
if (state)
|
|
107
|
+
this.#internals.states.add(state);
|
|
108
|
+
if (picking)
|
|
109
|
+
this.#internals.states.add('picking');
|
|
53
110
|
}
|
|
54
111
|
}
|
|
112
|
+
__decorate([
|
|
113
|
+
property({ type: String })
|
|
114
|
+
], TtsButtonBase.prototype, "label", void 0);
|
|
55
115
|
__decorate([
|
|
56
116
|
consume({ context: ttsContext, subscribe: true })
|
|
57
117
|
], TtsButtonBase.prototype, "_tts", void 0);
|
|
@@ -65,17 +125,62 @@ __decorate([
|
|
|
65
125
|
* ```html
|
|
66
126
|
* <test-item-to-speech language="nl-NL">
|
|
67
127
|
* <test-tts-play></test-tts-play>
|
|
68
|
-
* <test-tts-prev
|
|
69
|
-
* <test-tts-next
|
|
70
|
-
* <test-tts-stop
|
|
128
|
+
* <test-tts-prev></test-tts-prev>
|
|
129
|
+
* <test-tts-next></test-tts-next>
|
|
130
|
+
* <test-tts-stop></test-tts-stop>
|
|
71
131
|
* </test-item-to-speech>
|
|
72
132
|
* ```
|
|
73
133
|
*
|
|
134
|
+
* The controller lays its controls out as a wrapping row; each control draws an icon button,
|
|
135
|
+
* and slotted content replaces the icon — a slotted `<svg>` is sized like the default one. Give a
|
|
136
|
+
* control with a slotted icon a `label`, since an icon has no text to name the button by. Buttons read the qti-theme tokens (with the theme's
|
|
137
|
+
* defaults as fallbacks) and can be sized with `--test-button-size` and `--test-tts-icon-size`.
|
|
138
|
+
*
|
|
139
|
+
* ### Language resolution
|
|
140
|
+
*
|
|
141
|
+
* Each reading element is spoken in the language of the nearest `lang` attribute:
|
|
142
|
+
*
|
|
143
|
+
* 1. `lang` (or `xml:lang`) on the element itself
|
|
144
|
+
* 2. `lang` on the closest ancestor — including `<qti-assessment-item xml:lang>` and, across
|
|
145
|
+
* shadow roots, anything wrapping the test
|
|
146
|
+
* 3. `lang` on the document's `<html>` element
|
|
147
|
+
* 4. the `language` attribute on `<test-item-to-speech>`
|
|
148
|
+
*
|
|
149
|
+
* The transformer copies `xml:lang` over as a plain `lang` attribute, so QTI authored with
|
|
150
|
+
* `xml:lang="en-GB"` on a `<p>` or on the item root is honoured without further work.
|
|
151
|
+
*
|
|
152
|
+
* ### Which item is read
|
|
153
|
+
*
|
|
154
|
+
* By default the player reads the item the test is navigated to (`navItemRefId` from the
|
|
155
|
+
* session context). When several items share one page — a keep-together section or a
|
|
156
|
+
* vertically scrolling booklet — give each player its own `item-ref-id` so it reads that
|
|
157
|
+
* item regardless of the navigation cursor. The item is looked up by its
|
|
158
|
+
* `qti-assessment-item-ref` identifier, in the player's own tree first and otherwise in
|
|
159
|
+
* any `test-container` shadow root reachable from it, so the player can live inside the
|
|
160
|
+
* item card, next to it, or in the page chrome.
|
|
161
|
+
*
|
|
162
|
+
* Only one item can be spoken at a time; starting one player stops any other.
|
|
163
|
+
*
|
|
164
|
+
* ### Starting somewhere in the middle
|
|
165
|
+
*
|
|
166
|
+
* `<test-tts-pick>` switches the player into pick mode: every reading element gets the blue
|
|
167
|
+
* cursor highlight, and the first click on one of them leaves pick mode and starts reading
|
|
168
|
+
* from that element. Pressing the button again leaves pick mode without reading.
|
|
169
|
+
*
|
|
74
170
|
* @cssstate idle - No speech active
|
|
171
|
+
* @cssstate picking - Pick mode: all reading elements highlighted, waiting for a click
|
|
75
172
|
* @cssstate playing - Speech is playing
|
|
76
173
|
* @cssstate paused - Speech is paused
|
|
77
174
|
*/
|
|
78
175
|
export class TestItemToSpeech extends LitElement {
|
|
176
|
+
static { this.styles = css `
|
|
177
|
+
:host {
|
|
178
|
+
display: inline-flex;
|
|
179
|
+
flex-wrap: wrap;
|
|
180
|
+
align-items: center;
|
|
181
|
+
gap: var(--test-tts-gap, 0.25rem);
|
|
182
|
+
}
|
|
183
|
+
`; }
|
|
79
184
|
#internals;
|
|
80
185
|
#itemElements;
|
|
81
186
|
#boundHandleItemConnected;
|
|
@@ -85,14 +190,26 @@ export class TestItemToSpeech extends LitElement {
|
|
|
85
190
|
#currentElementIndex;
|
|
86
191
|
// Text nodes of the element currently being spoken (for word-boundary highlight)
|
|
87
192
|
#textNodes;
|
|
193
|
+
// The utterance this player is speaking; lets the error handler tell "our speech was
|
|
194
|
+
// cancelled by another player" apart from a cancel we issued ourselves.
|
|
195
|
+
#currentUtterance;
|
|
196
|
+
// Pick mode: all elements highlighted, a click on one starts reading from there.
|
|
197
|
+
#picking;
|
|
198
|
+
#pickRoot;
|
|
199
|
+
#boundHandlePickClick;
|
|
200
|
+
// Set when the last element has been read: the cursor stays on it (so prev still works),
|
|
201
|
+
// but the next play starts the item over instead of repeating that last element.
|
|
202
|
+
#finished;
|
|
88
203
|
// Two separate CSS Highlight objects
|
|
89
204
|
#elementHighlight; // whole-element cursor
|
|
90
205
|
#wordHighlight; // current word during speech
|
|
91
206
|
// Tracks previous navItemRefId to detect item switches
|
|
92
207
|
#prevNavItemRefId;
|
|
208
|
+
#prevNavSectionId;
|
|
93
209
|
#boundHandleNavigation;
|
|
94
210
|
constructor() {
|
|
95
211
|
super();
|
|
212
|
+
/** Fallback language when neither the item content nor the document declares one. */
|
|
96
213
|
this.language = 'nl-NL';
|
|
97
214
|
this.#internals = this.attachInternals();
|
|
98
215
|
this.#itemElements = new Map();
|
|
@@ -103,11 +220,22 @@ export class TestItemToSpeech extends LitElement {
|
|
|
103
220
|
this.#currentElementIndex = 0;
|
|
104
221
|
// Text nodes of the element currently being spoken (for word-boundary highlight)
|
|
105
222
|
this.#textNodes = [];
|
|
223
|
+
// The utterance this player is speaking; lets the error handler tell "our speech was
|
|
224
|
+
// cancelled by another player" apart from a cancel we issued ourselves.
|
|
225
|
+
this.#currentUtterance = null;
|
|
226
|
+
// Pick mode: all elements highlighted, a click on one starts reading from there.
|
|
227
|
+
this.#picking = false;
|
|
228
|
+
this.#pickRoot = null;
|
|
229
|
+
this.#boundHandlePickClick = this.#handlePickClick.bind(this);
|
|
230
|
+
// Set when the last element has been read: the cursor stays on it (so prev still works),
|
|
231
|
+
// but the next play starts the item over instead of repeating that last element.
|
|
232
|
+
this.#finished = false;
|
|
106
233
|
// Two separate CSS Highlight objects
|
|
107
234
|
this.#elementHighlight = new Highlight(); // whole-element cursor
|
|
108
235
|
this.#wordHighlight = new Highlight(); // current word during speech
|
|
109
236
|
// Tracks previous navItemRefId to detect item switches
|
|
110
237
|
this.#prevNavItemRefId = undefined;
|
|
238
|
+
this.#prevNavSectionId = undefined;
|
|
111
239
|
this.#boundHandleNavigation = () => this.#stop();
|
|
112
240
|
this._ttsContext = {
|
|
113
241
|
state: 'idle',
|
|
@@ -116,26 +244,47 @@ export class TestItemToSpeech extends LitElement {
|
|
|
116
244
|
play: () => this.#playSpeech(),
|
|
117
245
|
pause: () => this.#pause(),
|
|
118
246
|
resume: () => this.#resume(),
|
|
119
|
-
stop: () =>
|
|
247
|
+
stop: () => {
|
|
248
|
+
this.#stopPicking();
|
|
249
|
+
this.#stop();
|
|
250
|
+
},
|
|
120
251
|
prevElement: () => this.#prevElement(),
|
|
121
|
-
nextElement: () => this.#nextElement()
|
|
252
|
+
nextElement: () => this.#nextElement(),
|
|
253
|
+
togglePick: () => this.#togglePick(),
|
|
254
|
+
picking: false
|
|
122
255
|
};
|
|
123
256
|
}
|
|
124
257
|
willUpdate(changed) {
|
|
125
258
|
// no property-driven context updates needed currently
|
|
126
259
|
void changed;
|
|
127
260
|
}
|
|
128
|
-
updated(
|
|
261
|
+
updated(changed) {
|
|
262
|
+
if (changed.has('itemRefId') && changed.get('itemRefId') !== undefined) {
|
|
263
|
+
this.#resetReadingPosition();
|
|
264
|
+
}
|
|
129
265
|
const current = this._sessionContext?.navItemRefId ?? null;
|
|
130
|
-
// Stop speech when navItemRefId changes (ignore the initial undefined → value transition)
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
266
|
+
// Stop speech when navItemRefId changes (ignore the initial undefined → value transition).
|
|
267
|
+
// A player pinned to an item keeps its reading position: the cursor moving elsewhere on a
|
|
268
|
+
// multi-item page does not change what this player reads.
|
|
269
|
+
// Moving to another section does take a pinned player's item off the page, so that
|
|
270
|
+
// stops every player; section navigation leaves navItemRefId null, so it needs its own check.
|
|
271
|
+
const section = this._sessionContext?.navSectionId ?? null;
|
|
272
|
+
const itemChanged = this.#prevNavItemRefId !== undefined && this.#prevNavItemRefId !== current;
|
|
273
|
+
const sectionChanged = this.#prevNavSectionId !== undefined && this.#prevNavSectionId !== section;
|
|
274
|
+
if (sectionChanged || (itemChanged && !this.itemRefId)) {
|
|
275
|
+
this.#resetReadingPosition();
|
|
137
276
|
}
|
|
138
277
|
this.#prevNavItemRefId = current;
|
|
278
|
+
this.#prevNavSectionId = section;
|
|
279
|
+
}
|
|
280
|
+
/** Stop speech and forget the collected reading elements, so the next play starts afresh. */
|
|
281
|
+
#resetReadingPosition() {
|
|
282
|
+
this.#stopPicking();
|
|
283
|
+
this.#stop();
|
|
284
|
+
this.#readingElements = null;
|
|
285
|
+
this.#currentElementIndex = 0;
|
|
286
|
+
this.#finished = false;
|
|
287
|
+
this.#updateContext();
|
|
139
288
|
}
|
|
140
289
|
connectedCallback() {
|
|
141
290
|
super.connectedCallback();
|
|
@@ -151,7 +300,12 @@ export class TestItemToSpeech extends LitElement {
|
|
|
151
300
|
}
|
|
152
301
|
disconnectedCallback() {
|
|
153
302
|
super.disconnectedCallback();
|
|
154
|
-
|
|
303
|
+
// Only silence the synthesizer if it is speaking for this player.
|
|
304
|
+
if (this.#currentUtterance) {
|
|
305
|
+
this.#currentUtterance = null;
|
|
306
|
+
speechSynthesis.cancel();
|
|
307
|
+
}
|
|
308
|
+
this.#stopPicking();
|
|
155
309
|
this.#clearAllHighlights();
|
|
156
310
|
this.#eventHost?.removeEventListener('qti-assessment-item-connected', this.#boundHandleItemConnected);
|
|
157
311
|
this.#eventHost?.removeEventListener('qti-request-navigation', this.#boundHandleNavigation);
|
|
@@ -165,7 +319,46 @@ export class TestItemToSpeech extends LitElement {
|
|
|
165
319
|
const item = event.detail;
|
|
166
320
|
if (item?.identifier) {
|
|
167
321
|
this.#itemElements.set(item.identifier, item);
|
|
322
|
+
// Our item was (re)rendered: the elements we collected earlier are detached now.
|
|
323
|
+
if (this.itemRefId && item.identifier === this.itemRefId && this.#readingElements !== null) {
|
|
324
|
+
this.#resetReadingPosition();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Find the rendered `qti-assessment-item` for an item-ref identifier without relying on the
|
|
330
|
+
* connected-event cache — the player may be added after the item rendered (a header built
|
|
331
|
+
* around an existing item) or sit in a shadow root the event never bubbled through.
|
|
332
|
+
*/
|
|
333
|
+
#findItemInDom(identifier) {
|
|
334
|
+
const esc = (v) => (typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(v) : v.replace(/"/g, '\\"'));
|
|
335
|
+
const selector = `qti-assessment-item-ref[identifier="${esc(identifier)}"] qti-assessment-item, ` +
|
|
336
|
+
`qti-assessment-item[identifier="${esc(identifier)}"]`;
|
|
337
|
+
// Nearest first: a player placed inside the item's own card.
|
|
338
|
+
const ownRef = this.closest('qti-assessment-item-ref');
|
|
339
|
+
if (ownRef?.getAttribute('identifier') === identifier) {
|
|
340
|
+
const own = ownRef.querySelector('qti-assessment-item');
|
|
341
|
+
if (own)
|
|
342
|
+
return own;
|
|
168
343
|
}
|
|
344
|
+
// Then every root from here up to the document, and the test-container shadow roots in them.
|
|
345
|
+
let root = this.getRootNode();
|
|
346
|
+
while (root) {
|
|
347
|
+
const scope = root;
|
|
348
|
+
const direct = scope.querySelector?.(selector);
|
|
349
|
+
if (direct)
|
|
350
|
+
return direct;
|
|
351
|
+
for (const container of Array.from(scope.querySelectorAll?.('test-container') ?? [])) {
|
|
352
|
+
const inShadow = container.shadowRoot?.querySelector(selector);
|
|
353
|
+
if (inShadow)
|
|
354
|
+
return inShadow;
|
|
355
|
+
}
|
|
356
|
+
const host = root.host;
|
|
357
|
+
if (!host)
|
|
358
|
+
break;
|
|
359
|
+
root = host.getRootNode();
|
|
360
|
+
}
|
|
361
|
+
return null;
|
|
169
362
|
}
|
|
170
363
|
/** Update speech state on both the context (notifies children) and the host's CSS custom states. */
|
|
171
364
|
#setSpeechState(state) {
|
|
@@ -187,26 +380,54 @@ export class TestItemToSpeech extends LitElement {
|
|
|
187
380
|
#getReadingElements() {
|
|
188
381
|
if (this.#readingElements !== null)
|
|
189
382
|
return this.#readingElements;
|
|
190
|
-
const identifier = this._sessionContext?.navItemRefId;
|
|
383
|
+
const identifier = this.itemRefId || this._sessionContext?.navItemRefId;
|
|
191
384
|
if (!identifier)
|
|
192
385
|
return [];
|
|
193
|
-
const
|
|
386
|
+
const cached = this.#itemElements.get(identifier);
|
|
387
|
+
const assessmentItem = cached?.isConnected ? cached : this.#findItemInDom(identifier);
|
|
194
388
|
if (!assessmentItem)
|
|
195
389
|
return [];
|
|
196
390
|
const itemBody = assessmentItem.querySelector('qti-item-body');
|
|
197
391
|
if (!itemBody)
|
|
198
392
|
return [];
|
|
199
|
-
// Collect block-level elements that contain readable text
|
|
200
|
-
|
|
201
|
-
|
|
393
|
+
// Collect block-level elements (and QTI prompt / choice elements) that contain readable text.
|
|
394
|
+
// Keep only the innermost matches so `<li><p>…</p></li>` is not read twice.
|
|
395
|
+
const selector = 'p, h1, h2, h3, h4, h5, h6, li, blockquote, figcaption, qti-prompt, qti-simple-choice';
|
|
396
|
+
this.#readingElements = Array.from(itemBody.querySelectorAll(selector)).filter(el => (el.textContent ?? '').trim().length > 0 && !el.querySelector(selector));
|
|
202
397
|
return this.#readingElements;
|
|
203
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* Resolve the speech language for a reading element.
|
|
401
|
+
* Element `lang` → closest ancestor `lang` (crossing shadow hosts) → `<html lang>` → `language` attribute.
|
|
402
|
+
* Empty `lang=""` is treated as absent.
|
|
403
|
+
*/
|
|
404
|
+
#resolveLang(element) {
|
|
405
|
+
let node = element;
|
|
406
|
+
while (node) {
|
|
407
|
+
const lang = node.getAttribute('lang') || node.getAttribute('xml:lang');
|
|
408
|
+
if (lang?.trim())
|
|
409
|
+
return lang.trim();
|
|
410
|
+
node = node.parentElement ?? node.getRootNode().host ?? null;
|
|
411
|
+
}
|
|
412
|
+
const documentLang = document.documentElement.getAttribute('lang');
|
|
413
|
+
if (documentLang?.trim())
|
|
414
|
+
return documentLang.trim();
|
|
415
|
+
return this.language;
|
|
416
|
+
}
|
|
204
417
|
#playSpeech() {
|
|
205
418
|
const elements = this.#getReadingElements();
|
|
206
419
|
if (!elements.length) {
|
|
207
420
|
console.warn('test-item-to-speech: no readable elements found in qti-item-body');
|
|
208
421
|
return;
|
|
209
422
|
}
|
|
423
|
+
this.#stopPicking();
|
|
424
|
+
// speechSynthesis is one global queue: another player mid-sentence would otherwise
|
|
425
|
+
// finish first and this one would silently wait its turn.
|
|
426
|
+
speechSynthesis.cancel();
|
|
427
|
+
if (this.#finished) {
|
|
428
|
+
this.#finished = false;
|
|
429
|
+
this.#currentElementIndex = 0;
|
|
430
|
+
}
|
|
210
431
|
this.#speakElement(this.#currentElementIndex);
|
|
211
432
|
}
|
|
212
433
|
/** Speak the element at the given index, then auto-advance when it ends. */
|
|
@@ -218,11 +439,13 @@ export class TestItemToSpeech extends LitElement {
|
|
|
218
439
|
this.#currentElementIndex = elements.length - 1;
|
|
219
440
|
if (elements.length > 0)
|
|
220
441
|
this.#highlightElement(elements[this.#currentElementIndex]);
|
|
442
|
+
this.#finished = true;
|
|
221
443
|
this.#setSpeechState('idle');
|
|
222
444
|
this.#updateContext();
|
|
223
445
|
return;
|
|
224
446
|
}
|
|
225
447
|
this.#currentElementIndex = index;
|
|
448
|
+
this.#finished = false;
|
|
226
449
|
this.#updateContext();
|
|
227
450
|
const element = elements[index];
|
|
228
451
|
element.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
@@ -236,7 +459,7 @@ export class TestItemToSpeech extends LitElement {
|
|
|
236
459
|
return;
|
|
237
460
|
}
|
|
238
461
|
const utterance = new SpeechSynthesisUtterance(text);
|
|
239
|
-
utterance.lang = this
|
|
462
|
+
utterance.lang = this.#resolveLang(element);
|
|
240
463
|
utterance.rate = 1;
|
|
241
464
|
utterance.onboundary = (event) => {
|
|
242
465
|
if (event.name !== 'word')
|
|
@@ -244,9 +467,22 @@ export class TestItemToSpeech extends LitElement {
|
|
|
244
467
|
this.#updateWordHighlight(event.charIndex, event.charLength ?? 0);
|
|
245
468
|
};
|
|
246
469
|
utterance.onend = () => {
|
|
470
|
+
if (this.#currentUtterance !== utterance)
|
|
471
|
+
return;
|
|
472
|
+
this.#currentUtterance = null;
|
|
247
473
|
this.#clearWordHighlight();
|
|
248
474
|
this.#speakElement(this.#currentElementIndex + 1);
|
|
249
475
|
};
|
|
476
|
+
// Fires when speech is cancelled from outside this player (another player starting,
|
|
477
|
+
// or the browser); a cancel we issued ourselves has already cleared #currentUtterance.
|
|
478
|
+
utterance.onerror = () => {
|
|
479
|
+
if (this.#currentUtterance !== utterance)
|
|
480
|
+
return;
|
|
481
|
+
this.#currentUtterance = null;
|
|
482
|
+
this.#clearAllHighlights();
|
|
483
|
+
this.#setSpeechState('idle');
|
|
484
|
+
};
|
|
485
|
+
this.#currentUtterance = utterance;
|
|
250
486
|
speechSynthesis.speak(utterance);
|
|
251
487
|
this.#setSpeechState('playing');
|
|
252
488
|
}
|
|
@@ -259,17 +495,73 @@ export class TestItemToSpeech extends LitElement {
|
|
|
259
495
|
this.#setSpeechState('playing');
|
|
260
496
|
}
|
|
261
497
|
#stop() {
|
|
498
|
+
this.#currentUtterance = null;
|
|
262
499
|
speechSynthesis.cancel();
|
|
263
500
|
this.#clearAllHighlights();
|
|
264
501
|
this.#setSpeechState('idle');
|
|
265
502
|
}
|
|
503
|
+
// ─── Pick mode ────────────────────────────────────────────────────────────
|
|
504
|
+
#togglePick() {
|
|
505
|
+
if (this.#picking)
|
|
506
|
+
this.#stopPicking();
|
|
507
|
+
else
|
|
508
|
+
this.#startPicking();
|
|
509
|
+
}
|
|
510
|
+
/** Highlight every reading element and wait for a click on one of them. */
|
|
511
|
+
#startPicking() {
|
|
512
|
+
const elements = this.#getReadingElements();
|
|
513
|
+
if (!elements.length) {
|
|
514
|
+
console.warn('test-item-to-speech: no readable elements found in qti-item-body');
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
this.#stop();
|
|
518
|
+
this.#picking = true;
|
|
519
|
+
this.#internals.states.add('picking');
|
|
520
|
+
this.#highlightElements(elements);
|
|
521
|
+
// Listen on the item's root so the click is seen before any interaction handles it — in
|
|
522
|
+
// pick mode a click on a choice picks the sentence, it does not answer the question.
|
|
523
|
+
this.#pickRoot = elements[0].getRootNode();
|
|
524
|
+
this.#pickRoot.addEventListener('click', this.#boundHandlePickClick, true);
|
|
525
|
+
this._ttsContext = { ...this._ttsContext, picking: true };
|
|
526
|
+
}
|
|
527
|
+
#stopPicking() {
|
|
528
|
+
if (!this.#picking)
|
|
529
|
+
return;
|
|
530
|
+
this.#picking = false;
|
|
531
|
+
this.#internals.states.delete('picking');
|
|
532
|
+
this.#pickRoot?.removeEventListener('click', this.#boundHandlePickClick, true);
|
|
533
|
+
this.#pickRoot = null;
|
|
534
|
+
this.#clearAllHighlights();
|
|
535
|
+
this._ttsContext = { ...this._ttsContext, picking: false };
|
|
536
|
+
}
|
|
537
|
+
#handlePickClick(event) {
|
|
538
|
+
const elements = this.#readingElements ?? [];
|
|
539
|
+
const path = event.composedPath();
|
|
540
|
+
let index = elements.findIndex(el => path.includes(el));
|
|
541
|
+
if (index < 0) {
|
|
542
|
+
// Not on the text itself. A click on the choice or list item around it (its radio button,
|
|
543
|
+
// the padding) still means "this one": take the first reading element inside it.
|
|
544
|
+
const container = path.find((node) => node instanceof Element && node.matches('qti-simple-choice, li, qti-prompt'));
|
|
545
|
+
if (container)
|
|
546
|
+
index = elements.findIndex(el => container.contains(el));
|
|
547
|
+
}
|
|
548
|
+
if (index < 0)
|
|
549
|
+
return; // not on a highlighted element — stay in pick mode
|
|
550
|
+
event.preventDefault();
|
|
551
|
+
event.stopPropagation();
|
|
552
|
+
this.#stopPicking();
|
|
553
|
+
speechSynthesis.cancel();
|
|
554
|
+
this.#speakElement(index);
|
|
555
|
+
}
|
|
266
556
|
#prevElement() {
|
|
267
557
|
const elements = this.#getReadingElements();
|
|
268
558
|
if (!elements.length)
|
|
269
559
|
return;
|
|
560
|
+
this.#currentUtterance = null;
|
|
270
561
|
speechSynthesis.cancel();
|
|
271
562
|
this.#clearWordHighlight();
|
|
272
563
|
this.#currentElementIndex = Math.max(0, this.#currentElementIndex - 1);
|
|
564
|
+
this.#finished = false;
|
|
273
565
|
this.#updateContext();
|
|
274
566
|
const el = elements[this.#currentElementIndex];
|
|
275
567
|
el.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
@@ -280,9 +572,11 @@ export class TestItemToSpeech extends LitElement {
|
|
|
280
572
|
const elements = this.#getReadingElements();
|
|
281
573
|
if (!elements.length)
|
|
282
574
|
return;
|
|
575
|
+
this.#currentUtterance = null;
|
|
283
576
|
speechSynthesis.cancel();
|
|
284
577
|
this.#clearWordHighlight();
|
|
285
578
|
this.#currentElementIndex = Math.min(elements.length - 1, this.#currentElementIndex + 1);
|
|
579
|
+
this.#finished = false;
|
|
286
580
|
this.#updateContext();
|
|
287
581
|
const el = elements[this.#currentElementIndex];
|
|
288
582
|
el.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
@@ -303,13 +597,19 @@ export class TestItemToSpeech extends LitElement {
|
|
|
303
597
|
}
|
|
304
598
|
/** Highlight the whole element as the navigation cursor (blue). */
|
|
305
599
|
#highlightElement(element) {
|
|
600
|
+
this.#highlightElements([element]);
|
|
601
|
+
}
|
|
602
|
+
/** Put the blue cursor highlight on several elements at once (pick mode). */
|
|
603
|
+
#highlightElements(elements) {
|
|
306
604
|
if (!('highlights' in CSS))
|
|
307
605
|
return;
|
|
308
606
|
this.#ensureHighlightStyles();
|
|
309
607
|
this.#elementHighlight.clear();
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
608
|
+
for (const element of elements) {
|
|
609
|
+
const range = new Range();
|
|
610
|
+
range.selectNodeContents(element);
|
|
611
|
+
this.#elementHighlight.add(range);
|
|
612
|
+
}
|
|
313
613
|
CSS.highlights.set(HIGHLIGHT_ELEMENT, this.#elementHighlight);
|
|
314
614
|
}
|
|
315
615
|
/** Highlight the current word within #textNodes (yellow, during speech). */
|
|
@@ -350,15 +650,17 @@ export class TestItemToSpeech extends LitElement {
|
|
|
350
650
|
if (!('highlights' in CSS))
|
|
351
651
|
return;
|
|
352
652
|
this.#wordHighlight.clear();
|
|
353
|
-
|
|
653
|
+
// The registry is shared by every player on the page: only drop the entry if it is ours.
|
|
654
|
+
if (CSS.highlights.get(HIGHLIGHT_WORD) === this.#wordHighlight)
|
|
655
|
+
CSS.highlights.delete(HIGHLIGHT_WORD);
|
|
354
656
|
}
|
|
355
657
|
#clearAllHighlights() {
|
|
356
658
|
if (!('highlights' in CSS))
|
|
357
659
|
return;
|
|
358
|
-
this.#
|
|
359
|
-
CSS.highlights.delete(HIGHLIGHT_WORD);
|
|
660
|
+
this.#clearWordHighlight();
|
|
360
661
|
this.#elementHighlight.clear();
|
|
361
|
-
CSS.highlights.
|
|
662
|
+
if (CSS.highlights.get(HIGHLIGHT_ELEMENT) === this.#elementHighlight)
|
|
663
|
+
CSS.highlights.delete(HIGHLIGHT_ELEMENT);
|
|
362
664
|
}
|
|
363
665
|
/** Fallback word-length when charLength is 0. */
|
|
364
666
|
#wordLengthAt(charIndex, text) {
|
|
@@ -379,18 +681,32 @@ export class TestItemToSpeech extends LitElement {
|
|
|
379
681
|
if (!document.adoptedStyleSheets.includes(sheet)) {
|
|
380
682
|
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
|
|
381
683
|
}
|
|
382
|
-
// 2.
|
|
684
|
+
// 2. Shadow roots the highlighted text may live in: test-container's, and the player's own
|
|
685
|
+
// root when the player itself sits inside a shadow tree (e.g. built into an item card).
|
|
686
|
+
const roots = new Set();
|
|
383
687
|
const testContainer = this.closest('test-navigation')?.querySelector('test-container') ??
|
|
384
688
|
this.closest('qti-test')?.querySelector('test-container');
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
689
|
+
if (testContainer?.shadowRoot)
|
|
690
|
+
roots.add(testContainer.shadowRoot);
|
|
691
|
+
const ownRoot = this.getRootNode();
|
|
692
|
+
if (ownRoot instanceof ShadowRoot)
|
|
693
|
+
roots.add(ownRoot);
|
|
694
|
+
const itemRoot = this.#readingElements?.[0]?.getRootNode();
|
|
695
|
+
if (itemRoot instanceof ShadowRoot)
|
|
696
|
+
roots.add(itemRoot);
|
|
697
|
+
for (const root of roots) {
|
|
698
|
+
if (!root.adoptedStyleSheets.includes(sheet)) {
|
|
699
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
700
|
+
}
|
|
388
701
|
}
|
|
389
702
|
}
|
|
390
703
|
}
|
|
391
704
|
__decorate([
|
|
392
705
|
property({ type: String })
|
|
393
706
|
], TestItemToSpeech.prototype, "language", void 0);
|
|
707
|
+
__decorate([
|
|
708
|
+
property({ type: String, attribute: 'item-ref-id' })
|
|
709
|
+
], TestItemToSpeech.prototype, "itemRefId", void 0);
|
|
394
710
|
__decorate([
|
|
395
711
|
consume({ context: sessionContext, subscribe: true })
|
|
396
712
|
], TestItemToSpeech.prototype, "_sessionContext", void 0);
|
|
@@ -401,13 +717,14 @@ __decorate([
|
|
|
401
717
|
// ─── Child control elements ───────────────────────────────────────────────────
|
|
402
718
|
/**
|
|
403
719
|
* Play/pause/resume toggle button — always enabled, cycles through states.
|
|
404
|
-
* Use named slots `play` and `pause` to customise the icons/labels independently
|
|
720
|
+
* Use named slots `play` and `pause` to customise the icons/labels independently; `label` and
|
|
721
|
+
* `pause-label` name the button in each state (set them when the slotted content is an icon).
|
|
405
722
|
*
|
|
406
723
|
* @example
|
|
407
724
|
* ```html
|
|
408
725
|
* <test-tts-play>
|
|
409
|
-
* <span slot="play"
|
|
410
|
-
* <span slot="pause"
|
|
726
|
+
* <span slot="play">Voorlezen</span>
|
|
727
|
+
* <span slot="pause">Pauzeren</span>
|
|
411
728
|
* </test-tts-play>
|
|
412
729
|
* ```
|
|
413
730
|
*
|
|
@@ -428,12 +745,17 @@ export class TestTtsPlay extends TtsButtonBase {
|
|
|
428
745
|
render() {
|
|
429
746
|
const playing = this._tts?.state === 'playing';
|
|
430
747
|
return html `
|
|
431
|
-
<button part="button" @click=${this.#toggle}>
|
|
432
|
-
${playing
|
|
748
|
+
<button part="button" aria-label=${ifDefined(playing ? this.pauseLabel : this.label)} @click=${this.#toggle}>
|
|
749
|
+
${playing
|
|
750
|
+
? html `<slot name="pause">${iconContent('pause', this.pauseLabel ?? 'Pause')}</slot>`
|
|
751
|
+
: html `<slot name="play">${iconContent('play', this.label ?? 'Play')}</slot>`}
|
|
433
752
|
</button>
|
|
434
753
|
`;
|
|
435
754
|
}
|
|
436
755
|
}
|
|
756
|
+
__decorate([
|
|
757
|
+
property({ type: String, attribute: 'pause-label' })
|
|
758
|
+
], TestTtsPlay.prototype, "pauseLabel", void 0);
|
|
437
759
|
/**
|
|
438
760
|
* Pause button — enabled only when playing.
|
|
439
761
|
* @cssstate idle / playing / paused
|
|
@@ -442,8 +764,13 @@ export class TestTtsPlay extends TtsButtonBase {
|
|
|
442
764
|
export class TestTtsPause extends TtsButtonBase {
|
|
443
765
|
render() {
|
|
444
766
|
return html `
|
|
445
|
-
<button
|
|
446
|
-
|
|
767
|
+
<button
|
|
768
|
+
part="button"
|
|
769
|
+
aria-label=${ifDefined(this.label)}
|
|
770
|
+
?disabled=${this._tts?.state !== 'playing'}
|
|
771
|
+
@click=${() => this._tts?.pause()}
|
|
772
|
+
>
|
|
773
|
+
<slot>${iconContent('pause', this.label ?? 'Pause')}</slot>
|
|
447
774
|
</button>
|
|
448
775
|
`;
|
|
449
776
|
}
|
|
@@ -456,56 +783,96 @@ export class TestTtsPause extends TtsButtonBase {
|
|
|
456
783
|
export class TestTtsResume extends TtsButtonBase {
|
|
457
784
|
render() {
|
|
458
785
|
return html `
|
|
459
|
-
<button
|
|
460
|
-
|
|
786
|
+
<button
|
|
787
|
+
part="button"
|
|
788
|
+
aria-label=${ifDefined(this.label)}
|
|
789
|
+
?disabled=${this._tts?.state !== 'paused'}
|
|
790
|
+
@click=${() => this._tts?.resume()}
|
|
791
|
+
>
|
|
792
|
+
<slot>${iconContent('play', this.label ?? 'Resume')}</slot>
|
|
461
793
|
</button>
|
|
462
794
|
`;
|
|
463
795
|
}
|
|
464
796
|
}
|
|
465
797
|
/**
|
|
466
|
-
* Stop button — enabled when playing or
|
|
798
|
+
* Stop button — enabled when playing, paused, or in pick mode (where it leaves pick mode).
|
|
467
799
|
* @cssstate idle / playing / paused
|
|
468
800
|
* @csspart button
|
|
469
801
|
*/
|
|
470
802
|
export class TestTtsStop extends TtsButtonBase {
|
|
471
803
|
render() {
|
|
804
|
+
const idle = this._tts?.state === 'idle' && !this._tts?.picking;
|
|
472
805
|
return html `
|
|
473
|
-
<button part="button"
|
|
474
|
-
<slot
|
|
806
|
+
<button part="button" aria-label=${ifDefined(this.label)} ?disabled=${idle} @click=${() => this._tts?.stop()}>
|
|
807
|
+
<slot>${iconContent('stop', this.label ?? 'Stop')}</slot>
|
|
475
808
|
</button>
|
|
476
809
|
`;
|
|
477
810
|
}
|
|
478
811
|
}
|
|
479
812
|
/**
|
|
480
813
|
* Prev-element button — moves cursor to the previous reading element and pauses.
|
|
481
|
-
* Disabled at the first element
|
|
814
|
+
* Disabled at the first element. Reading elements are collected lazily, so an element count
|
|
815
|
+
* of 0 means "not looked yet" rather than "nothing to read": the button stays enabled and the
|
|
816
|
+
* first click collects them.
|
|
482
817
|
* @cssstate idle / playing / paused
|
|
483
818
|
* @csspart button
|
|
484
819
|
*/
|
|
485
820
|
export class TestTtsPrev extends TtsButtonBase {
|
|
486
821
|
render() {
|
|
822
|
+
const collected = (this._tts?.elementCount ?? 0) > 0;
|
|
487
823
|
const atStart = (this._tts?.currentElementIndex ?? 0) === 0;
|
|
488
|
-
const noElements = (this._tts?.elementCount ?? 0) === 0;
|
|
489
824
|
return html `
|
|
490
|
-
<button
|
|
491
|
-
|
|
825
|
+
<button
|
|
826
|
+
part="button"
|
|
827
|
+
aria-label=${ifDefined(this.label)}
|
|
828
|
+
?disabled=${collected && atStart}
|
|
829
|
+
@click=${() => this._tts?.prevElement()}
|
|
830
|
+
>
|
|
831
|
+
<slot>${iconContent('prev', this.label ?? 'Previous sentence')}</slot>
|
|
492
832
|
</button>
|
|
493
833
|
`;
|
|
494
834
|
}
|
|
495
835
|
}
|
|
496
836
|
/**
|
|
497
837
|
* Next-element button — moves cursor to the next reading element and pauses.
|
|
498
|
-
* Disabled at the last element
|
|
838
|
+
* Disabled at the last element; enabled while the elements have not been collected yet
|
|
839
|
+
* (see `TestTtsPrev`).
|
|
499
840
|
* @cssstate idle / playing / paused
|
|
500
841
|
* @csspart button
|
|
501
842
|
*/
|
|
502
843
|
export class TestTtsNext extends TtsButtonBase {
|
|
503
844
|
render() {
|
|
504
|
-
const
|
|
505
|
-
const
|
|
845
|
+
const count = this._tts?.elementCount ?? 0;
|
|
846
|
+
const atEnd = count > 0 && (this._tts?.currentElementIndex ?? 0) >= count - 1;
|
|
847
|
+
return html `
|
|
848
|
+
<button
|
|
849
|
+
part="button"
|
|
850
|
+
aria-label=${ifDefined(this.label)}
|
|
851
|
+
?disabled=${atEnd}
|
|
852
|
+
@click=${() => this._tts?.nextElement()}
|
|
853
|
+
>
|
|
854
|
+
<slot>${iconContent('next', this.label ?? 'Next sentence')}</slot>
|
|
855
|
+
</button>
|
|
856
|
+
`;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Pick button — toggles pick mode on the controller: every reading element is highlighted and
|
|
861
|
+
* a click on one of them starts reading from there. Reflects the mode as `:state(picking)`.
|
|
862
|
+
* @cssstate idle / playing / paused
|
|
863
|
+
* @cssstate picking
|
|
864
|
+
* @csspart button
|
|
865
|
+
*/
|
|
866
|
+
export class TestTtsPick extends TtsButtonBase {
|
|
867
|
+
render() {
|
|
506
868
|
return html `
|
|
507
|
-
<button
|
|
508
|
-
|
|
869
|
+
<button
|
|
870
|
+
part="button"
|
|
871
|
+
aria-label=${ifDefined(this.label)}
|
|
872
|
+
aria-pressed=${this._tts?.picking ? 'true' : 'false'}
|
|
873
|
+
@click=${() => this._tts?.togglePick()}
|
|
874
|
+
>
|
|
875
|
+
<slot>${iconContent('pick', this.label ?? 'Read from here')}</slot>
|
|
509
876
|
</button>
|
|
510
877
|
`;
|
|
511
878
|
}
|