@interlacejs/player 0.1.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/index.cjs ADDED
@@ -0,0 +1,859 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Anchor: () => Anchor,
34
+ BlockingOverlay: () => BlockingOverlay,
35
+ CompletedTag: () => CompletedTag,
36
+ ContentErrorBoundary: () => ContentErrorBoundary,
37
+ InteractiveVideoPlayer: () => InteractiveVideoPlayer,
38
+ OverlayLayer: () => OverlayLayer,
39
+ PreloadScheduler: () => PreloadScheduler,
40
+ useInteractiveMedia: () => useInteractiveMedia
41
+ });
42
+ module.exports = __toCommonJS(index_exports);
43
+
44
+ // src/components/anchor.tsx
45
+ var import_react = __toESM(require("react"), 1);
46
+
47
+ // src/tokens.ts
48
+ var COLORS = {
49
+ accent: "#0066cc",
50
+ accentHover: "#0052a3",
51
+ accentLight: "rgba(0, 102, 204, 0.15)",
52
+ accentLighter: "rgba(0, 102, 204, 0.08)",
53
+ text: "#0f172a",
54
+ textMuted: "#64748b",
55
+ border: "#e2e8f0",
56
+ borderStrong: "#cbd5e1",
57
+ surface: "#ffffff",
58
+ surfaceRaised: "#f8fafc",
59
+ ground: "#f6f7f9",
60
+ errorBg: "#fef2f2",
61
+ errorText: "#b91c1c",
62
+ errorBorder: "#fecaca",
63
+ white: "#ffffff",
64
+ black: "#000000"
65
+ };
66
+ var FONTS = {
67
+ display: 'Georgia, "Times New Roman", Times, serif',
68
+ body: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
69
+ };
70
+ var SPACE = {
71
+ 1: 4,
72
+ 2: 8,
73
+ 3: 12,
74
+ 4: 16,
75
+ 5: 24
76
+ };
77
+ var RADIUS = {
78
+ sm: 4,
79
+ md: 8,
80
+ lg: 12
81
+ };
82
+ var SHADOWS = {
83
+ sm: "0 1px 2px 0 rgba(15, 23, 42, 0.04)",
84
+ md: "0 4px 12px -2px rgba(15, 23, 42, 0.08)"
85
+ };
86
+ var TYPE = {
87
+ xs: 12,
88
+ sm: 13,
89
+ base: 15,
90
+ md: 16,
91
+ lg: 20,
92
+ xl: 24
93
+ };
94
+
95
+ // src/components/anchor.tsx
96
+ var import_jsx_runtime = require("react/jsx-runtime");
97
+ var Anchor = import_react.default.forwardRef(
98
+ ({ id, placement, title, isOpened, onClick }, ref) => {
99
+ const buttonRef = (0, import_react.useRef)(null);
100
+ const internalRef = ref || buttonRef;
101
+ const minSize = 44;
102
+ const width = Math.max(placement.width, minSize / 100);
103
+ const height = Math.max(placement.height, minSize / 100);
104
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
105
+ "button",
106
+ {
107
+ ref: internalRef,
108
+ onClick,
109
+ "aria-label": title,
110
+ "data-content-id": id,
111
+ style: {
112
+ position: "absolute",
113
+ left: `${placement.x}%`,
114
+ top: `${placement.y}%`,
115
+ width: `${width}%`,
116
+ height: `${height}%`,
117
+ minWidth: `${minSize}px`,
118
+ minHeight: `${minSize}px`,
119
+ padding: `${SPACE[2]}px ${SPACE[3]}px`,
120
+ fontSize: TYPE.md,
121
+ fontWeight: 600,
122
+ border: `2px solid ${COLORS.accent}`,
123
+ borderRadius: `${RADIUS.sm}px`,
124
+ backgroundColor: isOpened ? COLORS.accent : COLORS.surface,
125
+ color: isOpened ? COLORS.white : COLORS.accent,
126
+ cursor: "pointer",
127
+ transition: "background-color 150ms ease, color 150ms ease, box-shadow 150ms ease",
128
+ boxShadow: isOpened ? "0 4px 8px rgba(0, 102, 204, 0.3)" : SHADOWS.sm,
129
+ zIndex: 2,
130
+ // The anchors layer is pointer-transparent; each anchor
131
+ // re-enables pointer events for itself.
132
+ pointerEvents: "auto"
133
+ },
134
+ children: title
135
+ }
136
+ );
137
+ }
138
+ );
139
+ Anchor.displayName = "Anchor";
140
+
141
+ // src/components/blocking-overlay.tsx
142
+ var import_react2 = require("react");
143
+ var import_jsx_runtime2 = require("react/jsx-runtime");
144
+ var BlockingOverlay = ({
145
+ content,
146
+ contentType,
147
+ onContentComplete,
148
+ onSkip,
149
+ completing = false,
150
+ completingScore,
151
+ onContinue
152
+ }) => {
153
+ const overlayRef = (0, import_react2.useRef)(null);
154
+ const bodyRef = (0, import_react2.useRef)(null);
155
+ const [revealed, setRevealed] = (0, import_react2.useState)(false);
156
+ const onContentCompleteRef = (0, import_react2.useRef)(onContentComplete);
157
+ onContentCompleteRef.current = onContentComplete;
158
+ (0, import_react2.useEffect)(() => {
159
+ const body = bodyRef.current;
160
+ if (!body || !contentType) return;
161
+ try {
162
+ contentType.renderPlayback(body, content.getData(), {
163
+ onComplete: (score) => {
164
+ onContentCompleteRef.current?.(score);
165
+ }
166
+ });
167
+ } catch (err) {
168
+ const error = err instanceof Error ? err : new Error(String(err));
169
+ body.textContent = error.message;
170
+ return;
171
+ }
172
+ return () => {
173
+ contentType.unmountPlayback?.(body);
174
+ };
175
+ }, [contentType, content]);
176
+ (0, import_react2.useEffect)(() => {
177
+ if (!completing) {
178
+ setRevealed(false);
179
+ return;
180
+ }
181
+ const raf = requestAnimationFrame(() => setRevealed(true));
182
+ return () => cancelAnimationFrame(raf);
183
+ }, [completing]);
184
+ (0, import_react2.useEffect)(() => {
185
+ const overlay = overlayRef.current;
186
+ if (!overlay) return;
187
+ const focusableElements = overlay.querySelectorAll(
188
+ 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
189
+ );
190
+ if (focusableElements.length === 0) return;
191
+ const firstElement = focusableElements[0];
192
+ const lastElement = focusableElements[focusableElements.length - 1];
193
+ const handleKeyDown = (e) => {
194
+ if (e.key !== "Tab") return;
195
+ if (e.shiftKey) {
196
+ if (document.activeElement === firstElement) {
197
+ e.preventDefault();
198
+ lastElement.focus();
199
+ }
200
+ } else {
201
+ if (document.activeElement === lastElement) {
202
+ e.preventDefault();
203
+ firstElement.focus();
204
+ }
205
+ }
206
+ };
207
+ overlay.addEventListener("keydown", handleKeyDown);
208
+ firstElement.focus();
209
+ return () => {
210
+ overlay.removeEventListener("keydown", handleKeyDown);
211
+ };
212
+ }, [completing]);
213
+ const completionSurface = /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
214
+ "div",
215
+ {
216
+ style: {
217
+ display: "flex",
218
+ flexDirection: "column",
219
+ alignItems: "center",
220
+ justifyContent: "center",
221
+ gap: `${SPACE[3]}px`,
222
+ padding: `${SPACE[4]}px 0`
223
+ },
224
+ children: [
225
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
226
+ "div",
227
+ {
228
+ style: {
229
+ width: 56,
230
+ height: 56,
231
+ borderRadius: "50%",
232
+ backgroundColor: COLORS.accentLighter,
233
+ color: COLORS.accent,
234
+ display: "flex",
235
+ alignItems: "center",
236
+ justifyContent: "center",
237
+ fontSize: TYPE.xl,
238
+ fontWeight: 600,
239
+ lineHeight: 1,
240
+ opacity: revealed ? 1 : 0,
241
+ transform: revealed ? "scale(1)" : "scale(0.6)",
242
+ transition: "opacity 200ms ease, transform 200ms ease"
243
+ },
244
+ "aria-hidden": "true",
245
+ children: "\u2713"
246
+ }
247
+ ),
248
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
249
+ "div",
250
+ {
251
+ style: {
252
+ display: "flex",
253
+ flexDirection: "column",
254
+ alignItems: "center",
255
+ gap: `${SPACE[1]}px`,
256
+ margin: 0,
257
+ fontFamily: FONTS.display,
258
+ fontSize: TYPE.lg,
259
+ fontWeight: 600,
260
+ color: COLORS.text,
261
+ opacity: revealed ? 1 : 0,
262
+ transform: revealed ? "none" : "translateY(6px)",
263
+ transition: "opacity 250ms ease 120ms, transform 250ms ease 120ms"
264
+ },
265
+ children: [
266
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: "Completed" }),
267
+ typeof completingScore === "number" && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
268
+ "span",
269
+ {
270
+ "aria-label": `completion-score-${completingScore}`,
271
+ style: {
272
+ display: "inline-block",
273
+ padding: `0 ${SPACE[2]}px`,
274
+ borderRadius: `${RADIUS.sm}px`,
275
+ backgroundColor: COLORS.accent,
276
+ color: COLORS.white,
277
+ fontSize: TYPE.sm,
278
+ fontWeight: 600,
279
+ verticalAlign: "middle"
280
+ },
281
+ children: [
282
+ "Score: ",
283
+ completingScore,
284
+ "/",
285
+ content.getMaximumScore()
286
+ ]
287
+ }
288
+ )
289
+ ]
290
+ }
291
+ ),
292
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
293
+ "button",
294
+ {
295
+ type: "button",
296
+ onClick: onContinue,
297
+ style: {
298
+ marginTop: `${SPACE[3]}px`,
299
+ padding: `${SPACE[2]}px ${SPACE[5]}px`,
300
+ backgroundColor: COLORS.accent,
301
+ color: COLORS.white,
302
+ border: "none",
303
+ borderRadius: `${RADIUS.sm}px`,
304
+ cursor: "pointer",
305
+ fontSize: TYPE.md,
306
+ fontWeight: 600,
307
+ transition: "background-color 150ms ease",
308
+ opacity: revealed ? 1 : 0
309
+ },
310
+ children: "Continue"
311
+ }
312
+ )
313
+ ]
314
+ }
315
+ );
316
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
317
+ "div",
318
+ {
319
+ ref: overlayRef,
320
+ style: {
321
+ position: "fixed",
322
+ top: 0,
323
+ left: 0,
324
+ right: 0,
325
+ bottom: 0,
326
+ display: "flex",
327
+ alignItems: "center",
328
+ justifyContent: "center",
329
+ backgroundColor: "rgba(0, 0, 0, 0.7)",
330
+ zIndex: 1e3,
331
+ // Re-enable pointer events explicitly: the editor's preview
332
+ // modal mounts the player inside a `pointerEvents: "none"`
333
+ // container, and a descendant with `auto` re-enables it.
334
+ pointerEvents: "auto"
335
+ },
336
+ role: "dialog",
337
+ "aria-modal": "true",
338
+ "aria-labelledby": `blocking-content-${content.getId()}`,
339
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
340
+ "div",
341
+ {
342
+ style: {
343
+ backgroundColor: COLORS.surface,
344
+ borderRadius: `${RADIUS.lg}px`,
345
+ padding: `${SPACE[5]}px`,
346
+ maxWidth: "500px",
347
+ width: "90%",
348
+ maxHeight: "80vh",
349
+ overflowY: "auto",
350
+ boxShadow: "0 16px 48px rgba(0, 0, 0, 0.35)"
351
+ },
352
+ children: [
353
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
354
+ "div",
355
+ {
356
+ style: {
357
+ display: "flex",
358
+ justifyContent: "space-between",
359
+ alignItems: "flex-start",
360
+ gap: `${SPACE[2]}px`
361
+ },
362
+ children: [
363
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
364
+ "h2",
365
+ {
366
+ id: `blocking-content-${content.getId()}`,
367
+ style: {
368
+ marginTop: 0,
369
+ fontFamily: FONTS.display,
370
+ fontSize: TYPE.lg,
371
+ fontWeight: 600,
372
+ color: COLORS.text
373
+ },
374
+ children: content.getTitle()
375
+ }
376
+ ),
377
+ content.getHook().type === "non-blocking" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
378
+ "button",
379
+ {
380
+ type: "button",
381
+ onClick: () => {
382
+ content.skip();
383
+ onSkip?.();
384
+ },
385
+ style: {
386
+ padding: `${SPACE[1]}px ${SPACE[2]}px`,
387
+ backgroundColor: "red",
388
+ color: COLORS.white,
389
+ border: "none",
390
+ borderRadius: `${RADIUS.sm}px`,
391
+ cursor: "pointer",
392
+ fontSize: TYPE.sm,
393
+ fontWeight: 600,
394
+ transition: "background-color 150ms ease"
395
+ },
396
+ children: "Skip"
397
+ }
398
+ )
399
+ ]
400
+ }
401
+ ),
402
+ completing ? completionSurface : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { margin: `${SPACE[4]}px 0` }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ref: bodyRef }) })
403
+ ]
404
+ }
405
+ )
406
+ }
407
+ );
408
+ };
409
+
410
+ // src/components/completed-tag.tsx
411
+ var import_jsx_runtime3 = require("react/jsx-runtime");
412
+ var CompletedTag = ({
413
+ id,
414
+ title,
415
+ placement
416
+ }) => {
417
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
418
+ "div",
419
+ {
420
+ "data-content-id": id,
421
+ style: {
422
+ position: "absolute",
423
+ left: `${placement.x}%`,
424
+ top: `${placement.y}%`,
425
+ width: `${placement.width}%`,
426
+ height: `${placement.height}%`,
427
+ display: "flex",
428
+ alignItems: "center",
429
+ justifyContent: "center",
430
+ backgroundColor: COLORS.accent,
431
+ color: COLORS.white,
432
+ fontSize: TYPE.xs,
433
+ fontWeight: 700,
434
+ borderRadius: `${RADIUS.sm}px`,
435
+ padding: `${SPACE[1]}px ${SPACE[2]}px`,
436
+ textAlign: "center",
437
+ zIndex: 4,
438
+ boxShadow: SHADOWS.sm
439
+ },
440
+ children: [
441
+ "\u2713 ",
442
+ title
443
+ ]
444
+ }
445
+ );
446
+ };
447
+
448
+ // src/components/content-error-boundary.tsx
449
+ var import_react3 = __toESM(require("react"), 1);
450
+ var import_jsx_runtime4 = require("react/jsx-runtime");
451
+ var ContentErrorBoundary = class extends import_react3.default.Component {
452
+ constructor(props) {
453
+ super(props);
454
+ this.state = { hasError: false, error: null };
455
+ }
456
+ static getDerivedStateFromError(error) {
457
+ return { hasError: true, error };
458
+ }
459
+ componentDidCatch(error) {
460
+ this.props.onError?.(error);
461
+ }
462
+ render() {
463
+ if (this.state.hasError) {
464
+ return this.props.fallback || /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
465
+ "div",
466
+ {
467
+ style: {
468
+ padding: `${SPACE[4]}px`,
469
+ backgroundColor: COLORS.errorBg,
470
+ color: COLORS.errorText,
471
+ border: `1px solid ${COLORS.errorBorder}`,
472
+ borderRadius: `${RADIUS.md}px`,
473
+ fontSize: TYPE.md,
474
+ lineHeight: 1.5
475
+ },
476
+ children: [
477
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("strong", { children: "Content Error" }),
478
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { style: { margin: `${SPACE[1]}px 0 0 0` }, children: this.state.error?.message })
479
+ ]
480
+ }
481
+ );
482
+ }
483
+ return this.props.children;
484
+ }
485
+ };
486
+
487
+ // src/components/interactive-video-player.tsx
488
+ var import_react6 = require("react");
489
+
490
+ // src/hooks/use-interactive-media.ts
491
+ var import_core = require("@interlacejs/core");
492
+ var import_react4 = require("react");
493
+ function useInteractiveMedia(options) {
494
+ const {
495
+ adapter,
496
+ document: doc,
497
+ registry,
498
+ onRenderStateChange,
499
+ onError
500
+ } = options;
501
+ const controllerRef = (0, import_react4.useRef)(null);
502
+ const rafRef = (0, import_react4.useRef)(null);
503
+ const isPlayingRef = (0, import_react4.useRef)(false);
504
+ const [initialized, setInitialized] = (0, import_react4.useState)(false);
505
+ const [error, setError] = (0, import_react4.useState)(null);
506
+ const [items, setItems] = (0, import_react4.useState)(null);
507
+ (0, import_react4.useEffect)(() => {
508
+ try {
509
+ const result = (0, import_core.deserialize)(doc, registry);
510
+ if (result.validationErrors.length > 0) {
511
+ throw new Error(
512
+ `Document validation failed: ${result.validationErrors[0]}`
513
+ );
514
+ }
515
+ const controller = new import_core.InteractiveMediaController(
516
+ result.items,
517
+ result.videoDuration
518
+ );
519
+ controllerRef.current = controller;
520
+ setItems(result.items);
521
+ setInitialized(true);
522
+ return () => {
523
+ controllerRef.current = null;
524
+ setItems(null);
525
+ };
526
+ } catch (err) {
527
+ const error2 = err instanceof Error ? err : new Error(String(err));
528
+ setError(error2);
529
+ onError?.(error2);
530
+ }
531
+ }, [doc, registry, onError]);
532
+ (0, import_react4.useEffect)(() => {
533
+ if (!initialized || !controllerRef.current) return;
534
+ const controller = controllerRef.current;
535
+ const unsubscribePause = adapter.on("pause", () => {
536
+ isPlayingRef.current = false;
537
+ });
538
+ const unsubscribePlaying = adapter.on("play", () => {
539
+ isPlayingRef.current = true;
540
+ });
541
+ const unsubscribeRequestPause = controller.on("requestPause", () => {
542
+ adapter.pause();
543
+ });
544
+ const unsubscribeRequestSeek = controller.on("requestSeek", (event) => {
545
+ adapter.seek(event.timeInSeconds);
546
+ });
547
+ return () => {
548
+ unsubscribePause();
549
+ unsubscribePlaying();
550
+ unsubscribeRequestPause();
551
+ unsubscribeRequestSeek();
552
+ };
553
+ }, [initialized, adapter]);
554
+ (0, import_react4.useEffect)(() => {
555
+ if (!initialized || !controllerRef.current) return;
556
+ const controller = controllerRef.current;
557
+ const tick = () => {
558
+ const currentTime = adapter.getCurrentTime();
559
+ const isPlayingForward = isPlayingRef.current;
560
+ controller.tick(currentTime, isPlayingForward);
561
+ const renderState = controller.getRenderState(currentTime);
562
+ onRenderStateChange?.(renderState);
563
+ rafRef.current = requestAnimationFrame(tick);
564
+ };
565
+ rafRef.current = requestAnimationFrame(tick);
566
+ return () => {
567
+ if (rafRef.current) {
568
+ cancelAnimationFrame(rafRef.current);
569
+ }
570
+ };
571
+ }, [initialized, adapter, onRenderStateChange]);
572
+ return {
573
+ controller: controllerRef.current,
574
+ /** The deserialized content instances backing the controller. */
575
+ items,
576
+ initialized,
577
+ error
578
+ };
579
+ }
580
+
581
+ // src/components/overlay-layer.tsx
582
+ var import_react5 = require("react");
583
+ var import_jsx_runtime5 = require("react/jsx-runtime");
584
+ var OverlayLayer = ({
585
+ renderState,
586
+ items,
587
+ onAnchorClick
588
+ }) => {
589
+ const itemMap = (0, import_react5.useMemo)(() => {
590
+ const map = /* @__PURE__ */ new Map();
591
+ for (const item of items) {
592
+ map.set(item.content.getId(), item);
593
+ }
594
+ return map;
595
+ }, [items]);
596
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
597
+ "div",
598
+ {
599
+ style: {
600
+ position: "absolute",
601
+ top: 0,
602
+ left: 0,
603
+ width: "100%",
604
+ height: "100%",
605
+ zIndex: 10,
606
+ pointerEvents: "none"
607
+ },
608
+ children: [
609
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
610
+ "div",
611
+ {
612
+ style: {
613
+ position: "absolute",
614
+ top: 0,
615
+ left: 0,
616
+ width: "100%",
617
+ height: "100%",
618
+ pointerEvents: "none"
619
+ },
620
+ children: renderState.visibleAnchorIds?.map((contentId) => {
621
+ const item = itemMap.get(contentId);
622
+ if (!item) return null;
623
+ const hook = item.hook;
624
+ if (hook.type !== "non-blocking") return null;
625
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
626
+ Anchor,
627
+ {
628
+ id: contentId,
629
+ placement: hook.placement,
630
+ title: item.content.getTitle(),
631
+ isOpened: renderState.openedContentIds.includes(contentId),
632
+ onClick: () => {
633
+ item.content.open();
634
+ onAnchorClick?.(contentId);
635
+ }
636
+ },
637
+ contentId
638
+ );
639
+ })
640
+ }
641
+ ),
642
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
643
+ "div",
644
+ {
645
+ style: {
646
+ position: "absolute",
647
+ top: 0,
648
+ left: 0,
649
+ width: "100%",
650
+ height: "100%",
651
+ pointerEvents: "none"
652
+ },
653
+ children: renderState.completedTagIds?.map((contentId) => {
654
+ const item = itemMap.get(contentId);
655
+ if (!item) return null;
656
+ const hook = item.hook;
657
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
658
+ CompletedTag,
659
+ {
660
+ id: contentId,
661
+ title: item.content.getTitle(),
662
+ placement: hook.placement
663
+ },
664
+ `completed-${contentId}`
665
+ );
666
+ })
667
+ }
668
+ )
669
+ ]
670
+ }
671
+ );
672
+ };
673
+
674
+ // src/components/interactive-video-player.tsx
675
+ var import_jsx_runtime6 = require("react/jsx-runtime");
676
+ var InteractiveVideoPlayer = ({
677
+ adapter,
678
+ document: doc,
679
+ registry,
680
+ onError
681
+ }) => {
682
+ const [renderState, setRenderState] = (0, import_react6.useState)({
683
+ visibleAnchorIds: [],
684
+ completedTagIds: [],
685
+ openedContentIds: []
686
+ });
687
+ const [activeContentId, setActiveContentId] = (0, import_react6.useState)(null);
688
+ const [completingId, setCompletingId] = (0, import_react6.useState)(null);
689
+ const [completingScore, setCompletingScore] = (0, import_react6.useState)(
690
+ void 0
691
+ );
692
+ const lastAutoOpenedBlockingIdRef = (0, import_react6.useRef)(null);
693
+ const { controller, items, initialized, error } = useInteractiveMedia({
694
+ adapter,
695
+ document: doc,
696
+ registry,
697
+ onRenderStateChange: setRenderState,
698
+ onError
699
+ });
700
+ const handleAnchorClick = (0, import_react6.useCallback)(
701
+ (contentId) => {
702
+ adapter.pause();
703
+ setActiveContentId(contentId);
704
+ },
705
+ [adapter]
706
+ );
707
+ (0, import_react6.useEffect)(() => {
708
+ const blockingId = renderState.activeBlockingContentId;
709
+ if (blockingId && blockingId !== lastAutoOpenedBlockingIdRef.current) {
710
+ lastAutoOpenedBlockingIdRef.current = blockingId;
711
+ setActiveContentId(blockingId);
712
+ } else if (!blockingId && lastAutoOpenedBlockingIdRef.current) {
713
+ lastAutoOpenedBlockingIdRef.current = null;
714
+ }
715
+ }, [renderState.activeBlockingContentId]);
716
+ const handleContentComplete = (0, import_react6.useCallback)(
717
+ (score) => {
718
+ if (!controller || !activeContentId) return;
719
+ const item = (items ?? []).find((it) => it.getId() === activeContentId);
720
+ item?.complete(score ?? 0);
721
+ setCompletingScore(score ?? 0);
722
+ setCompletingId(activeContentId);
723
+ setActiveContentId(null);
724
+ },
725
+ [controller, items, activeContentId]
726
+ );
727
+ const handleContinueAfterCompletion = (0, import_react6.useCallback)(() => {
728
+ setCompletingId(null);
729
+ setCompletingScore(void 0);
730
+ void adapter.play();
731
+ }, [adapter]);
732
+ if (error) {
733
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
734
+ "div",
735
+ {
736
+ style: {
737
+ padding: `${SPACE[4]}px`,
738
+ color: COLORS.errorText,
739
+ backgroundColor: COLORS.errorBg,
740
+ border: `1px solid ${COLORS.errorBorder}`,
741
+ borderRadius: `${RADIUS.md}px`,
742
+ fontSize: TYPE.md,
743
+ lineHeight: 1.5
744
+ },
745
+ children: [
746
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("strong", { children: "Player Error:" }),
747
+ " ",
748
+ error.message
749
+ ]
750
+ }
751
+ );
752
+ }
753
+ if (!initialized || !controller) {
754
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
755
+ "div",
756
+ {
757
+ style: {
758
+ padding: `${SPACE[4]}px`,
759
+ color: COLORS.textMuted,
760
+ fontSize: TYPE.md
761
+ },
762
+ children: "Loading player\u2026"
763
+ }
764
+ );
765
+ }
766
+ const overlayItems = (items ?? []).map((content) => ({
767
+ content,
768
+ hook: content.getHook()
769
+ }));
770
+ const overlayContentId = activeContentId ?? completingId;
771
+ const activeItem = overlayItems.find(
772
+ (item) => item.content.getId() === overlayContentId
773
+ );
774
+ const activeContentType = activeItem ? registry.get(activeItem.content.getContentTypeId()) : void 0;
775
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ContentErrorBoundary, { onError, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { position: "relative", width: "100%", height: "100%" }, children: [
776
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
777
+ OverlayLayer,
778
+ {
779
+ renderState,
780
+ items: overlayItems,
781
+ onAnchorClick: handleAnchorClick
782
+ }
783
+ ),
784
+ activeItem && activeContentType && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
785
+ BlockingOverlay,
786
+ {
787
+ content: activeItem.content,
788
+ contentType: activeContentType,
789
+ onSkip: () => {
790
+ const item = (items ?? []).find(
791
+ (it) => it.getId() === overlayContentId
792
+ );
793
+ item?.skip();
794
+ setActiveContentId(null);
795
+ void adapter.play();
796
+ },
797
+ onContentComplete: handleContentComplete,
798
+ onContinue: handleContinueAfterCompletion,
799
+ completing: completingId !== null && completingId === overlayContentId,
800
+ completingScore
801
+ }
802
+ )
803
+ ] }) });
804
+ };
805
+
806
+ // src/utils/preload-scheduler.ts
807
+ var PreloadScheduler = class {
808
+ leadTimeMs;
809
+ scheduledPreloads = /* @__PURE__ */ new Map();
810
+ constructor(leadTimeMs = 2e3) {
811
+ this.leadTimeMs = leadTimeMs;
812
+ }
813
+ /**
814
+ * Get content IDs that should be preloaded at the current time.
815
+ */
816
+ getPreloadQueue(currentTime, items) {
817
+ const queue = [];
818
+ const cutoffTime = currentTime + this.leadTimeMs / 1e3;
819
+ for (const item of items) {
820
+ const contentId = item.content.getId();
821
+ const hook = item.hook;
822
+ if (hook.type === "non-blocking") {
823
+ if (hook.start <= cutoffTime && hook.start > currentTime) {
824
+ queue.push(contentId);
825
+ }
826
+ }
827
+ if (hook.type === "blocking") {
828
+ if (hook.timestamp <= cutoffTime && hook.timestamp > currentTime) {
829
+ queue.push(contentId);
830
+ }
831
+ }
832
+ }
833
+ return queue;
834
+ }
835
+ /**
836
+ * Clear all scheduled preloads (e.g., on seek).
837
+ */
838
+ clear() {
839
+ this.scheduledPreloads.clear();
840
+ }
841
+ /**
842
+ * Set lead time in milliseconds.
843
+ */
844
+ setLeadTime(ms) {
845
+ this.leadTimeMs = ms;
846
+ }
847
+ };
848
+ // Annotate the CommonJS export names for ESM import in node:
849
+ 0 && (module.exports = {
850
+ Anchor,
851
+ BlockingOverlay,
852
+ CompletedTag,
853
+ ContentErrorBoundary,
854
+ InteractiveVideoPlayer,
855
+ OverlayLayer,
856
+ PreloadScheduler,
857
+ useInteractiveMedia
858
+ });
859
+ //# sourceMappingURL=index.cjs.map