@scarlett-player/audio-ui 0.2.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/dist/index.cjs +739 -0
- package/dist/index.d.cts +113 -0
- package/dist/index.d.ts +113 -0
- package/dist/index.js +714 -0
- package/package.json +61 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var DEFAULT_THEME = {
|
|
3
|
+
primary: "#6366f1",
|
|
4
|
+
background: "#18181b",
|
|
5
|
+
text: "#fafafa",
|
|
6
|
+
textSecondary: "#a1a1aa",
|
|
7
|
+
progressBackground: "#3f3f46",
|
|
8
|
+
progressFill: "#6366f1",
|
|
9
|
+
borderRadius: "12px",
|
|
10
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
|
|
11
|
+
};
|
|
12
|
+
var DEFAULT_CONFIG = {
|
|
13
|
+
layout: "full",
|
|
14
|
+
showArtwork: true,
|
|
15
|
+
showTitle: true,
|
|
16
|
+
showArtist: true,
|
|
17
|
+
showTime: true,
|
|
18
|
+
showVolume: true,
|
|
19
|
+
showShuffle: true,
|
|
20
|
+
showRepeat: true,
|
|
21
|
+
showNavigation: true,
|
|
22
|
+
classPrefix: "scarlett-audio",
|
|
23
|
+
autoHide: 0,
|
|
24
|
+
theme: DEFAULT_THEME
|
|
25
|
+
};
|
|
26
|
+
function formatTime(seconds) {
|
|
27
|
+
if (!isFinite(seconds) || seconds < 0) return "0:00";
|
|
28
|
+
const h = Math.floor(seconds / 3600);
|
|
29
|
+
const m = Math.floor(seconds % 3600 / 60);
|
|
30
|
+
const s = Math.floor(seconds % 60);
|
|
31
|
+
if (h > 0) {
|
|
32
|
+
return `${h}:${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`;
|
|
33
|
+
}
|
|
34
|
+
return `${m}:${s.toString().padStart(2, "0")}`;
|
|
35
|
+
}
|
|
36
|
+
function createStyles(prefix, theme) {
|
|
37
|
+
return `
|
|
38
|
+
.${prefix} {
|
|
39
|
+
font-family: ${theme.fontFamily};
|
|
40
|
+
background: ${theme.background};
|
|
41
|
+
color: ${theme.text};
|
|
42
|
+
border-radius: ${theme.borderRadius};
|
|
43
|
+
overflow: hidden;
|
|
44
|
+
user-select: none;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.${prefix}--full {
|
|
48
|
+
display: flex;
|
|
49
|
+
flex-direction: column;
|
|
50
|
+
padding: 20px;
|
|
51
|
+
gap: 16px;
|
|
52
|
+
max-width: 400px;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.${prefix}--compact {
|
|
56
|
+
display: flex;
|
|
57
|
+
align-items: center;
|
|
58
|
+
padding: 12px 16px;
|
|
59
|
+
gap: 12px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.${prefix}--mini {
|
|
63
|
+
display: flex;
|
|
64
|
+
align-items: center;
|
|
65
|
+
padding: 8px 12px;
|
|
66
|
+
gap: 8px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.${prefix}__artwork {
|
|
70
|
+
flex-shrink: 0;
|
|
71
|
+
background: ${theme.progressBackground};
|
|
72
|
+
border-radius: 8px;
|
|
73
|
+
overflow: hidden;
|
|
74
|
+
display: flex;
|
|
75
|
+
align-items: center;
|
|
76
|
+
justify-content: center;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.${prefix}--full .${prefix}__artwork {
|
|
80
|
+
width: 100%;
|
|
81
|
+
aspect-ratio: 1;
|
|
82
|
+
border-radius: ${theme.borderRadius};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.${prefix}--compact .${prefix}__artwork {
|
|
86
|
+
width: 56px;
|
|
87
|
+
height: 56px;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.${prefix}--mini .${prefix}__artwork {
|
|
91
|
+
width: 40px;
|
|
92
|
+
height: 40px;
|
|
93
|
+
border-radius: 6px;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.${prefix}__artwork img {
|
|
97
|
+
width: 100%;
|
|
98
|
+
height: 100%;
|
|
99
|
+
object-fit: cover;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.${prefix}__artwork-placeholder {
|
|
103
|
+
width: 50%;
|
|
104
|
+
height: 50%;
|
|
105
|
+
opacity: 0.3;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.${prefix}__info {
|
|
109
|
+
flex: 1;
|
|
110
|
+
min-width: 0;
|
|
111
|
+
display: flex;
|
|
112
|
+
flex-direction: column;
|
|
113
|
+
gap: 4px;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
.${prefix}__title {
|
|
117
|
+
font-size: 16px;
|
|
118
|
+
font-weight: 600;
|
|
119
|
+
white-space: nowrap;
|
|
120
|
+
overflow: hidden;
|
|
121
|
+
text-overflow: ellipsis;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.${prefix}--mini .${prefix}__title {
|
|
125
|
+
font-size: 14px;
|
|
126
|
+
display: inline-block;
|
|
127
|
+
animation: none;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.${prefix}__title-wrapper {
|
|
131
|
+
overflow: hidden;
|
|
132
|
+
width: 100%;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.${prefix}--mini .${prefix}__title-wrapper .${prefix}__title.scrolling {
|
|
136
|
+
animation: marquee 8s linear infinite;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@keyframes marquee {
|
|
140
|
+
0% { transform: translateX(0); }
|
|
141
|
+
100% { transform: translateX(-50%); }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.${prefix}--mini .${prefix}__progress {
|
|
145
|
+
margin-top: 4px;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.${prefix}--mini .${prefix}__progress-bar {
|
|
149
|
+
height: 4px;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.${prefix}__artist {
|
|
153
|
+
font-size: 14px;
|
|
154
|
+
color: ${theme.textSecondary};
|
|
155
|
+
white-space: nowrap;
|
|
156
|
+
overflow: hidden;
|
|
157
|
+
text-overflow: ellipsis;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.${prefix}--mini .${prefix}__artist {
|
|
161
|
+
font-size: 12px;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.${prefix}__progress {
|
|
165
|
+
display: flex;
|
|
166
|
+
align-items: center;
|
|
167
|
+
gap: 12px;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.${prefix}__progress-bar {
|
|
171
|
+
flex: 1;
|
|
172
|
+
height: 6px;
|
|
173
|
+
background: ${theme.progressBackground};
|
|
174
|
+
border-radius: 3px;
|
|
175
|
+
cursor: pointer;
|
|
176
|
+
position: relative;
|
|
177
|
+
overflow: hidden;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.${prefix}__progress-bar:hover {
|
|
181
|
+
height: 8px;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.${prefix}__progress-fill {
|
|
185
|
+
height: 100%;
|
|
186
|
+
background: ${theme.progressFill};
|
|
187
|
+
border-radius: 3px;
|
|
188
|
+
width: 100%;
|
|
189
|
+
transform-origin: left center;
|
|
190
|
+
will-change: transform;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
.${prefix}__progress-buffered {
|
|
194
|
+
position: absolute;
|
|
195
|
+
top: 0;
|
|
196
|
+
left: 0;
|
|
197
|
+
height: 100%;
|
|
198
|
+
background: ${theme.progressBackground};
|
|
199
|
+
opacity: 0.5;
|
|
200
|
+
border-radius: 3px;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.${prefix}__time {
|
|
204
|
+
font-size: 12px;
|
|
205
|
+
color: ${theme.textSecondary};
|
|
206
|
+
min-width: 40px;
|
|
207
|
+
text-align: center;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
.${prefix}__controls {
|
|
211
|
+
display: flex;
|
|
212
|
+
align-items: center;
|
|
213
|
+
justify-content: center;
|
|
214
|
+
gap: 8px;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
.${prefix}__btn {
|
|
218
|
+
background: transparent;
|
|
219
|
+
border: none;
|
|
220
|
+
color: ${theme.text};
|
|
221
|
+
cursor: pointer;
|
|
222
|
+
padding: 8px;
|
|
223
|
+
border-radius: 50%;
|
|
224
|
+
display: flex;
|
|
225
|
+
align-items: center;
|
|
226
|
+
justify-content: center;
|
|
227
|
+
transition: background 0.2s, transform 0.1s;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
.${prefix}__btn:hover {
|
|
231
|
+
background: rgba(255, 255, 255, 0.1);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
.${prefix}__btn:active {
|
|
235
|
+
transform: scale(0.95);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
.${prefix}__btn--primary {
|
|
239
|
+
background: ${theme.primary};
|
|
240
|
+
width: 48px;
|
|
241
|
+
height: 48px;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
.${prefix}__btn--primary:hover {
|
|
245
|
+
background: ${theme.primary};
|
|
246
|
+
opacity: 0.9;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.${prefix}--mini .${prefix}__btn--primary {
|
|
250
|
+
width: 36px;
|
|
251
|
+
height: 36px;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
.${prefix}__btn--active {
|
|
255
|
+
color: ${theme.primary};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
.${prefix}__btn svg {
|
|
259
|
+
width: 20px;
|
|
260
|
+
height: 20px;
|
|
261
|
+
fill: currentColor;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
.${prefix}__btn--primary svg {
|
|
265
|
+
width: 24px;
|
|
266
|
+
height: 24px;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.${prefix}__volume {
|
|
270
|
+
display: flex;
|
|
271
|
+
align-items: center;
|
|
272
|
+
gap: 8px;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
.${prefix}__volume-slider {
|
|
276
|
+
width: 80px;
|
|
277
|
+
height: 4px;
|
|
278
|
+
background: ${theme.progressBackground};
|
|
279
|
+
border-radius: 2px;
|
|
280
|
+
cursor: pointer;
|
|
281
|
+
position: relative;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
.${prefix}__volume-fill {
|
|
285
|
+
height: 100%;
|
|
286
|
+
background: ${theme.text};
|
|
287
|
+
border-radius: 2px;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
.${prefix}__secondary-controls {
|
|
291
|
+
display: flex;
|
|
292
|
+
align-items: center;
|
|
293
|
+
justify-content: space-between;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
.${prefix}--hidden {
|
|
297
|
+
display: none;
|
|
298
|
+
}
|
|
299
|
+
`;
|
|
300
|
+
}
|
|
301
|
+
var ICONS = {
|
|
302
|
+
play: `<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>`,
|
|
303
|
+
pause: `<svg viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>`,
|
|
304
|
+
previous: `<svg viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>`,
|
|
305
|
+
next: `<svg viewBox="0 0 24 24"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>`,
|
|
306
|
+
shuffle: `<svg viewBox="0 0 24 24"><path d="M10.59 9.17L5.41 4 4 5.41l5.17 5.17 1.42-1.41zM14.5 4l2.04 2.04L4 18.59 5.41 20 17.96 7.46 20 9.5V4h-5.5zm.33 9.41l-1.41 1.41 3.13 3.13L14.5 20H20v-5.5l-2.04 2.04-3.13-3.13z"/></svg>`,
|
|
307
|
+
repeatOff: `<svg viewBox="0 0 24 24"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/></svg>`,
|
|
308
|
+
repeatAll: `<svg viewBox="0 0 24 24"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z"/></svg>`,
|
|
309
|
+
repeatOne: `<svg viewBox="0 0 24 24"><path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4zm-4-2V9h-1l-2 1v1h1.5v4H13z"/></svg>`,
|
|
310
|
+
volumeHigh: `<svg viewBox="0 0 24 24"><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>`,
|
|
311
|
+
volumeMuted: `<svg viewBox="0 0 24 24"><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>`,
|
|
312
|
+
music: `<svg viewBox="0 0 24 24"><path d="M12 3v10.55c-.59-.34-1.27-.55-2-.55-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z"/></svg>`
|
|
313
|
+
};
|
|
314
|
+
function createAudioUIPlugin(config) {
|
|
315
|
+
const mergedConfig = { ...DEFAULT_CONFIG, ...config };
|
|
316
|
+
const theme = { ...DEFAULT_THEME, ...mergedConfig.theme };
|
|
317
|
+
const prefix = mergedConfig.classPrefix;
|
|
318
|
+
let api = null;
|
|
319
|
+
let container = null;
|
|
320
|
+
let styleElement = null;
|
|
321
|
+
let layout = mergedConfig.layout;
|
|
322
|
+
let isVisible = true;
|
|
323
|
+
let animationFrameId = null;
|
|
324
|
+
let lastKnownTime = 0;
|
|
325
|
+
let lastUpdateTimestamp = 0;
|
|
326
|
+
let isPlaying = false;
|
|
327
|
+
let artworkImg = null;
|
|
328
|
+
let titleEl = null;
|
|
329
|
+
let artistEl = null;
|
|
330
|
+
let progressFill = null;
|
|
331
|
+
let currentTimeEl = null;
|
|
332
|
+
let durationEl = null;
|
|
333
|
+
let playPauseBtn = null;
|
|
334
|
+
let shuffleBtn = null;
|
|
335
|
+
let repeatBtn = null;
|
|
336
|
+
let volumeBtn = null;
|
|
337
|
+
let volumeFill = null;
|
|
338
|
+
const startProgressAnimation = () => {
|
|
339
|
+
if (animationFrameId !== null) return;
|
|
340
|
+
const animate = (timestamp) => {
|
|
341
|
+
if (!api || !isPlaying) {
|
|
342
|
+
animationFrameId = null;
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const duration = api.getState("duration") || 0;
|
|
346
|
+
if (duration <= 0) {
|
|
347
|
+
animationFrameId = requestAnimationFrame(animate);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const elapsed = (timestamp - lastUpdateTimestamp) / 1e3;
|
|
351
|
+
const interpolatedTime = Math.min(lastKnownTime + elapsed, duration);
|
|
352
|
+
const scale = interpolatedTime / duration;
|
|
353
|
+
if (progressFill) {
|
|
354
|
+
progressFill.style.transform = `scaleX(${scale})`;
|
|
355
|
+
}
|
|
356
|
+
if (currentTimeEl) {
|
|
357
|
+
currentTimeEl.textContent = formatTime(interpolatedTime);
|
|
358
|
+
}
|
|
359
|
+
animationFrameId = requestAnimationFrame(animate);
|
|
360
|
+
};
|
|
361
|
+
lastUpdateTimestamp = performance.now();
|
|
362
|
+
animationFrameId = requestAnimationFrame(animate);
|
|
363
|
+
};
|
|
364
|
+
const stopProgressAnimation = () => {
|
|
365
|
+
if (animationFrameId !== null) {
|
|
366
|
+
cancelAnimationFrame(animationFrameId);
|
|
367
|
+
animationFrameId = null;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
const createUI = () => {
|
|
371
|
+
if (!api) return;
|
|
372
|
+
styleElement = document.createElement("style");
|
|
373
|
+
styleElement.textContent = createStyles(prefix, theme);
|
|
374
|
+
document.head.appendChild(styleElement);
|
|
375
|
+
container = document.createElement("div");
|
|
376
|
+
container.className = `${prefix} ${prefix}--${layout}`;
|
|
377
|
+
if (layout === "full") {
|
|
378
|
+
container.innerHTML = buildFullLayout();
|
|
379
|
+
} else if (layout === "compact") {
|
|
380
|
+
container.innerHTML = buildCompactLayout();
|
|
381
|
+
} else {
|
|
382
|
+
container.innerHTML = buildMiniLayout();
|
|
383
|
+
}
|
|
384
|
+
artworkImg = container.querySelector(`.${prefix}__artwork img`);
|
|
385
|
+
titleEl = container.querySelector(`.${prefix}__title`);
|
|
386
|
+
artistEl = container.querySelector(`.${prefix}__artist`);
|
|
387
|
+
progressFill = container.querySelector(`.${prefix}__progress-fill`);
|
|
388
|
+
currentTimeEl = container.querySelector(`.${prefix}__time--current`);
|
|
389
|
+
durationEl = container.querySelector(`.${prefix}__time--duration`);
|
|
390
|
+
playPauseBtn = container.querySelector(`.${prefix}__btn--play`);
|
|
391
|
+
shuffleBtn = container.querySelector(`.${prefix}__btn--shuffle`);
|
|
392
|
+
repeatBtn = container.querySelector(`.${prefix}__btn--repeat`);
|
|
393
|
+
volumeBtn = container.querySelector(`.${prefix}__btn--volume`);
|
|
394
|
+
volumeFill = container.querySelector(`.${prefix}__volume-fill`);
|
|
395
|
+
attachEventListeners();
|
|
396
|
+
api.container.appendChild(container);
|
|
397
|
+
};
|
|
398
|
+
const buildFullLayout = () => {
|
|
399
|
+
return `
|
|
400
|
+
${mergedConfig.showArtwork ? `
|
|
401
|
+
<div class="${prefix}__artwork">
|
|
402
|
+
<img src="${mergedConfig.defaultArtwork || ""}" alt="Album art" />
|
|
403
|
+
${!mergedConfig.defaultArtwork ? `<div class="${prefix}__artwork-placeholder">${ICONS.music}</div>` : ""}
|
|
404
|
+
</div>
|
|
405
|
+
` : ""}
|
|
406
|
+
<div class="${prefix}__info">
|
|
407
|
+
${mergedConfig.showTitle ? `<div class="${prefix}__title">-</div>` : ""}
|
|
408
|
+
${mergedConfig.showArtist ? `<div class="${prefix}__artist">-</div>` : ""}
|
|
409
|
+
</div>
|
|
410
|
+
<div class="${prefix}__progress">
|
|
411
|
+
${mergedConfig.showTime ? `<span class="${prefix}__time ${prefix}__time--current">0:00</span>` : ""}
|
|
412
|
+
<div class="${prefix}__progress-bar">
|
|
413
|
+
<div class="${prefix}__progress-fill" style="transform: scaleX(0)"></div>
|
|
414
|
+
</div>
|
|
415
|
+
${mergedConfig.showTime ? `<span class="${prefix}__time ${prefix}__time--duration">0:00</span>` : ""}
|
|
416
|
+
</div>
|
|
417
|
+
<div class="${prefix}__controls">
|
|
418
|
+
${mergedConfig.showShuffle ? `<button class="${prefix}__btn ${prefix}__btn--shuffle" title="Shuffle">${ICONS.shuffle}</button>` : ""}
|
|
419
|
+
${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--prev" title="Previous">${ICONS.previous}</button>` : ""}
|
|
420
|
+
<button class="${prefix}__btn ${prefix}__btn--primary ${prefix}__btn--play" title="Play">${ICONS.play}</button>
|
|
421
|
+
${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--next" title="Next">${ICONS.next}</button>` : ""}
|
|
422
|
+
${mergedConfig.showRepeat ? `<button class="${prefix}__btn ${prefix}__btn--repeat" title="Repeat">${ICONS.repeatOff}</button>` : ""}
|
|
423
|
+
</div>
|
|
424
|
+
${mergedConfig.showVolume ? `
|
|
425
|
+
<div class="${prefix}__secondary-controls">
|
|
426
|
+
<div class="${prefix}__volume">
|
|
427
|
+
<button class="${prefix}__btn ${prefix}__btn--volume" title="Volume">${ICONS.volumeHigh}</button>
|
|
428
|
+
<div class="${prefix}__volume-slider">
|
|
429
|
+
<div class="${prefix}__volume-fill" style="width: 100%"></div>
|
|
430
|
+
</div>
|
|
431
|
+
</div>
|
|
432
|
+
</div>
|
|
433
|
+
` : ""}
|
|
434
|
+
`;
|
|
435
|
+
};
|
|
436
|
+
const buildCompactLayout = () => {
|
|
437
|
+
return `
|
|
438
|
+
${mergedConfig.showArtwork ? `
|
|
439
|
+
<div class="${prefix}__artwork">
|
|
440
|
+
<img src="${mergedConfig.defaultArtwork || ""}" alt="Album art" />
|
|
441
|
+
</div>
|
|
442
|
+
` : ""}
|
|
443
|
+
<div class="${prefix}__info">
|
|
444
|
+
${mergedConfig.showTitle ? `<div class="${prefix}__title">-</div>` : ""}
|
|
445
|
+
${mergedConfig.showArtist ? `<div class="${prefix}__artist">-</div>` : ""}
|
|
446
|
+
<div class="${prefix}__progress">
|
|
447
|
+
<div class="${prefix}__progress-bar">
|
|
448
|
+
<div class="${prefix}__progress-fill" style="transform: scaleX(0)"></div>
|
|
449
|
+
</div>
|
|
450
|
+
</div>
|
|
451
|
+
</div>
|
|
452
|
+
<div class="${prefix}__controls">
|
|
453
|
+
${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--prev" title="Previous">${ICONS.previous}</button>` : ""}
|
|
454
|
+
<button class="${prefix}__btn ${prefix}__btn--primary ${prefix}__btn--play" title="Play">${ICONS.play}</button>
|
|
455
|
+
${mergedConfig.showNavigation ? `<button class="${prefix}__btn ${prefix}__btn--next" title="Next">${ICONS.next}</button>` : ""}
|
|
456
|
+
</div>
|
|
457
|
+
`;
|
|
458
|
+
};
|
|
459
|
+
const buildMiniLayout = () => {
|
|
460
|
+
return `
|
|
461
|
+
<button class="${prefix}__btn ${prefix}__btn--primary ${prefix}__btn--play" title="Play">${ICONS.play}</button>
|
|
462
|
+
${mergedConfig.showArtwork ? `
|
|
463
|
+
<div class="${prefix}__artwork">
|
|
464
|
+
<img src="${mergedConfig.defaultArtwork || ""}" alt="Album art" />
|
|
465
|
+
</div>
|
|
466
|
+
` : ""}
|
|
467
|
+
<div class="${prefix}__info">
|
|
468
|
+
${mergedConfig.showTitle ? `<div class="${prefix}__title-wrapper"><div class="${prefix}__title">-</div></div>` : ""}
|
|
469
|
+
<div class="${prefix}__progress">
|
|
470
|
+
<div class="${prefix}__progress-bar">
|
|
471
|
+
<div class="${prefix}__progress-fill" style="transform: scaleX(0)"></div>
|
|
472
|
+
</div>
|
|
473
|
+
</div>
|
|
474
|
+
</div>
|
|
475
|
+
`;
|
|
476
|
+
};
|
|
477
|
+
const attachEventListeners = () => {
|
|
478
|
+
if (!container || !api) return;
|
|
479
|
+
playPauseBtn?.addEventListener("click", () => {
|
|
480
|
+
const playing = api?.getState("playing");
|
|
481
|
+
if (playing) {
|
|
482
|
+
api?.emit("playback:pause", void 0);
|
|
483
|
+
} else {
|
|
484
|
+
api?.emit("playback:play", void 0);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
container.querySelector(`.${prefix}__btn--prev`)?.addEventListener("click", () => {
|
|
488
|
+
const playlist = api?.getPlugin("playlist");
|
|
489
|
+
if (playlist) {
|
|
490
|
+
playlist.previous();
|
|
491
|
+
} else {
|
|
492
|
+
api?.emit("playback:seeking", { time: 0 });
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
container.querySelector(`.${prefix}__btn--next`)?.addEventListener("click", () => {
|
|
496
|
+
const playlist = api?.getPlugin("playlist");
|
|
497
|
+
playlist?.next();
|
|
498
|
+
});
|
|
499
|
+
shuffleBtn?.addEventListener("click", () => {
|
|
500
|
+
const playlist = api?.getPlugin("playlist");
|
|
501
|
+
playlist?.toggleShuffle();
|
|
502
|
+
});
|
|
503
|
+
repeatBtn?.addEventListener("click", () => {
|
|
504
|
+
const playlist = api?.getPlugin("playlist");
|
|
505
|
+
playlist?.cycleRepeat();
|
|
506
|
+
});
|
|
507
|
+
volumeBtn?.addEventListener("click", () => {
|
|
508
|
+
const muted = api?.getState("muted");
|
|
509
|
+
api?.emit("volume:mute", { muted: !muted });
|
|
510
|
+
});
|
|
511
|
+
const progressBar = container.querySelector(`.${prefix}__progress-bar`);
|
|
512
|
+
progressBar?.addEventListener("click", (e) => {
|
|
513
|
+
const mouseEvent = e;
|
|
514
|
+
const rect = mouseEvent.currentTarget.getBoundingClientRect();
|
|
515
|
+
const percent = (mouseEvent.clientX - rect.left) / rect.width;
|
|
516
|
+
const duration = api?.getState("duration") || 0;
|
|
517
|
+
const time = percent * duration;
|
|
518
|
+
api?.emit("playback:seeking", { time });
|
|
519
|
+
});
|
|
520
|
+
const volumeSlider = container.querySelector(`.${prefix}__volume-slider`);
|
|
521
|
+
volumeSlider?.addEventListener("click", (e) => {
|
|
522
|
+
const mouseEvent = e;
|
|
523
|
+
const rect = mouseEvent.currentTarget.getBoundingClientRect();
|
|
524
|
+
const percent = Math.max(0, Math.min(1, (mouseEvent.clientX - rect.left) / rect.width));
|
|
525
|
+
api?.emit("volume:change", { volume: percent, muted: false });
|
|
526
|
+
});
|
|
527
|
+
};
|
|
528
|
+
const updateUI = () => {
|
|
529
|
+
if (!api || !container) return;
|
|
530
|
+
const playing = api.getState("playing");
|
|
531
|
+
const wasPlaying = isPlaying;
|
|
532
|
+
isPlaying = playing;
|
|
533
|
+
if (playPauseBtn) {
|
|
534
|
+
playPauseBtn.innerHTML = playing ? ICONS.pause : ICONS.play;
|
|
535
|
+
playPauseBtn.title = playing ? "Pause" : "Play";
|
|
536
|
+
}
|
|
537
|
+
const currentTime = api.getState("currentTime") || 0;
|
|
538
|
+
const duration = api.getState("duration") || 0;
|
|
539
|
+
lastKnownTime = currentTime;
|
|
540
|
+
lastUpdateTimestamp = performance.now();
|
|
541
|
+
if (playing && !wasPlaying) {
|
|
542
|
+
startProgressAnimation();
|
|
543
|
+
} else if (!playing && wasPlaying) {
|
|
544
|
+
stopProgressAnimation();
|
|
545
|
+
}
|
|
546
|
+
if (!playing) {
|
|
547
|
+
const scale = duration > 0 ? currentTime / duration : 0;
|
|
548
|
+
if (progressFill) {
|
|
549
|
+
progressFill.style.transform = `scaleX(${scale})`;
|
|
550
|
+
}
|
|
551
|
+
if (currentTimeEl) {
|
|
552
|
+
currentTimeEl.textContent = formatTime(currentTime);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (durationEl) {
|
|
556
|
+
durationEl.textContent = formatTime(duration);
|
|
557
|
+
}
|
|
558
|
+
const title = api.getState("title");
|
|
559
|
+
const poster = api.getState("poster");
|
|
560
|
+
if (titleEl && title) {
|
|
561
|
+
titleEl.textContent = title;
|
|
562
|
+
if (layout === "mini") {
|
|
563
|
+
const wrapper = titleEl.parentElement;
|
|
564
|
+
if (wrapper && titleEl.scrollWidth > wrapper.clientWidth) {
|
|
565
|
+
titleEl.textContent = `${title} \u2022 ${title} \u2022 `;
|
|
566
|
+
titleEl.classList.add("scrolling");
|
|
567
|
+
} else {
|
|
568
|
+
titleEl.classList.remove("scrolling");
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (artworkImg && poster) {
|
|
573
|
+
artworkImg.src = poster;
|
|
574
|
+
}
|
|
575
|
+
const volume = api.getState("volume") || 1;
|
|
576
|
+
const muted = api.getState("muted");
|
|
577
|
+
if (volumeFill) {
|
|
578
|
+
volumeFill.style.width = `${(muted ? 0 : volume) * 100}%`;
|
|
579
|
+
}
|
|
580
|
+
if (volumeBtn) {
|
|
581
|
+
volumeBtn.innerHTML = muted || volume === 0 ? ICONS.volumeMuted : ICONS.volumeHigh;
|
|
582
|
+
}
|
|
583
|
+
const playlist = api.getPlugin("playlist");
|
|
584
|
+
if (playlist) {
|
|
585
|
+
const state = playlist.getState();
|
|
586
|
+
if (shuffleBtn) {
|
|
587
|
+
shuffleBtn.classList.toggle(`${prefix}__btn--active`, state.shuffle);
|
|
588
|
+
}
|
|
589
|
+
if (repeatBtn) {
|
|
590
|
+
repeatBtn.classList.toggle(`${prefix}__btn--active`, state.repeat !== "none");
|
|
591
|
+
if (state.repeat === "one") {
|
|
592
|
+
repeatBtn.innerHTML = ICONS.repeatOne;
|
|
593
|
+
} else if (state.repeat === "all") {
|
|
594
|
+
repeatBtn.innerHTML = ICONS.repeatAll;
|
|
595
|
+
} else {
|
|
596
|
+
repeatBtn.innerHTML = ICONS.repeatOff;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
const plugin = {
|
|
602
|
+
id: "audio-ui",
|
|
603
|
+
name: "Audio UI",
|
|
604
|
+
version: "1.0.0",
|
|
605
|
+
type: "ui",
|
|
606
|
+
description: "Compact audio player interface",
|
|
607
|
+
async init(pluginApi) {
|
|
608
|
+
api = pluginApi;
|
|
609
|
+
api.logger.info("Audio UI plugin initialized");
|
|
610
|
+
createUI();
|
|
611
|
+
const unsubState = api.subscribeToState(() => {
|
|
612
|
+
updateUI();
|
|
613
|
+
});
|
|
614
|
+
const unsubTime = api.on("playback:timeupdate", () => {
|
|
615
|
+
updateUI();
|
|
616
|
+
});
|
|
617
|
+
const unsubPlaylist = api.on("playlist:change", (payload) => {
|
|
618
|
+
if (payload?.track) {
|
|
619
|
+
if (titleEl) titleEl.textContent = payload.track.title || "-";
|
|
620
|
+
if (artistEl) artistEl.textContent = payload.track.artist || "-";
|
|
621
|
+
if (artworkImg && payload.track.artwork) {
|
|
622
|
+
artworkImg.src = payload.track.artwork;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
const unsubShuffle = api.on("playlist:shuffle", () => {
|
|
627
|
+
updateUI();
|
|
628
|
+
});
|
|
629
|
+
const unsubRepeat = api.on("playlist:repeat", () => {
|
|
630
|
+
updateUI();
|
|
631
|
+
});
|
|
632
|
+
api.onDestroy(() => {
|
|
633
|
+
unsubState();
|
|
634
|
+
unsubTime();
|
|
635
|
+
unsubPlaylist();
|
|
636
|
+
unsubShuffle();
|
|
637
|
+
unsubRepeat();
|
|
638
|
+
});
|
|
639
|
+
updateUI();
|
|
640
|
+
},
|
|
641
|
+
async destroy() {
|
|
642
|
+
api?.logger.info("Audio UI plugin destroying");
|
|
643
|
+
stopProgressAnimation();
|
|
644
|
+
if (container?.parentNode) {
|
|
645
|
+
container.parentNode.removeChild(container);
|
|
646
|
+
}
|
|
647
|
+
if (styleElement?.parentNode) {
|
|
648
|
+
styleElement.parentNode.removeChild(styleElement);
|
|
649
|
+
}
|
|
650
|
+
container = null;
|
|
651
|
+
styleElement = null;
|
|
652
|
+
api = null;
|
|
653
|
+
},
|
|
654
|
+
getElement() {
|
|
655
|
+
return container;
|
|
656
|
+
},
|
|
657
|
+
setLayout(newLayout) {
|
|
658
|
+
if (!container) return;
|
|
659
|
+
stopProgressAnimation();
|
|
660
|
+
layout = newLayout;
|
|
661
|
+
container.className = `${prefix} ${prefix}--${layout}`;
|
|
662
|
+
if (layout === "full") {
|
|
663
|
+
container.innerHTML = buildFullLayout();
|
|
664
|
+
} else if (layout === "compact") {
|
|
665
|
+
container.innerHTML = buildCompactLayout();
|
|
666
|
+
} else {
|
|
667
|
+
container.innerHTML = buildMiniLayout();
|
|
668
|
+
}
|
|
669
|
+
artworkImg = container.querySelector(`.${prefix}__artwork img`);
|
|
670
|
+
titleEl = container.querySelector(`.${prefix}__title`);
|
|
671
|
+
artistEl = container.querySelector(`.${prefix}__artist`);
|
|
672
|
+
progressFill = container.querySelector(`.${prefix}__progress-fill`);
|
|
673
|
+
currentTimeEl = container.querySelector(`.${prefix}__time--current`);
|
|
674
|
+
durationEl = container.querySelector(`.${prefix}__time--duration`);
|
|
675
|
+
playPauseBtn = container.querySelector(`.${prefix}__btn--play`);
|
|
676
|
+
shuffleBtn = container.querySelector(`.${prefix}__btn--shuffle`);
|
|
677
|
+
repeatBtn = container.querySelector(`.${prefix}__btn--repeat`);
|
|
678
|
+
volumeBtn = container.querySelector(`.${prefix}__btn--volume`);
|
|
679
|
+
volumeFill = container.querySelector(`.${prefix}__volume-fill`);
|
|
680
|
+
attachEventListeners();
|
|
681
|
+
updateUI();
|
|
682
|
+
if (isPlaying) {
|
|
683
|
+
startProgressAnimation();
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
setTheme(newTheme) {
|
|
687
|
+
Object.assign(theme, newTheme);
|
|
688
|
+
if (styleElement) {
|
|
689
|
+
styleElement.textContent = createStyles(prefix, theme);
|
|
690
|
+
}
|
|
691
|
+
},
|
|
692
|
+
show() {
|
|
693
|
+
isVisible = true;
|
|
694
|
+
container?.classList.remove(`${prefix}--hidden`);
|
|
695
|
+
},
|
|
696
|
+
hide() {
|
|
697
|
+
isVisible = false;
|
|
698
|
+
container?.classList.add(`${prefix}--hidden`);
|
|
699
|
+
},
|
|
700
|
+
toggle() {
|
|
701
|
+
if (isVisible) {
|
|
702
|
+
this.hide();
|
|
703
|
+
} else {
|
|
704
|
+
this.show();
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
return plugin;
|
|
709
|
+
}
|
|
710
|
+
var index_default = createAudioUIPlugin;
|
|
711
|
+
export {
|
|
712
|
+
createAudioUIPlugin,
|
|
713
|
+
index_default as default
|
|
714
|
+
};
|