@arraypress/waveform-player 1.21.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,7 +1090,7 @@ 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");
1093
+ this.artworkEl = this.container.querySelector(".waveform-artwork, .waveform-btn-artwork");
1059
1094
  this.bindArtworkFallback(this.artworkEl);
1060
1095
  this.currentTimeEl = this.container.querySelector(".time-current");
1061
1096
  this.totalTimeEl = this.container.querySelector(".time-total");
@@ -1098,6 +1133,24 @@ var WaveformPlayer = class _WaveformPlayer {
1098
1133
  this.bindArtworkFallback(img);
1099
1134
  return img;
1100
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
+ }
1101
1154
  /**
1102
1155
  * Create an artist text element matching the initial player markup.
1103
1156
  *
@@ -1132,16 +1185,51 @@ var WaveformPlayer = class _WaveformPlayer {
1132
1185
  this.artistEl.textContent = artist;
1133
1186
  this.artistEl.style.display = "";
1134
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
+ }
1135
1214
  /**
1136
1215
  * Reconcile artwork metadata and markup for the current track.
1137
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
+ *
1138
1221
  * @param {string|null} artwork - Artwork image URL, or a falsy value to remove artwork.
1139
- * @param {string} artworkAlt - Artwork alt text.
1222
+ * @param {string} artworkAlt - Artwork alt text. Info-row placement only:
1223
+ * button artwork is decorative and defers to the button's own label.
1140
1224
  * @private
1141
1225
  */
1142
1226
  syncArtwork(artwork, artworkAlt = "") {
1143
1227
  this.options.artwork = artwork || null;
1144
1228
  this.options.artworkAlt = artworkAlt || "";
1229
+ if (this.options.artworkPosition === "button") {
1230
+ this.syncButtonArtwork(this.options.artwork);
1231
+ return;
1232
+ }
1145
1233
  if (!this.options.showInfo) return;
1146
1234
  if (!artwork) {
1147
1235
  this.artworkEl?.remove();