@arraypress/waveform-player 1.19.0 → 1.19.1
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/waveform-player.cjs +59 -15
- package/dist/waveform-player.cjs.map +2 -2
- package/dist/waveform-player.esm.js +8 -8
- package/dist/waveform-player.esm.js.map +3 -3
- package/dist/waveform-player.js +59 -15
- package/dist/waveform-player.min.js +8 -8
- package/dist/waveform-player.min.js.map +3 -3
- package/package.json +1 -1
- package/src/js/core.js +56 -24
package/package.json
CHANGED
package/src/js/core.js
CHANGED
|
@@ -295,12 +295,12 @@ export class WaveformPlayer {
|
|
|
295
295
|
` : ''}
|
|
296
296
|
${this.options.showPlaybackSpeed ? `
|
|
297
297
|
<div class="waveform-speed">
|
|
298
|
-
<button class="speed-btn" aria-label="Playback speed" aria-haspopup="
|
|
298
|
+
<button class="speed-btn" aria-label="Playback speed" aria-haspopup="menu" aria-expanded="false">
|
|
299
299
|
<span class="speed-value">1x</span>
|
|
300
300
|
</button>
|
|
301
|
-
<div class="speed-menu" style="display: none;">
|
|
301
|
+
<div class="speed-menu" role="menu" aria-label="Playback speed" style="display: none;">
|
|
302
302
|
${this.options.playbackRates.map(rate =>
|
|
303
|
-
`<button class="speed-option" data-rate="${rate}">${rate}x</button>`
|
|
303
|
+
`<button class="speed-option" role="menuitemradio" tabindex="-1" aria-checked="false" data-rate="${rate}">${rate}x</button>`
|
|
304
304
|
).join('')}
|
|
305
305
|
</div>
|
|
306
306
|
</div>
|
|
@@ -427,40 +427,68 @@ export class WaveformPlayer {
|
|
|
427
427
|
|
|
428
428
|
if (!speedBtn || !speedMenu) return;
|
|
429
429
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
430
|
+
const options = () => Array.from(speedMenu.querySelectorAll('.speed-option'));
|
|
431
|
+
const isOpen = () => speedMenu.style.display !== 'none';
|
|
432
|
+
|
|
433
|
+
// Open/close the menu (a proper role="menu"): keep aria-expanded in sync
|
|
434
|
+
// and, on open, move focus into the menu — the checked rate, else the
|
|
435
|
+
// first option — so arrow keys drive it.
|
|
433
436
|
const setSpeedMenu = (open) => {
|
|
434
437
|
speedMenu.style.display = open ? 'block' : 'none';
|
|
435
438
|
speedBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
|
|
439
|
+
if (open) {
|
|
440
|
+
const opts = options();
|
|
441
|
+
(opts.find((o) => o.getAttribute('aria-checked') === 'true') || opts[0])?.focus();
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
const focusOption = (i) => {
|
|
446
|
+
const opts = options();
|
|
447
|
+
if (opts.length) opts[(i + opts.length) % opts.length].focus();
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const selectOption = (opt) => {
|
|
451
|
+
this.setPlaybackRate(parseFloat(opt.dataset.rate));
|
|
452
|
+
setSpeedMenu(false);
|
|
453
|
+
speedBtn.focus();
|
|
436
454
|
};
|
|
437
455
|
|
|
438
|
-
// Toggle
|
|
456
|
+
// Toggle from the trigger.
|
|
439
457
|
speedBtn.addEventListener('click', (e) => {
|
|
440
458
|
e.stopPropagation();
|
|
441
|
-
setSpeedMenu(
|
|
459
|
+
setSpeedMenu(!isOpen());
|
|
442
460
|
}, {signal: this._ac.signal});
|
|
443
461
|
|
|
444
|
-
// Close
|
|
462
|
+
// Close when clicking outside.
|
|
445
463
|
document.addEventListener('click', () => setSpeedMenu(false), {signal: this._ac.signal});
|
|
446
464
|
|
|
447
|
-
//
|
|
448
|
-
// the trigger.
|
|
465
|
+
// Pointer + native-button (Enter/Space on an option) selection.
|
|
449
466
|
speedMenu.addEventListener('click', (e) => {
|
|
450
467
|
e.stopPropagation();
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
setSpeedMenu(false);
|
|
454
|
-
speedBtn.focus();
|
|
455
|
-
}
|
|
468
|
+
const opt = e.target.closest('.speed-option');
|
|
469
|
+
if (opt) selectOption(opt);
|
|
456
470
|
}, {signal: this._ac.signal});
|
|
457
471
|
|
|
458
|
-
//
|
|
459
|
-
//
|
|
472
|
+
// Keyboard menu: arrows/Home/End roam, Enter/Space select (via the
|
|
473
|
+
// native button click above), Escape/Tab close. ArrowDown/Up on the
|
|
474
|
+
// trigger opens and focuses the first/checked option.
|
|
460
475
|
speedBtn.closest('.waveform-speed')?.addEventListener('keydown', (e) => {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
476
|
+
const opts = options();
|
|
477
|
+
const i = opts.indexOf(document.activeElement);
|
|
478
|
+
if (!isOpen()) {
|
|
479
|
+
if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && document.activeElement === speedBtn) {
|
|
480
|
+
e.preventDefault();
|
|
481
|
+
setSpeedMenu(true);
|
|
482
|
+
}
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
switch (e.key) {
|
|
486
|
+
case 'ArrowDown': e.preventDefault(); focusOption(i < 0 ? 0 : i + 1); break;
|
|
487
|
+
case 'ArrowUp': e.preventDefault(); focusOption(i < 0 ? opts.length - 1 : i - 1); break;
|
|
488
|
+
case 'Home': e.preventDefault(); focusOption(0); break;
|
|
489
|
+
case 'End': e.preventDefault(); focusOption(opts.length - 1); break;
|
|
490
|
+
case 'Escape': e.preventDefault(); setSpeedMenu(false); speedBtn.focus(); break;
|
|
491
|
+
case 'Tab': setSpeedMenu(false); break;
|
|
464
492
|
}
|
|
465
493
|
}, {signal: this._ac.signal});
|
|
466
494
|
|
|
@@ -761,7 +789,10 @@ export class WaveformPlayer {
|
|
|
761
789
|
_updateMediaSession(state) {
|
|
762
790
|
if (!('mediaSession' in navigator) || !this.options.enableMediaSession || !this.audio) return;
|
|
763
791
|
try {
|
|
764
|
-
|
|
792
|
+
// Claim the (global, singleton) Media Session for THIS player on play —
|
|
793
|
+
// (re)register our action handlers + metadata so the lock-screen controls
|
|
794
|
+
// drive the player you're actually listening to, not the last one loaded.
|
|
795
|
+
if (state === 'playing') this.initMediaSession();
|
|
765
796
|
navigator.mediaSession.playbackState = state;
|
|
766
797
|
const d = this.audio.duration;
|
|
767
798
|
if (navigator.mediaSession.setPositionState && d && isFinite(d)) {
|
|
@@ -982,7 +1013,6 @@ export class WaveformPlayer {
|
|
|
982
1013
|
|
|
983
1014
|
this.drawWaveform();
|
|
984
1015
|
this.renderMarkers();
|
|
985
|
-
this.initMediaSession();
|
|
986
1016
|
|
|
987
1017
|
// Fire callback
|
|
988
1018
|
if (this.options.onLoad) {
|
|
@@ -1806,7 +1836,9 @@ export class WaveformPlayer {
|
|
|
1806
1836
|
|
|
1807
1837
|
// Update active state in menu
|
|
1808
1838
|
this.container.querySelectorAll('.speed-option').forEach(btn => {
|
|
1809
|
-
|
|
1839
|
+
const on = parseFloat(btn.dataset.rate) === this.audio.playbackRate;
|
|
1840
|
+
btn.classList.toggle('active', on);
|
|
1841
|
+
btn.setAttribute('aria-checked', on ? 'true' : 'false');
|
|
1810
1842
|
});
|
|
1811
1843
|
}
|
|
1812
1844
|
|