@arraypress/waveform-player 1.20.0 → 1.22.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.
@@ -36,6 +36,9 @@ function maxOf(values) {
36
36
  function escapeHtml(str) {
37
37
  return String(str == null ? "" : str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
38
38
  }
39
+ function formatCssLength(value) {
40
+ return escapeHtml(typeof value === "number" ? `${value}px` : value);
41
+ }
39
42
  function isSafeHref(url) {
40
43
  if (typeof url !== "string" || url === "") return false;
41
44
  try {
@@ -70,6 +73,11 @@ function parseDataAttributes(element) {
70
73
  const raw = element.dataset[dataKey];
71
74
  if (raw) options[optKey] = float ? parseFloat(raw) : parseInt(raw, 10);
72
75
  };
76
+ const setLength = (optKey, dataKey = optKey) => {
77
+ const raw = element.dataset[dataKey];
78
+ if (!raw) return;
79
+ options[optKey] = /^\d+(\.\d+)?$/.test(raw.trim()) ? parseFloat(raw) : raw;
80
+ };
73
81
  const setJson = (optKey, dataKey = optKey) => {
74
82
  const raw = element.dataset[dataKey];
75
83
  if (!raw) return;
@@ -96,10 +104,8 @@ function parseDataAttributes(element) {
96
104
  if (element.dataset.buttonAlign) options.buttonAlign = element.dataset.buttonAlign;
97
105
  if (element.dataset.layout) options.layout = element.dataset.layout;
98
106
  if (element.dataset.buttonStyle) options.buttonStyle = element.dataset.buttonStyle;
99
- if (element.dataset.buttonSize) {
100
- const bs = element.dataset.buttonSize;
101
- options.buttonSize = /^\d+(\.\d+)?$/.test(bs.trim()) ? parseFloat(bs) : bs;
102
- }
107
+ setLength("buttonSize");
108
+ setLength("buttonRadius");
103
109
  if (element.dataset.colorPreset) options.colorPreset = element.dataset.colorPreset;
104
110
  if (element.dataset.waveformColor) options.waveformColor = parseColorValue(element.dataset.waveformColor);
105
111
  if (element.dataset.progressColor) options.progressColor = parseColorValue(element.dataset.progressColor);
@@ -119,6 +125,7 @@ function parseDataAttributes(element) {
119
125
  if (element.dataset.artist) options.artist = element.dataset.artist;
120
126
  if (element.dataset.album) options.album = element.dataset.album;
121
127
  if (element.dataset.artwork) options.artwork = element.dataset.artwork;
128
+ if (element.dataset.artworkPosition) options.artworkPosition = element.dataset.artworkPosition;
122
129
  if (element.dataset.waveform) options.waveform = element.dataset.waveform;
123
130
  setJson("markers");
124
131
  setNum("playbackRate", "playbackRate", true);
@@ -721,6 +728,12 @@ var DEFAULT_OPTIONS = {
721
728
  // is used verbatim. Sets the `--wfp-btn-size` CSS var, which scales BOTH
722
729
  // styles — box and glyph — proportionally.
723
730
  buttonSize: null,
731
+ // Play/pause button corner radius. null = the stylesheet default (50%, a
732
+ // circle). A number is treated as px; a string (e.g. '8px') is used
733
+ // verbatim. Set 0 for a square button. Sets the `--wfp-btn-radius` CSS var,
734
+ // which shapes the 'circle' style's box — the bare 'minimal' glyph has no
735
+ // box to round, so this is a no-op there.
736
+ buttonRadius: null,
724
737
  // Default waveform style
725
738
  waveformStyle: "mirror",
726
739
  barWidth: 2,
@@ -775,6 +788,17 @@ var DEFAULT_OPTIONS = {
775
788
  title: null,
776
789
  artist: null,
777
790
  artwork: null,
791
+ // Where the `artwork` image renders. 'info' = the info row beside the title
792
+ // (needs showInfo). 'button' = the cover becomes the play/pause button,
793
+ // which still works with the info row hidden. Only ONE placement ever
794
+ // renders, so the same cover can't appear twice.
795
+ //
796
+ // 'button' is self-contained: the stylesheet drops the button's ring and
797
+ // swaps its defaults for cover-sized ones (64px, 8px rounding), because the
798
+ // transport defaults are wrong for artwork in both directions — 36px
799
+ // smudges a cover, and 50% crops square art into a circle. buttonSize /
800
+ // buttonRadius still override it if you want a different tile.
801
+ artworkPosition: "info",
778
802
  album: "",
779
803
  // Message shown in the error state when audio fails to load.
780
804
  errorText: "Unable to load audio",
@@ -983,16 +1007,27 @@ var WaveformPlayer = class _WaveformPlayer {
983
1007
  this.container.classList.add("waveform-layout-preview");
984
1008
  }
985
1009
  this.container.classList.toggle("waveform-theme-light", this._scheme === "light");
1010
+ const buttonVars = [];
1011
+ if (this.options.buttonSize != null) {
1012
+ buttonVars.push(`--wfp-btn-size: ${formatCssLength(this.options.buttonSize)}`);
1013
+ }
1014
+ if (this.options.buttonRadius != null) {
1015
+ buttonVars.push(`--wfp-btn-radius: ${formatCssLength(this.options.buttonRadius)}`);
1016
+ }
1017
+ const buttonStyleAttr = buttonVars.length ? ` style="${buttonVars.join("; ")};"` : "";
1018
+ const artworkOnButton = this.options.artworkPosition === "button" && this.options.artwork;
1019
+ const buttonArtworkHTML = artworkOnButton ? `<img class="waveform-btn-artwork" src="${escapeHtml(this.options.artwork)}" alt="" aria-hidden="true">` : "";
986
1020
  const buttonHTML = this.options.showControls ? `
987
- <button class="waveform-btn${this.options.buttonStyle === "minimal" ? " waveform-btn-minimal" : ""}" aria-label="${escapeHtml(this.options.playPauseLabel)}"${this.options.buttonSize != null ? ` style="--wfp-btn-size: ${typeof this.options.buttonSize === "number" ? `${this.options.buttonSize}px` : this.options.buttonSize};"` : ""}>
1021
+ <button class="waveform-btn${this.options.buttonStyle === "minimal" ? " waveform-btn-minimal" : ""}${artworkOnButton ? " waveform-btn-has-artwork" : ""}" aria-label="${escapeHtml(this.options.playPauseLabel)}"${buttonStyleAttr}>
1022
+ ${buttonArtworkHTML}
988
1023
  <span class="waveform-icon-play">${this.options.playIcon}</span>
989
1024
  <span class="waveform-icon-pause" style="display:none;">${this.options.pauseIcon}</span>
990
1025
  </button>
991
1026
  ` : "";
992
1027
  const infoHTML = this.options.showInfo ? `
993
1028
  <div class="waveform-info">
994
- ${this.options.artwork ? `
995
- <img class="waveform-artwork" src="${this.options.artwork}" alt="${escapeHtml(this.options.artworkAlt)}" style="
1029
+ ${this.options.artworkPosition !== "button" && this.options.artwork ? `
1030
+ <img class="waveform-artwork" src="${escapeHtml(this.options.artwork)}" alt="${escapeHtml(this.options.artworkAlt)}" style="
996
1031
  width: 40px;
997
1032
  height: 40px;
998
1033
  border-radius: 4px;
@@ -1002,7 +1037,7 @@ var WaveformPlayer = class _WaveformPlayer {
1002
1037
  ` : ""}
1003
1038
  <div class="waveform-text">
1004
1039
  <span class="waveform-title"></span>
1005
- ${this.options.artist ? `<span class="waveform-artist">${this.options.artist}</span>` : ""}
1040
+ ${this.options.artist ? `<span class="waveform-artist">${escapeHtml(this.options.artist)}</span>` : ""}
1006
1041
  </div>
1007
1042
  <div class="waveform-meta" style="display: flex; align-items: center; gap: 1rem;">
1008
1043
  ${this.options.showBPM ? `
@@ -1055,12 +1090,8 @@ var WaveformPlayer = class _WaveformPlayer {
1055
1090
  this.ctx = this.canvas.getContext("2d");
1056
1091
  this.titleEl = this.container.querySelector(".waveform-title");
1057
1092
  this.artistEl = this.container.querySelector(".waveform-artist");
1058
- this.artworkEl = this.container.querySelector(".waveform-artwork");
1059
- if (this.artworkEl) {
1060
- this.artworkEl.addEventListener("error", () => {
1061
- if (!this.artworkEl.src.startsWith("data:")) this.artworkEl.src = ARTWORK_FALLBACK;
1062
- });
1063
- }
1093
+ this.artworkEl = this.container.querySelector(".waveform-artwork, .waveform-btn-artwork");
1094
+ this.bindArtworkFallback(this.artworkEl);
1064
1095
  this.currentTimeEl = this.container.querySelector(".time-current");
1065
1096
  this.totalTimeEl = this.container.querySelector(".time-total");
1066
1097
  this.bpmEl = this.container.querySelector(".waveform-bpm");
@@ -1073,6 +1104,147 @@ var WaveformPlayer = class _WaveformPlayer {
1073
1104
  this.resizeCanvas();
1074
1105
  this.updateBPMDisplay();
1075
1106
  }
1107
+ /**
1108
+ * Bind the artwork error fallback to an image element.
1109
+ *
1110
+ * @param {HTMLImageElement|null} img - Artwork image element.
1111
+ * @private
1112
+ */
1113
+ bindArtworkFallback(img) {
1114
+ if (!img) return;
1115
+ img.addEventListener("error", () => {
1116
+ if (!img.src.startsWith("data:")) img.src = ARTWORK_FALLBACK;
1117
+ }, { signal: this._ac.signal });
1118
+ }
1119
+ /**
1120
+ * Create an artwork image element matching the initial player markup.
1121
+ *
1122
+ * @returns {HTMLImageElement} Artwork image element.
1123
+ * @private
1124
+ */
1125
+ createArtworkElement() {
1126
+ const img = document.createElement("img");
1127
+ img.className = "waveform-artwork";
1128
+ img.style.width = "40px";
1129
+ img.style.height = "40px";
1130
+ img.style.borderRadius = "4px";
1131
+ img.style.objectFit = "cover";
1132
+ img.style.flexShrink = "0";
1133
+ this.bindArtworkFallback(img);
1134
+ return img;
1135
+ }
1136
+ /**
1137
+ * Create a play-button artwork image matching the initial player markup.
1138
+ *
1139
+ * Decorative by design — the button already has an accessible name from its
1140
+ * `aria-label`, so the cover is hidden from assistive tech rather than
1141
+ * given a competing one. Sizing and clipping are the stylesheet's job.
1142
+ *
1143
+ * @returns {HTMLImageElement} Play-button artwork image element.
1144
+ * @private
1145
+ */
1146
+ createButtonArtworkElement() {
1147
+ const img = document.createElement("img");
1148
+ img.className = "waveform-btn-artwork";
1149
+ img.alt = "";
1150
+ img.setAttribute("aria-hidden", "true");
1151
+ this.bindArtworkFallback(img);
1152
+ return img;
1153
+ }
1154
+ /**
1155
+ * Create an artist text element matching the initial player markup.
1156
+ *
1157
+ * @returns {HTMLSpanElement} Artist text element.
1158
+ * @private
1159
+ */
1160
+ createArtistElement() {
1161
+ const span = document.createElement("span");
1162
+ span.className = "waveform-artist";
1163
+ return span;
1164
+ }
1165
+ /**
1166
+ * Reconcile artist metadata and markup for the current track.
1167
+ *
1168
+ * @param {string|null} artist - Artist text, or a falsy value to remove it.
1169
+ * @private
1170
+ */
1171
+ syncArtist(artist) {
1172
+ this.options.artist = artist || null;
1173
+ if (!this.options.showInfo) return;
1174
+ if (!artist) {
1175
+ this.artistEl?.remove();
1176
+ this.artistEl = null;
1177
+ return;
1178
+ }
1179
+ if (!this.artistEl) {
1180
+ const titleEl = this.container.querySelector(".waveform-title");
1181
+ if (!titleEl) return;
1182
+ this.artistEl = this.createArtistElement();
1183
+ titleEl.after(this.artistEl);
1184
+ }
1185
+ this.artistEl.textContent = artist;
1186
+ this.artistEl.style.display = "";
1187
+ }
1188
+ /**
1189
+ * Reconcile the play button's artwork image (`artworkPosition: 'button'`).
1190
+ *
1191
+ * Shares {@link bindArtworkFallback} with the info-row image, so a broken
1192
+ * cover degrades to the same placeholder tile in either placement. The
1193
+ * `waveform-btn-has-artwork` class is what turns on the scrim + glyph
1194
+ * treatment, so it tracks the image's presence exactly.
1195
+ *
1196
+ * @param {string|null} artwork - Artwork image URL, or a falsy value to remove it.
1197
+ * @private
1198
+ */
1199
+ syncButtonArtwork(artwork) {
1200
+ if (!this.playBtn) return;
1201
+ if (!artwork) {
1202
+ this.artworkEl?.remove();
1203
+ this.artworkEl = null;
1204
+ this.playBtn.classList.remove("waveform-btn-has-artwork");
1205
+ return;
1206
+ }
1207
+ if (!this.artworkEl) {
1208
+ this.artworkEl = this.createButtonArtworkElement();
1209
+ this.playBtn.prepend(this.artworkEl);
1210
+ }
1211
+ this.artworkEl.src = artwork;
1212
+ this.playBtn.classList.add("waveform-btn-has-artwork");
1213
+ }
1214
+ /**
1215
+ * Reconcile artwork metadata and markup for the current track.
1216
+ *
1217
+ * The image lives in one of two homes depending on `artworkPosition` — the
1218
+ * info row (default) or the play button — and only ever one of them, so a
1219
+ * cover can't render twice.
1220
+ *
1221
+ * @param {string|null} artwork - Artwork image URL, or a falsy value to remove artwork.
1222
+ * @param {string} artworkAlt - Artwork alt text. Info-row placement only:
1223
+ * button artwork is decorative and defers to the button's own label.
1224
+ * @private
1225
+ */
1226
+ syncArtwork(artwork, artworkAlt = "") {
1227
+ this.options.artwork = artwork || null;
1228
+ this.options.artworkAlt = artworkAlt || "";
1229
+ if (this.options.artworkPosition === "button") {
1230
+ this.syncButtonArtwork(this.options.artwork);
1231
+ return;
1232
+ }
1233
+ if (!this.options.showInfo) return;
1234
+ if (!artwork) {
1235
+ this.artworkEl?.remove();
1236
+ this.artworkEl = null;
1237
+ return;
1238
+ }
1239
+ if (!this.artworkEl) {
1240
+ const textEl = this.container.querySelector(".waveform-text");
1241
+ if (!textEl) return;
1242
+ this.artworkEl = this.createArtworkElement();
1243
+ textEl.before(this.artworkEl);
1244
+ }
1245
+ this.artworkEl.src = artwork;
1246
+ this.artworkEl.alt = artworkAlt || "";
1247
+ }
1076
1248
  /**
1077
1249
  * Create audio element
1078
1250
  * @private
@@ -1622,10 +1794,12 @@ var WaveformPlayer = class _WaveformPlayer {
1622
1794
  * @param {string|null} [artist=null] - Track artist; pass `''` to hide
1623
1795
  * the artist row, or null to keep the existing one.
1624
1796
  * @param {Object} [options={}] - Additional options to merge (e.g.
1625
- * `preload`, `artwork`, `markers`, `autoplay`).
1797
+ * `preload`, `artwork`, `artworkAlt`, `markers`, `autoplay`).
1626
1798
  * @returns {Promise<void>}
1627
1799
  */
1628
1800
  async loadTrack(url, title = null, artist = null, options = {}) {
1801
+ const hasArtworkOption = Object.prototype.hasOwnProperty.call(options, "artwork");
1802
+ const hasArtworkAltOption = Object.prototype.hasOwnProperty.call(options, "artworkAlt");
1629
1803
  if (this.isPlaying) {
1630
1804
  this.pause();
1631
1805
  }
@@ -1647,23 +1821,29 @@ var WaveformPlayer = class _WaveformPlayer {
1647
1821
  this.waveformData = [];
1648
1822
  this.options = mergeOptions(this.options, {
1649
1823
  url,
1650
- title: title || this.options.title,
1651
- artist: artist || this.options.artist,
1824
+ title: title === null ? this.options.title : title,
1825
+ artist: artist === null ? this.options.artist : artist,
1652
1826
  ...options
1653
1827
  });
1828
+ if (hasArtworkOption) {
1829
+ this.options.artwork = options.artwork || null;
1830
+ }
1831
+ if (hasArtworkAltOption) {
1832
+ this.options.artworkAlt = options.artworkAlt || "";
1833
+ } else if (hasArtworkOption) {
1834
+ this.options.artworkAlt = this.options.artwork ? DEFAULT_OPTIONS.artworkAlt : "";
1835
+ }
1654
1836
  if (options.preload && this.audio) {
1655
1837
  this.audio.preload = options.preload;
1656
1838
  }
1657
- if (this.artistEl) {
1658
- if (artist) {
1659
- this.artistEl.textContent = artist;
1660
- this.artistEl.style.display = "";
1661
- } else if (artist === "") {
1662
- this.artistEl.style.display = "none";
1663
- }
1839
+ if (artist !== null) {
1840
+ this.syncArtist(artist);
1664
1841
  }
1665
- if (options.artwork && this.artworkEl) {
1666
- this.artworkEl.src = options.artwork;
1842
+ if (hasArtworkOption || hasArtworkAltOption) {
1843
+ this.syncArtwork(
1844
+ hasArtworkOption ? options.artwork : this.options.artwork,
1845
+ hasArtworkAltOption ? options.artworkAlt : this.options.artworkAlt
1846
+ );
1667
1847
  }
1668
1848
  this.options.markers = options.markers || [];
1669
1849
  this.options.waveform = options.waveform || null;