@arraypress/waveform-player 1.16.1 → 1.18.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/README.md +4 -4
- package/dist/waveform-player.cjs +264 -148
- package/dist/waveform-player.cjs.map +2 -2
- package/dist/waveform-player.css +1 -1
- package/dist/waveform-player.esm.js +13 -17
- package/dist/waveform-player.esm.js.map +3 -3
- package/dist/waveform-player.js +264 -148
- package/dist/waveform-player.min.js +13 -17
- package/dist/waveform-player.min.js.map +3 -3
- package/index.d.ts +10 -11
- package/package.json +1 -1
- package/src/css/waveform-player.css +47 -3
- package/src/js/audio.js +9 -9
- package/src/js/core.js +253 -88
- package/src/js/drawing.js +59 -58
- package/src/js/themes.js +19 -24
- package/src/js/utils.js +27 -7
package/src/js/core.js
CHANGED
|
@@ -13,10 +13,11 @@ import {
|
|
|
13
13
|
mergeOptions,
|
|
14
14
|
debounce,
|
|
15
15
|
clamp,
|
|
16
|
-
escapeHtml
|
|
16
|
+
escapeHtml,
|
|
17
|
+
DEFAULT_SAMPLES
|
|
17
18
|
} from './utils.js';
|
|
18
19
|
|
|
19
|
-
import {DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS} from './themes.js';
|
|
20
|
+
import {DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS, detectColorScheme} from './themes.js';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Placeholder shown when an artwork URL fails to load (404 / broken) — a muted
|
|
@@ -31,6 +32,12 @@ const ARTWORK_FALLBACK = 'data:image/svg+xml,' + encodeURIComponent(
|
|
|
31
32
|
const SEEK_STEP_SECONDS = 5;
|
|
32
33
|
const SEEK_PAGE_SECONDS = 10;
|
|
33
34
|
|
|
35
|
+
// Controls that own their own focus. A click landing on one of these (or a
|
|
36
|
+
// descendant) must leave focus on the control, not pull it onto the player
|
|
37
|
+
// container — otherwise activating the play button/slider steals focus onto the
|
|
38
|
+
// wrapper, moving it off the control the user just operated.
|
|
39
|
+
const INTERACTIVE_ELEMENTS = 'button, a[href], input, [role="slider"]';
|
|
40
|
+
|
|
34
41
|
/**
|
|
35
42
|
* WaveformPlayer - Modern audio player with waveform visualization
|
|
36
43
|
* @class
|
|
@@ -89,6 +96,9 @@ export class WaveformPlayer {
|
|
|
89
96
|
// explicit colorPreset or hand-set colour is never overridden.
|
|
90
97
|
this._autoTheme = this.options.colorPreset == null || !COLOR_PRESETS[this.options.colorPreset];
|
|
91
98
|
this._presetKeys = [];
|
|
99
|
+
// Resolved scheme name for the CSS theme class (CSS provides the DOM
|
|
100
|
+
// chrome colours via --wfp-* variables).
|
|
101
|
+
this._scheme = (this.options.colorPreset && COLOR_PRESETS[this.options.colorPreset]) ? this.options.colorPreset : detectColorScheme();
|
|
92
102
|
|
|
93
103
|
// Apply preset colors only if individual colors aren't explicitly set
|
|
94
104
|
for (const [key, value] of Object.entries(preset)) {
|
|
@@ -223,7 +233,7 @@ export class WaveformPlayer {
|
|
|
223
233
|
*
|
|
224
234
|
* Clears the container, resolves button alignment (`auto` → `bottom` for
|
|
225
235
|
* the `bars` style, `center` otherwise), and conditionally renders the play
|
|
226
|
-
* button, info row (artwork/title/
|
|
236
|
+
* button, info row (artwork/title/artist), BPM badge, playback-speed
|
|
227
237
|
* menu, and time display based on the relevant `show*` options. Caches the
|
|
228
238
|
* canvas, controls, and text elements onto `this`, then sizes the canvas.
|
|
229
239
|
* @private
|
|
@@ -251,14 +261,11 @@ export class WaveformPlayer {
|
|
|
251
261
|
if (isPreview) {
|
|
252
262
|
this.container.classList.add('waveform-layout-preview');
|
|
253
263
|
}
|
|
264
|
+
this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');
|
|
254
265
|
|
|
255
266
|
// Build play button HTML (conditional)
|
|
256
267
|
const buttonHTML = this.options.showControls ? `
|
|
257
|
-
<button class="waveform-btn${this.options.buttonStyle === 'minimal' ? ' waveform-btn-minimal' : ''}" aria-label="Play/Pause" style="
|
|
258
|
-
border-color: ${this.options.buttonColor};
|
|
259
|
-
color: ${this.options.buttonColor};
|
|
260
|
-
${this.options.buttonSize != null ? `--wfp-btn-size: ${typeof this.options.buttonSize === 'number' ? `${this.options.buttonSize}px` : this.options.buttonSize};` : ''}
|
|
261
|
-
">
|
|
268
|
+
<button class="waveform-btn${this.options.buttonStyle === 'minimal' ? ' waveform-btn-minimal' : ''}" aria-label="Play/Pause"${this.options.buttonSize != null ? ` style="--wfp-btn-size: ${typeof this.options.buttonSize === 'number' ? `${this.options.buttonSize}px` : this.options.buttonSize};"` : ''}>
|
|
262
269
|
<span class="waveform-icon-play">${this.options.playIcon}</span>
|
|
263
270
|
<span class="waveform-icon-pause" style="display:none;">${this.options.pauseIcon}</span>
|
|
264
271
|
</button>
|
|
@@ -277,18 +284,18 @@ export class WaveformPlayer {
|
|
|
277
284
|
">
|
|
278
285
|
` : ''}
|
|
279
286
|
<div class="waveform-text">
|
|
280
|
-
<span class="waveform-title"
|
|
281
|
-
${this.options.
|
|
287
|
+
<span class="waveform-title"></span>
|
|
288
|
+
${this.options.artist ? `<span class="waveform-artist">${this.options.artist}</span>` : ''}
|
|
282
289
|
</div>
|
|
283
290
|
<div class="waveform-meta" style="display: flex; align-items: center; gap: 1rem;">
|
|
284
291
|
${this.options.showBPM ? `
|
|
285
|
-
<span class="waveform-bpm" style="
|
|
292
|
+
<span class="waveform-bpm" style="display: none;">
|
|
286
293
|
<span class="bpm-value">--</span> BPM
|
|
287
294
|
</span>
|
|
288
295
|
` : ''}
|
|
289
296
|
${this.options.showPlaybackSpeed ? `
|
|
290
297
|
<div class="waveform-speed">
|
|
291
|
-
<button class="speed-btn" aria-label="Playback speed">
|
|
298
|
+
<button class="speed-btn" aria-label="Playback speed" aria-haspopup="true" aria-expanded="false">
|
|
292
299
|
<span class="speed-value">1x</span>
|
|
293
300
|
</button>
|
|
294
301
|
<div class="speed-menu" style="display: none;">
|
|
@@ -299,7 +306,7 @@ export class WaveformPlayer {
|
|
|
299
306
|
</div>
|
|
300
307
|
` : ''}
|
|
301
308
|
${this.options.showTime ? `
|
|
302
|
-
<span class="waveform-time"
|
|
309
|
+
<span class="waveform-time">
|
|
303
310
|
<span class="time-current">0:00</span> / <span class="time-total">0:00</span>
|
|
304
311
|
</span>
|
|
305
312
|
` : ''}
|
|
@@ -334,7 +341,7 @@ export class WaveformPlayer {
|
|
|
334
341
|
this.canvas = this.container.querySelector('canvas');
|
|
335
342
|
this.ctx = this.canvas.getContext('2d');
|
|
336
343
|
this.titleEl = this.container.querySelector('.waveform-title');
|
|
337
|
-
this.
|
|
344
|
+
this.artistEl = this.container.querySelector('.waveform-artist');
|
|
338
345
|
this.artworkEl = this.container.querySelector('.waveform-artwork');
|
|
339
346
|
if (this.artworkEl) {
|
|
340
347
|
// Fall back to the placeholder tile if the cover URL fails to load.
|
|
@@ -407,8 +414,9 @@ export class WaveformPlayer {
|
|
|
407
414
|
|
|
408
415
|
/**
|
|
409
416
|
* Wire up the playback-speed menu: toggle it open on the speed button,
|
|
410
|
-
* close it on
|
|
411
|
-
*
|
|
417
|
+
* close it on outside click or Escape (keeping `aria-expanded` on the
|
|
418
|
+
* trigger in sync), and apply the chosen rate when a `.speed-option` is
|
|
419
|
+
* clicked, returning focus to the trigger. All listeners are registered against the
|
|
412
420
|
* instance `AbortController` signal so {@link WaveformPlayer#destroy} tears
|
|
413
421
|
* them down. No-op if the speed elements are absent.
|
|
414
422
|
* @private
|
|
@@ -419,24 +427,40 @@ export class WaveformPlayer {
|
|
|
419
427
|
|
|
420
428
|
if (!speedBtn || !speedMenu) return;
|
|
421
429
|
|
|
430
|
+
// Open/close the menu, keeping aria-expanded on the trigger in sync so
|
|
431
|
+
// assistive tech announces the state. This is a disclosure, not a full
|
|
432
|
+
// ARIA menu — the options stay plain buttons in the tab order.
|
|
433
|
+
const setSpeedMenu = (open) => {
|
|
434
|
+
speedMenu.style.display = open ? 'block' : 'none';
|
|
435
|
+
speedBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
|
|
436
|
+
};
|
|
437
|
+
|
|
422
438
|
// Toggle menu
|
|
423
439
|
speedBtn.addEventListener('click', (e) => {
|
|
424
440
|
e.stopPropagation();
|
|
425
|
-
speedMenu.style.display
|
|
441
|
+
setSpeedMenu(speedMenu.style.display === 'none');
|
|
426
442
|
}, {signal: this._ac.signal});
|
|
427
443
|
|
|
428
444
|
// Close menu when clicking outside
|
|
429
|
-
document.addEventListener('click', () => {
|
|
430
|
-
speedMenu.style.display = 'none';
|
|
431
|
-
}, {signal: this._ac.signal});
|
|
445
|
+
document.addEventListener('click', () => setSpeedMenu(false), {signal: this._ac.signal});
|
|
432
446
|
|
|
433
|
-
// Handle speed selection
|
|
447
|
+
// Handle speed selection — apply the rate, close, and return focus to
|
|
448
|
+
// the trigger.
|
|
434
449
|
speedMenu.addEventListener('click', (e) => {
|
|
435
450
|
e.stopPropagation();
|
|
436
451
|
if (e.target.classList.contains('speed-option')) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
452
|
+
this.setPlaybackRate(parseFloat(e.target.dataset.rate));
|
|
453
|
+
setSpeedMenu(false);
|
|
454
|
+
speedBtn.focus();
|
|
455
|
+
}
|
|
456
|
+
}, {signal: this._ac.signal});
|
|
457
|
+
|
|
458
|
+
// Escape closes the menu and restores focus to the trigger, whether
|
|
459
|
+
// focus is on the button or one of the options.
|
|
460
|
+
speedBtn.closest('.waveform-speed')?.addEventListener('keydown', (e) => {
|
|
461
|
+
if (e.key === 'Escape' && speedMenu.style.display !== 'none') {
|
|
462
|
+
setSpeedMenu(false);
|
|
463
|
+
speedBtn.focus();
|
|
440
464
|
}
|
|
441
465
|
}, {signal: this._ac.signal});
|
|
442
466
|
|
|
@@ -459,8 +483,12 @@ export class WaveformPlayer {
|
|
|
459
483
|
// Make container focusable but not in tab order by default
|
|
460
484
|
this.container.setAttribute('tabindex', '-1');
|
|
461
485
|
|
|
462
|
-
// Only activate keyboard controls when explicitly focused (clicked)
|
|
463
|
-
|
|
486
|
+
// Only activate keyboard controls when explicitly focused (clicked).
|
|
487
|
+
// Skip when the click landed on an interactive control (play button,
|
|
488
|
+
// link, slider, input): those own their focus, so pulling it onto the
|
|
489
|
+
// container would move focus off the control the user just activated.
|
|
490
|
+
this.container.addEventListener('click', (e) => {
|
|
491
|
+
if (e.target.closest(INTERACTIVE_ELEMENTS)) return;
|
|
464
492
|
// Remove focus from all other players
|
|
465
493
|
WaveformPlayer.getAllInstances().forEach(player => {
|
|
466
494
|
if (player !== this) {
|
|
@@ -673,7 +701,7 @@ export class WaveformPlayer {
|
|
|
673
701
|
// Set metadata
|
|
674
702
|
navigator.mediaSession.metadata = new MediaMetadata({
|
|
675
703
|
title: this.options.title || 'Unknown Track',
|
|
676
|
-
artist: this.options.
|
|
704
|
+
artist: this.options.artist || '',
|
|
677
705
|
album: this.options.album || '',
|
|
678
706
|
artwork: this.options.artwork ? [
|
|
679
707
|
{src: this.options.artwork, sizes: '512x512', type: 'image/jpeg'}
|
|
@@ -729,13 +757,72 @@ export class WaveformPlayer {
|
|
|
729
757
|
this.audio.addEventListener('error', (e) => this.onError(e));
|
|
730
758
|
}
|
|
731
759
|
|
|
732
|
-
// Canvas interactions — seek
|
|
733
|
-
//
|
|
734
|
-
//
|
|
760
|
+
// Canvas interactions — click + drag to seek. pointerdown starts a
|
|
761
|
+
// scrub (with pointer capture so moves keep flowing if the cursor
|
|
762
|
+
// leaves the canvas), pointermove updates it while held, pointerup /
|
|
763
|
+
// pointercancel end it. The click handler stays for keyboard/synthetic
|
|
764
|
+
// compatibility. In external mode each seek dispatches a cancelable
|
|
765
|
+
// `waveformplayer:request-seek` so the controller positions its audio.
|
|
735
766
|
this.canvas.addEventListener('click', (e) => this.handleCanvasClick(e));
|
|
767
|
+
this._dragging = false;
|
|
768
|
+
this._seekHover = false;
|
|
769
|
+
this._handleNear = false;
|
|
770
|
+
|
|
771
|
+
// Hover state — reveals the handle + lifts the seekbar brightness. A
|
|
772
|
+
// redraw is needed because the canvas has no CSS :hover of its own.
|
|
773
|
+
this.canvas.addEventListener('pointerenter', () => {
|
|
774
|
+
this._seekHover = true;
|
|
775
|
+
this.drawWaveform();
|
|
776
|
+
this._updateSeekHandle();
|
|
777
|
+
});
|
|
778
|
+
this.canvas.addEventListener('pointerleave', () => {
|
|
779
|
+
this._seekHover = false;
|
|
780
|
+
this._handleNear = false;
|
|
781
|
+
if (!this._dragging) this._hideHoverTip();
|
|
782
|
+
this.drawWaveform();
|
|
783
|
+
this._updateSeekHandle();
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
// Drag = a VISUAL scrub: the playhead previews the cursor while the
|
|
787
|
+
// audio keeps playing untouched (a per-move seek glitches the audio and
|
|
788
|
+
// fights the progress loop). The real seek is committed once, on
|
|
789
|
+
// release. updateProgress() bails while _dragging so playback can't drag
|
|
790
|
+
// the handle off the cursor.
|
|
791
|
+
this.canvas.addEventListener('pointerdown', (e) => {
|
|
792
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return; // primary / touch only
|
|
793
|
+
this._dragging = true;
|
|
794
|
+
try { this.canvas.setPointerCapture(e.pointerId); } catch (err) { /* unsupported */ }
|
|
795
|
+
this._scrubTo(e.clientX);
|
|
796
|
+
});
|
|
797
|
+
this.canvas.addEventListener('pointermove', (e) => {
|
|
798
|
+
if (this._dragging) {
|
|
799
|
+
this._scrubTo(e.clientX);
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
// Hover (not dragging): grow the handle when the cursor is over it.
|
|
803
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
804
|
+
if (rect.width) {
|
|
805
|
+
this._handleNear = Math.abs((e.clientX - rect.left) - this.progress * rect.width) <= 10;
|
|
806
|
+
this._updateSeekHandle();
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
const endDrag = (e) => {
|
|
810
|
+
if (!this._dragging) return;
|
|
811
|
+
this._dragging = false;
|
|
812
|
+
try { this.canvas.releasePointerCapture(e.pointerId); } catch (err) { /* ignore */ }
|
|
813
|
+
this._seekFromPointer(e.clientX); // commit the seek now — audio jumps once
|
|
814
|
+
// Hide the drag tooltip on release, unless hover-time keeps it (or
|
|
815
|
+
// the seekbar handle is being hovered), to avoid a hide/show flicker.
|
|
816
|
+
if (!this._seekHover && !this.options.showHoverTime) this._hideHoverTip();
|
|
817
|
+
this._updateSeekHandle();
|
|
818
|
+
};
|
|
819
|
+
this.canvas.addEventListener('pointerup', endDrag);
|
|
820
|
+
this.canvas.addEventListener('pointercancel', endDrag);
|
|
736
821
|
|
|
737
822
|
// Hover-time tooltip over the waveform (when showHoverTime is on).
|
|
738
823
|
this.setupHoverTime();
|
|
824
|
+
// Draggable seek handle (DOM circle, shown on hover / drag).
|
|
825
|
+
this.setupSeekHandle();
|
|
739
826
|
|
|
740
827
|
// Window resize - store handler for cleanup
|
|
741
828
|
this.resizeHandler = debounce(() => this.resizeCanvas(), 100);
|
|
@@ -860,19 +947,19 @@ export class WaveformPlayer {
|
|
|
860
947
|
*
|
|
861
948
|
* Pauses any current playback, fully resets the audio element (self mode),
|
|
862
949
|
* clears error/marker/progress state, merges the new metadata into
|
|
863
|
-
* `this.options`, updates the
|
|
950
|
+
* `this.options`, updates the artist/artwork DOM, then calls
|
|
864
951
|
* {@link WaveformPlayer#load}. Auto-plays the new track unless
|
|
865
952
|
* `options.autoplay === false`.
|
|
866
953
|
* @param {string} url - Audio URL.
|
|
867
954
|
* @param {string|null} [title=null] - Track title; keeps the existing
|
|
868
955
|
* title when null.
|
|
869
|
-
* @param {string|null} [
|
|
870
|
-
* the
|
|
956
|
+
* @param {string|null} [artist=null] - Track artist; pass `''` to hide
|
|
957
|
+
* the artist row, or null to keep the existing one.
|
|
871
958
|
* @param {Object} [options={}] - Additional options to merge (e.g.
|
|
872
959
|
* `preload`, `artwork`, `markers`, `autoplay`).
|
|
873
960
|
* @returns {Promise<void>}
|
|
874
961
|
*/
|
|
875
|
-
async loadTrack(url, title = null,
|
|
962
|
+
async loadTrack(url, title = null, artist = null, options = {}) {
|
|
876
963
|
// Stop current playback and clear state
|
|
877
964
|
if (this.isPlaying) {
|
|
878
965
|
this.pause();
|
|
@@ -904,7 +991,7 @@ export class WaveformPlayer {
|
|
|
904
991
|
this.options = mergeOptions(this.options, {
|
|
905
992
|
url,
|
|
906
993
|
title: title || this.options.title,
|
|
907
|
-
|
|
994
|
+
artist: artist || this.options.artist,
|
|
908
995
|
...options
|
|
909
996
|
});
|
|
910
997
|
|
|
@@ -914,12 +1001,12 @@ export class WaveformPlayer {
|
|
|
914
1001
|
}
|
|
915
1002
|
|
|
916
1003
|
// Update UI elements
|
|
917
|
-
if (this.
|
|
918
|
-
if (
|
|
919
|
-
this.
|
|
920
|
-
this.
|
|
921
|
-
} else if (
|
|
922
|
-
this.
|
|
1004
|
+
if (this.artistEl) {
|
|
1005
|
+
if (artist) {
|
|
1006
|
+
this.artistEl.textContent = artist;
|
|
1007
|
+
this.artistEl.style.display = '';
|
|
1008
|
+
} else if (artist === '') {
|
|
1009
|
+
this.artistEl.style.display = 'none';
|
|
923
1010
|
}
|
|
924
1011
|
}
|
|
925
1012
|
|
|
@@ -1008,7 +1095,8 @@ export class WaveformPlayer {
|
|
|
1008
1095
|
...this.options,
|
|
1009
1096
|
waveformStyle: this.options.waveformStyle || 'bars',
|
|
1010
1097
|
color: this.options.waveformColor,
|
|
1011
|
-
progressColor: this.options.progressColor
|
|
1098
|
+
progressColor: this.options.progressColor,
|
|
1099
|
+
seekActive: this._seekHover || this._dragging
|
|
1012
1100
|
});
|
|
1013
1101
|
}
|
|
1014
1102
|
|
|
@@ -1154,30 +1242,109 @@ export class WaveformPlayer {
|
|
|
1154
1242
|
}
|
|
1155
1243
|
|
|
1156
1244
|
/**
|
|
1157
|
-
* Build
|
|
1158
|
-
*
|
|
1245
|
+
* Build the time tooltip that follows the pointer over the waveform. The
|
|
1246
|
+
* element is always created so **drag-scrub** can show the time you're
|
|
1247
|
+
* dragging to; the *hover* reveal is gated on `showHoverTime`. Works in both
|
|
1159
1248
|
* self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).
|
|
1160
|
-
* Uses the same pointer-X → percentage math as seeking.
|
|
1161
1249
|
* @private
|
|
1162
1250
|
*/
|
|
1163
1251
|
setupHoverTime() {
|
|
1164
|
-
if (!this.
|
|
1252
|
+
if (!this.seekEl) return;
|
|
1165
1253
|
const tip = document.createElement('div');
|
|
1166
1254
|
tip.className = 'waveform-hover-time';
|
|
1167
1255
|
tip.setAttribute('aria-hidden', 'true');
|
|
1168
1256
|
this.seekEl.appendChild(tip);
|
|
1169
1257
|
this.hoverTimeEl = tip;
|
|
1170
1258
|
|
|
1259
|
+
// Without showHoverTime the tip is drag-only — no hover listeners.
|
|
1260
|
+
if (!this.options.showHoverTime) return;
|
|
1261
|
+
|
|
1262
|
+
// Hover updates the tip; the drag handlers also call _updateHoverTip so
|
|
1263
|
+
// the time follows the playhead while scrubbing (Spotify-style). The
|
|
1264
|
+
// guard avoids a double-update when a drag is in progress (the canvas
|
|
1265
|
+
// pointermove already drives it then).
|
|
1171
1266
|
this.seekEl.addEventListener('pointermove', (e) => {
|
|
1172
|
-
|
|
1173
|
-
if (!dur) { tip.style.opacity = '0'; return; }
|
|
1174
|
-
const rect = this.canvas.getBoundingClientRect();
|
|
1175
|
-
const pct = clamp((e.clientX - rect.left) / rect.width);
|
|
1176
|
-
tip.textContent = formatTime(pct * dur);
|
|
1177
|
-
tip.style.left = (pct * 100) + '%';
|
|
1178
|
-
tip.style.opacity = '1';
|
|
1267
|
+
if (!this._dragging) this._updateHoverTip(e.clientX);
|
|
1179
1268
|
});
|
|
1180
|
-
this.seekEl.addEventListener('pointerleave', () => {
|
|
1269
|
+
this.seekEl.addEventListener('pointerleave', () => {
|
|
1270
|
+
if (!this._dragging) this._hideHoverTip();
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
/**
|
|
1275
|
+
* Position + fill the hover-time tooltip for a given client-X. Shared by
|
|
1276
|
+
* hover and drag-scrub. No-op when the tooltip isn't enabled
|
|
1277
|
+
* (`showHoverTime` off).
|
|
1278
|
+
* @param {number} clientX - Pointer client-X coordinate.
|
|
1279
|
+
* @private
|
|
1280
|
+
*/
|
|
1281
|
+
_updateHoverTip(clientX) {
|
|
1282
|
+
const tip = this.hoverTimeEl;
|
|
1283
|
+
if (!tip) return;
|
|
1284
|
+
const dur = this.getSeekDuration();
|
|
1285
|
+
if (!dur) { tip.style.opacity = '0'; return; }
|
|
1286
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
1287
|
+
const pct = clamp((clientX - rect.left) / rect.width);
|
|
1288
|
+
tip.textContent = formatTime(pct * dur);
|
|
1289
|
+
tip.style.left = (pct * 100) + '%';
|
|
1290
|
+
tip.style.opacity = '1';
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
/** Hide the hover-time tooltip. @private */
|
|
1294
|
+
_hideHoverTip() {
|
|
1295
|
+
if (this.hoverTimeEl) this.hoverTimeEl.style.opacity = '0';
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
/**
|
|
1299
|
+
* Visually preview a seek during a drag — moves the playhead + handle +
|
|
1300
|
+
* tooltip to the cursor WITHOUT touching the audio (playback continues; the
|
|
1301
|
+
* real seek is committed on release).
|
|
1302
|
+
* @param {number} clientX - Pointer client-X coordinate.
|
|
1303
|
+
* @private
|
|
1304
|
+
*/
|
|
1305
|
+
_scrubTo(clientX) {
|
|
1306
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
1307
|
+
if (!rect.width) return;
|
|
1308
|
+
this.progress = clamp((clientX - rect.left) / rect.width);
|
|
1309
|
+
this.drawWaveform();
|
|
1310
|
+
this._updateSeekHandle();
|
|
1311
|
+
this._updateHoverTip(clientX);
|
|
1312
|
+
// Live time readout while scrubbing (Spotify-style): the audio keeps
|
|
1313
|
+
// playing at its old position; the display shows where release lands.
|
|
1314
|
+
const dur = this.getSeekDuration();
|
|
1315
|
+
if (dur && this.currentTimeEl) {
|
|
1316
|
+
this.currentTimeEl.textContent = formatTime(this.progress * dur);
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
/**
|
|
1321
|
+
* Create the draggable seek handle — a DOM circle over the playhead, shown
|
|
1322
|
+
* only on hover / drag (Spotify-style). Pointer-events are off so it never
|
|
1323
|
+
* blocks the canvas scrub; size + visibility are CSS.
|
|
1324
|
+
* @private
|
|
1325
|
+
*/
|
|
1326
|
+
setupSeekHandle() {
|
|
1327
|
+
// Only the seekbar style — on a real waveform the fill-edge already is
|
|
1328
|
+
// the playhead, so a floating dot is just noise.
|
|
1329
|
+
if (!this.options.seekHandle || this.options.waveformStyle !== 'seekbar' || !this.seekEl) return;
|
|
1330
|
+
const h = document.createElement('div');
|
|
1331
|
+
h.className = 'waveform-seek-handle';
|
|
1332
|
+
h.setAttribute('aria-hidden', 'true');
|
|
1333
|
+
this.seekEl.appendChild(h);
|
|
1334
|
+
this.seekHandleEl = h;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* Position the seek handle at the current progress and toggle its
|
|
1339
|
+
* visibility (hover / drag) + expanded (drag / direct-hover) classes.
|
|
1340
|
+
* @private
|
|
1341
|
+
*/
|
|
1342
|
+
_updateSeekHandle() {
|
|
1343
|
+
const h = this.seekHandleEl;
|
|
1344
|
+
if (!h) return;
|
|
1345
|
+
h.style.left = (this.progress * 100) + '%';
|
|
1346
|
+
h.classList.toggle('is-visible', this._seekHover || this._dragging);
|
|
1347
|
+
h.classList.toggle('is-active', this._dragging || this._handleNear);
|
|
1181
1348
|
}
|
|
1182
1349
|
|
|
1183
1350
|
// ============================================
|
|
@@ -1197,15 +1364,24 @@ export class WaveformPlayer {
|
|
|
1197
1364
|
* @fires WaveformPlayer#waveformplayer:request-seek
|
|
1198
1365
|
*/
|
|
1199
1366
|
handleCanvasClick(event) {
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1367
|
+
this._seekFromPointer(event.clientX);
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
/**
|
|
1371
|
+
* Seek to a horizontal client-X position over the canvas — shared by
|
|
1372
|
+
* click and drag. In external mode the player has no audio of its own,
|
|
1373
|
+
* so it dispatches a cancelable `waveformplayer:request-seek` with the
|
|
1374
|
+
* target percentage (updating the local visual optimistically); in self
|
|
1375
|
+
* mode it seeks the owned `<audio>` via
|
|
1376
|
+
* {@link WaveformPlayer#seekToPercent}.
|
|
1377
|
+
* @param {number} clientX - Pointer client-X coordinate.
|
|
1378
|
+
* @private
|
|
1379
|
+
* @fires WaveformPlayer#waveformplayer:request-seek
|
|
1380
|
+
*/
|
|
1381
|
+
_seekFromPointer(clientX) {
|
|
1206
1382
|
const rect = this.canvas.getBoundingClientRect();
|
|
1207
|
-
|
|
1208
|
-
const targetPercent = clamp(
|
|
1383
|
+
if (!rect.width) return;
|
|
1384
|
+
const targetPercent = clamp((clientX - rect.left) / rect.width);
|
|
1209
1385
|
|
|
1210
1386
|
if (this.options.audioMode === 'external') {
|
|
1211
1387
|
this._requestSeek(targetPercent);
|
|
@@ -1442,12 +1618,16 @@ export class WaveformPlayer {
|
|
|
1442
1618
|
// Self-mode only — external mode receives progress via
|
|
1443
1619
|
// setProgress() from the controller and never calls this.
|
|
1444
1620
|
if (!this.audio || !this.audio.duration) return;
|
|
1621
|
+
// While dragging, the playhead previews the cursor — don't let playback
|
|
1622
|
+
// move it (the audio keeps playing; the seek commits on release).
|
|
1623
|
+
if (this._dragging) return;
|
|
1445
1624
|
|
|
1446
1625
|
const newProgress = this.audio.currentTime / this.audio.duration;
|
|
1447
1626
|
|
|
1448
1627
|
if (Math.abs(newProgress - this.progress) > 0.001) {
|
|
1449
1628
|
this.progress = newProgress;
|
|
1450
1629
|
this.drawWaveform();
|
|
1630
|
+
this._updateSeekHandle();
|
|
1451
1631
|
}
|
|
1452
1632
|
|
|
1453
1633
|
if (this.currentTimeEl) {
|
|
@@ -1498,34 +1678,22 @@ export class WaveformPlayer {
|
|
|
1498
1678
|
* @public
|
|
1499
1679
|
*/
|
|
1500
1680
|
refreshTheme() {
|
|
1501
|
-
if (!this._autoTheme
|
|
1681
|
+
if (!this._autoTheme) return;
|
|
1682
|
+
this._scheme = detectColorScheme();
|
|
1502
1683
|
const preset = getColorPreset(this.options.colorPreset);
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
if (this.options[key] !== preset[key]) {
|
|
1506
|
-
this.options[key] = preset[key];
|
|
1507
|
-
changed = true;
|
|
1508
|
-
}
|
|
1684
|
+
for (const key of (this._presetKeys || [])) {
|
|
1685
|
+
if (key in preset) this.options[key] = preset[key];
|
|
1509
1686
|
}
|
|
1510
|
-
|
|
1687
|
+
this._applyThemeColors();
|
|
1511
1688
|
}
|
|
1512
1689
|
|
|
1513
1690
|
/**
|
|
1514
|
-
*
|
|
1515
|
-
*
|
|
1691
|
+
* Sync the theme class (CSS drives the DOM chrome colours via `--wfp-*`
|
|
1692
|
+
* variables) and redraw the canvas with the resolved waveform colours.
|
|
1516
1693
|
* @private
|
|
1517
1694
|
*/
|
|
1518
1695
|
_applyThemeColors() {
|
|
1519
|
-
|
|
1520
|
-
if (this.playBtn) {
|
|
1521
|
-
this.playBtn.style.borderColor = o.buttonColor;
|
|
1522
|
-
this.playBtn.style.color = o.buttonColor;
|
|
1523
|
-
}
|
|
1524
|
-
if (this.titleEl) this.titleEl.style.color = o.textColor;
|
|
1525
|
-
if (this.subtitleEl) this.subtitleEl.style.color = o.textSecondaryColor;
|
|
1526
|
-
this.container.querySelectorAll('.waveform-time, .waveform-bpm').forEach((el) => {
|
|
1527
|
-
el.style.color = o.textSecondaryColor;
|
|
1528
|
-
});
|
|
1696
|
+
this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');
|
|
1529
1697
|
if (this.canvas) this.drawWaveform();
|
|
1530
1698
|
}
|
|
1531
1699
|
|
|
@@ -1651,16 +1819,13 @@ export class WaveformPlayer {
|
|
|
1651
1819
|
* directly: `WaveformBar.play(event.detail)`.
|
|
1652
1820
|
*
|
|
1653
1821
|
* @private
|
|
1654
|
-
* @return {{url:string,title:?string,
|
|
1822
|
+
* @return {{url:string,title:?string,artist:?string,artwork:?string,player:WaveformPlayer}}
|
|
1655
1823
|
*/
|
|
1656
1824
|
_buildTrackDetail() {
|
|
1657
1825
|
return {
|
|
1658
1826
|
url: this.options.url,
|
|
1659
1827
|
title: this.options.title,
|
|
1660
|
-
|
|
1661
|
-
// Core has no separate `artist` option; mirror subtitle so the
|
|
1662
|
-
// published event detail is self-consistent for controllers.
|
|
1663
|
-
artist: this.options.artist || this.options.subtitle,
|
|
1828
|
+
artist: this.options.artist,
|
|
1664
1829
|
artwork: this.options.artwork,
|
|
1665
1830
|
markers: this.options.markers,
|
|
1666
1831
|
waveform: this.options.waveform,
|
|
@@ -1937,7 +2102,7 @@ export class WaveformPlayer {
|
|
|
1937
2102
|
* @param {number} samples - Number of samples
|
|
1938
2103
|
* @returns {Promise<number[]>} Waveform peak data
|
|
1939
2104
|
*/
|
|
1940
|
-
static async generateWaveformData(url, samples =
|
|
2105
|
+
static async generateWaveformData(url, samples = DEFAULT_SAMPLES) {
|
|
1941
2106
|
try {
|
|
1942
2107
|
const result = await generateWaveform(url, samples);
|
|
1943
2108
|
return result.peaks;
|