@arraypress/waveform-bar 1.0.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.
- package/LICENSE +21 -0
- package/README.md +732 -0
- package/dist/waveform-bar.css +943 -0
- package/dist/waveform-bar.esm.js +1389 -0
- package/dist/waveform-bar.js +1387 -0
- package/dist/waveform-bar.min.css +1 -0
- package/dist/waveform-bar.min.js +51 -0
- package/package.json +60 -0
- package/src/css/waveform-bar.css +943 -0
- package/src/js/actions.js +35 -0
- package/src/js/core.js +1271 -0
- package/src/js/dom.js +84 -0
- package/src/js/icons.js +24 -0
- package/src/js/index.js +20 -0
- package/src/js/queue.js +113 -0
- package/src/js/storage.js +92 -0
- package/src/js/utils.js +74 -0
|
@@ -0,0 +1,1389 @@
|
|
|
1
|
+
// src/js/icons.js
|
|
2
|
+
var ICONS = {
|
|
3
|
+
play: '<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>',
|
|
4
|
+
pause: '<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M6 4h4v16H6zM14 4h4v16h-4z"/></svg>',
|
|
5
|
+
prev: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>',
|
|
6
|
+
next: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>',
|
|
7
|
+
queue: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z"/></svg>',
|
|
8
|
+
music: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" opacity="0.5"><path d="M12 3v10.55c-.59-.34-1.27-.55-2-.55C7.79 13 6 14.79 6 17s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z"/></svg>',
|
|
9
|
+
volHigh: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>',
|
|
10
|
+
volLow: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M18.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM5 9v6h4l5 5V4L9 9H5z"/></svg>',
|
|
11
|
+
volMute: '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z"/></svg>',
|
|
12
|
+
heart: '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>',
|
|
13
|
+
heartFilled: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>',
|
|
14
|
+
cart: '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/></svg>',
|
|
15
|
+
close: '<svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg>',
|
|
16
|
+
speaker: '<svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z"/></svg>',
|
|
17
|
+
repeatOff: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/></svg>',
|
|
18
|
+
repeatAll: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/></svg>',
|
|
19
|
+
repeatOne: '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/><text x="12" y="15" text-anchor="middle" font-size="7" font-weight="bold" fill="currentColor">1</text></svg>'
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/js/utils.js
|
|
23
|
+
function extractTitle(url) {
|
|
24
|
+
if (!url) return "Untitled";
|
|
25
|
+
return url.split("/").pop().split(".")[0].replace(/[-_]/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());
|
|
26
|
+
}
|
|
27
|
+
function escapeHtml(str) {
|
|
28
|
+
if (!str) return "";
|
|
29
|
+
const d = document.createElement("div");
|
|
30
|
+
d.textContent = str;
|
|
31
|
+
return d.innerHTML;
|
|
32
|
+
}
|
|
33
|
+
function formatTime(seconds) {
|
|
34
|
+
if (!seconds || isNaN(seconds)) return "0:00";
|
|
35
|
+
const m = Math.floor(seconds / 60);
|
|
36
|
+
const s = Math.floor(seconds % 60);
|
|
37
|
+
return `${m}:${s.toString().padStart(2, "0")}`;
|
|
38
|
+
}
|
|
39
|
+
function parseTrackFromElement(el) {
|
|
40
|
+
const url = el.dataset.wbUrl || el.dataset.url;
|
|
41
|
+
if (!url) return null;
|
|
42
|
+
let meta = {};
|
|
43
|
+
try {
|
|
44
|
+
meta = JSON.parse(el.dataset.wbMeta || el.dataset.meta || "{}");
|
|
45
|
+
} catch (e) {
|
|
46
|
+
}
|
|
47
|
+
let markers = null;
|
|
48
|
+
try {
|
|
49
|
+
markers = JSON.parse(el.dataset.wbMarkers || el.dataset.markers || "null");
|
|
50
|
+
} catch (e) {
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
url,
|
|
54
|
+
id: el.dataset.wbId || el.dataset.id || url,
|
|
55
|
+
title: el.dataset.wbTitle || el.dataset.title || extractTitle(url),
|
|
56
|
+
artist: el.dataset.wbArtist || el.dataset.artist || "",
|
|
57
|
+
artwork: el.dataset.wbArtwork || el.dataset.artwork || "",
|
|
58
|
+
album: el.dataset.wbAlbum || el.dataset.album || "",
|
|
59
|
+
link: el.dataset.wbLink || el.dataset.link || "",
|
|
60
|
+
duration: el.dataset.wbDuration || el.dataset.duration || "",
|
|
61
|
+
bpm: el.dataset.wbBpm || el.dataset.bpm || "",
|
|
62
|
+
key: el.dataset.wbKey || el.dataset.key || "",
|
|
63
|
+
waveform: el.dataset.wbWaveform || el.dataset.waveform || "",
|
|
64
|
+
markers,
|
|
65
|
+
favorited: el.dataset.wbFavorited === "true",
|
|
66
|
+
inCart: el.dataset.wbInCart === "true",
|
|
67
|
+
meta
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/js/storage.js
|
|
72
|
+
function saveQueueState(key, state) {
|
|
73
|
+
try {
|
|
74
|
+
sessionStorage.setItem(key, JSON.stringify(state));
|
|
75
|
+
} catch (e) {
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function restoreQueueState(key) {
|
|
79
|
+
try {
|
|
80
|
+
const raw = sessionStorage.getItem(key);
|
|
81
|
+
if (!raw) return null;
|
|
82
|
+
const d = JSON.parse(raw);
|
|
83
|
+
if (!d || !d.queue || !d.queue.length) return null;
|
|
84
|
+
return d;
|
|
85
|
+
} catch (e) {
|
|
86
|
+
sessionStorage.removeItem(key);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function saveVolume(key, volume, muted, volumeBeforeMute) {
|
|
91
|
+
try {
|
|
92
|
+
localStorage.setItem(key + "-vol", JSON.stringify({
|
|
93
|
+
v: volume,
|
|
94
|
+
m: muted,
|
|
95
|
+
b: volumeBeforeMute
|
|
96
|
+
}));
|
|
97
|
+
} catch (e) {
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function restoreVolume(key) {
|
|
101
|
+
try {
|
|
102
|
+
const d = JSON.parse(localStorage.getItem(key + "-vol"));
|
|
103
|
+
if (!d) return null;
|
|
104
|
+
return {
|
|
105
|
+
volume: d.v != null ? d.v : 1,
|
|
106
|
+
muted: d.m || false,
|
|
107
|
+
volumeBeforeMute: d.b || 1
|
|
108
|
+
};
|
|
109
|
+
} catch (e) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function saveFavorites(key, favorites) {
|
|
114
|
+
try {
|
|
115
|
+
localStorage.setItem(key + "-favs", JSON.stringify([...favorites]));
|
|
116
|
+
} catch (e) {
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function restoreFavorites(key) {
|
|
120
|
+
try {
|
|
121
|
+
const d = JSON.parse(localStorage.getItem(key + "-favs"));
|
|
122
|
+
return Array.isArray(d) ? new Set(d) : /* @__PURE__ */ new Set();
|
|
123
|
+
} catch (e) {
|
|
124
|
+
return /* @__PURE__ */ new Set();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/js/actions.js
|
|
129
|
+
function fireAction(actionConfig, payload) {
|
|
130
|
+
if (!actionConfig || !actionConfig.endpoint) return;
|
|
131
|
+
if (typeof actionConfig.endpoint === "function") {
|
|
132
|
+
try {
|
|
133
|
+
actionConfig.endpoint(payload);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
console.warn("WaveformBar action callback error:", err);
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (typeof actionConfig.endpoint === "string") {
|
|
140
|
+
fetch(actionConfig.endpoint, {
|
|
141
|
+
method: actionConfig.method || "POST",
|
|
142
|
+
headers: {
|
|
143
|
+
"Content-Type": "application/json",
|
|
144
|
+
...actionConfig.headers || {}
|
|
145
|
+
},
|
|
146
|
+
body: JSON.stringify(payload)
|
|
147
|
+
}).catch((err) => console.warn("WaveformBar action request failed:", err));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/js/dom.js
|
|
152
|
+
function buildBarHTML(config) {
|
|
153
|
+
let left = '<div class="wb-left">';
|
|
154
|
+
left += '<div class="wb-controls">';
|
|
155
|
+
if (config.showPrevNext) {
|
|
156
|
+
left += `<button class="wb-btn wb-prev" aria-label="Previous" title="Previous">${ICONS.prev}</button>`;
|
|
157
|
+
}
|
|
158
|
+
left += `<button class="wb-btn wb-play" aria-label="Play/Pause" title="Play">
|
|
159
|
+
<span class="wb-icon-play">${ICONS.play}</span>
|
|
160
|
+
<span class="wb-icon-pause" style="display:none">${ICONS.pause}</span>
|
|
161
|
+
</button>`;
|
|
162
|
+
if (config.showPrevNext) {
|
|
163
|
+
left += `<button class="wb-btn wb-next" aria-label="Next" title="Next">${ICONS.next}</button>`;
|
|
164
|
+
}
|
|
165
|
+
if (config.showRepeat) {
|
|
166
|
+
left += `<button class="wb-btn wb-btn-sm wb-repeat" aria-label="Repeat" title="Repeat: Off">${ICONS.repeatOff}</button>`;
|
|
167
|
+
}
|
|
168
|
+
left += "</div>";
|
|
169
|
+
left += `<div class="wb-track">
|
|
170
|
+
<div class="wb-artwork">${ICONS.music}</div>
|
|
171
|
+
<div class="wb-track-text">
|
|
172
|
+
<div class="wb-title">No track selected</div>
|
|
173
|
+
<div class="wb-artist">—</div>
|
|
174
|
+
</div>
|
|
175
|
+
</div>`;
|
|
176
|
+
left += "</div>";
|
|
177
|
+
const centre = `<div class="wb-centre">
|
|
178
|
+
<div class="wb-waveform-container"></div>
|
|
179
|
+
<div class="wb-time"><span class="wb-time-current">0:00</span> / <span class="wb-time-total">0:00</span></div>
|
|
180
|
+
</div>`;
|
|
181
|
+
let right = '<div class="wb-right">';
|
|
182
|
+
if (config.showMeta) {
|
|
183
|
+
right += '<div class="wb-meta"></div>';
|
|
184
|
+
}
|
|
185
|
+
if (config.actions) {
|
|
186
|
+
right += '<div class="wb-actions">';
|
|
187
|
+
if (config.actions.favorite) {
|
|
188
|
+
right += `<button class="wb-btn wb-btn-sm wb-fav" aria-label="Favorite" title="Favorite">${ICONS.heart}</button>`;
|
|
189
|
+
}
|
|
190
|
+
if (config.actions.cart) {
|
|
191
|
+
right += `<button class="wb-btn wb-btn-sm wb-cart" aria-label="Add to cart" title="Add to Cart">${ICONS.cart}</button>`;
|
|
192
|
+
}
|
|
193
|
+
right += "</div>";
|
|
194
|
+
}
|
|
195
|
+
if (config.showMute || config.showVolume) {
|
|
196
|
+
right += '<div class="wb-volume">';
|
|
197
|
+
right += `<button class="wb-btn wb-btn-sm wb-mute" aria-label="Volume" title="Volume">${ICONS.volHigh}</button>`;
|
|
198
|
+
if (config.showVolume) {
|
|
199
|
+
right += `<div class="wb-volume-popup">
|
|
200
|
+
<input type="range" class="wb-volume-slider" min="0" max="100" value="100" orient="vertical" aria-label="Volume">
|
|
201
|
+
</div>`;
|
|
202
|
+
}
|
|
203
|
+
right += "</div>";
|
|
204
|
+
}
|
|
205
|
+
if (config.showQueue) {
|
|
206
|
+
right += `<button class="wb-btn wb-btn-sm wb-queue-btn" aria-label="Queue" title="Queue">${ICONS.queue}</button>`;
|
|
207
|
+
}
|
|
208
|
+
right += "</div>";
|
|
209
|
+
return `<div class="wb-inner">${left}${centre}${right}</div>`;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/js/queue.js
|
|
213
|
+
function createQueuePanel() {
|
|
214
|
+
const el = document.createElement("div");
|
|
215
|
+
el.className = "wb-queue-panel";
|
|
216
|
+
el.innerHTML = `
|
|
217
|
+
<div class="wb-queue-header">
|
|
218
|
+
<div class="wb-queue-title">
|
|
219
|
+
${ICONS.queue}
|
|
220
|
+
Queue
|
|
221
|
+
<span class="wb-queue-count">0</span>
|
|
222
|
+
</div>
|
|
223
|
+
<button class="wb-btn wb-btn-sm wb-queue-clear" aria-label="Clear queue">Clear</button>
|
|
224
|
+
</div>
|
|
225
|
+
<div class="wb-queue-body"></div>
|
|
226
|
+
`;
|
|
227
|
+
return el;
|
|
228
|
+
}
|
|
229
|
+
function renderQueue(bodyEl, countEl, queue, currentIndex, callbacks) {
|
|
230
|
+
if (!bodyEl) return;
|
|
231
|
+
const upcoming = Math.max(0, queue.length - 1 - currentIndex);
|
|
232
|
+
if (countEl) countEl.textContent = upcoming;
|
|
233
|
+
if (queue.length === 0) {
|
|
234
|
+
bodyEl.innerHTML = `<div class="wb-queue-empty">${ICONS.queue}<p>Queue is empty</p></div>`;
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
let html = "";
|
|
238
|
+
if (currentIndex >= 0 && currentIndex < queue.length) {
|
|
239
|
+
const current = queue[currentIndex];
|
|
240
|
+
html += '<div class="wb-queue-label">Now Playing</div>';
|
|
241
|
+
html += `<div class="wb-queue-item wb-queue-current" data-qi="${currentIndex}">
|
|
242
|
+
<span class="wb-queue-num">${ICONS.speaker}</span>
|
|
243
|
+
<div class="wb-queue-info">
|
|
244
|
+
<div class="wb-queue-item-title">${escapeHtml(current.title)}</div>
|
|
245
|
+
<div class="wb-queue-item-artist">${escapeHtml(current.artist)}</div>
|
|
246
|
+
</div>
|
|
247
|
+
</div>`;
|
|
248
|
+
}
|
|
249
|
+
let hasNext = false;
|
|
250
|
+
for (let i = currentIndex + 1; i < queue.length; i++) {
|
|
251
|
+
if (!hasNext) {
|
|
252
|
+
html += '<div class="wb-queue-label">Next Up</div>';
|
|
253
|
+
hasNext = true;
|
|
254
|
+
}
|
|
255
|
+
const t = queue[i];
|
|
256
|
+
html += `<div class="wb-queue-item" data-qi="${i}">
|
|
257
|
+
<span class="wb-queue-num">${i - currentIndex}</span>
|
|
258
|
+
<div class="wb-queue-info">
|
|
259
|
+
<div class="wb-queue-item-title">${escapeHtml(t.title)}</div>
|
|
260
|
+
<div class="wb-queue-item-artist">${escapeHtml(t.artist)}</div>
|
|
261
|
+
</div>
|
|
262
|
+
<button class="wb-queue-remove" data-qi="${i}" aria-label="Remove">${ICONS.close}</button>
|
|
263
|
+
</div>`;
|
|
264
|
+
}
|
|
265
|
+
if (currentIndex > 0) {
|
|
266
|
+
html += '<div class="wb-queue-label">Previously Played</div>';
|
|
267
|
+
for (let j = currentIndex - 1; j >= 0; j--) {
|
|
268
|
+
const t = queue[j];
|
|
269
|
+
html += `<div class="wb-queue-item wb-queue-played" data-qi="${j}">
|
|
270
|
+
<span class="wb-queue-num">${j + 1}</span>
|
|
271
|
+
<div class="wb-queue-info">
|
|
272
|
+
<div class="wb-queue-item-title">${escapeHtml(t.title)}</div>
|
|
273
|
+
<div class="wb-queue-item-artist">${escapeHtml(t.artist)}</div>
|
|
274
|
+
</div>
|
|
275
|
+
</div>`;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
bodyEl.innerHTML = html;
|
|
279
|
+
bodyEl.querySelectorAll(".wb-queue-item[data-qi]").forEach((el) => {
|
|
280
|
+
el.addEventListener("click", (e) => {
|
|
281
|
+
if (e.target.closest(".wb-queue-remove")) return;
|
|
282
|
+
if (callbacks.onSkipTo) callbacks.onSkipTo(parseInt(el.dataset.qi));
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
bodyEl.querySelectorAll(".wb-queue-remove").forEach((btn) => {
|
|
286
|
+
btn.addEventListener("click", (e) => {
|
|
287
|
+
e.stopPropagation();
|
|
288
|
+
if (callbacks.onRemove) callbacks.onRemove(parseInt(btn.dataset.qi));
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/js/core.js
|
|
294
|
+
var DEFAULTS = {
|
|
295
|
+
persist: true,
|
|
296
|
+
autoResume: true,
|
|
297
|
+
continuous: true,
|
|
298
|
+
repeat: "off",
|
|
299
|
+
// 'off', 'all', 'one'
|
|
300
|
+
showRepeat: true,
|
|
301
|
+
showQueue: true,
|
|
302
|
+
showPrevNext: true,
|
|
303
|
+
showVolume: true,
|
|
304
|
+
showMute: true,
|
|
305
|
+
showMeta: true,
|
|
306
|
+
showTime: true,
|
|
307
|
+
showTrackLink: true,
|
|
308
|
+
maxMeta: 3,
|
|
309
|
+
defaultArtwork: null,
|
|
310
|
+
// URL to fallback artwork image
|
|
311
|
+
theme: null,
|
|
312
|
+
// 'dark', 'light', or null (dark by default)
|
|
313
|
+
waveformStyle: "mirror",
|
|
314
|
+
waveformHeight: 32,
|
|
315
|
+
barWidth: 2,
|
|
316
|
+
barSpacing: 0,
|
|
317
|
+
waveformColor: null,
|
|
318
|
+
progressColor: null,
|
|
319
|
+
markerColor: "rgba(255, 255, 255, 0.25)",
|
|
320
|
+
volume: 1,
|
|
321
|
+
storageKey: "waveform-bar",
|
|
322
|
+
actions: null,
|
|
323
|
+
onPlay: null,
|
|
324
|
+
onPause: null,
|
|
325
|
+
onTrackChange: null,
|
|
326
|
+
onQueueChange: null,
|
|
327
|
+
onVolumeChange: null,
|
|
328
|
+
onFavorite: null,
|
|
329
|
+
onCart: null
|
|
330
|
+
};
|
|
331
|
+
var WaveformBar = class {
|
|
332
|
+
constructor() {
|
|
333
|
+
this.config = null;
|
|
334
|
+
this.player = null;
|
|
335
|
+
this.queue = [];
|
|
336
|
+
this.currentIndex = -1;
|
|
337
|
+
this.isPlaying = false;
|
|
338
|
+
this.isInitialized = false;
|
|
339
|
+
this.queueOpen = false;
|
|
340
|
+
this.volume = 1;
|
|
341
|
+
this.isMuted = false;
|
|
342
|
+
this._volumeBeforeMute = 1;
|
|
343
|
+
this._lastPosition = 0;
|
|
344
|
+
this._favorites = /* @__PURE__ */ new Set();
|
|
345
|
+
this._cartItems = /* @__PURE__ */ new Set();
|
|
346
|
+
this._observer = null;
|
|
347
|
+
this._activeMarkers = null;
|
|
348
|
+
this._currentMarkerIndex = -1;
|
|
349
|
+
this.repeat = "off";
|
|
350
|
+
this.barEl = null;
|
|
351
|
+
this.queueEl = null;
|
|
352
|
+
this.waveformContainer = null;
|
|
353
|
+
this.volumePopupEl = null;
|
|
354
|
+
this.titleEl = null;
|
|
355
|
+
this.artistEl = null;
|
|
356
|
+
this.metaEl = null;
|
|
357
|
+
this.playBtnEl = null;
|
|
358
|
+
this.repeatBtnEl = null;
|
|
359
|
+
this.queueBtnEl = null;
|
|
360
|
+
this.queueBodyEl = null;
|
|
361
|
+
this.queueCountEl = null;
|
|
362
|
+
this.volumeSliderEl = null;
|
|
363
|
+
this.muteBtnEl = null;
|
|
364
|
+
this.favBtnEl = null;
|
|
365
|
+
this.cartBtnEl = null;
|
|
366
|
+
this.timeCurrentEl = null;
|
|
367
|
+
this.timeTotalEl = null;
|
|
368
|
+
}
|
|
369
|
+
// =====================================================================
|
|
370
|
+
// Init / Destroy
|
|
371
|
+
// =====================================================================
|
|
372
|
+
/**
|
|
373
|
+
* Initialize WaveformBar
|
|
374
|
+
* @param {Object} [config={}]
|
|
375
|
+
* @returns {WaveformBar}
|
|
376
|
+
*/
|
|
377
|
+
init(config = {}) {
|
|
378
|
+
if (this.isInitialized) this.destroy();
|
|
379
|
+
this.config = { ...DEFAULTS, ...config };
|
|
380
|
+
this.volume = this.config.volume;
|
|
381
|
+
if (typeof window.WaveformPlayer === "undefined") {
|
|
382
|
+
console.error("WaveformBar: WaveformPlayer is required.");
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
this._createBar();
|
|
386
|
+
this._createQueue();
|
|
387
|
+
this._initPlayer();
|
|
388
|
+
this._bindTriggers();
|
|
389
|
+
this._observeDOM();
|
|
390
|
+
if (this.config.persist) {
|
|
391
|
+
this._restoreVolume();
|
|
392
|
+
this._restoreFavorites();
|
|
393
|
+
}
|
|
394
|
+
this._seedFromAttributes();
|
|
395
|
+
if (this.config.persist) {
|
|
396
|
+
this._restoreState();
|
|
397
|
+
}
|
|
398
|
+
this.isInitialized = true;
|
|
399
|
+
this._beforeUnloadHandler = () => this._saveState();
|
|
400
|
+
window.addEventListener("beforeunload", this._beforeUnloadHandler);
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Destroy everything
|
|
405
|
+
* @returns {WaveformBar}
|
|
406
|
+
*/
|
|
407
|
+
destroy() {
|
|
408
|
+
if (this.player) {
|
|
409
|
+
this.player.destroy();
|
|
410
|
+
this.player = null;
|
|
411
|
+
}
|
|
412
|
+
if (this.barEl) {
|
|
413
|
+
this.barEl.remove();
|
|
414
|
+
this.barEl = null;
|
|
415
|
+
}
|
|
416
|
+
if (this.queueEl) {
|
|
417
|
+
this.queueEl.remove();
|
|
418
|
+
this.queueEl = null;
|
|
419
|
+
}
|
|
420
|
+
if (this._observer) {
|
|
421
|
+
this._observer.disconnect();
|
|
422
|
+
this._observer = null;
|
|
423
|
+
}
|
|
424
|
+
if (this._beforeUnloadHandler) {
|
|
425
|
+
window.removeEventListener("beforeunload", this._beforeUnloadHandler);
|
|
426
|
+
this._beforeUnloadHandler = null;
|
|
427
|
+
}
|
|
428
|
+
document.querySelectorAll("[data-wb-play],[data-wb-queue]").forEach((el) => delete el._wbBound);
|
|
429
|
+
document.querySelectorAll(".wb-current,.wb-playing").forEach((el) => el.classList.remove("wb-current", "wb-playing"));
|
|
430
|
+
this.queue = [];
|
|
431
|
+
this.currentIndex = -1;
|
|
432
|
+
this.isPlaying = false;
|
|
433
|
+
this.queueOpen = false;
|
|
434
|
+
this.isInitialized = false;
|
|
435
|
+
this.config = null;
|
|
436
|
+
return this;
|
|
437
|
+
}
|
|
438
|
+
// =====================================================================
|
|
439
|
+
// DOM Setup (private)
|
|
440
|
+
// =====================================================================
|
|
441
|
+
_createBar() {
|
|
442
|
+
this.barEl = document.createElement("div");
|
|
443
|
+
this.barEl.className = "waveform-bar";
|
|
444
|
+
const theme = this.config.theme || this._detectTheme();
|
|
445
|
+
if (theme === "light") this.barEl.classList.add("wb-light");
|
|
446
|
+
this._resolvedTheme = theme;
|
|
447
|
+
this.barEl.id = "waveform-bar";
|
|
448
|
+
this.barEl.innerHTML = buildBarHTML(this.config);
|
|
449
|
+
document.body.appendChild(this.barEl);
|
|
450
|
+
this.titleEl = this.barEl.querySelector(".wb-title");
|
|
451
|
+
this.artistEl = this.barEl.querySelector(".wb-artist");
|
|
452
|
+
this.metaEl = this.barEl.querySelector(".wb-meta");
|
|
453
|
+
this.playBtnEl = this.barEl.querySelector(".wb-play");
|
|
454
|
+
this.waveformContainer = this.barEl.querySelector(".wb-waveform-container");
|
|
455
|
+
this.queueBtnEl = this.barEl.querySelector(".wb-queue-btn");
|
|
456
|
+
this.muteBtnEl = this.barEl.querySelector(".wb-mute");
|
|
457
|
+
this.volumeSliderEl = this.barEl.querySelector(".wb-volume-slider");
|
|
458
|
+
this.favBtnEl = this.barEl.querySelector(".wb-fav");
|
|
459
|
+
this.cartBtnEl = this.barEl.querySelector(".wb-cart");
|
|
460
|
+
this.timeCurrentEl = this.barEl.querySelector(".wb-time-current");
|
|
461
|
+
this.timeTotalEl = this.barEl.querySelector(".wb-time-total");
|
|
462
|
+
this.playBtnEl.addEventListener("click", () => this.togglePlay());
|
|
463
|
+
const prevBtn = this.barEl.querySelector(".wb-prev");
|
|
464
|
+
const nextBtn = this.barEl.querySelector(".wb-next");
|
|
465
|
+
if (prevBtn) prevBtn.addEventListener("click", () => this.previous());
|
|
466
|
+
if (nextBtn) nextBtn.addEventListener("click", () => this.next());
|
|
467
|
+
this.repeatBtnEl = this.barEl.querySelector(".wb-repeat");
|
|
468
|
+
if (this.repeatBtnEl) {
|
|
469
|
+
this.repeat = this.config.repeat || "off";
|
|
470
|
+
this._updateRepeatButton();
|
|
471
|
+
this.repeatBtnEl.addEventListener("click", () => this.cycleRepeat());
|
|
472
|
+
}
|
|
473
|
+
if (this.queueBtnEl) this.queueBtnEl.addEventListener("click", () => this.toggleQueuePanel());
|
|
474
|
+
this.volumePopupEl = this.barEl.querySelector(".wb-volume-popup");
|
|
475
|
+
const volumeWrapper = this.barEl.querySelector(".wb-volume");
|
|
476
|
+
if (this.muteBtnEl) {
|
|
477
|
+
this.muteBtnEl.addEventListener("click", (e) => {
|
|
478
|
+
e.stopPropagation();
|
|
479
|
+
this.toggleMute();
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
if (volumeWrapper && this.volumePopupEl) {
|
|
483
|
+
let hoverTimeout;
|
|
484
|
+
volumeWrapper.addEventListener("mouseenter", () => {
|
|
485
|
+
clearTimeout(hoverTimeout);
|
|
486
|
+
this.openVolumePopup();
|
|
487
|
+
});
|
|
488
|
+
volumeWrapper.addEventListener("mouseleave", () => {
|
|
489
|
+
hoverTimeout = setTimeout(() => this.closeVolumePopup(), 300);
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
if (this.volumeSliderEl) {
|
|
493
|
+
this.volumeSliderEl.addEventListener("input", (e) => {
|
|
494
|
+
e.stopPropagation();
|
|
495
|
+
this.setVolume(parseInt(e.target.value) / 100);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
document.addEventListener("click", (e) => {
|
|
499
|
+
if (this.volumePopupEl?.classList.contains("wb-volume-open") && !this.barEl.querySelector(".wb-volume")?.contains(e.target)) {
|
|
500
|
+
this.closeVolumePopup();
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
if (this.favBtnEl) this.favBtnEl.addEventListener("click", () => this.toggleFavorite());
|
|
504
|
+
if (this.cartBtnEl) this.cartBtnEl.addEventListener("click", () => this.addToCart());
|
|
505
|
+
if (this.config.showTrackLink) {
|
|
506
|
+
this.barEl.querySelector(".wb-track").addEventListener("click", () => {
|
|
507
|
+
const t = this.getCurrentTrack();
|
|
508
|
+
if (t && t.link) window.location.href = t.link;
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
_createQueue() {
|
|
513
|
+
if (!this.config.showQueue) return;
|
|
514
|
+
this.queueEl = createQueuePanel();
|
|
515
|
+
if (this._resolvedTheme === "light") this.queueEl.classList.add("wb-light");
|
|
516
|
+
document.body.appendChild(this.queueEl);
|
|
517
|
+
this.queueBodyEl = this.queueEl.querySelector(".wb-queue-body");
|
|
518
|
+
this.queueCountEl = this.queueEl.querySelector(".wb-queue-count");
|
|
519
|
+
this.queueEl.querySelector(".wb-queue-clear").addEventListener("click", () => this.clearQueue());
|
|
520
|
+
document.addEventListener("click", (e) => {
|
|
521
|
+
if (this.queueOpen && !this.queueEl.contains(e.target) && !this.queueBtnEl.contains(e.target)) {
|
|
522
|
+
this.closeQueuePanel();
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
_initPlayer() {
|
|
527
|
+
const opts = {
|
|
528
|
+
showControls: false,
|
|
529
|
+
showInfo: false,
|
|
530
|
+
waveformStyle: this.config.waveformStyle,
|
|
531
|
+
height: this.config.waveformHeight,
|
|
532
|
+
barWidth: this.config.barWidth,
|
|
533
|
+
barSpacing: this.config.barSpacing,
|
|
534
|
+
singlePlay: false,
|
|
535
|
+
onPlay: () => {
|
|
536
|
+
this.isPlaying = true;
|
|
537
|
+
this._updatePlayButton();
|
|
538
|
+
this._syncPageState();
|
|
539
|
+
const track = this.getCurrentTrack();
|
|
540
|
+
this._emit("play", { track });
|
|
541
|
+
if (this.config.onPlay) this.config.onPlay(track);
|
|
542
|
+
},
|
|
543
|
+
onPause: () => {
|
|
544
|
+
this.isPlaying = false;
|
|
545
|
+
this._updatePlayButton();
|
|
546
|
+
this._syncPageState();
|
|
547
|
+
this._saveState();
|
|
548
|
+
const track = this.getCurrentTrack();
|
|
549
|
+
this._emit("pause", { track });
|
|
550
|
+
if (this.config.onPause) this.config.onPause(track);
|
|
551
|
+
},
|
|
552
|
+
onEnd: () => {
|
|
553
|
+
this.isPlaying = false;
|
|
554
|
+
this._updatePlayButton();
|
|
555
|
+
this._syncPageState();
|
|
556
|
+
if (this.timeCurrentEl) this.timeCurrentEl.textContent = "0:00";
|
|
557
|
+
if (this.repeat === "one") {
|
|
558
|
+
if (this.player) {
|
|
559
|
+
this.player.seekTo(0);
|
|
560
|
+
this.player.play().catch(() => {
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (this.config.continuous && this.currentIndex < this.queue.length - 1) {
|
|
566
|
+
this.currentIndex++;
|
|
567
|
+
this._loadCurrentTrack();
|
|
568
|
+
} else if (this.repeat === "all" && this.queue.length > 0) {
|
|
569
|
+
this.currentIndex = 0;
|
|
570
|
+
this._loadCurrentTrack();
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
onTimeUpdate: (currentTime, duration) => {
|
|
574
|
+
this._lastPosition = currentTime;
|
|
575
|
+
if (this.timeCurrentEl) this.timeCurrentEl.textContent = formatTime(currentTime);
|
|
576
|
+
if (this.timeTotalEl) this.timeTotalEl.textContent = formatTime(duration);
|
|
577
|
+
if (!this._lastSaveTime || currentTime - this._lastSaveTime > 2) {
|
|
578
|
+
this._lastSaveTime = currentTime;
|
|
579
|
+
this._saveState();
|
|
580
|
+
}
|
|
581
|
+
if (this._activeMarkers) {
|
|
582
|
+
this._checkMarkerBoundary(currentTime);
|
|
583
|
+
}
|
|
584
|
+
},
|
|
585
|
+
onLoad: null
|
|
586
|
+
};
|
|
587
|
+
if (this.config.waveformColor) opts.waveformColor = this.config.waveformColor;
|
|
588
|
+
if (this.config.progressColor) opts.progressColor = this.config.progressColor;
|
|
589
|
+
this.player = new window.WaveformPlayer(this.waveformContainer, opts);
|
|
590
|
+
this.player.setVolume(this.volume);
|
|
591
|
+
}
|
|
592
|
+
// =====================================================================
|
|
593
|
+
// Triggers (private)
|
|
594
|
+
// =====================================================================
|
|
595
|
+
_bindTriggers() {
|
|
596
|
+
document.querySelectorAll("[data-wb-play]").forEach((el) => {
|
|
597
|
+
if (el._wbBound) return;
|
|
598
|
+
el._wbBound = true;
|
|
599
|
+
el.addEventListener("click", (e) => {
|
|
600
|
+
e.preventDefault();
|
|
601
|
+
const track = parseTrackFromElement(el);
|
|
602
|
+
if (track) this.play(track);
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
document.querySelectorAll("[data-wb-queue]").forEach((el) => {
|
|
606
|
+
if (el._wbBound) return;
|
|
607
|
+
el._wbBound = true;
|
|
608
|
+
el.addEventListener("click", (e) => {
|
|
609
|
+
e.preventDefault();
|
|
610
|
+
e.stopPropagation();
|
|
611
|
+
const track = parseTrackFromElement(el);
|
|
612
|
+
if (track) this.addToQueue(track);
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
_observeDOM() {
|
|
617
|
+
if (typeof MutationObserver === "undefined") return;
|
|
618
|
+
this._observer = new MutationObserver(() => {
|
|
619
|
+
this._bindTriggers();
|
|
620
|
+
this._syncPageState();
|
|
621
|
+
});
|
|
622
|
+
this._observer.observe(document.body, { childList: true, subtree: true });
|
|
623
|
+
}
|
|
624
|
+
// =====================================================================
|
|
625
|
+
// Playback (public)
|
|
626
|
+
// =====================================================================
|
|
627
|
+
/**
|
|
628
|
+
* Play a track immediately
|
|
629
|
+
* @param {Object|string} trackOrUrl
|
|
630
|
+
* @returns {WaveformBar}
|
|
631
|
+
*/
|
|
632
|
+
play(trackOrUrl) {
|
|
633
|
+
const track = typeof trackOrUrl === "string" ? { url: trackOrUrl, id: trackOrUrl, title: extractTitle(trackOrUrl) } : trackOrUrl;
|
|
634
|
+
if (!track || !track.url) return this;
|
|
635
|
+
const current = this.getCurrentTrack();
|
|
636
|
+
if (current && current.url === track.url) {
|
|
637
|
+
this.togglePlay();
|
|
638
|
+
return this;
|
|
639
|
+
}
|
|
640
|
+
const existing = this.queue.findIndex((t) => t.url === track.url);
|
|
641
|
+
if (existing >= 0) {
|
|
642
|
+
this.queue[existing] = { ...this.queue[existing], ...track };
|
|
643
|
+
this.currentIndex = existing;
|
|
644
|
+
} else {
|
|
645
|
+
const insertAt = this.currentIndex + 1;
|
|
646
|
+
this.queue.splice(insertAt, 0, track);
|
|
647
|
+
this.currentIndex = insertAt;
|
|
648
|
+
}
|
|
649
|
+
this._loadCurrentTrack();
|
|
650
|
+
return this;
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Add to end of queue
|
|
654
|
+
* @param {Object|string} trackOrUrl
|
|
655
|
+
* @returns {WaveformBar}
|
|
656
|
+
*/
|
|
657
|
+
addToQueue(trackOrUrl) {
|
|
658
|
+
const track = typeof trackOrUrl === "string" ? { url: trackOrUrl, id: trackOrUrl, title: extractTitle(trackOrUrl) } : trackOrUrl;
|
|
659
|
+
if (!track || !track.url) return this;
|
|
660
|
+
if (this.queue.find((t) => t.url === track.url)) return this;
|
|
661
|
+
this.queue.push(track);
|
|
662
|
+
this._renderQueue();
|
|
663
|
+
this._saveState();
|
|
664
|
+
this._updateNavButtons();
|
|
665
|
+
if (this.currentIndex === -1) {
|
|
666
|
+
this.currentIndex = 0;
|
|
667
|
+
this._loadCurrentTrack();
|
|
668
|
+
}
|
|
669
|
+
if (this.config.onQueueChange) this.config.onQueueChange(this.queue, this.currentIndex);
|
|
670
|
+
return this;
|
|
671
|
+
}
|
|
672
|
+
togglePlay() {
|
|
673
|
+
if (!this.player) return this;
|
|
674
|
+
this.isPlaying ? this.player.pause() : this.player.play();
|
|
675
|
+
return this;
|
|
676
|
+
}
|
|
677
|
+
pause() {
|
|
678
|
+
if (this.player && this.isPlaying) this.player.pause();
|
|
679
|
+
return this;
|
|
680
|
+
}
|
|
681
|
+
next() {
|
|
682
|
+
if (this.currentIndex < this.queue.length - 1) {
|
|
683
|
+
this.currentIndex++;
|
|
684
|
+
this._loadCurrentTrack();
|
|
685
|
+
} else if (this.repeat === "all" && this.queue.length > 0) {
|
|
686
|
+
this.currentIndex = 0;
|
|
687
|
+
this._loadCurrentTrack();
|
|
688
|
+
}
|
|
689
|
+
return this;
|
|
690
|
+
}
|
|
691
|
+
previous() {
|
|
692
|
+
if (this.player && this.player.audio && this.player.audio.currentTime > 3) {
|
|
693
|
+
this.player.seekTo(0);
|
|
694
|
+
return this;
|
|
695
|
+
}
|
|
696
|
+
if (this.currentIndex > 0) {
|
|
697
|
+
this.currentIndex--;
|
|
698
|
+
this._loadCurrentTrack();
|
|
699
|
+
} else if (this.repeat === "all" && this.queue.length > 0) {
|
|
700
|
+
this.currentIndex = this.queue.length - 1;
|
|
701
|
+
this._loadCurrentTrack();
|
|
702
|
+
}
|
|
703
|
+
return this;
|
|
704
|
+
}
|
|
705
|
+
skipTo(index) {
|
|
706
|
+
if (index < 0 || index >= this.queue.length) return this;
|
|
707
|
+
if (index === this.currentIndex) {
|
|
708
|
+
this.togglePlay();
|
|
709
|
+
return this;
|
|
710
|
+
}
|
|
711
|
+
this.currentIndex = index;
|
|
712
|
+
this._loadCurrentTrack();
|
|
713
|
+
return this;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Seek to a specific marker by index on the current track
|
|
717
|
+
* @param {number} markerIndex
|
|
718
|
+
* @returns {WaveformBar}
|
|
719
|
+
*/
|
|
720
|
+
seekToMarker(markerIndex) {
|
|
721
|
+
if (!this._activeMarkers || markerIndex < 0 || markerIndex >= this._activeMarkers.length) return this;
|
|
722
|
+
const marker = this._activeMarkers[markerIndex];
|
|
723
|
+
if (this.player) {
|
|
724
|
+
this.player.seekTo(marker.time);
|
|
725
|
+
if (!this.isPlaying) this.togglePlay();
|
|
726
|
+
}
|
|
727
|
+
return this;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Seek to a marker by label on the current track
|
|
731
|
+
* @param {string} label
|
|
732
|
+
* @returns {WaveformBar}
|
|
733
|
+
*/
|
|
734
|
+
seekToMarkerByLabel(label) {
|
|
735
|
+
if (!this._activeMarkers) return this;
|
|
736
|
+
const index = this._activeMarkers.findIndex(
|
|
737
|
+
(m) => (m.label || m.title || "").toLowerCase() === label.toLowerCase()
|
|
738
|
+
);
|
|
739
|
+
if (index >= 0) this.seekToMarker(index);
|
|
740
|
+
return this;
|
|
741
|
+
}
|
|
742
|
+
// =====================================================================
|
|
743
|
+
// Volume (public)
|
|
744
|
+
// =====================================================================
|
|
745
|
+
setVolume(level) {
|
|
746
|
+
this.volume = Math.max(0, Math.min(1, level));
|
|
747
|
+
this.isMuted = this.volume === 0;
|
|
748
|
+
if (this.player) this.player.setVolume(this.volume);
|
|
749
|
+
this._updateVolumeUI();
|
|
750
|
+
saveVolume(this.config.storageKey, this.volume, this.isMuted, this._volumeBeforeMute);
|
|
751
|
+
this._emit("volumechange", { volume: this.volume });
|
|
752
|
+
if (this.config.onVolumeChange) this.config.onVolumeChange(this.volume);
|
|
753
|
+
return this;
|
|
754
|
+
}
|
|
755
|
+
getVolume() {
|
|
756
|
+
return this.volume;
|
|
757
|
+
}
|
|
758
|
+
toggleMute() {
|
|
759
|
+
if (this.isMuted) {
|
|
760
|
+
this.setVolume(this._volumeBeforeMute || 1);
|
|
761
|
+
} else {
|
|
762
|
+
this._volumeBeforeMute = this.volume;
|
|
763
|
+
this.isMuted = true;
|
|
764
|
+
if (this.player) this.player.setVolume(0);
|
|
765
|
+
this._updateVolumeUI();
|
|
766
|
+
}
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
// =====================================================================
|
|
770
|
+
// Actions (public)
|
|
771
|
+
// =====================================================================
|
|
772
|
+
toggleFavorite() {
|
|
773
|
+
const track = this.getCurrentTrack();
|
|
774
|
+
if (!track) return this;
|
|
775
|
+
const id = track.id || track.url;
|
|
776
|
+
const wasFav = this._favorites.has(id);
|
|
777
|
+
if (wasFav) {
|
|
778
|
+
this._favorites.delete(id);
|
|
779
|
+
} else {
|
|
780
|
+
this._favorites.add(id);
|
|
781
|
+
}
|
|
782
|
+
this._updateFavoriteUI();
|
|
783
|
+
this._syncFavoriteAttributes(track.url, !wasFav);
|
|
784
|
+
saveFavorites(this.config.storageKey, this._favorites);
|
|
785
|
+
this._emit("favorite", { track, favorited: !wasFav });
|
|
786
|
+
if (this.config.onFavorite) this.config.onFavorite(track, !wasFav);
|
|
787
|
+
if (this.config.actions?.favorite) {
|
|
788
|
+
fireAction(this.config.actions.favorite, {
|
|
789
|
+
action: "favorite",
|
|
790
|
+
id,
|
|
791
|
+
url: track.url,
|
|
792
|
+
title: track.title,
|
|
793
|
+
favorited: !wasFav
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
addToCart() {
|
|
799
|
+
const track = this.getCurrentTrack();
|
|
800
|
+
if (!track) return this;
|
|
801
|
+
const id = track.id || track.url;
|
|
802
|
+
this._cartItems.add(id);
|
|
803
|
+
if (this.cartBtnEl) {
|
|
804
|
+
this.cartBtnEl.classList.add("wb-action-done");
|
|
805
|
+
setTimeout(() => this.cartBtnEl.classList.remove("wb-action-done"), 1500);
|
|
806
|
+
}
|
|
807
|
+
this._syncCartAttributes(track.url, true);
|
|
808
|
+
this._emit("cart", { track });
|
|
809
|
+
if (this.config.onCart) this.config.onCart(track);
|
|
810
|
+
if (this.config.actions?.cart) {
|
|
811
|
+
fireAction(this.config.actions.cart, {
|
|
812
|
+
action: "cart",
|
|
813
|
+
id,
|
|
814
|
+
url: track.url,
|
|
815
|
+
title: track.title
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
return this;
|
|
819
|
+
}
|
|
820
|
+
isFavorited(id) {
|
|
821
|
+
if (!id) {
|
|
822
|
+
const t = this.getCurrentTrack();
|
|
823
|
+
id = t ? t.id || t.url : null;
|
|
824
|
+
}
|
|
825
|
+
return id ? this._favorites.has(id) : false;
|
|
826
|
+
}
|
|
827
|
+
isInCart(id) {
|
|
828
|
+
if (!id) {
|
|
829
|
+
const t = this.getCurrentTrack();
|
|
830
|
+
id = t ? t.id || t.url : null;
|
|
831
|
+
}
|
|
832
|
+
return id ? this._cartItems.has(id) : false;
|
|
833
|
+
}
|
|
834
|
+
// =====================================================================
|
|
835
|
+
// Queue (public)
|
|
836
|
+
// =====================================================================
|
|
837
|
+
removeFromQueue(index) {
|
|
838
|
+
if (index < 0 || index >= this.queue.length || index === this.currentIndex) return this;
|
|
839
|
+
this.queue.splice(index, 1);
|
|
840
|
+
if (index < this.currentIndex) this.currentIndex--;
|
|
841
|
+
this._renderQueue();
|
|
842
|
+
this._saveState();
|
|
843
|
+
this._updateNavButtons();
|
|
844
|
+
this._emit("queuechange", { queue: this.queue, currentIndex: this.currentIndex });
|
|
845
|
+
if (this.config.onQueueChange) this.config.onQueueChange(this.queue, this.currentIndex);
|
|
846
|
+
return this;
|
|
847
|
+
}
|
|
848
|
+
clearQueue() {
|
|
849
|
+
const current = this.getCurrentTrack();
|
|
850
|
+
this.queue = current ? [current] : [];
|
|
851
|
+
this.currentIndex = current ? 0 : -1;
|
|
852
|
+
this._renderQueue();
|
|
853
|
+
this._saveState();
|
|
854
|
+
this._updateNavButtons();
|
|
855
|
+
this._emit("queuechange", { queue: this.queue, currentIndex: this.currentIndex });
|
|
856
|
+
if (this.config.onQueueChange) this.config.onQueueChange(this.queue, this.currentIndex);
|
|
857
|
+
return this;
|
|
858
|
+
}
|
|
859
|
+
getCurrentTrack() {
|
|
860
|
+
return this.currentIndex >= 0 && this.currentIndex < this.queue.length ? this.queue[this.currentIndex] : null;
|
|
861
|
+
}
|
|
862
|
+
getQueue() {
|
|
863
|
+
return [...this.queue];
|
|
864
|
+
}
|
|
865
|
+
getCurrentIndex() {
|
|
866
|
+
return this.currentIndex;
|
|
867
|
+
}
|
|
868
|
+
isCurrentlyPlaying(url) {
|
|
869
|
+
const c = this.getCurrentTrack();
|
|
870
|
+
return this.isPlaying && c && c.url === url;
|
|
871
|
+
}
|
|
872
|
+
isCurrentTrack(url) {
|
|
873
|
+
const c = this.getCurrentTrack();
|
|
874
|
+
return c && c.url === url;
|
|
875
|
+
}
|
|
876
|
+
getPlayer() {
|
|
877
|
+
return this.player;
|
|
878
|
+
}
|
|
879
|
+
// =====================================================================
|
|
880
|
+
// Events
|
|
881
|
+
// =====================================================================
|
|
882
|
+
/**
|
|
883
|
+
* Dispatch a custom DOM event on the bar element.
|
|
884
|
+
* All events bubble and are prefixed with 'waveformbar:'.
|
|
885
|
+
*
|
|
886
|
+
* Events dispatched:
|
|
887
|
+
* - waveformbar:play { track }
|
|
888
|
+
* - waveformbar:pause { track }
|
|
889
|
+
* - waveformbar:trackchange { track, index }
|
|
890
|
+
* - waveformbar:markerchange { marker, index, track }
|
|
891
|
+
* - waveformbar:favorite { track, favorited }
|
|
892
|
+
* - waveformbar:cart { track }
|
|
893
|
+
* - waveformbar:queuechange { queue, currentIndex }
|
|
894
|
+
* - waveformbar:volumechange { volume }
|
|
895
|
+
*
|
|
896
|
+
* @private
|
|
897
|
+
* @param {string} name - Event name (without prefix)
|
|
898
|
+
* @param {Object} detail - Event detail data
|
|
899
|
+
*/
|
|
900
|
+
_emit(name, detail = {}) {
|
|
901
|
+
if (!this.barEl) return;
|
|
902
|
+
this.barEl.dispatchEvent(new CustomEvent("waveformbar:" + name, {
|
|
903
|
+
bubbles: true,
|
|
904
|
+
detail
|
|
905
|
+
}));
|
|
906
|
+
}
|
|
907
|
+
// =====================================================================
|
|
908
|
+
// UI: Bar visibility & Queue panel
|
|
909
|
+
// =====================================================================
|
|
910
|
+
show() {
|
|
911
|
+
if (this.barEl) this.barEl.classList.add("wb-active");
|
|
912
|
+
return this;
|
|
913
|
+
}
|
|
914
|
+
hide() {
|
|
915
|
+
if (this.barEl) this.barEl.classList.remove("wb-active");
|
|
916
|
+
this.closeQueuePanel();
|
|
917
|
+
this.closeVolumePopup();
|
|
918
|
+
return this;
|
|
919
|
+
}
|
|
920
|
+
toggleQueuePanel() {
|
|
921
|
+
return this.queueOpen ? this.closeQueuePanel() : this.openQueuePanel();
|
|
922
|
+
}
|
|
923
|
+
openQueuePanel() {
|
|
924
|
+
if (!this.queueEl) return this;
|
|
925
|
+
this.queueOpen = true;
|
|
926
|
+
this.closeVolumePopup();
|
|
927
|
+
if (this.queueBtnEl) {
|
|
928
|
+
const rect = this.queueBtnEl.getBoundingClientRect();
|
|
929
|
+
this.queueEl.style.right = window.innerWidth - rect.right + "px";
|
|
930
|
+
}
|
|
931
|
+
this.queueEl.classList.add("wb-queue-open");
|
|
932
|
+
if (this.queueBtnEl) this.queueBtnEl.classList.add("wb-active");
|
|
933
|
+
this._renderQueue();
|
|
934
|
+
return this;
|
|
935
|
+
}
|
|
936
|
+
closeQueuePanel() {
|
|
937
|
+
if (!this.queueEl) return this;
|
|
938
|
+
this.queueOpen = false;
|
|
939
|
+
this.queueEl.classList.remove("wb-queue-open");
|
|
940
|
+
if (this.queueBtnEl) this.queueBtnEl.classList.remove("wb-active");
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
943
|
+
toggleVolumePopup() {
|
|
944
|
+
if (this.volumePopupEl?.classList.contains("wb-volume-open")) {
|
|
945
|
+
this.closeVolumePopup();
|
|
946
|
+
} else {
|
|
947
|
+
this.openVolumePopup();
|
|
948
|
+
}
|
|
949
|
+
return this;
|
|
950
|
+
}
|
|
951
|
+
openVolumePopup() {
|
|
952
|
+
if (!this.volumePopupEl) return this;
|
|
953
|
+
this.closeQueuePanel();
|
|
954
|
+
this.volumePopupEl.classList.add("wb-volume-open");
|
|
955
|
+
if (this.muteBtnEl) this.muteBtnEl.classList.add("wb-active");
|
|
956
|
+
return this;
|
|
957
|
+
}
|
|
958
|
+
closeVolumePopup() {
|
|
959
|
+
if (!this.volumePopupEl) return this;
|
|
960
|
+
this.volumePopupEl.classList.remove("wb-volume-open");
|
|
961
|
+
if (this.muteBtnEl) this.muteBtnEl.classList.remove("wb-active");
|
|
962
|
+
return this;
|
|
963
|
+
}
|
|
964
|
+
// =====================================================================
|
|
965
|
+
// Internal: Loading & Display
|
|
966
|
+
// =====================================================================
|
|
967
|
+
_loadCurrentTrack() {
|
|
968
|
+
const track = this.getCurrentTrack();
|
|
969
|
+
if (!track || !this.player) return;
|
|
970
|
+
this.show();
|
|
971
|
+
this._updateTrackDisplay(track);
|
|
972
|
+
this._updateFavoriteUI();
|
|
973
|
+
const loadOpts = { artwork: track.artwork, album: track.album };
|
|
974
|
+
if (track.waveform) loadOpts.waveform = track.waveform;
|
|
975
|
+
if (track.markers && track.markers.length) {
|
|
976
|
+
const defaultColor = this.config.markerColor;
|
|
977
|
+
loadOpts.markers = track.markers.map((m) => ({
|
|
978
|
+
...m,
|
|
979
|
+
color: m.color || defaultColor
|
|
980
|
+
}));
|
|
981
|
+
} else {
|
|
982
|
+
loadOpts.markers = [];
|
|
983
|
+
}
|
|
984
|
+
this.player.loadTrack(track.url, track.title, track.artist, loadOpts);
|
|
985
|
+
this._activeMarkers = track.markers && track.markers.length ? track.markers : null;
|
|
986
|
+
this._currentMarkerIndex = -1;
|
|
987
|
+
if (this.player) this.player.setVolume(this.isMuted ? 0 : this.volume);
|
|
988
|
+
this._renderQueue();
|
|
989
|
+
this._syncPageState();
|
|
990
|
+
this._saveState();
|
|
991
|
+
this._updateNavButtons();
|
|
992
|
+
this._emit("trackchange", { track, index: this.currentIndex });
|
|
993
|
+
if (this.config.onTrackChange) this.config.onTrackChange(track, this.currentIndex);
|
|
994
|
+
}
|
|
995
|
+
_updateTrackDisplay(track) {
|
|
996
|
+
if (this.titleEl) this._setScrollText(this.titleEl, track.title || "Untitled");
|
|
997
|
+
if (this.artistEl) this._setScrollText(this.artistEl, track.artist || "");
|
|
998
|
+
const artworkEl = this.barEl.querySelector(".wb-artwork");
|
|
999
|
+
if (artworkEl) {
|
|
1000
|
+
const artworkUrl = track.artwork || this.config.defaultArtwork;
|
|
1001
|
+
artworkEl.innerHTML = artworkUrl ? `<img src="${escapeHtml(artworkUrl)}" alt="${escapeHtml(track.title)}" />` : ICONS.music;
|
|
1002
|
+
}
|
|
1003
|
+
if (this.metaEl && this.config.showMeta) this._renderMeta(track);
|
|
1004
|
+
const trackEl = this.barEl.querySelector(".wb-track");
|
|
1005
|
+
if (trackEl) trackEl.style.cursor = track.link ? "pointer" : "default";
|
|
1006
|
+
if (this.timeCurrentEl) this.timeCurrentEl.textContent = "0:00";
|
|
1007
|
+
if (this.timeTotalEl) this.timeTotalEl.textContent = "0:00";
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Set text on an element with auto-scroll if it overflows.
|
|
1011
|
+
* @private
|
|
1012
|
+
*/
|
|
1013
|
+
_setScrollText(el, text) {
|
|
1014
|
+
el.classList.remove("wb-scrolling");
|
|
1015
|
+
el.textContent = text;
|
|
1016
|
+
requestAnimationFrame(() => {
|
|
1017
|
+
if (el.scrollWidth > el.clientWidth) {
|
|
1018
|
+
const overflow = el.scrollWidth - el.clientWidth;
|
|
1019
|
+
const duration = Math.max(4, overflow / 20);
|
|
1020
|
+
el.innerHTML = `<span class="wb-scroll-inner">${escapeHtml(text)}</span>`;
|
|
1021
|
+
el.style.setProperty("--wb-scroll-distance", `-${overflow + 48}px`);
|
|
1022
|
+
el.style.setProperty("--wb-scroll-duration", `${duration}s`);
|
|
1023
|
+
el.classList.add("wb-scrolling");
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
_renderMeta(track) {
|
|
1028
|
+
if (!this.metaEl) return;
|
|
1029
|
+
const tags = [];
|
|
1030
|
+
if (track.bpm) tags.push({ label: track.bpm + " BPM", type: "bpm" });
|
|
1031
|
+
if (track.key) tags.push({ label: track.key, type: "key" });
|
|
1032
|
+
if (track.duration) tags.push({ label: track.duration, type: "duration" });
|
|
1033
|
+
if (track.meta) {
|
|
1034
|
+
for (const [k, v] of Object.entries(track.meta)) {
|
|
1035
|
+
if (v && tags.length < this.config.maxMeta) tags.push({ label: String(v), type: k });
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
const limited = tags.slice(0, this.config.maxMeta);
|
|
1039
|
+
this.metaEl.style.display = limited.length ? "flex" : "none";
|
|
1040
|
+
this.metaEl.innerHTML = limited.map(
|
|
1041
|
+
(t) => `<span class="wb-tag wb-tag-${escapeHtml(t.type)}">${escapeHtml(t.label)}</span>`
|
|
1042
|
+
).join("");
|
|
1043
|
+
}
|
|
1044
|
+
_updatePlayButton() {
|
|
1045
|
+
if (!this.playBtnEl) return;
|
|
1046
|
+
const play = this.playBtnEl.querySelector(".wb-icon-play");
|
|
1047
|
+
const pause = this.playBtnEl.querySelector(".wb-icon-pause");
|
|
1048
|
+
if (play) play.style.display = this.isPlaying ? "none" : "block";
|
|
1049
|
+
if (pause) pause.style.display = this.isPlaying ? "block" : "none";
|
|
1050
|
+
this.playBtnEl.title = this.isPlaying ? "Pause" : "Play";
|
|
1051
|
+
}
|
|
1052
|
+
_updateNavButtons() {
|
|
1053
|
+
const prevBtn = this.barEl?.querySelector(".wb-prev");
|
|
1054
|
+
const nextBtn = this.barEl?.querySelector(".wb-next");
|
|
1055
|
+
if (this.repeat === "all") {
|
|
1056
|
+
if (prevBtn) prevBtn.classList.remove("wb-disabled");
|
|
1057
|
+
if (nextBtn) nextBtn.classList.remove("wb-disabled");
|
|
1058
|
+
} else {
|
|
1059
|
+
if (prevBtn) prevBtn.classList.toggle("wb-disabled", this.currentIndex <= 0);
|
|
1060
|
+
if (nextBtn) nextBtn.classList.toggle("wb-disabled", this.currentIndex >= this.queue.length - 1);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
// =====================================================================
|
|
1064
|
+
// Repeat
|
|
1065
|
+
// =====================================================================
|
|
1066
|
+
/**
|
|
1067
|
+
* Cycle through repeat modes: off → all → one → off
|
|
1068
|
+
* @returns {WaveformBar}
|
|
1069
|
+
*/
|
|
1070
|
+
cycleRepeat() {
|
|
1071
|
+
const modes = ["off", "all", "one"];
|
|
1072
|
+
const current = modes.indexOf(this.repeat);
|
|
1073
|
+
this.repeat = modes[(current + 1) % modes.length];
|
|
1074
|
+
this._updateRepeatButton();
|
|
1075
|
+
this._updateNavButtons();
|
|
1076
|
+
this._emit("repeatchange", { mode: this.repeat });
|
|
1077
|
+
return this;
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Set repeat mode directly
|
|
1081
|
+
* @param {'off'|'all'|'one'} mode
|
|
1082
|
+
* @returns {WaveformBar}
|
|
1083
|
+
*/
|
|
1084
|
+
setRepeat(mode) {
|
|
1085
|
+
if (["off", "all", "one"].includes(mode)) {
|
|
1086
|
+
this.repeat = mode;
|
|
1087
|
+
this._updateRepeatButton();
|
|
1088
|
+
this._updateNavButtons();
|
|
1089
|
+
this._emit("repeatchange", { mode: this.repeat });
|
|
1090
|
+
}
|
|
1091
|
+
return this;
|
|
1092
|
+
}
|
|
1093
|
+
/** @private */
|
|
1094
|
+
_updateRepeatButton() {
|
|
1095
|
+
if (!this.repeatBtnEl) return;
|
|
1096
|
+
const icons = { off: ICONS.repeatOff, all: ICONS.repeatAll, one: ICONS.repeatOne };
|
|
1097
|
+
const labels = { off: "Repeat: Off", all: "Repeat: All", one: "Repeat: One" };
|
|
1098
|
+
this.repeatBtnEl.innerHTML = icons[this.repeat];
|
|
1099
|
+
this.repeatBtnEl.title = labels[this.repeat];
|
|
1100
|
+
this.repeatBtnEl.classList.toggle("wb-repeat-active", this.repeat !== "off");
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* DJ mode: check if playback has crossed a marker boundary
|
|
1104
|
+
* and update the bar's title/artist/artwork/meta display.
|
|
1105
|
+
* Markers should be sorted by time and can include:
|
|
1106
|
+
* { time, label, title, artist, artwork, bpm, key }
|
|
1107
|
+
* @private
|
|
1108
|
+
*/
|
|
1109
|
+
_checkMarkerBoundary(currentTime) {
|
|
1110
|
+
if (!this._activeMarkers) return;
|
|
1111
|
+
let markerIndex = -1;
|
|
1112
|
+
for (let i = this._activeMarkers.length - 1; i >= 0; i--) {
|
|
1113
|
+
if (currentTime >= this._activeMarkers[i].time) {
|
|
1114
|
+
markerIndex = i;
|
|
1115
|
+
break;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
if (markerIndex === this._currentMarkerIndex) return;
|
|
1119
|
+
this._currentMarkerIndex = markerIndex;
|
|
1120
|
+
if (markerIndex < 0) return;
|
|
1121
|
+
const marker = this._activeMarkers[markerIndex];
|
|
1122
|
+
const track = this.getCurrentTrack();
|
|
1123
|
+
if (marker.title && this.titleEl) this._setScrollText(this.titleEl, marker.title);
|
|
1124
|
+
if (marker.artist && this.artistEl) this._setScrollText(this.artistEl, marker.artist);
|
|
1125
|
+
const markerEls = this.waveformContainer?.querySelectorAll(".waveform-marker");
|
|
1126
|
+
if (markerEls) {
|
|
1127
|
+
markerEls.forEach((el, i) => el.classList.toggle("wb-marker-active", i === markerIndex));
|
|
1128
|
+
}
|
|
1129
|
+
if (marker.artwork) {
|
|
1130
|
+
const artworkEl = this.barEl.querySelector(".wb-artwork");
|
|
1131
|
+
if (artworkEl) artworkEl.innerHTML = `<img src="${marker.artwork}" alt="${marker.title || ""}" />`;
|
|
1132
|
+
}
|
|
1133
|
+
if (this.metaEl && (marker.bpm || marker.key)) {
|
|
1134
|
+
const metaTrack = {
|
|
1135
|
+
...track || {},
|
|
1136
|
+
bpm: marker.bpm || "",
|
|
1137
|
+
key: marker.key || ""
|
|
1138
|
+
};
|
|
1139
|
+
this._renderMeta(metaTrack);
|
|
1140
|
+
}
|
|
1141
|
+
this._emit("markerchange", { marker, index: markerIndex, track });
|
|
1142
|
+
}
|
|
1143
|
+
_updateVolumeUI() {
|
|
1144
|
+
if (this.volumeSliderEl) {
|
|
1145
|
+
this.volumeSliderEl.value = this.isMuted ? 0 : Math.round(this.volume * 100);
|
|
1146
|
+
}
|
|
1147
|
+
if (this.muteBtnEl) {
|
|
1148
|
+
if (this.isMuted || this.volume === 0) {
|
|
1149
|
+
this.muteBtnEl.innerHTML = ICONS.volMute;
|
|
1150
|
+
this.muteBtnEl.classList.add("wb-muted");
|
|
1151
|
+
this.muteBtnEl.title = "Unmute";
|
|
1152
|
+
} else if (this.volume < 0.5) {
|
|
1153
|
+
this.muteBtnEl.innerHTML = ICONS.volLow;
|
|
1154
|
+
this.muteBtnEl.classList.remove("wb-muted");
|
|
1155
|
+
this.muteBtnEl.title = "Mute";
|
|
1156
|
+
} else {
|
|
1157
|
+
this.muteBtnEl.innerHTML = ICONS.volHigh;
|
|
1158
|
+
this.muteBtnEl.classList.remove("wb-muted");
|
|
1159
|
+
this.muteBtnEl.title = "Mute";
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* Auto-detect light/dark theme from the page.
|
|
1165
|
+
* Checks: 1) HTML/body classes, 2) background brightness, 3) system preference
|
|
1166
|
+
* @private
|
|
1167
|
+
* @returns {'dark'|'light'}
|
|
1168
|
+
*/
|
|
1169
|
+
_detectTheme() {
|
|
1170
|
+
const root = document.documentElement;
|
|
1171
|
+
const body = document.body;
|
|
1172
|
+
const darkIndicators = ["dark", "dark-mode", "theme-dark"];
|
|
1173
|
+
const lightIndicators = ["light", "light-mode", "theme-light"];
|
|
1174
|
+
for (const cls of darkIndicators) {
|
|
1175
|
+
if (root.classList.contains(cls) || body.classList.contains(cls)) return "dark";
|
|
1176
|
+
}
|
|
1177
|
+
if (root.getAttribute("data-theme") === "dark" || body.getAttribute("data-theme") === "dark") return "dark";
|
|
1178
|
+
for (const cls of lightIndicators) {
|
|
1179
|
+
if (root.classList.contains(cls) || body.classList.contains(cls)) return "light";
|
|
1180
|
+
}
|
|
1181
|
+
if (root.getAttribute("data-theme") === "light" || body.getAttribute("data-theme") === "light") return "light";
|
|
1182
|
+
try {
|
|
1183
|
+
const bg = getComputedStyle(body).backgroundColor;
|
|
1184
|
+
const rgb = bg.match(/\d+/g);
|
|
1185
|
+
if (rgb && rgb.length >= 3) {
|
|
1186
|
+
const brightness = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1e3;
|
|
1187
|
+
if (brightness > 128) return "light";
|
|
1188
|
+
if (brightness < 128) return "dark";
|
|
1189
|
+
}
|
|
1190
|
+
} catch (e) {
|
|
1191
|
+
}
|
|
1192
|
+
if (window.matchMedia?.("(prefers-color-scheme: light)").matches) return "light";
|
|
1193
|
+
return "dark";
|
|
1194
|
+
}
|
|
1195
|
+
_updateFavoriteUI() {
|
|
1196
|
+
if (!this.favBtnEl) return;
|
|
1197
|
+
const fav = this.isFavorited();
|
|
1198
|
+
this.favBtnEl.innerHTML = fav ? ICONS.heartFilled : ICONS.heart;
|
|
1199
|
+
this.favBtnEl.classList.toggle("wb-fav-active", fav);
|
|
1200
|
+
}
|
|
1201
|
+
_renderQueue() {
|
|
1202
|
+
renderQueue(this.queueBodyEl, this.queueCountEl, this.queue, this.currentIndex, {
|
|
1203
|
+
onSkipTo: (i) => this.skipTo(i),
|
|
1204
|
+
onRemove: (i) => this.removeFromQueue(i)
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
// =====================================================================
|
|
1208
|
+
// Page State Sync
|
|
1209
|
+
// =====================================================================
|
|
1210
|
+
/**
|
|
1211
|
+
* Sync all state classes and attributes back to page trigger elements.
|
|
1212
|
+
*
|
|
1213
|
+
* Classes applied:
|
|
1214
|
+
* - .wb-current — track is current (playing or paused)
|
|
1215
|
+
* - .wb-playing — track is actively playing
|
|
1216
|
+
* - .wb-favorited — track is in favorites
|
|
1217
|
+
* - .wb-in-cart — track has been added to cart
|
|
1218
|
+
* @private
|
|
1219
|
+
*/
|
|
1220
|
+
_syncPageState() {
|
|
1221
|
+
const current = this.getCurrentTrack();
|
|
1222
|
+
const currentUrl = current ? current.url : null;
|
|
1223
|
+
document.querySelectorAll("[data-wb-play]").forEach((el) => {
|
|
1224
|
+
const url = el.dataset.wbUrl || el.dataset.url;
|
|
1225
|
+
const id = el.dataset.wbId || el.dataset.id || url;
|
|
1226
|
+
const isCurrent = url && url === currentUrl;
|
|
1227
|
+
el.classList.toggle("wb-current", isCurrent);
|
|
1228
|
+
el.classList.toggle("wb-playing", isCurrent && this.isPlaying);
|
|
1229
|
+
el.classList.toggle("wb-favorited", this._favorites.has(id));
|
|
1230
|
+
el.classList.toggle("wb-in-cart", this._cartItems.has(id));
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Seed favorites and cart state from data attributes on page elements.
|
|
1235
|
+
* This is the authoritative source — server renders the initial state,
|
|
1236
|
+
* and we read it on init. Overrides localStorage.
|
|
1237
|
+
* @private
|
|
1238
|
+
*/
|
|
1239
|
+
_seedFromAttributes() {
|
|
1240
|
+
let seededFav = false;
|
|
1241
|
+
let seededCart = false;
|
|
1242
|
+
document.querySelectorAll("[data-wb-play]").forEach((el) => {
|
|
1243
|
+
const id = el.dataset.wbId || el.dataset.id || el.dataset.wbUrl || el.dataset.url;
|
|
1244
|
+
if (!id) return;
|
|
1245
|
+
if (el.dataset.wbFavorited === "true") {
|
|
1246
|
+
this._favorites.add(id);
|
|
1247
|
+
seededFav = true;
|
|
1248
|
+
}
|
|
1249
|
+
if (el.dataset.wbInCart === "true") {
|
|
1250
|
+
this._cartItems.add(id);
|
|
1251
|
+
seededCart = true;
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
if (seededFav) {
|
|
1255
|
+
saveFavorites(this.config.storageKey, this._favorites);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Sync favorite state back to trigger element data attributes
|
|
1260
|
+
* @private
|
|
1261
|
+
* @param {string} url - Track URL to match
|
|
1262
|
+
* @param {boolean} favorited - New state
|
|
1263
|
+
*/
|
|
1264
|
+
_syncFavoriteAttributes(url, favorited) {
|
|
1265
|
+
document.querySelectorAll("[data-wb-play]").forEach((el) => {
|
|
1266
|
+
const elUrl = el.dataset.wbUrl || el.dataset.url;
|
|
1267
|
+
if (elUrl === url) {
|
|
1268
|
+
el.dataset.wbFavorited = favorited ? "true" : "false";
|
|
1269
|
+
el.classList.toggle("wb-favorited", favorited);
|
|
1270
|
+
}
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Sync cart state back to trigger element data attributes
|
|
1275
|
+
* @private
|
|
1276
|
+
* @param {string} url - Track URL to match
|
|
1277
|
+
* @param {boolean} inCart - New state
|
|
1278
|
+
*/
|
|
1279
|
+
_syncCartAttributes(url, inCart) {
|
|
1280
|
+
document.querySelectorAll("[data-wb-play]").forEach((el) => {
|
|
1281
|
+
const elUrl = el.dataset.wbUrl || el.dataset.url;
|
|
1282
|
+
if (elUrl === url) {
|
|
1283
|
+
el.dataset.wbInCart = inCart ? "true" : "false";
|
|
1284
|
+
el.classList.toggle("wb-in-cart", inCart);
|
|
1285
|
+
}
|
|
1286
|
+
});
|
|
1287
|
+
}
|
|
1288
|
+
// =====================================================================
|
|
1289
|
+
// Persistence
|
|
1290
|
+
// =====================================================================
|
|
1291
|
+
_saveState() {
|
|
1292
|
+
if (!this.config.persist) return;
|
|
1293
|
+
saveQueueState(this.config.storageKey, {
|
|
1294
|
+
queue: this.queue,
|
|
1295
|
+
currentIndex: this.currentIndex,
|
|
1296
|
+
position: this._lastPosition || 0,
|
|
1297
|
+
isPlaying: this.isPlaying
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
_restoreState() {
|
|
1301
|
+
if (!this.config.persist) return;
|
|
1302
|
+
const state = restoreQueueState(this.config.storageKey);
|
|
1303
|
+
if (!state) return;
|
|
1304
|
+
this.queue = state.queue;
|
|
1305
|
+
this.currentIndex = state.currentIndex;
|
|
1306
|
+
const track = this.getCurrentTrack();
|
|
1307
|
+
if (!track) return;
|
|
1308
|
+
this.show();
|
|
1309
|
+
this._updateTrackDisplay(track);
|
|
1310
|
+
this._updateFavoriteUI();
|
|
1311
|
+
this._updateNavButtons();
|
|
1312
|
+
if (track.waveform) this.player.options.waveform = track.waveform;
|
|
1313
|
+
this.player.options.title = track.title || "";
|
|
1314
|
+
this.player.options.subtitle = track.artist || "";
|
|
1315
|
+
if (track.markers && track.markers.length) {
|
|
1316
|
+
const defaultColor = this.config.markerColor;
|
|
1317
|
+
this.player.options.markers = track.markers.map((m) => ({
|
|
1318
|
+
...m,
|
|
1319
|
+
color: m.color || defaultColor
|
|
1320
|
+
}));
|
|
1321
|
+
this._activeMarkers = track.markers;
|
|
1322
|
+
} else {
|
|
1323
|
+
this.player.options.markers = [];
|
|
1324
|
+
this._activeMarkers = null;
|
|
1325
|
+
}
|
|
1326
|
+
this._currentMarkerIndex = -1;
|
|
1327
|
+
this.player.load(track.url).then(() => {
|
|
1328
|
+
if (this.player) this.player.setVolume(this.isMuted ? 0 : this.volume);
|
|
1329
|
+
console.log("RESTORE: position =", state.position, "duration =", this.player?.audio?.duration);
|
|
1330
|
+
if (state.isPlaying && this.config.autoResume) {
|
|
1331
|
+
try {
|
|
1332
|
+
const p = this.player.play();
|
|
1333
|
+
if (p && typeof p.catch === "function") {
|
|
1334
|
+
p.catch(() => {
|
|
1335
|
+
this.isPlaying = false;
|
|
1336
|
+
this._updatePlayButton();
|
|
1337
|
+
this._syncPageState();
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
} catch (e) {
|
|
1341
|
+
this.isPlaying = false;
|
|
1342
|
+
this._updatePlayButton();
|
|
1343
|
+
this._syncPageState();
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
if (state.position > 0) {
|
|
1347
|
+
setTimeout(() => {
|
|
1348
|
+
if (this.player) {
|
|
1349
|
+
this.player.seekTo(state.position);
|
|
1350
|
+
this._lastPosition = state.position;
|
|
1351
|
+
}
|
|
1352
|
+
}, 100);
|
|
1353
|
+
}
|
|
1354
|
+
}).catch(() => {
|
|
1355
|
+
});
|
|
1356
|
+
this._renderQueue();
|
|
1357
|
+
this._syncPageState();
|
|
1358
|
+
}
|
|
1359
|
+
_restoreVolume() {
|
|
1360
|
+
const data = restoreVolume(this.config.storageKey);
|
|
1361
|
+
if (!data) return;
|
|
1362
|
+
this.volume = data.volume;
|
|
1363
|
+
this.isMuted = data.muted;
|
|
1364
|
+
this._volumeBeforeMute = data.volumeBeforeMute;
|
|
1365
|
+
if (this.player) this.player.setVolume(this.isMuted ? 0 : this.volume);
|
|
1366
|
+
this._updateVolumeUI();
|
|
1367
|
+
}
|
|
1368
|
+
_restoreFavorites() {
|
|
1369
|
+
this._favorites = restoreFavorites(this.config.storageKey);
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
// src/js/index.js
|
|
1374
|
+
var instance = new WaveformBar();
|
|
1375
|
+
if (typeof window !== "undefined") {
|
|
1376
|
+
window.WaveformBar = instance;
|
|
1377
|
+
}
|
|
1378
|
+
var index_default = instance;
|
|
1379
|
+
export {
|
|
1380
|
+
WaveformBar,
|
|
1381
|
+
index_default as default
|
|
1382
|
+
};
|
|
1383
|
+
/**
|
|
1384
|
+
* WaveformBar v1.0.0
|
|
1385
|
+
* Persistent bottom audio player bar for WaveformPlayer
|
|
1386
|
+
*
|
|
1387
|
+
* @author ArrayPress
|
|
1388
|
+
* @license MIT
|
|
1389
|
+
*/
|