@arraypress/waveform-player 1.15.0 → 1.16.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.
@@ -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,7 @@ var WaveformPlayer = class _WaveformPlayer {
845
848
  this.ctx = null;
846
849
  this.waveformData = [];
847
850
  this.progress = 0;
851
+ this._activeMarkerIndex = -1;
848
852
  this.isPlaying = false;
849
853
  this.isLoading = false;
850
854
  this.hasError = false;
@@ -1027,6 +1031,11 @@ var WaveformPlayer = class _WaveformPlayer {
1027
1031
  this.titleEl = this.container.querySelector(".waveform-title");
1028
1032
  this.subtitleEl = this.container.querySelector(".waveform-subtitle");
1029
1033
  this.artworkEl = this.container.querySelector(".waveform-artwork");
1034
+ if (this.artworkEl) {
1035
+ this.artworkEl.addEventListener("error", () => {
1036
+ if (!this.artworkEl.src.startsWith("data:")) this.artworkEl.src = ARTWORK_FALLBACK;
1037
+ });
1038
+ }
1030
1039
  this.currentTimeEl = this.container.querySelector(".time-current");
1031
1040
  this.totalTimeEl = this.container.querySelector(".time-total");
1032
1041
  this.bpmEl = this.container.querySelector(".waveform-bpm");
@@ -1329,6 +1338,7 @@ var WaveformPlayer = class _WaveformPlayer {
1329
1338
  this.audio.addEventListener("error", (e) => this.onError(e));
1330
1339
  }
1331
1340
  this.canvas.addEventListener("click", (e) => this.handleCanvasClick(e));
1341
+ this.setupHoverTime();
1332
1342
  this.resizeHandler = debounce(() => this.resizeCanvas(), 100);
1333
1343
  window.addEventListener("resize", this.resizeHandler);
1334
1344
  }
@@ -1572,6 +1582,7 @@ var WaveformPlayer = class _WaveformPlayer {
1572
1582
  renderMarkers() {
1573
1583
  if (!this.markersContainer) return;
1574
1584
  this.markersContainer.innerHTML = "";
1585
+ this._activeMarkerIndex = -1;
1575
1586
  if (!this.options.showMarkers || !this.options.markers?.length) return;
1576
1587
  const duration = this.getSeekDuration();
1577
1588
  if (!duration) {
@@ -1615,6 +1626,64 @@ var WaveformPlayer = class _WaveformPlayer {
1615
1626
  const markers = this.markersContainer.querySelectorAll(".waveform-marker");
1616
1627
  markers.forEach((el, i) => el.classList.toggle("active", i === index));
1617
1628
  }
1629
+ /**
1630
+ * Highlight the marker the playhead has most recently passed — the rendered
1631
+ * marker with the greatest `time` ≤ the current time — and reveal its label.
1632
+ * Driven from the progress loop; a no-op when there are no markers. Reuses
1633
+ * {@link WaveformPlayer#setActiveMarker}, and only re-applies when the active
1634
+ * marker actually changes.
1635
+ * @private
1636
+ */
1637
+ updateActiveMarker() {
1638
+ if (!this.markersContainer) return;
1639
+ const els = this.markersContainer.querySelectorAll(".waveform-marker");
1640
+ if (!els.length) return;
1641
+ const dur = this.getSeekDuration();
1642
+ const t = dur ? this.progress * dur : 0;
1643
+ let active = -1;
1644
+ let best = -Infinity;
1645
+ els.forEach((el, i) => {
1646
+ const mt = parseFloat(el.getAttribute("data-time"));
1647
+ if (Number.isFinite(mt) && mt <= t + 0.05 && mt > best) {
1648
+ best = mt;
1649
+ active = i;
1650
+ }
1651
+ });
1652
+ if (active !== this._activeMarkerIndex) {
1653
+ this._activeMarkerIndex = active;
1654
+ this.setActiveMarker(active);
1655
+ }
1656
+ }
1657
+ /**
1658
+ * Build a tooltip that follows the pointer over the waveform and shows the
1659
+ * time at that position. Enabled by the `showHoverTime` option; works in both
1660
+ * self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).
1661
+ * Uses the same pointer-X → percentage math as seeking.
1662
+ * @private
1663
+ */
1664
+ setupHoverTime() {
1665
+ if (!this.options.showHoverTime || !this.seekEl) return;
1666
+ const tip = document.createElement("div");
1667
+ tip.className = "waveform-hover-time";
1668
+ tip.setAttribute("aria-hidden", "true");
1669
+ this.seekEl.appendChild(tip);
1670
+ this.hoverTimeEl = tip;
1671
+ this.seekEl.addEventListener("pointermove", (e) => {
1672
+ const dur = this.getSeekDuration();
1673
+ if (!dur) {
1674
+ tip.style.opacity = "0";
1675
+ return;
1676
+ }
1677
+ const rect = this.canvas.getBoundingClientRect();
1678
+ const pct = clamp((e.clientX - rect.left) / rect.width);
1679
+ tip.textContent = formatTime(pct * dur);
1680
+ tip.style.left = pct * 100 + "%";
1681
+ tip.style.opacity = "1";
1682
+ });
1683
+ this.seekEl.addEventListener("pointerleave", () => {
1684
+ tip.style.opacity = "0";
1685
+ });
1686
+ }
1618
1687
  // ============================================
1619
1688
  // Event Handlers
1620
1689
  // ============================================
@@ -1833,6 +1902,7 @@ var WaveformPlayer = class _WaveformPlayer {
1833
1902
  if (this.options.onTimeUpdate) {
1834
1903
  this.options.onTimeUpdate(this.audio.currentTime, this.audio.duration, this);
1835
1904
  }
1905
+ this.updateActiveMarker();
1836
1906
  this.updateSeekAccessibility();
1837
1907
  }
1838
1908
  // ============================================
@@ -2069,6 +2139,7 @@ var WaveformPlayer = class _WaveformPlayer {
2069
2139
  this.totalTimeEl.dataset._extDur = String(duration);
2070
2140
  }
2071
2141
  this.drawWaveform?.();
2142
+ this.updateActiveMarker();
2072
2143
  this._emit("waveformplayer:timeupdate", { player: this, currentTime, duration, progress: this.progress, url: this.options.url });
2073
2144
  if (this.options.onTimeUpdate) this.options.onTimeUpdate(currentTime, duration, this);
2074
2145
  if (this.progress >= 1) {