stagebook 0.1.0 → 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.
@@ -1,11 +1,11 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React$1 from 'react';
3
- import { b as DiscussionType, i as MetadataType, C as Condition } from '../evaluateConditions-DzlC6fez.cjs';
4
- export { al as MediaPlayer, am as MediaPlayerProps, V as VideoEvent } from '../evaluateConditions-DzlC6fez.cjs';
3
+ import { b as DiscussionType, i as MetadataType, C as Condition } from '../evaluateConditions-fqWC3Vz2.cjs';
4
+ export { al as MediaPlayer, am as MediaPlayerProps, V as VideoEvent } from '../evaluateConditions-fqWC3Vz2.cjs';
5
5
  import 'zod';
6
6
 
7
7
  interface StagebookContext {
8
- resolve(reference: string, position?: string): unknown[];
8
+ get(key: string, scope?: string): unknown[];
9
9
  save(key: string, value: unknown, scope?: "player" | "shared"): void;
10
10
  getElapsedTime(): number;
11
11
  submit(): void;
@@ -27,11 +27,14 @@ interface StagebookContext {
27
27
  onComplete: (results: unknown) => void;
28
28
  }) => React$1.ReactNode;
29
29
  }
30
+ interface InternalStagebookContext extends StagebookContext {
31
+ resolve(reference: string, position?: string): unknown[];
32
+ }
30
33
  declare function StagebookProvider({ value, children, }: {
31
34
  value: StagebookContext;
32
35
  children: React$1.ReactNode;
33
36
  }): react_jsx_runtime.JSX.Element;
34
- declare function useStagebookContext(): StagebookContext;
37
+ declare function useStagebookContext(): InternalStagebookContext;
35
38
  declare function useResolve(reference: string, position?: string): unknown[];
36
39
  declare function useSave(): StagebookContext["save"];
37
40
  declare function useElapsedTime(): number;
@@ -1,11 +1,11 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React$1 from 'react';
3
- import { b as DiscussionType, i as MetadataType, C as Condition } from '../evaluateConditions-DzlC6fez.js';
4
- export { al as MediaPlayer, am as MediaPlayerProps, V as VideoEvent } from '../evaluateConditions-DzlC6fez.js';
3
+ import { b as DiscussionType, i as MetadataType, C as Condition } from '../evaluateConditions-fqWC3Vz2.js';
4
+ export { al as MediaPlayer, am as MediaPlayerProps, V as VideoEvent } from '../evaluateConditions-fqWC3Vz2.js';
5
5
  import 'zod';
6
6
 
7
7
  interface StagebookContext {
8
- resolve(reference: string, position?: string): unknown[];
8
+ get(key: string, scope?: string): unknown[];
9
9
  save(key: string, value: unknown, scope?: "player" | "shared"): void;
10
10
  getElapsedTime(): number;
11
11
  submit(): void;
@@ -27,11 +27,14 @@ interface StagebookContext {
27
27
  onComplete: (results: unknown) => void;
28
28
  }) => React$1.ReactNode;
29
29
  }
30
+ interface InternalStagebookContext extends StagebookContext {
31
+ resolve(reference: string, position?: string): unknown[];
32
+ }
30
33
  declare function StagebookProvider({ value, children, }: {
31
34
  value: StagebookContext;
32
35
  children: React$1.ReactNode;
33
36
  }): react_jsx_runtime.JSX.Element;
34
- declare function useStagebookContext(): StagebookContext;
37
+ declare function useStagebookContext(): InternalStagebookContext;
35
38
  declare function useResolve(reference: string, position?: string): unknown[];
36
39
  declare function useSave(): StagebookContext["save"];
37
40
  declare function useElapsedTime(): number;
@@ -1,18 +1,41 @@
1
1
  import {
2
2
  computeWatchedRanges,
3
3
  evaluateCondition,
4
+ getNestedValueByPath,
5
+ getReferenceKeyAndPath,
4
6
  promptFileSchema
5
- } from "../chunk-SSOMQ3BK.js";
7
+ } from "../chunk-2J4CDBDM.js";
6
8
 
7
9
  // src/components/StagebookProvider.tsx
8
- import { createContext, useContext, useState, useEffect } from "react";
10
+ import React, { createContext, useContext, useState, useEffect } from "react";
9
11
  import { jsx } from "react/jsx-runtime";
