@arraypress/waveform-player 1.21.0 → 1.23.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;
@@ -86,6 +94,9 @@ function parseDataAttributes(element) {
86
94
  if (element.dataset.preload) {
87
95
  options.preload = element.dataset.preload;
88
96
  }
97
+ if (element.dataset.crossOrigin) {
98
+ options.crossOrigin = element.dataset.crossOrigin;
99
+ }
89
100
  if (element.dataset.audioMode) options.audioMode = element.dataset.audioMode;
90
101
  if (element.dataset.style) options.waveformStyle = element.dataset.style;
91
102
  if (element.dataset.waveformStyle) options.waveformStyle = element.dataset.waveformStyle;
@@ -96,10 +107,8 @@ function parseDataAttributes(element) {
96
107
  if (element.dataset.buttonAlign) options.buttonAlign = element.dataset.buttonAlign;
97
108
  if (element.dataset.layout) options.layout = element.dataset.layout;
98
109
  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
- }
110
+ setLength("buttonSize");
111
+ setLength("buttonRadius");
103
112
  if (element.dataset.colorPreset) options.colorPreset = element.dataset.colorPreset;
104
113
  if (element.dataset.waveformColor) options.waveformColor = parseColorValue(element.dataset.waveformColor);
105
114
  if (element.dataset.progressColor) options.progressColor = parseColorValue(element.dataset.progressColor);
@@ -119,6 +128,7 @@ function parseDataAttributes(element) {
119
128
  if (element.dataset.artist) options.artist = element.dataset.artist;
120
129
  if (element.dataset.album) options.album = element.dataset.album;
121
130
  if (element.dataset.artwork) options.artwork = element.dataset.artwork;
131
+ if (element.dataset.artworkPosition) options.artworkPosition = element.dataset.artworkPosition;
122
132
  if (element.dataset.waveform) options.waveform = element.dataset.waveform;
123
133
  setJson("markers");
124
134
  setNum("playbackRate", "playbackRate", true);
@@ -695,6 +705,15 @@ var DEFAULT_OPTIONS = {
695
705
  // every-frame scan means a higher value costs no extra extraction time.
696
706
  samples: DEFAULT_SAMPLES,
697
707
  preload: "metadata",
708
+ // CORS mode for the underlying <audio> element. Left null by default so the
709
+ // player behaves like a plain <audio> and never forces a CORS request —
710
+ // setting 'anonymous' here would break playback of media hosted on origins
711
+ // that don't send Access-Control-Allow-Origin (default-config S3/CDN). The
712
+ // playback element is never fed to createMediaElementSource, so it never
713
+ // needs CORS-clean media; peak analysis uses a separate fetch() path with
714
+ // its own placeholder fallback. Set 'anonymous' / 'use-credentials' only if
715
+ // you specifically need it. See issue #18.
716
+ crossOrigin: null,
698
717
  // Audio mode — 'self' = player owns the <audio> element (default, current
699
718
  // behavior). 'external' = player is a visualization-only surface; no audio
700
719
  // element is created, play() dispatches `waveformplayer:request-play`
@@ -721,6 +740,12 @@ var DEFAULT_OPTIONS = {
721
740
  // is used verbatim. Sets the `--wfp-btn-size` CSS var, which scales BOTH
722
741
  // styles — box and glyph — proportionally.
723
742
  buttonSize: null,
743
+ // Play/pause button corner radius. null = the stylesheet default (50%, a
744
+ // circle). A number is treated as px; a string (e.g. '8px') is used
745
+ // verbatim. Set 0 for a square button. Sets the `--wfp-btn-radius` CSS var,
746
+ // which shapes the 'circle' style's box — the bare 'minimal' glyph has no
747
+ // box to round, so this is a no-op there.
748
+ buttonRadius: null,
724
749
  // Default waveform style
725
750
  waveformStyle: "mirror",
726
751
  barWidth: 2,
@@ -775,6 +800,17 @@ var DEFAULT_OPTIONS = {
775
800
  title: null,
776
801
  artist: null,
777
802
  artwork: null,
803
+ // Where the `artwork` image renders. 'info' = the info row beside the title
804
+ // (needs showInfo). 'button' = the cover becomes the play/pause button,
805
+ // which still works with the info row hidden. Only ONE placement ever
806
+ // renders, so the same cover can't appear twice.
807
+ //
808
+ // 'button' is self-contained: the stylesheet drops the button's ring and
809
+ // swaps its defaults for cover-sized ones (64px, 8px rounding), because the
810
+ // transport defaults are wrong for artwork in both directions — 36px
811
+ // smudges a cover, and 50% crops square art into a circle. buttonSize /
812
+ // buttonRadius still override it if you want a different tile.
813
+ artworkPosition: "info",
778
814
  album: "",
779
815
  // Message shown in the error state when audio fails to load.
780
816
  errorText: "Unable to load audio",
@@ -983,16 +1019,27 @@ var WaveformPlayer = class _WaveformPlayer {
983
1019
  this.container.classList.add("waveform-layout-preview");
984
1020
  }
985
1021
  this.container.classList.toggle("waveform-theme-light", this._scheme === "light");
1022
+ const buttonVars = [];
1023
+ if (this.options.buttonSize != null) {
1024
+ buttonVars.push(`--wfp-btn-size: ${formatCssLength(this.options.buttonSize)}`);
1025
+ }
1026
+ if (this.options.buttonRadius != null) {
1027
+ buttonVars.push(`--wfp-btn-radius: ${formatCssLength(this.options.buttonRadius)}`);
1028
+ }
1029
+ const buttonStyleAttr = buttonVars.length ? ` style="${buttonVars.join("; ")};"` : "";
1030
+ const artworkOnButton = this.options.artworkPosition === "button" && this.options.artwork;
1031
+ const buttonArtworkHTML = artworkOnButton ? `<img class="waveform-btn-artwork" src="${escapeHtml(this.options.artwork)}" alt="" aria-hidden="true">` : "";
986
1032
  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};"` : ""}>
1033
+ <button class="waveform-btn${this.options.buttonStyle === "minimal" ? " waveform-btn-minimal" : ""}${artworkOnButton ? " waveform-btn-has-artwork" : ""}" aria-label="${escapeHtml(this.options.playPauseLabel)}"${buttonStyleAttr}>
1034
+ ${buttonArtworkHTML}
988
1035
  <span class="waveform-icon-play">${this.options.playIcon}</span>
989
1036
  <span class="waveform-icon-pause" style="display:none;">${this.options.pauseIcon}</span>
990
1037
  </button>
991
1038
  ` : "";
992
1039
  const infoHTML = this.options.showInfo ? `
993
1040
  <div class="waveform-info">
994
- ${this.options.artwork ? `
995
- <img class="waveform-artwork" src="${this.options.artwork}" alt="${escapeHtml(this.options.artworkAlt)}" style="
1041
+ ${this.options.artworkPosition !== "button" && this.options.artwork ? `
1042
+ <img class="waveform-artwork" src="${escapeHtml(this.options.artwork)}" alt="${escapeHtml(this.options.artworkAlt)}" style="
996
1043
  width: 40px;
997
1044
  height: 40px;
998
1045
  border-radius: 4px;
@@ -1002,7 +1049,7 @@ var WaveformPlayer = class _WaveformPlayer {
1002
1049
  ` : ""}
1003
1050
  <div class="waveform-text">
1004
1051
  <span class="waveform-title"></span>
1005
- ${this.options.artist ? `<span class="waveform-artist">${this.options.artist}</span>` : ""}
1052
+ ${this.options.artist ? `<span class="waveform-artist">${escapeHtml(this.options.artist)}</span>` : ""}
1006
1053
  </div>
1007
1054
  <div class="waveform-meta" style="display: flex; align-items: center; gap: 1rem;">
1008
1055
  ${this.options.showBPM ? `
@@ -1055,7 +1102,7 @@ var WaveformPlayer = class _WaveformPlayer {
1055
1102
  this.ctx = this.canvas.getContext("2d");
1056
1103
  this.titleEl = this.container.querySelector(".waveform-title");
1057
1104
  this.artistEl = this.container.querySelector(".waveform-artist");
1058
- this.artworkEl = this.container.querySelector(".waveform-artwork");
1105
+ this.artworkEl = this.container.querySelector(".waveform-artwork, .waveform-btn-artwork");
1059
1106
  this.bindArtworkFallback(this.artworkEl);
1060
1107
  this.currentTimeEl = this.container.querySelector(".time-current");
1061
1108
  this.totalTimeEl = this.container.querySelector(".time-total");
@@ -1098,6 +1145,24 @@ var WaveformPlayer = class _WaveformPlayer {
1098
1145
  this.bindArtworkFallback(img);
1099
1146
  return img;
1100
1147
  }
1148
+ /**
1149
+ * Create a play-button artwork image matching the initial player markup.
1150
+ *
1151
+ * Decorative by design — the button already has an accessible name from its
1152
+ * `aria-label`, so the cover is hidden from assistive tech rather than
1153
+ * given a competing one. Sizing and clipping are the stylesheet's job.
1154
+ *
1155
+ * @returns {HTMLImageElement} Play-button artwork image element.
1156
+ * @private
1157
+ */
1158
+ createButtonArtworkElement() {
1159
+ const img = document.createElement("img");
1160
+ img.className = "waveform-btn-artwork";
1161
+ img.alt = "";
1162
+ img.setAttribute("aria-hidden", "true");
1163
+ this.bindArtworkFallback(img);
1164
+ return img;
1165
+ }
1101
1166
  /**
1102
1167
  * Create an artist text element matching the initial player markup.
1103
1168
  *
@@ -1132,16 +1197,51 @@ var WaveformPlayer = class _WaveformPlayer {
1132
1197
  this.artistEl.textContent = artist;
1133
1198
  this.artistEl.style.display = "";
1134
1199
  }
1200
+ /**
1201
+ * Reconcile the play button's artwork image (`artworkPosition: 'button'`).
1202
+ *
1203
+ * Shares {@link bindArtworkFallback} with the info-row image, so a broken
1204
+ * cover degrades to the same placeholder tile in either placement. The
1205
+ * `waveform-btn-has-artwork` class is what turns on the scrim + glyph
1206
+ * treatment, so it tracks the image's presence exactly.
1207
+ *
1208
+ * @param {string|null} artwork - Artwork image URL, or a falsy value to remove it.
1209
+ * @private
1210
+ */
1211
+ syncButtonArtwork(artwork) {
1212
+ if (!this.playBtn) return;
1213
+ if (!artwork) {
1214
+ this.artworkEl?.remove();
1215
+ this.artworkEl = null;
1216
+ this.playBtn.classList.remove("waveform-btn-has-artwork");
1217
+ return;
1218
+ }
1219
+ if (!this.artworkEl) {
1220
+ this.artworkEl = this.createButtonArtworkElement();
1221
+ this.playBtn.prepend(this.artworkEl);
1222
+ }
1223
+ this.artworkEl.src = artwork;
1224
+ this.playBtn.classList.add("waveform-btn-has-artwork");
1225
+ }
1135
1226
  /**
1136
1227
  * Reconcile artwork metadata and markup for the current track.
1137
1228
  *
1229
+ * The image lives in one of two homes depending on `artworkPosition` — the
1230
+ * info row (default) or the play button — and only ever one of them, so a
1231
+ * cover can't render twice.
1232
+ *
1138
1233
  * @param {string|null} artwork - Artwork image URL, or a falsy value to remove artwork.
1139
- * @param {string} artworkAlt - Artwork alt text.
1234
+ * @param {string} artworkAlt - Artwork alt text. Info-row placement only:
1235
+ * button artwork is decorative and defers to the button's own label.
1140
1236
  * @private
1141
1237
  */
1142
1238
  syncArtwork(artwork, artworkAlt = "") {
1143
1239
  this.options.artwork = artwork || null;
1144
1240
  this.options.artworkAlt = artworkAlt || "";
1241
+ if (this.options.artworkPosition === "button") {
1242
+ this.syncButtonArtwork(this.options.artwork);
1243
+ return;
1244
+ }
1145
1245
  if (!this.options.showInfo) return;
1146
1246
  if (!artwork) {
1147
1247
  this.artworkEl?.remove();
@@ -1174,7 +1274,9 @@ var WaveformPlayer = class _WaveformPlayer {
1174
1274
  }
1175
1275
  this.audio = new Audio();
1176
1276
  this.audio.preload = this.options.preload || "metadata";
1177
- this.audio.crossOrigin = "anonymous";
1277
+ if (this.options.crossOrigin) {
1278
+ this.audio.crossOrigin = this.options.crossOrigin;
1279
+ }
1178
1280
  }
1179
1281
  // ============================================
1180
1282
  // Feature Initialization
@@ -1748,6 +1850,9 @@ var WaveformPlayer = class _WaveformPlayer {
1748
1850
  if (options.preload && this.audio) {
1749
1851
  this.audio.preload = options.preload;
1750
1852
  }
1853
+ if (options.crossOrigin && this.audio) {
1854
+ this.audio.crossOrigin = options.crossOrigin;
1855
+ }
1751
1856
  if (artist !== null) {
1752
1857
  this.syncArtist(artist);
1753
1858
  }