@arraypress/waveform-player 1.24.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.
- package/dist/waveform-player.cjs +57 -31
- package/dist/waveform-player.cjs.map +2 -2
- package/dist/waveform-player.esm.js +3 -3
- package/dist/waveform-player.esm.js.map +3 -3
- package/dist/waveform-player.js +57 -31
- package/dist/waveform-player.min.js +3 -3
- package/dist/waveform-player.min.js.map +3 -3
- package/index.d.ts +7 -0
- package/package.json +2 -1
- package/src/js/core.js +7 -5
- package/src/js/index.js +8 -3
- package/src/js/themes.js +119 -33
- package/src/js/utils.js +52 -12
package/dist/waveform-player.cjs
CHANGED
|
@@ -156,10 +156,11 @@ function formatSeekValueText(template, ...args) {
|
|
|
156
156
|
});
|
|
157
157
|
}
|
|
158
158
|
function formatTime(seconds) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
const
|
|
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
|
|
186
|
-
if (!
|
|
187
|
-
|
|
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
|
|
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
|
|
663
|
-
const
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
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;
|
|
@@ -2400,8 +2426,8 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
2400
2426
|
*/
|
|
2401
2427
|
refreshTheme() {
|
|
2402
2428
|
if (!this._autoTheme) return;
|
|
2403
|
-
this._scheme = detectColorScheme();
|
|
2404
|
-
const preset = getColorPreset(this.options.colorPreset);
|
|
2429
|
+
this._scheme = detectColorScheme(this.container);
|
|
2430
|
+
const preset = getColorPreset(this.options.colorPreset, this.container);
|
|
2405
2431
|
for (const key of this._presetKeys || []) {
|
|
2406
2432
|
if (key in preset) this.options[key] = preset[key];
|
|
2407
2433
|
}
|
|
@@ -2816,7 +2842,7 @@ var WaveformPlayer = class _WaveformPlayer {
|
|
|
2816
2842
|
};
|
|
2817
2843
|
|
|
2818
2844
|
// src/js/index.js
|
|
2819
|
-
WaveformPlayer.utils = { formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes };
|
|
2845
|
+
WaveformPlayer.utils = { formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes, detectColorScheme };
|
|
2820
2846
|
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
|
|
2821
2847
|
function autoInit() {
|
|
2822
2848
|
if (!isBrowser()) return;
|