10
- var StagebookReactContext = createContext(null);
12
+ var StagebookReactContext = createContext(
13
+ null
14
+ );
11
15
  function StagebookProvider({
12
16
  value,
13
17
  children
14
18
  }) {
15
- return /* @__PURE__ */ jsx(StagebookReactContext.Provider, { value, children });
19
+ const resolve = React.useCallback(
20
+ (reference, position) => {
21
+ let referenceKey;
22
+ let path;
23
+ try {
24
+ ({ referenceKey, path } = getReferenceKeyAndPath(reference));
25
+ } catch {
26
+ console.error(`Invalid reference: "${reference}"`);
27
+ return [];
28
+ }
29
+ const rawValues = value.get(referenceKey, position);
30
+ return rawValues.map((v) => getNestedValueByPath(v, path)).filter((v) => v !== void 0);
31
+ },
32
+ [value]
33
+ );
34
+ const internal = React.useMemo(
35
+ () => ({ ...value, resolve }),
36
+ [value, resolve]
37
+ );
38
+ return /* @__PURE__ */ jsx(StagebookReactContext.Provider, { value: internal, children });
16
39
  }
17
40
  function useStagebookContext() {
18
41
  const ctx = useContext(StagebookReactContext);
@@ -38,9 +61,15 @@ function useElapsedTime() {
38
61
  function useTextContent(path) {
39
62
  const { getTextContent } = useStagebookContext();
40
63
  const [data, setData] = useState(void 0);
41
- const [isLoading, setIsLoading] = useState(true);
64
+ const [isLoading, setIsLoading] = useState(false);
42
65
  const [error, setError] = useState(void 0);
43
66
  useEffect(() => {
67
+ if (!path) {
68
+ setData(void 0);
69
+ setError(void 0);
70
+ setIsLoading(false);
71
+ return;
72
+ }
44
73
  let cancelled = false;
45
74
  setIsLoading(true);
46
75
  setError(void 0);
@@ -1316,16 +1345,21 @@ function MediaPlayer({
1316
1345
  stopAt,
1317
1346
  allowScrubOutsideBounds = false,
1318
1347
  stepDuration = 1,
1348
+ playback,
1319
1349
  controls
1320
1350
  }) {
1321
1351
  const youtubeVideoId = isYouTubeURL(url);
1322
1352
  const saveKey = `mediaPlayer_${name}`;
1353
+ const hasAnyControls = controls !== void 0 && (controls.playPause || controls.seek || controls.step || controls.speed);
1354
+ const effectivePlayback = playback ?? (hasAnyControls || syncToStageTime ? "manual" : "once");
1323
1355
  if (!youtubeVideoId && !isSafeURL(url)) {
1324
1356
  return /* @__PURE__ */ jsx13("div", { "data-testid": "mediaPlayer", role: "alert", children: "Invalid media URL" });
1325
1357
  }
1326
1358
  const eventsRef = useRef4([]);
1327
1359
  const videoRef = useRef4(null);
1360
+ const autoplayAttemptedRef = useRef4(false);
1328
1361
  const [isPaused, setIsPaused] = useState4(true);
1362
+ const [showPlayOnce, setShowPlayOnce] = useState4(false);
1329
1363
  const [isHovered, setIsHovered] = useState4(false);
1330
1364
  const [currentTime, setCurrentTime] = useState4(0);
1331
1365
  const [duration, setDuration] = useState4(0);
@@ -1336,6 +1370,8 @@ function MediaPlayer({
1336
1370
  const [loadError, setLoadError] = useState4(null);
1337
1371
  useEffect7(() => {
1338
1372
  setLoadError(null);
1373
+ autoplayAttemptedRef.current = false;
1374
+ setShowPlayOnce(false);
1339
1375
  }, [url]);
1340
1376
  const [ytHandle, setYtHandle] = useState4(null);
1341
1377
  const scrubWasPlayingRef = useRef4(false);
@@ -1512,6 +1548,17 @@ function MediaPlayer({
1512
1548
  videoRef.current.currentTime = startAt;
1513
1549
  }
1514
1550
  }, []);
1551
+ useEffect7(() => {
1552
+ if (effectivePlayback !== "once") return;
1553
+ if (autoplayAttemptedRef.current) return;
1554
+ const v = videoRef.current;
1555
+ if (!v) return;
1556
+ if (duration === 0) return;
1557
+ autoplayAttemptedRef.current = true;
1558
+ void v.play().catch(() => {
1559
+ setShowPlayOnce(true);
1560
+ });
1561
+ }, [effectivePlayback, duration]);
1515
1562
  const recordEvent = useCallback3(
1516
1563
  (type, videoTime, extra) => {
1517
1564
  const event = {
@@ -1671,6 +1718,7 @@ function MediaPlayer({
1671
1718
  }, [playbackRate, recordEvent]);
1672
1719
  const handleKeyDown = useCallback3(
1673
1720
  (e) => {
1721
+ if (effectivePlayback === "once") return;
1674
1722
  if (ytHandle) {
1675
1723
  switch (e.key) {
1676
1724
  case " ":
@@ -1773,7 +1821,13 @@ function MediaPlayer({
1773
1821
  break;
1774
1822
  }
1775
1823
  },
1776
- [seek, stepDuration, playbackRate, enterFastScrubForward]
1824
+ [
1825
+ effectivePlayback,
1826
+ seek,
1827
+ stepDuration,
1828
+ playbackRate,
1829
+ enterFastScrubForward
1830
+ ]
1777
1831
  );
1778
1832
  const handleKeyUp = useCallback3(
1779
1833
  (e) => {
@@ -2161,10 +2215,86 @@ function MediaPlayer({
2161
2215
  },
2162
2216
  children: /* @__PURE__ */ jsx13(HTML5Controls, { ...html5ControlsProps })
2163
2217
  }
2218
+ ),
2219
+ showPlayOnce && /* @__PURE__ */ jsx13(
2220
+ "button",
2221
+ {
2222
+ type: "button",
2223
+ "data-testid": "mediaPlayer-playOnce",
2224
+ "aria-label": "Play video",
2225
+ onClick: () => {
2226
+ setShowPlayOnce(false);
2227
+ const v = videoRef.current;
2228
+ if (!v) {
2229
+ setShowPlayOnce(true);
2230
+ return;
2231
+ }
2232
+ void v.play().catch(() => {
2233
+ setShowPlayOnce(true);
2234
+ });
2235
+ },
2236
+ style: {
2237
+ position: "absolute",
2238
+ top: "50%",
2239
+ left: "50%",
2240
+ transform: "translate(-50%, -50%)",
2241
+ background: "rgba(0,0,0,0.6)",
2242
+ border: "none",
2243
+ borderRadius: "50%",
2244
+ width: 64,
2245
+ height: 64,
2246
+ cursor: "pointer",
2247
+ display: "flex",
2248
+ alignItems: "center",
2249
+ justifyContent: "center",
2250
+ color: "#fff"
2251
+ },
2252
+ children: /* @__PURE__ */ jsx13(
2253
+ "svg",
2254
+ {
2255
+ width: 32,
2256
+ height: 32,
2257
+ viewBox: "0 0 24 24",
2258
+ fill: "currentColor",
2259
+ "aria-hidden": "true",
2260
+ children: /* @__PURE__ */ jsx13("polygon", { points: "6,3 20,12 6,21" })
2261
+ }
2262
+ )
2263
+ }
2164
2264
  )
2165
2265
  ]
2166
2266
  }
2167
2267
  ),
2268
+ !playVideo && showPlayOnce && /* @__PURE__ */ jsx13(
2269
+ "button",
2270
+ {
2271
+ type: "button",
2272
+ "data-testid": "mediaPlayer-playOnce",
2273
+ "aria-label": "Play audio",
2274
+ onClick: () => {
2275
+ setShowPlayOnce(false);
2276
+ const v = videoRef.current;
2277
+ if (!v) {
2278
+ setShowPlayOnce(true);
2279
+ return;
2280
+ }
2281
+ void v.play().catch(() => {
2282
+ setShowPlayOnce(true);
2283
+ });
2284
+ },
2285
+ style: {
2286
+ background: "rgba(28,28,30,0.96)",
2287
+ border: "1px solid rgba(255,255,255,0.2)",
2288
+ borderRadius: "0.5rem",
2289
+ padding: "0.75rem 1.5rem",
2290
+ cursor: "pointer",
2291
+ color: "#fff",
2292
+ fontSize: "0.875rem",
2293
+ fontWeight: 500
2294
+ },
2295
+ children: "Play"
2296
+ }
2297
+ ),
2168
2298
  !playVideo && controlsVisible && !loadError && /* @__PURE__ */ jsx13(
2169
2299
  "div",
2170
2300
  {
@@ -5387,7 +5517,10 @@ function Element({ element, onSubmit, stageDuration }) {
5387
5517
  case "prompt": {
5388
5518
  if (promptError) {
5389
5519
  return /* @__PURE__ */ jsxs21("p", { style: { color: "#dc2626", fontSize: "0.875rem" }, children: [
5390
- "Error loading prompt: ",
5520
+ "Error loading prompt",
5521
+ element.file ? ` "${element.file}"` : "",
5522
+ ":",
5523
+ " ",
5391
5524
  promptError.message
5392
5525
  ] });
5393
5526
  }
@@ -5404,7 +5537,10 @@ function Element({ element, onSubmit, stageDuration }) {
5404
5537
  fontSize: "0.875rem"
5405
5538
  },
5406
5539
  children: [
5407
- "Error parsing prompt: ",
5540
+ "Error parsing prompt",
5541
+ element.file ? ` "${element.file}"` : "",
5542
+ ":",
5543
+ " ",
5408
5544
  parsed.error.issues[0]?.message
5409
5545
  ]
5410
5546
  }
@@ -5477,6 +5613,7 @@ function Element({ element, onSubmit, stageDuration }) {
5477
5613
  stopAt: element.stopAt,
5478
5614
  allowScrubOutsideBounds: element.allowScrubOutsideBounds,
5479
5615
  stepDuration: element.stepDuration,
5616
+ playback: element.playback,
5480
5617
  controls: element.controls
5481
5618
  }
5482
5619
  );