@arraypress/waveform-player 1.15.0 → 1.16.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 +81 -0
- package/dist/waveform-player.cjs.map +2 -2
- package/dist/waveform-player.css +1 -1
- package/dist/waveform-player.esm.js +2 -2
- package/dist/waveform-player.esm.js.map +3 -3
- package/dist/waveform-player.js +81 -0
- package/dist/waveform-player.min.js +2 -2
- package/dist/waveform-player.min.js.map +3 -3
- package/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/css/waveform-player.css +28 -0
- package/src/js/core.js +93 -0
package/dist/waveform-player.cjs
CHANGED
|
@@ -788,6 +788,9 @@ var STYLE_DEFAULTS = {
|
|
|
788
788
|
};
|
|
789
789
|
|
|
790
790
|
// src/js/core.js
|
|
791
|
+
var ARTWORK_FALLBACK = "data:image/svg+xml," + encodeURIComponent(
|
|
792
|
+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24" rx="4" fill="#71717a" fill-opacity="0.15"/><g fill="none" stroke="#a1a1aa" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><circle cx="8" cy="17" r="2.2"/><circle cx="17" cy="15" r="2.2"/><path d="M10.2 17V7l9-1.6v9"/></g></svg>'
|
|
793
|
+
);
|
|
791
794
|
var SEEK_STEP_SECONDS = 5;
|
|
792
795
|
var SEEK_PAGE_SECONDS = 10;
|
|
793
796
|
var WaveformPlayer = class _WaveformPlayer {
|
|
@@ -845,6 +848,8 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
845
848
|
this.ctx = null;
|
|
846
849
|
this.waveformData = [];
|
|
847
850
|
this.progress = 0;
|
|
851
|
+
this._activeMarkerIndex = -1;
|
|
852
|
+
this._markerLabelTimer = null;
|
|
848
853
|
this.isPlaying = false;
|
|
849
854
|
this.isLoading = false;
|
|
850
855
|
this.hasError = false;
|
|
@@ -1027,6 +1032,11 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
1027
1032
|
this.titleEl = this.container.querySelector(".waveform-title");
|
|
1028
1033
|
this.subtitleEl = this.container.querySelector(".waveform-subtitle");
|
|
1029
1034
|
this.artworkEl = this.container.querySelector(".waveform-artwork");
|
|
1035
|
+
if (this.artworkEl) {
|
|
1036
|
+
this.artworkEl.addEventListener("error", () => {
|
|
1037
|
+
if (!this.artworkEl.src.startsWith("data:")) this.artworkEl.src = ARTWORK_FALLBACK;
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1030
1040
|
this.currentTimeEl = this.container.querySelector(".time-current");
|
|
1031
1041
|
this.totalTimeEl = this.container.querySelector(".time-total");
|
|
1032
1042
|
this.bpmEl = this.container.querySelector(".waveform-bpm");
|
|
@@ -1329,6 +1339,7 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
1329
1339
|
this.audio.addEventListener("error", (e) => this.onError(e));
|
|
1330
1340
|
}
|
|
1331
1341
|
this.canvas.addEventListener("click", (e) => this.handleCanvasClick(e));
|
|
1342
|
+
this.setupHoverTime();
|
|
1332
1343
|
this.resizeHandler = debounce(() => this.resizeCanvas(), 100);
|
|
1333
1344
|
window.addEventListener("resize", this.resizeHandler);
|
|
1334
1345
|
}
|
|
@@ -1572,6 +1583,8 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
1572
1583
|
renderMarkers() {
|
|
1573
1584
|
if (!this.markersContainer) return;
|
|
1574
1585
|
this.markersContainer.innerHTML = "";
|
|
1586
|
+
this._activeMarkerIndex = -1;
|
|
1587
|
+
clearTimeout(this._markerLabelTimer);
|
|
1575
1588
|
if (!this.options.showMarkers || !this.options.markers?.length) return;
|
|
1576
1589
|
const duration = this.getSeekDuration();
|
|
1577
1590
|
if (!duration) {
|
|
@@ -1615,6 +1628,71 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
1615
1628
|
const markers = this.markersContainer.querySelectorAll(".waveform-marker");
|
|
1616
1629
|
markers.forEach((el, i) => el.classList.toggle("active", i === index));
|
|
1617
1630
|
}
|
|
1631
|
+
/**
|
|
1632
|
+
* Highlight the marker the playhead has most recently passed — the rendered
|
|
1633
|
+
* marker with the greatest `time` ≤ the current time — and reveal its label.
|
|
1634
|
+
* Driven from the progress loop; a no-op when there are no markers. Reuses
|
|
1635
|
+
* {@link WaveformPlayer#setActiveMarker}, and only re-applies when the active
|
|
1636
|
+
* marker actually changes.
|
|
1637
|
+
* @private
|
|
1638
|
+
*/
|
|
1639
|
+
updateActiveMarker() {
|
|
1640
|
+
if (!this.markersContainer) return;
|
|
1641
|
+
const els = this.markersContainer.querySelectorAll(".waveform-marker");
|
|
1642
|
+
if (!els.length) return;
|
|
1643
|
+
const dur = this.getSeekDuration();
|
|
1644
|
+
const t = dur ? this.progress * dur : 0;
|
|
1645
|
+
let active = -1;
|
|
1646
|
+
let best = -Infinity;
|
|
1647
|
+
els.forEach((el, i) => {
|
|
1648
|
+
const mt = parseFloat(el.getAttribute("data-time"));
|
|
1649
|
+
if (Number.isFinite(mt) && mt <= t + 0.05 && mt > best) {
|
|
1650
|
+
best = mt;
|
|
1651
|
+
active = i;
|
|
1652
|
+
}
|
|
1653
|
+
});
|
|
1654
|
+
if (active !== this._activeMarkerIndex) {
|
|
1655
|
+
this._activeMarkerIndex = active;
|
|
1656
|
+
this.setActiveMarker(active);
|
|
1657
|
+
clearTimeout(this._markerLabelTimer);
|
|
1658
|
+
els.forEach((el, i) => el.classList.toggle("show-label", i === active));
|
|
1659
|
+
if (active >= 0) {
|
|
1660
|
+
this._markerLabelTimer = setTimeout(() => {
|
|
1661
|
+
this.markersContainer?.querySelectorAll(".waveform-marker").forEach((el) => el.classList.remove("show-label"));
|
|
1662
|
+
}, 2500);
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
/**
|
|
1667
|
+
* Build a tooltip that follows the pointer over the waveform and shows the
|
|
1668
|
+
* time at that position. Enabled by the `showHoverTime` option; works in both
|
|
1669
|
+
* self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).
|
|
1670
|
+
* Uses the same pointer-X → percentage math as seeking.
|
|
1671
|
+
* @private
|
|
1672
|
+
*/
|
|
1673
|
+
setupHoverTime() {
|
|
1674
|
+
if (!this.options.showHoverTime || !this.seekEl) return;
|
|
1675
|
+
const tip = document.createElement("div");
|
|
1676
|
+
tip.className = "waveform-hover-time";
|
|
1677
|
+
tip.setAttribute("aria-hidden", "true");
|
|
1678
|
+
this.seekEl.appendChild(tip);
|
|
1679
|
+
this.hoverTimeEl = tip;
|
|
1680
|
+
this.seekEl.addEventListener("pointermove", (e) => {
|
|
1681
|
+
const dur = this.getSeekDuration();
|
|
1682
|
+
if (!dur) {
|
|
1683
|
+
tip.style.opacity = "0";
|
|
1684
|
+
return;
|
|
1685
|
+
}
|
|
1686
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
1687
|
+
const pct = clamp((e.clientX - rect.left) / rect.width);
|
|
1688
|
+
tip.textContent = formatTime(pct * dur);
|
|
1689
|
+
tip.style.left = pct * 100 + "%";
|
|
1690
|
+
tip.style.opacity = "1";
|
|
1691
|
+
});
|
|
1692
|
+
this.seekEl.addEventListener("pointerleave", () => {
|
|
1693
|
+
tip.style.opacity = "0";
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1618
1696
|
// ============================================
|
|
1619
1697
|
// Event Handlers
|
|
1620
1698
|
// ============================================
|
|
@@ -1833,6 +1911,7 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
1833
1911
|
if (this.options.onTimeUpdate) {
|
|
1834
1912
|
this.options.onTimeUpdate(this.audio.currentTime, this.audio.duration, this);
|
|
1835
1913
|
}
|
|
1914
|
+
this.updateActiveMarker();
|
|
1836
1915
|
this.updateSeekAccessibility();
|
|
1837
1916
|
}
|
|
1838
1917
|
// ============================================
|
|
@@ -2069,6 +2148,7 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
2069
2148
|
this.totalTimeEl.dataset._extDur = String(duration);
|
|
2070
2149
|
}
|
|
2071
2150
|
this.drawWaveform?.();
|
|
2151
|
+
this.updateActiveMarker();
|
|
2072
2152
|
this._emit("waveformplayer:timeupdate", { player: this, currentTime, duration, progress: this.progress, url: this.options.url });
|
|
2073
2153
|
if (this.options.onTimeUpdate) this.options.onTimeUpdate(currentTime, duration, this);
|
|
2074
2154
|
if (this.progress >= 1) {
|
|
@@ -2159,6 +2239,7 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
2159
2239
|
this._emit("waveformplayer:destroy", { player: this, url: this.options.url });
|
|
2160
2240
|
this.pause();
|
|
2161
2241
|
this.stopSmoothUpdate();
|
|
2242
|
+
clearTimeout(this._markerLabelTimer);
|
|
2162
2243
|
this._ac?.abort();
|
|
2163
2244
|
if (this.resizeObserver) {
|
|
2164
2245
|
this.resizeObserver.disconnect();
|