@molecule/app-audio-player-angular 0.0.0-stage → 1.1.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/LICENSE +202 -0
- package/README.md +388 -2
- package/dist/audio-player.component.d.ts +260 -0
- package/dist/audio-player.component.d.ts.map +1 -0
- package/dist/audio-player.component.js +707 -0
- package/dist/audio-player.component.js.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +69 -4
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AudioPlayer component (React parity).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { NgTemplateOutlet } from '@angular/common';
|
|
7
|
+
import { ChangeDetectorRef, Component, ElementRef, inject, Renderer2, ViewChild, } from '@angular/core';
|
|
8
|
+
import { useTranslation } from '@molecule/app-angular';
|
|
9
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
10
|
+
import { Button, SlotWrapper } from '@molecule/app-ui-angular';
|
|
11
|
+
import * as i0 from "@angular/core";
|
|
12
|
+
const _c0 = ["audio"];
|
|
13
|
+
const _c1 = ["track"];
|
|
14
|
+
const _c2 = ["header"];
|
|
15
|
+
const _c3 = ["titleSpan"];
|
|
16
|
+
const _c4 = ["subtitleSpan"];
|
|
17
|
+
const _c5 = ["visualizerSlot"];
|
|
18
|
+
const _c6 = [[["", "slot", "title"]], [["", "slot", "subtitle"]], [["", "slot", "visualizer"]]];
|
|
19
|
+
const _c7 = ["[slot=title]", "[slot=subtitle]", "[slot=visualizer]"];
|
|
20
|
+
function AudioPlayer_Conditional_9_Template(rf, ctx) { if (rf & 1) {
|
|
21
|
+
i0.ɵɵelementContainer(0, 8);
|
|
22
|
+
} if (rf & 2) {
|
|
23
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
24
|
+
i0.ɵɵproperty("ngTemplateOutlet", ctx_r0.asTemplate(ctx_r0.title));
|
|
25
|
+
} }
|
|
26
|
+
function AudioPlayer_Conditional_10_Template(rf, ctx) { if (rf & 1) {
|
|
27
|
+
i0.ɵɵtext(0);
|
|
28
|
+
} if (rf & 2) {
|
|
29
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
30
|
+
i0.ɵɵtextInterpolate(ctx_r0.titleText());
|
|
31
|
+
} }
|
|
32
|
+
function AudioPlayer_Conditional_14_Template(rf, ctx) { if (rf & 1) {
|
|
33
|
+
i0.ɵɵelementContainer(0, 8);
|
|
34
|
+
} if (rf & 2) {
|
|
35
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
36
|
+
i0.ɵɵproperty("ngTemplateOutlet", ctx_r0.asTemplate(ctx_r0.subtitle));
|
|
37
|
+
} }
|
|
38
|
+
function AudioPlayer_Conditional_15_Template(rf, ctx) { if (rf & 1) {
|
|
39
|
+
i0.ɵɵtext(0);
|
|
40
|
+
} if (rf & 2) {
|
|
41
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
42
|
+
i0.ɵɵtextInterpolate(ctx_r0.subtitleText());
|
|
43
|
+
} }
|
|
44
|
+
function AudioPlayer_Conditional_19_Template(rf, ctx) { if (rf & 1) {
|
|
45
|
+
i0.ɵɵelementContainer(0, 8);
|
|
46
|
+
} if (rf & 2) {
|
|
47
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
48
|
+
i0.ɵɵproperty("ngTemplateOutlet", ctx_r0.asTemplate(ctx_r0.visualizer));
|
|
49
|
+
} }
|
|
50
|
+
function AudioPlayer_Conditional_20_Template(rf, ctx) { if (rf & 1) {
|
|
51
|
+
i0.ɵɵtext(0);
|
|
52
|
+
} if (rf & 2) {
|
|
53
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
54
|
+
i0.ɵɵtextInterpolate(ctx_r0.visualizerText());
|
|
55
|
+
} }
|
|
56
|
+
function AudioPlayer_Conditional_31_Template(rf, ctx) { if (rf & 1) {
|
|
57
|
+
i0.ɵɵelementStart(0, "p", 15);
|
|
58
|
+
i0.ɵɵtext(1);
|
|
59
|
+
i0.ɵɵelementEnd();
|
|
60
|
+
} if (rf & 2) {
|
|
61
|
+
const ctx_r0 = i0.ɵɵnextContext();
|
|
62
|
+
i0.ɵɵclassMap(ctx_r0.captionClasses());
|
|
63
|
+
i0.ɵɵadvance();
|
|
64
|
+
i0.ɵɵtextInterpolate(ctx_r0.caption);
|
|
65
|
+
} }
|
|
66
|
+
/**
|
|
67
|
+
* Whether a field is one of the cases' projected node templates (a `{ $nodes }`
|
|
68
|
+
* value inside a prop becomes a `TemplateRef`).
|
|
69
|
+
*
|
|
70
|
+
* @param value - The field value.
|
|
71
|
+
* @returns True for a projected node template.
|
|
72
|
+
*/
|
|
73
|
+
const isTemplate = (value) => typeof value === 'object' && value !== null && 'createEmbeddedView' in value;
|
|
74
|
+
/**
|
|
75
|
+
* Whether a capture element holds projected content, ignoring the text the
|
|
76
|
+
* component itself renders there (a string input's interpolation).
|
|
77
|
+
*
|
|
78
|
+
* @param el - The capture element.
|
|
79
|
+
* @param ownText - The text this component interpolates into it.
|
|
80
|
+
* @returns True when projected nodes exist.
|
|
81
|
+
*/
|
|
82
|
+
function hasProjectedNodes(el, ownText) {
|
|
83
|
+
if (!el)
|
|
84
|
+
return false;
|
|
85
|
+
for (const node of Array.from(el.childNodes)) {
|
|
86
|
+
if (node.nodeType === Node.ELEMENT_NODE)
|
|
87
|
+
return true;
|
|
88
|
+
if (node.nodeType !== Node.TEXT_NODE)
|
|
89
|
+
continue;
|
|
90
|
+
const value = node.textContent ?? '';
|
|
91
|
+
if (value.trim() && value !== ownText)
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Formats a duration in seconds as M:SS (90 → `1:30`), React's `fmt`.
|
|
98
|
+
*
|
|
99
|
+
* @param s - The duration in seconds.
|
|
100
|
+
* @returns The formatted time.
|
|
101
|
+
*/
|
|
102
|
+
function fmt(s) {
|
|
103
|
+
if (!isFinite(s))
|
|
104
|
+
return '0:00';
|
|
105
|
+
const m = Math.floor(s / 60);
|
|
106
|
+
const sec = Math.floor(s % 60);
|
|
107
|
+
return `${m}:${String(sec).padStart(2, '0')}`;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* HTML5 audio player chrome — play/pause, scrub, time, mute, optional
|
|
111
|
+
* visualizer slot.
|
|
112
|
+
*
|
|
113
|
+
* The host IS the outer `<div>` (`div[mol-audio-player]`), matching React's
|
|
114
|
+
* root exactly, so the wrapper's ClassMap classes are the host's. The
|
|
115
|
+
* `<audio>` element, the optional title / subtitle header, the visualizer and
|
|
116
|
+
* the controls row render inside it, in React's order.
|
|
117
|
+
*
|
|
118
|
+
* Angular projection needs always-mounted `<ng-content>` capture points, so
|
|
119
|
+
* the header (and each of its spans) and the visualizer wrapper live in the
|
|
120
|
+
* template unconditionally and {@link AudioPlayer.ngDoCheck} reconciles them
|
|
121
|
+
* against React's DOM — React omits the header when neither title nor subtitle
|
|
122
|
+
* renders, and renders the visualizer bare, without a wrapper element.
|
|
123
|
+
*/
|
|
124
|
+
export class AudioPlayer {
|
|
125
|
+
/** Audio source URL (a file, a stream or a data URI). */
|
|
126
|
+
src = '';
|
|
127
|
+
/** Optional title / track name: a string input, or projected `[slot=title]` content. */
|
|
128
|
+
title;
|
|
129
|
+
/** Optional artist / source label: a string input, or projected `[slot=subtitle]` content. */
|
|
130
|
+
subtitle;
|
|
131
|
+
/** Optional waveform / visualizer: projected `[slot=visualizer]` content. */
|
|
132
|
+
visualizer;
|
|
133
|
+
/**
|
|
134
|
+
* WebVTT captions file URL. When set, the player loads it on the media
|
|
135
|
+
* element's captions `<track>` and shows the current caption under the
|
|
136
|
+
* controls, in a polite live region that screen readers announce. Without
|
|
137
|
+
* it the track is empty and no caption line is shown.
|
|
138
|
+
*/
|
|
139
|
+
captions;
|
|
140
|
+
/** BCP 47 language of the captions (the track's `srclang`). */
|
|
141
|
+
captionsLang;
|
|
142
|
+
/**
|
|
143
|
+
* Human-readable name of the captions track. Defaults through i18n
|
|
144
|
+
* (`audio.captions`, English "Captions").
|
|
145
|
+
*/
|
|
146
|
+
captionsLabel;
|
|
147
|
+
/** Initial muted state. */
|
|
148
|
+
defaultMuted;
|
|
149
|
+
/** Autoplay (browser may block). */
|
|
150
|
+
autoPlay;
|
|
151
|
+
/** Called when the media starts playing. */
|
|
152
|
+
onPlay;
|
|
153
|
+
/** Called when the media is paused. */
|
|
154
|
+
onPause;
|
|
155
|
+
/** Called when the media reaches its end. */
|
|
156
|
+
onEnded;
|
|
157
|
+
/** Extra classes on the outer wrapper. */
|
|
158
|
+
className;
|
|
159
|
+
/** The hidden media element. */
|
|
160
|
+
audioRef;
|
|
161
|
+
/** The captions track element (its text track fires `cuechange`). */
|
|
162
|
+
trackRef;
|
|
163
|
+
/** The title / subtitle header (present only while React renders it). */
|
|
164
|
+
headerRef;
|
|
165
|
+
/** The title span (present only while React renders it). */
|
|
166
|
+
titleSpanRef;
|
|
167
|
+
/** The subtitle span (present only while React renders it). */
|
|
168
|
+
subtitleSpanRef;
|
|
169
|
+
/** The visualizer's capture wrapper (unwrapped when React renders content). */
|
|
170
|
+
visualizerSlotRef;
|
|
171
|
+
/** Whether the media is playing (React's `playing` state). */
|
|
172
|
+
playing = false;
|
|
173
|
+
/** Whether the player is muted (React's `muted` state). */
|
|
174
|
+
muted = false;
|
|
175
|
+
/** The media duration in seconds (React's `duration` state). */
|
|
176
|
+
duration = 0;
|
|
177
|
+
/** The current position in seconds (React's `current` state). */
|
|
178
|
+
current = 0;
|
|
179
|
+
/** The active cues' text (React's `caption` state). */
|
|
180
|
+
caption = '';
|
|
181
|
+
i18n = useTranslation();
|
|
182
|
+
cd = inject(ChangeDetectorRef);
|
|
183
|
+
host = inject(ElementRef).nativeElement;
|
|
184
|
+
renderer = inject(Renderer2);
|
|
185
|
+
slots = new SlotWrapper(inject(ElementRef), this.renderer);
|
|
186
|
+
/** Whether `[slot=title]` content was projected. */
|
|
187
|
+
hasTitleSlot = false;
|
|
188
|
+
/** Whether `[slot=subtitle]` content was projected. */
|
|
189
|
+
hasSubtitleSlot = false;
|
|
190
|
+
/** Whether `[slot=visualizer]` content was projected. */
|
|
191
|
+
hasVisualizerSlot = false;
|
|
192
|
+
/** Whether the visualizer's projected nodes were moved out of their wrapper. */
|
|
193
|
+
visualizerOpen = false;
|
|
194
|
+
/** The visualizer nodes, while they live outside the wrapper. */
|
|
195
|
+
visualizerChildren = [];
|
|
196
|
+
/** The positioned listener cleanups, for the teardown. */
|
|
197
|
+
detach = [];
|
|
198
|
+
/** Whether the view (and with it the `<track>` element) exists. */
|
|
199
|
+
viewReady = false;
|
|
200
|
+
/** Removes the captions `cuechange` listener, while it is attached. */
|
|
201
|
+
detachCaptions = null;
|
|
202
|
+
/**
|
|
203
|
+
* The current class map.
|
|
204
|
+
* @returns The UIClassMap.
|
|
205
|
+
*/
|
|
206
|
+
cm() {
|
|
207
|
+
return getClassMap();
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Computes the wrapper class list (React's `cm.cn(cm.stack(2), cm.sp('p', 3), className)`).
|
|
211
|
+
* @returns The class string.
|
|
212
|
+
*/
|
|
213
|
+
classes() {
|
|
214
|
+
return this.cm().cn(this.cm().stack(2), this.cm().sp('p', 3), this.className);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The title span's classes (React's `cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'))`).
|
|
218
|
+
* @returns The class string.
|
|
219
|
+
*/
|
|
220
|
+
titleClasses() {
|
|
221
|
+
return this.cm().cn(this.cm().textSize('sm'), this.cm().fontWeight('semibold'));
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* The subtitle span's classes (React's `cm.textSize('xs')`).
|
|
225
|
+
* @returns The class string.
|
|
226
|
+
*/
|
|
227
|
+
subtitleClasses() {
|
|
228
|
+
return this.cm().textSize('xs');
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The caption line's classes (React's `cm.textSize('sm')`).
|
|
232
|
+
* @returns The class string.
|
|
233
|
+
*/
|
|
234
|
+
captionClasses() {
|
|
235
|
+
return this.cm().textSize('sm');
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The header's classes (React's `cm.stack(0)`).
|
|
239
|
+
* @returns The class string.
|
|
240
|
+
*/
|
|
241
|
+
stackClasses() {
|
|
242
|
+
return this.cm().cn(this.cm().stack(0));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The elapsed / total span's classes (React's `cm.textSize('xs')`).
|
|
246
|
+
* @returns The class string.
|
|
247
|
+
*/
|
|
248
|
+
timeClasses() {
|
|
249
|
+
return this.cm().textSize('xs');
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The range input's classes (React's `cm.flex1`).
|
|
253
|
+
* @returns The class string.
|
|
254
|
+
*/
|
|
255
|
+
seekClasses() {
|
|
256
|
+
return this.cm().cn(this.cm().flex1);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* The controls row's classes (React's `cm.flex({ align: 'center', gap: 'sm' })`).
|
|
260
|
+
* @returns The class string.
|
|
261
|
+
*/
|
|
262
|
+
controlsClasses() {
|
|
263
|
+
return this.cm().flex({ align: 'center', gap: 'sm' });
|
|
264
|
+
}
|
|
265
|
+
/** Reads React's `useState` initializers, once the inputs are bound. */
|
|
266
|
+
ngOnInit() {
|
|
267
|
+
this.playing = !!this.autoPlay;
|
|
268
|
+
this.muted = !!this.defaultMuted;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Tears the captions setup down and rebuilds it after a `captions` input
|
|
272
|
+
* change (React's effect over the `captions` dependency) — nothing to do
|
|
273
|
+
* before the view exists ({@link AudioPlayer.ngAfterViewInit} runs it then).
|
|
274
|
+
* @param changes - The inputs that changed.
|
|
275
|
+
*/
|
|
276
|
+
ngOnChanges(changes) {
|
|
277
|
+
if (!this.viewReady || !('captions' in changes))
|
|
278
|
+
return;
|
|
279
|
+
this.dropCaptions();
|
|
280
|
+
this.syncCaptions();
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Attaches the media listeners (React's effect) and reads the projected
|
|
284
|
+
* slots' presence — view queries resolve only at view init.
|
|
285
|
+
*/
|
|
286
|
+
ngAfterViewInit() {
|
|
287
|
+
const audio = this.audioRef?.nativeElement;
|
|
288
|
+
if (audio) {
|
|
289
|
+
const onTimeUpdate = () => {
|
|
290
|
+
this.current = audio.currentTime;
|
|
291
|
+
this.cd.markForCheck();
|
|
292
|
+
};
|
|
293
|
+
const onLoadedMetadata = () => {
|
|
294
|
+
this.duration = audio.duration;
|
|
295
|
+
this.cd.markForCheck();
|
|
296
|
+
};
|
|
297
|
+
const onPlayEvent = () => {
|
|
298
|
+
this.playing = true;
|
|
299
|
+
this.onPlay?.();
|
|
300
|
+
this.cd.markForCheck();
|
|
301
|
+
};
|
|
302
|
+
const onPauseEvent = () => {
|
|
303
|
+
this.playing = false;
|
|
304
|
+
this.onPause?.();
|
|
305
|
+
this.cd.markForCheck();
|
|
306
|
+
};
|
|
307
|
+
const onEndedEvent = () => {
|
|
308
|
+
this.playing = false;
|
|
309
|
+
this.onEnded?.();
|
|
310
|
+
this.cd.markForCheck();
|
|
311
|
+
};
|
|
312
|
+
audio.addEventListener('timeupdate', onTimeUpdate);
|
|
313
|
+
audio.addEventListener('loadedmetadata', onLoadedMetadata);
|
|
314
|
+
audio.addEventListener('play', onPlayEvent);
|
|
315
|
+
audio.addEventListener('pause', onPauseEvent);
|
|
316
|
+
audio.addEventListener('ended', onEndedEvent);
|
|
317
|
+
this.detach.push(() => {
|
|
318
|
+
audio.removeEventListener('timeupdate', onTimeUpdate);
|
|
319
|
+
audio.removeEventListener('loadedmetadata', onLoadedMetadata);
|
|
320
|
+
audio.removeEventListener('play', onPlayEvent);
|
|
321
|
+
audio.removeEventListener('pause', onPauseEvent);
|
|
322
|
+
audio.removeEventListener('ended', onEndedEvent);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
this.hasTitleSlot = hasProjectedNodes(this.titleSpanRef?.nativeElement, this.titleText());
|
|
326
|
+
this.hasSubtitleSlot = hasProjectedNodes(this.subtitleSpanRef?.nativeElement, this.subtitleText());
|
|
327
|
+
this.hasVisualizerSlot = hasProjectedNodes(this.visualizerSlotRef?.nativeElement, this.visualizerText());
|
|
328
|
+
this.viewReady = true;
|
|
329
|
+
this.syncCaptions();
|
|
330
|
+
this.reconcile();
|
|
331
|
+
}
|
|
332
|
+
/** Re-reconciles React's conditional DOM after input writes and state changes. */
|
|
333
|
+
ngDoCheck() {
|
|
334
|
+
this.reconcile();
|
|
335
|
+
}
|
|
336
|
+
/** Removes the media listeners and the captions listener (React's effect cleanup). */
|
|
337
|
+
ngOnDestroy() {
|
|
338
|
+
for (const undo of this.detach.splice(0))
|
|
339
|
+
undo();
|
|
340
|
+
this.dropCaptions();
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Whether React's `{node && …}` branch renders for a node-valued prop: a
|
|
344
|
+
* projected template always does, a plain value only when truthy.
|
|
345
|
+
* @param value - The node-valued prop.
|
|
346
|
+
* @returns True when React renders it.
|
|
347
|
+
*/
|
|
348
|
+
shows(value) {
|
|
349
|
+
if (isTemplate(value))
|
|
350
|
+
return true;
|
|
351
|
+
return value !== undefined && value !== null && value !== false && value !== '' && value !== 0;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Whether a value is a projected node template.
|
|
355
|
+
* @param value - The field value.
|
|
356
|
+
* @returns True for a projected node template.
|
|
357
|
+
*/
|
|
358
|
+
isTemplate(value) {
|
|
359
|
+
return isTemplate(value);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* The value as a template (call under {@link AudioPlayer.isTemplate}).
|
|
363
|
+
* @param value - The field value.
|
|
364
|
+
* @returns The template.
|
|
365
|
+
*/
|
|
366
|
+
asTemplate(value) {
|
|
367
|
+
return value;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The title input's text (a string input renders as text; projected nodes
|
|
371
|
+
* render through the capture point instead).
|
|
372
|
+
* @returns The title text, or `''` when absent.
|
|
373
|
+
*/
|
|
374
|
+
titleText() {
|
|
375
|
+
return typeof this.title === 'string' || typeof this.title === 'number'
|
|
376
|
+
? String(this.title)
|
|
377
|
+
: '';
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* The subtitle input's text.
|
|
381
|
+
* @returns The subtitle text, or `''` when absent.
|
|
382
|
+
*/
|
|
383
|
+
subtitleText() {
|
|
384
|
+
return typeof this.subtitle === 'string' || typeof this.subtitle === 'number'
|
|
385
|
+
? String(this.subtitle)
|
|
386
|
+
: '';
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* The visualizer input's text.
|
|
390
|
+
* @returns The visualizer text, or `''` when absent.
|
|
391
|
+
*/
|
|
392
|
+
visualizerText() {
|
|
393
|
+
return typeof this.visualizer === 'string' || typeof this.visualizer === 'number'
|
|
394
|
+
? String(this.visualizer)
|
|
395
|
+
: '';
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The elapsed / total display (React's `{fmt(current)} / {fmt(duration)}`).
|
|
399
|
+
* @returns The time text.
|
|
400
|
+
*/
|
|
401
|
+
timeText() {
|
|
402
|
+
return `${fmt(this.current)} / ${fmt(this.duration)}`;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* The play/pause control's accessible name (React's `aria-label`).
|
|
406
|
+
* @returns The label text.
|
|
407
|
+
*/
|
|
408
|
+
toggleLabel() {
|
|
409
|
+
return this.playing
|
|
410
|
+
? this.i18n.t('audio.pause', {}, { defaultValue: 'Pause' })
|
|
411
|
+
: this.i18n.t('audio.play', {}, { defaultValue: 'Play' });
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* The mute control's accessible name (React's `aria-label`).
|
|
415
|
+
* @returns The label text.
|
|
416
|
+
*/
|
|
417
|
+
muteLabel() {
|
|
418
|
+
return this.muted
|
|
419
|
+
? this.i18n.t('audio.unmute', {}, { defaultValue: 'Unmute' })
|
|
420
|
+
: this.i18n.t('audio.mute', {}, { defaultValue: 'Mute' });
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* The seek control's accessible name (React's `aria-label`).
|
|
424
|
+
* @returns The label text.
|
|
425
|
+
*/
|
|
426
|
+
seekLabel() {
|
|
427
|
+
return this.i18n.t('audio.seek', {}, { defaultValue: 'Seek' });
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* The captions track's accessible name (React's `label`): the
|
|
431
|
+
* `captionsLabel` input, or the i18n default.
|
|
432
|
+
* @returns The label text.
|
|
433
|
+
*/
|
|
434
|
+
trackLabel() {
|
|
435
|
+
return this.captionsLabel ?? this.i18n.t('audio.captions', {}, { defaultValue: 'Captions' });
|
|
436
|
+
}
|
|
437
|
+
/** Toggles play/pause on the audio element (React's `toggle`). */
|
|
438
|
+
toggle() {
|
|
439
|
+
const audio = this.audioRef?.nativeElement;
|
|
440
|
+
if (!audio)
|
|
441
|
+
return;
|
|
442
|
+
if (audio.paused)
|
|
443
|
+
void audio.play();
|
|
444
|
+
else
|
|
445
|
+
audio.pause();
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Seeks the audio element to a position in seconds (React's `seek`). React
|
|
449
|
+
* restores a controlled input to the state value right after the change
|
|
450
|
+
* handler returns, so the range is written back too.
|
|
451
|
+
* @param event - The range input's native input event.
|
|
452
|
+
*/
|
|
453
|
+
onSeek(event) {
|
|
454
|
+
const element = event.target;
|
|
455
|
+
const audio = this.audioRef?.nativeElement;
|
|
456
|
+
if (audio)
|
|
457
|
+
audio.currentTime = Number(element.value);
|
|
458
|
+
element.value = String(this.current);
|
|
459
|
+
}
|
|
460
|
+
/** Toggles the muted state on the media element and in the state (React's `toggleMute`). */
|
|
461
|
+
toggleMute() {
|
|
462
|
+
const audio = this.audioRef?.nativeElement;
|
|
463
|
+
if (!audio)
|
|
464
|
+
return;
|
|
465
|
+
audio.muted = !audio.muted;
|
|
466
|
+
this.muted = audio.muted;
|
|
467
|
+
this.cd.markForCheck();
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Removes the captions `cuechange` listener and clears the caption (React's
|
|
471
|
+
* effect cleanup; the `<track>` element the listener rode on stays).
|
|
472
|
+
*/
|
|
473
|
+
dropCaptions() {
|
|
474
|
+
this.detachCaptions?.();
|
|
475
|
+
this.detachCaptions = null;
|
|
476
|
+
if (this.caption) {
|
|
477
|
+
this.caption = '';
|
|
478
|
+
this.cd.markForCheck();
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Puts the captions track in `hidden` mode — a hidden track still fires
|
|
483
|
+
* `cuechange`, while the hidden `<audio>` paints nothing — and listens for
|
|
484
|
+
* `cuechange` to show the active cues' text (React's captions effect).
|
|
485
|
+
*/
|
|
486
|
+
syncCaptions() {
|
|
487
|
+
const textTrack = this.trackRef?.nativeElement?.track ?? null;
|
|
488
|
+
if (!this.captions || !textTrack)
|
|
489
|
+
return;
|
|
490
|
+
if (this.detachCaptions)
|
|
491
|
+
return;
|
|
492
|
+
textTrack.mode = 'hidden';
|
|
493
|
+
const onCue = () => {
|
|
494
|
+
const cues = textTrack.activeCues ? Array.from(textTrack.activeCues) : [];
|
|
495
|
+
this.caption = cues.map((cue) => ('text' in cue ? String(cue.text) : '')).join('\n');
|
|
496
|
+
this.cd.markForCheck();
|
|
497
|
+
};
|
|
498
|
+
textTrack.addEventListener('cuechange', onCue);
|
|
499
|
+
this.detachCaptions = () => {
|
|
500
|
+
textTrack.removeEventListener('cuechange', onCue);
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Applies React's conditional DOM: the header and each span render only when
|
|
505
|
+
* React would render them, and the visualizer's projected nodes sit bare in
|
|
506
|
+
* the wrapper's place.
|
|
507
|
+
*/
|
|
508
|
+
reconcile() {
|
|
509
|
+
this.syncRange();
|
|
510
|
+
const showTitle = this.hasTitleSlot || this.shows(this.title);
|
|
511
|
+
const showSubtitle = this.hasSubtitleSlot || this.shows(this.subtitle);
|
|
512
|
+
const header = this.headerRef?.nativeElement;
|
|
513
|
+
if (header) {
|
|
514
|
+
if (showTitle || showSubtitle) {
|
|
515
|
+
this.slots.sync(header, true);
|
|
516
|
+
this.slots.sync(this.titleSpanRef?.nativeElement, showTitle, header);
|
|
517
|
+
this.slots.sync(this.subtitleSpanRef?.nativeElement, showSubtitle, header);
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
this.slots.sync(header, false);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
this.reconcileVisualizer();
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Keeps the range input's live value on React's controlled value (its
|
|
527
|
+
* state): an attribute binding alone does not re-write the property after a
|
|
528
|
+
* user edit, and React re-writes the property on every render.
|
|
529
|
+
*/
|
|
530
|
+
syncRange() {
|
|
531
|
+
const input = this.host.querySelector('input[type="range"]');
|
|
532
|
+
if (input && input.value !== String(this.current))
|
|
533
|
+
input.value = String(this.current);
|
|
534
|
+
}
|
|
535
|
+
/** Applies React's visualizer DOM: the wrapper is unwrapped, or kept out of the DOM empty. */
|
|
536
|
+
reconcileVisualizer() {
|
|
537
|
+
const wrapper = this.visualizerSlotRef?.nativeElement;
|
|
538
|
+
if (!wrapper)
|
|
539
|
+
return;
|
|
540
|
+
if (!this.hasVisualizerSlot) {
|
|
541
|
+
if (this.visualizerOpen) {
|
|
542
|
+
for (const node of this.visualizerChildren)
|
|
543
|
+
wrapper.appendChild(node);
|
|
544
|
+
this.visualizerChildren = [];
|
|
545
|
+
this.visualizerOpen = false;
|
|
546
|
+
}
|
|
547
|
+
else if (wrapper.isConnected) {
|
|
548
|
+
wrapper.parentNode?.removeChild(wrapper);
|
|
549
|
+
}
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (this.visualizerOpen)
|
|
553
|
+
return;
|
|
554
|
+
const parent = wrapper.parentNode;
|
|
555
|
+
if (!parent)
|
|
556
|
+
return;
|
|
557
|
+
parent.insertBefore(this.renderer.createComment('slot'), wrapper);
|
|
558
|
+
this.visualizerChildren = Array.from(wrapper.childNodes);
|
|
559
|
+
for (const node of this.visualizerChildren)
|
|
560
|
+
parent.insertBefore(node, wrapper);
|
|
561
|
+
parent.removeChild(wrapper);
|
|
562
|
+
this.visualizerOpen = true;
|
|
563
|
+
}
|
|
564
|
+
static ɵfac = function AudioPlayer_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AudioPlayer)(); };
|
|
565
|
+
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: AudioPlayer, selectors: [["div", "mol-audio-player", ""]], viewQuery: function AudioPlayer_Query(rf, ctx) { if (rf & 1) {
|
|
566
|
+
i0.ɵɵviewQuery(_c0, 5)(_c1, 5)(_c2, 5)(_c3, 5)(_c4, 5)(_c5, 5);
|
|
567
|
+
} if (rf & 2) {
|
|
568
|
+
let _t;
|
|
569
|
+
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.audioRef = _t.first);
|
|
570
|
+
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.trackRef = _t.first);
|
|
571
|
+
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.headerRef = _t.first);
|
|
572
|
+
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.titleSpanRef = _t.first);
|
|
573
|
+
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.subtitleSpanRef = _t.first);
|
|
574
|
+
i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.visualizerSlotRef = _t.first);
|
|
575
|
+
} }, hostVars: 2, hostBindings: function AudioPlayer_HostBindings(rf, ctx) { if (rf & 2) {
|
|
576
|
+
i0.ɵɵclassMap(ctx.classes());
|
|
577
|
+
} }, inputs: { src: "src", title: "title", subtitle: "subtitle", visualizer: "visualizer", captions: "captions", captionsLang: "captionsLang", captionsLabel: "captionsLabel", defaultMuted: "defaultMuted", autoPlay: "autoPlay", onPlay: "onPlay", onPause: "onPause", onEnded: "onEnded", className: "className" }, features: [i0.ɵɵNgOnChangesFeature], ngContentSelectors: _c7, decls: 32, vars: 39, consts: [["audio", ""], ["track", ""], ["header", ""], ["titleSpan", ""], ["subtitleSpan", ""], ["visualizerSlot", ""], ["preload", "metadata", 2, "display", "none", 3, "muted"], ["kind", "captions"], [3, "ngTemplateOutlet"], ["mol-button", "", "data-mol-id", "audio-player-toggle", 3, "click", "variant", "size"], ["aria-hidden", "true"], [2, "min-width", "64px"], ["type", "range", "data-mol-id", "audio-player-seek", 3, "input"], ["mol-button", "", "data-mol-id", "audio-player-mute", 3, "click", "variant", "size"], ["aria-live", "polite", "data-mol-id", "audio-player-captions", 2, "min-height", "1.5em", "white-space", "pre-line", 3, "class"], ["aria-live", "polite", "data-mol-id", "audio-player-captions", 2, "min-height", "1.5em", "white-space", "pre-line"]], template: function AudioPlayer_Template(rf, ctx) { if (rf & 1) {
|
|
578
|
+
i0.ɵɵprojectionDef(_c6);
|
|
579
|
+
i0.ɵɵelementStart(0, "audio", 6, 0);
|
|
580
|
+
i0.ɵɵelement(2, "track", 7, 1);
|
|
581
|
+
i0.ɵɵelementEnd();
|
|
582
|
+
i0.ɵɵelementStart(4, "header", null, 2)(6, "span", null, 3);
|
|
583
|
+
i0.ɵɵprojection(8);
|
|
584
|
+
i0.ɵɵconditionalCreate(9, AudioPlayer_Conditional_9_Template, 1, 1, "ng-container", 8);
|
|
585
|
+
i0.ɵɵconditionalCreate(10, AudioPlayer_Conditional_10_Template, 1, 1);
|
|
586
|
+
i0.ɵɵelementEnd();
|
|
587
|
+
i0.ɵɵelementStart(11, "span", null, 4);
|
|
588
|
+
i0.ɵɵprojection(13, 1);
|
|
589
|
+
i0.ɵɵconditionalCreate(14, AudioPlayer_Conditional_14_Template, 1, 1, "ng-container", 8);
|
|
590
|
+
i0.ɵɵconditionalCreate(15, AudioPlayer_Conditional_15_Template, 1, 1);
|
|
591
|
+
i0.ɵɵelementEnd()();
|
|
592
|
+
i0.ɵɵelementStart(16, "span", null, 5);
|
|
593
|
+
i0.ɵɵprojection(18, 2);
|
|
594
|
+
i0.ɵɵelementEnd();
|
|
595
|
+
i0.ɵɵconditionalCreate(19, AudioPlayer_Conditional_19_Template, 1, 1, "ng-container", 8);
|
|
596
|
+
i0.ɵɵconditionalCreate(20, AudioPlayer_Conditional_20_Template, 1, 1);
|
|
597
|
+
i0.ɵɵelementStart(21, "div")(22, "button", 9);
|
|
598
|
+
i0.ɵɵlistener("click", function AudioPlayer_Template_button_click_22_listener() { return ctx.toggle(); });
|
|
599
|
+
i0.ɵɵelementStart(23, "span", 10);
|
|
600
|
+
i0.ɵɵtext(24);
|
|
601
|
+
i0.ɵɵelementEnd()();
|
|
602
|
+
i0.ɵɵelementStart(25, "span", 11);
|
|
603
|
+
i0.ɵɵtext(26);
|
|
604
|
+
i0.ɵɵelementEnd();
|
|
605
|
+
i0.ɵɵelementStart(27, "input", 12);
|
|
606
|
+
i0.ɵɵlistener("input", function AudioPlayer_Template_input_input_27_listener($event) { return ctx.onSeek($event); });
|
|
607
|
+
i0.ɵɵelementEnd();
|
|
608
|
+
i0.ɵɵelementStart(28, "button", 13);
|
|
609
|
+
i0.ɵɵlistener("click", function AudioPlayer_Template_button_click_28_listener() { return ctx.toggleMute(); });
|
|
610
|
+
i0.ɵɵelementStart(29, "span", 10);
|
|
611
|
+
i0.ɵɵtext(30);
|
|
612
|
+
i0.ɵɵelementEnd()()();
|
|
613
|
+
i0.ɵɵconditionalCreate(31, AudioPlayer_Conditional_31_Template, 2, 3, "p", 14);
|
|
614
|
+
} if (rf & 2) {
|
|
615
|
+
i0.ɵɵproperty("muted", ctx.muted);
|
|
616
|
+
i0.ɵɵattribute("src", ctx.src)("autoplay", ctx.autoPlay ? "" : null);
|
|
617
|
+
i0.ɵɵadvance(2);
|
|
618
|
+
i0.ɵɵattribute("src", ctx.captions)("srclang", ctx.captionsLang)("label", ctx.trackLabel());
|
|
619
|
+
i0.ɵɵadvance(2);
|
|
620
|
+
i0.ɵɵclassMap(ctx.stackClasses());
|
|
621
|
+
i0.ɵɵadvance(2);
|
|
622
|
+
i0.ɵɵclassMap(ctx.titleClasses());
|
|
623
|
+
i0.ɵɵadvance(3);
|
|
624
|
+
i0.ɵɵconditional(ctx.isTemplate(ctx.title) ? 9 : -1);
|
|
625
|
+
i0.ɵɵadvance();
|
|
626
|
+
i0.ɵɵconditional(ctx.titleText() ? 10 : -1);
|
|
627
|
+
i0.ɵɵadvance();
|
|
628
|
+
i0.ɵɵclassMap(ctx.subtitleClasses());
|
|
629
|
+
i0.ɵɵadvance(3);
|
|
630
|
+
i0.ɵɵconditional(ctx.isTemplate(ctx.subtitle) ? 14 : -1);
|
|
631
|
+
i0.ɵɵadvance();
|
|
632
|
+
i0.ɵɵconditional(ctx.subtitleText() ? 15 : -1);
|
|
633
|
+
i0.ɵɵadvance(4);
|
|
634
|
+
i0.ɵɵconditional(ctx.isTemplate(ctx.visualizer) ? 19 : -1);
|
|
635
|
+
i0.ɵɵadvance();
|
|
636
|
+
i0.ɵɵconditional(ctx.visualizerText() ? 20 : -1);
|
|
637
|
+
i0.ɵɵadvance();
|
|
638
|
+
i0.ɵɵclassMap(ctx.controlsClasses());
|
|
639
|
+
i0.ɵɵadvance();
|
|
640
|
+
i0.ɵɵproperty("variant", "ghost")("size", "sm");
|
|
641
|
+
i0.ɵɵattribute("aria-label", ctx.toggleLabel());
|
|
642
|
+
i0.ɵɵadvance(2);
|
|
643
|
+
i0.ɵɵtextInterpolate(ctx.playing ? "\u23F8" : "\u25B6");
|
|
644
|
+
i0.ɵɵadvance();
|
|
645
|
+
i0.ɵɵclassMap(ctx.timeClasses());
|
|
646
|
+
i0.ɵɵadvance();
|
|
647
|
+
i0.ɵɵtextInterpolate(ctx.timeText());
|
|
648
|
+
i0.ɵɵadvance();
|
|
649
|
+
i0.ɵɵclassMap(ctx.seekClasses());
|
|
650
|
+
i0.ɵɵattribute("min", 0)("max", ctx.duration || 0)("step", 0.1)("value", ctx.current)("aria-label", ctx.seekLabel());
|
|
651
|
+
i0.ɵɵadvance();
|
|
652
|
+
i0.ɵɵproperty("variant", "ghost")("size", "sm");
|
|
653
|
+
i0.ɵɵattribute("aria-label", ctx.muteLabel());
|
|
654
|
+
i0.ɵɵadvance(2);
|
|
655
|
+
i0.ɵɵtextInterpolate(ctx.muted ? "\uD83D\uDD07" : "\uD83D\uDD0A");
|
|
656
|
+
i0.ɵɵadvance();
|
|
657
|
+
i0.ɵɵconditional(ctx.captions ? 31 : -1);
|
|
658
|
+
} }, dependencies: [Button, NgTemplateOutlet], encapsulation: 2 });
|
|
659
|
+
}
|
|
660
|
+
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AudioPlayer, [{
|
|
661
|
+
type: Component,
|
|
662
|
+
args: [{
|
|
663
|
+
selector: 'div[mol-audio-player]',
|
|
664
|
+
standalone: true,
|
|
665
|
+
imports: [Button, NgTemplateOutlet],
|
|
666
|
+
inputs: [
|
|
667
|
+
'src',
|
|
668
|
+
'title',
|
|
669
|
+
'subtitle',
|
|
670
|
+
'visualizer',
|
|
671
|
+
'captions',
|
|
672
|
+
'captionsLang',
|
|
673
|
+
'captionsLabel',
|
|
674
|
+
'defaultMuted',
|
|
675
|
+
'autoPlay',
|
|
676
|
+
'onPlay',
|
|
677
|
+
'onPause',
|
|
678
|
+
'onEnded',
|
|
679
|
+
'className',
|
|
680
|
+
],
|
|
681
|
+
host: {
|
|
682
|
+
'[class]': 'classes()',
|
|
683
|
+
},
|
|
684
|
+
// prettier-ignore
|
|
685
|
+
template: `<audio #audio [attr.src]="src" [attr.autoplay]="autoPlay ? '' : null" [muted]="muted" preload="metadata" style="display: none"><track #track kind="captions" [attr.src]="captions" [attr.srclang]="captionsLang" [attr.label]="trackLabel()" /></audio><header #header [class]="stackClasses()"><span #titleSpan [class]="titleClasses()"><ng-content select="[slot=title]"></ng-content>@if (isTemplate(title)) {<ng-container [ngTemplateOutlet]="asTemplate(title)" />}@if (titleText()) {{{ titleText() }}}</span><span #subtitleSpan [class]="subtitleClasses()"><ng-content select="[slot=subtitle]"></ng-content>@if (isTemplate(subtitle)) {<ng-container [ngTemplateOutlet]="asTemplate(subtitle)" />}@if (subtitleText()) {{{ subtitleText() }}}</span></header><span #visualizerSlot><ng-content select="[slot=visualizer]"></ng-content></span>@if (isTemplate(visualizer)) {<ng-container [ngTemplateOutlet]="asTemplate(visualizer)" />}@if (visualizerText()) {{{ visualizerText() }}}<div [class]="controlsClasses()"><button mol-button [variant]="'ghost'" [size]="'sm'" (click)="toggle()" data-mol-id="audio-player-toggle" [attr.aria-label]="toggleLabel()"><span aria-hidden="true">{{ playing ? '⏸' : '▶' }}</span></button><span [class]="timeClasses()" style="min-width: 64px">{{ timeText() }}</span><input type="range" [attr.min]="0" [attr.max]="duration || 0" [attr.step]="0.1" [attr.value]="current" (input)="onSeek($event)" [attr.aria-label]="seekLabel()" data-mol-id="audio-player-seek" [class]="seekClasses()" /><button mol-button [variant]="'ghost'" [size]="'sm'" (click)="toggleMute()" data-mol-id="audio-player-mute" [attr.aria-label]="muteLabel()"><span aria-hidden="true">{{ muted ? '🔇' : '🔊' }}</span></button></div>@if (captions) {<p aria-live="polite" data-mol-id="audio-player-captions" [class]="captionClasses()" style="min-height: 1.5em; white-space: pre-line">{{ caption }}</p>}`,
|
|
686
|
+
}]
|
|
687
|
+
}], null, { audioRef: [{
|
|
688
|
+
type: ViewChild,
|
|
689
|
+
args: ['audio']
|
|
690
|
+
}], trackRef: [{
|
|
691
|
+
type: ViewChild,
|
|
692
|
+
args: ['track']
|
|
693
|
+
}], headerRef: [{
|
|
694
|
+
type: ViewChild,
|
|
695
|
+
args: ['header']
|
|
696
|
+
}], titleSpanRef: [{
|
|
697
|
+
type: ViewChild,
|
|
698
|
+
args: ['titleSpan']
|
|
699
|
+
}], subtitleSpanRef: [{
|
|
700
|
+
type: ViewChild,
|
|
701
|
+
args: ['subtitleSpan']
|
|
702
|
+
}], visualizerSlotRef: [{
|
|
703
|
+
type: ViewChild,
|
|
704
|
+
args: ['visualizerSlot']
|
|
705
|
+
}] }); })();
|
|
706
|
+
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(AudioPlayer, { className: "AudioPlayer", filePath: "audio-player.component.ts", lineNumber: 111 }); })();
|
|
707
|
+
//# sourceMappingURL=audio-player.component.js.map
|