@qti-components/test 1.7.0 → 1.9.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 +12 -0
- package/dist/components/qti-assessment-item-ref/qti-assessment-item-ref.js +55 -28
- package/dist/components/qti-assessment-item-ref/qti-assessment-item-ref.js.map +1 -1
- package/dist/components/qti-test/qti-test.d.ts +16 -2
- package/dist/components/qti-test/qti-test.js +33 -6
- package/dist/components/qti-test/qti-test.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.d.ts +2 -0
- package/dist/components/test-check-item/test-check-item.js +20 -2
- package/dist/components/test-check-item/test-check-item.js.map +1 -1
- package/dist/components/test-container/test-container.d.ts +3 -0
- package/dist/components/test-container/test-container.js +5 -1
- package/dist/components/test-container/test-container.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 +128 -11
- package/dist/components/test-item-to-speech/test-item-to-speech.js +652 -103
- package/dist/components/test-item-to-speech/test-item-to-speech.js.map +1 -1
- package/dist/components/test-navigation/test-navigation.d.ts +9 -1
- package/dist/components/test-navigation/test-navigation.js +62 -17
- package/dist/components/test-navigation/test-navigation.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-scoring-buttons/test-scoring-buttons.d.ts +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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/test-host.context.d.ts +24 -0
- package/dist/internal/test-host.context.js +3 -0
- package/dist/internal/test-host.context.js.map +1 -0
- package/dist/mixins/index.d.ts +1 -0
- package/dist/mixins/index.js +1 -0
- package/dist/mixins/index.js.map +1 -1
- package/dist/mixins/test-base.d.ts +7 -2
- package/dist/mixins/test-base.js +53 -19
- package/dist/mixins/test-base.js.map +1 -1
- package/dist/mixins/test-navigation.mixin.d.ts +3 -4
- package/dist/mixins/test-navigation.mixin.js +11 -12
- package/dist/mixins/test-navigation.mixin.js.map +1 -1
- package/dist/mixins/test-processing.mixin.js +3 -1
- package/dist/mixins/test-processing.mixin.js.map +1 -1
- package/dist/mixins/test-state.mixin.d.ts +33 -0
- package/dist/mixins/test-state.mixin.js +130 -0
- package/dist/mixins/test-state.mixin.js.map +1 -0
- package/dist/types/iqti-test.d.ts +8 -5
- package/dist/types/iqti-test.js.map +1 -1
- package/dist/types/qti-test-state.d.ts +45 -0
- package/dist/types/qti-test-state.js +2 -0
- package/dist/types/qti-test-state.js.map +1 -0
- package/package.json +3 -3
|
@@ -7,7 +7,10 @@ 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 { testHostContext } from '../../internal/test-host.context';
|
|
13
|
+
import * as styles from '../styles';
|
|
11
14
|
// ─── CSS Custom Highlight ─────────────────────────────────────────────────────
|
|
12
15
|
const HIGHLIGHT_ELEMENT = 'tts-element'; // navigation cursor — whole element
|
|
13
16
|
const HIGHLIGHT_WORD = 'tts-word'; // word boundary during speech
|
|
@@ -24,6 +27,25 @@ function getHighlightSheet() {
|
|
|
24
27
|
return highlightSheet;
|
|
25
28
|
}
|
|
26
29
|
export const ttsContext = createContext(Symbol('tts-context'));
|
|
30
|
+
// ─── Icons ────────────────────────────────────────────────────────────────────
|
|
31
|
+
// Plain geometric glyphs on a 24-unit grid, painted in currentColor. SVG rather than text so
|
|
32
|
+
// every button gets the same box: text glyphs (◀◀ ▶ ⏸ ■ ☞) differ in width, and some fall back
|
|
33
|
+
// to an emoji font with different line metrics.
|
|
34
|
+
const ICONS = {
|
|
35
|
+
play: 'M8 5v14l11-7z',
|
|
36
|
+
pause: 'M6 5h4v14H6zM14 5h4v14h-4z',
|
|
37
|
+
stop: 'M6 6h12v12H6z',
|
|
38
|
+
prev: 'M6 5h2v14H6zM20 5v14L9 12z',
|
|
39
|
+
next: 'M16 5h2v14h-2zM4 5v14l11-7z',
|
|
40
|
+
pick: 'M5 3l13 7-5.5 1.6 3.4 6.6-2.4 1.3-3.4-6.7L6 17z'
|
|
41
|
+
};
|
|
42
|
+
/** Default button content: the icon, plus a visually hidden label as the accessible name. */
|
|
43
|
+
const iconContent = (icon, label) => {
|
|
44
|
+
const svg = html `<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
|
45
|
+
<path d="${ICONS[icon]}"></path>
|
|
46
|
+
</svg>`;
|
|
47
|
+
return html `${svg}<span class="label">${label}</span>`;
|
|
48
|
+
};
|
|
27
49
|
// ─── Shared base for child button elements ────────────────────────────────────
|
|
28
50
|
/**
|
|
29
51
|
* Base class shared by all TTS control elements. Consumes `ttsContext` and
|
|
@@ -33,25 +55,72 @@ export const ttsContext = createContext(Symbol('tts-context'));
|
|
|
33
55
|
class TtsButtonBase extends LitElement {
|
|
34
56
|
static { this.styles = css `
|
|
35
57
|
:host {
|
|
36
|
-
display: inline-
|
|
58
|
+
display: inline-flex;
|
|
37
59
|
}
|
|
38
60
|
button {
|
|
39
|
-
|
|
61
|
+
${styles.btn};
|
|
62
|
+
min-inline-size: var(--test-button-size, 2.25rem);
|
|
63
|
+
padding-inline: 0.5rem;
|
|
64
|
+
/* Containing block for the visually hidden .label. Without it the label is placed against
|
|
65
|
+
the nearest positioned ancestor, which can sit outside a scroll container and make the
|
|
66
|
+
page as tall as the scrolled content. */
|
|
67
|
+
position: relative;
|
|
68
|
+
}
|
|
69
|
+
button:hover:not(:disabled) {
|
|
70
|
+
${styles.btnInteractive};
|
|
71
|
+
}
|
|
72
|
+
button:focus-visible {
|
|
73
|
+
${styles.focusRing};
|
|
40
74
|
}
|
|
41
75
|
button:disabled {
|
|
42
|
-
|
|
43
|
-
|
|
76
|
+
${styles.dis};
|
|
77
|
+
}
|
|
78
|
+
button[aria-pressed='true'],
|
|
79
|
+
button[aria-pressed='true']:hover {
|
|
80
|
+
background-color: var(--test-button-selected-background-color, var(--qti-selected-bg, #0175aa));
|
|
81
|
+
color: var(--test-button-selected-color, var(--qti-selected-color, #fff));
|
|
82
|
+
}
|
|
83
|
+
svg,
|
|
84
|
+
::slotted(svg) {
|
|
85
|
+
flex: none;
|
|
86
|
+
inline-size: var(--test-tts-icon-size, 1.125rem);
|
|
87
|
+
block-size: var(--test-tts-icon-size, 1.125rem);
|
|
88
|
+
}
|
|
89
|
+
svg {
|
|
90
|
+
fill: currentColor;
|
|
91
|
+
}
|
|
92
|
+
/* The default content is icon-only; this keeps the button's accessible name. */
|
|
93
|
+
.label {
|
|
94
|
+
position: absolute;
|
|
95
|
+
inline-size: 1px;
|
|
96
|
+
block-size: 1px;
|
|
97
|
+
overflow: hidden;
|
|
98
|
+
clip-path: inset(50%);
|
|
99
|
+
white-space: nowrap;
|
|
44
100
|
}
|
|
45
101
|
`; }
|
|
46
102
|
#internals = this.attachInternals();
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
103
|
+
#mirroredState;
|
|
104
|
+
#mirroredPicking = false;
|
|
105
|
+
updated(_changed) {
|
|
106
|
+
// `_tts` is a context field, not a reactive property, so it never shows up in `changed`:
|
|
107
|
+
// compare against what was last mirrored instead.
|
|
108
|
+
const state = this._tts?.state;
|
|
109
|
+
const picking = this._tts?.picking ?? false;
|
|
110
|
+
if (state === this.#mirroredState && picking === this.#mirroredPicking)
|
|
111
|
+
return;
|
|
112
|
+
this.#mirroredState = state;
|
|
113
|
+
this.#mirroredPicking = picking;
|
|
114
|
+
this.#internals.states.clear();
|
|
115
|
+
if (state)
|
|
116
|
+
this.#internals.states.add(state);
|
|
117
|
+
if (picking)
|
|
118
|
+
this.#internals.states.add('picking');
|
|
53
119
|
}
|
|
54
120
|
}
|
|
121
|
+
__decorate([
|
|
122
|
+
property({ type: String })
|
|
123
|
+
], TtsButtonBase.prototype, "label", void 0);
|
|
55
124
|
__decorate([
|
|
56
125
|
consume({ context: ttsContext, subscribe: true })
|
|
57
126
|
], TtsButtonBase.prototype, "_tts", void 0);
|
|
@@ -65,50 +134,187 @@ __decorate([
|
|
|
65
134
|
* ```html
|
|
66
135
|
* <test-item-to-speech language="nl-NL">
|
|
67
136
|
* <test-tts-play></test-tts-play>
|
|
68
|
-
* <test-tts-prev
|
|
69
|
-
* <test-tts-next
|
|
70
|
-
* <test-tts-stop
|
|
137
|
+
* <test-tts-prev></test-tts-prev>
|
|
138
|
+
* <test-tts-next></test-tts-next>
|
|
139
|
+
* <test-tts-stop></test-tts-stop>
|
|
71
140
|
* </test-item-to-speech>
|
|
72
141
|
* ```
|
|
73
142
|
*
|
|
143
|
+
* The controller lays its controls out as a wrapping row; each control draws an icon button,
|
|
144
|
+
* and slotted content replaces the icon — a slotted `<svg>` is sized like the default one. Give a
|
|
145
|
+
* 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
|
|
146
|
+
* defaults as fallbacks) and can be sized with `--test-button-size` and `--test-tts-icon-size`.
|
|
147
|
+
*
|
|
148
|
+
* ### Language resolution
|
|
149
|
+
*
|
|
150
|
+
* Each reading element is spoken in the language of the nearest `lang` attribute:
|
|
151
|
+
*
|
|
152
|
+
* 1. `lang` (or `xml:lang`) on the element itself
|
|
153
|
+
* 2. `lang` on the closest ancestor — including `<qti-assessment-item xml:lang>` and, across
|
|
154
|
+
* shadow roots, anything wrapping the test
|
|
155
|
+
* 3. `lang` on the document's `<html>` element
|
|
156
|
+
* 4. the `language` attribute on `<test-item-to-speech>`
|
|
157
|
+
*
|
|
158
|
+
* The transformer copies `xml:lang` over as a plain `lang` attribute, so QTI authored with
|
|
159
|
+
* `xml:lang="en-GB"` on a `<p>` or on the item root is honoured without further work.
|
|
160
|
+
*
|
|
161
|
+
* ### What is read
|
|
162
|
+
*
|
|
163
|
+
* Decided once per connect, by where the player is placed. The first rule that matches wins:
|
|
164
|
+
*
|
|
165
|
+
* 1. **Next to a stimulus placeholder** — the player shares its parent with a
|
|
166
|
+
* `[data-stimulus-idref]` element, e.g. in a reading-text panel beside the questions: the
|
|
167
|
+
* player reads that stimulus (its `qti-stimulus-body`), and only that. Not inside the
|
|
168
|
+
* placeholder: loading a stimulus replaces the placeholder's content. Items and navigation
|
|
169
|
+
* are ignored; when the stimulus is (re)loaded or the placeholder swapped, the player starts
|
|
170
|
+
* over on the new content.
|
|
171
|
+
* 2. **Next to `[data-tts-content]`** — the player shares its parent with an element marked
|
|
172
|
+
* `data-tts-content`: any HTML, not QTI, e.g. a start screen rendered by React. The marker goes
|
|
173
|
+
* on one container; the player reads the readable elements inside it. Navigation is ignored.
|
|
174
|
+
* The reading list is rebuilt when the player starts from the top, and when an element in it
|
|
175
|
+
* has left the page — so a re-render of the content never interrupts speech.
|
|
176
|
+
* 3. **Inside a `qti-assessment-item-ref`** — e.g. in a `<template item-ref>` on `<qti-test>`,
|
|
177
|
+
* so every item gets a player of its own: the player reads that item, and only that item.
|
|
178
|
+
* The navigation cursor is ignored; speech stops when the player leaves the page with its item.
|
|
179
|
+
* 4. **Anywhere else** — a toolbar in the page chrome: the player follows the navigation cursor
|
|
180
|
+
* and reads the item `navItemRefId` (session context) points at, found in any
|
|
181
|
+
* `test-container` reachable from the player. Navigating stops speech and starts the next
|
|
182
|
+
* item from the top.
|
|
183
|
+
*
|
|
184
|
+
* ```html
|
|
185
|
+
* <!-- 1. a player for the shared reading text -->
|
|
186
|
+
* <aside>
|
|
187
|
+
* <test-item-to-speech>…</test-item-to-speech>
|
|
188
|
+
* <div data-stimulus-idref="Stimulus1"></div>
|
|
189
|
+
* </aside>
|
|
190
|
+
*
|
|
191
|
+
* <!-- 2. a player for any other HTML -->
|
|
192
|
+
* <div>
|
|
193
|
+
* <test-item-to-speech>…</test-item-to-speech>
|
|
194
|
+
* <section data-tts-content lang="nl-NL">…</section>
|
|
195
|
+
* </div>
|
|
196
|
+
*
|
|
197
|
+
* <!-- 3. one player per item -->
|
|
198
|
+
* <qti-test>
|
|
199
|
+
* <template item-ref>
|
|
200
|
+
* <test-item-to-speech>…</test-item-to-speech>
|
|
201
|
+
* {{ xmlDoc }}
|
|
202
|
+
* </template>
|
|
203
|
+
* …
|
|
204
|
+
* </qti-test>
|
|
205
|
+
*
|
|
206
|
+
* <!-- 4. one player in the toolbar, following navigation -->
|
|
207
|
+
* <test-navigation>
|
|
208
|
+
* <header><test-item-to-speech>…</test-item-to-speech></header>
|
|
209
|
+
* <test-container test-url="…"></test-container>
|
|
210
|
+
* </test-navigation>
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* In modes 3 and 4 the item re-rendering (navigating to the item already shown) restarts the
|
|
214
|
+
* player on the fresh content. An item player reads the item body as rendered: a stimulus
|
|
215
|
+
* placed inside the item is read with it, one placed outside (mode 1) is not. Only one player
|
|
216
|
+
* can speak at a time; starting one stops any other.
|
|
217
|
+
*
|
|
218
|
+
* Read are `p`, `h1`–`h6`, `li`, `dt`, `dd`, `th`, `td`, `caption`, `blockquote`, `figcaption`,
|
|
219
|
+
* `qti-prompt` and `qti-simple-choice` — the innermost match only — unless hidden (`display: none`)
|
|
220
|
+
* or inside `aria-hidden="true"`.
|
|
221
|
+
*
|
|
222
|
+
* ### Starting somewhere in the middle
|
|
223
|
+
*
|
|
224
|
+
* `<test-tts-pick>` switches the player into pick mode: every reading element gets the blue
|
|
225
|
+
* cursor highlight, and the first click on one of them leaves pick mode and starts reading
|
|
226
|
+
* from that element. Pressing the button again leaves pick mode without reading.
|
|
227
|
+
*
|
|
74
228
|
* @cssstate idle - No speech active
|
|
229
|
+
* @cssstate picking - Pick mode: all reading elements highlighted, waiting for a click
|
|
75
230
|
* @cssstate playing - Speech is playing
|
|
76
231
|
* @cssstate paused - Speech is paused
|
|
77
232
|
*/
|
|
78
233
|
export class TestItemToSpeech extends LitElement {
|
|
234
|
+
static { this.styles = css `
|
|
235
|
+
:host {
|
|
236
|
+
display: inline-flex;
|
|
237
|
+
flex-wrap: wrap;
|
|
238
|
+
align-items: center;
|
|
239
|
+
gap: var(--test-tts-gap, 0.25rem);
|
|
240
|
+
}
|
|
241
|
+
`; }
|
|
79
242
|
#internals;
|
|
80
|
-
|
|
81
|
-
|
|
243
|
+
// Stimulus mode: set on connect when a stimulus placeholder sits next to the player. The
|
|
244
|
+
// placeholder itself is looked up on every use — a host may swap the element.
|
|
245
|
+
#stimulusMode;
|
|
246
|
+
// Stimulus mode: resets the player when the stimulus content next to it is replaced.
|
|
247
|
+
#stimulusObserver;
|
|
248
|
+
// Content mode: set on connect when a `[data-tts-content]` element sits next to the player.
|
|
249
|
+
// Any HTML, not QTI. No observer: the reading list is rebuilt on demand (#getReadingElements).
|
|
250
|
+
#contentMode;
|
|
251
|
+
// Item-ref mode: the item-ref this player sits in, resolved on connect. Null means cursor mode.
|
|
252
|
+
#itemRef;
|
|
253
|
+
// Where the item-connected (and, in cursor mode, navigation) events are listened for.
|
|
82
254
|
#eventHost;
|
|
83
|
-
|
|
255
|
+
#boundHandleItemConnected;
|
|
256
|
+
#boundHandleNavigation;
|
|
257
|
+
// Cursor mode: the previous navigation position, to detect moving to another item or section.
|
|
258
|
+
#prevNavItemRefId;
|
|
259
|
+
#prevNavSectionId;
|
|
260
|
+
// Current set of block-level reading elements for the item
|
|
84
261
|
#readingElements;
|
|
85
262
|
#currentElementIndex;
|
|
86
263
|
// Text nodes of the element currently being spoken (for word-boundary highlight)
|
|
87
264
|
#textNodes;
|
|
265
|
+
// The utterance this player is speaking; lets the error handler tell "our speech was
|
|
266
|
+
// cancelled by another player" apart from a cancel we issued ourselves.
|
|
267
|
+
#currentUtterance;
|
|
268
|
+
// Pick mode: all elements highlighted, a click on one starts reading from there.
|
|
269
|
+
#picking;
|
|
270
|
+
#pickRoot;
|
|
271
|
+
#boundHandlePickClick;
|
|
272
|
+
// Set when the last element has been read: the cursor stays on it (so prev still works),
|
|
273
|
+
// but the next play starts the item over instead of repeating that last element.
|
|
274
|
+
#finished;
|
|
88
275
|
// Two separate CSS Highlight objects
|
|
89
276
|
#elementHighlight; // whole-element cursor
|
|
90
277
|
#wordHighlight; // current word during speech
|
|
91
|
-
// Tracks previous navItemRefId to detect item switches
|
|
92
|
-
#prevNavItemRefId;
|
|
93
|
-
#boundHandleNavigation;
|
|
94
278
|
constructor() {
|
|
95
279
|
super();
|
|
280
|
+
/** Fallback language when neither the item content nor the document declares one. */
|
|
96
281
|
this.language = 'nl-NL';
|
|
97
282
|
this.#internals = this.attachInternals();
|
|
98
|
-
|
|
99
|
-
|
|
283
|
+
// Stimulus mode: set on connect when a stimulus placeholder sits next to the player. The
|
|
284
|
+
// placeholder itself is looked up on every use — a host may swap the element.
|
|
285
|
+
this.#stimulusMode = false;
|
|
286
|
+
// Stimulus mode: resets the player when the stimulus content next to it is replaced.
|
|
287
|
+
this.#stimulusObserver = null;
|
|
288
|
+
// Content mode: set on connect when a `[data-tts-content]` element sits next to the player.
|
|
289
|
+
// Any HTML, not QTI. No observer: the reading list is rebuilt on demand (#getReadingElements).
|
|
290
|
+
this.#contentMode = false;
|
|
291
|
+
// Item-ref mode: the item-ref this player sits in, resolved on connect. Null means cursor mode.
|
|
292
|
+
this.#itemRef = null;
|
|
293
|
+
// Where the item-connected (and, in cursor mode, navigation) events are listened for.
|
|
100
294
|
this.#eventHost = null;
|
|
101
|
-
|
|
295
|
+
this.#boundHandleItemConnected = this.#handleItemConnected.bind(this);
|
|
296
|
+
this.#boundHandleNavigation = () => this.#stop();
|
|
297
|
+
// Cursor mode: the previous navigation position, to detect moving to another item or section.
|
|
298
|
+
this.#prevNavItemRefId = undefined;
|
|
299
|
+
this.#prevNavSectionId = undefined;
|
|
300
|
+
// Current set of block-level reading elements for the item
|
|
102
301
|
this.#readingElements = null;
|
|
103
302
|
this.#currentElementIndex = 0;
|
|
104
303
|
// Text nodes of the element currently being spoken (for word-boundary highlight)
|
|
105
304
|
this.#textNodes = [];
|
|
305
|
+
// The utterance this player is speaking; lets the error handler tell "our speech was
|
|
306
|
+
// cancelled by another player" apart from a cancel we issued ourselves.
|
|
307
|
+
this.#currentUtterance = null;
|
|
308
|
+
// Pick mode: all elements highlighted, a click on one starts reading from there.
|
|
309
|
+
this.#picking = false;
|
|
310
|
+
this.#pickRoot = null;
|
|
311
|
+
this.#boundHandlePickClick = this.#handlePickClick.bind(this);
|
|
312
|
+
// Set when the last element has been read: the cursor stays on it (so prev still works),
|
|
313
|
+
// but the next play starts the item over instead of repeating that last element.
|
|
314
|
+
this.#finished = false;
|
|
106
315
|
// Two separate CSS Highlight objects
|
|
107
316
|
this.#elementHighlight = new Highlight(); // whole-element cursor
|
|
108
317
|
this.#wordHighlight = new Highlight(); // current word during speech
|
|
109
|
-
// Tracks previous navItemRefId to detect item switches
|
|
110
|
-
this.#prevNavItemRefId = undefined;
|
|
111
|
-
this.#boundHandleNavigation = () => this.#stop();
|
|
112
318
|
this._ttsContext = {
|
|
113
319
|
state: 'idle',
|
|
114
320
|
currentElementIndex: 0,
|
|
@@ -116,55 +322,195 @@ export class TestItemToSpeech extends LitElement {
|
|
|
116
322
|
play: () => this.#playSpeech(),
|
|
117
323
|
pause: () => this.#pause(),
|
|
118
324
|
resume: () => this.#resume(),
|
|
119
|
-
stop: () =>
|
|
325
|
+
stop: () => {
|
|
326
|
+
this.#stopPicking();
|
|
327
|
+
this.#stop();
|
|
328
|
+
},
|
|
120
329
|
prevElement: () => this.#prevElement(),
|
|
121
|
-
nextElement: () => this.#nextElement()
|
|
330
|
+
nextElement: () => this.#nextElement(),
|
|
331
|
+
togglePick: () => this.#togglePick(),
|
|
332
|
+
picking: false
|
|
122
333
|
};
|
|
123
334
|
}
|
|
335
|
+
// willUpdate, not updated: resetting writes _ttsContext, and state set here goes into the
|
|
336
|
+
// render already under way instead of scheduling a second one (Lit's change-in-update).
|
|
124
337
|
willUpdate(changed) {
|
|
125
|
-
|
|
126
|
-
|
|
338
|
+
super.willUpdate(changed);
|
|
339
|
+
// Stimulus, content and item-ref mode: the navigation cursor does not change what this player reads.
|
|
340
|
+
if (this.#stimulusMode || this.#contentMode || this.#itemRef)
|
|
341
|
+
return;
|
|
342
|
+
// Cursor mode: moving to another item or section stops speech and starts over (ignore the
|
|
343
|
+
// initial undefined → value transition). Section navigation leaves navItemRefId null, so it
|
|
344
|
+
// needs its own check.
|
|
345
|
+
const item = this._sessionContext?.navItemRefId ?? null;
|
|
346
|
+
const section = this._sessionContext?.navSectionId ?? null;
|
|
347
|
+
const itemChanged = this.#prevNavItemRefId !== undefined && this.#prevNavItemRefId !== item;
|
|
348
|
+
const sectionChanged = this.#prevNavSectionId !== undefined && this.#prevNavSectionId !== section;
|
|
349
|
+
if (itemChanged || sectionChanged)
|
|
350
|
+
this.#resetReadingPosition();
|
|
351
|
+
this.#prevNavItemRefId = item;
|
|
352
|
+
this.#prevNavSectionId = section;
|
|
127
353
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this.#updateContext();
|
|
137
|
-
}
|
|
138
|
-
this.#prevNavItemRefId = current;
|
|
354
|
+
/** Stop speech and forget the collected reading elements, so the next play starts afresh. */
|
|
355
|
+
#resetReadingPosition() {
|
|
356
|
+
this.#stopPicking();
|
|
357
|
+
this.#stop();
|
|
358
|
+
this.#readingElements = null;
|
|
359
|
+
this.#currentElementIndex = 0;
|
|
360
|
+
this.#finished = false;
|
|
361
|
+
this.#updateContext();
|
|
139
362
|
}
|
|
140
363
|
connectedCallback() {
|
|
141
364
|
super.connectedCallback();
|
|
142
365
|
this.#setSpeechState('idle');
|
|
143
366
|
// NOTE: #ensureHighlightStyles() is called lazily on first highlight use
|
|
144
367
|
// because test-container may not be in the DOM yet at this point.
|
|
145
|
-
|
|
368
|
+
this.#stimulusMode = this.#stimulusPlaceholder() !== null;
|
|
369
|
+
if (this.#stimulusMode) {
|
|
370
|
+
// Stimulus mode: neither items nor navigation decide what is read, only the content of the
|
|
371
|
+
// placeholder. qti-components replaces it whenever it loads a stimulus (innerHTML, then
|
|
372
|
+
// append), so watch the parent — that also catches a host swapping the placeholder itself.
|
|
373
|
+
this.#stimulusObserver = new MutationObserver(records => {
|
|
374
|
+
if (records.some(record => !this.contains(record.target)))
|
|
375
|
+
this.#handleStimulusReplaced();
|
|
376
|
+
});
|
|
377
|
+
this.#stimulusObserver.observe(this.parentElement, { childList: true, subtree: true });
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
// Content mode: plain HTML next to the player. Nothing to listen for: what is read only
|
|
381
|
+
// depends on that element, and the reading list is rebuilt when it goes stale.
|
|
382
|
+
this.#contentMode = this.#contentElement() !== null;
|
|
383
|
+
if (this.#contentMode)
|
|
384
|
+
return;
|
|
385
|
+
this.#itemRef = this.closest('qti-assessment-item-ref');
|
|
386
|
+
// qti-assessment-item-connected is bubbles+composed: it passes the item's own item-ref, and
|
|
387
|
+
// reaches test-navigation / qti-test from inside test-container's shadow root.
|
|
146
388
|
this.#eventHost =
|
|
147
|
-
this.closest('test-navigation') ?? this.
|
|
389
|
+
this.#itemRef ?? this.closest('test-navigation') ?? this.testHost ?? this.getRootNode();
|
|
148
390
|
this.#eventHost.addEventListener('qti-assessment-item-connected', this.#boundHandleItemConnected);
|
|
149
|
-
|
|
150
|
-
|
|
391
|
+
if (!this.#itemRef) {
|
|
392
|
+
// Cursor mode: qti-request-navigation fires synchronously on prev/next — stop right away
|
|
393
|
+
// instead of waiting for the new position to arrive through the session context.
|
|
394
|
+
this.#eventHost.addEventListener('qti-request-navigation', this.#boundHandleNavigation);
|
|
395
|
+
}
|
|
151
396
|
}
|
|
152
397
|
disconnectedCallback() {
|
|
153
398
|
super.disconnectedCallback();
|
|
154
|
-
|
|
399
|
+
// Only silence the synthesizer if it is speaking for this player.
|
|
400
|
+
if (this.#currentUtterance) {
|
|
401
|
+
this.#currentUtterance = null;
|
|
402
|
+
speechSynthesis.cancel();
|
|
403
|
+
}
|
|
404
|
+
this.#stopPicking();
|
|
155
405
|
this.#clearAllHighlights();
|
|
156
406
|
this.#eventHost?.removeEventListener('qti-assessment-item-connected', this.#boundHandleItemConnected);
|
|
157
407
|
this.#eventHost?.removeEventListener('qti-request-navigation', this.#boundHandleNavigation);
|
|
158
408
|
this.#eventHost = null;
|
|
159
|
-
this.#
|
|
409
|
+
this.#itemRef = null;
|
|
410
|
+
this.#stimulusObserver?.disconnect();
|
|
411
|
+
this.#stimulusObserver = null;
|
|
412
|
+
this.#stimulusMode = false;
|
|
413
|
+
this.#contentMode = false;
|
|
160
414
|
}
|
|
161
415
|
render() {
|
|
162
416
|
return html `<slot></slot>`;
|
|
163
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* An item (re)rendered. If it is ours — e.g. after navigating to the item already shown — the
|
|
420
|
+
* elements collected earlier are detached now. That does not move the navigation cursor, so
|
|
421
|
+
* the check in updated() does not catch it.
|
|
422
|
+
*/
|
|
164
423
|
#handleItemConnected(event) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
424
|
+
if (this.#readingElements === null)
|
|
425
|
+
return;
|
|
426
|
+
// In item-ref mode the event came through our own item-ref, so it is always ours.
|
|
427
|
+
const identifier = event.detail?.identifier;
|
|
428
|
+
if (this.#itemRef || identifier === this._sessionContext?.navItemRefId)
|
|
429
|
+
this.#resetReadingPosition();
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Stimulus mode: the stimulus next to the player was (re)loaded or swapped. Forget what was
|
|
433
|
+
* collected, so the next play reads the new content from the top. Unlike
|
|
434
|
+
* #resetReadingPosition this only cancels speech that is ours: a stimulus can finish loading
|
|
435
|
+
* while an item player on the same page is speaking.
|
|
436
|
+
*/
|
|
437
|
+
#handleStimulusReplaced() {
|
|
438
|
+
if (this.#currentUtterance) {
|
|
439
|
+
this.#currentUtterance = null;
|
|
440
|
+
speechSynthesis.cancel();
|
|
441
|
+
}
|
|
442
|
+
this.#stopPicking();
|
|
443
|
+
this.#clearAllHighlights();
|
|
444
|
+
this.#setSpeechState('idle');
|
|
445
|
+
this.#readingElements = null;
|
|
446
|
+
this.#currentElementIndex = 0;
|
|
447
|
+
this.#finished = false;
|
|
448
|
+
this.#updateContext();
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Stimulus mode: the `[data-stimulus-idref]` placeholder that shares this player's parent, or
|
|
452
|
+
* null. A placeholder holds the stimulus once loaded; the player cannot sit inside it, because
|
|
453
|
+
* loading replaces the placeholder's content.
|
|
454
|
+
*/
|
|
455
|
+
#stimulusPlaceholder() {
|
|
456
|
+
return this.parentElement?.querySelector(':scope > [data-stimulus-idref]') ?? null;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Content mode: the `[data-tts-content]` element that shares this player's parent, or null.
|
|
460
|
+
* The marker goes on one container; the player finds the readable elements inside it.
|
|
461
|
+
*/
|
|
462
|
+
#contentElement() {
|
|
463
|
+
return this.parentElement?.querySelector(':scope > [data-tts-content]') ?? null;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* The element whose text is read: the stimulus body next to the player (stimulus mode), the
|
|
467
|
+
* marked content next to it (content mode), or the item body of the item to read.
|
|
468
|
+
*/
|
|
469
|
+
#resolveReadingRoot() {
|
|
470
|
+
if (this.#stimulusMode)
|
|
471
|
+
return this.#stimulusPlaceholder()?.querySelector('qti-stimulus-body') ?? null;
|
|
472
|
+
if (this.#contentMode)
|
|
473
|
+
return this.#contentElement();
|
|
474
|
+
return this.#resolveItem()?.querySelector('qti-item-body') ?? null;
|
|
475
|
+
}
|
|
476
|
+
/** The item to read: our item-ref's (item-ref mode), or the navigated one (cursor mode). */
|
|
477
|
+
#resolveItem() {
|
|
478
|
+
if (this.#itemRef)
|
|
479
|
+
return this.#itemRef.assessmentItem;
|
|
480
|
+
const identifier = this._sessionContext?.navItemRefId;
|
|
481
|
+
return identifier ? this.#findItemInDom(identifier) : null;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Cursor mode: find the rendered item for an item-ref identifier. Our own `<qti-test>` first —
|
|
485
|
+
* a page can hold several tests showing the same item — then every root from here up to the
|
|
486
|
+
* document; each scope directly and in the `test-container` shadow roots inside it.
|
|
487
|
+
*/
|
|
488
|
+
#findItemInDom(identifier) {
|
|
489
|
+
const selector = `qti-assessment-item-ref[identifier="${CSS.escape(identifier)}"] qti-assessment-item`;
|
|
490
|
+
const search = (scope) => {
|
|
491
|
+
const direct = scope.querySelector(selector);
|
|
492
|
+
if (direct)
|
|
493
|
+
return direct;
|
|
494
|
+
for (const container of Array.from(scope.querySelectorAll('test-container'))) {
|
|
495
|
+
const inShadow = container.shadowRoot?.querySelector(selector);
|
|
496
|
+
if (inShadow)
|
|
497
|
+
return inShadow;
|
|
498
|
+
}
|
|
499
|
+
return null;
|
|
500
|
+
};
|
|
501
|
+
const test = this.closest('qti-test');
|
|
502
|
+
const inTest = test && search(test);
|
|
503
|
+
if (inTest)
|
|
504
|
+
return inTest;
|
|
505
|
+
let root = this.getRootNode();
|
|
506
|
+
for (;;) {
|
|
507
|
+
const found = search(root);
|
|
508
|
+
if (found)
|
|
509
|
+
return found;
|
|
510
|
+
const host = root.host;
|
|
511
|
+
if (!host)
|
|
512
|
+
return null;
|
|
513
|
+
root = host.getRootNode();
|
|
168
514
|
}
|
|
169
515
|
}
|
|
170
516
|
/** Update speech state on both the context (notifies children) and the host's CSS custom states. */
|
|
@@ -180,49 +526,103 @@ export class TestItemToSpeech extends LitElement {
|
|
|
180
526
|
this._ttsContext = {
|
|
181
527
|
...this._ttsContext,
|
|
182
528
|
currentElementIndex: this.#currentElementIndex,
|
|
183
|
-
elementCount: this.#
|
|
529
|
+
elementCount: this.#getReadingElements().length
|
|
184
530
|
};
|
|
185
531
|
}
|
|
186
|
-
/**
|
|
532
|
+
/**
|
|
533
|
+
* Return the currently readable elements: cached candidates for the item or stimulus (collected lazily on
|
|
534
|
+
* first call), filtered live for visibility on every call. Feedback and other
|
|
535
|
+
* conditionally-shown content (e.g. `qti-feedback-block`, which is `display: none` until a
|
|
536
|
+
* CSS custom state reveals it) is excluded while hidden and picked up again once shown,
|
|
537
|
+
* without needing to re-collect the candidate set.
|
|
538
|
+
*/
|
|
187
539
|
#getReadingElements() {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (!
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
540
|
+
// Content mode has no events or observer telling it the content changed (a framework like
|
|
541
|
+
// React re-renders it at will). Rebuild the list once an element in it has left the page;
|
|
542
|
+
// text changed inside an element needs nothing, it is read when that element is spoken.
|
|
543
|
+
if (this.#contentMode && this.#readingElements?.some(el => !el.isConnected)) {
|
|
544
|
+
this.#readingElements = null;
|
|
545
|
+
this.#currentElementIndex = 0;
|
|
546
|
+
this.#finished = false;
|
|
547
|
+
}
|
|
548
|
+
if (this.#readingElements === null) {
|
|
549
|
+
const root = this.#resolveReadingRoot();
|
|
550
|
+
// Leave the cache empty (rather than `[]`) while the item or stimulus has not rendered yet,
|
|
551
|
+
// so the next call retries instead of getting stuck on an empty result forever.
|
|
552
|
+
if (!root)
|
|
553
|
+
return [];
|
|
554
|
+
// Collect block-level elements (and QTI prompt / choice elements) that contain readable text.
|
|
555
|
+
// Keep only the innermost matches so `<li><p>…</p></li>` is not read twice. Content hidden
|
|
556
|
+
// from assistive technology (`aria-hidden="true"`, e.g. decorative icons) is not read either.
|
|
557
|
+
const selector = 'p, h1, h2, h3, h4, h5, h6, li, dt, dd, th, td, caption, blockquote, figcaption, qti-prompt, qti-simple-choice';
|
|
558
|
+
this.#readingElements = Array.from(root.querySelectorAll(selector)).filter(el => (el.textContent ?? '').trim().length > 0 && !el.querySelector(selector) && !el.closest('[aria-hidden="true"]'));
|
|
559
|
+
}
|
|
560
|
+
return this.#readingElements.filter(el => this.#isVisible(el));
|
|
561
|
+
}
|
|
562
|
+
/** True when an element is actually rendered — not `display: none` (itself or an ancestor). */
|
|
563
|
+
#isVisible(element) {
|
|
564
|
+
if (typeof element.checkVisibility === 'function') {
|
|
565
|
+
return element.checkVisibility({ checkOpacity: false, checkVisibilityCSS: true });
|
|
566
|
+
}
|
|
567
|
+
// Older Safari (< 17.4) fallback: display:none leaves no client rects. Doesn't catch
|
|
568
|
+
// visibility:hidden, which still occupies layout, but display:none is what QTI feedback
|
|
569
|
+
// elements actually use (see qti-feedback-block/qti-feedback-inline default styles).
|
|
570
|
+
return element.getClientRects().length > 0;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Resolve the speech language for a reading element.
|
|
574
|
+
* Element `lang` → closest ancestor `lang` (crossing shadow hosts) → `<html lang>` → `language` attribute.
|
|
575
|
+
* Empty `lang=""` is treated as absent.
|
|
576
|
+
*/
|
|
577
|
+
#resolveLang(element) {
|
|
578
|
+
let node = element;
|
|
579
|
+
while (node) {
|
|
580
|
+
const lang = node.getAttribute('lang') || node.getAttribute('xml:lang');
|
|
581
|
+
if (lang?.trim())
|
|
582
|
+
return lang.trim();
|
|
583
|
+
node = node.parentElement ?? node.getRootNode().host ?? null;
|
|
584
|
+
}
|
|
585
|
+
const documentLang = document.documentElement.getAttribute('lang');
|
|
586
|
+
if (documentLang?.trim())
|
|
587
|
+
return documentLang.trim();
|
|
588
|
+
return this.language;
|
|
203
589
|
}
|
|
204
590
|
#playSpeech() {
|
|
205
591
|
const elements = this.#getReadingElements();
|
|
206
592
|
if (!elements.length) {
|
|
207
|
-
console.warn('test-item-to-speech: no readable elements found
|
|
593
|
+
console.warn('test-item-to-speech: no readable elements found');
|
|
208
594
|
return;
|
|
209
595
|
}
|
|
596
|
+
this.#stopPicking();
|
|
597
|
+
// speechSynthesis is one global queue: another player mid-sentence would otherwise
|
|
598
|
+
// finish first and this one would silently wait its turn.
|
|
599
|
+
speechSynthesis.cancel();
|
|
600
|
+
if (this.#finished) {
|
|
601
|
+
this.#finished = false;
|
|
602
|
+
this.#currentElementIndex = 0;
|
|
603
|
+
}
|
|
604
|
+
// Content mode: starting from the top rebuilds the list, so elements the page added since
|
|
605
|
+
// the last read (a framework re-render) are included.
|
|
606
|
+
if (this.#contentMode && this.#currentElementIndex === 0)
|
|
607
|
+
this.#readingElements = null;
|
|
210
608
|
this.#speakElement(this.#currentElementIndex);
|
|
211
609
|
}
|
|
212
610
|
/** Speak the element at the given index, then auto-advance when it ends. */
|
|
213
611
|
#speakElement(index) {
|
|
214
|
-
const elements = this.#
|
|
612
|
+
const elements = this.#getReadingElements();
|
|
215
613
|
if (index >= elements.length) {
|
|
216
614
|
// Finished all elements — stay on the last element so the user can walk back with prev
|
|
217
615
|
this.#clearWordHighlight();
|
|
218
616
|
this.#currentElementIndex = elements.length - 1;
|
|
219
617
|
if (elements.length > 0)
|
|
220
618
|
this.#highlightElement(elements[this.#currentElementIndex]);
|
|
619
|
+
this.#finished = true;
|
|
221
620
|
this.#setSpeechState('idle');
|
|
222
621
|
this.#updateContext();
|
|
223
622
|
return;
|
|
224
623
|
}
|
|
225
624
|
this.#currentElementIndex = index;
|
|
625
|
+
this.#finished = false;
|
|
226
626
|
this.#updateContext();
|
|
227
627
|
const element = elements[index];
|
|
228
628
|
element.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
@@ -236,7 +636,7 @@ export class TestItemToSpeech extends LitElement {
|
|
|
236
636
|
return;
|
|
237
637
|
}
|
|
238
638
|
const utterance = new SpeechSynthesisUtterance(text);
|
|
239
|
-
utterance.lang = this
|
|
639
|
+
utterance.lang = this.#resolveLang(element);
|
|
240
640
|
utterance.rate = 1;
|
|
241
641
|
utterance.onboundary = (event) => {
|
|
242
642
|
if (event.name !== 'word')
|
|
@@ -244,9 +644,22 @@ export class TestItemToSpeech extends LitElement {
|
|
|
244
644
|
this.#updateWordHighlight(event.charIndex, event.charLength ?? 0);
|
|
245
645
|
};
|
|
246
646
|
utterance.onend = () => {
|
|
647
|
+
if (this.#currentUtterance !== utterance)
|
|
648
|
+
return;
|
|
649
|
+
this.#currentUtterance = null;
|
|
247
650
|
this.#clearWordHighlight();
|
|
248
651
|
this.#speakElement(this.#currentElementIndex + 1);
|
|
249
652
|
};
|
|
653
|
+
// Fires when speech is cancelled from outside this player (another player starting,
|
|
654
|
+
// or the browser); a cancel we issued ourselves has already cleared #currentUtterance.
|
|
655
|
+
utterance.onerror = () => {
|
|
656
|
+
if (this.#currentUtterance !== utterance)
|
|
657
|
+
return;
|
|
658
|
+
this.#currentUtterance = null;
|
|
659
|
+
this.#clearAllHighlights();
|
|
660
|
+
this.#setSpeechState('idle');
|
|
661
|
+
};
|
|
662
|
+
this.#currentUtterance = utterance;
|
|
250
663
|
speechSynthesis.speak(utterance);
|
|
251
664
|
this.#setSpeechState('playing');
|
|
252
665
|
}
|
|
@@ -259,17 +672,76 @@ export class TestItemToSpeech extends LitElement {
|
|
|
259
672
|
this.#setSpeechState('playing');
|
|
260
673
|
}
|
|
261
674
|
#stop() {
|
|
675
|
+
this.#currentUtterance = null;
|
|
262
676
|
speechSynthesis.cancel();
|
|
263
677
|
this.#clearAllHighlights();
|
|
264
678
|
this.#setSpeechState('idle');
|
|
265
679
|
}
|
|
680
|
+
// ─── Pick mode ────────────────────────────────────────────────────────────
|
|
681
|
+
#togglePick() {
|
|
682
|
+
if (this.#picking)
|
|
683
|
+
this.#stopPicking();
|
|
684
|
+
else
|
|
685
|
+
this.#startPicking();
|
|
686
|
+
}
|
|
687
|
+
/** Highlight every reading element and wait for a click on one of them. */
|
|
688
|
+
#startPicking() {
|
|
689
|
+
// Content mode: pick from the page as it is now, like starting from the top does.
|
|
690
|
+
if (this.#contentMode)
|
|
691
|
+
this.#readingElements = null;
|
|
692
|
+
const elements = this.#getReadingElements();
|
|
693
|
+
if (!elements.length) {
|
|
694
|
+
console.warn('test-item-to-speech: no readable elements found');
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
this.#stop();
|
|
698
|
+
this.#picking = true;
|
|
699
|
+
this.#internals.states.add('picking');
|
|
700
|
+
this.#highlightElements(elements);
|
|
701
|
+
// Listen on the item's root so the click is seen before any interaction handles it — in
|
|
702
|
+
// pick mode a click on a choice picks the sentence, it does not answer the question.
|
|
703
|
+
this.#pickRoot = elements[0].getRootNode();
|
|
704
|
+
this.#pickRoot.addEventListener('click', this.#boundHandlePickClick, true);
|
|
705
|
+
this._ttsContext = { ...this._ttsContext, picking: true };
|
|
706
|
+
}
|
|
707
|
+
#stopPicking() {
|
|
708
|
+
if (!this.#picking)
|
|
709
|
+
return;
|
|
710
|
+
this.#picking = false;
|
|
711
|
+
this.#internals.states.delete('picking');
|
|
712
|
+
this.#pickRoot?.removeEventListener('click', this.#boundHandlePickClick, true);
|
|
713
|
+
this.#pickRoot = null;
|
|
714
|
+
this.#clearAllHighlights();
|
|
715
|
+
this._ttsContext = { ...this._ttsContext, picking: false };
|
|
716
|
+
}
|
|
717
|
+
#handlePickClick(event) {
|
|
718
|
+
const elements = this.#getReadingElements();
|
|
719
|
+
const path = event.composedPath();
|
|
720
|
+
let index = elements.findIndex(el => path.includes(el));
|
|
721
|
+
if (index < 0) {
|
|
722
|
+
// Not on the text itself. A click on the choice or list item around it (its radio button,
|
|
723
|
+
// the padding) still means "this one": take the first reading element inside it.
|
|
724
|
+
const container = path.find((node) => node instanceof Element && node.matches('qti-simple-choice, li, qti-prompt'));
|
|
725
|
+
if (container)
|
|
726
|
+
index = elements.findIndex(el => container.contains(el));
|
|
727
|
+
}
|
|
728
|
+
if (index < 0)
|
|
729
|
+
return; // not on a highlighted element — stay in pick mode
|
|
730
|
+
event.preventDefault();
|
|
731
|
+
event.stopPropagation();
|
|
732
|
+
this.#stopPicking();
|
|
733
|
+
speechSynthesis.cancel();
|
|
734
|
+
this.#speakElement(index);
|
|
735
|
+
}
|
|
266
736
|
#prevElement() {
|
|
267
737
|
const elements = this.#getReadingElements();
|
|
268
738
|
if (!elements.length)
|
|
269
739
|
return;
|
|
740
|
+
this.#currentUtterance = null;
|
|
270
741
|
speechSynthesis.cancel();
|
|
271
742
|
this.#clearWordHighlight();
|
|
272
743
|
this.#currentElementIndex = Math.max(0, this.#currentElementIndex - 1);
|
|
744
|
+
this.#finished = false;
|
|
273
745
|
this.#updateContext();
|
|
274
746
|
const el = elements[this.#currentElementIndex];
|
|
275
747
|
el.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
@@ -280,9 +752,11 @@ export class TestItemToSpeech extends LitElement {
|
|
|
280
752
|
const elements = this.#getReadingElements();
|
|
281
753
|
if (!elements.length)
|
|
282
754
|
return;
|
|
755
|
+
this.#currentUtterance = null;
|
|
283
756
|
speechSynthesis.cancel();
|
|
284
757
|
this.#clearWordHighlight();
|
|
285
758
|
this.#currentElementIndex = Math.min(elements.length - 1, this.#currentElementIndex + 1);
|
|
759
|
+
this.#finished = false;
|
|
286
760
|
this.#updateContext();
|
|
287
761
|
const el = elements[this.#currentElementIndex];
|
|
288
762
|
el.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
|
@@ -295,7 +769,9 @@ export class TestItemToSpeech extends LitElement {
|
|
|
295
769
|
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
296
770
|
let node;
|
|
297
771
|
while ((node = walker.nextNode())) {
|
|
298
|
-
|
|
772
|
+
// Text hidden from assistive technology inside a read element — a decorative icon in a
|
|
773
|
+
// list item — is not spoken either. Skipped here, so speech and word highlight agree.
|
|
774
|
+
if ((node.textContent ?? '').length > 0 && !node.parentElement?.closest('[aria-hidden="true"]')) {
|
|
299
775
|
nodes.push(node);
|
|
300
776
|
}
|
|
301
777
|
}
|
|
@@ -303,13 +779,19 @@ export class TestItemToSpeech extends LitElement {
|
|
|
303
779
|
}
|
|
304
780
|
/** Highlight the whole element as the navigation cursor (blue). */
|
|
305
781
|
#highlightElement(element) {
|
|
782
|
+
this.#highlightElements([element]);
|
|
783
|
+
}
|
|
784
|
+
/** Put the blue cursor highlight on several elements at once (pick mode). */
|
|
785
|
+
#highlightElements(elements) {
|
|
306
786
|
if (!('highlights' in CSS))
|
|
307
787
|
return;
|
|
308
788
|
this.#ensureHighlightStyles();
|
|
309
789
|
this.#elementHighlight.clear();
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
790
|
+
for (const element of elements) {
|
|
791
|
+
const range = new Range();
|
|
792
|
+
range.selectNodeContents(element);
|
|
793
|
+
this.#elementHighlight.add(range);
|
|
794
|
+
}
|
|
313
795
|
CSS.highlights.set(HIGHLIGHT_ELEMENT, this.#elementHighlight);
|
|
314
796
|
}
|
|
315
797
|
/** Highlight the current word within #textNodes (yellow, during speech). */
|
|
@@ -350,15 +832,17 @@ export class TestItemToSpeech extends LitElement {
|
|
|
350
832
|
if (!('highlights' in CSS))
|
|
351
833
|
return;
|
|
352
834
|
this.#wordHighlight.clear();
|
|
353
|
-
|
|
835
|
+
// The registry is shared by every player on the page: only drop the entry if it is ours.
|
|
836
|
+
if (CSS.highlights.get(HIGHLIGHT_WORD) === this.#wordHighlight)
|
|
837
|
+
CSS.highlights.delete(HIGHLIGHT_WORD);
|
|
354
838
|
}
|
|
355
839
|
#clearAllHighlights() {
|
|
356
840
|
if (!('highlights' in CSS))
|
|
357
841
|
return;
|
|
358
|
-
this.#
|
|
359
|
-
CSS.highlights.delete(HIGHLIGHT_WORD);
|
|
842
|
+
this.#clearWordHighlight();
|
|
360
843
|
this.#elementHighlight.clear();
|
|
361
|
-
CSS.highlights.
|
|
844
|
+
if (CSS.highlights.get(HIGHLIGHT_ELEMENT) === this.#elementHighlight)
|
|
845
|
+
CSS.highlights.delete(HIGHLIGHT_ELEMENT);
|
|
362
846
|
}
|
|
363
847
|
/** Fallback word-length when charLength is 0. */
|
|
364
848
|
#wordLengthAt(charIndex, text) {
|
|
@@ -379,12 +863,23 @@ export class TestItemToSpeech extends LitElement {
|
|
|
379
863
|
if (!document.adoptedStyleSheets.includes(sheet)) {
|
|
380
864
|
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
|
|
381
865
|
}
|
|
382
|
-
// 2.
|
|
866
|
+
// 2. Shadow roots the highlighted text may live in: test-container's, and the player's own
|
|
867
|
+
// root when the player itself sits inside a shadow tree (e.g. built into an item card).
|
|
868
|
+
const roots = new Set();
|
|
383
869
|
const testContainer = this.closest('test-navigation')?.querySelector('test-container') ??
|
|
384
|
-
this.
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
870
|
+
this.testHost?.querySelector('test-container');
|
|
871
|
+
if (testContainer?.shadowRoot)
|
|
872
|
+
roots.add(testContainer.shadowRoot);
|
|
873
|
+
const ownRoot = this.getRootNode();
|
|
874
|
+
if (ownRoot instanceof ShadowRoot)
|
|
875
|
+
roots.add(ownRoot);
|
|
876
|
+
const itemRoot = this.#readingElements?.[0]?.getRootNode();
|
|
877
|
+
if (itemRoot instanceof ShadowRoot)
|
|
878
|
+
roots.add(itemRoot);
|
|
879
|
+
for (const root of roots) {
|
|
880
|
+
if (!root.adoptedStyleSheets.includes(sheet)) {
|
|
881
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
882
|
+
}
|
|
388
883
|
}
|
|
389
884
|
}
|
|
390
885
|
}
|
|
@@ -394,6 +889,9 @@ __decorate([
|
|
|
394
889
|
__decorate([
|
|
395
890
|
consume({ context: sessionContext, subscribe: true })
|
|
396
891
|
], TestItemToSpeech.prototype, "_sessionContext", void 0);
|
|
892
|
+
__decorate([
|
|
893
|
+
consume({ context: testHostContext, subscribe: true })
|
|
894
|
+
], TestItemToSpeech.prototype, "testHost", void 0);
|
|
397
895
|
__decorate([
|
|
398
896
|
state(),
|
|
399
897
|
provide({ context: ttsContext })
|
|
@@ -401,13 +899,14 @@ __decorate([
|
|
|
401
899
|
// ─── Child control elements ───────────────────────────────────────────────────
|
|
402
900
|
/**
|
|
403
901
|
* Play/pause/resume toggle button — always enabled, cycles through states.
|
|
404
|
-
* Use named slots `play` and `pause` to customise the icons/labels independently
|
|
902
|
+
* Use named slots `play` and `pause` to customise the icons/labels independently; `label` and
|
|
903
|
+
* `pause-label` name the button in each state (set them when the slotted content is an icon).
|
|
405
904
|
*
|
|
406
905
|
* @example
|
|
407
906
|
* ```html
|
|
408
907
|
* <test-tts-play>
|
|
409
|
-
* <span slot="play"
|
|
410
|
-
* <span slot="pause"
|
|
908
|
+
* <span slot="play">Voorlezen</span>
|
|
909
|
+
* <span slot="pause">Pauzeren</span>
|
|
411
910
|
* </test-tts-play>
|
|
412
911
|
* ```
|
|
413
912
|
*
|
|
@@ -428,12 +927,17 @@ export class TestTtsPlay extends TtsButtonBase {
|
|
|
428
927
|
render() {
|
|
429
928
|
const playing = this._tts?.state === 'playing';
|
|
430
929
|
return html `
|
|
431
|
-
<button part="button" @click=${this.#toggle}>
|
|
432
|
-
${playing
|
|
930
|
+
<button part="button" aria-label=${ifDefined(playing ? this.pauseLabel : this.label)} @click=${this.#toggle}>
|
|
931
|
+
${playing
|
|
932
|
+
? html `<slot name="pause">${iconContent('pause', this.pauseLabel ?? 'Pause')}</slot>`
|
|
933
|
+
: html `<slot name="play">${iconContent('play', this.label ?? 'Play')}</slot>`}
|
|
433
934
|
</button>
|
|
434
935
|
`;
|
|
435
936
|
}
|
|
436
937
|
}
|
|
938
|
+
__decorate([
|
|
939
|
+
property({ type: String, attribute: 'pause-label' })
|
|
940
|
+
], TestTtsPlay.prototype, "pauseLabel", void 0);
|
|
437
941
|
/**
|
|
438
942
|
* Pause button — enabled only when playing.
|
|
439
943
|
* @cssstate idle / playing / paused
|
|
@@ -442,8 +946,13 @@ export class TestTtsPlay extends TtsButtonBase {
|
|
|
442
946
|
export class TestTtsPause extends TtsButtonBase {
|
|
443
947
|
render() {
|
|
444
948
|
return html `
|
|
445
|
-
<button
|
|
446
|
-
|
|
949
|
+
<button
|
|
950
|
+
part="button"
|
|
951
|
+
aria-label=${ifDefined(this.label)}
|
|
952
|
+
?disabled=${this._tts?.state !== 'playing'}
|
|
953
|
+
@click=${() => this._tts?.pause()}
|
|
954
|
+
>
|
|
955
|
+
<slot>${iconContent('pause', this.label ?? 'Pause')}</slot>
|
|
447
956
|
</button>
|
|
448
957
|
`;
|
|
449
958
|
}
|
|
@@ -456,56 +965,96 @@ export class TestTtsPause extends TtsButtonBase {
|
|
|
456
965
|
export class TestTtsResume extends TtsButtonBase {
|
|
457
966
|
render() {
|
|
458
967
|
return html `
|
|
459
|
-
<button
|
|
460
|
-
|
|
968
|
+
<button
|
|
969
|
+
part="button"
|
|
970
|
+
aria-label=${ifDefined(this.label)}
|
|
971
|
+
?disabled=${this._tts?.state !== 'paused'}
|
|
972
|
+
@click=${() => this._tts?.resume()}
|
|
973
|
+
>
|
|
974
|
+
<slot>${iconContent('play', this.label ?? 'Resume')}</slot>
|
|
461
975
|
</button>
|
|
462
976
|
`;
|
|
463
977
|
}
|
|
464
978
|
}
|
|
465
979
|
/**
|
|
466
|
-
* Stop button — enabled when playing or
|
|
980
|
+
* Stop button — enabled when playing, paused, or in pick mode (where it leaves pick mode).
|
|
467
981
|
* @cssstate idle / playing / paused
|
|
468
982
|
* @csspart button
|
|
469
983
|
*/
|
|
470
984
|
export class TestTtsStop extends TtsButtonBase {
|
|
471
985
|
render() {
|
|
986
|
+
const idle = this._tts?.state === 'idle' && !this._tts?.picking;
|
|
472
987
|
return html `
|
|
473
|
-
<button part="button"
|
|
474
|
-
<slot
|
|
988
|
+
<button part="button" aria-label=${ifDefined(this.label)} ?disabled=${idle} @click=${() => this._tts?.stop()}>
|
|
989
|
+
<slot>${iconContent('stop', this.label ?? 'Stop')}</slot>
|
|
475
990
|
</button>
|
|
476
991
|
`;
|
|
477
992
|
}
|
|
478
993
|
}
|
|
479
994
|
/**
|
|
480
995
|
* Prev-element button — moves cursor to the previous reading element and pauses.
|
|
481
|
-
* Disabled at the first element
|
|
996
|
+
* Disabled at the first element. Reading elements are collected lazily, so an element count
|
|
997
|
+
* of 0 means "not looked yet" rather than "nothing to read": the button stays enabled and the
|
|
998
|
+
* first click collects them.
|
|
482
999
|
* @cssstate idle / playing / paused
|
|
483
1000
|
* @csspart button
|
|
484
1001
|
*/
|
|
485
1002
|
export class TestTtsPrev extends TtsButtonBase {
|
|
486
1003
|
render() {
|
|
1004
|
+
const collected = (this._tts?.elementCount ?? 0) > 0;
|
|
487
1005
|
const atStart = (this._tts?.currentElementIndex ?? 0) === 0;
|
|
488
|
-
const noElements = (this._tts?.elementCount ?? 0) === 0;
|
|
489
1006
|
return html `
|
|
490
|
-
<button
|
|
491
|
-
|
|
1007
|
+
<button
|
|
1008
|
+
part="button"
|
|
1009
|
+
aria-label=${ifDefined(this.label)}
|
|
1010
|
+
?disabled=${collected && atStart}
|
|
1011
|
+
@click=${() => this._tts?.prevElement()}
|
|
1012
|
+
>
|
|
1013
|
+
<slot>${iconContent('prev', this.label ?? 'Previous sentence')}</slot>
|
|
492
1014
|
</button>
|
|
493
1015
|
`;
|
|
494
1016
|
}
|
|
495
1017
|
}
|
|
496
1018
|
/**
|
|
497
1019
|
* Next-element button — moves cursor to the next reading element and pauses.
|
|
498
|
-
* Disabled at the last element
|
|
1020
|
+
* Disabled at the last element; enabled while the elements have not been collected yet
|
|
1021
|
+
* (see `TestTtsPrev`).
|
|
499
1022
|
* @cssstate idle / playing / paused
|
|
500
1023
|
* @csspart button
|
|
501
1024
|
*/
|
|
502
1025
|
export class TestTtsNext extends TtsButtonBase {
|
|
503
1026
|
render() {
|
|
504
|
-
const
|
|
505
|
-
const
|
|
1027
|
+
const count = this._tts?.elementCount ?? 0;
|
|
1028
|
+
const atEnd = count > 0 && (this._tts?.currentElementIndex ?? 0) >= count - 1;
|
|
1029
|
+
return html `
|
|
1030
|
+
<button
|
|
1031
|
+
part="button"
|
|
1032
|
+
aria-label=${ifDefined(this.label)}
|
|
1033
|
+
?disabled=${atEnd}
|
|
1034
|
+
@click=${() => this._tts?.nextElement()}
|
|
1035
|
+
>
|
|
1036
|
+
<slot>${iconContent('next', this.label ?? 'Next sentence')}</slot>
|
|
1037
|
+
</button>
|
|
1038
|
+
`;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Pick button — toggles pick mode on the controller: every reading element is highlighted and
|
|
1043
|
+
* a click on one of them starts reading from there. Reflects the mode as `:state(picking)`.
|
|
1044
|
+
* @cssstate idle / playing / paused
|
|
1045
|
+
* @cssstate picking
|
|
1046
|
+
* @csspart button
|
|
1047
|
+
*/
|
|
1048
|
+
export class TestTtsPick extends TtsButtonBase {
|
|
1049
|
+
render() {
|
|
506
1050
|
return html `
|
|
507
|
-
<button
|
|
508
|
-
|
|
1051
|
+
<button
|
|
1052
|
+
part="button"
|
|
1053
|
+
aria-label=${ifDefined(this.label)}
|
|
1054
|
+
aria-pressed=${this._tts?.picking ? 'true' : 'false'}
|
|
1055
|
+
@click=${() => this._tts?.togglePick()}
|
|
1056
|
+
>
|
|
1057
|
+
<slot>${iconContent('pick', this.label ?? 'Read from here')}</slot>
|
|
509
1058
|
</button>
|
|
510
1059
|
`;
|
|
511
1060
|
}
|