@scarlett-player/playlist 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 +395 -0
- package/dist/index.d.cts +197 -0
- package/dist/index.d.ts +197 -0
- package/dist/index.js +370 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hackney Enterprises Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createPlaylistPlugin: () => createPlaylistPlugin,
|
|
24
|
+
default: () => index_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var DEFAULT_CONFIG = {
|
|
28
|
+
autoAdvance: true,
|
|
29
|
+
preloadNext: true,
|
|
30
|
+
persist: false,
|
|
31
|
+
persistKey: "scarlett-playlist",
|
|
32
|
+
shuffle: false,
|
|
33
|
+
repeat: "none"
|
|
34
|
+
};
|
|
35
|
+
function generateId() {
|
|
36
|
+
return `track-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
37
|
+
}
|
|
38
|
+
function shuffleArray(array) {
|
|
39
|
+
const result = [...array];
|
|
40
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
41
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
42
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
function createPlaylistPlugin(config) {
|
|
47
|
+
const mergedConfig = { ...DEFAULT_CONFIG, ...config };
|
|
48
|
+
let api = null;
|
|
49
|
+
let tracks = mergedConfig.tracks || [];
|
|
50
|
+
let currentIndex = -1;
|
|
51
|
+
let shuffle = mergedConfig.shuffle || false;
|
|
52
|
+
let repeat = mergedConfig.repeat || "none";
|
|
53
|
+
let shuffleOrder = [];
|
|
54
|
+
tracks = tracks.map((t) => ({ ...t, id: t.id || generateId() }));
|
|
55
|
+
const generateShuffleOrder = () => {
|
|
56
|
+
const indices = tracks.map((_, i) => i);
|
|
57
|
+
shuffleOrder = shuffleArray(indices);
|
|
58
|
+
if (currentIndex >= 0) {
|
|
59
|
+
const currentPos = shuffleOrder.indexOf(currentIndex);
|
|
60
|
+
if (currentPos > 0) {
|
|
61
|
+
shuffleOrder.splice(currentPos, 1);
|
|
62
|
+
shuffleOrder.unshift(currentIndex);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const getActualIndex = (logicalIndex) => {
|
|
67
|
+
if (!shuffle || shuffleOrder.length === 0) {
|
|
68
|
+
return logicalIndex;
|
|
69
|
+
}
|
|
70
|
+
return shuffleOrder[logicalIndex] ?? logicalIndex;
|
|
71
|
+
};
|
|
72
|
+
const getLogicalIndex = (actualIndex) => {
|
|
73
|
+
if (!shuffle || shuffleOrder.length === 0) {
|
|
74
|
+
return actualIndex;
|
|
75
|
+
}
|
|
76
|
+
return shuffleOrder.indexOf(actualIndex);
|
|
77
|
+
};
|
|
78
|
+
const hasNextTrack = () => {
|
|
79
|
+
if (tracks.length === 0) return false;
|
|
80
|
+
if (repeat === "one" || repeat === "all") return true;
|
|
81
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
82
|
+
return logicalIndex < tracks.length - 1;
|
|
83
|
+
};
|
|
84
|
+
const hasPreviousTrack = () => {
|
|
85
|
+
if (tracks.length === 0) return false;
|
|
86
|
+
if (repeat === "one" || repeat === "all") return true;
|
|
87
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
88
|
+
return logicalIndex > 0;
|
|
89
|
+
};
|
|
90
|
+
const getNextIndex = () => {
|
|
91
|
+
if (tracks.length === 0) return -1;
|
|
92
|
+
if (repeat === "one") {
|
|
93
|
+
return currentIndex;
|
|
94
|
+
}
|
|
95
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
96
|
+
let nextLogical = logicalIndex + 1;
|
|
97
|
+
if (nextLogical >= tracks.length) {
|
|
98
|
+
if (repeat === "all") {
|
|
99
|
+
if (shuffle) {
|
|
100
|
+
generateShuffleOrder();
|
|
101
|
+
}
|
|
102
|
+
nextLogical = 0;
|
|
103
|
+
} else {
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return getActualIndex(nextLogical);
|
|
108
|
+
};
|
|
109
|
+
const getPreviousIndex = () => {
|
|
110
|
+
if (tracks.length === 0) return -1;
|
|
111
|
+
if (repeat === "one") {
|
|
112
|
+
return currentIndex;
|
|
113
|
+
}
|
|
114
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
115
|
+
let prevLogical = logicalIndex - 1;
|
|
116
|
+
if (prevLogical < 0) {
|
|
117
|
+
if (repeat === "all") {
|
|
118
|
+
prevLogical = tracks.length - 1;
|
|
119
|
+
} else {
|
|
120
|
+
return -1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return getActualIndex(prevLogical);
|
|
124
|
+
};
|
|
125
|
+
const persistPlaylist = () => {
|
|
126
|
+
if (!mergedConfig.persist) return;
|
|
127
|
+
try {
|
|
128
|
+
const data = {
|
|
129
|
+
tracks,
|
|
130
|
+
currentIndex,
|
|
131
|
+
shuffle,
|
|
132
|
+
repeat,
|
|
133
|
+
shuffleOrder
|
|
134
|
+
};
|
|
135
|
+
localStorage.setItem(mergedConfig.persistKey, JSON.stringify(data));
|
|
136
|
+
} catch (e) {
|
|
137
|
+
api?.logger.warn("Failed to persist playlist", e);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const loadPersistedPlaylist = () => {
|
|
141
|
+
if (!mergedConfig.persist) return;
|
|
142
|
+
try {
|
|
143
|
+
const data = localStorage.getItem(mergedConfig.persistKey);
|
|
144
|
+
if (data) {
|
|
145
|
+
const parsed = JSON.parse(data);
|
|
146
|
+
tracks = parsed.tracks || [];
|
|
147
|
+
currentIndex = parsed.currentIndex ?? -1;
|
|
148
|
+
shuffle = parsed.shuffle ?? false;
|
|
149
|
+
repeat = parsed.repeat ?? "none";
|
|
150
|
+
shuffleOrder = parsed.shuffleOrder || [];
|
|
151
|
+
}
|
|
152
|
+
} catch (e) {
|
|
153
|
+
api?.logger.warn("Failed to load persisted playlist", e);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const emitChange = () => {
|
|
157
|
+
const track = currentIndex >= 0 ? tracks[currentIndex] : null;
|
|
158
|
+
api?.emit("playlist:change", { track, index: currentIndex });
|
|
159
|
+
persistPlaylist();
|
|
160
|
+
};
|
|
161
|
+
const setCurrentTrack = (index) => {
|
|
162
|
+
if (index < 0 || index >= tracks.length) {
|
|
163
|
+
api?.logger.warn("Invalid track index", { index });
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const track = tracks[index];
|
|
167
|
+
currentIndex = index;
|
|
168
|
+
api?.logger.info("Track changed", { index, title: track.title, src: track.src });
|
|
169
|
+
if (track.title) {
|
|
170
|
+
api?.setState("title", track.title);
|
|
171
|
+
}
|
|
172
|
+
if (track.artwork) {
|
|
173
|
+
api?.setState("poster", track.artwork);
|
|
174
|
+
}
|
|
175
|
+
api?.setState("mediaType", track.type || "audio");
|
|
176
|
+
emitChange();
|
|
177
|
+
};
|
|
178
|
+
const plugin = {
|
|
179
|
+
id: "playlist",
|
|
180
|
+
name: "Playlist",
|
|
181
|
+
version: "1.0.0",
|
|
182
|
+
type: "feature",
|
|
183
|
+
description: "Playlist management with shuffle, repeat, and gapless playback",
|
|
184
|
+
async init(pluginApi) {
|
|
185
|
+
api = pluginApi;
|
|
186
|
+
api.logger.info("Playlist plugin initialized");
|
|
187
|
+
loadPersistedPlaylist();
|
|
188
|
+
if (shuffle && tracks.length > 0) {
|
|
189
|
+
generateShuffleOrder();
|
|
190
|
+
}
|
|
191
|
+
const unsubEnded = api.on("playback:ended", () => {
|
|
192
|
+
if (!mergedConfig.autoAdvance) return;
|
|
193
|
+
const nextIdx = getNextIndex();
|
|
194
|
+
if (nextIdx >= 0) {
|
|
195
|
+
api?.logger.debug("Auto-advancing to next track", { nextIdx });
|
|
196
|
+
setCurrentTrack(nextIdx);
|
|
197
|
+
} else {
|
|
198
|
+
api?.logger.info("Playlist ended");
|
|
199
|
+
api?.emit("playlist:ended", void 0);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
api.onDestroy(() => {
|
|
203
|
+
unsubEnded();
|
|
204
|
+
persistPlaylist();
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
async destroy() {
|
|
208
|
+
api?.logger.info("Playlist plugin destroying");
|
|
209
|
+
persistPlaylist();
|
|
210
|
+
api = null;
|
|
211
|
+
},
|
|
212
|
+
add(trackOrTracks) {
|
|
213
|
+
const newTracks = Array.isArray(trackOrTracks) ? trackOrTracks : [trackOrTracks];
|
|
214
|
+
newTracks.forEach((track) => {
|
|
215
|
+
const normalizedTrack = { ...track, id: track.id || generateId() };
|
|
216
|
+
const index = tracks.length;
|
|
217
|
+
tracks.push(normalizedTrack);
|
|
218
|
+
api?.emit("playlist:add", { track: normalizedTrack, index });
|
|
219
|
+
api?.logger.debug("Track added", { title: normalizedTrack.title, index });
|
|
220
|
+
});
|
|
221
|
+
if (shuffle) {
|
|
222
|
+
const startIndex = tracks.length - newTracks.length;
|
|
223
|
+
for (let i = startIndex; i < tracks.length; i++) {
|
|
224
|
+
const insertPos = Math.floor(Math.random() * (shuffleOrder.length - getLogicalIndex(currentIndex))) + getLogicalIndex(currentIndex) + 1;
|
|
225
|
+
shuffleOrder.splice(Math.min(insertPos, shuffleOrder.length), 0, i);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
persistPlaylist();
|
|
229
|
+
},
|
|
230
|
+
insert(index, track) {
|
|
231
|
+
const normalizedTrack = { ...track, id: track.id || generateId() };
|
|
232
|
+
const clampedIndex = Math.max(0, Math.min(index, tracks.length));
|
|
233
|
+
tracks.splice(clampedIndex, 0, normalizedTrack);
|
|
234
|
+
if (currentIndex >= clampedIndex) {
|
|
235
|
+
currentIndex++;
|
|
236
|
+
}
|
|
237
|
+
if (shuffle) {
|
|
238
|
+
shuffleOrder = shuffleOrder.map((i) => i >= clampedIndex ? i + 1 : i);
|
|
239
|
+
const insertPos = Math.floor(Math.random() * shuffleOrder.length);
|
|
240
|
+
shuffleOrder.splice(insertPos, 0, clampedIndex);
|
|
241
|
+
}
|
|
242
|
+
api?.emit("playlist:add", { track: normalizedTrack, index: clampedIndex });
|
|
243
|
+
persistPlaylist();
|
|
244
|
+
},
|
|
245
|
+
remove(idOrIndex) {
|
|
246
|
+
let index;
|
|
247
|
+
if (typeof idOrIndex === "string") {
|
|
248
|
+
index = tracks.findIndex((t) => t.id === idOrIndex);
|
|
249
|
+
if (index === -1) {
|
|
250
|
+
api?.logger.warn("Track not found", { id: idOrIndex });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
index = idOrIndex;
|
|
255
|
+
}
|
|
256
|
+
if (index < 0 || index >= tracks.length) {
|
|
257
|
+
api?.logger.warn("Invalid track index", { index });
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const [removedTrack] = tracks.splice(index, 1);
|
|
261
|
+
if (index < currentIndex) {
|
|
262
|
+
currentIndex--;
|
|
263
|
+
} else if (index === currentIndex) {
|
|
264
|
+
if (currentIndex >= tracks.length) {
|
|
265
|
+
currentIndex = tracks.length - 1;
|
|
266
|
+
}
|
|
267
|
+
emitChange();
|
|
268
|
+
}
|
|
269
|
+
if (shuffle) {
|
|
270
|
+
shuffleOrder = shuffleOrder.filter((i) => i !== index).map((i) => i > index ? i - 1 : i);
|
|
271
|
+
}
|
|
272
|
+
api?.emit("playlist:remove", { track: removedTrack, index });
|
|
273
|
+
persistPlaylist();
|
|
274
|
+
},
|
|
275
|
+
clear() {
|
|
276
|
+
tracks = [];
|
|
277
|
+
currentIndex = -1;
|
|
278
|
+
shuffleOrder = [];
|
|
279
|
+
api?.emit("playlist:clear", void 0);
|
|
280
|
+
emitChange();
|
|
281
|
+
},
|
|
282
|
+
play(idOrIndex) {
|
|
283
|
+
let index;
|
|
284
|
+
if (idOrIndex === void 0) {
|
|
285
|
+
index = currentIndex >= 0 ? currentIndex : shuffle ? getActualIndex(0) : 0;
|
|
286
|
+
} else if (typeof idOrIndex === "string") {
|
|
287
|
+
index = tracks.findIndex((t) => t.id === idOrIndex);
|
|
288
|
+
if (index === -1) {
|
|
289
|
+
api?.logger.warn("Track not found", { id: idOrIndex });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
index = idOrIndex;
|
|
294
|
+
}
|
|
295
|
+
if (tracks.length === 0) {
|
|
296
|
+
api?.logger.warn("Playlist is empty");
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
setCurrentTrack(index);
|
|
300
|
+
},
|
|
301
|
+
next() {
|
|
302
|
+
const nextIdx = getNextIndex();
|
|
303
|
+
if (nextIdx >= 0) {
|
|
304
|
+
setCurrentTrack(nextIdx);
|
|
305
|
+
} else {
|
|
306
|
+
api?.logger.info("No next track");
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
previous() {
|
|
310
|
+
const currentTime = api?.getState("currentTime") || 0;
|
|
311
|
+
if (currentTime > 3) {
|
|
312
|
+
api?.emit("playback:seeking", { time: 0 });
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const prevIdx = getPreviousIndex();
|
|
316
|
+
if (prevIdx >= 0) {
|
|
317
|
+
setCurrentTrack(prevIdx);
|
|
318
|
+
} else {
|
|
319
|
+
api?.logger.info("No previous track");
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
toggleShuffle() {
|
|
323
|
+
this.setShuffle(!shuffle);
|
|
324
|
+
},
|
|
325
|
+
setShuffle(enabled) {
|
|
326
|
+
shuffle = enabled;
|
|
327
|
+
if (enabled) {
|
|
328
|
+
generateShuffleOrder();
|
|
329
|
+
} else {
|
|
330
|
+
shuffleOrder = [];
|
|
331
|
+
}
|
|
332
|
+
api?.emit("playlist:shuffle", { enabled });
|
|
333
|
+
api?.logger.info("Shuffle mode", { enabled });
|
|
334
|
+
persistPlaylist();
|
|
335
|
+
},
|
|
336
|
+
cycleRepeat() {
|
|
337
|
+
const modes = ["none", "all", "one"];
|
|
338
|
+
const currentIdx = modes.indexOf(repeat);
|
|
339
|
+
const nextIdx = (currentIdx + 1) % modes.length;
|
|
340
|
+
this.setRepeat(modes[nextIdx]);
|
|
341
|
+
},
|
|
342
|
+
setRepeat(mode) {
|
|
343
|
+
repeat = mode;
|
|
344
|
+
api?.emit("playlist:repeat", { mode });
|
|
345
|
+
api?.logger.info("Repeat mode", { mode });
|
|
346
|
+
persistPlaylist();
|
|
347
|
+
},
|
|
348
|
+
move(fromIndex, toIndex) {
|
|
349
|
+
if (fromIndex < 0 || fromIndex >= tracks.length) return;
|
|
350
|
+
if (toIndex < 0 || toIndex >= tracks.length) return;
|
|
351
|
+
if (fromIndex === toIndex) return;
|
|
352
|
+
const [track] = tracks.splice(fromIndex, 1);
|
|
353
|
+
tracks.splice(toIndex, 0, track);
|
|
354
|
+
if (currentIndex === fromIndex) {
|
|
355
|
+
currentIndex = toIndex;
|
|
356
|
+
} else if (fromIndex < currentIndex && toIndex >= currentIndex) {
|
|
357
|
+
currentIndex--;
|
|
358
|
+
} else if (fromIndex > currentIndex && toIndex <= currentIndex) {
|
|
359
|
+
currentIndex++;
|
|
360
|
+
}
|
|
361
|
+
if (shuffle) {
|
|
362
|
+
generateShuffleOrder();
|
|
363
|
+
}
|
|
364
|
+
api?.emit("playlist:reorder", { tracks: [...tracks] });
|
|
365
|
+
persistPlaylist();
|
|
366
|
+
},
|
|
367
|
+
getState() {
|
|
368
|
+
return {
|
|
369
|
+
tracks: [...tracks],
|
|
370
|
+
currentIndex,
|
|
371
|
+
currentTrack: currentIndex >= 0 ? tracks[currentIndex] : null,
|
|
372
|
+
shuffle,
|
|
373
|
+
repeat,
|
|
374
|
+
shuffleOrder: [...shuffleOrder],
|
|
375
|
+
hasNext: hasNextTrack(),
|
|
376
|
+
hasPrevious: hasPreviousTrack()
|
|
377
|
+
};
|
|
378
|
+
},
|
|
379
|
+
getTracks() {
|
|
380
|
+
return [...tracks];
|
|
381
|
+
},
|
|
382
|
+
getCurrentTrack() {
|
|
383
|
+
return currentIndex >= 0 ? tracks[currentIndex] : null;
|
|
384
|
+
},
|
|
385
|
+
getTrack(id) {
|
|
386
|
+
return tracks.find((t) => t.id === id) || null;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
return plugin;
|
|
390
|
+
}
|
|
391
|
+
var index_default = createPlaylistPlugin;
|
|
392
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
393
|
+
0 && (module.exports = {
|
|
394
|
+
createPlaylistPlugin
|
|
395
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { Plugin } from '@scarlett-player/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Playlist Plugin Types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A track in the playlist
|
|
9
|
+
*/
|
|
10
|
+
interface PlaylistTrack {
|
|
11
|
+
/** Unique track ID */
|
|
12
|
+
id: string;
|
|
13
|
+
/** Media source URL */
|
|
14
|
+
src: string;
|
|
15
|
+
/** Track title */
|
|
16
|
+
title?: string;
|
|
17
|
+
/** Artist/creator name */
|
|
18
|
+
artist?: string;
|
|
19
|
+
/** Album name */
|
|
20
|
+
album?: string;
|
|
21
|
+
/** Album art / thumbnail URL */
|
|
22
|
+
artwork?: string;
|
|
23
|
+
/** Duration in seconds (if known) */
|
|
24
|
+
duration?: number;
|
|
25
|
+
/** Media type */
|
|
26
|
+
type?: 'audio' | 'video';
|
|
27
|
+
/** MIME type hint */
|
|
28
|
+
mimeType?: string;
|
|
29
|
+
/** Additional metadata */
|
|
30
|
+
metadata?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Repeat mode
|
|
34
|
+
*/
|
|
35
|
+
type RepeatMode = 'none' | 'one' | 'all';
|
|
36
|
+
/**
|
|
37
|
+
* Playlist state
|
|
38
|
+
*/
|
|
39
|
+
interface PlaylistState {
|
|
40
|
+
/** All tracks in the playlist */
|
|
41
|
+
tracks: PlaylistTrack[];
|
|
42
|
+
/** Current track index (-1 if no track) */
|
|
43
|
+
currentIndex: number;
|
|
44
|
+
/** Currently playing track */
|
|
45
|
+
currentTrack: PlaylistTrack | null;
|
|
46
|
+
/** Shuffle mode enabled */
|
|
47
|
+
shuffle: boolean;
|
|
48
|
+
/** Repeat mode */
|
|
49
|
+
repeat: RepeatMode;
|
|
50
|
+
/** Shuffled order (indices into tracks array) */
|
|
51
|
+
shuffleOrder: number[];
|
|
52
|
+
/** Whether there's a next track available */
|
|
53
|
+
hasNext: boolean;
|
|
54
|
+
/** Whether there's a previous track available */
|
|
55
|
+
hasPrevious: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Playlist plugin configuration
|
|
59
|
+
*/
|
|
60
|
+
interface PlaylistPluginConfig {
|
|
61
|
+
/** Auto-advance to next track when current ends (default: true) */
|
|
62
|
+
autoAdvance?: boolean;
|
|
63
|
+
/** Preload next track for gapless playback (default: true) */
|
|
64
|
+
preloadNext?: boolean;
|
|
65
|
+
/** Persist playlist to localStorage (default: false) */
|
|
66
|
+
persist?: boolean;
|
|
67
|
+
/** localStorage key for persistence (default: 'scarlett-playlist') */
|
|
68
|
+
persistKey?: string;
|
|
69
|
+
/** Initial tracks */
|
|
70
|
+
tracks?: PlaylistTrack[];
|
|
71
|
+
/** Initial shuffle state */
|
|
72
|
+
shuffle?: boolean;
|
|
73
|
+
/** Initial repeat mode */
|
|
74
|
+
repeat?: RepeatMode;
|
|
75
|
+
/** Index signature for PluginConfig compatibility */
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Playlist plugin interface
|
|
80
|
+
*/
|
|
81
|
+
interface IPlaylistPlugin extends Plugin<PlaylistPluginConfig> {
|
|
82
|
+
/**
|
|
83
|
+
* Add a track to the end of the playlist
|
|
84
|
+
*/
|
|
85
|
+
add(track: PlaylistTrack | PlaylistTrack[]): void;
|
|
86
|
+
/**
|
|
87
|
+
* Insert a track at a specific position
|
|
88
|
+
*/
|
|
89
|
+
insert(index: number, track: PlaylistTrack): void;
|
|
90
|
+
/**
|
|
91
|
+
* Remove a track by ID or index
|
|
92
|
+
*/
|
|
93
|
+
remove(idOrIndex: string | number): void;
|
|
94
|
+
/**
|
|
95
|
+
* Clear all tracks
|
|
96
|
+
*/
|
|
97
|
+
clear(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Select a specific track by ID or index.
|
|
100
|
+
* Emits playlist:change event - consumer should load and play from that event.
|
|
101
|
+
*/
|
|
102
|
+
play(idOrIndex?: string | number): void;
|
|
103
|
+
/**
|
|
104
|
+
* Select the next track.
|
|
105
|
+
* Emits playlist:change event - consumer should load and play from that event.
|
|
106
|
+
*/
|
|
107
|
+
next(): void;
|
|
108
|
+
/**
|
|
109
|
+
* Select the previous track.
|
|
110
|
+
* Emits playlist:change event - consumer should load and play from that event.
|
|
111
|
+
*/
|
|
112
|
+
previous(): void;
|
|
113
|
+
/**
|
|
114
|
+
* Toggle shuffle mode
|
|
115
|
+
*/
|
|
116
|
+
toggleShuffle(): void;
|
|
117
|
+
/**
|
|
118
|
+
* Set shuffle mode
|
|
119
|
+
*/
|
|
120
|
+
setShuffle(enabled: boolean): void;
|
|
121
|
+
/**
|
|
122
|
+
* Cycle through repeat modes (none -> all -> one -> none)
|
|
123
|
+
*/
|
|
124
|
+
cycleRepeat(): void;
|
|
125
|
+
/**
|
|
126
|
+
* Set repeat mode
|
|
127
|
+
*/
|
|
128
|
+
setRepeat(mode: RepeatMode): void;
|
|
129
|
+
/**
|
|
130
|
+
* Move a track to a new position
|
|
131
|
+
*/
|
|
132
|
+
move(fromIndex: number, toIndex: number): void;
|
|
133
|
+
/**
|
|
134
|
+
* Get current playlist state
|
|
135
|
+
*/
|
|
136
|
+
getState(): PlaylistState;
|
|
137
|
+
/**
|
|
138
|
+
* Get all tracks
|
|
139
|
+
*/
|
|
140
|
+
getTracks(): PlaylistTrack[];
|
|
141
|
+
/**
|
|
142
|
+
* Get current track
|
|
143
|
+
*/
|
|
144
|
+
getCurrentTrack(): PlaylistTrack | null;
|
|
145
|
+
/**
|
|
146
|
+
* Get track by ID
|
|
147
|
+
*/
|
|
148
|
+
getTrack(id: string): PlaylistTrack | null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Playlist Plugin for Scarlett Player
|
|
153
|
+
*
|
|
154
|
+
* Provides playlist management with:
|
|
155
|
+
* - Queue management (add, remove, reorder)
|
|
156
|
+
* - Shuffle mode with Fisher-Yates algorithm
|
|
157
|
+
* - Repeat modes (none, one, all)
|
|
158
|
+
* - Auto-advance to next track
|
|
159
|
+
* - Gapless playback preparation
|
|
160
|
+
* - LocalStorage persistence
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Create a Playlist Plugin instance.
|
|
165
|
+
*
|
|
166
|
+
* @param config - Plugin configuration
|
|
167
|
+
* @returns Playlist Plugin instance
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* import { createPlaylistPlugin } from '@scarlett-player/playlist';
|
|
172
|
+
*
|
|
173
|
+
* const player = new ScarlettPlayer({
|
|
174
|
+
* container: document.getElementById('player'),
|
|
175
|
+
* plugins: [
|
|
176
|
+
* createPlaylistPlugin({
|
|
177
|
+
* autoAdvance: true,
|
|
178
|
+
* shuffle: false,
|
|
179
|
+
* repeat: 'none',
|
|
180
|
+
* }),
|
|
181
|
+
* ],
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* // Add tracks
|
|
185
|
+
* const playlist = player.getPlugin('playlist');
|
|
186
|
+
* playlist.add([
|
|
187
|
+
* { id: '1', src: 'track1.mp3', title: 'Track 1', artist: 'Artist' },
|
|
188
|
+
* { id: '2', src: 'track2.mp3', title: 'Track 2', artist: 'Artist' },
|
|
189
|
+
* ]);
|
|
190
|
+
*
|
|
191
|
+
* // Start playback
|
|
192
|
+
* playlist.play();
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare function createPlaylistPlugin(config?: Partial<PlaylistPluginConfig>): IPlaylistPlugin;
|
|
196
|
+
|
|
197
|
+
export { type IPlaylistPlugin, type PlaylistPluginConfig, type PlaylistState, type PlaylistTrack, type RepeatMode, createPlaylistPlugin, createPlaylistPlugin as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { Plugin } from '@scarlett-player/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Playlist Plugin Types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A track in the playlist
|
|
9
|
+
*/
|
|
10
|
+
interface PlaylistTrack {
|
|
11
|
+
/** Unique track ID */
|
|
12
|
+
id: string;
|
|
13
|
+
/** Media source URL */
|
|
14
|
+
src: string;
|
|
15
|
+
/** Track title */
|
|
16
|
+
title?: string;
|
|
17
|
+
/** Artist/creator name */
|
|
18
|
+
artist?: string;
|
|
19
|
+
/** Album name */
|
|
20
|
+
album?: string;
|
|
21
|
+
/** Album art / thumbnail URL */
|
|
22
|
+
artwork?: string;
|
|
23
|
+
/** Duration in seconds (if known) */
|
|
24
|
+
duration?: number;
|
|
25
|
+
/** Media type */
|
|
26
|
+
type?: 'audio' | 'video';
|
|
27
|
+
/** MIME type hint */
|
|
28
|
+
mimeType?: string;
|
|
29
|
+
/** Additional metadata */
|
|
30
|
+
metadata?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Repeat mode
|
|
34
|
+
*/
|
|
35
|
+
type RepeatMode = 'none' | 'one' | 'all';
|
|
36
|
+
/**
|
|
37
|
+
* Playlist state
|
|
38
|
+
*/
|
|
39
|
+
interface PlaylistState {
|
|
40
|
+
/** All tracks in the playlist */
|
|
41
|
+
tracks: PlaylistTrack[];
|
|
42
|
+
/** Current track index (-1 if no track) */
|
|
43
|
+
currentIndex: number;
|
|
44
|
+
/** Currently playing track */
|
|
45
|
+
currentTrack: PlaylistTrack | null;
|
|
46
|
+
/** Shuffle mode enabled */
|
|
47
|
+
shuffle: boolean;
|
|
48
|
+
/** Repeat mode */
|
|
49
|
+
repeat: RepeatMode;
|
|
50
|
+
/** Shuffled order (indices into tracks array) */
|
|
51
|
+
shuffleOrder: number[];
|
|
52
|
+
/** Whether there's a next track available */
|
|
53
|
+
hasNext: boolean;
|
|
54
|
+
/** Whether there's a previous track available */
|
|
55
|
+
hasPrevious: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Playlist plugin configuration
|
|
59
|
+
*/
|
|
60
|
+
interface PlaylistPluginConfig {
|
|
61
|
+
/** Auto-advance to next track when current ends (default: true) */
|
|
62
|
+
autoAdvance?: boolean;
|
|
63
|
+
/** Preload next track for gapless playback (default: true) */
|
|
64
|
+
preloadNext?: boolean;
|
|
65
|
+
/** Persist playlist to localStorage (default: false) */
|
|
66
|
+
persist?: boolean;
|
|
67
|
+
/** localStorage key for persistence (default: 'scarlett-playlist') */
|
|
68
|
+
persistKey?: string;
|
|
69
|
+
/** Initial tracks */
|
|
70
|
+
tracks?: PlaylistTrack[];
|
|
71
|
+
/** Initial shuffle state */
|
|
72
|
+
shuffle?: boolean;
|
|
73
|
+
/** Initial repeat mode */
|
|
74
|
+
repeat?: RepeatMode;
|
|
75
|
+
/** Index signature for PluginConfig compatibility */
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Playlist plugin interface
|
|
80
|
+
*/
|
|
81
|
+
interface IPlaylistPlugin extends Plugin<PlaylistPluginConfig> {
|
|
82
|
+
/**
|
|
83
|
+
* Add a track to the end of the playlist
|
|
84
|
+
*/
|
|
85
|
+
add(track: PlaylistTrack | PlaylistTrack[]): void;
|
|
86
|
+
/**
|
|
87
|
+
* Insert a track at a specific position
|
|
88
|
+
*/
|
|
89
|
+
insert(index: number, track: PlaylistTrack): void;
|
|
90
|
+
/**
|
|
91
|
+
* Remove a track by ID or index
|
|
92
|
+
*/
|
|
93
|
+
remove(idOrIndex: string | number): void;
|
|
94
|
+
/**
|
|
95
|
+
* Clear all tracks
|
|
96
|
+
*/
|
|
97
|
+
clear(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Select a specific track by ID or index.
|
|
100
|
+
* Emits playlist:change event - consumer should load and play from that event.
|
|
101
|
+
*/
|
|
102
|
+
play(idOrIndex?: string | number): void;
|
|
103
|
+
/**
|
|
104
|
+
* Select the next track.
|
|
105
|
+
* Emits playlist:change event - consumer should load and play from that event.
|
|
106
|
+
*/
|
|
107
|
+
next(): void;
|
|
108
|
+
/**
|
|
109
|
+
* Select the previous track.
|
|
110
|
+
* Emits playlist:change event - consumer should load and play from that event.
|
|
111
|
+
*/
|
|
112
|
+
previous(): void;
|
|
113
|
+
/**
|
|
114
|
+
* Toggle shuffle mode
|
|
115
|
+
*/
|
|
116
|
+
toggleShuffle(): void;
|
|
117
|
+
/**
|
|
118
|
+
* Set shuffle mode
|
|
119
|
+
*/
|
|
120
|
+
setShuffle(enabled: boolean): void;
|
|
121
|
+
/**
|
|
122
|
+
* Cycle through repeat modes (none -> all -> one -> none)
|
|
123
|
+
*/
|
|
124
|
+
cycleRepeat(): void;
|
|
125
|
+
/**
|
|
126
|
+
* Set repeat mode
|
|
127
|
+
*/
|
|
128
|
+
setRepeat(mode: RepeatMode): void;
|
|
129
|
+
/**
|
|
130
|
+
* Move a track to a new position
|
|
131
|
+
*/
|
|
132
|
+
move(fromIndex: number, toIndex: number): void;
|
|
133
|
+
/**
|
|
134
|
+
* Get current playlist state
|
|
135
|
+
*/
|
|
136
|
+
getState(): PlaylistState;
|
|
137
|
+
/**
|
|
138
|
+
* Get all tracks
|
|
139
|
+
*/
|
|
140
|
+
getTracks(): PlaylistTrack[];
|
|
141
|
+
/**
|
|
142
|
+
* Get current track
|
|
143
|
+
*/
|
|
144
|
+
getCurrentTrack(): PlaylistTrack | null;
|
|
145
|
+
/**
|
|
146
|
+
* Get track by ID
|
|
147
|
+
*/
|
|
148
|
+
getTrack(id: string): PlaylistTrack | null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Playlist Plugin for Scarlett Player
|
|
153
|
+
*
|
|
154
|
+
* Provides playlist management with:
|
|
155
|
+
* - Queue management (add, remove, reorder)
|
|
156
|
+
* - Shuffle mode with Fisher-Yates algorithm
|
|
157
|
+
* - Repeat modes (none, one, all)
|
|
158
|
+
* - Auto-advance to next track
|
|
159
|
+
* - Gapless playback preparation
|
|
160
|
+
* - LocalStorage persistence
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Create a Playlist Plugin instance.
|
|
165
|
+
*
|
|
166
|
+
* @param config - Plugin configuration
|
|
167
|
+
* @returns Playlist Plugin instance
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* import { createPlaylistPlugin } from '@scarlett-player/playlist';
|
|
172
|
+
*
|
|
173
|
+
* const player = new ScarlettPlayer({
|
|
174
|
+
* container: document.getElementById('player'),
|
|
175
|
+
* plugins: [
|
|
176
|
+
* createPlaylistPlugin({
|
|
177
|
+
* autoAdvance: true,
|
|
178
|
+
* shuffle: false,
|
|
179
|
+
* repeat: 'none',
|
|
180
|
+
* }),
|
|
181
|
+
* ],
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* // Add tracks
|
|
185
|
+
* const playlist = player.getPlugin('playlist');
|
|
186
|
+
* playlist.add([
|
|
187
|
+
* { id: '1', src: 'track1.mp3', title: 'Track 1', artist: 'Artist' },
|
|
188
|
+
* { id: '2', src: 'track2.mp3', title: 'Track 2', artist: 'Artist' },
|
|
189
|
+
* ]);
|
|
190
|
+
*
|
|
191
|
+
* // Start playback
|
|
192
|
+
* playlist.play();
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare function createPlaylistPlugin(config?: Partial<PlaylistPluginConfig>): IPlaylistPlugin;
|
|
196
|
+
|
|
197
|
+
export { type IPlaylistPlugin, type PlaylistPluginConfig, type PlaylistState, type PlaylistTrack, type RepeatMode, createPlaylistPlugin, createPlaylistPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var DEFAULT_CONFIG = {
|
|
3
|
+
autoAdvance: true,
|
|
4
|
+
preloadNext: true,
|
|
5
|
+
persist: false,
|
|
6
|
+
persistKey: "scarlett-playlist",
|
|
7
|
+
shuffle: false,
|
|
8
|
+
repeat: "none"
|
|
9
|
+
};
|
|
10
|
+
function generateId() {
|
|
11
|
+
return `track-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
12
|
+
}
|
|
13
|
+
function shuffleArray(array) {
|
|
14
|
+
const result = [...array];
|
|
15
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
16
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
17
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
function createPlaylistPlugin(config) {
|
|
22
|
+
const mergedConfig = { ...DEFAULT_CONFIG, ...config };
|
|
23
|
+
let api = null;
|
|
24
|
+
let tracks = mergedConfig.tracks || [];
|
|
25
|
+
let currentIndex = -1;
|
|
26
|
+
let shuffle = mergedConfig.shuffle || false;
|
|
27
|
+
let repeat = mergedConfig.repeat || "none";
|
|
28
|
+
let shuffleOrder = [];
|
|
29
|
+
tracks = tracks.map((t) => ({ ...t, id: t.id || generateId() }));
|
|
30
|
+
const generateShuffleOrder = () => {
|
|
31
|
+
const indices = tracks.map((_, i) => i);
|
|
32
|
+
shuffleOrder = shuffleArray(indices);
|
|
33
|
+
if (currentIndex >= 0) {
|
|
34
|
+
const currentPos = shuffleOrder.indexOf(currentIndex);
|
|
35
|
+
if (currentPos > 0) {
|
|
36
|
+
shuffleOrder.splice(currentPos, 1);
|
|
37
|
+
shuffleOrder.unshift(currentIndex);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const getActualIndex = (logicalIndex) => {
|
|
42
|
+
if (!shuffle || shuffleOrder.length === 0) {
|
|
43
|
+
return logicalIndex;
|
|
44
|
+
}
|
|
45
|
+
return shuffleOrder[logicalIndex] ?? logicalIndex;
|
|
46
|
+
};
|
|
47
|
+
const getLogicalIndex = (actualIndex) => {
|
|
48
|
+
if (!shuffle || shuffleOrder.length === 0) {
|
|
49
|
+
return actualIndex;
|
|
50
|
+
}
|
|
51
|
+
return shuffleOrder.indexOf(actualIndex);
|
|
52
|
+
};
|
|
53
|
+
const hasNextTrack = () => {
|
|
54
|
+
if (tracks.length === 0) return false;
|
|
55
|
+
if (repeat === "one" || repeat === "all") return true;
|
|
56
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
57
|
+
return logicalIndex < tracks.length - 1;
|
|
58
|
+
};
|
|
59
|
+
const hasPreviousTrack = () => {
|
|
60
|
+
if (tracks.length === 0) return false;
|
|
61
|
+
if (repeat === "one" || repeat === "all") return true;
|
|
62
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
63
|
+
return logicalIndex > 0;
|
|
64
|
+
};
|
|
65
|
+
const getNextIndex = () => {
|
|
66
|
+
if (tracks.length === 0) return -1;
|
|
67
|
+
if (repeat === "one") {
|
|
68
|
+
return currentIndex;
|
|
69
|
+
}
|
|
70
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
71
|
+
let nextLogical = logicalIndex + 1;
|
|
72
|
+
if (nextLogical >= tracks.length) {
|
|
73
|
+
if (repeat === "all") {
|
|
74
|
+
if (shuffle) {
|
|
75
|
+
generateShuffleOrder();
|
|
76
|
+
}
|
|
77
|
+
nextLogical = 0;
|
|
78
|
+
} else {
|
|
79
|
+
return -1;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return getActualIndex(nextLogical);
|
|
83
|
+
};
|
|
84
|
+
const getPreviousIndex = () => {
|
|
85
|
+
if (tracks.length === 0) return -1;
|
|
86
|
+
if (repeat === "one") {
|
|
87
|
+
return currentIndex;
|
|
88
|
+
}
|
|
89
|
+
const logicalIndex = getLogicalIndex(currentIndex);
|
|
90
|
+
let prevLogical = logicalIndex - 1;
|
|
91
|
+
if (prevLogical < 0) {
|
|
92
|
+
if (repeat === "all") {
|
|
93
|
+
prevLogical = tracks.length - 1;
|
|
94
|
+
} else {
|
|
95
|
+
return -1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return getActualIndex(prevLogical);
|
|
99
|
+
};
|
|
100
|
+
const persistPlaylist = () => {
|
|
101
|
+
if (!mergedConfig.persist) return;
|
|
102
|
+
try {
|
|
103
|
+
const data = {
|
|
104
|
+
tracks,
|
|
105
|
+
currentIndex,
|
|
106
|
+
shuffle,
|
|
107
|
+
repeat,
|
|
108
|
+
shuffleOrder
|
|
109
|
+
};
|
|
110
|
+
localStorage.setItem(mergedConfig.persistKey, JSON.stringify(data));
|
|
111
|
+
} catch (e) {
|
|
112
|
+
api?.logger.warn("Failed to persist playlist", e);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const loadPersistedPlaylist = () => {
|
|
116
|
+
if (!mergedConfig.persist) return;
|
|
117
|
+
try {
|
|
118
|
+
const data = localStorage.getItem(mergedConfig.persistKey);
|
|
119
|
+
if (data) {
|
|
120
|
+
const parsed = JSON.parse(data);
|
|
121
|
+
tracks = parsed.tracks || [];
|
|
122
|
+
currentIndex = parsed.currentIndex ?? -1;
|
|
123
|
+
shuffle = parsed.shuffle ?? false;
|
|
124
|
+
repeat = parsed.repeat ?? "none";
|
|
125
|
+
shuffleOrder = parsed.shuffleOrder || [];
|
|
126
|
+
}
|
|
127
|
+
} catch (e) {
|
|
128
|
+
api?.logger.warn("Failed to load persisted playlist", e);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const emitChange = () => {
|
|
132
|
+
const track = currentIndex >= 0 ? tracks[currentIndex] : null;
|
|
133
|
+
api?.emit("playlist:change", { track, index: currentIndex });
|
|
134
|
+
persistPlaylist();
|
|
135
|
+
};
|
|
136
|
+
const setCurrentTrack = (index) => {
|
|
137
|
+
if (index < 0 || index >= tracks.length) {
|
|
138
|
+
api?.logger.warn("Invalid track index", { index });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const track = tracks[index];
|
|
142
|
+
currentIndex = index;
|
|
143
|
+
api?.logger.info("Track changed", { index, title: track.title, src: track.src });
|
|
144
|
+
if (track.title) {
|
|
145
|
+
api?.setState("title", track.title);
|
|
146
|
+
}
|
|
147
|
+
if (track.artwork) {
|
|
148
|
+
api?.setState("poster", track.artwork);
|
|
149
|
+
}
|
|
150
|
+
api?.setState("mediaType", track.type || "audio");
|
|
151
|
+
emitChange();
|
|
152
|
+
};
|
|
153
|
+
const plugin = {
|
|
154
|
+
id: "playlist",
|
|
155
|
+
name: "Playlist",
|
|
156
|
+
version: "1.0.0",
|
|
157
|
+
type: "feature",
|
|
158
|
+
description: "Playlist management with shuffle, repeat, and gapless playback",
|
|
159
|
+
async init(pluginApi) {
|
|
160
|
+
api = pluginApi;
|
|
161
|
+
api.logger.info("Playlist plugin initialized");
|
|
162
|
+
loadPersistedPlaylist();
|
|
163
|
+
if (shuffle && tracks.length > 0) {
|
|
164
|
+
generateShuffleOrder();
|
|
165
|
+
}
|
|
166
|
+
const unsubEnded = api.on("playback:ended", () => {
|
|
167
|
+
if (!mergedConfig.autoAdvance) return;
|
|
168
|
+
const nextIdx = getNextIndex();
|
|
169
|
+
if (nextIdx >= 0) {
|
|
170
|
+
api?.logger.debug("Auto-advancing to next track", { nextIdx });
|
|
171
|
+
setCurrentTrack(nextIdx);
|
|
172
|
+
} else {
|
|
173
|
+
api?.logger.info("Playlist ended");
|
|
174
|
+
api?.emit("playlist:ended", void 0);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
api.onDestroy(() => {
|
|
178
|
+
unsubEnded();
|
|
179
|
+
persistPlaylist();
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
async destroy() {
|
|
183
|
+
api?.logger.info("Playlist plugin destroying");
|
|
184
|
+
persistPlaylist();
|
|
185
|
+
api = null;
|
|
186
|
+
},
|
|
187
|
+
add(trackOrTracks) {
|
|
188
|
+
const newTracks = Array.isArray(trackOrTracks) ? trackOrTracks : [trackOrTracks];
|
|
189
|
+
newTracks.forEach((track) => {
|
|
190
|
+
const normalizedTrack = { ...track, id: track.id || generateId() };
|
|
191
|
+
const index = tracks.length;
|
|
192
|
+
tracks.push(normalizedTrack);
|
|
193
|
+
api?.emit("playlist:add", { track: normalizedTrack, index });
|
|
194
|
+
api?.logger.debug("Track added", { title: normalizedTrack.title, index });
|
|
195
|
+
});
|
|
196
|
+
if (shuffle) {
|
|
197
|
+
const startIndex = tracks.length - newTracks.length;
|
|
198
|
+
for (let i = startIndex; i < tracks.length; i++) {
|
|
199
|
+
const insertPos = Math.floor(Math.random() * (shuffleOrder.length - getLogicalIndex(currentIndex))) + getLogicalIndex(currentIndex) + 1;
|
|
200
|
+
shuffleOrder.splice(Math.min(insertPos, shuffleOrder.length), 0, i);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
persistPlaylist();
|
|
204
|
+
},
|
|
205
|
+
insert(index, track) {
|
|
206
|
+
const normalizedTrack = { ...track, id: track.id || generateId() };
|
|
207
|
+
const clampedIndex = Math.max(0, Math.min(index, tracks.length));
|
|
208
|
+
tracks.splice(clampedIndex, 0, normalizedTrack);
|
|
209
|
+
if (currentIndex >= clampedIndex) {
|
|
210
|
+
currentIndex++;
|
|
211
|
+
}
|
|
212
|
+
if (shuffle) {
|
|
213
|
+
shuffleOrder = shuffleOrder.map((i) => i >= clampedIndex ? i + 1 : i);
|
|
214
|
+
const insertPos = Math.floor(Math.random() * shuffleOrder.length);
|
|
215
|
+
shuffleOrder.splice(insertPos, 0, clampedIndex);
|
|
216
|
+
}
|
|
217
|
+
api?.emit("playlist:add", { track: normalizedTrack, index: clampedIndex });
|
|
218
|
+
persistPlaylist();
|
|
219
|
+
},
|
|
220
|
+
remove(idOrIndex) {
|
|
221
|
+
let index;
|
|
222
|
+
if (typeof idOrIndex === "string") {
|
|
223
|
+
index = tracks.findIndex((t) => t.id === idOrIndex);
|
|
224
|
+
if (index === -1) {
|
|
225
|
+
api?.logger.warn("Track not found", { id: idOrIndex });
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
index = idOrIndex;
|
|
230
|
+
}
|
|
231
|
+
if (index < 0 || index >= tracks.length) {
|
|
232
|
+
api?.logger.warn("Invalid track index", { index });
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const [removedTrack] = tracks.splice(index, 1);
|
|
236
|
+
if (index < currentIndex) {
|
|
237
|
+
currentIndex--;
|
|
238
|
+
} else if (index === currentIndex) {
|
|
239
|
+
if (currentIndex >= tracks.length) {
|
|
240
|
+
currentIndex = tracks.length - 1;
|
|
241
|
+
}
|
|
242
|
+
emitChange();
|
|
243
|
+
}
|
|
244
|
+
if (shuffle) {
|
|
245
|
+
shuffleOrder = shuffleOrder.filter((i) => i !== index).map((i) => i > index ? i - 1 : i);
|
|
246
|
+
}
|
|
247
|
+
api?.emit("playlist:remove", { track: removedTrack, index });
|
|
248
|
+
persistPlaylist();
|
|
249
|
+
},
|
|
250
|
+
clear() {
|
|
251
|
+
tracks = [];
|
|
252
|
+
currentIndex = -1;
|
|
253
|
+
shuffleOrder = [];
|
|
254
|
+
api?.emit("playlist:clear", void 0);
|
|
255
|
+
emitChange();
|
|
256
|
+
},
|
|
257
|
+
play(idOrIndex) {
|
|
258
|
+
let index;
|
|
259
|
+
if (idOrIndex === void 0) {
|
|
260
|
+
index = currentIndex >= 0 ? currentIndex : shuffle ? getActualIndex(0) : 0;
|
|
261
|
+
} else if (typeof idOrIndex === "string") {
|
|
262
|
+
index = tracks.findIndex((t) => t.id === idOrIndex);
|
|
263
|
+
if (index === -1) {
|
|
264
|
+
api?.logger.warn("Track not found", { id: idOrIndex });
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
index = idOrIndex;
|
|
269
|
+
}
|
|
270
|
+
if (tracks.length === 0) {
|
|
271
|
+
api?.logger.warn("Playlist is empty");
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
setCurrentTrack(index);
|
|
275
|
+
},
|
|
276
|
+
next() {
|
|
277
|
+
const nextIdx = getNextIndex();
|
|
278
|
+
if (nextIdx >= 0) {
|
|
279
|
+
setCurrentTrack(nextIdx);
|
|
280
|
+
} else {
|
|
281
|
+
api?.logger.info("No next track");
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
previous() {
|
|
285
|
+
const currentTime = api?.getState("currentTime") || 0;
|
|
286
|
+
if (currentTime > 3) {
|
|
287
|
+
api?.emit("playback:seeking", { time: 0 });
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const prevIdx = getPreviousIndex();
|
|
291
|
+
if (prevIdx >= 0) {
|
|
292
|
+
setCurrentTrack(prevIdx);
|
|
293
|
+
} else {
|
|
294
|
+
api?.logger.info("No previous track");
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
toggleShuffle() {
|
|
298
|
+
this.setShuffle(!shuffle);
|
|
299
|
+
},
|
|
300
|
+
setShuffle(enabled) {
|
|
301
|
+
shuffle = enabled;
|
|
302
|
+
if (enabled) {
|
|
303
|
+
generateShuffleOrder();
|
|
304
|
+
} else {
|
|
305
|
+
shuffleOrder = [];
|
|
306
|
+
}
|
|
307
|
+
api?.emit("playlist:shuffle", { enabled });
|
|
308
|
+
api?.logger.info("Shuffle mode", { enabled });
|
|
309
|
+
persistPlaylist();
|
|
310
|
+
},
|
|
311
|
+
cycleRepeat() {
|
|
312
|
+
const modes = ["none", "all", "one"];
|
|
313
|
+
const currentIdx = modes.indexOf(repeat);
|
|
314
|
+
const nextIdx = (currentIdx + 1) % modes.length;
|
|
315
|
+
this.setRepeat(modes[nextIdx]);
|
|
316
|
+
},
|
|
317
|
+
setRepeat(mode) {
|
|
318
|
+
repeat = mode;
|
|
319
|
+
api?.emit("playlist:repeat", { mode });
|
|
320
|
+
api?.logger.info("Repeat mode", { mode });
|
|
321
|
+
persistPlaylist();
|
|
322
|
+
},
|
|
323
|
+
move(fromIndex, toIndex) {
|
|
324
|
+
if (fromIndex < 0 || fromIndex >= tracks.length) return;
|
|
325
|
+
if (toIndex < 0 || toIndex >= tracks.length) return;
|
|
326
|
+
if (fromIndex === toIndex) return;
|
|
327
|
+
const [track] = tracks.splice(fromIndex, 1);
|
|
328
|
+
tracks.splice(toIndex, 0, track);
|
|
329
|
+
if (currentIndex === fromIndex) {
|
|
330
|
+
currentIndex = toIndex;
|
|
331
|
+
} else if (fromIndex < currentIndex && toIndex >= currentIndex) {
|
|
332
|
+
currentIndex--;
|
|
333
|
+
} else if (fromIndex > currentIndex && toIndex <= currentIndex) {
|
|
334
|
+
currentIndex++;
|
|
335
|
+
}
|
|
336
|
+
if (shuffle) {
|
|
337
|
+
generateShuffleOrder();
|
|
338
|
+
}
|
|
339
|
+
api?.emit("playlist:reorder", { tracks: [...tracks] });
|
|
340
|
+
persistPlaylist();
|
|
341
|
+
},
|
|
342
|
+
getState() {
|
|
343
|
+
return {
|
|
344
|
+
tracks: [...tracks],
|
|
345
|
+
currentIndex,
|
|
346
|
+
currentTrack: currentIndex >= 0 ? tracks[currentIndex] : null,
|
|
347
|
+
shuffle,
|
|
348
|
+
repeat,
|
|
349
|
+
shuffleOrder: [...shuffleOrder],
|
|
350
|
+
hasNext: hasNextTrack(),
|
|
351
|
+
hasPrevious: hasPreviousTrack()
|
|
352
|
+
};
|
|
353
|
+
},
|
|
354
|
+
getTracks() {
|
|
355
|
+
return [...tracks];
|
|
356
|
+
},
|
|
357
|
+
getCurrentTrack() {
|
|
358
|
+
return currentIndex >= 0 ? tracks[currentIndex] : null;
|
|
359
|
+
},
|
|
360
|
+
getTrack(id) {
|
|
361
|
+
return tracks.find((t) => t.id === id) || null;
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
return plugin;
|
|
365
|
+
}
|
|
366
|
+
var index_default = createPlaylistPlugin;
|
|
367
|
+
export {
|
|
368
|
+
createPlaylistPlugin,
|
|
369
|
+
index_default as default
|
|
370
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scarlett-player/playlist",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Playlist Plugin for Scarlett Player - Queue management, shuffle, repeat, and gapless playback",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@scarlett-player/core": "^0.2.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"tsup": "^8.0.0",
|
|
29
|
+
"typescript": "^5.3.0",
|
|
30
|
+
"vitest": "^1.6.0",
|
|
31
|
+
"jsdom": "^24.0.0",
|
|
32
|
+
"@scarlett-player/core": "0.2.0"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"video",
|
|
36
|
+
"audio",
|
|
37
|
+
"player",
|
|
38
|
+
"playlist",
|
|
39
|
+
"queue",
|
|
40
|
+
"shuffle",
|
|
41
|
+
"repeat",
|
|
42
|
+
"gapless",
|
|
43
|
+
"scarlett"
|
|
44
|
+
],
|
|
45
|
+
"author": "The Stream Platform",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/Hackney-Enterprises-Inc/scarlett-player.git",
|
|
50
|
+
"directory": "packages/plugins/playlist"
|
|
51
|
+
},
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/Hackney-Enterprises-Inc/scarlett-player/issues"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://scarlettplayer.com",
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
58
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
59
|
+
"test": "vitest --run",
|
|
60
|
+
"test:watch": "vitest",
|
|
61
|
+
"typecheck": "tsc --noEmit"
|
|
62
|
+
}
|
|
63
|
+
}
|