@uvalib/web-components 1.11.1 → 1.12.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/CHANGELOG.md +21 -0
- package/README.md +192 -54
- package/dist/base.css +56 -2
- package/dist/components/uvalib-audio/UvalibAudio.d.ts +186 -0
- package/dist/components/uvalib-audio/UvalibAudio.js +1154 -0
- package/dist/components/uvalib-audio/UvalibAudio.js.map +1 -0
- package/dist/components/uvalib-audio/uvalib-audio.d.ts +1 -0
- package/dist/components/uvalib-audio/uvalib-audio.js +4 -0
- package/dist/components/uvalib-audio/uvalib-audio.js.map +1 -0
- package/dist/components/uvalib-pagefind-search/UvalibPagefindSearch.d.ts +25 -5
- package/dist/components/uvalib-pagefind-search/UvalibPagefindSearch.js +62 -9
- package/dist/components/uvalib-pagefind-search/UvalibPagefindSearch.js.map +1 -1
- package/dist/custom-elements.json +8486 -7187
- package/dist/edition-epub.css +511 -0
- package/dist/edition-print.css +51 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/styles/base.js +55 -1
- package/dist/styles/base.js.map +1 -1
- package/dist/styles/edition.d.ts +6 -0
- package/dist/styles/edition.js +20 -0
- package/dist/styles/edition.js.map +1 -1
- package/dist-cdn/base.css +56 -2
- package/dist-cdn/chunks/{base-DtkA4Pgg.js → base-D_oPO5cX.js} +56 -2
- package/dist-cdn/chunks/{base-DtkA4Pgg.js.map → base-D_oPO5cX.js.map} +1 -1
- package/dist-cdn/custom-elements.json +8486 -7187
- package/dist-cdn/edition-epub.css +511 -0
- package/dist-cdn/edition-print.css +51 -0
- package/dist-cdn/manifest.json +6 -3
- package/dist-cdn/site-page.js +1 -1
- package/dist-cdn/site-style.js +1 -1
- package/dist-cdn/skills/uvalib-web-components/SKILL.md +35 -4
- package/dist-cdn/skills/uvalib-web-components/reference/components.md +53 -1
- package/dist-cdn/uvalib-audio.js +747 -0
- package/dist-cdn/uvalib-audio.js.map +1 -0
- package/dist-cdn/uvalib-pagefind-search.js +70 -44
- package/dist-cdn/uvalib-pagefind-search.js.map +1 -1
- package/package.json +7 -1
- package/skills/uvalib-web-components/SKILL.md +35 -4
- package/skills/uvalib-web-components/reference/components.md +53 -1
|
@@ -0,0 +1,1154 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { css, html, nothing, svg, unsafeCSS } from 'lit';
|
|
8
|
+
import { property, state } from 'lit/decorators.js';
|
|
9
|
+
import { trackError, trackEvent } from '../../base/analytics.js';
|
|
10
|
+
import { UvalibElement } from '../../base/UvalibElement.js';
|
|
11
|
+
import { installLightDomStyles } from '../../base/light-dom-styles.js';
|
|
12
|
+
import { inlineCopy } from '../../base/inline-copy.js';
|
|
13
|
+
import { normalize, safeHref, SourceChildren } from '../../base/source-children.js';
|
|
14
|
+
import { accessible } from '../../styles/accessible.js';
|
|
15
|
+
const TAG = 'uvalib-audio';
|
|
16
|
+
let uid = 0;
|
|
17
|
+
/** The playback speeds offered, slowest first. */
|
|
18
|
+
export const SPEEDS = [0.75, 1, 1.25, 1.5, 1.75, 2];
|
|
19
|
+
/** Back and Forward, in seconds: the podcast convention (a missed phrase back, a skip ahead). */
|
|
20
|
+
export const SKIP = { back: 15, forward: 30 };
|
|
21
|
+
/**
|
|
22
|
+
* How long (ms) to wait before announcing (a burst of skips is announced once, where it
|
|
23
|
+
* ends), saving the position while playing, and reporting a speed change to analytics.
|
|
24
|
+
* Tests may shorten them.
|
|
25
|
+
*/
|
|
26
|
+
export const audioTiming = { announce: 600, save: 5000, speed: 1000 };
|
|
27
|
+
/** How long an announcement stays in the live region before it is cleared. */
|
|
28
|
+
const CLEAR_MS = 5000;
|
|
29
|
+
/** `12:31`, `1:02:03` or `751` (seconds) to seconds; undefined when unreadable. */
|
|
30
|
+
export function parseDuration(text) {
|
|
31
|
+
const t = normalize(text);
|
|
32
|
+
if (!t)
|
|
33
|
+
return undefined;
|
|
34
|
+
if (/^\d+(\.\d+)?$/.test(t))
|
|
35
|
+
return Number(t);
|
|
36
|
+
if (!/^\d+(:\d{1,2}){1,2}$/.test(t))
|
|
37
|
+
return undefined;
|
|
38
|
+
return t.split(':').reduce((sum, part) => sum * 60 + Number(part), 0);
|
|
39
|
+
}
|
|
40
|
+
/** 754 → `12:34`, 3723 → `1:02:03`. */
|
|
41
|
+
export function clock(seconds) {
|
|
42
|
+
const s = Math.max(0, Math.floor(seconds || 0));
|
|
43
|
+
const h = Math.floor(s / 3600);
|
|
44
|
+
const m = Math.floor((s % 3600) / 60);
|
|
45
|
+
const ss = String(s % 60).padStart(2, '0');
|
|
46
|
+
return h ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`;
|
|
47
|
+
}
|
|
48
|
+
const unit = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
|
|
49
|
+
/** 750 → `12 minutes 30 seconds`, 3600 → `1 hour`, 0 → `0 seconds`. */
|
|
50
|
+
export function spoken(seconds) {
|
|
51
|
+
const s = Math.max(0, Math.floor(seconds || 0));
|
|
52
|
+
const parts = [];
|
|
53
|
+
const h = Math.floor(s / 3600);
|
|
54
|
+
const m = Math.floor((s % 3600) / 60);
|
|
55
|
+
if (h)
|
|
56
|
+
parts.push(unit(h, 'hour'));
|
|
57
|
+
if (m)
|
|
58
|
+
parts.push(unit(m, 'minute'));
|
|
59
|
+
if (s % 60 || !parts.length)
|
|
60
|
+
parts.push(unit(s % 60, 'second'));
|
|
61
|
+
return parts.join(' ');
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A size as written (`312 MB`), or a number of bytes made readable in decimal units, as
|
|
65
|
+
* operating systems show file sizes: `312000000` → `312 MB`, `1500000` → `1.5 MB`.
|
|
66
|
+
*/
|
|
67
|
+
export function formatSize(value) {
|
|
68
|
+
const t = normalize(value);
|
|
69
|
+
if (!/^\d+$/.test(t))
|
|
70
|
+
return t;
|
|
71
|
+
const n = Number(t);
|
|
72
|
+
const units = ['bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
73
|
+
let i = 0;
|
|
74
|
+
let v = n;
|
|
75
|
+
while (v >= 1000 && i < units.length - 1) {
|
|
76
|
+
v /= 1000;
|
|
77
|
+
i++;
|
|
78
|
+
}
|
|
79
|
+
if (i === 0)
|
|
80
|
+
return `${n} ${n === 1 ? 'byte' : 'bytes'}`;
|
|
81
|
+
const shown = v < 10 ? Math.round(v * 10) / 10 : Math.round(v);
|
|
82
|
+
return `${shown.toLocaleString('en-US')} ${units[i]}`;
|
|
83
|
+
}
|
|
84
|
+
/** Media files may also be blob: URLs (made in script) and data:audio URLs. */
|
|
85
|
+
const mediaHref = (href, base) => {
|
|
86
|
+
if (safeHref(href, base))
|
|
87
|
+
return true;
|
|
88
|
+
try {
|
|
89
|
+
const u = new URL(href, base);
|
|
90
|
+
return u.protocol === 'blob:' || (u.protocol === 'data:' && /^data:audio\//i.test(href));
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const KEPT_LINK_ATTRS = ['hreflang', 'type', 'target', 'rel', 'download', 'lang'];
|
|
97
|
+
/** Every instance on the page: starting one pauses the others. */
|
|
98
|
+
const players = new Set();
|
|
99
|
+
const PLAY = svg `<svg class="icon" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M4 2.5v11l9.5-5.5z" fill="currentColor"/></svg>`;
|
|
100
|
+
const PAUSE = svg `<svg class="icon" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M3.5 2.5h3v11h-3zM9.5 2.5h3v11h-3z" fill="currentColor"/></svg>`;
|
|
101
|
+
const BACK = svg `<svg class="icon" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M3 8a5 5 0 1 0 1.6-3.7" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/><path d="M2.2 1.8v3.6h3.6" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
|
102
|
+
const FORWARD = svg `<svg class="icon" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M13 8a5 5 0 1 1-1.6-3.7" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/><path d="M13.8 1.8v3.6h-3.6" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
|
|
103
|
+
/**
|
|
104
|
+
* Kept with `no-style`: the player's rows and 44px controls, the chapter rows' 44px buttons,
|
|
105
|
+
* the visually hidden text, and `[hidden]`. Shared with base.css's pre-upgrade hold: keep the
|
|
106
|
+
* heights in step with `audioHold` in src/styles/base.ts. `host` is `:host` or the tag.
|
|
107
|
+
*/
|
|
108
|
+
const structuralRules = (host) => `
|
|
109
|
+
${host} { display: block; container-type: inline-size; font-size: 1rem; line-height: 1.5; }
|
|
110
|
+
${host} .player {
|
|
111
|
+
box-sizing: border-box;
|
|
112
|
+
display: grid;
|
|
113
|
+
gap: 0.5rem;
|
|
114
|
+
margin: 0 0 1rem;
|
|
115
|
+
padding: 0.75rem;
|
|
116
|
+
border: 1px solid transparent;
|
|
117
|
+
}
|
|
118
|
+
${host} .now { display: flex; align-items: center; min-block-size: 44px; margin: 0; overflow-wrap: anywhere; }
|
|
119
|
+
${host} .controls {
|
|
120
|
+
display: grid;
|
|
121
|
+
grid-template-columns: auto minmax(6rem, 1fr) auto;
|
|
122
|
+
grid-template-areas: 'buttons seek speed';
|
|
123
|
+
gap: 0.5rem 0.75rem;
|
|
124
|
+
align-items: center;
|
|
125
|
+
}
|
|
126
|
+
${host} .buttons { grid-area: buttons; display: flex; gap: 0.5rem; }
|
|
127
|
+
${host} .seek { grid-area: seek; display: flex; align-items: center; gap: 0.5rem; min-inline-size: 0; block-size: 44px; }
|
|
128
|
+
${host} .seek input { flex: 1; min-inline-size: 0; block-size: 44px; margin: 0; }
|
|
129
|
+
${host} .time { white-space: nowrap; font-variant-numeric: tabular-nums; }
|
|
130
|
+
${host} .speed { grid-area: speed; display: flex; align-items: center; gap: 0.5rem; block-size: 44px; }
|
|
131
|
+
${host} .speed select { box-sizing: border-box; min-block-size: 44px; font: inherit; }
|
|
132
|
+
${host} button, ${host} .link-button {
|
|
133
|
+
box-sizing: border-box;
|
|
134
|
+
min-block-size: 44px;
|
|
135
|
+
min-inline-size: 44px;
|
|
136
|
+
margin: 0;
|
|
137
|
+
font: inherit;
|
|
138
|
+
line-height: 1.2;
|
|
139
|
+
cursor: pointer;
|
|
140
|
+
}
|
|
141
|
+
${host} .ctl { display: inline-flex; align-items: center; justify-content: center; gap: 0.375rem; padding: 0.25rem 0.5rem; white-space: nowrap; }
|
|
142
|
+
${host} .ctl.play { min-inline-size: 5.5rem; }
|
|
143
|
+
${host} .icon { flex: none; inline-size: 1em; block-size: 1em; }
|
|
144
|
+
${host} .download { margin: 0 0 1rem; }
|
|
145
|
+
/* One inner span keeps the spaces around “·” (a flex container would drop them). */
|
|
146
|
+
${host} .download a, ${host} .chapter a { display: inline-flex; align-items: center; min-block-size: 44px; }
|
|
147
|
+
${host} .chapters { margin: 0; padding: 0; list-style: none; }
|
|
148
|
+
${host} .chapter {
|
|
149
|
+
display: grid;
|
|
150
|
+
grid-template-columns: minmax(0, 1fr) auto;
|
|
151
|
+
align-items: center;
|
|
152
|
+
column-gap: 0.75rem;
|
|
153
|
+
box-sizing: border-box;
|
|
154
|
+
margin: 0;
|
|
155
|
+
padding: 0.25rem 0;
|
|
156
|
+
border-block-end: 1px solid transparent;
|
|
157
|
+
}
|
|
158
|
+
${host} .chapter-play {
|
|
159
|
+
display: flex;
|
|
160
|
+
align-items: center;
|
|
161
|
+
gap: 0.75rem;
|
|
162
|
+
min-inline-size: 0;
|
|
163
|
+
padding: 0.25rem 0.5rem;
|
|
164
|
+
border: 0;
|
|
165
|
+
background: none;
|
|
166
|
+
color: inherit;
|
|
167
|
+
text-align: start;
|
|
168
|
+
}
|
|
169
|
+
${host} .chip { display: inline-flex; flex: none; align-items: center; justify-content: center; gap: 0.375rem; box-sizing: border-box; inline-size: 5rem; min-block-size: 2rem; }
|
|
170
|
+
${host} .chapter-title { min-inline-size: 0; overflow-wrap: anywhere; }
|
|
171
|
+
${host} .duration { white-space: nowrap; font-variant-numeric: tabular-nums; }
|
|
172
|
+
${host} .more { grid-column: 1 / -1; display: flex; flex-wrap: wrap; column-gap: 1rem; padding-inline-start: calc(0.5rem + 5rem + 0.75rem); }
|
|
173
|
+
${host} .error { grid-column: 1 / -1; margin: 0; padding-inline: 0.5rem; }
|
|
174
|
+
${host} .visually-hidden {
|
|
175
|
+
position: absolute;
|
|
176
|
+
width: 1px;
|
|
177
|
+
height: 1px;
|
|
178
|
+
margin: -1px;
|
|
179
|
+
padding: 0;
|
|
180
|
+
overflow: hidden;
|
|
181
|
+
clip: rect(0, 0, 0, 0);
|
|
182
|
+
white-space: nowrap;
|
|
183
|
+
border: 0;
|
|
184
|
+
}
|
|
185
|
+
${host}[hidden], ${host} [hidden] { display: none !important; }
|
|
186
|
+
@container (width < 44rem) {
|
|
187
|
+
${host} .controls {
|
|
188
|
+
grid-template-columns: auto auto;
|
|
189
|
+
grid-template-areas: 'seek seek' 'buttons speed';
|
|
190
|
+
justify-content: space-between;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
@container (width < 26rem) {
|
|
194
|
+
${host} .controls { grid-template-columns: auto; grid-template-areas: 'seek' 'buttons' 'speed'; justify-content: stretch; }
|
|
195
|
+
${host} .more { padding-inline-start: 0.5rem; }
|
|
196
|
+
}
|
|
197
|
+
`;
|
|
198
|
+
const shadowStructure = unsafeCSS(structuralRules(':host'));
|
|
199
|
+
const TEXT = unsafeCSS('var(--uva-color-text, var(--uva-grey-darkest, #2b2b2b))');
|
|
200
|
+
const MUTED = unsafeCSS('var(--uva-color-text-muted, #4b5563)');
|
|
201
|
+
const LINK = unsafeCSS('var(--uva-color-link, var(--uva-brand-blue, #232d4b))');
|
|
202
|
+
const BUTTON_BG = unsafeCSS('var(--uva-color-button-bg, var(--uva-blue-alt, #007bac))');
|
|
203
|
+
const BUTTON_HOVER = unsafeCSS('var(--uva-color-button-hover-bg, var(--uva-blue-alt-A, #005679))');
|
|
204
|
+
const BUTTON_TEXT = unsafeCSS('var(--uva-color-button-text, #ffffff)');
|
|
205
|
+
const RAISED = unsafeCSS('var(--uva-color-surface-raised, #ffffff)');
|
|
206
|
+
const RULE = unsafeCSS('var(--uva-color-table-border, var(--uva-grey-100, #dadada))');
|
|
207
|
+
const ACCENT = unsafeCSS('var(--uva-color-accent-bg, var(--uva-brand-blue, #232d4b))');
|
|
208
|
+
const ON_ACCENT = unsafeCSS('var(--uva-color-on-accent, #ffffff)');
|
|
209
|
+
const CURRENT = unsafeCSS('var(--uva-color-current, var(--uva-brand-orange, #e57200))');
|
|
210
|
+
const BORDER = unsafeCSS('var(--uva-color-control-border, var(--uva-grey, #808080))');
|
|
211
|
+
/**
|
|
212
|
+
* A long recording played by chapter: an audiobook, an oral history's sessions. One player
|
|
213
|
+
* for the whole list, so starting a chapter stops the one playing.
|
|
214
|
+
*
|
|
215
|
+
* Give the chapters as a list of links to their files, so the page works before (and
|
|
216
|
+
* without) the script: `<ol><li><a href="ch1.mp3" data-duration="12:31">Chapter 1. Down the
|
|
217
|
+
* Rabbit-Hole</a></li>…</ol>`. The first link in each item is the chapter (its text is the
|
|
218
|
+
* title; inline markup such as `<i>` or `lang` is kept); `data-duration` (`12:31`, `1:02:03` or
|
|
219
|
+
* seconds) is shown beside it and gives the slider its length before the file loads. Other
|
|
220
|
+
* links in the item (the chapter's text, a transcript) are kept as links after it.
|
|
221
|
+
* `data-download` on the `<li>` adds a Download link for that chapter (its own file, or the
|
|
222
|
+
* URL given), with `data-size` (bytes or text) after it.
|
|
223
|
+
*
|
|
224
|
+
* Upgraded, each chapter is a real button (“Play Chapter 1. Down the Rabbit-Hole”, its
|
|
225
|
+
* visible text), with its duration; the current chapter gets `aria-current="true"` and says
|
|
226
|
+
* “Now playing” or “Paused” in text, beside an orange bar. Above the list, a player for the
|
|
227
|
+
* current chapter: what is playing (as text, not a live region), Back 15 s, Play/Pause,
|
|
228
|
+
* Forward 30 s, a position slider (a native range, read as “12 minutes 30 seconds of 41
|
|
229
|
+
* minutes”), the elapsed and total time, and a native Speed select (0.75× to 2×). One
|
|
230
|
+
* `<audio preload="none">` plays everything: nothing loads until the visitor presses Play,
|
|
231
|
+
* and nothing ever plays without a press.
|
|
232
|
+
*
|
|
233
|
+
* `continuous` plays the next chapter when one ends. The position is remembered per page in
|
|
234
|
+
* localStorage (the page's path, plus `storage-key` when a page has more than one
|
|
235
|
+
* recording): on the next visit a “Resume “Chapter 4” at 12:30” button offers to carry on,
|
|
236
|
+
* never playing by itself. Saved on pause, every few seconds while playing, and when the page
|
|
237
|
+
* is left; cleared when the last chapter ends. `no-resume` turns it off. Without storage it
|
|
238
|
+
* simply forgets.
|
|
239
|
+
*
|
|
240
|
+
* Chapter changes, pauses, speed changes and skips are announced politely in one status
|
|
241
|
+
* (a burst of skips once, where it ends); the time is never announced as it runs. A chapter
|
|
242
|
+
* that fails to load says so in text, with a direct link to its file. `download-href` adds a
|
|
243
|
+
* link to the whole recording (an M4B audiobook), with `download-label` and `download-size`
|
|
244
|
+
* (bytes, shown as “312 MB”, or text). Where the browser has the Media Session API, the
|
|
245
|
+
* system's media keys and lock screen show the chapter and can play, pause, seek and change
|
|
246
|
+
* chapter.
|
|
247
|
+
*
|
|
248
|
+
* Before it upgrades, `base.css` holds the player's height above the list and gives each
|
|
249
|
+
* item the height of its upgraded row, so the list does not move.
|
|
250
|
+
*
|
|
251
|
+
* @tagname uvalib-audio
|
|
252
|
+
* @fires uvalib-analytics-event - For `<site-analytics>`, on the visitor's actions only, never titles or URLs: `['uvalib-audio', 'play', 'chapter <N>', N]` the first time each chapter plays in a page view (N is its place in the list), `['uvalib-audio', 'complete', 'chapter <N>', N]` when it plays to its end, `['uvalib-audio', 'speed', '<speed>', speed]` once the visitor settles on a speed, `['uvalib-audio', 'resume', 'chapter <N>', N]`, and `['uvalib-audio', 'download', 'recording' | 'chapter <N>']`. Bubbles and is composed.
|
|
253
|
+
* @csspart player - The player box above the list.
|
|
254
|
+
* @csspart now - The line saying what is playing (or the Resume button).
|
|
255
|
+
* @csspart controls - The player's buttons, slider, time and speed.
|
|
256
|
+
* @csspart download - The whole recording's download line.
|
|
257
|
+
* @csspart list - The list of chapters.
|
|
258
|
+
* @csspart chapter - Each chapter's row.
|
|
259
|
+
* @cssprop --uva-color-text - Dark-mode role token: text (else `--uva-grey-darkest`).
|
|
260
|
+
* @cssprop --uva-color-text-muted - Dark-mode role token: durations (else #4b5563).
|
|
261
|
+
* @cssprop --uva-color-link - Dark-mode role token: links and the outline buttons (else `--uva-brand-blue`).
|
|
262
|
+
* @cssprop --uva-color-button-bg - Dark-mode role token: Play/Pause and the slider (else `--uva-blue-alt`).
|
|
263
|
+
* @cssprop --uva-color-button-text - Dark-mode role token: Play/Pause text (else #fff).
|
|
264
|
+
* @cssprop --uva-color-surface-raised - Dark-mode role token: the player's and the controls' background (else #fff).
|
|
265
|
+
* @cssprop --uva-color-table-border - Dark-mode role token: the player's border and the rules between chapters (else #dadada).
|
|
266
|
+
* @cssprop --uva-color-accent-bg - Dark-mode role token: the “Now playing” marker (else `--uva-brand-blue`).
|
|
267
|
+
* @cssprop --uva-color-on-accent - Dark-mode role token: the marker's text (else #fff).
|
|
268
|
+
* @cssprop --uva-color-current - Dark-mode role token: the current chapter's bar (else `--uva-brand-orange`).
|
|
269
|
+
* @cssprop --uva-color-focus - Dark-mode role token: focus ring (else `--uva-brand-orange`).
|
|
270
|
+
*/
|
|
271
|
+
export class UvalibAudio extends UvalibElement {
|
|
272
|
+
static { this.styles = [
|
|
273
|
+
accessible,
|
|
274
|
+
shadowStructure,
|
|
275
|
+
css `
|
|
276
|
+
:host {
|
|
277
|
+
color: ${TEXT};
|
|
278
|
+
}
|
|
279
|
+
.player {
|
|
280
|
+
border-color: ${RULE};
|
|
281
|
+
border-radius: 5px;
|
|
282
|
+
background: ${RAISED};
|
|
283
|
+
}
|
|
284
|
+
.now .state {
|
|
285
|
+
font-weight: 700;
|
|
286
|
+
}
|
|
287
|
+
.ctl {
|
|
288
|
+
border: 2px solid ${BUTTON_BG};
|
|
289
|
+
border-radius: 5px;
|
|
290
|
+
background: ${RAISED};
|
|
291
|
+
color: ${LINK};
|
|
292
|
+
}
|
|
293
|
+
.ctl.play {
|
|
294
|
+
background: ${BUTTON_BG};
|
|
295
|
+
color: ${BUTTON_TEXT};
|
|
296
|
+
}
|
|
297
|
+
.ctl:hover {
|
|
298
|
+
border-color: ${BUTTON_HOVER};
|
|
299
|
+
text-decoration: underline;
|
|
300
|
+
}
|
|
301
|
+
.ctl.play:hover {
|
|
302
|
+
background: ${BUTTON_HOVER};
|
|
303
|
+
}
|
|
304
|
+
.resume {
|
|
305
|
+
padding: 0.25rem 0.75rem;
|
|
306
|
+
border: 2px solid ${BUTTON_BG};
|
|
307
|
+
border-radius: 5px;
|
|
308
|
+
background: ${BUTTON_BG};
|
|
309
|
+
color: ${BUTTON_TEXT};
|
|
310
|
+
text-align: start;
|
|
311
|
+
}
|
|
312
|
+
.resume:hover {
|
|
313
|
+
background: ${BUTTON_HOVER};
|
|
314
|
+
border-color: ${BUTTON_HOVER};
|
|
315
|
+
}
|
|
316
|
+
.seek input {
|
|
317
|
+
accent-color: ${BUTTON_BG};
|
|
318
|
+
}
|
|
319
|
+
.speed label {
|
|
320
|
+
font-weight: 600;
|
|
321
|
+
}
|
|
322
|
+
.speed select {
|
|
323
|
+
padding: 0.25rem 0.5rem;
|
|
324
|
+
border: 1px solid ${BORDER};
|
|
325
|
+
border-radius: 5px;
|
|
326
|
+
background: ${RAISED};
|
|
327
|
+
color: ${TEXT};
|
|
328
|
+
}
|
|
329
|
+
a {
|
|
330
|
+
color: ${LINK};
|
|
331
|
+
}
|
|
332
|
+
.chapter {
|
|
333
|
+
border-block-end-color: ${RULE};
|
|
334
|
+
padding-inline-start: 4px;
|
|
335
|
+
border-inline-start: 4px solid transparent;
|
|
336
|
+
}
|
|
337
|
+
.chapter[aria-current] {
|
|
338
|
+
border-inline-start-color: ${CURRENT};
|
|
339
|
+
}
|
|
340
|
+
.chapter-play {
|
|
341
|
+
border-radius: 5px;
|
|
342
|
+
}
|
|
343
|
+
.chip {
|
|
344
|
+
padding: 0.125rem 0.5rem;
|
|
345
|
+
border: 2px solid ${BUTTON_BG};
|
|
346
|
+
border-radius: 5px;
|
|
347
|
+
color: ${LINK};
|
|
348
|
+
font-weight: 600;
|
|
349
|
+
}
|
|
350
|
+
.chapter-play:hover .chip {
|
|
351
|
+
border-color: ${BUTTON_HOVER};
|
|
352
|
+
}
|
|
353
|
+
.chapter-play:hover .chapter-title {
|
|
354
|
+
text-decoration: underline;
|
|
355
|
+
}
|
|
356
|
+
.chapter[aria-current] .chip {
|
|
357
|
+
background: ${BUTTON_BG};
|
|
358
|
+
color: ${BUTTON_TEXT};
|
|
359
|
+
}
|
|
360
|
+
.marker {
|
|
361
|
+
display: inline-block;
|
|
362
|
+
margin-inline-start: 0.5rem;
|
|
363
|
+
padding: 0 0.375rem;
|
|
364
|
+
border: 1px solid transparent;
|
|
365
|
+
border-radius: 3px;
|
|
366
|
+
background: ${ACCENT};
|
|
367
|
+
color: ${ON_ACCENT};
|
|
368
|
+
font-size: 0.875rem;
|
|
369
|
+
font-weight: 700;
|
|
370
|
+
white-space: nowrap;
|
|
371
|
+
}
|
|
372
|
+
.duration {
|
|
373
|
+
color: ${MUTED};
|
|
374
|
+
}
|
|
375
|
+
.error {
|
|
376
|
+
font-weight: 600;
|
|
377
|
+
}
|
|
378
|
+
@media (forced-colors: active) {
|
|
379
|
+
.player,
|
|
380
|
+
.ctl,
|
|
381
|
+
.resume,
|
|
382
|
+
.chip {
|
|
383
|
+
border-color: ButtonText;
|
|
384
|
+
}
|
|
385
|
+
.ctl,
|
|
386
|
+
.chip {
|
|
387
|
+
color: ButtonText;
|
|
388
|
+
}
|
|
389
|
+
.chapter[aria-current] {
|
|
390
|
+
border-inline-start-color: Highlight;
|
|
391
|
+
}
|
|
392
|
+
.chapter[aria-current] .chip,
|
|
393
|
+
.marker {
|
|
394
|
+
forced-color-adjust: none;
|
|
395
|
+
border-color: Highlight;
|
|
396
|
+
background: Highlight;
|
|
397
|
+
color: HighlightText;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
@media (prefers-reduced-motion: reduce) {
|
|
401
|
+
* {
|
|
402
|
+
transition: none !important;
|
|
403
|
+
animation: none !important;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
`,
|
|
407
|
+
]; }
|
|
408
|
+
constructor() {
|
|
409
|
+
super();
|
|
410
|
+
/** The player's accessible name, such as the book's title (default “Player”). */
|
|
411
|
+
this.label = '';
|
|
412
|
+
/** Play the next chapter when one ends. */
|
|
413
|
+
this.continuous = false;
|
|
414
|
+
/** Don't remember the position (nothing is read from or written to localStorage). */
|
|
415
|
+
this.noResume = false;
|
|
416
|
+
/** Added to the page's path to name what is remembered, for a page with several recordings. */
|
|
417
|
+
this.storageKey = '';
|
|
418
|
+
/** The whole recording to download (an M4B audiobook with chapters, an MP3). */
|
|
419
|
+
this.downloadHref = '';
|
|
420
|
+
/** The download link's text. */
|
|
421
|
+
this.downloadLabel = 'Download the whole recording';
|
|
422
|
+
/** The download's size: bytes (shown as “312 MB”) or text. */
|
|
423
|
+
this.downloadSize = '';
|
|
424
|
+
this.chapters = [];
|
|
425
|
+
/** The current chapter's index, or -1 before any. */
|
|
426
|
+
this.currentIndex = -1;
|
|
427
|
+
this.isPlaying = false;
|
|
428
|
+
this.finished = false;
|
|
429
|
+
/** Whole seconds into the current chapter. */
|
|
430
|
+
this.time = 0;
|
|
431
|
+
this.speed = 1;
|
|
432
|
+
this.failed = new Set();
|
|
433
|
+
/** Durations read from the files once loaded, by index. */
|
|
434
|
+
this.loaded = new Map();
|
|
435
|
+
this.announcement = '';
|
|
436
|
+
this.uid = `${TAG}-${++uid}`;
|
|
437
|
+
this.sourceKey = '';
|
|
438
|
+
this.restored = false;
|
|
439
|
+
/** The chapter whose file is in the audio element, or -1. */
|
|
440
|
+
this.srcIndex = -1;
|
|
441
|
+
this.played = new Set();
|
|
442
|
+
this.completed = new Set();
|
|
443
|
+
this.lastSave = 0;
|
|
444
|
+
this.reportedSpeed = 1;
|
|
445
|
+
this.warned = new Set();
|
|
446
|
+
/** Save the current chapter and position (nothing before the visitor has played one). */
|
|
447
|
+
this.save = () => {
|
|
448
|
+
const i = this.currentIndex;
|
|
449
|
+
const c = this.chapters[i];
|
|
450
|
+
if (!c || this.finished || this.failed.has(i))
|
|
451
|
+
return;
|
|
452
|
+
const a = this.audio;
|
|
453
|
+
const t = this.srcIndex === i && a ? a.currentTime : (this.pendingTime ?? this.time);
|
|
454
|
+
this.lastSave = Date.now();
|
|
455
|
+
this.write({ src: c.src, chapter: i, time: Math.floor(t || 0), speed: this.speed });
|
|
456
|
+
};
|
|
457
|
+
this.onSeekInput = (e) => {
|
|
458
|
+
this.adoptResume();
|
|
459
|
+
this.resume = undefined;
|
|
460
|
+
this.seek(Number(e.target.value));
|
|
461
|
+
};
|
|
462
|
+
this.onSpeed = (e) => {
|
|
463
|
+
const v = Number(e.target.value);
|
|
464
|
+
if (!SPEEDS.includes(v))
|
|
465
|
+
return;
|
|
466
|
+
this.speed = v;
|
|
467
|
+
const a = this.audio;
|
|
468
|
+
if (a)
|
|
469
|
+
a.defaultPlaybackRate = a.playbackRate = v;
|
|
470
|
+
this.announce(`Speed ${v} times`);
|
|
471
|
+
this.save();
|
|
472
|
+
clearTimeout(this.speedTimer);
|
|
473
|
+
this.speedTimer = setTimeout(() => this.flushSpeed(), audioTiming.speed);
|
|
474
|
+
};
|
|
475
|
+
this.onResume = () => {
|
|
476
|
+
const r = this.resume;
|
|
477
|
+
if (!r)
|
|
478
|
+
return;
|
|
479
|
+
trackEvent(this, [TAG, 'resume', `chapter ${r.index + 1}`, r.index + 1]);
|
|
480
|
+
void this.play(r.index, r.time);
|
|
481
|
+
};
|
|
482
|
+
// ---- the audio element's events
|
|
483
|
+
this.onPlay = () => {
|
|
484
|
+
for (const p of players)
|
|
485
|
+
if (p !== this)
|
|
486
|
+
p.pause();
|
|
487
|
+
const i = this.currentIndex;
|
|
488
|
+
const c = this.chapters[i];
|
|
489
|
+
this.isPlaying = true;
|
|
490
|
+
if (!c)
|
|
491
|
+
return;
|
|
492
|
+
if (!this.played.has(i)) {
|
|
493
|
+
this.played.add(i);
|
|
494
|
+
trackEvent(this, [TAG, 'play', `chapter ${i + 1}`, i + 1]);
|
|
495
|
+
}
|
|
496
|
+
this.announce(`Playing ${c.title}`);
|
|
497
|
+
this.claimMediaSession(c);
|
|
498
|
+
};
|
|
499
|
+
this.onPause = () => {
|
|
500
|
+
this.isPlaying = false;
|
|
501
|
+
const a = this.audio;
|
|
502
|
+
// Removed from the page: disconnectedCallback saved already.
|
|
503
|
+
if (a?.ended || !this.isConnected || this.failed.has(this.srcIndex))
|
|
504
|
+
return;
|
|
505
|
+
this.save();
|
|
506
|
+
this.announce('Paused');
|
|
507
|
+
this.setPlaybackState('paused');
|
|
508
|
+
};
|
|
509
|
+
this.onTime = () => {
|
|
510
|
+
const a = this.audio;
|
|
511
|
+
if (!a || this.srcIndex !== this.currentIndex)
|
|
512
|
+
return;
|
|
513
|
+
const t = Math.floor(a.currentTime);
|
|
514
|
+
if (t !== this.time)
|
|
515
|
+
this.time = t;
|
|
516
|
+
if (this.isPlaying && Date.now() - this.lastSave >= audioTiming.save)
|
|
517
|
+
this.save();
|
|
518
|
+
};
|
|
519
|
+
this.onMeta = () => {
|
|
520
|
+
const a = this.audio;
|
|
521
|
+
const i = this.srcIndex;
|
|
522
|
+
if (!a || i < 0)
|
|
523
|
+
return;
|
|
524
|
+
if (Number.isFinite(a.duration) && a.duration > 0)
|
|
525
|
+
this.loaded = new Map(this.loaded).set(i, a.duration);
|
|
526
|
+
a.playbackRate = this.speed;
|
|
527
|
+
if (this.pendingTime !== undefined) {
|
|
528
|
+
a.currentTime = Math.min(this.pendingTime, a.duration || this.pendingTime);
|
|
529
|
+
this.pendingTime = undefined;
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
this.onEnded = () => {
|
|
533
|
+
const i = this.currentIndex;
|
|
534
|
+
const c = this.chapters[i];
|
|
535
|
+
this.isPlaying = false;
|
|
536
|
+
if (!c)
|
|
537
|
+
return;
|
|
538
|
+
if (!this.completed.has(i)) {
|
|
539
|
+
this.completed.add(i);
|
|
540
|
+
trackEvent(this, [TAG, 'complete', `chapter ${i + 1}`, i + 1]);
|
|
541
|
+
}
|
|
542
|
+
const next = this.chapters[i + 1];
|
|
543
|
+
if (!next) {
|
|
544
|
+
this.finished = true;
|
|
545
|
+
this.write(null);
|
|
546
|
+
this.announce('Finished');
|
|
547
|
+
this.setPlaybackState('none');
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
if (this.continuous) {
|
|
551
|
+
void this.play(i + 1, 0);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
// Not continuing: the next visit offers the next chapter from its start.
|
|
555
|
+
this.write({ src: next.src, chapter: i + 1, time: 0, speed: this.speed });
|
|
556
|
+
this.announce(`Finished ${c.title}`);
|
|
557
|
+
this.setPlaybackState('paused');
|
|
558
|
+
};
|
|
559
|
+
this.onError = () => {
|
|
560
|
+
if (this.srcIndex >= 0 && this.audio?.error)
|
|
561
|
+
this.fail(this.srcIndex);
|
|
562
|
+
};
|
|
563
|
+
// ---- drawing
|
|
564
|
+
this.onDownload = (name) => () => trackEvent(this, [TAG, 'download', name]);
|
|
565
|
+
this.source = new SourceChildren(this, () => this.lightDom, (nodes) => this.read(nodes));
|
|
566
|
+
}
|
|
567
|
+
/** The current chapter's index (0-based), or -1 before any is chosen. */
|
|
568
|
+
get current() {
|
|
569
|
+
return this.currentIndex;
|
|
570
|
+
}
|
|
571
|
+
/** Whether a chapter is playing. */
|
|
572
|
+
get playing() {
|
|
573
|
+
return this.isPlaying;
|
|
574
|
+
}
|
|
575
|
+
createRenderRoot() {
|
|
576
|
+
const root = super.createRenderRoot();
|
|
577
|
+
if (root instanceof ShadowRoot && this.noStyle) {
|
|
578
|
+
const sheet = shadowStructure.styleSheet;
|
|
579
|
+
if (sheet)
|
|
580
|
+
root.adoptedStyleSheets = [sheet];
|
|
581
|
+
}
|
|
582
|
+
return root;
|
|
583
|
+
}
|
|
584
|
+
connectedCallback() {
|
|
585
|
+
super.connectedCallback();
|
|
586
|
+
if (this.lightDom && this.noStyle) {
|
|
587
|
+
const node = this.getRootNode();
|
|
588
|
+
installLightDomStyles(`${TAG}::structure`, unsafeCSS(structuralRules(TAG)), {
|
|
589
|
+
root: node instanceof ShadowRoot || node instanceof Document ? node : undefined,
|
|
590
|
+
scope: false,
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
players.add(this);
|
|
594
|
+
window.addEventListener('pagehide', this.save);
|
|
595
|
+
}
|
|
596
|
+
disconnectedCallback() {
|
|
597
|
+
super.disconnectedCallback();
|
|
598
|
+
players.delete(this);
|
|
599
|
+
window.removeEventListener('pagehide', this.save);
|
|
600
|
+
this.save();
|
|
601
|
+
this.audio?.pause();
|
|
602
|
+
this.releaseMediaSession();
|
|
603
|
+
clearTimeout(this.announceTimer);
|
|
604
|
+
clearTimeout(this.clearTimer);
|
|
605
|
+
this.flushSpeed();
|
|
606
|
+
}
|
|
607
|
+
warnOnce(kind, message) {
|
|
608
|
+
if (this.warned.has(kind))
|
|
609
|
+
return;
|
|
610
|
+
this.warned.add(kind);
|
|
611
|
+
console.warn(`${TAG}: ${message}`);
|
|
612
|
+
}
|
|
613
|
+
// ---- the chapters, from the page's list
|
|
614
|
+
read(nodes) {
|
|
615
|
+
const list = nodes.find((n) => n instanceof Element && (n.localName === 'ol' || n.localName === 'ul'));
|
|
616
|
+
const doc = this.ownerDocument;
|
|
617
|
+
const base = doc.baseURI;
|
|
618
|
+
const chapters = [];
|
|
619
|
+
let key = '';
|
|
620
|
+
for (const li of list ? [...list.children] : []) {
|
|
621
|
+
if (li.localName !== 'li')
|
|
622
|
+
continue;
|
|
623
|
+
const links = [...li.querySelectorAll('a[href]')];
|
|
624
|
+
const a = links[0];
|
|
625
|
+
const href = a?.getAttribute('href') ?? '';
|
|
626
|
+
if (!a || !mediaHref(href, base)) {
|
|
627
|
+
this.warnOnce('chapter', 'ignoring a list item without a link to its audio file.');
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
const title = inlineCopy(a.childNodes, doc);
|
|
631
|
+
const extras = [];
|
|
632
|
+
for (const x of links.slice(1)) {
|
|
633
|
+
const xh = x.getAttribute('href') ?? '';
|
|
634
|
+
if (!safeHref(xh, base))
|
|
635
|
+
continue;
|
|
636
|
+
const attrs = KEPT_LINK_ATTRS.flatMap((n) => {
|
|
637
|
+
const v = x.getAttribute(n);
|
|
638
|
+
return v === null ? [] : [[n, v]];
|
|
639
|
+
});
|
|
640
|
+
const copy = inlineCopy(x.childNodes, doc);
|
|
641
|
+
extras.push({ href: xh, nodes: copy.nodes, attrs });
|
|
642
|
+
key += `|x${xh}${copy.key}${JSON.stringify(attrs)}`;
|
|
643
|
+
}
|
|
644
|
+
let download;
|
|
645
|
+
if (li.hasAttribute('data-download')) {
|
|
646
|
+
const dh = normalize(li.getAttribute('data-download')) || href;
|
|
647
|
+
if (mediaHref(dh, base))
|
|
648
|
+
download = { href: dh, size: formatSize(li.getAttribute('data-size')) };
|
|
649
|
+
}
|
|
650
|
+
const duration = parseDuration(a.getAttribute('data-duration'));
|
|
651
|
+
chapters.push({
|
|
652
|
+
src: new URL(href, base).href,
|
|
653
|
+
href,
|
|
654
|
+
title: normalize(a.textContent) || `Chapter ${chapters.length + 1}`,
|
|
655
|
+
nodes: title.nodes,
|
|
656
|
+
duration,
|
|
657
|
+
extras,
|
|
658
|
+
download,
|
|
659
|
+
});
|
|
660
|
+
key += `|${href}${title.key}${duration}${download?.href}${download?.size}`;
|
|
661
|
+
}
|
|
662
|
+
if (key === this.sourceKey)
|
|
663
|
+
return;
|
|
664
|
+
this.sourceKey = key;
|
|
665
|
+
this.chapters = chapters;
|
|
666
|
+
}
|
|
667
|
+
// ---- the remembered position
|
|
668
|
+
get storeKey() {
|
|
669
|
+
const extra = normalize(this.storageKey);
|
|
670
|
+
return `${TAG}:${location.pathname}${extra ? `#${extra}` : ''}`;
|
|
671
|
+
}
|
|
672
|
+
readSaved() {
|
|
673
|
+
if (this.noResume)
|
|
674
|
+
return undefined;
|
|
675
|
+
try {
|
|
676
|
+
const raw = localStorage.getItem(this.storeKey);
|
|
677
|
+
const v = raw ? JSON.parse(raw) : undefined;
|
|
678
|
+
return v && typeof v.chapter === 'number' && typeof v.time === 'number' ? v : undefined;
|
|
679
|
+
}
|
|
680
|
+
catch {
|
|
681
|
+
return undefined;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
write(v) {
|
|
685
|
+
if (this.noResume)
|
|
686
|
+
return;
|
|
687
|
+
try {
|
|
688
|
+
if (v)
|
|
689
|
+
localStorage.setItem(this.storeKey, JSON.stringify(v));
|
|
690
|
+
else
|
|
691
|
+
localStorage.removeItem(this.storeKey);
|
|
692
|
+
}
|
|
693
|
+
catch {
|
|
694
|
+
// Storage blocked or full: the position lasts for this visit only.
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
willUpdate(changed) {
|
|
698
|
+
super.willUpdate(changed);
|
|
699
|
+
if (this.restored || !this.chapters.length)
|
|
700
|
+
return;
|
|
701
|
+
this.restored = true;
|
|
702
|
+
const saved = this.readSaved();
|
|
703
|
+
if (!saved)
|
|
704
|
+
return;
|
|
705
|
+
if (SPEEDS.includes(saved.speed))
|
|
706
|
+
this.speed = this.reportedSpeed = saved.speed;
|
|
707
|
+
// The chapter by its file (the list may have changed since), else by its place.
|
|
708
|
+
let index = saved.src ? this.chapters.findIndex((c) => c.src === saved.src) : -1;
|
|
709
|
+
if (index < 0 && !saved.src && saved.chapter < this.chapters.length)
|
|
710
|
+
index = saved.chapter;
|
|
711
|
+
if (index < 0 || (index === 0 && saved.time < 1))
|
|
712
|
+
return;
|
|
713
|
+
this.resume = { index, time: Math.max(0, saved.time) };
|
|
714
|
+
}
|
|
715
|
+
// ---- playing
|
|
716
|
+
get audio() {
|
|
717
|
+
return this.renderRoot?.querySelector?.('audio') ?? null;
|
|
718
|
+
}
|
|
719
|
+
durationOf(i) {
|
|
720
|
+
return this.loaded.get(i) ?? this.chapters[i]?.duration;
|
|
721
|
+
}
|
|
722
|
+
/** Play chapter `index` (0-based; default the current one, else the first) from where it was. */
|
|
723
|
+
async play(index = this.currentIndex < 0 ? 0 : this.currentIndex, at) {
|
|
724
|
+
await this.updateComplete;
|
|
725
|
+
const c = this.chapters[index];
|
|
726
|
+
const a = this.audio;
|
|
727
|
+
if (!c || !a)
|
|
728
|
+
return;
|
|
729
|
+
this.resume = undefined;
|
|
730
|
+
this.finished = false;
|
|
731
|
+
if (index !== this.srcIndex) {
|
|
732
|
+
// The current chapter not loaded yet (skipped or seeked before playing): from there.
|
|
733
|
+
const from = at ?? (index === this.currentIndex ? (this.pendingTime ?? this.time) : 0);
|
|
734
|
+
this.currentIndex = index;
|
|
735
|
+
this.srcIndex = index;
|
|
736
|
+
this.time = Math.floor(from);
|
|
737
|
+
this.pendingTime = from || undefined;
|
|
738
|
+
if (this.failed.has(index)) {
|
|
739
|
+
this.failed.delete(index);
|
|
740
|
+
this.failed = new Set(this.failed);
|
|
741
|
+
}
|
|
742
|
+
a.src = c.src;
|
|
743
|
+
a.defaultPlaybackRate = a.playbackRate = this.speed;
|
|
744
|
+
}
|
|
745
|
+
else if (at !== undefined)
|
|
746
|
+
this.seek(at);
|
|
747
|
+
else if (a.ended)
|
|
748
|
+
this.seek(0);
|
|
749
|
+
this.currentIndex = index;
|
|
750
|
+
try {
|
|
751
|
+
await a.play();
|
|
752
|
+
}
|
|
753
|
+
catch (e) {
|
|
754
|
+
const name = e?.name;
|
|
755
|
+
// AbortError: another chapter was chosen before this one started.
|
|
756
|
+
if (name !== 'AbortError' && name !== 'NotAllowedError')
|
|
757
|
+
this.fail(index);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
/** Pause the chapter playing. */
|
|
761
|
+
pause() {
|
|
762
|
+
this.audio?.pause();
|
|
763
|
+
}
|
|
764
|
+
toggle(index) {
|
|
765
|
+
if (index === this.currentIndex && this.isPlaying)
|
|
766
|
+
this.pause();
|
|
767
|
+
else {
|
|
768
|
+
const resumeAt = index !== this.currentIndex && this.resume?.index === index ? this.resume.time : undefined;
|
|
769
|
+
void this.play(index, resumeAt);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
seek(t) {
|
|
773
|
+
const i = this.currentIndex;
|
|
774
|
+
const max = this.durationOf(i);
|
|
775
|
+
const to = Math.max(0, max ? Math.min(t, max) : t);
|
|
776
|
+
const a = this.audio;
|
|
777
|
+
if (a && this.srcIndex === i && a.readyState >= 1)
|
|
778
|
+
a.currentTime = to;
|
|
779
|
+
else
|
|
780
|
+
this.pendingTime = to;
|
|
781
|
+
this.time = Math.floor(to);
|
|
782
|
+
}
|
|
783
|
+
currentPosition() {
|
|
784
|
+
const a = this.audio;
|
|
785
|
+
return a && this.srcIndex === this.currentIndex && this.srcIndex >= 0
|
|
786
|
+
? a.currentTime
|
|
787
|
+
: this.time;
|
|
788
|
+
}
|
|
789
|
+
/** Before any chapter is current, the controls act on the one Resume offers, else the first. */
|
|
790
|
+
adoptResume() {
|
|
791
|
+
if (this.currentIndex >= 0)
|
|
792
|
+
return;
|
|
793
|
+
this.currentIndex = this.resume?.index ?? 0;
|
|
794
|
+
this.time = this.resume?.time ?? 0;
|
|
795
|
+
this.pendingTime = this.time || undefined;
|
|
796
|
+
this.resume = undefined;
|
|
797
|
+
}
|
|
798
|
+
skip(by) {
|
|
799
|
+
this.adoptResume();
|
|
800
|
+
this.resume = undefined;
|
|
801
|
+
this.seek(this.currentPosition() + by);
|
|
802
|
+
this.announce(`At ${spoken(this.time)}`, true);
|
|
803
|
+
this.save();
|
|
804
|
+
}
|
|
805
|
+
flushSpeed() {
|
|
806
|
+
clearTimeout(this.speedTimer);
|
|
807
|
+
this.speedTimer = undefined;
|
|
808
|
+
if (this.speed === this.reportedSpeed)
|
|
809
|
+
return;
|
|
810
|
+
this.reportedSpeed = this.speed;
|
|
811
|
+
trackEvent(this, [TAG, 'speed', String(this.speed), this.speed]);
|
|
812
|
+
}
|
|
813
|
+
fail(index) {
|
|
814
|
+
if (this.failed.has(index))
|
|
815
|
+
return;
|
|
816
|
+
this.failed = new Set(this.failed).add(index);
|
|
817
|
+
if (index === this.currentIndex)
|
|
818
|
+
this.isPlaying = false;
|
|
819
|
+
this.announce('This chapter could not be played');
|
|
820
|
+
trackError(this, TAG, 'chapter-load');
|
|
821
|
+
}
|
|
822
|
+
// ---- the Media Session (media keys, lock screen), for the instance playing
|
|
823
|
+
claimMediaSession(c) {
|
|
824
|
+
const ms = navigator.mediaSession;
|
|
825
|
+
if (!ms)
|
|
826
|
+
return;
|
|
827
|
+
try {
|
|
828
|
+
if (typeof MediaMetadata === 'function')
|
|
829
|
+
ms.metadata = new MediaMetadata({
|
|
830
|
+
title: c.title,
|
|
831
|
+
album: normalize(this.label) || normalize(this.ownerDocument.title),
|
|
832
|
+
});
|
|
833
|
+
ms.playbackState = 'playing';
|
|
834
|
+
const set = (a, h) => {
|
|
835
|
+
try {
|
|
836
|
+
ms.setActionHandler(a, h);
|
|
837
|
+
}
|
|
838
|
+
catch {
|
|
839
|
+
// An action this browser does not know.
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
const i = this.currentIndex;
|
|
843
|
+
set('play', () => void this.play());
|
|
844
|
+
set('pause', () => this.pause());
|
|
845
|
+
set('seekbackward', (d) => this.skip(-(d.seekOffset ?? SKIP.back)));
|
|
846
|
+
set('seekforward', (d) => this.skip(d.seekOffset ?? SKIP.forward));
|
|
847
|
+
set('seekto', (d) => d.seekTime !== undefined && this.seek(d.seekTime));
|
|
848
|
+
set('previoustrack', i > 0 ? () => void this.play(i - 1, 0) : null);
|
|
849
|
+
set('nexttrack', i < this.chapters.length - 1 ? () => void this.play(i + 1, 0) : null);
|
|
850
|
+
media.owner = this;
|
|
851
|
+
}
|
|
852
|
+
catch {
|
|
853
|
+
// No Media Session here.
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
setPlaybackState(s) {
|
|
857
|
+
if (media.owner !== this || !navigator.mediaSession)
|
|
858
|
+
return;
|
|
859
|
+
try {
|
|
860
|
+
navigator.mediaSession.playbackState = s;
|
|
861
|
+
}
|
|
862
|
+
catch {
|
|
863
|
+
// ignore
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
releaseMediaSession() {
|
|
867
|
+
const ms = navigator.mediaSession;
|
|
868
|
+
if (media.owner !== this || !ms)
|
|
869
|
+
return;
|
|
870
|
+
media.owner = undefined;
|
|
871
|
+
for (const a of [
|
|
872
|
+
'play',
|
|
873
|
+
'pause',
|
|
874
|
+
'seekbackward',
|
|
875
|
+
'seekforward',
|
|
876
|
+
'seekto',
|
|
877
|
+
'previoustrack',
|
|
878
|
+
'nexttrack',
|
|
879
|
+
])
|
|
880
|
+
try {
|
|
881
|
+
ms.setActionHandler(a, null);
|
|
882
|
+
}
|
|
883
|
+
catch {
|
|
884
|
+
// ignore
|
|
885
|
+
}
|
|
886
|
+
try {
|
|
887
|
+
ms.metadata = null;
|
|
888
|
+
ms.playbackState = 'none';
|
|
889
|
+
}
|
|
890
|
+
catch {
|
|
891
|
+
// ignore
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
// ---- announcements: one polite status
|
|
895
|
+
announce(text, debounced = false) {
|
|
896
|
+
clearTimeout(this.announceTimer);
|
|
897
|
+
const say = () => {
|
|
898
|
+
clearTimeout(this.clearTimer);
|
|
899
|
+
// The same words still in the region would not be read again: empty it first.
|
|
900
|
+
if (this.announcement === text) {
|
|
901
|
+
this.announcement = '';
|
|
902
|
+
void this.updateComplete.then(() => requestAnimationFrame(() => (this.announcement = text)));
|
|
903
|
+
}
|
|
904
|
+
else
|
|
905
|
+
this.announcement = text;
|
|
906
|
+
this.clearTimer = setTimeout(() => (this.announcement = ''), CLEAR_MS);
|
|
907
|
+
};
|
|
908
|
+
this.announceTimer = setTimeout(say, debounced ? audioTiming.announce : 0);
|
|
909
|
+
}
|
|
910
|
+
sizeText(size) {
|
|
911
|
+
return size
|
|
912
|
+
? html `<span aria-hidden="true"> · </span><span class="visually-hidden">, </span>${size}`
|
|
913
|
+
: nothing;
|
|
914
|
+
}
|
|
915
|
+
renderNow() {
|
|
916
|
+
const r = this.resume;
|
|
917
|
+
const rc = r && this.chapters[r.index];
|
|
918
|
+
if (rc)
|
|
919
|
+
return html `<button type="button" class="resume" @click=${this.onResume}>
|
|
920
|
+
Resume “${rc.title}” ${r.time >= 1 ? `at ${clock(r.time)}` : 'from the start'}
|
|
921
|
+
</button>`;
|
|
922
|
+
const c = this.chapters[this.currentIndex];
|
|
923
|
+
if (!c)
|
|
924
|
+
return html `<span class="state">Not playing</span>`;
|
|
925
|
+
const failed = this.failed.has(this.currentIndex);
|
|
926
|
+
const state = failed
|
|
927
|
+
? 'Could not play'
|
|
928
|
+
: this.finished
|
|
929
|
+
? 'Finished'
|
|
930
|
+
: this.isPlaying
|
|
931
|
+
? 'Now playing'
|
|
932
|
+
: 'Paused';
|
|
933
|
+
return html `<span><span class="state">${state}:</span> ${c.title}</span>`;
|
|
934
|
+
}
|
|
935
|
+
renderPlayer() {
|
|
936
|
+
const id = this.uid;
|
|
937
|
+
const i = this.currentIndex < 0 ? (this.resume?.index ?? 0) : this.currentIndex;
|
|
938
|
+
const total = this.durationOf(i) ?? 0;
|
|
939
|
+
const time = this.currentIndex < 0 ? (this.resume?.time ?? 0) : this.time;
|
|
940
|
+
const playing = this.isPlaying;
|
|
941
|
+
return html `<div
|
|
942
|
+
class="player"
|
|
943
|
+
part="player"
|
|
944
|
+
role="group"
|
|
945
|
+
aria-label=${normalize(this.label) || 'Player'}
|
|
946
|
+
>
|
|
947
|
+
<p class="now" part="now">${this.renderNow()}</p>
|
|
948
|
+
<div class="controls" part="controls">
|
|
949
|
+
<div class="buttons">
|
|
950
|
+
<button type="button" class="ctl back" @click=${() => this.skip(-SKIP.back)}>
|
|
951
|
+
${BACK}<span class="visually-hidden">Back </span>${SKIP.back}<span
|
|
952
|
+
class="visually-hidden"
|
|
953
|
+
>
|
|
954
|
+
seconds</span
|
|
955
|
+
>
|
|
956
|
+
</button>
|
|
957
|
+
<button
|
|
958
|
+
type="button"
|
|
959
|
+
class="ctl play"
|
|
960
|
+
@click=${() => (playing ? this.pause() : this.toggle(i))}
|
|
961
|
+
>
|
|
962
|
+
${playing ? PAUSE : PLAY}<span>${playing ? 'Pause' : 'Play'}</span>
|
|
963
|
+
</button>
|
|
964
|
+
<button type="button" class="ctl forward" @click=${() => this.skip(SKIP.forward)}>
|
|
965
|
+
<span class="visually-hidden">Forward </span>${SKIP.forward}<span
|
|
966
|
+
class="visually-hidden"
|
|
967
|
+
>
|
|
968
|
+
seconds</span
|
|
969
|
+
>${FORWARD}
|
|
970
|
+
</button>
|
|
971
|
+
</div>
|
|
972
|
+
<div class="seek">
|
|
973
|
+
<label class="visually-hidden" for="${id}-seek">Position</label>
|
|
974
|
+
<input
|
|
975
|
+
id="${id}-seek"
|
|
976
|
+
type="range"
|
|
977
|
+
min="0"
|
|
978
|
+
max=${Math.max(1, Math.ceil(total))}
|
|
979
|
+
step="1"
|
|
980
|
+
.value=${String(Math.min(time, Math.ceil(total) || time))}
|
|
981
|
+
aria-valuetext="${spoken(time)} of ${total ? spoken(total) : 'unknown length'}"
|
|
982
|
+
?disabled=${!total}
|
|
983
|
+
@input=${this.onSeekInput}
|
|
984
|
+
@change=${(e) => e.stopPropagation()}
|
|
985
|
+
/>
|
|
986
|
+
<span class="time"
|
|
987
|
+
>${clock(time)}<span aria-hidden="true"> / </span
|
|
988
|
+
><span class="visually-hidden"> of </span>${total ? clock(total) : '–:––'}</span
|
|
989
|
+
>
|
|
990
|
+
</div>
|
|
991
|
+
<div class="speed">
|
|
992
|
+
<label for="${id}-speed">Speed</label>
|
|
993
|
+
<select
|
|
994
|
+
id="${id}-speed"
|
|
995
|
+
@change=${(e) => {
|
|
996
|
+
e.stopPropagation();
|
|
997
|
+
this.onSpeed(e);
|
|
998
|
+
}}
|
|
999
|
+
@input=${(e) => e.stopPropagation()}
|
|
1000
|
+
>
|
|
1001
|
+
${SPEEDS.map((s) => html `<option value=${s} ?selected=${s === this.speed}>${s}×</option>`)}
|
|
1002
|
+
</select>
|
|
1003
|
+
</div>
|
|
1004
|
+
</div>
|
|
1005
|
+
</div>`;
|
|
1006
|
+
}
|
|
1007
|
+
renderDownload() {
|
|
1008
|
+
const href = normalize(this.downloadHref);
|
|
1009
|
+
if (!href)
|
|
1010
|
+
return nothing;
|
|
1011
|
+
if (!mediaHref(href, this.ownerDocument.baseURI)) {
|
|
1012
|
+
this.warnOnce('download', 'ignoring download-href: it is not http(s), blob: or relative.');
|
|
1013
|
+
return nothing;
|
|
1014
|
+
}
|
|
1015
|
+
return html `<p class="download" part="download">
|
|
1016
|
+
<a href=${href} download @click=${this.onDownload('recording')}
|
|
1017
|
+
><span
|
|
1018
|
+
>${normalize(this.downloadLabel) || 'Download the whole recording'}${this.sizeText(formatSize(this.downloadSize))}</span
|
|
1019
|
+
></a
|
|
1020
|
+
>
|
|
1021
|
+
</p>`;
|
|
1022
|
+
}
|
|
1023
|
+
renderChapter(c, i) {
|
|
1024
|
+
const current = i === this.currentIndex;
|
|
1025
|
+
const playing = current && this.isPlaying;
|
|
1026
|
+
const failed = this.failed.has(i);
|
|
1027
|
+
const d = this.chapters[i]?.duration;
|
|
1028
|
+
return html `<li class="chapter" part="chapter" aria-current=${current ? 'true' : nothing}>
|
|
1029
|
+
<button type="button" class="chapter-play" @click=${() => this.toggle(i)}>
|
|
1030
|
+
<span class="chip"
|
|
1031
|
+
>${playing ? PAUSE : PLAY}<span>${playing ? 'Pause' : 'Play'}</span></span
|
|
1032
|
+
>
|
|
1033
|
+
<span class="chapter-title"
|
|
1034
|
+
>${c.nodes}${current
|
|
1035
|
+
? html ` <span class="marker"
|
|
1036
|
+
>${failed ? 'Not played' : playing ? 'Now playing' : this.finished ? 'Finished' : 'Paused'}</span
|
|
1037
|
+
>`
|
|
1038
|
+
: nothing}</span
|
|
1039
|
+
>
|
|
1040
|
+
</button>
|
|
1041
|
+
<span class="duration"
|
|
1042
|
+
>${d !== undefined
|
|
1043
|
+
? html `<span aria-hidden="true">${clock(d)}</span
|
|
1044
|
+
><span class="visually-hidden">${spoken(d)}</span>`
|
|
1045
|
+
: nothing}</span
|
|
1046
|
+
>
|
|
1047
|
+
${c.extras.length || c.download
|
|
1048
|
+
? html `<span class="more"
|
|
1049
|
+
>${c.extras.map((x) => html `<a
|
|
1050
|
+
href=${x.href}
|
|
1051
|
+
hreflang=${attr(x, 'hreflang')}
|
|
1052
|
+
type=${attr(x, 'type')}
|
|
1053
|
+
target=${attr(x, 'target')}
|
|
1054
|
+
rel=${attr(x, 'rel')}
|
|
1055
|
+
lang=${attr(x, 'lang')}
|
|
1056
|
+
download=${attr(x, 'download')}
|
|
1057
|
+
><span>${x.nodes}</span></a
|
|
1058
|
+
>`)}${c.download
|
|
1059
|
+
? html `<a
|
|
1060
|
+
href=${c.download.href}
|
|
1061
|
+
download
|
|
1062
|
+
@click=${this.onDownload(`chapter ${i + 1}`)}
|
|
1063
|
+
><span
|
|
1064
|
+
>Download<span class="visually-hidden"> ${c.title}</span>${this.sizeText(c.download.size)}</span
|
|
1065
|
+
></a
|
|
1066
|
+
>`
|
|
1067
|
+
: nothing}</span
|
|
1068
|
+
>`
|
|
1069
|
+
: nothing}
|
|
1070
|
+
${failed
|
|
1071
|
+
? html `<p class="error" role="note">
|
|
1072
|
+
This chapter could not be played.
|
|
1073
|
+
<a href=${c.href}>Open its file<span class="visually-hidden"> (${c.title})</span></a>
|
|
1074
|
+
</p>`
|
|
1075
|
+
: nothing}
|
|
1076
|
+
</li>`;
|
|
1077
|
+
}
|
|
1078
|
+
render() {
|
|
1079
|
+
const chapters = this.chapters;
|
|
1080
|
+
if (!chapters.length)
|
|
1081
|
+
return nothing;
|
|
1082
|
+
return html `${this.renderPlayer()}${this.renderDownload()}
|
|
1083
|
+
<ol class="chapters" part="list">
|
|
1084
|
+
${chapters.map((c, i) => this.renderChapter(c, i))}
|
|
1085
|
+
</ol>
|
|
1086
|
+
<audio
|
|
1087
|
+
preload="none"
|
|
1088
|
+
@play=${this.onPlay}
|
|
1089
|
+
@pause=${this.onPause}
|
|
1090
|
+
@timeupdate=${this.onTime}
|
|
1091
|
+
@loadedmetadata=${this.onMeta}
|
|
1092
|
+
@ended=${this.onEnded}
|
|
1093
|
+
@error=${this.onError}
|
|
1094
|
+
></audio>
|
|
1095
|
+
<span class="visually-hidden" role="status" aria-live="polite" aria-atomic="true"
|
|
1096
|
+
>${this.announcement}</span
|
|
1097
|
+
>`;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
__decorate([
|
|
1101
|
+
property({ type: String })
|
|
1102
|
+
], UvalibAudio.prototype, "label", void 0);
|
|
1103
|
+
__decorate([
|
|
1104
|
+
property({ type: Boolean, reflect: true })
|
|
1105
|
+
], UvalibAudio.prototype, "continuous", void 0);
|
|
1106
|
+
__decorate([
|
|
1107
|
+
property({ type: Boolean, attribute: 'no-resume' })
|
|
1108
|
+
], UvalibAudio.prototype, "noResume", void 0);
|
|
1109
|
+
__decorate([
|
|
1110
|
+
property({ type: String, attribute: 'storage-key' })
|
|
1111
|
+
], UvalibAudio.prototype, "storageKey", void 0);
|
|
1112
|
+
__decorate([
|
|
1113
|
+
property({ type: String, attribute: 'download-href' })
|
|
1114
|
+
], UvalibAudio.prototype, "downloadHref", void 0);
|
|
1115
|
+
__decorate([
|
|
1116
|
+
property({ type: String, attribute: 'download-label' })
|
|
1117
|
+
], UvalibAudio.prototype, "downloadLabel", void 0);
|
|
1118
|
+
__decorate([
|
|
1119
|
+
property({ type: String, attribute: 'download-size' })
|
|
1120
|
+
], UvalibAudio.prototype, "downloadSize", void 0);
|
|
1121
|
+
__decorate([
|
|
1122
|
+
state()
|
|
1123
|
+
], UvalibAudio.prototype, "chapters", void 0);
|
|
1124
|
+
__decorate([
|
|
1125
|
+
state()
|
|
1126
|
+
], UvalibAudio.prototype, "currentIndex", void 0);
|
|
1127
|
+
__decorate([
|
|
1128
|
+
state()
|
|
1129
|
+
], UvalibAudio.prototype, "isPlaying", void 0);
|
|
1130
|
+
__decorate([
|
|
1131
|
+
state()
|
|
1132
|
+
], UvalibAudio.prototype, "finished", void 0);
|
|
1133
|
+
__decorate([
|
|
1134
|
+
state()
|
|
1135
|
+
], UvalibAudio.prototype, "time", void 0);
|
|
1136
|
+
__decorate([
|
|
1137
|
+
state()
|
|
1138
|
+
], UvalibAudio.prototype, "speed", void 0);
|
|
1139
|
+
__decorate([
|
|
1140
|
+
state()
|
|
1141
|
+
], UvalibAudio.prototype, "resume", void 0);
|
|
1142
|
+
__decorate([
|
|
1143
|
+
state()
|
|
1144
|
+
], UvalibAudio.prototype, "failed", void 0);
|
|
1145
|
+
__decorate([
|
|
1146
|
+
state()
|
|
1147
|
+
], UvalibAudio.prototype, "loaded", void 0);
|
|
1148
|
+
__decorate([
|
|
1149
|
+
state()
|
|
1150
|
+
], UvalibAudio.prototype, "announcement", void 0);
|
|
1151
|
+
/** The instance whose handlers the Media Session has. */
|
|
1152
|
+
const media = {};
|
|
1153
|
+
const attr = (x, name) => x.attrs.find(([n]) => n === name)?.[1] ?? nothing;
|
|
1154
|
+
//# sourceMappingURL=UvalibAudio.js.map
|