@shortkitsdk/web 0.3.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/dist/shortkit.cjs +36697 -0
- package/dist/shortkit.js +36701 -0
- package/dist/shortkit.min.js +1 -0
- package/dist/shortkit.mjs +36697 -0
- package/dist/shortkit.slim.cjs +3751 -0
- package/dist/shortkit.slim.js +3755 -0
- package/dist/shortkit.slim.min.js +1 -0
- package/dist/shortkit.slim.mjs +3751 -0
- package/package.json +42 -0
|
@@ -0,0 +1,3755 @@
|
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.ShortKit = {}));
|
|
3
|
+
})(this, (function(exports2) {
|
|
4
|
+
"use strict";
|
|
5
|
+
class IdentityManager {
|
|
6
|
+
constructor({ userId, onResolve } = {}) {
|
|
7
|
+
this._onResolve = onResolve || null;
|
|
8
|
+
const savedAnon = localStorage.getItem("sk_anonymous_id");
|
|
9
|
+
if (savedAnon) {
|
|
10
|
+
this._anonymousId = savedAnon;
|
|
11
|
+
} else {
|
|
12
|
+
this._anonymousId = generateUUID();
|
|
13
|
+
localStorage.setItem("sk_anonymous_id", this._anonymousId);
|
|
14
|
+
}
|
|
15
|
+
this._userId = localStorage.getItem("sk_user_id") || null;
|
|
16
|
+
if (userId) {
|
|
17
|
+
this.setUserId(userId);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
get anonymousId() {
|
|
21
|
+
return this._anonymousId;
|
|
22
|
+
}
|
|
23
|
+
get userId() {
|
|
24
|
+
return this._userId;
|
|
25
|
+
}
|
|
26
|
+
get effectiveId() {
|
|
27
|
+
return this._userId ?? this._anonymousId;
|
|
28
|
+
}
|
|
29
|
+
setUserId(id) {
|
|
30
|
+
const previousAnonId = this._anonymousId;
|
|
31
|
+
this._userId = id;
|
|
32
|
+
localStorage.setItem("sk_user_id", id);
|
|
33
|
+
if (this._onResolve) {
|
|
34
|
+
this._onResolve(id, previousAnonId);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
clearUserId() {
|
|
38
|
+
this._userId = null;
|
|
39
|
+
localStorage.removeItem("sk_user_id");
|
|
40
|
+
this._anonymousId = generateUUID();
|
|
41
|
+
localStorage.setItem("sk_anonymous_id", this._anonymousId);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function generateUUID() {
|
|
45
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
46
|
+
return crypto.randomUUID();
|
|
47
|
+
}
|
|
48
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
49
|
+
const r = Math.random() * 16 | 0;
|
|
50
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const VALID_CLICK_ACTIONS = ["feed", "mute", "article", "none"];
|
|
54
|
+
const VALID_FEED_SOURCES = ["algorithmic", "custom"];
|
|
55
|
+
function createFeedConfig(overrides = {}) {
|
|
56
|
+
const config = {
|
|
57
|
+
feedHeight: "fullscreen",
|
|
58
|
+
muteOnStart: true,
|
|
59
|
+
autoplay: true,
|
|
60
|
+
feedSource: "algorithmic",
|
|
61
|
+
filter: null,
|
|
62
|
+
overlay: null,
|
|
63
|
+
preload: null,
|
|
64
|
+
...overrides
|
|
65
|
+
};
|
|
66
|
+
if (config.feedSource && !VALID_FEED_SOURCES.includes(config.feedSource)) {
|
|
67
|
+
throw new Error(`Invalid feedSource: ${config.feedSource}`);
|
|
68
|
+
}
|
|
69
|
+
if (config.feedHeight !== "fullscreen" && !(config.feedHeight?.type === "percentage" && typeof config.feedHeight?.value === "number")) {
|
|
70
|
+
throw new Error(`Invalid feedHeight: ${JSON.stringify(config.feedHeight)}`);
|
|
71
|
+
}
|
|
72
|
+
return config;
|
|
73
|
+
}
|
|
74
|
+
function createPlayerConfig(overrides = {}) {
|
|
75
|
+
const config = {
|
|
76
|
+
cornerRadius: 12,
|
|
77
|
+
clickAction: "feed",
|
|
78
|
+
autoplay: true,
|
|
79
|
+
loop: true,
|
|
80
|
+
muteOnStart: true,
|
|
81
|
+
overlay: null,
|
|
82
|
+
...overrides
|
|
83
|
+
};
|
|
84
|
+
if (!VALID_CLICK_ACTIONS.includes(config.clickAction)) {
|
|
85
|
+
throw new Error(`Invalid clickAction: ${config.clickAction}`);
|
|
86
|
+
}
|
|
87
|
+
return config;
|
|
88
|
+
}
|
|
89
|
+
function createWidgetConfig(overrides = {}) {
|
|
90
|
+
const config = {
|
|
91
|
+
cardCount: 3,
|
|
92
|
+
cardSpacing: 8,
|
|
93
|
+
cornerRadius: 12,
|
|
94
|
+
autoplay: true,
|
|
95
|
+
muteOnStart: true,
|
|
96
|
+
loop: true,
|
|
97
|
+
rotationInterval: 1e4,
|
|
98
|
+
clickAction: "feed",
|
|
99
|
+
overlay: null,
|
|
100
|
+
...overrides
|
|
101
|
+
};
|
|
102
|
+
if (!VALID_CLICK_ACTIONS.includes(config.clickAction)) {
|
|
103
|
+
throw new Error(`Invalid clickAction: ${config.clickAction}`);
|
|
104
|
+
}
|
|
105
|
+
return config;
|
|
106
|
+
}
|
|
107
|
+
function createFeedFilter(opts = {}) {
|
|
108
|
+
return {
|
|
109
|
+
tags: opts.tags || null,
|
|
110
|
+
section: opts.section || null,
|
|
111
|
+
author: opts.author || null,
|
|
112
|
+
contentType: opts.contentType || null,
|
|
113
|
+
metadata: opts.metadata || null,
|
|
114
|
+
toQueryParams() {
|
|
115
|
+
const params = [];
|
|
116
|
+
if (this.tags?.length) params.push(["tags", this.tags.join(",")]);
|
|
117
|
+
if (this.section) params.push(["section", this.section]);
|
|
118
|
+
if (this.author) params.push(["author", this.author]);
|
|
119
|
+
if (this.contentType) params.push(["content_type", this.contentType]);
|
|
120
|
+
if (this.metadata) {
|
|
121
|
+
for (const [key, value] of Object.entries(this.metadata).sort(([a], [b]) => a.localeCompare(b))) {
|
|
122
|
+
params.push([`metadata.${key}`, value]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return params;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
class APIClient {
|
|
130
|
+
constructor({ baseUrl, apiKey, getEffectiveId }) {
|
|
131
|
+
this._baseUrl = baseUrl;
|
|
132
|
+
this._apiKey = apiKey;
|
|
133
|
+
this._getEffectiveId = getEffectiveId;
|
|
134
|
+
}
|
|
135
|
+
_headers() {
|
|
136
|
+
return {
|
|
137
|
+
"X-API-Key": this._apiKey,
|
|
138
|
+
"X-User-Id": this._getEffectiveId(),
|
|
139
|
+
"Content-Type": "application/json"
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async fetchFeed({ limit = 10, cursor, filter } = {}) {
|
|
143
|
+
let url;
|
|
144
|
+
const params = new URLSearchParams({ limit: String(limit) });
|
|
145
|
+
if (cursor) params.set("cursor", cursor);
|
|
146
|
+
if (filter) {
|
|
147
|
+
const f = filter.toQueryParams ? filter : createFeedFilter(filter);
|
|
148
|
+
for (const [key, value] of f.toQueryParams()) {
|
|
149
|
+
params.set(key, value);
|
|
150
|
+
}
|
|
151
|
+
url = `${this._baseUrl}/v1/feed/filter?${params}`;
|
|
152
|
+
} else {
|
|
153
|
+
url = `${this._baseUrl}/v1/feed?${params}`;
|
|
154
|
+
}
|
|
155
|
+
const res = await fetch(url, { headers: this._headers() });
|
|
156
|
+
if (!res.ok) throw new Error(`Feed API ${res.status}: ${res.statusText}`);
|
|
157
|
+
const json = await res.json();
|
|
158
|
+
const nextCursor = json.meta?.next_cursor || null;
|
|
159
|
+
const items = (json.data || []).filter((item) => !item.type || item.type === "content").map((item) => parseContentItem(item));
|
|
160
|
+
return { items, nextCursor, hasMore: !!nextCursor };
|
|
161
|
+
}
|
|
162
|
+
async resolveIdentity(userId, anonymousId) {
|
|
163
|
+
const res = await fetch(`${this._baseUrl}/v1/identity/resolve`, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers: this._headers(),
|
|
166
|
+
body: JSON.stringify({ userId, anonymousId })
|
|
167
|
+
});
|
|
168
|
+
if (!res.ok) {
|
|
169
|
+
console.warn(`Identity resolve failed: ${res.status}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async postEvents(events) {
|
|
173
|
+
const res = await fetch(`${this._baseUrl}/v1/events`, {
|
|
174
|
+
method: "POST",
|
|
175
|
+
headers: this._headers(),
|
|
176
|
+
body: JSON.stringify({ events })
|
|
177
|
+
});
|
|
178
|
+
if (!res.ok) throw new Error(`Events API ${res.status}`);
|
|
179
|
+
}
|
|
180
|
+
beaconEvents(events) {
|
|
181
|
+
const blob = new Blob(
|
|
182
|
+
[JSON.stringify({
|
|
183
|
+
events,
|
|
184
|
+
apiKey: this._apiKey,
|
|
185
|
+
userId: this._getEffectiveId()
|
|
186
|
+
})],
|
|
187
|
+
{ type: "application/json" }
|
|
188
|
+
);
|
|
189
|
+
navigator.sendBeacon(`${this._baseUrl}/v1/events`, blob);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function parseContentItem(raw) {
|
|
193
|
+
return {
|
|
194
|
+
id: raw.id,
|
|
195
|
+
playbackId: raw.playbackId || null,
|
|
196
|
+
title: raw.title || "",
|
|
197
|
+
description: raw.description || null,
|
|
198
|
+
duration: raw.duration || 0,
|
|
199
|
+
streamingUrl: raw.streamingUrl || "",
|
|
200
|
+
thumbnailUrl: raw.thumbnailUrl || "",
|
|
201
|
+
captionTracks: (raw.captionTracks || []).map((t) => ({
|
|
202
|
+
language: t.language,
|
|
203
|
+
label: t.label || t.language,
|
|
204
|
+
source: t.source || "external",
|
|
205
|
+
url: t.url || null
|
|
206
|
+
})),
|
|
207
|
+
customMetadata: raw.customMetadata || null,
|
|
208
|
+
author: raw.author || null,
|
|
209
|
+
articleUrl: raw.articleUrl || raw.publisherUrl || null,
|
|
210
|
+
commentCount: raw.commentCount ?? null,
|
|
211
|
+
fallbackUrl: raw.fallbackUrl || null
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
class ViewSession {
|
|
215
|
+
constructor(contentId) {
|
|
216
|
+
this.contentId = contentId;
|
|
217
|
+
this.startTime = Date.now();
|
|
218
|
+
this.watchDuration = 0;
|
|
219
|
+
this.loopCount = 0;
|
|
220
|
+
this.completed = false;
|
|
221
|
+
}
|
|
222
|
+
addWatchTime(seconds) {
|
|
223
|
+
this.watchDuration += seconds;
|
|
224
|
+
}
|
|
225
|
+
recordLoop() {
|
|
226
|
+
this.loopCount++;
|
|
227
|
+
}
|
|
228
|
+
markCompleted() {
|
|
229
|
+
this.completed = true;
|
|
230
|
+
}
|
|
231
|
+
toSummary() {
|
|
232
|
+
return {
|
|
233
|
+
contentId: this.contentId,
|
|
234
|
+
startTime: this.startTime,
|
|
235
|
+
watchDuration: this.watchDuration,
|
|
236
|
+
loopCount: this.loopCount,
|
|
237
|
+
completed: this.completed
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
class EngagementTracker {
|
|
242
|
+
constructor({ onEvent, sessionId }) {
|
|
243
|
+
this._onEvent = onEvent;
|
|
244
|
+
this._sessionId = sessionId || crypto.randomUUID?.() || Math.random().toString(36).slice(2);
|
|
245
|
+
this.activeSession = null;
|
|
246
|
+
}
|
|
247
|
+
_emit(type, contentId, data = {}) {
|
|
248
|
+
this._onEvent({
|
|
249
|
+
type,
|
|
250
|
+
contentId: contentId || null,
|
|
251
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
252
|
+
sessionId: this._sessionId,
|
|
253
|
+
data
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
trackFeedEntry() {
|
|
257
|
+
this._emit("feedEntry");
|
|
258
|
+
}
|
|
259
|
+
trackFeedExit() {
|
|
260
|
+
this._emit("feedExit");
|
|
261
|
+
}
|
|
262
|
+
trackImpression(contentId) {
|
|
263
|
+
this._emit("impression", contentId);
|
|
264
|
+
}
|
|
265
|
+
trackPlayStart(contentId) {
|
|
266
|
+
this._emit("playStart", contentId);
|
|
267
|
+
}
|
|
268
|
+
trackFirstFrame(contentId) {
|
|
269
|
+
this._emit("firstFrame", contentId);
|
|
270
|
+
}
|
|
271
|
+
trackWatchProgress(contentId, seconds) {
|
|
272
|
+
this._emit("watchProgress", contentId, { seconds });
|
|
273
|
+
}
|
|
274
|
+
trackCompletion(contentId) {
|
|
275
|
+
this._emit("completion", contentId);
|
|
276
|
+
}
|
|
277
|
+
trackSwipe(fromId, toId, direction) {
|
|
278
|
+
this._emit("swipe", fromId, { toId, direction });
|
|
279
|
+
}
|
|
280
|
+
trackRebuffer(contentId) {
|
|
281
|
+
this._emit("rebuffer", contentId);
|
|
282
|
+
}
|
|
283
|
+
trackQualityChange(contentId, data) {
|
|
284
|
+
this._emit("qualityChange", contentId, data);
|
|
285
|
+
}
|
|
286
|
+
trackError(contentId, message) {
|
|
287
|
+
this._emit("error", contentId, { message });
|
|
288
|
+
}
|
|
289
|
+
trackInteraction(contentId, action) {
|
|
290
|
+
this._emit("interaction", contentId, { action });
|
|
291
|
+
}
|
|
292
|
+
trackContentSignal(contentId, signal) {
|
|
293
|
+
this._emit("contentSignal", contentId, { signal });
|
|
294
|
+
}
|
|
295
|
+
trackPlaybackFallback(contentId) {
|
|
296
|
+
this._emit("playbackFallback", contentId);
|
|
297
|
+
}
|
|
298
|
+
trackViewEnd(contentId, sessionData) {
|
|
299
|
+
this._emit("viewEnd", contentId, sessionData);
|
|
300
|
+
}
|
|
301
|
+
activateContent(contentId) {
|
|
302
|
+
if (this.activeSession) this.deactivateContent();
|
|
303
|
+
this.activeSession = new ViewSession(contentId);
|
|
304
|
+
this.trackImpression(contentId);
|
|
305
|
+
}
|
|
306
|
+
deactivateContent() {
|
|
307
|
+
if (!this.activeSession) return;
|
|
308
|
+
this.trackViewEnd(this.activeSession.contentId, this.activeSession.toSummary());
|
|
309
|
+
this.activeSession = null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
class EventBatcher {
|
|
313
|
+
constructor({ postEvents, beaconEvents, flushIntervalMs = 3e4 }) {
|
|
314
|
+
this._postEvents = postEvents;
|
|
315
|
+
this._beaconEvents = beaconEvents;
|
|
316
|
+
this._flushIntervalMs = flushIntervalMs;
|
|
317
|
+
this._queue = [];
|
|
318
|
+
this._timerId = null;
|
|
319
|
+
this._started = false;
|
|
320
|
+
this._visibilityHandler = null;
|
|
321
|
+
}
|
|
322
|
+
get pending() {
|
|
323
|
+
return this._queue.length;
|
|
324
|
+
}
|
|
325
|
+
add(event) {
|
|
326
|
+
this._queue.push(event);
|
|
327
|
+
if (this._started && this._timerId === null) {
|
|
328
|
+
this._scheduleFlush();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
start() {
|
|
332
|
+
this._started = true;
|
|
333
|
+
this._scheduleFlush();
|
|
334
|
+
this._visibilityHandler = () => {
|
|
335
|
+
if (document.hidden) this.flushBeacon();
|
|
336
|
+
};
|
|
337
|
+
document.addEventListener("visibilitychange", this._visibilityHandler);
|
|
338
|
+
}
|
|
339
|
+
_scheduleFlush() {
|
|
340
|
+
this._timerId = setTimeout(async () => {
|
|
341
|
+
this._timerId = null;
|
|
342
|
+
await this.flush();
|
|
343
|
+
}, this._flushIntervalMs);
|
|
344
|
+
}
|
|
345
|
+
async flush() {
|
|
346
|
+
if (this._queue.length === 0) return;
|
|
347
|
+
const batch = this._queue.splice(0);
|
|
348
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
349
|
+
try {
|
|
350
|
+
await this._postEvents(batch);
|
|
351
|
+
return;
|
|
352
|
+
} catch {
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
console.warn(`ShortKit: dropped ${batch.length} analytics events after 3 retries`);
|
|
356
|
+
}
|
|
357
|
+
flushBeacon() {
|
|
358
|
+
if (this._queue.length === 0) return;
|
|
359
|
+
const batch = this._queue.splice(0);
|
|
360
|
+
this._beaconEvents(batch);
|
|
361
|
+
}
|
|
362
|
+
destroy() {
|
|
363
|
+
this._started = false;
|
|
364
|
+
if (this._timerId) {
|
|
365
|
+
clearTimeout(this._timerId);
|
|
366
|
+
this._timerId = null;
|
|
367
|
+
}
|
|
368
|
+
if (this._visibilityHandler) {
|
|
369
|
+
document.removeEventListener("visibilitychange", this._visibilityHandler);
|
|
370
|
+
this._visibilityHandler = null;
|
|
371
|
+
}
|
|
372
|
+
this.flushBeacon();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
class EventEmitter {
|
|
376
|
+
constructor() {
|
|
377
|
+
this._listeners = /* @__PURE__ */ new Map();
|
|
378
|
+
}
|
|
379
|
+
on(event, fn) {
|
|
380
|
+
if (!this._listeners.has(event)) {
|
|
381
|
+
this._listeners.set(event, []);
|
|
382
|
+
}
|
|
383
|
+
this._listeners.get(event).push(fn);
|
|
384
|
+
}
|
|
385
|
+
off(event, fn) {
|
|
386
|
+
const fns = this._listeners.get(event);
|
|
387
|
+
if (!fns) return;
|
|
388
|
+
const idx = fns.indexOf(fn);
|
|
389
|
+
if (idx !== -1) fns.splice(idx, 1);
|
|
390
|
+
}
|
|
391
|
+
emit(event, data) {
|
|
392
|
+
const fns = this._listeners.get(event);
|
|
393
|
+
if (!fns) return;
|
|
394
|
+
const snapshot = [...fns];
|
|
395
|
+
for (const fn of snapshot) {
|
|
396
|
+
fn(data);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
removeAllListeners() {
|
|
400
|
+
this._listeners.clear();
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const STATE_EVENTS = {
|
|
404
|
+
playerState: "stateChange",
|
|
405
|
+
currentItem: "itemChange",
|
|
406
|
+
isMuted: "mutedChange",
|
|
407
|
+
playbackRate: "playbackRateChange",
|
|
408
|
+
captionsEnabled: "captionsChange",
|
|
409
|
+
activeCaptionTrack: "captionTrackChange",
|
|
410
|
+
activeCue: "cueChange",
|
|
411
|
+
feedScrollPhase: "feedScrollPhase",
|
|
412
|
+
prefetchedAheadCount: "prefetchedAheadCountChange",
|
|
413
|
+
remainingContentCount: "remainingContentCountChange"
|
|
414
|
+
};
|
|
415
|
+
const COMMANDS = [
|
|
416
|
+
"play",
|
|
417
|
+
"pause",
|
|
418
|
+
"seek",
|
|
419
|
+
"seekAndPlay",
|
|
420
|
+
"setMuted",
|
|
421
|
+
"skipToNext",
|
|
422
|
+
"skipToPrevious",
|
|
423
|
+
"setPlaybackRate",
|
|
424
|
+
"setCaptionsEnabled",
|
|
425
|
+
"selectCaptionTrack",
|
|
426
|
+
"sendContentSignal",
|
|
427
|
+
"setMaxBitrate",
|
|
428
|
+
"seekThumbnail"
|
|
429
|
+
];
|
|
430
|
+
class ShortKitPlayer {
|
|
431
|
+
constructor() {
|
|
432
|
+
this._emitter = new EventEmitter();
|
|
433
|
+
this._surface = null;
|
|
434
|
+
this._state = {
|
|
435
|
+
playerState: "idle",
|
|
436
|
+
currentItem: null,
|
|
437
|
+
time: { current: 0, duration: 0, buffered: 0 },
|
|
438
|
+
isMuted: true,
|
|
439
|
+
playbackRate: 1,
|
|
440
|
+
captionsEnabled: false,
|
|
441
|
+
activeCaptionTrack: null,
|
|
442
|
+
activeCue: null,
|
|
443
|
+
feedScrollPhase: null,
|
|
444
|
+
prefetchedAheadCount: 0,
|
|
445
|
+
remainingContentCount: 0
|
|
446
|
+
};
|
|
447
|
+
for (const cmd of COMMANDS) {
|
|
448
|
+
this[cmd] = (...args) => {
|
|
449
|
+
if (this._surface && typeof this._surface[cmd] === "function") {
|
|
450
|
+
return this._surface[cmd](...args);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
get playerState() {
|
|
456
|
+
return this._state.playerState;
|
|
457
|
+
}
|
|
458
|
+
get currentItem() {
|
|
459
|
+
return this._state.currentItem;
|
|
460
|
+
}
|
|
461
|
+
get time() {
|
|
462
|
+
return this._state.time;
|
|
463
|
+
}
|
|
464
|
+
get isMuted() {
|
|
465
|
+
return this._state.isMuted;
|
|
466
|
+
}
|
|
467
|
+
get playbackRate() {
|
|
468
|
+
return this._state.playbackRate;
|
|
469
|
+
}
|
|
470
|
+
get captionsEnabled() {
|
|
471
|
+
return this._state.captionsEnabled;
|
|
472
|
+
}
|
|
473
|
+
get activeCaptionTrack() {
|
|
474
|
+
return this._state.activeCaptionTrack;
|
|
475
|
+
}
|
|
476
|
+
get activeCue() {
|
|
477
|
+
return this._state.activeCue;
|
|
478
|
+
}
|
|
479
|
+
get feedScrollPhase() {
|
|
480
|
+
return this._state.feedScrollPhase;
|
|
481
|
+
}
|
|
482
|
+
get prefetchedAheadCount() {
|
|
483
|
+
return this._state.prefetchedAheadCount;
|
|
484
|
+
}
|
|
485
|
+
get remainingContentCount() {
|
|
486
|
+
return this._state.remainingContentCount;
|
|
487
|
+
}
|
|
488
|
+
on(event, fn) {
|
|
489
|
+
this._emitter.on(event, fn);
|
|
490
|
+
}
|
|
491
|
+
off(event, fn) {
|
|
492
|
+
this._emitter.off(event, fn);
|
|
493
|
+
}
|
|
494
|
+
registerSurface(surface) {
|
|
495
|
+
this._surface = surface;
|
|
496
|
+
}
|
|
497
|
+
unregisterSurface(surface) {
|
|
498
|
+
if (this._surface === surface) this._surface = null;
|
|
499
|
+
}
|
|
500
|
+
_pushState(updates) {
|
|
501
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
502
|
+
if (key === "time") {
|
|
503
|
+
this._state.time = value;
|
|
504
|
+
this._emitter.emit("timeUpdate", value);
|
|
505
|
+
} else if (this._state[key] !== value) {
|
|
506
|
+
this._state[key] = value;
|
|
507
|
+
const eventName = STATE_EVENTS[key];
|
|
508
|
+
if (eventName) this._emitter.emit(eventName, value);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
emitEvent(event, data) {
|
|
513
|
+
this._emitter.emit(event, data);
|
|
514
|
+
}
|
|
515
|
+
destroy() {
|
|
516
|
+
this._surface = null;
|
|
517
|
+
this._emitter.removeAllListeners();
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
class PlayerPool {
|
|
521
|
+
// HLS.js config for preload/speculative players: low buffer, lowest quality start
|
|
522
|
+
static PRELOAD_HLS_CONFIG = { startLevel: 0, capLevelToPlayerSize: true, maxBufferLength: 2, maxMaxBufferLength: 4 };
|
|
523
|
+
// HLS.js config for the active (visible) player: ABR picks quality, generous buffer
|
|
524
|
+
static ACTIVE_HLS_CONFIG = { startLevel: -1, capLevelToPlayerSize: true, maxBufferLength: 8, maxMaxBufferLength: 15 };
|
|
525
|
+
/**
|
|
526
|
+
* @param {object} [options]
|
|
527
|
+
* @param {number} [options.poolSize=3] Number of video elements to pre-create.
|
|
528
|
+
* @param {Function} [options.Hls] HLS.js constructor (defaults to global Hls).
|
|
529
|
+
*/
|
|
530
|
+
constructor({ poolSize = 3, Hls: HlsClass } = {}) {
|
|
531
|
+
this._Hls = HlsClass ?? (typeof Hls !== "undefined" ? Hls : null);
|
|
532
|
+
this.players = Array.from({ length: poolSize }, () => this._createPlayer());
|
|
533
|
+
this.assignments = /* @__PURE__ */ new Map();
|
|
534
|
+
this.hlsInstances = /* @__PURE__ */ new Map();
|
|
535
|
+
}
|
|
536
|
+
_createPlayer() {
|
|
537
|
+
const video = document.createElement("video");
|
|
538
|
+
video.playsInline = true;
|
|
539
|
+
video.preload = "auto";
|
|
540
|
+
video.loop = true;
|
|
541
|
+
video.muted = true;
|
|
542
|
+
video.setAttribute("webkit-playsinline", "");
|
|
543
|
+
return video;
|
|
544
|
+
}
|
|
545
|
+
/** Get or recycle a player for an item. */
|
|
546
|
+
acquire(itemId) {
|
|
547
|
+
if (this.assignments.has(itemId)) return this.assignments.get(itemId);
|
|
548
|
+
const assigned = new Set(this.assignments.values());
|
|
549
|
+
let player = this.players.find((p) => !assigned.has(p));
|
|
550
|
+
if (!player) {
|
|
551
|
+
const oldest = this.assignments.keys().next().value;
|
|
552
|
+
player = this.assignments.get(oldest);
|
|
553
|
+
player.pause();
|
|
554
|
+
this._destroyHls(oldest);
|
|
555
|
+
player.removeAttribute("src");
|
|
556
|
+
player.load();
|
|
557
|
+
if (player.parentNode) player.parentNode.removeChild(player);
|
|
558
|
+
this.assignments.delete(oldest);
|
|
559
|
+
}
|
|
560
|
+
this.assignments.set(itemId, player);
|
|
561
|
+
return player;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Attach an HLS stream (or plain URL) to the player for this item.
|
|
565
|
+
* Pass { isActive: true } for the currently-playing item to use generous
|
|
566
|
+
* buffer/ABR settings; preload players default to conservative config.
|
|
567
|
+
*
|
|
568
|
+
* @param {string} itemId
|
|
569
|
+
* @param {string} streamingUrl
|
|
570
|
+
* @param {{ isActive?: boolean }} [options]
|
|
571
|
+
*/
|
|
572
|
+
attachStream(itemId, streamingUrl, { isActive = false } = {}) {
|
|
573
|
+
const player = this.assignments.get(itemId);
|
|
574
|
+
if (!player) return;
|
|
575
|
+
if (player._skCurrentUrl === streamingUrl) return;
|
|
576
|
+
this._destroyHls(itemId);
|
|
577
|
+
const HlsClass = this._Hls;
|
|
578
|
+
if (streamingUrl.includes(".m3u8") && HlsClass && HlsClass.isSupported()) {
|
|
579
|
+
const hlsConfig = isActive ? PlayerPool.ACTIVE_HLS_CONFIG : PlayerPool.PRELOAD_HLS_CONFIG;
|
|
580
|
+
const hls = new HlsClass(hlsConfig);
|
|
581
|
+
hls.loadSource(streamingUrl);
|
|
582
|
+
hls.attachMedia(player);
|
|
583
|
+
hls.on(HlsClass.Events.ERROR, (_event, data) => {
|
|
584
|
+
if (!data.fatal) return;
|
|
585
|
+
switch (data.type) {
|
|
586
|
+
case HlsClass.ErrorTypes.MEDIA_ERROR:
|
|
587
|
+
hls.recoverMediaError();
|
|
588
|
+
break;
|
|
589
|
+
case HlsClass.ErrorTypes.NETWORK_ERROR:
|
|
590
|
+
setTimeout(() => {
|
|
591
|
+
if (!hls.destroyed) hls.startLoad();
|
|
592
|
+
}, 2e3);
|
|
593
|
+
break;
|
|
594
|
+
default:
|
|
595
|
+
hls.destroy();
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
this.hlsInstances.set(itemId, hls);
|
|
600
|
+
} else {
|
|
601
|
+
player.src = streamingUrl;
|
|
602
|
+
}
|
|
603
|
+
player._skCurrentUrl = streamingUrl;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Promote a preload player to active: raise buffer limits and uncap ABR.
|
|
607
|
+
* Safe to call even if the HLS instance was already created with active config.
|
|
608
|
+
*/
|
|
609
|
+
promoteToActive(itemId) {
|
|
610
|
+
const hls = this.hlsInstances.get(itemId);
|
|
611
|
+
if (hls) {
|
|
612
|
+
hls.config.maxBufferLength = PlayerPool.ACTIVE_HLS_CONFIG.maxBufferLength;
|
|
613
|
+
hls.config.maxMaxBufferLength = PlayerPool.ACTIVE_HLS_CONFIG.maxMaxBufferLength;
|
|
614
|
+
hls.autoLevelCapping = -1;
|
|
615
|
+
hls.nextAutoLevel = -1;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/** Pause, destroy HLS, and remove player from DOM and pool tracking. */
|
|
619
|
+
release(itemId) {
|
|
620
|
+
const player = this.assignments.get(itemId);
|
|
621
|
+
if (player) {
|
|
622
|
+
player.pause();
|
|
623
|
+
this._destroyHls(itemId);
|
|
624
|
+
player._skCurrentUrl = null;
|
|
625
|
+
if (player.parentNode) player.parentNode.removeChild(player);
|
|
626
|
+
this.assignments.delete(itemId);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
/** Look up the video element for an item, or null if not assigned. */
|
|
630
|
+
getPlayer(itemId) {
|
|
631
|
+
return this.assignments.get(itemId) || null;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Remove a video+HLS from pool tracking WITHOUT destroying or pausing.
|
|
635
|
+
* Use this when transferring a player to another surface (e.g. widget → feed).
|
|
636
|
+
* Returns { video, hls } or null if not found.
|
|
637
|
+
* A fresh replacement video element is inserted into the pool slot.
|
|
638
|
+
*/
|
|
639
|
+
ejectPlayer(itemId) {
|
|
640
|
+
const video = this.assignments.get(itemId);
|
|
641
|
+
const hls = this.hlsInstances.get(itemId);
|
|
642
|
+
if (!video) return null;
|
|
643
|
+
if (video.parentNode) video.parentNode.removeChild(video);
|
|
644
|
+
this.assignments.delete(itemId);
|
|
645
|
+
this.hlsInstances.delete(itemId);
|
|
646
|
+
const idx = this.players.indexOf(video);
|
|
647
|
+
if (idx >= 0) this.players[idx] = this._createPlayer();
|
|
648
|
+
return { video, hls };
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Re-inject a previously ejected (or externally created) video+HLS into
|
|
652
|
+
* this pool. If itemId is already assigned, it is released first.
|
|
653
|
+
*
|
|
654
|
+
* @param {string} itemId
|
|
655
|
+
* @param {HTMLVideoElement} video
|
|
656
|
+
* @param {object|null} hls HLS.js instance, or null for native playback.
|
|
657
|
+
* @param {string|null} url The streaming URL currently loaded into the player.
|
|
658
|
+
*/
|
|
659
|
+
injectPlayer(itemId, video, hls, url) {
|
|
660
|
+
if (this.assignments.has(itemId)) this.release(itemId);
|
|
661
|
+
this.assignments.set(itemId, video);
|
|
662
|
+
if (hls) this.hlsInstances.set(itemId, hls);
|
|
663
|
+
video._skCurrentUrl = url || null;
|
|
664
|
+
if (!this.players.includes(video)) this.players.push(video);
|
|
665
|
+
}
|
|
666
|
+
/** Destroy all HLS instances and release all players. Call from sk.destroy(). */
|
|
667
|
+
destroyAll() {
|
|
668
|
+
for (const itemId of [...this.assignments.keys()]) {
|
|
669
|
+
this.release(itemId);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
_destroyHls(itemId) {
|
|
673
|
+
const hls = this.hlsInstances.get(itemId);
|
|
674
|
+
if (hls) {
|
|
675
|
+
hls.destroy();
|
|
676
|
+
this.hlsInstances.delete(itemId);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
class StoryboardCache {
|
|
681
|
+
constructor() {
|
|
682
|
+
this._cache = /* @__PURE__ */ new Map();
|
|
683
|
+
this._fetching = /* @__PURE__ */ new Map();
|
|
684
|
+
}
|
|
685
|
+
static extractPlaybackId(thumbnailUrl) {
|
|
686
|
+
try {
|
|
687
|
+
const url = new URL(thumbnailUrl);
|
|
688
|
+
if (url.host !== "image.media.shortkit.dev" && url.host !== "image.mux.com") return null;
|
|
689
|
+
return url.pathname.split("/")[1] || null;
|
|
690
|
+
} catch {
|
|
691
|
+
return null;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
async get(playbackId) {
|
|
695
|
+
if (this._cache.has(playbackId)) return this._cache.get(playbackId);
|
|
696
|
+
if (this._fetching.has(playbackId)) return this._fetching.get(playbackId);
|
|
697
|
+
const promise = (async () => {
|
|
698
|
+
try {
|
|
699
|
+
const res = await fetch(`https://image.media.shortkit.dev/${playbackId}/storyboard.json`);
|
|
700
|
+
if (!res.ok) return null;
|
|
701
|
+
const meta = await res.json();
|
|
702
|
+
const img = new Image();
|
|
703
|
+
img.src = meta.url;
|
|
704
|
+
let sheetWidth = meta.tile_width;
|
|
705
|
+
let sheetHeight = meta.tile_height;
|
|
706
|
+
for (const t of meta.tiles) {
|
|
707
|
+
sheetWidth = Math.max(sheetWidth, t.x + meta.tile_width);
|
|
708
|
+
sheetHeight = Math.max(sheetHeight, t.y + meta.tile_height);
|
|
709
|
+
}
|
|
710
|
+
const entry = {
|
|
711
|
+
spriteSheetUrl: meta.url,
|
|
712
|
+
tileWidth: meta.tile_width,
|
|
713
|
+
tileHeight: meta.tile_height,
|
|
714
|
+
sheetWidth,
|
|
715
|
+
sheetHeight,
|
|
716
|
+
duration: meta.duration,
|
|
717
|
+
tiles: meta.tiles
|
|
718
|
+
};
|
|
719
|
+
this._cache.set(playbackId, entry);
|
|
720
|
+
return entry;
|
|
721
|
+
} catch {
|
|
722
|
+
return null;
|
|
723
|
+
} finally {
|
|
724
|
+
this._fetching.delete(playbackId);
|
|
725
|
+
}
|
|
726
|
+
})();
|
|
727
|
+
this._fetching.set(playbackId, promise);
|
|
728
|
+
return promise;
|
|
729
|
+
}
|
|
730
|
+
/** Find the tile for a given seek time. Returns CSS background props or null. */
|
|
731
|
+
tileAt(playbackId, seekTime) {
|
|
732
|
+
const entry = this._cache.get(playbackId);
|
|
733
|
+
if (!entry || !entry.tiles.length) return null;
|
|
734
|
+
let best = entry.tiles[0];
|
|
735
|
+
for (const tile of entry.tiles) {
|
|
736
|
+
if (tile.start <= seekTime) best = tile;
|
|
737
|
+
else break;
|
|
738
|
+
}
|
|
739
|
+
const scale = 80 / entry.tileWidth;
|
|
740
|
+
return {
|
|
741
|
+
backgroundImage: `url(${entry.spriteSheetUrl})`,
|
|
742
|
+
backgroundPosition: `-${best.x * scale}px -${best.y * scale}px`,
|
|
743
|
+
backgroundSize: `${entry.sheetWidth * scale}px ${entry.sheetHeight * scale}px`
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
function parseVTT(text) {
|
|
748
|
+
const cues = [];
|
|
749
|
+
const blocks = text.split(/\n\n+/);
|
|
750
|
+
for (const block of blocks) {
|
|
751
|
+
const lines = block.split("\n").filter((l) => l.trim());
|
|
752
|
+
const timingIdx = lines.findIndex((l) => l.includes(" --> "));
|
|
753
|
+
if (timingIdx < 0) continue;
|
|
754
|
+
const [startStr, endStr] = lines[timingIdx].split(" --> ");
|
|
755
|
+
const start = parseVTTTime(startStr.trim());
|
|
756
|
+
const end = parseVTTTime(endStr.trim());
|
|
757
|
+
if (start == null || end == null) continue;
|
|
758
|
+
const cueText = lines.slice(timingIdx + 1).join("\n").trim();
|
|
759
|
+
if (!cueText) continue;
|
|
760
|
+
cues.push({ startTime: start, endTime: end, text: cueText });
|
|
761
|
+
}
|
|
762
|
+
return cues;
|
|
763
|
+
}
|
|
764
|
+
function parseVTTTime(str) {
|
|
765
|
+
const parts = str.split(":");
|
|
766
|
+
if (parts.length === 3) {
|
|
767
|
+
return +parts[0] * 3600 + +parts[1] * 60 + parseFloat(parts[2]);
|
|
768
|
+
} else if (parts.length === 2) {
|
|
769
|
+
return +parts[0] * 60 + parseFloat(parts[1]);
|
|
770
|
+
}
|
|
771
|
+
return null;
|
|
772
|
+
}
|
|
773
|
+
function createFeedItem(item) {
|
|
774
|
+
const el = document.createElement("div");
|
|
775
|
+
el.className = "sk-feed-item";
|
|
776
|
+
el.dataset.itemId = item.id;
|
|
777
|
+
const thumbStyle = item.thumbnailUrl ? `background-image: url('${item.thumbnailUrl}'); background-size: cover; background-position: center;` : "";
|
|
778
|
+
el.innerHTML = `
|
|
779
|
+
<div class="sk-video-container" data-ref="videoContainer" style="${thumbStyle}">
|
|
780
|
+
<div class="sk-spinner" data-ref="spinner"></div>
|
|
781
|
+
</div>
|
|
782
|
+
<div class="sk-tap-zone" data-ref="tapZone"></div>
|
|
783
|
+
<div class="sk-overlay" data-ref="overlay"></div>
|
|
784
|
+
`;
|
|
785
|
+
return el;
|
|
786
|
+
}
|
|
787
|
+
class FeedManager {
|
|
788
|
+
/**
|
|
789
|
+
* @param {HTMLElement} containerEl The .sk-feed element
|
|
790
|
+
* @param {import('../core/shortkit.js').ShortKit} shortKit
|
|
791
|
+
* @param {object} [options]
|
|
792
|
+
* @param {object} [options.config] FeedConfig from createFeedConfig()
|
|
793
|
+
* @param {string} [options.startAtItemId]
|
|
794
|
+
* @param {Function} [options.onFeedReady]
|
|
795
|
+
* @param {Function} [options.onDismiss]
|
|
796
|
+
* @param {Function} [options.onContentTapped]
|
|
797
|
+
* @param {Function} [options.onRemainingContentCountChange]
|
|
798
|
+
* @param {Function} [options.onDidFetchContentItems]
|
|
799
|
+
* @param {Function} [options.onLoop]
|
|
800
|
+
* @param {Function} [options.onFeedTransition]
|
|
801
|
+
* @param {Function} [options.onFormatChange]
|
|
802
|
+
*/
|
|
803
|
+
constructor(containerEl, shortKit, options = {}) {
|
|
804
|
+
this.container = containerEl;
|
|
805
|
+
this._sk = shortKit;
|
|
806
|
+
this._options = options;
|
|
807
|
+
this._config = options.config || {};
|
|
808
|
+
this.pool = shortKit._playerPool;
|
|
809
|
+
this.storyboardCache = shortKit._storyboardCache;
|
|
810
|
+
this.items = [];
|
|
811
|
+
this._cursor = null;
|
|
812
|
+
this._hasMore = true;
|
|
813
|
+
this.activeItemId = null;
|
|
814
|
+
this.activeIndex = 0;
|
|
815
|
+
this.isMuted = this._config.muteOnStart !== false;
|
|
816
|
+
this.playbackRate = 1;
|
|
817
|
+
this.itemEls = /* @__PURE__ */ new Map();
|
|
818
|
+
this.rafIds = /* @__PURE__ */ new Map();
|
|
819
|
+
this._centerTimeout = null;
|
|
820
|
+
this._loadingMore = false;
|
|
821
|
+
this._wasPlayingBeforeHidden = false;
|
|
822
|
+
this._speculativeItemId = null;
|
|
823
|
+
this._speculativePlayer = null;
|
|
824
|
+
this._lastPredictedIndex = -1;
|
|
825
|
+
this._captionsOn = false;
|
|
826
|
+
this._captionCues = /* @__PURE__ */ new Map();
|
|
827
|
+
this._captionFetches = /* @__PURE__ */ new Map();
|
|
828
|
+
this._prefetchedThumbs = /* @__PURE__ */ new Set();
|
|
829
|
+
this._destroyed = false;
|
|
830
|
+
this._overlayContainers = /* @__PURE__ */ new Map();
|
|
831
|
+
this.feedWrapper = containerEl.parentElement;
|
|
832
|
+
}
|
|
833
|
+
async init() {
|
|
834
|
+
const isCustom = this._config.feedSource === "custom";
|
|
835
|
+
if (!isCustom) {
|
|
836
|
+
const result = await this._fetchFeed(10);
|
|
837
|
+
this._appendItems(result.items);
|
|
838
|
+
this._prefetchThumbnails(0);
|
|
839
|
+
if (this._options.onDidFetchContentItems) this._options.onDidFetchContentItems(result.items);
|
|
840
|
+
if (this._sk._onDidFetchContentItems) this._sk._onDidFetchContentItems(result.items);
|
|
841
|
+
}
|
|
842
|
+
this._setupObserver();
|
|
843
|
+
this._setupScrollEnd();
|
|
844
|
+
this._setupKeyboard();
|
|
845
|
+
this._setupVisibilityHandler();
|
|
846
|
+
this._setupCellHeightObserver();
|
|
847
|
+
this._sk.player.registerSurface(this);
|
|
848
|
+
this._sk._tracker.trackFeedEntry();
|
|
849
|
+
this._pushPlayerState();
|
|
850
|
+
if (this._options.onFeedReady) this._options.onFeedReady();
|
|
851
|
+
}
|
|
852
|
+
destroy() {
|
|
853
|
+
if (this._destroyed) return;
|
|
854
|
+
this._destroyed = true;
|
|
855
|
+
this._sk._tracker.trackFeedExit();
|
|
856
|
+
this._sk._tracker.deactivateContent();
|
|
857
|
+
this._sk.player.unregisterSurface(this);
|
|
858
|
+
this._sk._unregisterSurface(this);
|
|
859
|
+
for (const [id] of this.rafIds) this._stopTimeLoop(id);
|
|
860
|
+
if (this.activeItemId) this._deactivateItem(this.activeItemId);
|
|
861
|
+
for (const [id] of [...this.pool.assignments]) this.pool.release(id);
|
|
862
|
+
if (this.observer) {
|
|
863
|
+
this.observer.disconnect();
|
|
864
|
+
this.observer = null;
|
|
865
|
+
}
|
|
866
|
+
if (this._resizeObserver) {
|
|
867
|
+
this._resizeObserver.disconnect();
|
|
868
|
+
this._resizeObserver = null;
|
|
869
|
+
}
|
|
870
|
+
this.container.innerHTML = "";
|
|
871
|
+
this.itemEls.clear();
|
|
872
|
+
this.items = [];
|
|
873
|
+
this.activeItemId = null;
|
|
874
|
+
this.activeIndex = 0;
|
|
875
|
+
this._speculativeItemId = null;
|
|
876
|
+
this._speculativePlayer = null;
|
|
877
|
+
}
|
|
878
|
+
// ── Surface command interface (forwarded from ShortKitPlayer) ──
|
|
879
|
+
play() {
|
|
880
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
881
|
+
if (player) {
|
|
882
|
+
player.play().catch(() => {
|
|
883
|
+
});
|
|
884
|
+
this._pushPlayerState({ playerState: "playing" });
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
pause() {
|
|
888
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
889
|
+
if (player) {
|
|
890
|
+
player.pause();
|
|
891
|
+
this._pushPlayerState({ playerState: "paused" });
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
seek(time) {
|
|
895
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
896
|
+
if (player && player.duration) {
|
|
897
|
+
player.currentTime = time;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
seekAndPlay(time) {
|
|
901
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
902
|
+
if (player && player.duration) {
|
|
903
|
+
player.currentTime = time;
|
|
904
|
+
player.play().catch(() => {
|
|
905
|
+
});
|
|
906
|
+
this._pushPlayerState({ playerState: "playing" });
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
setMuted(muted) {
|
|
910
|
+
this.isMuted = muted;
|
|
911
|
+
for (const [, player] of this.pool._players) {
|
|
912
|
+
player.muted = muted;
|
|
913
|
+
}
|
|
914
|
+
this._pushPlayerState({ isMuted: muted });
|
|
915
|
+
}
|
|
916
|
+
skipToNext() {
|
|
917
|
+
this.navigateDown();
|
|
918
|
+
}
|
|
919
|
+
skipToPrevious() {
|
|
920
|
+
this.navigateUp();
|
|
921
|
+
}
|
|
922
|
+
setPlaybackRate(rate) {
|
|
923
|
+
this.playbackRate = rate;
|
|
924
|
+
for (const [, player] of this.pool._players) {
|
|
925
|
+
player.playbackRate = rate;
|
|
926
|
+
}
|
|
927
|
+
this._pushPlayerState({ playbackRate: rate });
|
|
928
|
+
}
|
|
929
|
+
setCaptionsEnabled(enabled) {
|
|
930
|
+
this._captionsOn = enabled;
|
|
931
|
+
this._pushPlayerState({ captionsEnabled: enabled });
|
|
932
|
+
if (enabled) {
|
|
933
|
+
const item = this.items[this.activeIndex];
|
|
934
|
+
if (item?.captionTracks?.length) this._fetchCaptionVTT(item);
|
|
935
|
+
}
|
|
936
|
+
if (!enabled) {
|
|
937
|
+
this._sk.player._pushState({ activeCue: null });
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
selectCaptionTrack(track) {
|
|
941
|
+
this._pushPlayerState({ activeCaptionTrack: track });
|
|
942
|
+
}
|
|
943
|
+
sendContentSignal(signal) {
|
|
944
|
+
if (this.activeItemId) {
|
|
945
|
+
this._sk._tracker.trackContentSignal(this.activeItemId, signal);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
setMaxBitrate(_bitrate) {
|
|
949
|
+
}
|
|
950
|
+
seekThumbnail(time) {
|
|
951
|
+
if (!this.activeItemId) return null;
|
|
952
|
+
const item = this.items[this.activeIndex];
|
|
953
|
+
if (!item) return null;
|
|
954
|
+
const playbackId = StoryboardCache.extractPlaybackId(item.thumbnailUrl);
|
|
955
|
+
if (!playbackId) return null;
|
|
956
|
+
return this.storyboardCache.tileAt(playbackId, time);
|
|
957
|
+
}
|
|
958
|
+
// ── Custom feed mode methods ──
|
|
959
|
+
/**
|
|
960
|
+
* Replace all feed items with the provided list.
|
|
961
|
+
* @param {Array<{type: string, playbackId?: string, fallbackUrl?: string}|object>} items
|
|
962
|
+
* FeedInput objects or pre-resolved ContentItem objects.
|
|
963
|
+
*/
|
|
964
|
+
setFeedItems(items) {
|
|
965
|
+
if (this._config.feedSource !== "custom") {
|
|
966
|
+
console.warn("[ShortKit] setFeedItems() called on a non-custom feed — ignored");
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
if (this.activeItemId) {
|
|
970
|
+
this._deactivateItem(this.activeItemId);
|
|
971
|
+
this.activeItemId = null;
|
|
972
|
+
this.activeIndex = 0;
|
|
973
|
+
}
|
|
974
|
+
if (this.observer) this.observer.disconnect();
|
|
975
|
+
this.container.innerHTML = "";
|
|
976
|
+
this.itemEls.clear();
|
|
977
|
+
this.items = [];
|
|
978
|
+
const resolved = this._resolveFeedInputs(items);
|
|
979
|
+
this._appendItems(resolved);
|
|
980
|
+
this._setupObserver();
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Append items to the existing feed without disrupting current playback.
|
|
984
|
+
* @param {Array<{type: string, playbackId?: string, fallbackUrl?: string}|object>} items
|
|
985
|
+
*/
|
|
986
|
+
appendFeedItems(items) {
|
|
987
|
+
if (this._config.feedSource !== "custom") {
|
|
988
|
+
console.warn("[ShortKit] appendFeedItems() called on a non-custom feed — ignored");
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
const resolved = this._resolveFeedInputs(items);
|
|
992
|
+
this._appendItems(resolved);
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* Apply a filter to the feed.
|
|
996
|
+
* For algorithmic feeds: clears and re-fetches with the new filter.
|
|
997
|
+
* For custom feeds: no-op (log a warning).
|
|
998
|
+
* @param {object} filter
|
|
999
|
+
*/
|
|
1000
|
+
applyFilter(filter) {
|
|
1001
|
+
if (this._config.feedSource === "custom") {
|
|
1002
|
+
console.warn("[ShortKit] applyFilter() is a no-op for custom feeds — use setFeedItems() to update content");
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
this._config.filter = filter;
|
|
1006
|
+
this._cursor = null;
|
|
1007
|
+
this._hasMore = true;
|
|
1008
|
+
if (this.activeItemId) {
|
|
1009
|
+
this._deactivateItem(this.activeItemId);
|
|
1010
|
+
this.activeItemId = null;
|
|
1011
|
+
this.activeIndex = 0;
|
|
1012
|
+
}
|
|
1013
|
+
if (this.observer) this.observer.disconnect();
|
|
1014
|
+
this.container.innerHTML = "";
|
|
1015
|
+
this.itemEls.clear();
|
|
1016
|
+
this.items = [];
|
|
1017
|
+
this._fetchFeed(10).then((result) => {
|
|
1018
|
+
this._appendItems(result.items);
|
|
1019
|
+
this._setupObserver();
|
|
1020
|
+
this._prefetchThumbnails(0);
|
|
1021
|
+
if (this._options.onDidFetchContentItems) this._options.onDidFetchContentItems(result.items);
|
|
1022
|
+
if (this._sk._onDidFetchContentItems) this._sk._onDidFetchContentItems(result.items);
|
|
1023
|
+
}).catch(() => {
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Convert FeedInput objects to ContentItem objects suitable for _appendItems().
|
|
1028
|
+
* If the input already looks like a ContentItem (has streamingUrl), it's passed through.
|
|
1029
|
+
* For { type: 'video', playbackId } inputs, URLs are derived from Mux URL conventions.
|
|
1030
|
+
* @param {Array} inputs
|
|
1031
|
+
* @returns {Array} ContentItems
|
|
1032
|
+
*/
|
|
1033
|
+
_resolveFeedInputs(inputs) {
|
|
1034
|
+
return inputs.map((input) => {
|
|
1035
|
+
if (input.streamingUrl) return input;
|
|
1036
|
+
if (input.type === "video" && input.playbackId) {
|
|
1037
|
+
return {
|
|
1038
|
+
id: input.playbackId,
|
|
1039
|
+
streamingUrl: `https://stream.mux.com/${input.playbackId}.m3u8`,
|
|
1040
|
+
thumbnailUrl: `https://image.mux.com/${input.playbackId}/thumbnail.jpg`,
|
|
1041
|
+
title: input.title || "",
|
|
1042
|
+
description: input.description || "",
|
|
1043
|
+
author: input.author || "",
|
|
1044
|
+
section: input.section || "",
|
|
1045
|
+
articleUrl: input.articleUrl || null,
|
|
1046
|
+
duration: input.duration || 0,
|
|
1047
|
+
captionTracks: input.captionTracks || []
|
|
1048
|
+
};
|
|
1049
|
+
}
|
|
1050
|
+
if (input.fallbackUrl) {
|
|
1051
|
+
return {
|
|
1052
|
+
id: input.id || input.fallbackUrl,
|
|
1053
|
+
streamingUrl: input.fallbackUrl,
|
|
1054
|
+
thumbnailUrl: input.thumbnailUrl || "",
|
|
1055
|
+
title: input.title || "",
|
|
1056
|
+
description: input.description || "",
|
|
1057
|
+
author: input.author || "",
|
|
1058
|
+
section: input.section || "",
|
|
1059
|
+
articleUrl: input.articleUrl || null,
|
|
1060
|
+
duration: input.duration || 0,
|
|
1061
|
+
captionTracks: input.captionTracks || []
|
|
1062
|
+
};
|
|
1063
|
+
}
|
|
1064
|
+
console.warn("[ShortKit] Unresolvable FeedInput — skipping:", input);
|
|
1065
|
+
return null;
|
|
1066
|
+
}).filter(Boolean);
|
|
1067
|
+
}
|
|
1068
|
+
// ── Internal: API wrapper ──
|
|
1069
|
+
async _fetchFeed(limit = 10) {
|
|
1070
|
+
const result = await this._sk._apiClient.fetchFeed({
|
|
1071
|
+
limit,
|
|
1072
|
+
cursor: this._cursor,
|
|
1073
|
+
filter: this._config.filter || void 0
|
|
1074
|
+
});
|
|
1075
|
+
this._cursor = result.nextCursor;
|
|
1076
|
+
this._hasMore = result.hasMore;
|
|
1077
|
+
const items = result.items.map((item) => ({
|
|
1078
|
+
id: item.id,
|
|
1079
|
+
streamingUrl: item.streamingUrl,
|
|
1080
|
+
thumbnailUrl: item.thumbnailUrl,
|
|
1081
|
+
title: item.title || "",
|
|
1082
|
+
description: item.description || "",
|
|
1083
|
+
author: item.author || "",
|
|
1084
|
+
section: item.customMetadata?.category?.toUpperCase() || item.customMetadata?.section?.toUpperCase() || "",
|
|
1085
|
+
articleUrl: item.articleUrl || null,
|
|
1086
|
+
duration: item.duration || 0,
|
|
1087
|
+
captionTracks: item.captionTracks || []
|
|
1088
|
+
}));
|
|
1089
|
+
return { items, nextCursor: result.nextCursor, hasMore: result.hasMore };
|
|
1090
|
+
}
|
|
1091
|
+
// ── Internal: Push state to ShortKitPlayer ──
|
|
1092
|
+
_pushPlayerState(updates = {}) {
|
|
1093
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
1094
|
+
const item = this.items[this.activeIndex] || null;
|
|
1095
|
+
const state = {
|
|
1096
|
+
currentItem: item,
|
|
1097
|
+
isMuted: this.isMuted,
|
|
1098
|
+
playbackRate: this.playbackRate,
|
|
1099
|
+
captionsEnabled: this._captionsOn,
|
|
1100
|
+
activeCaptionTrack: null,
|
|
1101
|
+
activeCue: null,
|
|
1102
|
+
prefetchedAheadCount: Math.max(0, this.items.length - this.activeIndex - 1),
|
|
1103
|
+
remainingContentCount: this.items.length - this.activeIndex - 1,
|
|
1104
|
+
...updates
|
|
1105
|
+
};
|
|
1106
|
+
if (player) {
|
|
1107
|
+
state.time = {
|
|
1108
|
+
current: player.currentTime || 0,
|
|
1109
|
+
duration: player.duration || 0,
|
|
1110
|
+
buffered: player.buffered?.length > 0 ? player.buffered.end(player.buffered.length - 1) : 0
|
|
1111
|
+
};
|
|
1112
|
+
if (!updates.playerState) {
|
|
1113
|
+
state.playerState = player.paused ? "paused" : "playing";
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
this._sk.player._pushState(state);
|
|
1117
|
+
}
|
|
1118
|
+
// --- Build ---
|
|
1119
|
+
_appendItems(newItems) {
|
|
1120
|
+
for (const item of newItems) {
|
|
1121
|
+
if (this.itemEls.has(item.id)) continue;
|
|
1122
|
+
this.items.push(item);
|
|
1123
|
+
const el = createFeedItem(item);
|
|
1124
|
+
this.container.appendChild(el);
|
|
1125
|
+
this.itemEls.set(item.id, el);
|
|
1126
|
+
this._bindControls(item, el);
|
|
1127
|
+
this._createOverlayContainer(item.id, el);
|
|
1128
|
+
this.observer?.observe(el);
|
|
1129
|
+
}
|
|
1130
|
+
this._pushPlayerState({
|
|
1131
|
+
remainingContentCount: this.items.length - this.activeIndex - 1,
|
|
1132
|
+
prefetchedAheadCount: Math.max(0, this.items.length - this.activeIndex - 1)
|
|
1133
|
+
});
|
|
1134
|
+
if (this._options.onRemainingContentCountChange) {
|
|
1135
|
+
this._options.onRemainingContentCountChange(this.items.length - this.activeIndex - 1);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
async _loadMore() {
|
|
1139
|
+
if (this._loadingMore || !this._hasMore) return;
|
|
1140
|
+
this._loadingMore = true;
|
|
1141
|
+
try {
|
|
1142
|
+
const result = await this._fetchFeed(10);
|
|
1143
|
+
this._appendItems(result.items);
|
|
1144
|
+
if (this._options.onDidFetchContentItems) this._options.onDidFetchContentItems(result.items);
|
|
1145
|
+
if (this._sk._onDidFetchContentItems) this._sk._onDidFetchContentItems(result.items);
|
|
1146
|
+
} finally {
|
|
1147
|
+
this._loadingMore = false;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
// --- Navigation ---
|
|
1151
|
+
navigateTo(index) {
|
|
1152
|
+
const clamped = Math.max(0, Math.min(this.items.length - 1, index));
|
|
1153
|
+
const el = this.itemEls.get(this.items[clamped].id);
|
|
1154
|
+
el.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
1155
|
+
}
|
|
1156
|
+
navigateUp() {
|
|
1157
|
+
this.navigateTo(this.activeIndex - 1);
|
|
1158
|
+
}
|
|
1159
|
+
navigateDown() {
|
|
1160
|
+
this.navigateTo(this.activeIndex + 1);
|
|
1161
|
+
}
|
|
1162
|
+
// --- Keyboard (arrow keys) ---
|
|
1163
|
+
_setupKeyboard() {
|
|
1164
|
+
document.addEventListener("keydown", (e) => {
|
|
1165
|
+
if (e.key === "ArrowUp") {
|
|
1166
|
+
e.preventDefault();
|
|
1167
|
+
this.navigateUp();
|
|
1168
|
+
}
|
|
1169
|
+
if (e.key === "ArrowDown") {
|
|
1170
|
+
e.preventDefault();
|
|
1171
|
+
this.navigateDown();
|
|
1172
|
+
}
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
// --- Tab Visibility ---
|
|
1176
|
+
_setupVisibilityHandler() {
|
|
1177
|
+
document.addEventListener("visibilitychange", () => {
|
|
1178
|
+
if (document.hidden) {
|
|
1179
|
+
this._handleTabHidden();
|
|
1180
|
+
} else {
|
|
1181
|
+
this._handleTabVisible();
|
|
1182
|
+
}
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
_handleTabHidden() {
|
|
1186
|
+
this._cancelSpeculation();
|
|
1187
|
+
if (!this.activeItemId) return;
|
|
1188
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
1189
|
+
if (player && !player.paused) {
|
|
1190
|
+
this._wasPlayingBeforeHidden = true;
|
|
1191
|
+
player.pause();
|
|
1192
|
+
} else {
|
|
1193
|
+
this._wasPlayingBeforeHidden = false;
|
|
1194
|
+
}
|
|
1195
|
+
this._stopTimeLoop(this.activeItemId);
|
|
1196
|
+
}
|
|
1197
|
+
_handleTabVisible() {
|
|
1198
|
+
if (!this.activeItemId) return;
|
|
1199
|
+
if (this._wasPlayingBeforeHidden) {
|
|
1200
|
+
const player2 = this.pool.getPlayer(this.activeItemId);
|
|
1201
|
+
if (player2) player2.play().catch(() => {
|
|
1202
|
+
});
|
|
1203
|
+
this._wasPlayingBeforeHidden = false;
|
|
1204
|
+
}
|
|
1205
|
+
const el = this.itemEls.get(this.activeItemId);
|
|
1206
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
1207
|
+
if (el && player) this._startTimeLoop(this.activeItemId, el, player);
|
|
1208
|
+
}
|
|
1209
|
+
// --- Intersection Observer ---
|
|
1210
|
+
_setupObserver() {
|
|
1211
|
+
this.observer = new IntersectionObserver((entries) => {
|
|
1212
|
+
for (const entry of entries) {
|
|
1213
|
+
const id = entry.target.dataset.itemId;
|
|
1214
|
+
if (entry.isIntersecting && entry.intersectionRatio > 0.6) {
|
|
1215
|
+
this._activateItem(id);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
}, { root: this.container, threshold: [0, 0.6, 1] });
|
|
1219
|
+
for (const el of this.itemEls.values()) {
|
|
1220
|
+
this.observer.observe(el);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
// --- Stable cell height for scroll prediction ---
|
|
1224
|
+
_setupCellHeightObserver() {
|
|
1225
|
+
this._cellHeight = 0;
|
|
1226
|
+
const measure = () => {
|
|
1227
|
+
const first = this.container.firstElementChild;
|
|
1228
|
+
if (first) {
|
|
1229
|
+
this._cellHeight = first.offsetHeight + parseFloat(getComputedStyle(first).marginBottom || "0");
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
measure();
|
|
1233
|
+
this._resizeObserver = new ResizeObserver(measure);
|
|
1234
|
+
this._resizeObserver.observe(this.container);
|
|
1235
|
+
}
|
|
1236
|
+
// --- Pre-warm adjacent on scroll end + scroll prediction ---
|
|
1237
|
+
_setupScrollEnd() {
|
|
1238
|
+
let scrollTimer;
|
|
1239
|
+
this.container.addEventListener("scroll", () => {
|
|
1240
|
+
this._predictAndSpeculate();
|
|
1241
|
+
clearTimeout(scrollTimer);
|
|
1242
|
+
scrollTimer = setTimeout(() => this._onScrollEnd(), 100);
|
|
1243
|
+
}, { passive: true });
|
|
1244
|
+
this.container.addEventListener("pointerdown", () => {
|
|
1245
|
+
this._cancelSpeculation();
|
|
1246
|
+
}, { passive: true });
|
|
1247
|
+
this.container.addEventListener("touchstart", () => {
|
|
1248
|
+
this._cancelSpeculation();
|
|
1249
|
+
}, { passive: true });
|
|
1250
|
+
}
|
|
1251
|
+
_onScrollEnd() {
|
|
1252
|
+
const idx = this.items.findIndex((i) => i.id === this.activeItemId);
|
|
1253
|
+
if (idx < 0) return;
|
|
1254
|
+
const adjacent = [this.items[idx - 1], this.items[idx + 1]].filter(Boolean);
|
|
1255
|
+
for (const item of adjacent) {
|
|
1256
|
+
const player = this.pool.acquire(item.id);
|
|
1257
|
+
this.pool.attachStream(item.id, item.streamingUrl);
|
|
1258
|
+
player.currentTime = 0;
|
|
1259
|
+
if (this._speculativeItemId !== item.id) {
|
|
1260
|
+
player.muted = true;
|
|
1261
|
+
const primingItemId = item.id;
|
|
1262
|
+
const p = player.play();
|
|
1263
|
+
if (p) p.then(() => {
|
|
1264
|
+
if (this.activeItemId !== primingItemId) player.pause();
|
|
1265
|
+
}).catch(() => {
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
this._prefetchThumbnails(idx);
|
|
1270
|
+
for (const adj of adjacent) {
|
|
1271
|
+
if (adj.captionTracks?.length) this._fetchCaptionVTT(adj);
|
|
1272
|
+
}
|
|
1273
|
+
if (idx >= this.items.length - 3) {
|
|
1274
|
+
this._loadMore();
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
// --- Scroll Prediction + Speculative Playback ---
|
|
1278
|
+
_predictAndSpeculate() {
|
|
1279
|
+
const containerHeight = this._cellHeight || this.container.clientHeight;
|
|
1280
|
+
if (containerHeight <= 0 || this.items.length === 0) return;
|
|
1281
|
+
const scrollTop = this.container.scrollTop;
|
|
1282
|
+
const predictedIndex = Math.round(scrollTop / containerHeight);
|
|
1283
|
+
const clamped = Math.max(0, Math.min(this.items.length - 1, predictedIndex));
|
|
1284
|
+
if (clamped === this._lastPredictedIndex) return;
|
|
1285
|
+
this._lastPredictedIndex = clamped;
|
|
1286
|
+
if (clamped === this.activeIndex) {
|
|
1287
|
+
this._cancelSpeculation();
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
if (Math.abs(clamped - this.activeIndex) > 1) return;
|
|
1291
|
+
const item = this.items[clamped];
|
|
1292
|
+
if (!item) return;
|
|
1293
|
+
if (this._speculativeItemId === item.id) return;
|
|
1294
|
+
this._cancelSpeculation();
|
|
1295
|
+
const player = this.pool.acquire(item.id);
|
|
1296
|
+
this.pool.attachStream(item.id, item.streamingUrl);
|
|
1297
|
+
player.currentTime = 0;
|
|
1298
|
+
player.muted = true;
|
|
1299
|
+
const el = this.itemEls.get(item.id);
|
|
1300
|
+
if (el) {
|
|
1301
|
+
const container = el.querySelector('[data-ref="videoContainer"]');
|
|
1302
|
+
player.style.opacity = "0";
|
|
1303
|
+
if (!container.contains(player)) container.insertBefore(player, container.firstChild);
|
|
1304
|
+
}
|
|
1305
|
+
const p = player.play();
|
|
1306
|
+
if (p) p.catch(() => {
|
|
1307
|
+
});
|
|
1308
|
+
this._speculativeItemId = item.id;
|
|
1309
|
+
this._speculativePlayer = player;
|
|
1310
|
+
this._pushPlayerState({ feedScrollPhase: "decelerating" });
|
|
1311
|
+
}
|
|
1312
|
+
/** Cancel any in-flight speculative playback. */
|
|
1313
|
+
_cancelSpeculation() {
|
|
1314
|
+
if (!this._speculativeItemId) return;
|
|
1315
|
+
const player = this.pool.getPlayer(this._speculativeItemId);
|
|
1316
|
+
if (player) {
|
|
1317
|
+
player.pause();
|
|
1318
|
+
player.style.opacity = "0";
|
|
1319
|
+
}
|
|
1320
|
+
this._speculativeItemId = null;
|
|
1321
|
+
this._speculativePlayer = null;
|
|
1322
|
+
this._lastPredictedIndex = -1;
|
|
1323
|
+
}
|
|
1324
|
+
// --- Activation ---
|
|
1325
|
+
_activateItem(id) {
|
|
1326
|
+
if (this.activeItemId === id) return;
|
|
1327
|
+
if (this._speculativeItemId && this._speculativeItemId !== id) {
|
|
1328
|
+
this._cancelSpeculation();
|
|
1329
|
+
}
|
|
1330
|
+
this._speculativeItemId = null;
|
|
1331
|
+
this._speculativePlayer = null;
|
|
1332
|
+
this._lastPredictedIndex = -1;
|
|
1333
|
+
const previousId = this.activeItemId;
|
|
1334
|
+
const previousIndex = this.activeIndex;
|
|
1335
|
+
if (this.activeItemId) this._deactivateItem(this.activeItemId);
|
|
1336
|
+
this.activeItemId = id;
|
|
1337
|
+
this.activeIndex = this.items.findIndex((i) => i.id === id);
|
|
1338
|
+
const item = this.items[this.activeIndex];
|
|
1339
|
+
if (previousId) {
|
|
1340
|
+
const direction = this.activeIndex > previousIndex ? "down" : "up";
|
|
1341
|
+
this._sk._tracker.trackSwipe(previousId, id, direction);
|
|
1342
|
+
}
|
|
1343
|
+
this._sk._tracker.activateContent(id);
|
|
1344
|
+
if (this._options.onFeedTransition) {
|
|
1345
|
+
this._options.onFeedTransition({ fromIndex: previousIndex, toIndex: this.activeIndex, item });
|
|
1346
|
+
}
|
|
1347
|
+
const el = this.itemEls.get(id);
|
|
1348
|
+
const player = this.pool.acquire(id);
|
|
1349
|
+
player.style.opacity = "0";
|
|
1350
|
+
const container = el.querySelector('[data-ref="videoContainer"]');
|
|
1351
|
+
if (!container.contains(player)) container.insertBefore(player, container.firstChild);
|
|
1352
|
+
this.pool.attachStream(id, item.streamingUrl, { isActive: true });
|
|
1353
|
+
this.pool.promoteToActive(id);
|
|
1354
|
+
player.muted = this.isMuted;
|
|
1355
|
+
player.playbackRate = this.playbackRate;
|
|
1356
|
+
player.currentTime = 0;
|
|
1357
|
+
const spinner = el.querySelector('[data-ref="spinner"]');
|
|
1358
|
+
const REVEAL_EVENTS = ["loadeddata", "seeked", "canplay", "timeupdate"];
|
|
1359
|
+
let spinnerTimeout = null;
|
|
1360
|
+
const revealOnFirstFrame = () => {
|
|
1361
|
+
if (player._skRevealedFor === id) return;
|
|
1362
|
+
if (player.readyState < 2) return;
|
|
1363
|
+
player._skRevealedFor = id;
|
|
1364
|
+
player.style.opacity = "1";
|
|
1365
|
+
clearTimeout(spinnerTimeout);
|
|
1366
|
+
spinner.classList.remove("visible");
|
|
1367
|
+
REVEAL_EVENTS.forEach((e) => player.removeEventListener(e, revealOnFirstFrame));
|
|
1368
|
+
this._sk._tracker.trackFirstFrame(id);
|
|
1369
|
+
};
|
|
1370
|
+
if (player.readyState >= 2 && !player.seeking) {
|
|
1371
|
+
player._skRevealedFor = id;
|
|
1372
|
+
player.style.opacity = "1";
|
|
1373
|
+
this._sk._tracker.trackFirstFrame(id);
|
|
1374
|
+
} else {
|
|
1375
|
+
spinnerTimeout = setTimeout(() => spinner.classList.add("visible"), 1e3);
|
|
1376
|
+
REVEAL_EVENTS.forEach((e) => player.addEventListener(e, revealOnFirstFrame));
|
|
1377
|
+
}
|
|
1378
|
+
const playPromise = player.play();
|
|
1379
|
+
if (playPromise) {
|
|
1380
|
+
playPromise.catch(() => {
|
|
1381
|
+
if (player.readyState >= 2) revealOnFirstFrame();
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
1384
|
+
this._sk._tracker.trackPlayStart(id);
|
|
1385
|
+
this._startTimeLoop(id, el, player);
|
|
1386
|
+
this._invokeOverlay(id, item);
|
|
1387
|
+
this._prefetchThumbnails(this.activeIndex);
|
|
1388
|
+
if (item.captionTracks?.length) this._fetchCaptionVTT(item);
|
|
1389
|
+
const playbackId = StoryboardCache.extractPlaybackId(item.thumbnailUrl);
|
|
1390
|
+
if (playbackId) this.storyboardCache.get(playbackId);
|
|
1391
|
+
this._pushPlayerState({
|
|
1392
|
+
playerState: "playing",
|
|
1393
|
+
currentItem: item,
|
|
1394
|
+
feedScrollPhase: "idle"
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
_deactivateItem(id) {
|
|
1398
|
+
const player = this.pool.getPlayer(id);
|
|
1399
|
+
if (player) {
|
|
1400
|
+
player.pause();
|
|
1401
|
+
player.style.opacity = "0";
|
|
1402
|
+
player._skRevealedFor = null;
|
|
1403
|
+
}
|
|
1404
|
+
this._stopTimeLoop(id);
|
|
1405
|
+
this._clearOverlay(id);
|
|
1406
|
+
this._sk._tracker.deactivateContent();
|
|
1407
|
+
}
|
|
1408
|
+
// --- Time loop ---
|
|
1409
|
+
_startTimeLoop(id, el, player) {
|
|
1410
|
+
this._stopTimeLoop(id);
|
|
1411
|
+
const tick = () => {
|
|
1412
|
+
if (this._destroyed) return;
|
|
1413
|
+
if (!player || player.paused) {
|
|
1414
|
+
this.rafIds.set(id, requestAnimationFrame(tick));
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
const current = player.currentTime || 0;
|
|
1418
|
+
const duration = player.duration || 0;
|
|
1419
|
+
const buffered = player.buffered?.length > 0 ? player.buffered.end(player.buffered.length - 1) : 0;
|
|
1420
|
+
this._sk.player._pushState({
|
|
1421
|
+
time: { current, duration, buffered },
|
|
1422
|
+
playerState: "playing"
|
|
1423
|
+
});
|
|
1424
|
+
if (this._captionsOn) {
|
|
1425
|
+
const cues = this._captionCues.get(id);
|
|
1426
|
+
if (cues) {
|
|
1427
|
+
const cue = cues.find((c) => current >= c.startTime && current <= c.endTime) || null;
|
|
1428
|
+
const prev = this._sk.player.activeCue;
|
|
1429
|
+
if (cue !== prev) {
|
|
1430
|
+
this._sk.player._pushState({ activeCue: cue });
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
if (player.ended) {
|
|
1435
|
+
player.currentTime = 0;
|
|
1436
|
+
player.play().catch(() => {
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
this.rafIds.set(id, requestAnimationFrame(tick));
|
|
1440
|
+
};
|
|
1441
|
+
this.rafIds.set(id, requestAnimationFrame(tick));
|
|
1442
|
+
}
|
|
1443
|
+
_stopTimeLoop(id) {
|
|
1444
|
+
const rafId = this.rafIds.get(id);
|
|
1445
|
+
if (rafId) {
|
|
1446
|
+
cancelAnimationFrame(rafId);
|
|
1447
|
+
this.rafIds.delete(id);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
// --- Controls ---
|
|
1451
|
+
_bindControls(item, el) {
|
|
1452
|
+
const tapZone = el.querySelector('[data-ref="tapZone"]');
|
|
1453
|
+
if (tapZone) {
|
|
1454
|
+
tapZone.addEventListener("click", () => {
|
|
1455
|
+
const player = this.pool.getPlayer(item.id);
|
|
1456
|
+
if (!player) return;
|
|
1457
|
+
if (player.paused) {
|
|
1458
|
+
player.play().catch(() => {
|
|
1459
|
+
});
|
|
1460
|
+
this._pushPlayerState({ playerState: "playing" });
|
|
1461
|
+
} else {
|
|
1462
|
+
player.pause();
|
|
1463
|
+
this._pushPlayerState({ playerState: "paused" });
|
|
1464
|
+
}
|
|
1465
|
+
if (this._sk._onContentTapped) {
|
|
1466
|
+
this._sk._onContentTapped(item);
|
|
1467
|
+
}
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
// --- Caption VTT fetch (deduped + cached) ---
|
|
1472
|
+
_fetchCaptionVTT(item) {
|
|
1473
|
+
if (this._captionCues.has(item.id)) return;
|
|
1474
|
+
if (this._captionFetches.has(item.id)) return;
|
|
1475
|
+
const track = item.captionTracks[0];
|
|
1476
|
+
if (!track?.url) return;
|
|
1477
|
+
const promise = fetch(track.url).then((res) => res.ok ? res.text() : null).then((text) => {
|
|
1478
|
+
if (text) this._captionCues.set(item.id, parseVTT(text));
|
|
1479
|
+
}).catch(() => {
|
|
1480
|
+
}).finally(() => this._captionFetches.delete(item.id));
|
|
1481
|
+
this._captionFetches.set(item.id, promise);
|
|
1482
|
+
}
|
|
1483
|
+
// --- Thumbnail prefetch for +/-3 items ---
|
|
1484
|
+
_prefetchThumbnails(centerIndex) {
|
|
1485
|
+
const radius = 3;
|
|
1486
|
+
const start = Math.max(0, centerIndex - radius);
|
|
1487
|
+
const end = Math.min(this.items.length, centerIndex + radius + 1);
|
|
1488
|
+
for (let i = start; i < end; i++) {
|
|
1489
|
+
const url = this.items[i].thumbnailUrl;
|
|
1490
|
+
if (!url || this._prefetchedThumbs.has(url)) continue;
|
|
1491
|
+
this._prefetchedThumbs.add(url);
|
|
1492
|
+
const img = new Image();
|
|
1493
|
+
img.src = url;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
// --- Custom overlay helpers ---
|
|
1497
|
+
/** Create and register the overlay container element for a feed cell. */
|
|
1498
|
+
_createOverlayContainer(itemId, cellEl) {
|
|
1499
|
+
const overlayEl = cellEl.querySelector('[data-ref="overlay"]');
|
|
1500
|
+
if (!overlayEl) return;
|
|
1501
|
+
this._overlayContainers.set(itemId, { el: overlayEl, unsub: null });
|
|
1502
|
+
}
|
|
1503
|
+
/** Invoke the developer overlay callback for the given item. */
|
|
1504
|
+
_invokeOverlay(itemId, item) {
|
|
1505
|
+
if (!this._config.overlay) return;
|
|
1506
|
+
const entry = this._overlayContainers.get(itemId);
|
|
1507
|
+
if (!entry) return;
|
|
1508
|
+
const player = this._sk.player;
|
|
1509
|
+
const surface = this;
|
|
1510
|
+
const listeners = [];
|
|
1511
|
+
const scopedPlayer = {
|
|
1512
|
+
get isMuted() {
|
|
1513
|
+
return player.isMuted;
|
|
1514
|
+
},
|
|
1515
|
+
get playbackRate() {
|
|
1516
|
+
return player.playbackRate;
|
|
1517
|
+
},
|
|
1518
|
+
get currentTime() {
|
|
1519
|
+
return player.time.current;
|
|
1520
|
+
},
|
|
1521
|
+
get duration() {
|
|
1522
|
+
return player.time.duration;
|
|
1523
|
+
},
|
|
1524
|
+
get captionsEnabled() {
|
|
1525
|
+
return player.captionsEnabled;
|
|
1526
|
+
},
|
|
1527
|
+
on(event, fn) {
|
|
1528
|
+
player.on(event, fn);
|
|
1529
|
+
listeners.push({ event, fn });
|
|
1530
|
+
},
|
|
1531
|
+
off(event, fn) {
|
|
1532
|
+
player.off(event, fn);
|
|
1533
|
+
const idx = listeners.findIndex((l) => l.event === event && l.fn === fn);
|
|
1534
|
+
if (idx >= 0) listeners.splice(idx, 1);
|
|
1535
|
+
},
|
|
1536
|
+
play() {
|
|
1537
|
+
surface.play();
|
|
1538
|
+
},
|
|
1539
|
+
pause() {
|
|
1540
|
+
surface.pause();
|
|
1541
|
+
},
|
|
1542
|
+
seek(time) {
|
|
1543
|
+
surface.seek(time);
|
|
1544
|
+
},
|
|
1545
|
+
setMuted(muted) {
|
|
1546
|
+
surface.setMuted(muted);
|
|
1547
|
+
},
|
|
1548
|
+
setPlaybackRate(rate) {
|
|
1549
|
+
surface.setPlaybackRate(rate);
|
|
1550
|
+
},
|
|
1551
|
+
setCaptionsEnabled(enabled) {
|
|
1552
|
+
surface.setCaptionsEnabled(enabled);
|
|
1553
|
+
},
|
|
1554
|
+
selectCaptionTrack(track) {
|
|
1555
|
+
surface.selectCaptionTrack(track);
|
|
1556
|
+
},
|
|
1557
|
+
sendContentSignal(signal) {
|
|
1558
|
+
surface.sendContentSignal(signal);
|
|
1559
|
+
},
|
|
1560
|
+
skipToNext() {
|
|
1561
|
+
surface.skipToNext();
|
|
1562
|
+
},
|
|
1563
|
+
skipToPrevious() {
|
|
1564
|
+
surface.skipToPrevious();
|
|
1565
|
+
},
|
|
1566
|
+
seekThumbnail(time) {
|
|
1567
|
+
return surface.seekThumbnail(time);
|
|
1568
|
+
}
|
|
1569
|
+
};
|
|
1570
|
+
const feed = {
|
|
1571
|
+
dismiss: () => {
|
|
1572
|
+
if (surface._feedDismiss) surface._feedDismiss();
|
|
1573
|
+
},
|
|
1574
|
+
navigateUp: () => surface.navigateUp(),
|
|
1575
|
+
navigateDown: () => surface.navigateDown()
|
|
1576
|
+
};
|
|
1577
|
+
entry.unsub = () => {
|
|
1578
|
+
for (const { event, fn } of listeners) player.off(event, fn);
|
|
1579
|
+
listeners.length = 0;
|
|
1580
|
+
};
|
|
1581
|
+
try {
|
|
1582
|
+
this._config.overlay(entry.el, { item, player: scopedPlayer, feed });
|
|
1583
|
+
} catch (e) {
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
/** Clear the overlay container's DOM and unsubscribe tracked listeners. */
|
|
1587
|
+
_clearOverlay(itemId) {
|
|
1588
|
+
const entry = this._overlayContainers.get(itemId);
|
|
1589
|
+
if (!entry) return;
|
|
1590
|
+
if (entry.unsub) {
|
|
1591
|
+
entry.unsub();
|
|
1592
|
+
entry.unsub = null;
|
|
1593
|
+
}
|
|
1594
|
+
entry.el.innerHTML = "";
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
class EmbeddedFeedManager {
|
|
1598
|
+
/**
|
|
1599
|
+
* @param {HTMLElement} containerEl The feed scroll container
|
|
1600
|
+
* @param {import('../core/shortkit.js').ShortKit} shortKit
|
|
1601
|
+
* @param {object} [options]
|
|
1602
|
+
* @param {string} [options.idPrefix] Element ID prefix (default 'skp')
|
|
1603
|
+
*/
|
|
1604
|
+
constructor(containerEl, shortKit, options = {}) {
|
|
1605
|
+
this.container = containerEl;
|
|
1606
|
+
this._sk = shortKit;
|
|
1607
|
+
this._options = options;
|
|
1608
|
+
this._idPrefix = options.idPrefix || "skp";
|
|
1609
|
+
this.pool = shortKit._playerPool;
|
|
1610
|
+
this.storyboardCache = shortKit._storyboardCache;
|
|
1611
|
+
this.items = [];
|
|
1612
|
+
this._cursor = null;
|
|
1613
|
+
this._hasMore = true;
|
|
1614
|
+
this.activeItemId = null;
|
|
1615
|
+
this.activeIndex = 0;
|
|
1616
|
+
this.isMuted = true;
|
|
1617
|
+
this.playbackRate = 1;
|
|
1618
|
+
this.itemEls = /* @__PURE__ */ new Map();
|
|
1619
|
+
this.rafIds = /* @__PURE__ */ new Map();
|
|
1620
|
+
this._loadingMore = false;
|
|
1621
|
+
this._wasPlayingBeforeHidden = false;
|
|
1622
|
+
this._speculativeItemId = null;
|
|
1623
|
+
this._speculativePlayer = null;
|
|
1624
|
+
this._lastPredictedIndex = -1;
|
|
1625
|
+
this._captionsOn = false;
|
|
1626
|
+
this._captionCues = /* @__PURE__ */ new Map();
|
|
1627
|
+
this._captionFetches = /* @__PURE__ */ new Map();
|
|
1628
|
+
this._prefetchedThumbs = /* @__PURE__ */ new Set();
|
|
1629
|
+
this._destroyed = false;
|
|
1630
|
+
this._overlayContainers = /* @__PURE__ */ new Map();
|
|
1631
|
+
this._feedDismiss = null;
|
|
1632
|
+
}
|
|
1633
|
+
_id(suffix) {
|
|
1634
|
+
return `${this._idPrefix}${suffix}`;
|
|
1635
|
+
}
|
|
1636
|
+
async initWithItems(items, startIndex = 0, { deferActivation = false } = {}) {
|
|
1637
|
+
this._appendItems(items);
|
|
1638
|
+
this._prefetchThumbnails(0);
|
|
1639
|
+
if (!deferActivation) this._setupObserver();
|
|
1640
|
+
this._setupScrollEnd();
|
|
1641
|
+
this._setupVisibilityHandler();
|
|
1642
|
+
this._setupCellHeightObserver();
|
|
1643
|
+
if (startIndex > 0 && startIndex < items.length) {
|
|
1644
|
+
const targetEl = this.itemEls.get(items[startIndex].id);
|
|
1645
|
+
if (targetEl) targetEl.scrollIntoView({ behavior: "instant", block: "start" });
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
startObserver() {
|
|
1649
|
+
if (!this.observer) this._setupObserver();
|
|
1650
|
+
}
|
|
1651
|
+
destroy() {
|
|
1652
|
+
this._destroyed = true;
|
|
1653
|
+
for (const [id] of this.rafIds) this._stopTimeLoop(id);
|
|
1654
|
+
if (this.activeItemId) this._deactivateItem(this.activeItemId);
|
|
1655
|
+
for (const [id] of [...this.pool.assignments]) this.pool.release(id);
|
|
1656
|
+
if (this.observer) {
|
|
1657
|
+
this.observer.disconnect();
|
|
1658
|
+
this.observer = null;
|
|
1659
|
+
}
|
|
1660
|
+
if (this._resizeObserver) {
|
|
1661
|
+
this._resizeObserver.disconnect();
|
|
1662
|
+
this._resizeObserver = null;
|
|
1663
|
+
}
|
|
1664
|
+
if (this._visHandler) document.removeEventListener("visibilitychange", this._visHandler);
|
|
1665
|
+
this.container.innerHTML = "";
|
|
1666
|
+
this.itemEls.clear();
|
|
1667
|
+
this.items = [];
|
|
1668
|
+
this.activeItemId = null;
|
|
1669
|
+
this.activeIndex = 0;
|
|
1670
|
+
this._speculativeItemId = null;
|
|
1671
|
+
this._speculativePlayer = null;
|
|
1672
|
+
}
|
|
1673
|
+
getActiveVideoElement() {
|
|
1674
|
+
if (!this.activeItemId) return null;
|
|
1675
|
+
return this.pool.getPlayer(this.activeItemId);
|
|
1676
|
+
}
|
|
1677
|
+
getActiveItemRect() {
|
|
1678
|
+
if (!this.activeItemId) return null;
|
|
1679
|
+
const el = this.itemEls.get(this.activeItemId);
|
|
1680
|
+
if (!el) return null;
|
|
1681
|
+
const container = el.querySelector('[data-ref="videoContainer"]');
|
|
1682
|
+
return container ? container.getBoundingClientRect() : el.getBoundingClientRect();
|
|
1683
|
+
}
|
|
1684
|
+
// ── Surface command interface (forwarded from ShortKitPlayer) ──
|
|
1685
|
+
play() {
|
|
1686
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
1687
|
+
if (player) {
|
|
1688
|
+
player.play().catch(() => {
|
|
1689
|
+
});
|
|
1690
|
+
this._pushPlayerState({ playerState: "playing" });
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
pause() {
|
|
1694
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
1695
|
+
if (player) {
|
|
1696
|
+
player.pause();
|
|
1697
|
+
this._pushPlayerState({ playerState: "paused" });
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
seek(time) {
|
|
1701
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
1702
|
+
if (player && player.duration) {
|
|
1703
|
+
player.currentTime = time;
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
setMuted(muted) {
|
|
1707
|
+
this.isMuted = muted;
|
|
1708
|
+
for (const [, player] of this.pool._players) {
|
|
1709
|
+
player.muted = muted;
|
|
1710
|
+
}
|
|
1711
|
+
this._pushPlayerState({ isMuted: muted });
|
|
1712
|
+
}
|
|
1713
|
+
skipToNext() {
|
|
1714
|
+
this.navigateDown();
|
|
1715
|
+
}
|
|
1716
|
+
skipToPrevious() {
|
|
1717
|
+
this.navigateUp();
|
|
1718
|
+
}
|
|
1719
|
+
setPlaybackRate(rate) {
|
|
1720
|
+
this.playbackRate = rate;
|
|
1721
|
+
for (const [, player] of this.pool._players) {
|
|
1722
|
+
player.playbackRate = rate;
|
|
1723
|
+
}
|
|
1724
|
+
this._pushPlayerState({ playbackRate: rate });
|
|
1725
|
+
}
|
|
1726
|
+
setCaptionsEnabled(enabled) {
|
|
1727
|
+
this._captionsOn = enabled;
|
|
1728
|
+
this._pushPlayerState({ captionsEnabled: enabled });
|
|
1729
|
+
if (enabled) {
|
|
1730
|
+
const item = this.items[this.activeIndex];
|
|
1731
|
+
if (item?.captionTracks?.length) this._fetchCaptionVTT(item);
|
|
1732
|
+
}
|
|
1733
|
+
if (!enabled) {
|
|
1734
|
+
this._sk.player._pushState({ activeCue: null });
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
selectCaptionTrack(track) {
|
|
1738
|
+
this._pushPlayerState({ activeCaptionTrack: track });
|
|
1739
|
+
}
|
|
1740
|
+
sendContentSignal(signal) {
|
|
1741
|
+
if (this.activeItemId) {
|
|
1742
|
+
this._sk._tracker.trackContentSignal(this.activeItemId, signal);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
seekThumbnail(time) {
|
|
1746
|
+
if (!this.activeItemId) return null;
|
|
1747
|
+
const item = this.items[this.activeIndex];
|
|
1748
|
+
if (!item) return null;
|
|
1749
|
+
const playbackId = StoryboardCache.extractPlaybackId(item.thumbnailUrl);
|
|
1750
|
+
if (!playbackId) return null;
|
|
1751
|
+
return this.storyboardCache.tileAt(playbackId, time);
|
|
1752
|
+
}
|
|
1753
|
+
// ── Internal: API wrapper ──
|
|
1754
|
+
async _fetchFeed(limit = 10) {
|
|
1755
|
+
const result = await this._sk._apiClient.fetchFeed({
|
|
1756
|
+
limit,
|
|
1757
|
+
cursor: this._cursor
|
|
1758
|
+
});
|
|
1759
|
+
this._cursor = result.nextCursor;
|
|
1760
|
+
this._hasMore = result.hasMore;
|
|
1761
|
+
const items = result.items.map((item) => ({
|
|
1762
|
+
id: item.id,
|
|
1763
|
+
streamingUrl: item.streamingUrl,
|
|
1764
|
+
thumbnailUrl: item.thumbnailUrl,
|
|
1765
|
+
title: item.title || "",
|
|
1766
|
+
description: item.description || "",
|
|
1767
|
+
author: item.author || "",
|
|
1768
|
+
section: item.customMetadata?.category?.toUpperCase() || item.customMetadata?.section?.toUpperCase() || "",
|
|
1769
|
+
articleUrl: item.articleUrl || null,
|
|
1770
|
+
duration: item.duration || 0,
|
|
1771
|
+
captionTracks: item.captionTracks || []
|
|
1772
|
+
}));
|
|
1773
|
+
return { items, nextCursor: result.nextCursor, hasMore: result.hasMore };
|
|
1774
|
+
}
|
|
1775
|
+
_appendItems(newItems) {
|
|
1776
|
+
for (const item of newItems) {
|
|
1777
|
+
if (this.itemEls.has(item.id)) continue;
|
|
1778
|
+
this.items.push(item);
|
|
1779
|
+
const el = createFeedItem(item);
|
|
1780
|
+
this.container.appendChild(el);
|
|
1781
|
+
this.itemEls.set(item.id, el);
|
|
1782
|
+
this._bindControls(item, el);
|
|
1783
|
+
this._createOverlayContainer(item.id, el);
|
|
1784
|
+
this.observer?.observe(el);
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
async _loadMore() {
|
|
1788
|
+
if (this._loadingMore || !this._hasMore) return;
|
|
1789
|
+
this._loadingMore = true;
|
|
1790
|
+
try {
|
|
1791
|
+
const result = await this._fetchFeed(10);
|
|
1792
|
+
this._appendItems(result.items);
|
|
1793
|
+
} finally {
|
|
1794
|
+
this._loadingMore = false;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
navigateTo(index) {
|
|
1798
|
+
const clamped = Math.max(0, Math.min(this.items.length - 1, index));
|
|
1799
|
+
const el = this.itemEls.get(this.items[clamped].id);
|
|
1800
|
+
el.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
1801
|
+
}
|
|
1802
|
+
navigateUp() {
|
|
1803
|
+
this.navigateTo(this.activeIndex - 1);
|
|
1804
|
+
}
|
|
1805
|
+
navigateDown() {
|
|
1806
|
+
this.navigateTo(this.activeIndex + 1);
|
|
1807
|
+
}
|
|
1808
|
+
_setupVisibilityHandler() {
|
|
1809
|
+
this._visHandler = () => {
|
|
1810
|
+
if (document.hidden) this._handleTabHidden();
|
|
1811
|
+
else this._handleTabVisible();
|
|
1812
|
+
};
|
|
1813
|
+
document.addEventListener("visibilitychange", this._visHandler);
|
|
1814
|
+
}
|
|
1815
|
+
_handleTabHidden() {
|
|
1816
|
+
this._cancelSpeculation();
|
|
1817
|
+
if (!this.activeItemId) return;
|
|
1818
|
+
const p = this.pool.getPlayer(this.activeItemId);
|
|
1819
|
+
if (p && !p.paused) {
|
|
1820
|
+
this._wasPlayingBeforeHidden = true;
|
|
1821
|
+
p.pause();
|
|
1822
|
+
} else {
|
|
1823
|
+
this._wasPlayingBeforeHidden = false;
|
|
1824
|
+
}
|
|
1825
|
+
this._stopTimeLoop(this.activeItemId);
|
|
1826
|
+
}
|
|
1827
|
+
_handleTabVisible() {
|
|
1828
|
+
if (!this.activeItemId) return;
|
|
1829
|
+
if (this._wasPlayingBeforeHidden) {
|
|
1830
|
+
const p2 = this.pool.getPlayer(this.activeItemId);
|
|
1831
|
+
if (p2) p2.play().catch(() => {
|
|
1832
|
+
});
|
|
1833
|
+
this._wasPlayingBeforeHidden = false;
|
|
1834
|
+
}
|
|
1835
|
+
const el = this.itemEls.get(this.activeItemId);
|
|
1836
|
+
const p = this.pool.getPlayer(this.activeItemId);
|
|
1837
|
+
if (el && p) this._startTimeLoop(this.activeItemId, el, p);
|
|
1838
|
+
}
|
|
1839
|
+
_setupObserver() {
|
|
1840
|
+
this.observer = new IntersectionObserver((entries) => {
|
|
1841
|
+
for (const entry of entries) {
|
|
1842
|
+
const id = entry.target.dataset.itemId;
|
|
1843
|
+
if (entry.isIntersecting && entry.intersectionRatio > 0.6) this._activateItem(id);
|
|
1844
|
+
}
|
|
1845
|
+
}, { root: this.container, threshold: [0, 0.6, 1] });
|
|
1846
|
+
for (const el of this.itemEls.values()) this.observer.observe(el);
|
|
1847
|
+
}
|
|
1848
|
+
_setupCellHeightObserver() {
|
|
1849
|
+
this._cellHeight = 0;
|
|
1850
|
+
const measure = () => {
|
|
1851
|
+
const first = this.container.firstElementChild;
|
|
1852
|
+
if (first) this._cellHeight = first.offsetHeight + parseFloat(getComputedStyle(first).marginBottom || "0");
|
|
1853
|
+
};
|
|
1854
|
+
measure();
|
|
1855
|
+
this._resizeObserver = new ResizeObserver(measure);
|
|
1856
|
+
this._resizeObserver.observe(this.container);
|
|
1857
|
+
}
|
|
1858
|
+
_setupScrollEnd() {
|
|
1859
|
+
let scrollTimer;
|
|
1860
|
+
this.container.addEventListener("scroll", () => {
|
|
1861
|
+
this._predictAndSpeculate();
|
|
1862
|
+
clearTimeout(scrollTimer);
|
|
1863
|
+
scrollTimer = setTimeout(() => this._onScrollEnd(), 100);
|
|
1864
|
+
}, { passive: true });
|
|
1865
|
+
this.container.addEventListener("pointerdown", () => this._cancelSpeculation(), { passive: true });
|
|
1866
|
+
this.container.addEventListener("touchstart", () => this._cancelSpeculation(), { passive: true });
|
|
1867
|
+
}
|
|
1868
|
+
_onScrollEnd() {
|
|
1869
|
+
const idx = this.items.findIndex((i) => i.id === this.activeItemId);
|
|
1870
|
+
if (idx < 0) return;
|
|
1871
|
+
const adjacent = [this.items[idx - 1], this.items[idx + 1]].filter(Boolean);
|
|
1872
|
+
for (const item of adjacent) {
|
|
1873
|
+
const p = this.pool.acquire(item.id);
|
|
1874
|
+
this.pool.attachStream(item.id, item.streamingUrl);
|
|
1875
|
+
p.currentTime = 0;
|
|
1876
|
+
if (this._speculativeItemId !== item.id) {
|
|
1877
|
+
p.muted = true;
|
|
1878
|
+
const pid = item.id;
|
|
1879
|
+
const pp = p.play();
|
|
1880
|
+
if (pp) pp.then(() => {
|
|
1881
|
+
if (this.activeItemId !== pid) p.pause();
|
|
1882
|
+
}).catch(() => {
|
|
1883
|
+
});
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
this._prefetchThumbnails(idx);
|
|
1887
|
+
for (const adj of adjacent) {
|
|
1888
|
+
if (adj.captionTracks?.length) this._fetchCaptionVTT(adj);
|
|
1889
|
+
}
|
|
1890
|
+
if (idx >= this.items.length - 3) this._loadMore();
|
|
1891
|
+
}
|
|
1892
|
+
_predictAndSpeculate() {
|
|
1893
|
+
const containerHeight = this._cellHeight || this.container.clientHeight;
|
|
1894
|
+
if (containerHeight <= 0 || this.items.length === 0) return;
|
|
1895
|
+
const predictedIndex = Math.round(this.container.scrollTop / containerHeight);
|
|
1896
|
+
const clamped = Math.max(0, Math.min(this.items.length - 1, predictedIndex));
|
|
1897
|
+
if (clamped === this._lastPredictedIndex) return;
|
|
1898
|
+
this._lastPredictedIndex = clamped;
|
|
1899
|
+
if (clamped === this.activeIndex) {
|
|
1900
|
+
this._cancelSpeculation();
|
|
1901
|
+
return;
|
|
1902
|
+
}
|
|
1903
|
+
if (Math.abs(clamped - this.activeIndex) > 1) return;
|
|
1904
|
+
const item = this.items[clamped];
|
|
1905
|
+
if (!item || this._speculativeItemId === item.id) return;
|
|
1906
|
+
this._cancelSpeculation();
|
|
1907
|
+
const player = this.pool.acquire(item.id);
|
|
1908
|
+
this.pool.attachStream(item.id, item.streamingUrl);
|
|
1909
|
+
player.currentTime = 0;
|
|
1910
|
+
player.muted = true;
|
|
1911
|
+
const el = this.itemEls.get(item.id);
|
|
1912
|
+
if (el) {
|
|
1913
|
+
const c = el.querySelector('[data-ref="videoContainer"]');
|
|
1914
|
+
player.style.opacity = "0";
|
|
1915
|
+
if (!c.contains(player)) c.insertBefore(player, c.firstChild);
|
|
1916
|
+
}
|
|
1917
|
+
const p = player.play();
|
|
1918
|
+
if (p) p.catch(() => {
|
|
1919
|
+
});
|
|
1920
|
+
this._speculativeItemId = item.id;
|
|
1921
|
+
this._speculativePlayer = player;
|
|
1922
|
+
}
|
|
1923
|
+
_cancelSpeculation() {
|
|
1924
|
+
if (!this._speculativeItemId) return;
|
|
1925
|
+
const p = this.pool.getPlayer(this._speculativeItemId);
|
|
1926
|
+
if (p) {
|
|
1927
|
+
p.pause();
|
|
1928
|
+
p.style.opacity = "0";
|
|
1929
|
+
}
|
|
1930
|
+
this._speculativeItemId = null;
|
|
1931
|
+
this._speculativePlayer = null;
|
|
1932
|
+
this._lastPredictedIndex = -1;
|
|
1933
|
+
}
|
|
1934
|
+
_activateItem(id) {
|
|
1935
|
+
if (this.activeItemId === id) return;
|
|
1936
|
+
if (this._speculativeItemId && this._speculativeItemId !== id) this._cancelSpeculation();
|
|
1937
|
+
this._speculativeItemId = null;
|
|
1938
|
+
this._speculativePlayer = null;
|
|
1939
|
+
this._lastPredictedIndex = -1;
|
|
1940
|
+
if (this.activeItemId) this._deactivateItem(this.activeItemId);
|
|
1941
|
+
this.activeItemId = id;
|
|
1942
|
+
this.activeIndex = this.items.findIndex((i) => i.id === id);
|
|
1943
|
+
const item = this.items[this.activeIndex];
|
|
1944
|
+
const el = this.itemEls.get(id);
|
|
1945
|
+
const player = this.pool.acquire(id);
|
|
1946
|
+
const isTransfer = !!player._skFeedTransfer;
|
|
1947
|
+
delete player._skFeedTransfer;
|
|
1948
|
+
if (!isTransfer) player.style.opacity = "0";
|
|
1949
|
+
const container = el.querySelector('[data-ref="videoContainer"]');
|
|
1950
|
+
if (!container.contains(player)) container.insertBefore(player, container.firstChild);
|
|
1951
|
+
this.pool.attachStream(id, item.streamingUrl, { isActive: true });
|
|
1952
|
+
this.pool.promoteToActive(id);
|
|
1953
|
+
player.muted = this.isMuted;
|
|
1954
|
+
player.playbackRate = this.playbackRate;
|
|
1955
|
+
if (!isTransfer) player.currentTime = 0;
|
|
1956
|
+
const spinner = el.querySelector('[data-ref="spinner"]');
|
|
1957
|
+
const REVEAL_EVENTS = ["loadeddata", "seeked", "canplay", "timeupdate"];
|
|
1958
|
+
let spinnerTimeout = null;
|
|
1959
|
+
const revealOnFirstFrame = () => {
|
|
1960
|
+
if (player._skRevealedFor === id) return;
|
|
1961
|
+
if (player.readyState < 2) return;
|
|
1962
|
+
player._skRevealedFor = id;
|
|
1963
|
+
player.style.opacity = "1";
|
|
1964
|
+
clearTimeout(spinnerTimeout);
|
|
1965
|
+
spinner.classList.remove("visible");
|
|
1966
|
+
REVEAL_EVENTS.forEach((e) => player.removeEventListener(e, revealOnFirstFrame));
|
|
1967
|
+
};
|
|
1968
|
+
if (isTransfer) {
|
|
1969
|
+
player._skRevealedFor = id;
|
|
1970
|
+
player.style.opacity = "1";
|
|
1971
|
+
} else if (player.readyState >= 2 && !player.seeking) {
|
|
1972
|
+
player._skRevealedFor = id;
|
|
1973
|
+
player.style.opacity = "1";
|
|
1974
|
+
} else {
|
|
1975
|
+
spinnerTimeout = setTimeout(() => spinner.classList.add("visible"), 1e3);
|
|
1976
|
+
REVEAL_EVENTS.forEach((e) => player.addEventListener(e, revealOnFirstFrame));
|
|
1977
|
+
}
|
|
1978
|
+
const playPromise = player.play();
|
|
1979
|
+
if (playPromise) playPromise.catch(() => {
|
|
1980
|
+
if (player.readyState >= 2) revealOnFirstFrame();
|
|
1981
|
+
});
|
|
1982
|
+
this._startTimeLoop(id, el, player);
|
|
1983
|
+
this._invokeOverlay(id, item);
|
|
1984
|
+
this._prefetchThumbnails(this.activeIndex);
|
|
1985
|
+
if (item.captionTracks?.length) this._fetchCaptionVTT(item);
|
|
1986
|
+
const playbackId = StoryboardCache.extractPlaybackId(item.thumbnailUrl);
|
|
1987
|
+
if (playbackId) this.storyboardCache.get(playbackId);
|
|
1988
|
+
}
|
|
1989
|
+
_deactivateItem(id) {
|
|
1990
|
+
const p = this.pool.getPlayer(id);
|
|
1991
|
+
if (p) {
|
|
1992
|
+
p.pause();
|
|
1993
|
+
p.style.opacity = "0";
|
|
1994
|
+
p._skRevealedFor = null;
|
|
1995
|
+
}
|
|
1996
|
+
this._stopTimeLoop(id);
|
|
1997
|
+
this._clearOverlay(id);
|
|
1998
|
+
}
|
|
1999
|
+
// --- Time loop (state emission only, no DOM updates) ---
|
|
2000
|
+
_startTimeLoop(id, el, player) {
|
|
2001
|
+
this._stopTimeLoop(id);
|
|
2002
|
+
const tick = () => {
|
|
2003
|
+
if (this._destroyed) return;
|
|
2004
|
+
if (!player || player.paused) {
|
|
2005
|
+
this.rafIds.set(id, requestAnimationFrame(tick));
|
|
2006
|
+
return;
|
|
2007
|
+
}
|
|
2008
|
+
const current = player.currentTime || 0;
|
|
2009
|
+
const duration = player.duration || 0;
|
|
2010
|
+
const buffered = player.buffered?.length > 0 ? player.buffered.end(player.buffered.length - 1) : 0;
|
|
2011
|
+
this._sk.player._pushState({
|
|
2012
|
+
time: { current, duration, buffered },
|
|
2013
|
+
playerState: "playing"
|
|
2014
|
+
});
|
|
2015
|
+
if (this._captionsOn) {
|
|
2016
|
+
const cues = this._captionCues.get(id);
|
|
2017
|
+
if (cues) {
|
|
2018
|
+
const cue = cues.find((c) => current >= c.startTime && current <= c.endTime) || null;
|
|
2019
|
+
const prev = this._sk.player.activeCue;
|
|
2020
|
+
if (cue !== prev) {
|
|
2021
|
+
this._sk.player._pushState({ activeCue: cue });
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
if (player.ended) {
|
|
2026
|
+
player.currentTime = 0;
|
|
2027
|
+
player.play().catch(() => {
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
this.rafIds.set(id, requestAnimationFrame(tick));
|
|
2031
|
+
};
|
|
2032
|
+
this.rafIds.set(id, requestAnimationFrame(tick));
|
|
2033
|
+
}
|
|
2034
|
+
_stopTimeLoop(id) {
|
|
2035
|
+
const r = this.rafIds.get(id);
|
|
2036
|
+
if (r) {
|
|
2037
|
+
cancelAnimationFrame(r);
|
|
2038
|
+
this.rafIds.delete(id);
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
// --- Controls ---
|
|
2042
|
+
_bindControls(item, el) {
|
|
2043
|
+
const tapZone = el.querySelector('[data-ref="tapZone"]');
|
|
2044
|
+
if (tapZone) {
|
|
2045
|
+
tapZone.addEventListener("click", () => {
|
|
2046
|
+
const player = this.pool.getPlayer(item.id);
|
|
2047
|
+
if (!player) return;
|
|
2048
|
+
if (player.paused) {
|
|
2049
|
+
player.play().catch(() => {
|
|
2050
|
+
});
|
|
2051
|
+
this._pushPlayerState({ playerState: "playing" });
|
|
2052
|
+
} else {
|
|
2053
|
+
player.pause();
|
|
2054
|
+
this._pushPlayerState({ playerState: "paused" });
|
|
2055
|
+
}
|
|
2056
|
+
});
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
// --- Push state to ShortKitPlayer ---
|
|
2060
|
+
_pushPlayerState(updates = {}) {
|
|
2061
|
+
const player = this.pool.getPlayer(this.activeItemId);
|
|
2062
|
+
const item = this.items[this.activeIndex] || null;
|
|
2063
|
+
const state = {
|
|
2064
|
+
currentItem: item,
|
|
2065
|
+
isMuted: this.isMuted,
|
|
2066
|
+
playbackRate: this.playbackRate,
|
|
2067
|
+
captionsEnabled: this._captionsOn,
|
|
2068
|
+
activeCaptionTrack: null,
|
|
2069
|
+
activeCue: null,
|
|
2070
|
+
prefetchedAheadCount: Math.max(0, this.items.length - this.activeIndex - 1),
|
|
2071
|
+
remainingContentCount: this.items.length - this.activeIndex - 1,
|
|
2072
|
+
...updates
|
|
2073
|
+
};
|
|
2074
|
+
if (player) {
|
|
2075
|
+
state.time = {
|
|
2076
|
+
current: player.currentTime || 0,
|
|
2077
|
+
duration: player.duration || 0,
|
|
2078
|
+
buffered: player.buffered?.length > 0 ? player.buffered.end(player.buffered.length - 1) : 0
|
|
2079
|
+
};
|
|
2080
|
+
if (!updates.playerState) {
|
|
2081
|
+
state.playerState = player.paused ? "paused" : "playing";
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
this._sk.player._pushState(state);
|
|
2085
|
+
}
|
|
2086
|
+
// --- Caption VTT fetch (deduped + cached) ---
|
|
2087
|
+
_fetchCaptionVTT(item) {
|
|
2088
|
+
if (this._captionCues.has(item.id) || this._captionFetches.has(item.id)) return;
|
|
2089
|
+
const track = item.captionTracks[0];
|
|
2090
|
+
if (!track?.url) return;
|
|
2091
|
+
const promise = fetch(track.url).then((res) => res.ok ? res.text() : null).then((text) => {
|
|
2092
|
+
if (text) this._captionCues.set(item.id, parseVTT(text));
|
|
2093
|
+
}).catch(() => {
|
|
2094
|
+
}).finally(() => this._captionFetches.delete(item.id));
|
|
2095
|
+
this._captionFetches.set(item.id, promise);
|
|
2096
|
+
}
|
|
2097
|
+
_prefetchThumbnails(centerIndex) {
|
|
2098
|
+
const start = Math.max(0, centerIndex - 3);
|
|
2099
|
+
const end = Math.min(this.items.length, centerIndex + 4);
|
|
2100
|
+
for (let i = start; i < end; i++) {
|
|
2101
|
+
const url = this.items[i].thumbnailUrl;
|
|
2102
|
+
if (!url || this._prefetchedThumbs.has(url)) continue;
|
|
2103
|
+
this._prefetchedThumbs.add(url);
|
|
2104
|
+
const img = new Image();
|
|
2105
|
+
img.src = url;
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
// --- Custom overlay helpers ---
|
|
2109
|
+
_createOverlayContainer(itemId, cellEl) {
|
|
2110
|
+
const overlayEl = cellEl.querySelector('[data-ref="overlay"]');
|
|
2111
|
+
if (!overlayEl) return;
|
|
2112
|
+
this._overlayContainers.set(itemId, { el: overlayEl, unsub: null });
|
|
2113
|
+
}
|
|
2114
|
+
_invokeOverlay(itemId, item) {
|
|
2115
|
+
if (!this._options?.config?.overlay) return;
|
|
2116
|
+
const entry = this._overlayContainers.get(itemId);
|
|
2117
|
+
if (!entry) return;
|
|
2118
|
+
const player = this._sk.player;
|
|
2119
|
+
const surface = this;
|
|
2120
|
+
const listeners = [];
|
|
2121
|
+
const scopedPlayer = {
|
|
2122
|
+
get isMuted() {
|
|
2123
|
+
return player.isMuted;
|
|
2124
|
+
},
|
|
2125
|
+
get playbackRate() {
|
|
2126
|
+
return player.playbackRate;
|
|
2127
|
+
},
|
|
2128
|
+
get currentTime() {
|
|
2129
|
+
return player.time.current;
|
|
2130
|
+
},
|
|
2131
|
+
get duration() {
|
|
2132
|
+
return player.time.duration;
|
|
2133
|
+
},
|
|
2134
|
+
get captionsEnabled() {
|
|
2135
|
+
return player.captionsEnabled;
|
|
2136
|
+
},
|
|
2137
|
+
on(event, fn) {
|
|
2138
|
+
player.on(event, fn);
|
|
2139
|
+
listeners.push({ event, fn });
|
|
2140
|
+
},
|
|
2141
|
+
off(event, fn) {
|
|
2142
|
+
player.off(event, fn);
|
|
2143
|
+
const idx = listeners.findIndex((l) => l.event === event && l.fn === fn);
|
|
2144
|
+
if (idx >= 0) listeners.splice(idx, 1);
|
|
2145
|
+
},
|
|
2146
|
+
play() {
|
|
2147
|
+
surface.play();
|
|
2148
|
+
},
|
|
2149
|
+
pause() {
|
|
2150
|
+
surface.pause();
|
|
2151
|
+
},
|
|
2152
|
+
seek(time) {
|
|
2153
|
+
surface.seek(time);
|
|
2154
|
+
},
|
|
2155
|
+
setMuted(muted) {
|
|
2156
|
+
surface.setMuted(muted);
|
|
2157
|
+
},
|
|
2158
|
+
setPlaybackRate(rate) {
|
|
2159
|
+
surface.setPlaybackRate(rate);
|
|
2160
|
+
},
|
|
2161
|
+
setCaptionsEnabled(enabled) {
|
|
2162
|
+
surface.setCaptionsEnabled(enabled);
|
|
2163
|
+
},
|
|
2164
|
+
selectCaptionTrack(track) {
|
|
2165
|
+
surface.selectCaptionTrack(track);
|
|
2166
|
+
},
|
|
2167
|
+
sendContentSignal(signal) {
|
|
2168
|
+
surface.sendContentSignal(signal);
|
|
2169
|
+
},
|
|
2170
|
+
skipToNext() {
|
|
2171
|
+
surface.skipToNext();
|
|
2172
|
+
},
|
|
2173
|
+
skipToPrevious() {
|
|
2174
|
+
surface.skipToPrevious();
|
|
2175
|
+
},
|
|
2176
|
+
seekThumbnail(time) {
|
|
2177
|
+
return surface.seekThumbnail(time);
|
|
2178
|
+
}
|
|
2179
|
+
};
|
|
2180
|
+
const feed = {
|
|
2181
|
+
dismiss: () => {
|
|
2182
|
+
if (surface._feedDismiss) surface._feedDismiss();
|
|
2183
|
+
},
|
|
2184
|
+
navigateUp: () => surface.navigateUp(),
|
|
2185
|
+
navigateDown: () => surface.navigateDown()
|
|
2186
|
+
};
|
|
2187
|
+
entry.unsub = () => {
|
|
2188
|
+
for (const { event, fn } of listeners) player.off(event, fn);
|
|
2189
|
+
listeners.length = 0;
|
|
2190
|
+
};
|
|
2191
|
+
try {
|
|
2192
|
+
this._options.config.overlay(entry.el, { item, player: scopedPlayer, feed });
|
|
2193
|
+
} catch (e) {
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
_clearOverlay(itemId) {
|
|
2197
|
+
const entry = this._overlayContainers.get(itemId);
|
|
2198
|
+
if (!entry) return;
|
|
2199
|
+
if (entry.unsub) {
|
|
2200
|
+
entry.unsub();
|
|
2201
|
+
entry.unsub = null;
|
|
2202
|
+
}
|
|
2203
|
+
entry.el.innerHTML = "";
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
const MuteOnSvg = '<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>';
|
|
2207
|
+
const MuteOffSvg = '<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>';
|
|
2208
|
+
class SinglePlayer {
|
|
2209
|
+
/**
|
|
2210
|
+
* @param {HTMLElement} containerEl The player container (.skp-player)
|
|
2211
|
+
* @param {import('../core/shortkit.js').ShortKit} shortKit
|
|
2212
|
+
* @param {object} [options]
|
|
2213
|
+
* @param {object} [options.config] PlayerConfig from createPlayerConfig()
|
|
2214
|
+
* @param {number} [options.scale] Visual scale multiplier (default 1.4)
|
|
2215
|
+
* @param {number} [options.padding] Inner padding (default 0)
|
|
2216
|
+
* @param {string} [options.overlayElId] ID of the feed overlay element (default 'skpFeedOverlay')
|
|
2217
|
+
* @param {Function} [options.onTap]
|
|
2218
|
+
*/
|
|
2219
|
+
constructor(containerEl, shortKit, options = {}) {
|
|
2220
|
+
this.container = containerEl;
|
|
2221
|
+
this._sk = shortKit;
|
|
2222
|
+
this._options = options;
|
|
2223
|
+
this._config = options.config || {};
|
|
2224
|
+
this._scale = options.scale ?? 1.4;
|
|
2225
|
+
this._padding = options.padding ?? 0;
|
|
2226
|
+
this._cornerRadius = this._config.cornerRadius ?? 12;
|
|
2227
|
+
this._clickAction = this._config.clickAction || "feed";
|
|
2228
|
+
this._autoplay = this._config.autoplay !== false;
|
|
2229
|
+
this._loop = this._config.loop !== false;
|
|
2230
|
+
this._overlayElId = options.overlayElId || "skpFeedOverlay";
|
|
2231
|
+
this.videoEl = null;
|
|
2232
|
+
this.hlsInstance = null;
|
|
2233
|
+
this.item = null;
|
|
2234
|
+
this.items = [];
|
|
2235
|
+
this._playOverlay = null;
|
|
2236
|
+
this._muteIconEl = null;
|
|
2237
|
+
this._muteFlashTimer = null;
|
|
2238
|
+
this._feedView = null;
|
|
2239
|
+
this._cursor = null;
|
|
2240
|
+
this._hasMore = true;
|
|
2241
|
+
this._destroyed = false;
|
|
2242
|
+
this._overlayEl = null;
|
|
2243
|
+
this._overlayUnsub = null;
|
|
2244
|
+
}
|
|
2245
|
+
// ── Public API ──
|
|
2246
|
+
async init() {
|
|
2247
|
+
const fetchCount = this._clickAction === "feed" ? 10 : 1;
|
|
2248
|
+
const result = await this._sk._apiClient.fetchFeed({ limit: fetchCount });
|
|
2249
|
+
if (!result.items.length) return;
|
|
2250
|
+
this._cursor = result.nextCursor;
|
|
2251
|
+
this._hasMore = result.hasMore;
|
|
2252
|
+
this.items = result.items.map((item) => this._normaliseItem(item));
|
|
2253
|
+
this.item = this.items[0];
|
|
2254
|
+
this._build();
|
|
2255
|
+
}
|
|
2256
|
+
/** Initialize with pre-fetched items instead of calling the API. */
|
|
2257
|
+
initWithItems(items) {
|
|
2258
|
+
this.items = items;
|
|
2259
|
+
this.item = items[0];
|
|
2260
|
+
this._build();
|
|
2261
|
+
}
|
|
2262
|
+
/** Update scale live (dashboard use). */
|
|
2263
|
+
setScale(scale) {
|
|
2264
|
+
this._scale = scale;
|
|
2265
|
+
this.container.style.maxWidth = `${Math.round(240 * scale)}px`;
|
|
2266
|
+
}
|
|
2267
|
+
/** Update padding live (dashboard use). */
|
|
2268
|
+
setPadding(padding) {
|
|
2269
|
+
this._padding = padding;
|
|
2270
|
+
this.container.style.setProperty("--skp-padding", `${padding}px`);
|
|
2271
|
+
}
|
|
2272
|
+
/** Update corner radius live (dashboard use). */
|
|
2273
|
+
setCornerRadius(radius) {
|
|
2274
|
+
this._cornerRadius = radius;
|
|
2275
|
+
this.container.style.setProperty("--skp-radius", `${radius}px`);
|
|
2276
|
+
this.container.style.borderRadius = `${radius}px`;
|
|
2277
|
+
}
|
|
2278
|
+
/** Update click action live (dashboard use). */
|
|
2279
|
+
setClickAction(action) {
|
|
2280
|
+
this._clickAction = action;
|
|
2281
|
+
}
|
|
2282
|
+
/** Update loop live (dashboard use). */
|
|
2283
|
+
setLoop(loop) {
|
|
2284
|
+
this._loop = loop;
|
|
2285
|
+
if (this.videoEl) this.videoEl.loop = loop;
|
|
2286
|
+
}
|
|
2287
|
+
destroy() {
|
|
2288
|
+
this._destroyed = true;
|
|
2289
|
+
clearTimeout(this._muteFlashTimer);
|
|
2290
|
+
this._clearOverlay();
|
|
2291
|
+
if (this.hlsInstance) {
|
|
2292
|
+
this.hlsInstance.destroy();
|
|
2293
|
+
this.hlsInstance = null;
|
|
2294
|
+
}
|
|
2295
|
+
this.container.innerHTML = "";
|
|
2296
|
+
this.videoEl = null;
|
|
2297
|
+
this.item = null;
|
|
2298
|
+
this._playOverlay = null;
|
|
2299
|
+
this._muteIconEl = null;
|
|
2300
|
+
this._overlayEl = null;
|
|
2301
|
+
if (this._feedView) {
|
|
2302
|
+
this._feedView = null;
|
|
2303
|
+
}
|
|
2304
|
+
this._sk._unregisterSurface(this);
|
|
2305
|
+
}
|
|
2306
|
+
// ── Internal: item normalisation ──
|
|
2307
|
+
_normaliseItem(item) {
|
|
2308
|
+
return {
|
|
2309
|
+
id: item.id,
|
|
2310
|
+
streamingUrl: item.streamingUrl,
|
|
2311
|
+
thumbnailUrl: item.thumbnailUrl,
|
|
2312
|
+
title: item.title || "",
|
|
2313
|
+
description: item.description || "",
|
|
2314
|
+
author: item.author || "",
|
|
2315
|
+
section: item.customMetadata?.category?.toUpperCase() || item.customMetadata?.section?.toUpperCase() || "",
|
|
2316
|
+
articleUrl: item.articleUrl || null,
|
|
2317
|
+
duration: item.duration || 0,
|
|
2318
|
+
captionTracks: item.captionTracks || []
|
|
2319
|
+
};
|
|
2320
|
+
}
|
|
2321
|
+
// ── Internal: DOM build ──
|
|
2322
|
+
_build() {
|
|
2323
|
+
this.container.innerHTML = "";
|
|
2324
|
+
this.container.style.setProperty("--skp-radius", `${this._cornerRadius}px`);
|
|
2325
|
+
this.container.style.maxWidth = `${Math.round(240 * this._scale)}px`;
|
|
2326
|
+
this.container.style.setProperty("--skp-padding", `${this._padding}px`);
|
|
2327
|
+
this.container.style.borderRadius = `${this._cornerRadius}px`;
|
|
2328
|
+
this.videoEl = document.createElement("video");
|
|
2329
|
+
this.videoEl.setAttribute("playsinline", "");
|
|
2330
|
+
this.videoEl.setAttribute("webkit-playsinline", "");
|
|
2331
|
+
this.videoEl.muted = true;
|
|
2332
|
+
this.videoEl.loop = this._loop;
|
|
2333
|
+
if (this.item.thumbnailUrl) this.videoEl.poster = this.item.thumbnailUrl;
|
|
2334
|
+
this.container.appendChild(this.videoEl);
|
|
2335
|
+
const meta = document.createElement("div");
|
|
2336
|
+
meta.className = "skp-meta";
|
|
2337
|
+
meta.innerHTML = `
|
|
2338
|
+
${this.item.section ? `<div class="skp-meta-section">${this.item.section}</div>` : ""}
|
|
2339
|
+
<div class="skp-meta-title">${this.item.title}</div>
|
|
2340
|
+
`;
|
|
2341
|
+
this.container.appendChild(meta);
|
|
2342
|
+
this._muteIconEl = document.createElement("div");
|
|
2343
|
+
this._muteIconEl.className = "skp-mute-icon";
|
|
2344
|
+
this._muteIconEl.innerHTML = MuteOnSvg;
|
|
2345
|
+
this.container.appendChild(this._muteIconEl);
|
|
2346
|
+
this._playOverlay = document.createElement("div");
|
|
2347
|
+
this._playOverlay.className = "skp-play-overlay";
|
|
2348
|
+
if (this._autoplay) this._playOverlay.classList.add("skp-hidden");
|
|
2349
|
+
this._playOverlay.innerHTML = '<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>';
|
|
2350
|
+
this._playOverlay.addEventListener("click", (e) => {
|
|
2351
|
+
e.stopPropagation();
|
|
2352
|
+
this._playOverlay.classList.add("skp-hidden");
|
|
2353
|
+
this.videoEl.play().catch(() => {
|
|
2354
|
+
});
|
|
2355
|
+
});
|
|
2356
|
+
this.container.appendChild(this._playOverlay);
|
|
2357
|
+
if (this._config.overlay) {
|
|
2358
|
+
this._overlayEl = document.createElement("div");
|
|
2359
|
+
this._overlayEl.style.cssText = "position:absolute;inset:0;z-index:6;pointer-events:auto;";
|
|
2360
|
+
this._overlayEl.dataset.ref = "customOverlay";
|
|
2361
|
+
this.container.appendChild(this._overlayEl);
|
|
2362
|
+
}
|
|
2363
|
+
this.container.addEventListener("click", (e) => {
|
|
2364
|
+
e.preventDefault();
|
|
2365
|
+
e.stopPropagation();
|
|
2366
|
+
this._handleClick(e);
|
|
2367
|
+
});
|
|
2368
|
+
this._attachStream();
|
|
2369
|
+
if (this._config.overlay && this._overlayEl) {
|
|
2370
|
+
this._invokeOverlay();
|
|
2371
|
+
}
|
|
2372
|
+
if (this._clickAction === "feed") {
|
|
2373
|
+
const overlayEl = document.getElementById(this._overlayElId);
|
|
2374
|
+
if (overlayEl) {
|
|
2375
|
+
this._feedView = new PlayerFeedView(overlayEl, this._sk, this._overlayElId);
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
/** Invoke the developer overlay callback with a scoped subscriber. */
|
|
2380
|
+
_invokeOverlay() {
|
|
2381
|
+
this._clearOverlay();
|
|
2382
|
+
if (!this._overlayEl || !this._config.overlay || !this.item) return;
|
|
2383
|
+
const player = this._sk.player;
|
|
2384
|
+
const listeners = [];
|
|
2385
|
+
const scopedPlayer = {
|
|
2386
|
+
on(event, fn) {
|
|
2387
|
+
player.on(event, fn);
|
|
2388
|
+
listeners.push({ event, fn });
|
|
2389
|
+
}
|
|
2390
|
+
};
|
|
2391
|
+
this._overlayUnsub = () => {
|
|
2392
|
+
for (const { event, fn } of listeners) player.off(event, fn);
|
|
2393
|
+
listeners.length = 0;
|
|
2394
|
+
};
|
|
2395
|
+
try {
|
|
2396
|
+
this._config.overlay(this._overlayEl, { item: this.item, player: scopedPlayer });
|
|
2397
|
+
} catch (e) {
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
/** Clear overlay content and unsubscribe tracked listeners. */
|
|
2401
|
+
_clearOverlay() {
|
|
2402
|
+
if (this._overlayUnsub) {
|
|
2403
|
+
this._overlayUnsub();
|
|
2404
|
+
this._overlayUnsub = null;
|
|
2405
|
+
}
|
|
2406
|
+
if (this._overlayEl) {
|
|
2407
|
+
this._overlayEl.innerHTML = "";
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
_attachStream() {
|
|
2411
|
+
const url = this.item.streamingUrl;
|
|
2412
|
+
if (!url) return;
|
|
2413
|
+
const Hls2 = typeof window !== "undefined" && window.Hls;
|
|
2414
|
+
if (url.includes(".m3u8") && Hls2 && Hls2.isSupported()) {
|
|
2415
|
+
const hls = new Hls2({
|
|
2416
|
+
startLevel: -1,
|
|
2417
|
+
capLevelToPlayerSize: true,
|
|
2418
|
+
maxBufferLength: 8,
|
|
2419
|
+
maxMaxBufferLength: 15
|
|
2420
|
+
});
|
|
2421
|
+
hls.loadSource(url);
|
|
2422
|
+
hls.attachMedia(this.videoEl);
|
|
2423
|
+
hls.on(Hls2.Events.MANIFEST_PARSED, () => {
|
|
2424
|
+
if (this._autoplay) this.videoEl.play().catch(() => {
|
|
2425
|
+
});
|
|
2426
|
+
});
|
|
2427
|
+
hls.on(Hls2.Events.ERROR, (_, data) => {
|
|
2428
|
+
if (data.fatal) {
|
|
2429
|
+
switch (data.type) {
|
|
2430
|
+
case Hls2.ErrorTypes.NETWORK_ERROR:
|
|
2431
|
+
hls.startLoad();
|
|
2432
|
+
break;
|
|
2433
|
+
default:
|
|
2434
|
+
hls.destroy();
|
|
2435
|
+
break;
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
});
|
|
2439
|
+
this.hlsInstance = hls;
|
|
2440
|
+
} else {
|
|
2441
|
+
this.videoEl.src = url;
|
|
2442
|
+
if (this._autoplay) this.videoEl.play().catch(() => {
|
|
2443
|
+
});
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
_handleClick(_e) {
|
|
2447
|
+
if (this._playOverlay && !this._playOverlay.classList.contains("skp-hidden")) return;
|
|
2448
|
+
const action = this._clickAction;
|
|
2449
|
+
if (action === "none") return;
|
|
2450
|
+
if (action === "article") {
|
|
2451
|
+
if (this.item?.articleUrl) window.open(this.item.articleUrl, "_blank", "noopener");
|
|
2452
|
+
return;
|
|
2453
|
+
}
|
|
2454
|
+
if (action === "mute") {
|
|
2455
|
+
this.videoEl.muted = !this.videoEl.muted;
|
|
2456
|
+
this._updateMuteIcon();
|
|
2457
|
+
this.container.classList.add("skp-mute-visible");
|
|
2458
|
+
clearTimeout(this._muteFlashTimer);
|
|
2459
|
+
this._muteFlashTimer = setTimeout(() => {
|
|
2460
|
+
this.container.classList.remove("skp-mute-visible");
|
|
2461
|
+
}, 1500);
|
|
2462
|
+
return;
|
|
2463
|
+
}
|
|
2464
|
+
if (action === "feed") {
|
|
2465
|
+
this._openFeed();
|
|
2466
|
+
return;
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
_openFeed() {
|
|
2470
|
+
if (!this._feedView || this._feedView._isOpen) return;
|
|
2471
|
+
if (this.videoEl) this.videoEl.pause();
|
|
2472
|
+
this._feedView.open({
|
|
2473
|
+
sourceEl: this.container,
|
|
2474
|
+
video: this.videoEl,
|
|
2475
|
+
hlsInstance: this.hlsInstance,
|
|
2476
|
+
item: this.item,
|
|
2477
|
+
startIndex: 0,
|
|
2478
|
+
items: [...this.items],
|
|
2479
|
+
cursor: this._cursor,
|
|
2480
|
+
hasMore: this._hasMore,
|
|
2481
|
+
onClose: (action, data) => {
|
|
2482
|
+
if (action === "getSourceRect") {
|
|
2483
|
+
return this.container.getBoundingClientRect();
|
|
2484
|
+
}
|
|
2485
|
+
if (action === "closed") {
|
|
2486
|
+
let video = data?.transferVideo;
|
|
2487
|
+
let hls = data?.transferHls;
|
|
2488
|
+
if (!video && this._feedView.feedManager) {
|
|
2489
|
+
const ejected = this._feedView.feedManager.pool.ejectPlayer(this.item.id);
|
|
2490
|
+
if (ejected) {
|
|
2491
|
+
video = ejected.video;
|
|
2492
|
+
hls = ejected.hls;
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
if (video) {
|
|
2496
|
+
this.videoEl = video;
|
|
2497
|
+
this.hlsInstance = hls;
|
|
2498
|
+
this.videoEl.loop = this._loop;
|
|
2499
|
+
this.videoEl.muted = true;
|
|
2500
|
+
if (!this.container.contains(this.videoEl)) {
|
|
2501
|
+
this.container.insertBefore(this.videoEl, this.container.firstChild);
|
|
2502
|
+
}
|
|
2503
|
+
this.videoEl.play().catch(() => {
|
|
2504
|
+
});
|
|
2505
|
+
} else {
|
|
2506
|
+
this._rebuildVideo();
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
});
|
|
2511
|
+
}
|
|
2512
|
+
_rebuildVideo() {
|
|
2513
|
+
this.videoEl = document.createElement("video");
|
|
2514
|
+
this.videoEl.setAttribute("playsinline", "");
|
|
2515
|
+
this.videoEl.setAttribute("webkit-playsinline", "");
|
|
2516
|
+
this.videoEl.muted = true;
|
|
2517
|
+
this.videoEl.loop = this._loop;
|
|
2518
|
+
if (this.item.thumbnailUrl) this.videoEl.poster = this.item.thumbnailUrl;
|
|
2519
|
+
this.container.insertBefore(this.videoEl, this.container.firstChild);
|
|
2520
|
+
this.hlsInstance = null;
|
|
2521
|
+
this._attachStream();
|
|
2522
|
+
}
|
|
2523
|
+
_updateMuteIcon() {
|
|
2524
|
+
if (!this._muteIconEl) return;
|
|
2525
|
+
this._muteIconEl.innerHTML = this.videoEl.muted ? MuteOnSvg : MuteOffSvg;
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
class PlayerFeedView {
|
|
2529
|
+
/**
|
|
2530
|
+
* @param {HTMLElement} overlayEl
|
|
2531
|
+
* @param {import('../core/shortkit.js').ShortKit} shortKit
|
|
2532
|
+
* @param {string} [idPrefix] Element ID prefix (default 'skp')
|
|
2533
|
+
*/
|
|
2534
|
+
constructor(overlayEl, shortKit, idPrefix = "skp") {
|
|
2535
|
+
this.overlayEl = overlayEl;
|
|
2536
|
+
this._sk = shortKit;
|
|
2537
|
+
this._prefix = idPrefix;
|
|
2538
|
+
this.backdropEl = overlayEl.querySelector(".skp-feed-backdrop");
|
|
2539
|
+
this.closeBtnEl = document.getElementById(`${idPrefix}FeedCloseBtn`);
|
|
2540
|
+
this.feedEl = document.getElementById(`${idPrefix}Feed`);
|
|
2541
|
+
this.feedManager = null;
|
|
2542
|
+
this._isOpen = false;
|
|
2543
|
+
this._onClose = null;
|
|
2544
|
+
this._escHandler = null;
|
|
2545
|
+
this._openItemId = null;
|
|
2546
|
+
this.closeBtnEl?.addEventListener("click", () => this.close());
|
|
2547
|
+
}
|
|
2548
|
+
async open({ sourceEl, video, hlsInstance, item, startIndex, items, cursor, hasMore, onClose }) {
|
|
2549
|
+
if (this._isOpen) return;
|
|
2550
|
+
this._isOpen = true;
|
|
2551
|
+
this._onClose = onClose;
|
|
2552
|
+
this._openItemId = item.id;
|
|
2553
|
+
const sourceRect = sourceEl.getBoundingClientRect();
|
|
2554
|
+
const sourceRadius = parseFloat(getComputedStyle(sourceEl).borderRadius) || 12;
|
|
2555
|
+
this.overlayEl.classList.add("skp-active");
|
|
2556
|
+
const feedPool = new PlayerPool();
|
|
2557
|
+
const feedShortKit = Object.create(this._sk);
|
|
2558
|
+
feedShortKit._playerPool = feedPool;
|
|
2559
|
+
feedShortKit._storyboardCache = this._sk._storyboardCache;
|
|
2560
|
+
this.feedManager = new EmbeddedFeedManager(this.feedEl, feedShortKit, {
|
|
2561
|
+
idPrefix: this._prefix
|
|
2562
|
+
});
|
|
2563
|
+
if (cursor) this.feedManager._cursor = cursor;
|
|
2564
|
+
if (hasMore !== void 0) this.feedManager._hasMore = hasMore;
|
|
2565
|
+
await this.feedManager.initWithItems([...items], startIndex, { deferActivation: true });
|
|
2566
|
+
if (video && hlsInstance) {
|
|
2567
|
+
this.feedManager.pool.injectPlayer(item.id, video, hlsInstance, item.streamingUrl);
|
|
2568
|
+
video._skFeedTransfer = true;
|
|
2569
|
+
video.loop = true;
|
|
2570
|
+
}
|
|
2571
|
+
const feedItemEl = this.feedManager.itemEls.get(item.id);
|
|
2572
|
+
if (feedItemEl && video) {
|
|
2573
|
+
const c = feedItemEl.querySelector('[data-ref="videoContainer"]');
|
|
2574
|
+
video.style.opacity = "1";
|
|
2575
|
+
if (!c.contains(video)) c.insertBefore(video, c.firstChild);
|
|
2576
|
+
}
|
|
2577
|
+
const feedEl = this.feedEl;
|
|
2578
|
+
feedEl.getBoundingClientRect();
|
|
2579
|
+
const feedRect = feedEl.getBoundingClientRect();
|
|
2580
|
+
const targetRadius = parseFloat(getComputedStyle(feedEl).borderRadius) || 16;
|
|
2581
|
+
const scaleX = sourceRect.width / feedRect.width;
|
|
2582
|
+
const scaleY = sourceRect.height / feedRect.height;
|
|
2583
|
+
const translateX = sourceRect.left - feedRect.left;
|
|
2584
|
+
const translateY = sourceRect.top - feedRect.top;
|
|
2585
|
+
const startRadius = sourceRadius / Math.min(scaleX, scaleY);
|
|
2586
|
+
feedEl.style.transformOrigin = "0 0";
|
|
2587
|
+
feedEl.style.overflow = "hidden";
|
|
2588
|
+
feedEl.style.scrollSnapType = "none";
|
|
2589
|
+
feedEl.style.borderRadius = `${startRadius}px`;
|
|
2590
|
+
feedEl.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scaleX}, ${scaleY})`;
|
|
2591
|
+
feedEl.getBoundingClientRect();
|
|
2592
|
+
feedEl.classList.add("skp-flip-animating");
|
|
2593
|
+
requestAnimationFrame(() => {
|
|
2594
|
+
feedEl.style.transform = "none";
|
|
2595
|
+
feedEl.style.borderRadius = `${targetRadius}px`;
|
|
2596
|
+
});
|
|
2597
|
+
await this._waitForTransitionEnd(feedEl, 450);
|
|
2598
|
+
feedEl.classList.remove("skp-flip-animating");
|
|
2599
|
+
feedEl.style.transformOrigin = "";
|
|
2600
|
+
feedEl.style.overflow = "";
|
|
2601
|
+
feedEl.style.scrollSnapType = "";
|
|
2602
|
+
feedEl.style.transform = "";
|
|
2603
|
+
feedEl.style.borderRadius = "";
|
|
2604
|
+
this.feedManager.startObserver();
|
|
2605
|
+
this.feedManager._activateItem(item.id);
|
|
2606
|
+
this.overlayEl.classList.add("skp-feed-ready");
|
|
2607
|
+
this._escHandler = (e) => {
|
|
2608
|
+
if (e.key === "Escape") this.close();
|
|
2609
|
+
};
|
|
2610
|
+
document.addEventListener("keydown", this._escHandler);
|
|
2611
|
+
}
|
|
2612
|
+
async close() {
|
|
2613
|
+
if (!this._isOpen) return;
|
|
2614
|
+
this._isOpen = false;
|
|
2615
|
+
const feedEl = this.feedEl;
|
|
2616
|
+
const feedWrapper = feedEl.parentNode;
|
|
2617
|
+
const feedRect = feedEl.getBoundingClientRect();
|
|
2618
|
+
const activeItemId = this.feedManager?.activeItemId;
|
|
2619
|
+
let transferBack = false, transferVideo = null, transferHls = null;
|
|
2620
|
+
if (activeItemId === this._openItemId) {
|
|
2621
|
+
const ejected = this.feedManager?.pool.ejectPlayer(activeItemId);
|
|
2622
|
+
if (ejected) {
|
|
2623
|
+
transferBack = true;
|
|
2624
|
+
transferVideo = ejected.video;
|
|
2625
|
+
transferHls = ejected.hls;
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
let targetRect = null;
|
|
2629
|
+
if (this._onClose) targetRect = this._onClose("getSourceRect");
|
|
2630
|
+
if (feedRect && targetRect) {
|
|
2631
|
+
if (this.feedManager?.activeItemId && !transferBack) {
|
|
2632
|
+
this.feedManager._deactivateItem(this.feedManager.activeItemId);
|
|
2633
|
+
}
|
|
2634
|
+
feedEl.style.position = "fixed";
|
|
2635
|
+
feedEl.style.left = `${feedRect.left}px`;
|
|
2636
|
+
feedEl.style.top = `${feedRect.top}px`;
|
|
2637
|
+
feedEl.style.width = `${feedRect.width}px`;
|
|
2638
|
+
feedEl.style.height = `${feedRect.height}px`;
|
|
2639
|
+
feedEl.style.zIndex = "9999";
|
|
2640
|
+
feedEl.style.margin = "0";
|
|
2641
|
+
feedEl.style.flex = "none";
|
|
2642
|
+
feedEl.style.aspectRatio = "unset";
|
|
2643
|
+
feedEl.style.maxHeight = "none";
|
|
2644
|
+
document.body.appendChild(feedEl);
|
|
2645
|
+
this.overlayEl.classList.remove("skp-active", "skp-feed-ready");
|
|
2646
|
+
const scaleX = targetRect.width / feedRect.width;
|
|
2647
|
+
const scaleY = targetRect.height / feedRect.height;
|
|
2648
|
+
const translateX = targetRect.left - feedRect.left;
|
|
2649
|
+
const translateY = targetRect.top - feedRect.top;
|
|
2650
|
+
const sourceRadius = parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--skp-radius").trim()) || 12;
|
|
2651
|
+
const endRadius = sourceRadius / Math.min(scaleX, scaleY);
|
|
2652
|
+
feedEl.style.transformOrigin = "0 0";
|
|
2653
|
+
feedEl.style.overflow = "hidden";
|
|
2654
|
+
feedEl.style.scrollSnapType = "none";
|
|
2655
|
+
feedEl.getBoundingClientRect();
|
|
2656
|
+
feedEl.classList.add("skp-flip-animating");
|
|
2657
|
+
requestAnimationFrame(() => {
|
|
2658
|
+
feedEl.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scaleX}, ${scaleY})`;
|
|
2659
|
+
feedEl.style.borderRadius = `${endRadius}px`;
|
|
2660
|
+
});
|
|
2661
|
+
await this._waitForTransitionEnd(feedEl, 450);
|
|
2662
|
+
feedEl.style.visibility = "hidden";
|
|
2663
|
+
feedEl.classList.remove("skp-flip-animating");
|
|
2664
|
+
["position", "left", "top", "width", "height", "zIndex", "margin", "flex", "aspectRatio", "maxHeight", "transformOrigin", "overflow", "scrollSnapType", "transform", "borderRadius"].forEach((p) => feedEl.style[p] = "");
|
|
2665
|
+
const sidebarEl = feedWrapper.querySelector(".sk-sidebar");
|
|
2666
|
+
feedWrapper.insertBefore(feedEl, sidebarEl);
|
|
2667
|
+
feedEl.style.visibility = "";
|
|
2668
|
+
} else {
|
|
2669
|
+
this.overlayEl.classList.remove("skp-active", "skp-feed-ready");
|
|
2670
|
+
}
|
|
2671
|
+
if (this._onClose) {
|
|
2672
|
+
this._onClose("closed", {
|
|
2673
|
+
transferVideo: transferBack ? transferVideo : null,
|
|
2674
|
+
transferHls: transferBack ? transferHls : null,
|
|
2675
|
+
transferItemId: transferBack ? this._openItemId : null
|
|
2676
|
+
});
|
|
2677
|
+
this._onClose = null;
|
|
2678
|
+
}
|
|
2679
|
+
if (this.feedManager) {
|
|
2680
|
+
this.feedManager.destroy();
|
|
2681
|
+
this.feedManager = null;
|
|
2682
|
+
}
|
|
2683
|
+
if (this._escHandler) {
|
|
2684
|
+
document.removeEventListener("keydown", this._escHandler);
|
|
2685
|
+
this._escHandler = null;
|
|
2686
|
+
}
|
|
2687
|
+
this.overlayEl.classList.remove("skp-active", "skp-feed-ready", "skp-closing");
|
|
2688
|
+
}
|
|
2689
|
+
_waitForTransitionEnd(el, maxMs) {
|
|
2690
|
+
return new Promise((resolve) => {
|
|
2691
|
+
const timeout = setTimeout(resolve, maxMs);
|
|
2692
|
+
el.addEventListener("transitionend", function handler(e) {
|
|
2693
|
+
if (e.target === el) {
|
|
2694
|
+
clearTimeout(timeout);
|
|
2695
|
+
el.removeEventListener("transitionend", handler);
|
|
2696
|
+
resolve();
|
|
2697
|
+
}
|
|
2698
|
+
});
|
|
2699
|
+
});
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
const _isPreview = typeof window !== "undefined" && new URLSearchParams(window.location.search).has("preview");
|
|
2703
|
+
class FeedView {
|
|
2704
|
+
/**
|
|
2705
|
+
* @param {HTMLElement} overlayEl The .skw-feed-overlay element
|
|
2706
|
+
* @param {import('../core/shortkit.js').ShortKit} shortKit
|
|
2707
|
+
* @param {object} [options]
|
|
2708
|
+
* @param {object} [options.storyboardCache]
|
|
2709
|
+
*/
|
|
2710
|
+
constructor(overlayEl, shortKit, options = {}) {
|
|
2711
|
+
this.overlayEl = overlayEl;
|
|
2712
|
+
this._sk = shortKit;
|
|
2713
|
+
this.backdropEl = overlayEl.querySelector(".skw-feed-backdrop");
|
|
2714
|
+
this.feedEl = overlayEl.querySelector(".sk-feed") || document.getElementById("skwFeed");
|
|
2715
|
+
this.storyboardCache = options.storyboardCache || shortKit._storyboardCache;
|
|
2716
|
+
this.feedManager = null;
|
|
2717
|
+
this._isOpen = false;
|
|
2718
|
+
this._onClose = null;
|
|
2719
|
+
this._escHandler = null;
|
|
2720
|
+
this._openSlotIndex = -1;
|
|
2721
|
+
this._openItemId = null;
|
|
2722
|
+
}
|
|
2723
|
+
async open({ slotEl, video, hlsInstance, item, startIndex, items, cursor, hasMore, onClose, overlayConfig }) {
|
|
2724
|
+
if (this._isOpen) return;
|
|
2725
|
+
this._isOpen = true;
|
|
2726
|
+
this._onClose = onClose;
|
|
2727
|
+
this._openSlotIndex = startIndex;
|
|
2728
|
+
this._openItemId = item.id;
|
|
2729
|
+
const slotRect = slotEl.getBoundingClientRect();
|
|
2730
|
+
const slotRadius = parseFloat(getComputedStyle(slotEl).borderRadius) || 12;
|
|
2731
|
+
if (_isPreview) {
|
|
2732
|
+
const widgetEl = document.getElementById("widget");
|
|
2733
|
+
if (widgetEl) widgetEl.style.visibility = "hidden";
|
|
2734
|
+
}
|
|
2735
|
+
this.overlayEl.classList.add("skw-active");
|
|
2736
|
+
this.feedManager = new EmbeddedFeedManager(this.feedEl, this._sk, {
|
|
2737
|
+
idPrefix: "skw",
|
|
2738
|
+
config: overlayConfig ? { overlay: overlayConfig } : void 0
|
|
2739
|
+
});
|
|
2740
|
+
this.feedManager._feedDismiss = () => this.close();
|
|
2741
|
+
this.feedManager._cursor = cursor || null;
|
|
2742
|
+
this.feedManager._hasMore = hasMore !== false;
|
|
2743
|
+
await this.feedManager.initWithItems([...items], startIndex, { deferActivation: true });
|
|
2744
|
+
if (video && hlsInstance) {
|
|
2745
|
+
this.feedManager.pool.injectPlayer(item.id, video, hlsInstance, item.streamingUrl);
|
|
2746
|
+
video._skFeedTransfer = true;
|
|
2747
|
+
video.loop = true;
|
|
2748
|
+
}
|
|
2749
|
+
const feedItemEl = this.feedManager.itemEls.get(item.id);
|
|
2750
|
+
if (feedItemEl && video) {
|
|
2751
|
+
const container = feedItemEl.querySelector('[data-ref="videoContainer"]');
|
|
2752
|
+
video.style.opacity = "1";
|
|
2753
|
+
if (!container.contains(video)) container.insertBefore(video, container.firstChild);
|
|
2754
|
+
}
|
|
2755
|
+
const feedEl = this.feedEl;
|
|
2756
|
+
feedEl.getBoundingClientRect();
|
|
2757
|
+
const feedRect = feedEl.getBoundingClientRect();
|
|
2758
|
+
const targetRadius = parseFloat(getComputedStyle(feedEl).borderRadius) || 16;
|
|
2759
|
+
const scaleX = slotRect.width / feedRect.width;
|
|
2760
|
+
const scaleY = slotRect.height / feedRect.height;
|
|
2761
|
+
const translateX = slotRect.left - feedRect.left;
|
|
2762
|
+
const translateY = slotRect.top - feedRect.top;
|
|
2763
|
+
const startRadius = slotRadius / Math.min(scaleX, scaleY);
|
|
2764
|
+
feedEl.style.transformOrigin = "0 0";
|
|
2765
|
+
feedEl.style.overflow = "hidden";
|
|
2766
|
+
feedEl.style.scrollSnapType = "none";
|
|
2767
|
+
feedEl.style.borderRadius = `${startRadius}px`;
|
|
2768
|
+
feedEl.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scaleX}, ${scaleY})`;
|
|
2769
|
+
feedEl.getBoundingClientRect();
|
|
2770
|
+
feedEl.classList.add("skw-flip-animating");
|
|
2771
|
+
requestAnimationFrame(() => {
|
|
2772
|
+
feedEl.style.transform = "none";
|
|
2773
|
+
feedEl.style.borderRadius = `${targetRadius}px`;
|
|
2774
|
+
});
|
|
2775
|
+
await this._waitForTransitionEnd(feedEl, 450);
|
|
2776
|
+
feedEl.classList.remove("skw-flip-animating");
|
|
2777
|
+
feedEl.style.transformOrigin = "";
|
|
2778
|
+
feedEl.style.overflow = "";
|
|
2779
|
+
feedEl.style.scrollSnapType = "";
|
|
2780
|
+
feedEl.style.transform = "";
|
|
2781
|
+
feedEl.style.borderRadius = "";
|
|
2782
|
+
this.feedManager.startObserver();
|
|
2783
|
+
this.feedManager._activateItem(item.id);
|
|
2784
|
+
this.overlayEl.classList.add("skw-feed-ready");
|
|
2785
|
+
this._escHandler = (e) => {
|
|
2786
|
+
if (e.key === "Escape") this.close();
|
|
2787
|
+
};
|
|
2788
|
+
document.addEventListener("keydown", this._escHandler);
|
|
2789
|
+
}
|
|
2790
|
+
dismiss() {
|
|
2791
|
+
this.close();
|
|
2792
|
+
}
|
|
2793
|
+
async close() {
|
|
2794
|
+
if (!this._isOpen) return;
|
|
2795
|
+
this._isOpen = false;
|
|
2796
|
+
if (_isPreview) {
|
|
2797
|
+
const widgetEl = document.getElementById("widget");
|
|
2798
|
+
if (widgetEl) widgetEl.style.visibility = "visible";
|
|
2799
|
+
}
|
|
2800
|
+
const feedEl = this.feedEl;
|
|
2801
|
+
const feedWrapper = feedEl.parentNode;
|
|
2802
|
+
const feedRect = feedEl.getBoundingClientRect();
|
|
2803
|
+
const activeItemId = this.feedManager?.activeItemId;
|
|
2804
|
+
const canTransfer = activeItemId === this._openItemId;
|
|
2805
|
+
let targetSlotRect = null;
|
|
2806
|
+
if (this._onClose) targetSlotRect = this._onClose("getSlotRect");
|
|
2807
|
+
if (feedRect && targetSlotRect) {
|
|
2808
|
+
if (this.feedManager?.activeItemId && !canTransfer) {
|
|
2809
|
+
this.feedManager._deactivateItem(this.feedManager.activeItemId);
|
|
2810
|
+
}
|
|
2811
|
+
feedEl.style.position = "fixed";
|
|
2812
|
+
feedEl.style.left = `${feedRect.left}px`;
|
|
2813
|
+
feedEl.style.top = `${feedRect.top}px`;
|
|
2814
|
+
feedEl.style.width = `${feedRect.width}px`;
|
|
2815
|
+
feedEl.style.height = `${feedRect.height}px`;
|
|
2816
|
+
feedEl.style.zIndex = String(getComputedStyle(this.overlayEl).zIndex || 9999);
|
|
2817
|
+
feedEl.style.margin = "0";
|
|
2818
|
+
feedEl.style.flex = "none";
|
|
2819
|
+
feedEl.style.aspectRatio = "unset";
|
|
2820
|
+
feedEl.style.maxHeight = "none";
|
|
2821
|
+
document.body.appendChild(feedEl);
|
|
2822
|
+
this.overlayEl.classList.remove("skw-active", "skw-feed-ready");
|
|
2823
|
+
const slotRadius = parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--skw-radius").trim()) || 12;
|
|
2824
|
+
const scaleX = targetSlotRect.width / feedRect.width;
|
|
2825
|
+
const scaleY = targetSlotRect.height / feedRect.height;
|
|
2826
|
+
const translateX = targetSlotRect.left - feedRect.left;
|
|
2827
|
+
const translateY = targetSlotRect.top - feedRect.top;
|
|
2828
|
+
const endRadius = slotRadius / Math.min(scaleX, scaleY);
|
|
2829
|
+
feedEl.style.transformOrigin = "0 0";
|
|
2830
|
+
feedEl.style.overflow = "hidden";
|
|
2831
|
+
feedEl.style.scrollSnapType = "none";
|
|
2832
|
+
feedEl.getBoundingClientRect();
|
|
2833
|
+
feedEl.classList.add("skw-flip-animating");
|
|
2834
|
+
requestAnimationFrame(() => {
|
|
2835
|
+
feedEl.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scaleX}, ${scaleY})`;
|
|
2836
|
+
feedEl.style.borderRadius = `${endRadius}px`;
|
|
2837
|
+
});
|
|
2838
|
+
await this._waitForTransitionEnd(feedEl, 450);
|
|
2839
|
+
let transferVideo = null;
|
|
2840
|
+
let transferHls = null;
|
|
2841
|
+
if (canTransfer) {
|
|
2842
|
+
const ejected = this.feedManager?.pool.ejectPlayer(this._openItemId);
|
|
2843
|
+
if (ejected) {
|
|
2844
|
+
transferVideo = ejected.video;
|
|
2845
|
+
transferHls = ejected.hls;
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
feedEl.style.visibility = "hidden";
|
|
2849
|
+
feedEl.classList.remove("skw-flip-animating");
|
|
2850
|
+
feedEl.style.position = "";
|
|
2851
|
+
feedEl.style.left = "";
|
|
2852
|
+
feedEl.style.top = "";
|
|
2853
|
+
feedEl.style.width = "";
|
|
2854
|
+
feedEl.style.height = "";
|
|
2855
|
+
feedEl.style.zIndex = "";
|
|
2856
|
+
feedEl.style.margin = "";
|
|
2857
|
+
feedEl.style.flex = "";
|
|
2858
|
+
feedEl.style.aspectRatio = "";
|
|
2859
|
+
feedEl.style.maxHeight = "";
|
|
2860
|
+
feedEl.style.transformOrigin = "";
|
|
2861
|
+
feedEl.style.overflow = "";
|
|
2862
|
+
feedEl.style.scrollSnapType = "";
|
|
2863
|
+
feedEl.style.transform = "";
|
|
2864
|
+
feedEl.style.borderRadius = "";
|
|
2865
|
+
feedWrapper.insertBefore(feedEl, feedWrapper.firstChild);
|
|
2866
|
+
feedEl.style.visibility = "";
|
|
2867
|
+
if (this._onClose) {
|
|
2868
|
+
this._onClose("closed", {
|
|
2869
|
+
transferVideo,
|
|
2870
|
+
transferHls,
|
|
2871
|
+
transferItemId: transferVideo ? this._openItemId : null
|
|
2872
|
+
});
|
|
2873
|
+
this._onClose = null;
|
|
2874
|
+
}
|
|
2875
|
+
} else {
|
|
2876
|
+
this.overlayEl.classList.remove("skw-active", "skw-feed-ready");
|
|
2877
|
+
if (this._onClose) {
|
|
2878
|
+
let transferVideo = null;
|
|
2879
|
+
let transferHls = null;
|
|
2880
|
+
if (canTransfer) {
|
|
2881
|
+
const ejected = this.feedManager?.pool.ejectPlayer(this._openItemId);
|
|
2882
|
+
if (ejected) {
|
|
2883
|
+
transferVideo = ejected.video;
|
|
2884
|
+
transferHls = ejected.hls;
|
|
2885
|
+
}
|
|
2886
|
+
}
|
|
2887
|
+
this._onClose("closed", {
|
|
2888
|
+
transferVideo,
|
|
2889
|
+
transferHls,
|
|
2890
|
+
transferItemId: transferVideo ? this._openItemId : null
|
|
2891
|
+
});
|
|
2892
|
+
this._onClose = null;
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
if (this.feedManager) {
|
|
2896
|
+
if (this.feedManager._keyHandler) document.removeEventListener("keydown", this.feedManager._keyHandler);
|
|
2897
|
+
if (this.feedManager._speedCloseHandler) document.removeEventListener("click", this.feedManager._speedCloseHandler);
|
|
2898
|
+
if (this.feedManager._visHandler) document.removeEventListener("visibilitychange", this.feedManager._visHandler);
|
|
2899
|
+
this.feedManager.destroy();
|
|
2900
|
+
this.feedManager = null;
|
|
2901
|
+
}
|
|
2902
|
+
if (this._escHandler) {
|
|
2903
|
+
document.removeEventListener("keydown", this._escHandler);
|
|
2904
|
+
this._escHandler = null;
|
|
2905
|
+
}
|
|
2906
|
+
this.overlayEl.classList.remove("skw-active", "skw-feed-ready", "skw-closing");
|
|
2907
|
+
}
|
|
2908
|
+
_waitForTransitionEnd(el, maxMs) {
|
|
2909
|
+
return new Promise((resolve) => {
|
|
2910
|
+
const timeout = setTimeout(resolve, maxMs);
|
|
2911
|
+
el.addEventListener("transitionend", function handler(e) {
|
|
2912
|
+
if (e.target === el) {
|
|
2913
|
+
clearTimeout(timeout);
|
|
2914
|
+
el.removeEventListener("transitionend", handler);
|
|
2915
|
+
resolve();
|
|
2916
|
+
}
|
|
2917
|
+
});
|
|
2918
|
+
});
|
|
2919
|
+
}
|
|
2920
|
+
}
|
|
2921
|
+
const _isIOSSafari = typeof navigator !== "undefined" && (/iPad|iPhone|iPod/.test(navigator.userAgent) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
|
|
2922
|
+
class WidgetPlayerPool {
|
|
2923
|
+
static PRELOAD_HLS_CONFIG = { startLevel: 0, capLevelToPlayerSize: true, maxBufferLength: 2, maxMaxBufferLength: 4 };
|
|
2924
|
+
static ACTIVE_HLS_CONFIG = { startLevel: -1, capLevelToPlayerSize: true, maxBufferLength: 8, maxMaxBufferLength: 15 };
|
|
2925
|
+
constructor(poolSize = 2, { Hls: HlsClass } = {}) {
|
|
2926
|
+
this._Hls = HlsClass ?? (typeof Hls !== "undefined" ? Hls : null);
|
|
2927
|
+
this.players = Array.from({ length: poolSize }, () => this._createPlayer());
|
|
2928
|
+
this.assignments = /* @__PURE__ */ new Map();
|
|
2929
|
+
this.hlsInstances = /* @__PURE__ */ new Map();
|
|
2930
|
+
}
|
|
2931
|
+
_createPlayer() {
|
|
2932
|
+
const video = document.createElement("video");
|
|
2933
|
+
video.playsInline = true;
|
|
2934
|
+
video.preload = _isIOSSafari ? "metadata" : "auto";
|
|
2935
|
+
video.loop = false;
|
|
2936
|
+
video.muted = true;
|
|
2937
|
+
video.setAttribute("webkit-playsinline", "");
|
|
2938
|
+
return video;
|
|
2939
|
+
}
|
|
2940
|
+
acquire(itemId) {
|
|
2941
|
+
if (this.assignments.has(itemId)) return this.assignments.get(itemId);
|
|
2942
|
+
const assigned = new Set(this.assignments.values());
|
|
2943
|
+
let player = this.players.find((p) => !assigned.has(p));
|
|
2944
|
+
if (!player) {
|
|
2945
|
+
const oldest = this.assignments.keys().next().value;
|
|
2946
|
+
player = this.assignments.get(oldest);
|
|
2947
|
+
player.pause();
|
|
2948
|
+
this._destroyHls(oldest);
|
|
2949
|
+
player.removeAttribute("src");
|
|
2950
|
+
player.load();
|
|
2951
|
+
if (player.parentNode) player.parentNode.removeChild(player);
|
|
2952
|
+
this.assignments.delete(oldest);
|
|
2953
|
+
}
|
|
2954
|
+
this.assignments.set(itemId, player);
|
|
2955
|
+
return player;
|
|
2956
|
+
}
|
|
2957
|
+
attachStream(itemId, streamingUrl, { isActive = false } = {}) {
|
|
2958
|
+
const player = this.assignments.get(itemId);
|
|
2959
|
+
if (!player) return;
|
|
2960
|
+
if (player._skCurrentUrl === streamingUrl) return;
|
|
2961
|
+
this._destroyHls(itemId);
|
|
2962
|
+
const HlsClass = this._Hls;
|
|
2963
|
+
if (streamingUrl.includes(".m3u8") && HlsClass && HlsClass.isSupported()) {
|
|
2964
|
+
const hlsConfig = isActive ? WidgetPlayerPool.ACTIVE_HLS_CONFIG : WidgetPlayerPool.PRELOAD_HLS_CONFIG;
|
|
2965
|
+
const hls = new HlsClass(hlsConfig);
|
|
2966
|
+
hls.loadSource(streamingUrl);
|
|
2967
|
+
hls.attachMedia(player);
|
|
2968
|
+
hls.on(HlsClass.Events.ERROR, (_event, data) => {
|
|
2969
|
+
if (!data.fatal) return;
|
|
2970
|
+
switch (data.type) {
|
|
2971
|
+
case HlsClass.ErrorTypes.MEDIA_ERROR:
|
|
2972
|
+
hls.recoverMediaError();
|
|
2973
|
+
break;
|
|
2974
|
+
case HlsClass.ErrorTypes.NETWORK_ERROR:
|
|
2975
|
+
setTimeout(() => {
|
|
2976
|
+
if (!hls.destroyed) hls.startLoad();
|
|
2977
|
+
}, 2e3);
|
|
2978
|
+
break;
|
|
2979
|
+
default:
|
|
2980
|
+
hls.destroy();
|
|
2981
|
+
break;
|
|
2982
|
+
}
|
|
2983
|
+
});
|
|
2984
|
+
this.hlsInstances.set(itemId, hls);
|
|
2985
|
+
} else {
|
|
2986
|
+
player.src = streamingUrl;
|
|
2987
|
+
}
|
|
2988
|
+
player._skCurrentUrl = streamingUrl;
|
|
2989
|
+
}
|
|
2990
|
+
promoteToActive(itemId) {
|
|
2991
|
+
const hls = this.hlsInstances.get(itemId);
|
|
2992
|
+
if (hls) {
|
|
2993
|
+
hls.config.maxBufferLength = WidgetPlayerPool.ACTIVE_HLS_CONFIG.maxBufferLength;
|
|
2994
|
+
hls.config.maxMaxBufferLength = WidgetPlayerPool.ACTIVE_HLS_CONFIG.maxMaxBufferLength;
|
|
2995
|
+
hls.autoLevelCapping = -1;
|
|
2996
|
+
hls.nextAutoLevel = -1;
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
_destroyHls(itemId) {
|
|
3000
|
+
const hls = this.hlsInstances.get(itemId);
|
|
3001
|
+
if (hls) {
|
|
3002
|
+
hls.destroy();
|
|
3003
|
+
this.hlsInstances.delete(itemId);
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
release(itemId) {
|
|
3007
|
+
const player = this.assignments.get(itemId);
|
|
3008
|
+
if (player) {
|
|
3009
|
+
player.pause();
|
|
3010
|
+
this._destroyHls(itemId);
|
|
3011
|
+
player._skCurrentUrl = null;
|
|
3012
|
+
if (player.parentNode) player.parentNode.removeChild(player);
|
|
3013
|
+
this.assignments.delete(itemId);
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
getPlayer(itemId) {
|
|
3017
|
+
return this.assignments.get(itemId) || null;
|
|
3018
|
+
}
|
|
3019
|
+
/** Remove video+HLS from pool WITHOUT destroying or pausing. Returns {video, hls} or null. */
|
|
3020
|
+
ejectPlayer(itemId) {
|
|
3021
|
+
const video = this.assignments.get(itemId);
|
|
3022
|
+
const hls = this.hlsInstances.get(itemId);
|
|
3023
|
+
if (!video) return null;
|
|
3024
|
+
if (video.parentNode) video.parentNode.removeChild(video);
|
|
3025
|
+
this.assignments.delete(itemId);
|
|
3026
|
+
this.hlsInstances.delete(itemId);
|
|
3027
|
+
const idx = this.players.indexOf(video);
|
|
3028
|
+
if (idx >= 0) this.players[idx] = this._createPlayer();
|
|
3029
|
+
return { video, hls };
|
|
3030
|
+
}
|
|
3031
|
+
/** Re-inject a previously ejected video+HLS back into the pool. */
|
|
3032
|
+
injectPlayer(itemId, video, hls, streamingUrl) {
|
|
3033
|
+
if (this.assignments.has(itemId)) this.release(itemId);
|
|
3034
|
+
this.assignments.set(itemId, video);
|
|
3035
|
+
if (hls) this.hlsInstances.set(itemId, hls);
|
|
3036
|
+
video._skCurrentUrl = streamingUrl || null;
|
|
3037
|
+
if (!this.players.includes(video)) this.players.push(video);
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
class WidgetManager {
|
|
3041
|
+
/**
|
|
3042
|
+
* @param {HTMLElement} containerEl The widget container element
|
|
3043
|
+
* @param {import('../core/shortkit.js').ShortKit} shortKit
|
|
3044
|
+
* @param {object} [options]
|
|
3045
|
+
* @param {object} [options.config] Widget configuration (from createWidgetConfig)
|
|
3046
|
+
*/
|
|
3047
|
+
constructor(containerEl, shortKit, options = {}) {
|
|
3048
|
+
this._sk = shortKit;
|
|
3049
|
+
const cfg = options.config || {};
|
|
3050
|
+
this.config = {
|
|
3051
|
+
count: cfg.cardCount || 3,
|
|
3052
|
+
visibleCount: cfg.visibleCount || cfg.cardCount || 3,
|
|
3053
|
+
scrollable: cfg.scrollable || false,
|
|
3054
|
+
autoRotate: cfg.autoRotate !== void 0 ? cfg.autoRotate : true,
|
|
3055
|
+
rotationPause: cfg.rotationInterval || 500,
|
|
3056
|
+
highlight: cfg.highlight === true,
|
|
3057
|
+
slotScale: cfg.slotScale || 1.4,
|
|
3058
|
+
slotGap: cfg.cardSpacing ?? 10,
|
|
3059
|
+
slotRadius: cfg.cornerRadius ?? 12,
|
|
3060
|
+
clickAction: cfg.clickAction || "feed",
|
|
3061
|
+
autoplay: cfg.autoplay !== false,
|
|
3062
|
+
// Custom overlay callback (accepts either key name)
|
|
3063
|
+
overlay: cfg.overlay || cfg.cardOverlay || null
|
|
3064
|
+
};
|
|
3065
|
+
this.container = containerEl;
|
|
3066
|
+
this.trackEl = containerEl.querySelector(".skw-track") || containerEl.querySelector('[data-ref="track"]');
|
|
3067
|
+
this.dotsEl = containerEl.querySelector(".skw-dots") || containerEl.querySelector('[data-ref="dots"]');
|
|
3068
|
+
this.pool = new WidgetPlayerPool(this.config.count + 1);
|
|
3069
|
+
this.storyboardCache = shortKit._storyboardCache;
|
|
3070
|
+
this.items = [];
|
|
3071
|
+
this.slotEls = [];
|
|
3072
|
+
this.activeIndex = -1;
|
|
3073
|
+
this.rotationTimer = null;
|
|
3074
|
+
this.isModalOpen = false;
|
|
3075
|
+
this._slotWidth = 200;
|
|
3076
|
+
this._hasActivated = false;
|
|
3077
|
+
this._wasPlaying = false;
|
|
3078
|
+
this._isHovering = false;
|
|
3079
|
+
this._feedOverlayEl = null;
|
|
3080
|
+
this.feedView = null;
|
|
3081
|
+
this._slotOverlays = /* @__PURE__ */ new Map();
|
|
3082
|
+
}
|
|
3083
|
+
async init() {
|
|
3084
|
+
const fetchCount = this.config.scrollable ? this.config.count * 3 : this.config.count;
|
|
3085
|
+
const result = await this._sk._apiClient.fetchFeed({ limit: Math.max(fetchCount, 10) });
|
|
3086
|
+
this.items = this._mapItems(result.items);
|
|
3087
|
+
this._cursor = result.nextCursor;
|
|
3088
|
+
this._hasMore = result.hasMore;
|
|
3089
|
+
this._computeSlotWidth();
|
|
3090
|
+
this._buildSlots();
|
|
3091
|
+
this._buildDots();
|
|
3092
|
+
this.container.style.setProperty("--skw-slot-gap", `${this.config.slotGap}px`);
|
|
3093
|
+
this.container.style.setProperty("--skw-radius", `${this.config.slotRadius}px`);
|
|
3094
|
+
this.container.style.setProperty("--skw-slot-scale", this.config.slotScale);
|
|
3095
|
+
if (this.config.highlight) {
|
|
3096
|
+
this.container.classList.add("skw-highlight");
|
|
3097
|
+
}
|
|
3098
|
+
const needsScroll = this.config.scrollable || this.config.count > this.config.visibleCount;
|
|
3099
|
+
if (needsScroll) {
|
|
3100
|
+
this.container.classList.add("skw-scrollable");
|
|
3101
|
+
this._setupCarousel();
|
|
3102
|
+
}
|
|
3103
|
+
this._setupResize();
|
|
3104
|
+
this._setupVisibility();
|
|
3105
|
+
this._prewarmSlots();
|
|
3106
|
+
if (this.items.length > 0) {
|
|
3107
|
+
if (this.config.autoplay) {
|
|
3108
|
+
this._activateSlot(0);
|
|
3109
|
+
} else {
|
|
3110
|
+
this.activeIndex = 0;
|
|
3111
|
+
this._hasActivated = true;
|
|
3112
|
+
this.slotEls[0]?.classList.add("skw-active");
|
|
3113
|
+
const dots = this.dotsEl.querySelectorAll(".skw-dot");
|
|
3114
|
+
if (dots[0]) dots[0].classList.add("skw-dot-active");
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
_mapItems(rawItems) {
|
|
3119
|
+
return rawItems.filter((item) => !item.type || item.type === "content").map((item) => ({
|
|
3120
|
+
id: item.id,
|
|
3121
|
+
streamingUrl: item.streamingUrl,
|
|
3122
|
+
thumbnailUrl: item.thumbnailUrl,
|
|
3123
|
+
title: item.title || "",
|
|
3124
|
+
description: item.description || "",
|
|
3125
|
+
author: item.author || "",
|
|
3126
|
+
section: item.customMetadata?.category?.toUpperCase() || item.customMetadata?.section?.toUpperCase() || "",
|
|
3127
|
+
articleUrl: item.articleUrl || item.publisherUrl || null,
|
|
3128
|
+
duration: item.duration || 0,
|
|
3129
|
+
captionTracks: item.captionTracks || []
|
|
3130
|
+
}));
|
|
3131
|
+
}
|
|
3132
|
+
/** Pre-attach HLS streams and prime decoders for all visible slots. */
|
|
3133
|
+
_prewarmSlots() {
|
|
3134
|
+
const count = Math.min(this.slotEls.length, this.items.length);
|
|
3135
|
+
const warmCount = _isIOSSafari ? Math.min(count, 2) : count;
|
|
3136
|
+
for (let i = 0; i < warmCount; i++) {
|
|
3137
|
+
const item = this.items[i];
|
|
3138
|
+
const slotEl = this.slotEls[i];
|
|
3139
|
+
const player = this.pool.acquire(item.id);
|
|
3140
|
+
this.pool.attachStream(item.id, item.streamingUrl);
|
|
3141
|
+
const thumbContainer = slotEl.querySelector(".skw-slot-thumb");
|
|
3142
|
+
player.style.opacity = "0";
|
|
3143
|
+
if (!thumbContainer.contains(player)) {
|
|
3144
|
+
thumbContainer.appendChild(player);
|
|
3145
|
+
}
|
|
3146
|
+
player.muted = true;
|
|
3147
|
+
player.currentTime = 0;
|
|
3148
|
+
if (this.config.autoplay) {
|
|
3149
|
+
if (_isIOSSafari && i !== 0) {
|
|
3150
|
+
player.preload = "auto";
|
|
3151
|
+
} else {
|
|
3152
|
+
const p = player.play();
|
|
3153
|
+
if (p) p.then(() => {
|
|
3154
|
+
if (i !== 0) player.pause();
|
|
3155
|
+
}).catch(() => {
|
|
3156
|
+
});
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
_computeSlotWidth() {
|
|
3162
|
+
const trackWidth = this.trackEl.clientWidth || this.container.clientWidth;
|
|
3163
|
+
const trackStyle = getComputedStyle(this.trackEl);
|
|
3164
|
+
const padding = parseFloat(trackStyle.paddingLeft || 0) + parseFloat(trackStyle.paddingRight || 0);
|
|
3165
|
+
const gapValue = this.config.slotGap;
|
|
3166
|
+
const scale = this.config.slotScale;
|
|
3167
|
+
const totalGap = this.config.count > 1 ? (this.config.count - 1) * gapValue : 0;
|
|
3168
|
+
this._slotWidth = Math.max(100, (trackWidth - padding - totalGap) / (this.config.count * scale));
|
|
3169
|
+
}
|
|
3170
|
+
_buildSlots() {
|
|
3171
|
+
this.trackEl.innerHTML = "";
|
|
3172
|
+
this.slotEls = [];
|
|
3173
|
+
const visibleCount = this.config.scrollable ? this.items.length : Math.min(this.config.count, this.items.length);
|
|
3174
|
+
for (let i = 0; i < visibleCount; i++) {
|
|
3175
|
+
const item = this.items[i];
|
|
3176
|
+
const slot = this._createSlotEl(item, i);
|
|
3177
|
+
this.trackEl.appendChild(slot);
|
|
3178
|
+
this.slotEls.push(slot);
|
|
3179
|
+
}
|
|
3180
|
+
}
|
|
3181
|
+
_createSlotEl(item, index) {
|
|
3182
|
+
const slot = document.createElement("div");
|
|
3183
|
+
slot.className = "skw-slot";
|
|
3184
|
+
slot.dataset.index = index;
|
|
3185
|
+
slot.style.setProperty("--skw-slot-width", `${this._slotWidth}px`);
|
|
3186
|
+
const thumbStyle = item.thumbnailUrl ? `background-image: url('${item.thumbnailUrl}');` : "";
|
|
3187
|
+
slot.innerHTML = `<div class="skw-slot-thumb" style="${thumbStyle}"></div>`;
|
|
3188
|
+
const overlayEl = document.createElement("div");
|
|
3189
|
+
overlayEl.className = "skw-slot-overlay";
|
|
3190
|
+
overlayEl.dataset.ref = "customOverlay";
|
|
3191
|
+
slot.appendChild(overlayEl);
|
|
3192
|
+
this._slotOverlays.set(index, { el: overlayEl, unsub: null });
|
|
3193
|
+
slot.addEventListener("click", () => this._handleSlotClick(index));
|
|
3194
|
+
slot.addEventListener("mouseenter", () => {
|
|
3195
|
+
if (index === this.activeIndex) return;
|
|
3196
|
+
this._isHovering = true;
|
|
3197
|
+
clearTimeout(this.rotationTimer);
|
|
3198
|
+
this._activateSlot(index, { scroll: false });
|
|
3199
|
+
});
|
|
3200
|
+
slot.addEventListener("mouseleave", () => {
|
|
3201
|
+
this._isHovering = false;
|
|
3202
|
+
if (this.config.autoRotate && !this.isModalOpen) {
|
|
3203
|
+
const player = this.pool.getPlayer(this.items[this.activeIndex]?.id);
|
|
3204
|
+
if (player && player.ended) {
|
|
3205
|
+
this._onPreviewEnded();
|
|
3206
|
+
}
|
|
3207
|
+
}
|
|
3208
|
+
});
|
|
3209
|
+
return slot;
|
|
3210
|
+
}
|
|
3211
|
+
_buildDots() {
|
|
3212
|
+
this.dotsEl.innerHTML = "";
|
|
3213
|
+
const dotCount = this.config.scrollable ? this.items.length : Math.min(this.config.count, this.items.length);
|
|
3214
|
+
for (let i = 0; i < dotCount; i++) {
|
|
3215
|
+
const dot = document.createElement("button");
|
|
3216
|
+
dot.className = "skw-dot";
|
|
3217
|
+
dot.setAttribute("aria-label", `Go to video ${i + 1}`);
|
|
3218
|
+
dot.addEventListener("click", (e) => {
|
|
3219
|
+
e.stopPropagation();
|
|
3220
|
+
clearTimeout(this.rotationTimer);
|
|
3221
|
+
this._activateSlot(i);
|
|
3222
|
+
});
|
|
3223
|
+
this.dotsEl.appendChild(dot);
|
|
3224
|
+
}
|
|
3225
|
+
}
|
|
3226
|
+
_activateSlot(index, { scroll = true } = {}) {
|
|
3227
|
+
if (index === this.activeIndex && this._hasActivated) return;
|
|
3228
|
+
if (this._hasActivated) {
|
|
3229
|
+
this._deactivateSlot(this.activeIndex);
|
|
3230
|
+
}
|
|
3231
|
+
this.activeIndex = index;
|
|
3232
|
+
this._hasActivated = true;
|
|
3233
|
+
const item = this.items[index];
|
|
3234
|
+
const slotEl = this.slotEls[index];
|
|
3235
|
+
this.slotEls.forEach((el, i) => el.classList.toggle("skw-active", i === index));
|
|
3236
|
+
const dots = this.dotsEl.querySelectorAll(".skw-dot");
|
|
3237
|
+
dots.forEach((dot, i) => dot.classList.toggle("skw-dot-active", i === index));
|
|
3238
|
+
const player = this.pool.acquire(item.id);
|
|
3239
|
+
const isTransferBack = !!player._skFeedTransfer;
|
|
3240
|
+
delete player._skFeedTransfer;
|
|
3241
|
+
player.loop = false;
|
|
3242
|
+
player.muted = true;
|
|
3243
|
+
if (!isTransferBack && (player.ended || player.currentTime === 0)) {
|
|
3244
|
+
player.currentTime = 0;
|
|
3245
|
+
}
|
|
3246
|
+
if (!isTransferBack) player.style.opacity = "0";
|
|
3247
|
+
const thumbContainer = slotEl.querySelector(".skw-slot-thumb");
|
|
3248
|
+
if (!thumbContainer.contains(player)) {
|
|
3249
|
+
thumbContainer.appendChild(player);
|
|
3250
|
+
}
|
|
3251
|
+
this.pool.attachStream(item.id, item.streamingUrl, { isActive: true });
|
|
3252
|
+
const REVEAL_EVENTS = ["loadeddata", "seeked", "canplay", "timeupdate"];
|
|
3253
|
+
const reveal = () => {
|
|
3254
|
+
if (player._skwRevealed === item.id) return;
|
|
3255
|
+
if (player.readyState < 2) return;
|
|
3256
|
+
player._skwRevealed = item.id;
|
|
3257
|
+
player.style.opacity = "1";
|
|
3258
|
+
REVEAL_EVENTS.forEach((e) => player.removeEventListener(e, reveal));
|
|
3259
|
+
};
|
|
3260
|
+
if (isTransferBack || player.readyState >= 2 && !player.seeking && player._skCurrentUrl === item.streamingUrl) {
|
|
3261
|
+
player._skwRevealed = item.id;
|
|
3262
|
+
player.style.opacity = "1";
|
|
3263
|
+
} else {
|
|
3264
|
+
REVEAL_EVENTS.forEach((e) => player.addEventListener(e, reveal));
|
|
3265
|
+
}
|
|
3266
|
+
player.onended = () => this._onPreviewEnded();
|
|
3267
|
+
player.play().catch(() => {
|
|
3268
|
+
});
|
|
3269
|
+
if (this.config.scrollable && scroll) {
|
|
3270
|
+
slotEl.scrollIntoView({ behavior: "smooth", inline: "center", block: "nearest" });
|
|
3271
|
+
}
|
|
3272
|
+
if (this.config.overlay) {
|
|
3273
|
+
this._invokeSlotOverlay(index, item);
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
_deactivateSlot(index) {
|
|
3277
|
+
const item = this.items[index];
|
|
3278
|
+
if (!item) return;
|
|
3279
|
+
const player = this.pool.getPlayer(item.id);
|
|
3280
|
+
if (player) {
|
|
3281
|
+
player.pause();
|
|
3282
|
+
player.onended = null;
|
|
3283
|
+
player.style.opacity = "0";
|
|
3284
|
+
player._skwRevealed = null;
|
|
3285
|
+
}
|
|
3286
|
+
this._clearSlotOverlay(index);
|
|
3287
|
+
}
|
|
3288
|
+
/** Invoke the developer overlay callback for a widget slot. */
|
|
3289
|
+
_invokeSlotOverlay(index, item) {
|
|
3290
|
+
const entry = this._slotOverlays.get(index);
|
|
3291
|
+
if (!entry) return;
|
|
3292
|
+
if (!this.config.overlay) return;
|
|
3293
|
+
this._clearSlotOverlay(index);
|
|
3294
|
+
const player = this._sk.player;
|
|
3295
|
+
const listeners = [];
|
|
3296
|
+
const scopedPlayer = {
|
|
3297
|
+
get isMuted() {
|
|
3298
|
+
return player.isMuted;
|
|
3299
|
+
},
|
|
3300
|
+
get playbackRate() {
|
|
3301
|
+
return player.playbackRate;
|
|
3302
|
+
},
|
|
3303
|
+
get currentTime() {
|
|
3304
|
+
return player.time.current;
|
|
3305
|
+
},
|
|
3306
|
+
get duration() {
|
|
3307
|
+
return player.time.duration;
|
|
3308
|
+
},
|
|
3309
|
+
get captionsEnabled() {
|
|
3310
|
+
return player.captionsEnabled;
|
|
3311
|
+
},
|
|
3312
|
+
on(event, fn) {
|
|
3313
|
+
player.on(event, fn);
|
|
3314
|
+
listeners.push({ event, fn });
|
|
3315
|
+
},
|
|
3316
|
+
off(event, fn) {
|
|
3317
|
+
player.off(event, fn);
|
|
3318
|
+
const idx = listeners.findIndex((l) => l.event === event && l.fn === fn);
|
|
3319
|
+
if (idx >= 0) listeners.splice(idx, 1);
|
|
3320
|
+
}
|
|
3321
|
+
};
|
|
3322
|
+
entry.unsub = () => {
|
|
3323
|
+
for (const { event, fn } of listeners) player.off(event, fn);
|
|
3324
|
+
listeners.length = 0;
|
|
3325
|
+
};
|
|
3326
|
+
try {
|
|
3327
|
+
this.config.overlay(entry.el, { item, player: scopedPlayer });
|
|
3328
|
+
} catch (e) {
|
|
3329
|
+
}
|
|
3330
|
+
}
|
|
3331
|
+
/** Clear a slot's overlay content and unsubscribe tracked listeners. */
|
|
3332
|
+
_clearSlotOverlay(index) {
|
|
3333
|
+
const entry = this._slotOverlays.get(index);
|
|
3334
|
+
if (!entry) return;
|
|
3335
|
+
if (entry.unsub) {
|
|
3336
|
+
entry.unsub();
|
|
3337
|
+
entry.unsub = null;
|
|
3338
|
+
}
|
|
3339
|
+
entry.el.innerHTML = "";
|
|
3340
|
+
}
|
|
3341
|
+
_onPreviewEnded() {
|
|
3342
|
+
if (!this.config.autoRotate || this.isModalOpen || this._isHovering) return;
|
|
3343
|
+
this.rotationTimer = setTimeout(() => {
|
|
3344
|
+
this._advanceRotation();
|
|
3345
|
+
}, this.config.rotationPause);
|
|
3346
|
+
}
|
|
3347
|
+
_advanceRotation() {
|
|
3348
|
+
const nextIndex = (this.activeIndex + 1) % this.slotEls.length;
|
|
3349
|
+
this._activateSlot(nextIndex);
|
|
3350
|
+
}
|
|
3351
|
+
_setupCarousel() {
|
|
3352
|
+
let scrollTimer;
|
|
3353
|
+
this.trackEl.addEventListener("scroll", () => {
|
|
3354
|
+
clearTimeout(scrollTimer);
|
|
3355
|
+
scrollTimer = setTimeout(() => this._onCarouselScrollEnd(), 100);
|
|
3356
|
+
}, { passive: true });
|
|
3357
|
+
}
|
|
3358
|
+
_onCarouselScrollEnd() {
|
|
3359
|
+
if (this._isHovering) return;
|
|
3360
|
+
const trackRect = this.trackEl.getBoundingClientRect();
|
|
3361
|
+
const centerX = trackRect.left + trackRect.width / 2;
|
|
3362
|
+
let closestIndex = 0;
|
|
3363
|
+
let closestDist = Infinity;
|
|
3364
|
+
this.slotEls.forEach((el, i) => {
|
|
3365
|
+
const rect = el.getBoundingClientRect();
|
|
3366
|
+
const slotCenter = rect.left + rect.width / 2;
|
|
3367
|
+
const dist = Math.abs(slotCenter - centerX);
|
|
3368
|
+
if (dist < closestDist) {
|
|
3369
|
+
closestDist = dist;
|
|
3370
|
+
closestIndex = i;
|
|
3371
|
+
}
|
|
3372
|
+
});
|
|
3373
|
+
if (closestIndex !== this.activeIndex) {
|
|
3374
|
+
clearTimeout(this.rotationTimer);
|
|
3375
|
+
this._activateSlot(closestIndex);
|
|
3376
|
+
}
|
|
3377
|
+
if (closestIndex >= this.items.length - 3) {
|
|
3378
|
+
this._loadMore();
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
async _loadMore() {
|
|
3382
|
+
if (this._loadingMore || !this._hasMore) return;
|
|
3383
|
+
this._loadingMore = true;
|
|
3384
|
+
try {
|
|
3385
|
+
const result = await this._sk._apiClient.fetchFeed({
|
|
3386
|
+
limit: 10,
|
|
3387
|
+
cursor: this._cursor
|
|
3388
|
+
});
|
|
3389
|
+
this._cursor = result.nextCursor;
|
|
3390
|
+
this._hasMore = result.hasMore;
|
|
3391
|
+
const newItems = this._mapItems(result.items);
|
|
3392
|
+
for (const item of newItems) {
|
|
3393
|
+
if (this.items.some((existing) => existing.id === item.id)) continue;
|
|
3394
|
+
this.items.push(item);
|
|
3395
|
+
const i = this.items.length - 1;
|
|
3396
|
+
const slot = this._createSlotEl(item, i);
|
|
3397
|
+
this.trackEl.appendChild(slot);
|
|
3398
|
+
this.slotEls.push(slot);
|
|
3399
|
+
const dot = document.createElement("button");
|
|
3400
|
+
dot.className = "skw-dot";
|
|
3401
|
+
dot.setAttribute("aria-label", `Go to video ${i + 1}`);
|
|
3402
|
+
dot.addEventListener("click", (e) => {
|
|
3403
|
+
e.stopPropagation();
|
|
3404
|
+
clearTimeout(this.rotationTimer);
|
|
3405
|
+
this._activateSlot(i);
|
|
3406
|
+
});
|
|
3407
|
+
this.dotsEl.appendChild(dot);
|
|
3408
|
+
}
|
|
3409
|
+
} finally {
|
|
3410
|
+
this._loadingMore = false;
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
_setupResize() {
|
|
3414
|
+
this._resizeObserver = new ResizeObserver(() => {
|
|
3415
|
+
this._computeSlotWidth();
|
|
3416
|
+
this.slotEls.forEach((el) => {
|
|
3417
|
+
el.style.setProperty("--skw-slot-width", `${this._slotWidth}px`);
|
|
3418
|
+
});
|
|
3419
|
+
});
|
|
3420
|
+
this._resizeObserver.observe(this.container);
|
|
3421
|
+
}
|
|
3422
|
+
_setupVisibility() {
|
|
3423
|
+
this._visHandler = () => {
|
|
3424
|
+
if (document.hidden) {
|
|
3425
|
+
clearTimeout(this.rotationTimer);
|
|
3426
|
+
const item = this.items[this.activeIndex];
|
|
3427
|
+
if (item) {
|
|
3428
|
+
const player = this.pool.getPlayer(item.id);
|
|
3429
|
+
if (player && !player.paused) {
|
|
3430
|
+
this._wasPlaying = true;
|
|
3431
|
+
player.pause();
|
|
3432
|
+
}
|
|
3433
|
+
}
|
|
3434
|
+
} else {
|
|
3435
|
+
if (this._wasPlaying && !this.isModalOpen) {
|
|
3436
|
+
const item = this.items[this.activeIndex];
|
|
3437
|
+
if (item) {
|
|
3438
|
+
const player = this.pool.getPlayer(item.id);
|
|
3439
|
+
if (player) player.play().catch(() => {
|
|
3440
|
+
});
|
|
3441
|
+
}
|
|
3442
|
+
this._wasPlaying = false;
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
};
|
|
3446
|
+
document.addEventListener("visibilitychange", this._visHandler);
|
|
3447
|
+
}
|
|
3448
|
+
destroy() {
|
|
3449
|
+
clearTimeout(this.rotationTimer);
|
|
3450
|
+
if (this._hasActivated) {
|
|
3451
|
+
this._deactivateSlot(this.activeIndex);
|
|
3452
|
+
}
|
|
3453
|
+
for (const [itemId] of this.pool.assignments) {
|
|
3454
|
+
this.pool.release(itemId);
|
|
3455
|
+
}
|
|
3456
|
+
if (this._resizeObserver) {
|
|
3457
|
+
this._resizeObserver.disconnect();
|
|
3458
|
+
this._resizeObserver = null;
|
|
3459
|
+
}
|
|
3460
|
+
if (this._visHandler) {
|
|
3461
|
+
document.removeEventListener("visibilitychange", this._visHandler);
|
|
3462
|
+
this._visHandler = null;
|
|
3463
|
+
}
|
|
3464
|
+
if (this._feedOverlayEl && this._feedOverlayEl.parentNode) {
|
|
3465
|
+
this._feedOverlayEl.parentNode.removeChild(this._feedOverlayEl);
|
|
3466
|
+
this._feedOverlayEl = null;
|
|
3467
|
+
}
|
|
3468
|
+
this.trackEl.innerHTML = "";
|
|
3469
|
+
this.dotsEl.innerHTML = "";
|
|
3470
|
+
this.container.classList.remove("skw-scrollable", "skw-highlight");
|
|
3471
|
+
this.container.removeAttribute("style");
|
|
3472
|
+
this.items = [];
|
|
3473
|
+
this.slotEls = [];
|
|
3474
|
+
this.activeIndex = -1;
|
|
3475
|
+
this._hasActivated = false;
|
|
3476
|
+
}
|
|
3477
|
+
_handleSlotClick(index) {
|
|
3478
|
+
const action = this.config.clickAction;
|
|
3479
|
+
if (action === "none") return;
|
|
3480
|
+
if (action === "article") {
|
|
3481
|
+
const item = this.items[index];
|
|
3482
|
+
if (item?.articleUrl) window.open(item.articleUrl, "_blank", "noopener");
|
|
3483
|
+
return;
|
|
3484
|
+
}
|
|
3485
|
+
if (action === "mute") {
|
|
3486
|
+
const item = this.items[index];
|
|
3487
|
+
const player = this.pool.getPlayer(item?.id);
|
|
3488
|
+
if (player) player.muted = !player.muted;
|
|
3489
|
+
return;
|
|
3490
|
+
}
|
|
3491
|
+
this._openFeedOverlay(index);
|
|
3492
|
+
}
|
|
3493
|
+
_ensureFeedView() {
|
|
3494
|
+
if (this.feedView) return;
|
|
3495
|
+
const overlay = document.createElement("div");
|
|
3496
|
+
overlay.className = "skw-feed-overlay";
|
|
3497
|
+
overlay.innerHTML = `
|
|
3498
|
+
<div class="skw-feed-backdrop"></div>
|
|
3499
|
+
<div class="sk-page">
|
|
3500
|
+
<div class="sk-feed-wrapper">
|
|
3501
|
+
<div class="sk-feed"></div>
|
|
3502
|
+
</div>
|
|
3503
|
+
</div>
|
|
3504
|
+
`;
|
|
3505
|
+
document.body.appendChild(overlay);
|
|
3506
|
+
this._feedOverlayEl = overlay;
|
|
3507
|
+
this.feedView = new FeedView(this._feedOverlayEl, this._sk, {
|
|
3508
|
+
storyboardCache: this.storyboardCache
|
|
3509
|
+
});
|
|
3510
|
+
}
|
|
3511
|
+
_openFeedOverlay(index) {
|
|
3512
|
+
this._ensureFeedView();
|
|
3513
|
+
if (!this.feedView) return;
|
|
3514
|
+
clearTimeout(this.rotationTimer);
|
|
3515
|
+
this.isModalOpen = true;
|
|
3516
|
+
const item = this.items[index];
|
|
3517
|
+
const activeItem = this.items[this.activeIndex];
|
|
3518
|
+
if (activeItem && activeItem.id !== item.id) {
|
|
3519
|
+
const player = this.pool.getPlayer(activeItem.id);
|
|
3520
|
+
if (player) {
|
|
3521
|
+
player.pause();
|
|
3522
|
+
player.onended = null;
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3525
|
+
const clickedPlayer = this.pool.getPlayer(item.id);
|
|
3526
|
+
if (clickedPlayer) clickedPlayer.onended = null;
|
|
3527
|
+
const slotEl = this.slotEls[index];
|
|
3528
|
+
const ejected = this.pool.ejectPlayer(item.id);
|
|
3529
|
+
this.feedView.open({
|
|
3530
|
+
slotEl,
|
|
3531
|
+
video: ejected?.video || null,
|
|
3532
|
+
hlsInstance: ejected?.hls || null,
|
|
3533
|
+
item,
|
|
3534
|
+
overlayConfig: this.config.overlay || null,
|
|
3535
|
+
startIndex: index,
|
|
3536
|
+
items: [...this.items],
|
|
3537
|
+
cursor: this._cursor,
|
|
3538
|
+
hasMore: this._hasMore,
|
|
3539
|
+
onClose: (action, data) => {
|
|
3540
|
+
if (action === "getSlotRect") {
|
|
3541
|
+
if (this.activeIndex !== index) {
|
|
3542
|
+
this._deactivateSlot(this.activeIndex);
|
|
3543
|
+
this.slotEls.forEach((el, i) => el.classList.toggle("skw-active", i === index));
|
|
3544
|
+
const dots = this.dotsEl.querySelectorAll(".skw-dot");
|
|
3545
|
+
dots.forEach((dot, i) => dot.classList.toggle("skw-dot-active", i === index));
|
|
3546
|
+
this.activeIndex = index;
|
|
3547
|
+
}
|
|
3548
|
+
const targetSlot = this.slotEls[index];
|
|
3549
|
+
if (targetSlot) {
|
|
3550
|
+
targetSlot.scrollIntoView({ behavior: "instant", inline: "center", block: "nearest" });
|
|
3551
|
+
return targetSlot.getBoundingClientRect();
|
|
3552
|
+
}
|
|
3553
|
+
return null;
|
|
3554
|
+
}
|
|
3555
|
+
if (action === "closed") {
|
|
3556
|
+
this.isModalOpen = false;
|
|
3557
|
+
let video = data?.transferVideo;
|
|
3558
|
+
let hls = data?.transferHls;
|
|
3559
|
+
if (!video && this.feedView.feedManager) {
|
|
3560
|
+
const ejected2 = this.feedView.feedManager.pool.ejectPlayer(item.id);
|
|
3561
|
+
if (ejected2) {
|
|
3562
|
+
video = ejected2.video;
|
|
3563
|
+
hls = ejected2.hls;
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
if (video) {
|
|
3567
|
+
video.loop = false;
|
|
3568
|
+
video.muted = true;
|
|
3569
|
+
video._skFeedTransfer = true;
|
|
3570
|
+
this.pool.injectPlayer(item.id, video, hls, item.streamingUrl);
|
|
3571
|
+
const slotThumb = this.slotEls[index]?.querySelector(".skw-slot-thumb");
|
|
3572
|
+
if (slotThumb && video) {
|
|
3573
|
+
video.style.opacity = "1";
|
|
3574
|
+
if (!slotThumb.contains(video)) slotThumb.appendChild(video);
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
this._hasActivated = false;
|
|
3578
|
+
this._activateSlot(this.activeIndex);
|
|
3579
|
+
}
|
|
3580
|
+
}
|
|
3581
|
+
});
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
let injected = false;
|
|
3585
|
+
const CSS = `
|
|
3586
|
+
/* ShortKit SDK — functional layout CSS (injected at runtime) */
|
|
3587
|
+
|
|
3588
|
+
/* Feed page layout */
|
|
3589
|
+
.sk-page{display:flex;justify-content:center;align-items:center;height:100%;background:#000;padding:12px 16px;overflow:hidden}
|
|
3590
|
+
.sk-feed-wrapper{display:flex;align-items:center;gap:12px;height:100%;max-height:100%}
|
|
3591
|
+
|
|
3592
|
+
/* Feed container */
|
|
3593
|
+
.sk-feed{position:relative;height:100%;aspect-ratio:9/16;overflow-y:scroll;scroll-snap-type:y mandatory;-webkit-overflow-scrolling:touch;scrollbar-width:none;background:#000;border-radius:16px}
|
|
3594
|
+
.sk-feed::-webkit-scrollbar{display:none}
|
|
3595
|
+
|
|
3596
|
+
/* Feed item */
|
|
3597
|
+
.sk-feed-item{position:relative;height:calc(100% - 6px);width:100%;scroll-snap-align:start;scroll-snap-stop:always;overflow:hidden;background:#0a0a0a;border-radius:16px;margin-bottom:6px}
|
|
3598
|
+
|
|
3599
|
+
/* Video container */
|
|
3600
|
+
.sk-video-container{position:absolute;inset:0;width:100%;height:100%;overflow:hidden;background-size:cover;background-position:center}
|
|
3601
|
+
.sk-video-container video{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;opacity:0;transition:opacity .15s ease}
|
|
3602
|
+
|
|
3603
|
+
/* Tap zone */
|
|
3604
|
+
.sk-tap-zone{position:absolute;inset:0;z-index:3;cursor:pointer}
|
|
3605
|
+
|
|
3606
|
+
/* Overlay container */
|
|
3607
|
+
.sk-overlay{position:absolute;inset:0;z-index:4;pointer-events:none}
|
|
3608
|
+
.sk-overlay>*{pointer-events:auto}
|
|
3609
|
+
|
|
3610
|
+
/* Spinner */
|
|
3611
|
+
.sk-spinner{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:32px;height:32px;border:3px solid rgba(255,255,255,.2);border-top-color:#fff;border-radius:50%;opacity:0;pointer-events:none;z-index:2}
|
|
3612
|
+
.sk-spinner.visible{opacity:1;animation:sk-spin .8s linear infinite}
|
|
3613
|
+
@keyframes sk-spin{to{transform:translate(-50%,-50%) rotate(360deg)}}
|
|
3614
|
+
|
|
3615
|
+
/* Widget slot */
|
|
3616
|
+
.skw-slot{position:relative;overflow:hidden;cursor:pointer}
|
|
3617
|
+
.skw-slot video{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;opacity:0;transition:opacity .2s ease}
|
|
3618
|
+
.skw-slot-thumb{position:absolute;inset:0;background-size:cover;background-position:center}
|
|
3619
|
+
.skw-slot-overlay{position:absolute;inset:0;z-index:4;pointer-events:none}
|
|
3620
|
+
.skw-slot-overlay>*{pointer-events:auto}
|
|
3621
|
+
|
|
3622
|
+
/* FLIP animation */
|
|
3623
|
+
.sk-feed.skw-flip-animating{transition:transform .4s cubic-bezier(.32,.72,0,1),border-radius .4s cubic-bezier(.32,.72,0,1);will-change:transform}
|
|
3624
|
+
|
|
3625
|
+
/* Feed overlay states */
|
|
3626
|
+
.skw-feed-overlay{position:fixed;inset:0;z-index:9999;display:none;flex-direction:column}
|
|
3627
|
+
.skw-feed-overlay.skw-active{display:flex}
|
|
3628
|
+
.skw-feed-overlay .skw-feed-backdrop{position:absolute;inset:0;background:rgba(0,0,0,.5);backdrop-filter:blur(16px);-webkit-backdrop-filter:blur(16px);opacity:0;transition:opacity .35s ease;pointer-events:none}
|
|
3629
|
+
.skw-feed-overlay.skw-active .skw-feed-backdrop{opacity:1}
|
|
3630
|
+
.skw-feed-overlay .sk-page{position:relative;z-index:1;flex:1;pointer-events:none}
|
|
3631
|
+
.skw-feed-overlay.skw-feed-ready .sk-page{pointer-events:auto}
|
|
3632
|
+
.skw-feed-overlay.skw-closing .skw-feed-backdrop{opacity:0;transition:none}
|
|
3633
|
+
`;
|
|
3634
|
+
function injectStyles() {
|
|
3635
|
+
if (injected) return;
|
|
3636
|
+
injected = true;
|
|
3637
|
+
const style = document.createElement("style");
|
|
3638
|
+
style.setAttribute("data-shortkit", "");
|
|
3639
|
+
style.textContent = CSS;
|
|
3640
|
+
(document.head || document.documentElement).appendChild(style);
|
|
3641
|
+
}
|
|
3642
|
+
const ShortKitVersion = "0.3.0";
|
|
3643
|
+
class ShortKit {
|
|
3644
|
+
constructor(options = {}) {
|
|
3645
|
+
injectStyles();
|
|
3646
|
+
if (!options.apiKey) throw new Error("ShortKit: apiKey is required");
|
|
3647
|
+
this._destroyed = false;
|
|
3648
|
+
this._options = options;
|
|
3649
|
+
this._surfaces = /* @__PURE__ */ new Set();
|
|
3650
|
+
this._loadingView = options.loadingView || null;
|
|
3651
|
+
this._clientAppName = options.clientAppName || null;
|
|
3652
|
+
this._clientAppVersion = options.clientAppVersion || null;
|
|
3653
|
+
this._customDimensions = options.customDimensions || null;
|
|
3654
|
+
this._onContentTapped = options.onContentTapped || null;
|
|
3655
|
+
this._onRefreshRequested = options.onRefreshRequested || null;
|
|
3656
|
+
this._onDidFetchContentItems = options.onDidFetchContentItems || null;
|
|
3657
|
+
this._identity = new IdentityManager({
|
|
3658
|
+
userId: options.userId,
|
|
3659
|
+
onResolve: (userId, anonId) => {
|
|
3660
|
+
this._apiClient.resolveIdentity(userId, anonId);
|
|
3661
|
+
}
|
|
3662
|
+
});
|
|
3663
|
+
this._apiClient = new APIClient({
|
|
3664
|
+
baseUrl: options.apiBase || "https://api.shortkit.dev",
|
|
3665
|
+
apiKey: options.apiKey,
|
|
3666
|
+
getEffectiveId: () => this._identity.effectiveId
|
|
3667
|
+
});
|
|
3668
|
+
this.player = new ShortKitPlayer();
|
|
3669
|
+
this._playerPool = new PlayerPool();
|
|
3670
|
+
this._storyboardCache = new StoryboardCache();
|
|
3671
|
+
this._batcher = new EventBatcher({
|
|
3672
|
+
postEvents: (events) => this._apiClient.postEvents(events),
|
|
3673
|
+
beaconEvents: (events) => this._apiClient.beaconEvents(events)
|
|
3674
|
+
});
|
|
3675
|
+
this._tracker = new EngagementTracker({
|
|
3676
|
+
onEvent: (event) => this._batcher.add(event)
|
|
3677
|
+
});
|
|
3678
|
+
this._batcher.start();
|
|
3679
|
+
}
|
|
3680
|
+
// Identity
|
|
3681
|
+
setUserId(id) {
|
|
3682
|
+
this._identity.setUserId(id);
|
|
3683
|
+
}
|
|
3684
|
+
clearUserId() {
|
|
3685
|
+
this._identity.clearUserId();
|
|
3686
|
+
}
|
|
3687
|
+
// Content
|
|
3688
|
+
async fetchContent({ limit = 10, filter } = {}) {
|
|
3689
|
+
const result = await this._apiClient.fetchFeed({ limit, filter });
|
|
3690
|
+
return result.items;
|
|
3691
|
+
}
|
|
3692
|
+
async preloadFeed({ filter, limit = 10 } = {}) {
|
|
3693
|
+
const result = await this._apiClient.fetchFeed({ limit, filter });
|
|
3694
|
+
if (result.items.length > 0) {
|
|
3695
|
+
const first = result.items[0];
|
|
3696
|
+
if (first.thumbnailUrl) new Image().src = first.thumbnailUrl;
|
|
3697
|
+
if (first.streamingUrl) fetch(first.streamingUrl).catch(() => {
|
|
3698
|
+
});
|
|
3699
|
+
}
|
|
3700
|
+
return result;
|
|
3701
|
+
}
|
|
3702
|
+
// Surface factories
|
|
3703
|
+
createFeed(containerEl, options = {}) {
|
|
3704
|
+
const config = createFeedConfig(options.config);
|
|
3705
|
+
const feed = new FeedManager(containerEl, this, { ...options, config });
|
|
3706
|
+
this._registerSurface(feed);
|
|
3707
|
+
feed.init();
|
|
3708
|
+
return feed;
|
|
3709
|
+
}
|
|
3710
|
+
createPlayer(containerEl, options = {}) {
|
|
3711
|
+
const config = createPlayerConfig(options.config);
|
|
3712
|
+
const player = new SinglePlayer(containerEl, this, { ...options, config });
|
|
3713
|
+
this._registerSurface(player);
|
|
3714
|
+
return player;
|
|
3715
|
+
}
|
|
3716
|
+
createWidget(containerEl, options = {}) {
|
|
3717
|
+
const config = createWidgetConfig(options.config);
|
|
3718
|
+
const widget = new WidgetManager(containerEl, this, { ...options, config });
|
|
3719
|
+
this._registerSurface(widget);
|
|
3720
|
+
return widget;
|
|
3721
|
+
}
|
|
3722
|
+
// Teardown
|
|
3723
|
+
destroy() {
|
|
3724
|
+
if (this._destroyed) return;
|
|
3725
|
+
this._destroyed = true;
|
|
3726
|
+
for (const surface of this._surfaces) {
|
|
3727
|
+
if (typeof surface.destroy === "function") surface.destroy();
|
|
3728
|
+
}
|
|
3729
|
+
this._surfaces.clear();
|
|
3730
|
+
this._batcher.destroy();
|
|
3731
|
+
if (this._playerPool.destroyAll) this._playerPool.destroyAll();
|
|
3732
|
+
this.player.destroy();
|
|
3733
|
+
}
|
|
3734
|
+
// Internal: surface registration
|
|
3735
|
+
_registerSurface(surface) {
|
|
3736
|
+
this._surfaces.add(surface);
|
|
3737
|
+
}
|
|
3738
|
+
_unregisterSurface(surface) {
|
|
3739
|
+
this._surfaces.delete(surface);
|
|
3740
|
+
}
|
|
3741
|
+
}
|
|
3742
|
+
exports2.EmbeddedFeedManager = EmbeddedFeedManager;
|
|
3743
|
+
exports2.FeedManager = FeedManager;
|
|
3744
|
+
exports2.FeedView = FeedView;
|
|
3745
|
+
exports2.ShortKit = ShortKit;
|
|
3746
|
+
exports2.ShortKitVersion = ShortKitVersion;
|
|
3747
|
+
exports2.SinglePlayer = SinglePlayer;
|
|
3748
|
+
exports2.WidgetManager = WidgetManager;
|
|
3749
|
+
exports2.createFeedConfig = createFeedConfig;
|
|
3750
|
+
exports2.createFeedFilter = createFeedFilter;
|
|
3751
|
+
exports2.createFeedItem = createFeedItem;
|
|
3752
|
+
exports2.createPlayerConfig = createPlayerConfig;
|
|
3753
|
+
exports2.createWidgetConfig = createWidgetConfig;
|
|
3754
|
+
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
3755
|
+
}));
|