@arraypress/waveform-player 1.23.0 → 1.24.2

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.
@@ -156,10 +156,11 @@ function formatSeekValueText(template, ...args) {
156
156
  });
157
157
  }
158
158
  function formatTime(seconds) {
159
- if (!seconds || isNaN(seconds) || seconds < 0) return "0:00";
160
- const hrs = Math.floor(seconds / 3600);
161
- const mins = Math.floor(seconds % 3600 / 60);
162
- const secs = Math.floor(seconds % 60);
159
+ const total = Number(seconds);
160
+ if (!total || !Number.isFinite(total) || total < 0) return "0:00";
161
+ const hrs = Math.floor(total / 3600);
162
+ const mins = Math.floor(total % 3600 / 60);
163
+ const secs = Math.floor(total % 60);
163
164
  if (hrs > 0) {
164
165
  return `${hrs}:${mins.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`;
165
166
  }
@@ -181,11 +182,21 @@ function extractTitleFromUrl(url) {
181
182
  const name = filename.split(".")[0];
182
183
  return name.replace(/[-_]/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());
183
184
  }
185
+ function parseColor(color) {
186
+ if (typeof color !== "string") return null;
187
+ const m = color.match(/rgba?\(\s*([\d.]+)\s*[,\s]\s*([\d.]+)\s*[,\s]\s*([\d.]+)\s*(?:[,/]\s*([\d.]+)(%?))?/i);
188
+ if (!m) return null;
189
+ const r = Number(m[1]), g = Number(m[2]), b = Number(m[3]);
190
+ if (!Number.isFinite(r) || !Number.isFinite(g) || !Number.isFinite(b)) return null;
191
+ let a = m[4] === void 0 ? 1 : Number(m[4]);
192
+ if (!Number.isFinite(a)) return null;
193
+ if (m[5] === "%") a /= 100;
194
+ return { r, g, b, a: clamp(a, 0, 1) };
195
+ }
184
196
  function perceivedBrightness(color) {
185
- const rgb = typeof color === "string" ? color.match(/\d+/g) : null;
186
- if (!rgb || rgb.length < 3) return null;
187
- const [r, g, b] = rgb.map(Number);
188
- return (r * 299 + g * 587 + b * 114) / 1e3;
197
+ const c = parseColor(color);
198
+ if (!c || c.a <= 0) return null;
199
+ return (c.r * 299 + c.g * 587 + c.b * 114) / 1e3;
189
200
  }
190
201
  function mergeOptions(...sources) {
191
202
  const result = {};
@@ -650,32 +661,47 @@ function normalizePeaks(peaks, targetMax = 0.95) {
650
661
  }
651
662
 
652
663
  // src/js/themes.js
664
+ var LIGHT_THRESHOLD = 128;
653
665
  function hasThemeHint(scheme) {
654
666
  const root = document.documentElement;
655
667
  const body = document.body;
656
668
  return root.classList.contains(scheme) || root.classList.contains(`${scheme}-mode`) || root.classList.contains(`theme-${scheme}`) || root.getAttribute("data-theme") === scheme || root.getAttribute("data-color-scheme") === scheme || body.classList.contains(scheme) || body.classList.contains(`${scheme}-mode`) || body.getAttribute("data-theme") === scheme;
657
669
  }
658
- function detectColorScheme() {
670
+ function collectBackdrop(el) {
671
+ let sum = 0;
672
+ let alpha = 0;
673
+ for (let node = el; node && node.nodeType === 1 && alpha < 0.995; node = node.parentElement) {
674
+ const c = parseColor(getComputedStyle(node).backgroundColor);
675
+ if (!c || c.a <= 0) continue;
676
+ const weight = c.a * (1 - alpha);
677
+ sum += (c.r * 299 + c.g * 587 + c.b * 114) / 1e3 * weight;
678
+ alpha += weight;
679
+ }
680
+ return { sum, alpha };
681
+ }
682
+ function detectCanvasScheme() {
683
+ const text = perceivedBrightness(getComputedStyle(document.body).color);
684
+ if (text !== null) return text > LIGHT_THRESHOLD ? "dark" : "light";
685
+ if (window.matchMedia) {
686
+ if (window.matchMedia("(prefers-color-scheme: dark)").matches) return "dark";
687
+ if (window.matchMedia("(prefers-color-scheme: light)").matches) return "light";
688
+ }
689
+ return "dark";
690
+ }
691
+ function detectColorScheme(el) {
659
692
  if (hasThemeHint("dark")) return "dark";
660
693
  if (hasThemeHint("light")) return "light";
661
694
  try {
662
- const bodyBg = getComputedStyle(document.body).backgroundColor;
663
- const brightness = perceivedBrightness(bodyBg);
664
- if (brightness !== null) {
665
- if (brightness > 128) return "light";
666
- if (brightness < 128) return "dark";
667
- }
695
+ const start = el && el.nodeType === 1 ? el : document.body;
696
+ const { sum, alpha } = collectBackdrop(start);
697
+ const canvas = detectCanvasScheme();
698
+ const brightness = sum + (canvas === "dark" ? 0 : 255) * (1 - alpha);
699
+ if (brightness > LIGHT_THRESHOLD) return "light";
700
+ if (brightness < LIGHT_THRESHOLD) return "dark";
701
+ return canvas;
668
702
  } catch (e) {
703
+ return "dark";
669
704
  }
670
- if (window.matchMedia) {
671
- if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
672
- return "dark";
673
- }
674
- if (window.matchMedia("(prefers-color-scheme: light)").matches) {
675
- return "light";
676
- }
677
- }
678
- return "dark";
679
705
  }
680
706
  var COLOR_PRESETS = {
681
707
  dark: {
@@ -687,11 +713,11 @@ var COLOR_PRESETS = {
687
713
  progressColor: "rgba(0, 0, 0, 0.8)"
688
714
  }
689
715
  };
690
- function getColorPreset(presetName) {
716
+ function getColorPreset(presetName, el) {
691
717
  if (presetName && COLOR_PRESETS[presetName]) {
692
718
  return COLOR_PRESETS[presetName];
693
719
  }
694
- const detected = detectColorScheme();
720
+ const detected = detectColorScheme(el);
695
721
  return COLOR_PRESETS[detected];
696
722
  }
697
723
  var DEFAULT_OPTIONS = {
@@ -887,10 +913,10 @@ var WaveformPlayer = class _WaveformPlayer {
887
913
  if (userOptions.style && !userOptions.waveformStyle) userOptions.waveformStyle = userOptions.style;
888
914
  if (userOptions.src && !userOptions.url) userOptions.url = userOptions.src;
889
915
  this.options = mergeOptions(DEFAULT_OPTIONS, dataOptions, userOptions);
890
- const preset = getColorPreset(this.options.colorPreset);
916
+ const preset = getColorPreset(this.options.colorPreset, this.container);
891
917
  this._autoTheme = this.options.colorPreset == null || !COLOR_PRESETS[this.options.colorPreset];
892
918
  this._presetKeys = [];
893
- this._scheme = this.options.colorPreset && COLOR_PRESETS[this.options.colorPreset] ? this.options.colorPreset : detectColorScheme();
919
+ this._scheme = this.options.colorPreset && COLOR_PRESETS[this.options.colorPreset] ? this.options.colorPreset : detectColorScheme(this.container);
894
920
  for (const [key, value] of Object.entries(preset)) {
895
921
  if (this.options[key] === null || this.options[key] === void 0) {
896
922
  this.options[key] = value;
@@ -1746,6 +1772,7 @@ var WaveformPlayer = class _WaveformPlayer {
1746
1772
  this.setLoading(true);
1747
1773
  this.progress = 0;
1748
1774
  this.hasError = false;
1775
+ this.container.classList.remove("waveform-is-placeholder");
1749
1776
  if (this.audio) {
1750
1777
  this.audio.src = url;
1751
1778
  await new Promise((resolve, reject) => {
@@ -1781,6 +1808,7 @@ var WaveformPlayer = class _WaveformPlayer {
1781
1808
  } catch (error) {
1782
1809
  console.warn("[WaveformPlayer] Using placeholder waveform:", error);
1783
1810
  this.waveformData = generatePlaceholderWaveform(this.options.samples);
1811
+ this.container.classList.add("waveform-is-placeholder");
1784
1812
  }
1785
1813
  }
1786
1814
  this.drawWaveform();
@@ -2398,8 +2426,8 @@ var WaveformPlayer = class _WaveformPlayer {
2398
2426
  */
2399
2427
  refreshTheme() {
2400
2428
  if (!this._autoTheme) return;
2401
- this._scheme = detectColorScheme();
2402
- const preset = getColorPreset(this.options.colorPreset);
2429
+ this._scheme = detectColorScheme(this.container);
2430
+ const preset = getColorPreset(this.options.colorPreset, this.container);
2403
2431
  for (const key of this._presetKeys || []) {
2404
2432
  if (key in preset) this.options[key] = preset[key];
2405
2433
  }
@@ -2814,7 +2842,7 @@ var WaveformPlayer = class _WaveformPlayer {
2814
2842
  };
2815
2843
 
2816
2844
  // src/js/index.js
2817
- WaveformPlayer.utils = { formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes };
2845
+ WaveformPlayer.utils = { formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes, detectColorScheme };
2818
2846
  var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
2819
2847
  function autoInit() {
2820
2848
  if (!isBrowser()) return;