@olenbetong/synergi-course 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/es/chrome/CategoryHeader.css +28 -0
  2. package/es/completion/CourseCompletion.css +53 -0
  3. package/es/completion/CourseCompletion.d.ts +32 -0
  4. package/es/completion/CourseCompletion.js +60 -0
  5. package/es/document/cues.d.ts +51 -6
  6. package/es/document/cues.js +117 -14
  7. package/es/document/effects.d.ts +30 -0
  8. package/es/document/effects.js +76 -0
  9. package/es/document/schema.d.ts +2722 -346
  10. package/es/document/schema.js +172 -5
  11. package/es/document/validate.d.ts +2 -1
  12. package/es/document/validate.js +83 -16
  13. package/es/index.d.ts +8 -3
  14. package/es/index.js +6 -2
  15. package/es/media/Media.css +16 -0
  16. package/es/media/icons.d.ts +1 -0
  17. package/es/media/icons.js +3 -0
  18. package/es/player/CoursePlayer.css +76 -0
  19. package/es/player/CoursePlayer.js +28 -13
  20. package/es/player/PlayerBars.d.ts +32 -0
  21. package/es/player/PlayerBars.js +14 -0
  22. package/es/player/useFullScreen.d.ts +17 -0
  23. package/es/player/useFullScreen.js +83 -0
  24. package/es/player/useMediaQuery.d.ts +7 -0
  25. package/es/player/useMediaQuery.js +16 -0
  26. package/es/steps/Blocks.d.ts +43 -3
  27. package/es/steps/Blocks.js +114 -26
  28. package/es/steps/Markdown.d.ts +7 -1
  29. package/es/steps/Markdown.js +31 -4
  30. package/es/steps/QuizStepView.d.ts +2 -1
  31. package/es/steps/QuizStepView.js +20 -10
  32. package/es/steps/RevealMotion.d.ts +29 -0
  33. package/es/steps/RevealMotion.js +55 -0
  34. package/es/steps/SlideStepView.d.ts +5 -3
  35. package/es/steps/SlideStepView.js +43 -10
  36. package/es/steps/StepBodyProps.d.ts +16 -1
  37. package/es/steps/StepView.css +390 -16
  38. package/es/steps/StepView.d.ts +36 -4
  39. package/es/steps/StepView.js +21 -5
  40. package/es/steps/motion.d.ts +59 -0
  41. package/es/steps/motion.js +136 -0
  42. package/es/transitions/Transitions.css +114 -0
  43. package/es/transitions/plan.d.ts +66 -0
  44. package/es/transitions/plan.js +185 -0
  45. package/es/transitions/playStepTransition.d.ts +23 -0
  46. package/es/transitions/playStepTransition.js +131 -0
  47. package/es/types.d.ts +16 -3
  48. package/package.json +4 -4
  49. package/src/chrome/CategoryHeader.css +28 -0
  50. package/src/completion/CourseCompletion.css +53 -0
  51. package/src/completion/CourseCompletion.test.tsx +45 -0
  52. package/src/completion/CourseCompletion.tsx +98 -0
  53. package/src/document/box.test.ts +234 -0
  54. package/src/document/cues.ts +143 -13
  55. package/src/document/document.test.ts +1 -1
  56. package/src/document/effects.ts +106 -0
  57. package/src/document/reveal.test.ts +263 -0
  58. package/src/document/schema.ts +203 -5
  59. package/src/document/validate.ts +88 -18
  60. package/src/index.ts +46 -1
  61. package/src/media/Media.css +16 -0
  62. package/src/media/icons.tsx +8 -0
  63. package/src/player/CoursePlayer.css +76 -0
  64. package/src/player/CoursePlayer.test.tsx +70 -6
  65. package/src/player/CoursePlayer.tsx +91 -60
  66. package/src/player/PlayerBars.tsx +106 -0
  67. package/src/player/fullScreen.test.tsx +70 -0
  68. package/src/player/useFullScreen.ts +91 -0
  69. package/src/player/useMediaQuery.ts +21 -0
  70. package/src/steps/Blocks.test.ts +34 -0
  71. package/src/steps/Blocks.tsx +219 -32
  72. package/src/steps/Box.test.tsx +95 -0
  73. package/src/steps/Markdown.test.tsx +46 -0
  74. package/src/steps/Markdown.tsx +42 -4
  75. package/src/steps/Presence.test.tsx +82 -0
  76. package/src/steps/QuizStepView.test.tsx +79 -0
  77. package/src/steps/QuizStepView.tsx +34 -12
  78. package/src/steps/RevealMotion.ts +66 -0
  79. package/src/steps/SlideStepView.tsx +74 -12
  80. package/src/steps/StepBodyProps.ts +16 -1
  81. package/src/steps/StepView.css +390 -16
  82. package/src/steps/StepView.test.tsx +157 -0
  83. package/src/steps/StepView.tsx +108 -19
  84. package/src/steps/motion.test.ts +98 -0
  85. package/src/steps/motion.ts +178 -0
  86. package/src/steps/visibility.test.ts +24 -0
  87. package/src/transitions/Transitions.css +114 -0
  88. package/src/transitions/plan.ts +258 -0
  89. package/src/transitions/playStepTransition.ts +162 -0
  90. package/src/transitions/transitions.test.ts +274 -0
  91. package/src/types.ts +16 -3
  92. package/es/Transitions.css +0 -55
  93. package/es/Transitions.d.ts +0 -10
  94. package/es/Transitions.js +0 -24
  95. package/src/Transitions.css +0 -55
  96. package/src/Transitions.ts +0 -35
@@ -1,20 +1,25 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import clsx from "clsx";
2
3
  import { Alert, Button } from "@digdir/designsystemet-react";
3
4
  import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
4
5
  import { useSwipeable } from "react-swipeable";
5
6
  import { ProgressBar } from "../chrome/ProgressBar";
6
- import { getRevealSteps } from "../document/cues";
7
+ import { getRevealSteps, isNarratedSlide } from "../document/cues";
7
8
  import { getLocalizedString, localize } from "../i18n";
8
- import { ArrowLeftIcon, ArrowRightIcon, CloseIcon } from "../media/icons";
9
+ import { CloseIcon, ExitFullscreenIcon } from "../media/icons";
9
10
  import { StepBody, StepFrame } from "../steps/StepView";
10
- import { runViewTransition } from "../Transitions";
11
+ import { playStepTransition } from "../transitions/playStepTransition";
11
12
  import { LanguageMenu } from "./LanguageMenu";
13
+ import { PlayerControls, PlayerHeader } from "./PlayerBars";
12
14
  import { StartScreen } from "./StartScreen";
15
+ import { useFullScreen } from "./useFullScreen";
16
+ import { COMPACT_QUERY, useMediaQuery } from "./useMediaQuery";
13
17
  import { useNavigationKeys } from "./useNavigationKeys";
14
18
  import "../steps/StepView.css";
15
19
  import "./CoursePlayer.css";
16
20
  function countReveals(step) {
17
- return step?.document?.type === "slide" ? getRevealSteps(step.document.blocks).length : 0;
21
+ let document = step?.document;
22
+ return document?.type === "slide" ? getRevealSteps(document.blocks, isNarratedSlide(document)).length : 0;
18
23
  }
19
24
  function advancesByItself(step) {
20
25
  let document = step?.document;
@@ -41,9 +46,15 @@ export function CoursePlayer({ course, steps, initialIndex = 0, unlockedIndex =
41
46
  let unlocked = preview ? lastIndex : Math.max(serverUnlocked, unlockedIndex);
42
47
  let isLast = current === lastIndex;
43
48
  let revealCount = countReveals(step);
44
- let pendingReveal = stepsShown < revealCount;
45
- let showStart = !preview && !languageChosen && course.cultureId == null && course.languages.length > 1;
49
+ // The main language is stored as no language, so a learner who is under way has chosen it
50
+ let underWay = initialIndex > 0 || unlockedIndex > 0;
51
+ let showStart = !preview && !languageChosen && !underWay && course.cultureId == null && course.languages.length > 1;
46
52
  let stepKey = step ? `${step.slideId}:${course.cultureId ?? ""}` : "";
53
+ // Where Next and Back go within a slide, from the slide itself: it knows the narration's time
54
+ let [navigation, setNavigation] = useState(null);
55
+ let onRevealNavigation = useCallback((reported) => setNavigation({ stepKey, navigation: reported }), [stepKey]);
56
+ let reveals = navigation?.stepKey === stepKey ? navigation.navigation : null;
57
+ let pendingReveal = reveals ? reveals.next !== null : stepsShown < revealCount;
47
58
  // Steps below the frontier were done before, and can be walked through freely.
48
59
  let gated = !preview && current >= unlocked;
49
60
  let minDuration = gated ? (step?.minDuration ?? 0) : 0;
@@ -66,7 +77,7 @@ export function CoursePlayer({ course, steps, initialIndex = 0, unlockedIndex =
66
77
  }, [current]);
67
78
  let go = useCallback((target, direction) => {
68
79
  movedRef.current = true;
69
- runViewTransition(() => {
80
+ void playStepTransition(() => {
70
81
  setIndex(target);
71
82
  setStepsShown(direction === "backward" ? countReveals(steps[target]) : 0);
72
83
  setDone(false);
@@ -75,8 +86,8 @@ export function CoursePlayer({ course, steps, initialIndex = 0, unlockedIndex =
75
86
  let time = Date.now();
76
87
  setShownAt(time);
77
88
  setNow(time);
78
- }, { direction });
79
- }, [steps]);
89
+ }, { scope: stageRef.current, from: steps[current], to: steps[target], direction });
90
+ }, [steps, current]);
80
91
  async function complete() {
81
92
  if (preview || !progress) {
82
93
  setCompleted(true);
@@ -124,7 +135,7 @@ export function CoursePlayer({ course, steps, initialIndex = 0, unlockedIndex =
124
135
  if (busy || completed || showStart || !step)
125
136
  return;
126
137
  if (pendingReveal) {
127
- setStepsShown((value) => value + 1);
138
+ setStepsShown(reveals?.next ?? stepsShown + 1);
128
139
  return;
129
140
  }
130
141
  if (!canLeave)
@@ -139,8 +150,8 @@ export function CoursePlayer({ course, steps, initialIndex = 0, unlockedIndex =
139
150
  function back() {
140
151
  if (busy || showStart)
141
152
  return;
142
- if (stepsShown > 0 && revealCount > 0) {
143
- setStepsShown((value) => Math.min(value, revealCount) - 1);
153
+ if (reveals ? reveals.previous !== null : stepsShown > 0 && revealCount > 0) {
154
+ setStepsShown(reveals?.previous ?? Math.min(stepsShown, revealCount) - 1);
144
155
  return;
145
156
  }
146
157
  if (current > 0)
@@ -194,6 +205,10 @@ export function CoursePlayer({ course, steps, initialIndex = 0, unlockedIndex =
194
205
  hint = getLocalizedString("Finish this part to continue.");
195
206
  else if (remaining > 0)
196
207
  hint = localize `You can continue in ${remaining} seconds.`;
208
+ let fullScreen = useFullScreen(rootRef);
209
+ // On a phone on its side the header and the buttons take the small size, to leave the slide the height
210
+ let chromeSize = useMediaQuery(COMPACT_QUERY) ? "sm" : undefined;
211
+ let fullScreenLabel = fullScreen.active ? getLocalizedString("Leave full screen") : getLocalizedString("Full screen");
197
212
  let nextLabel = isLast && !pendingReveal ? getLocalizedString("Complete") : getLocalizedString("Next");
198
- return (_jsxs("div", { className: "ob-course-player", "data-mode": mode, ref: rootRef, children: [_jsxs("header", { className: "ob-course-player__header", children: [_jsx("p", { className: "ob-course-player__title", children: course.title }), onCultureChange && course.languages.length > 1 && !showStart && (_jsx(LanguageMenu, { languages: course.languages, cultureId: course.cultureId, onChange: (cultureId) => onCultureChange(cultureId) })), onExit && (_jsx(Button, { variant: "tertiary", "data-size": "sm", icon: true, onClick: onExit, "aria-label": getLocalizedString("Close the course"), title: getLocalizedString("Close the course"), children: _jsx(CloseIcon, {}) }))] }), _jsx(ProgressBar, { length: steps.length, currentIndex: showStart ? -1 : current }), showStart ? (_jsx(StartScreen, { course: course, onChoose: chooseLanguage })) : !step ? (_jsx("div", { className: "ob-course-player__empty", children: _jsx(Alert, { "data-color": "info", children: getLocalizedString("This course has no content yet.") }) })) : (_jsxs(_Fragment, { children: [_jsx("section", { className: "ob-course-step", ref: setStage, tabIndex: -1, "aria-label": localize `Step ${current + 1} of ${steps.length}`, onMouseDown: onSwipeMouseDown, children: _jsx(StepFrame, { category: step.category, branding: branding, children: _jsx(StepBody, { step: step.document, slideId: step.slideId, mode: mode, resolveAsset: resolveAsset, quiz: quiz, stepsShown: stepsShown, subtitles: subtitles, onSubtitlesChange: setSubtitles, onDoneChange: setDone, onMediaEnded: onMediaEnded }, `${step.slideId}:${course.cultureId ?? ""}`) }) }), _jsxs("footer", { className: "ob-course-player__controls", children: [_jsxs(Button, { variant: "secondary", onClick: back, disabled: busy || (current === 0 && stepsShown === 0), "aria-label": getLocalizedString("Back"), children: [_jsx(ArrowLeftIcon, {}), _jsx("span", { className: "ob-course-player__button-text", children: getLocalizedString("Back") })] }), _jsxs("div", { className: "ob-course-player__status", children: [_jsxs("span", { className: "ob-course-player__count", children: [current + 1, " / ", steps.length] }), _jsxs("span", { className: "ob-course-player__note", children: [_jsx("span", { role: "status", className: "ob-course-player__message", children: message }), hint && _jsx("span", { className: "ob-course-player__hint", children: hint })] })] }), _jsxs(Button, { onClick: next, loading: busy, disabled: busy || completed || (!pendingReveal && !canLeave), "aria-label": nextLabel, children: [_jsx("span", { className: "ob-course-player__button-text", children: nextLabel }), !(isLast && !pendingReveal) && _jsx(ArrowRightIcon, {})] })] })] }))] }));
213
+ return (_jsxs("div", { className: clsx("ob-course-player", fullScreen.active && "is-full-screen", fullScreen.immersive && "is-immersive"), "data-mode": mode, ref: rootRef, children: [fullScreen.active && (_jsx(Button, { variant: "secondary", "data-size": "sm", icon: true, className: "ob-course-player__leave-full-screen", onClick: fullScreen.toggle, "aria-label": fullScreenLabel, title: fullScreenLabel, children: _jsx(ExitFullscreenIcon, {}) })), _jsx(PlayerHeader, { title: course.title, size: chromeSize, fullScreenLabel: fullScreenLabel, onFullScreen: fullScreen.toggle, end: onExit && (_jsx(Button, { variant: "tertiary", "data-size": "sm", icon: true, onClick: onExit, "aria-label": getLocalizedString("Close the course"), title: getLocalizedString("Close the course"), children: _jsx(CloseIcon, {}) })), children: onCultureChange && course.languages.length > 1 && !showStart && (_jsx(LanguageMenu, { languages: course.languages, cultureId: course.cultureId ?? course.mainCultureId ?? null, onChange: (cultureId) => onCultureChange(cultureId) })) }), _jsx(ProgressBar, { length: steps.length, currentIndex: showStart ? -1 : current }), showStart ? (_jsx(StartScreen, { course: course, onChoose: chooseLanguage })) : !step ? (_jsx("div", { className: "ob-course-player__empty", children: _jsx(Alert, { "data-color": "info", children: getLocalizedString("This course has no content yet.") }) })) : (_jsxs(_Fragment, { children: [_jsx("section", { className: "ob-course-step", ref: setStage, tabIndex: -1, "aria-label": localize `Step ${current + 1} of ${steps.length}`, onMouseDown: onSwipeMouseDown, children: _jsx(StepFrame, { category: step.category, branding: branding, children: _jsx(StepBody, { step: step.document, slideId: step.slideId, mode: mode, resolveAsset: resolveAsset, quiz: quiz, stepsShown: stepsShown, subtitles: subtitles, onSubtitlesChange: setSubtitles, onDoneChange: setDone, onMediaEnded: onMediaEnded, onRevealNavigation: onRevealNavigation }, `${step.slideId}:${course.cultureId ?? ""}`) }) }), _jsx(PlayerControls, { size: chromeSize, position: current + 1, count: steps.length, message: message, hint: hint, backDisabled: busy || (current === 0 && (reveals ? reveals.previous === null : stepsShown === 0)), nextDisabled: busy || completed || (!pendingReveal && !canLeave), busy: busy, nextLabel: nextLabel, nextArrow: !(isLast && !pendingReveal), onBack: back, onNext: next })] }))] }));
199
214
  }
@@ -0,0 +1,32 @@
1
+ import type { ReactNode } from "react";
2
+ export declare function PlayerHeader({ title, size, fullScreenLabel, onFullScreen, children, end, inert, }: {
3
+ title: string;
4
+ /** Drawn but not operable, and hidden from screen readers: a preview's sample. */
5
+ inert?: boolean;
6
+ /** `sm` on a phone on its side, to leave the step the height. */
7
+ size?: "sm";
8
+ fullScreenLabel: string;
9
+ onFullScreen?(): void;
10
+ /** Buttons between the title and the full screen button, such as the language menu. */
11
+ children?: ReactNode;
12
+ /** After the full screen button, such as closing the course. */
13
+ end?: ReactNode;
14
+ }): import("react").JSX.Element;
15
+ export declare function PlayerControls({ size, position, count, message, hint, backDisabled, nextDisabled, busy, nextLabel, nextArrow, onBack, onNext, inert, }: {
16
+ size?: "sm";
17
+ /** Drawn but not operable, and hidden from screen readers: a preview's sample. */
18
+ inert?: boolean;
19
+ /** The step shown, from 1. */
20
+ position: number;
21
+ count: number;
22
+ message?: string | null;
23
+ hint?: string | null;
24
+ backDisabled?: boolean;
25
+ nextDisabled?: boolean;
26
+ busy?: boolean;
27
+ nextLabel: string;
28
+ /** The arrow on Next; left out on Complete. */
29
+ nextArrow: boolean;
30
+ onBack?(): void;
31
+ onNext?(): void;
32
+ }): import("react").JSX.Element;
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Button } from "@digdir/designsystemet-react";
3
+ import { getLocalizedString } from "../i18n";
4
+ import { ArrowLeftIcon, ArrowRightIcon, FullscreenIcon } from "../media/icons";
5
+ /*
6
+ * The player's header and buttons, apart from the player, so an editor's preview draws the same
7
+ * bars around a step as the learner gets, and the step is left exactly the room it has there.
8
+ */
9
+ export function PlayerHeader({ title, size, fullScreenLabel, onFullScreen, children, end, inert, }) {
10
+ return (_jsxs("header", { className: "ob-course-player__header", "data-size": size, inert: inert, "aria-hidden": inert || undefined, children: [_jsx("p", { className: "ob-course-player__title", children: title }), children, _jsx(Button, { variant: "tertiary", "data-size": "sm", icon: true, onClick: onFullScreen, "aria-label": fullScreenLabel, title: fullScreenLabel, children: _jsx(FullscreenIcon, {}) }), end] }));
11
+ }
12
+ export function PlayerControls({ size, position, count, message, hint, backDisabled, nextDisabled, busy, nextLabel, nextArrow, onBack, onNext, inert, }) {
13
+ return (_jsxs("footer", { className: "ob-course-player__controls", "data-size": size, inert: inert, "aria-hidden": inert || undefined, children: [_jsxs(Button, { variant: "secondary", onClick: onBack, disabled: backDisabled, "aria-label": getLocalizedString("Back"), children: [_jsx(ArrowLeftIcon, {}), _jsx("span", { className: "ob-course-player__button-text", children: getLocalizedString("Back") })] }), _jsxs("div", { className: "ob-course-player__status", children: [_jsxs("span", { className: "ob-course-player__count", children: [position, " / ", count] }), _jsxs("span", { className: "ob-course-player__note", children: [_jsx("span", { role: "status", className: "ob-course-player__message", children: message }), hint && _jsx("span", { className: "ob-course-player__hint", children: hint })] })] }), _jsxs(Button, { onClick: onNext, loading: busy, disabled: nextDisabled, "aria-label": nextLabel, children: [_jsx("span", { className: "ob-course-player__button-text", children: nextLabel }), nextArrow && _jsx(ArrowRightIcon, {})] })] }));
14
+ }
@@ -0,0 +1,17 @@
1
+ import { type RefObject } from "react";
2
+ /**
3
+ * How full screen is done here: `native` asks the browser to show the element on the whole screen
4
+ * (the Fullscreen API, prefixed or not); `immersive` lays the player over the whole page itself,
5
+ * for browsers that only put videos on the screen, such as Safari on iPhone.
6
+ */
7
+ export declare function fullScreenKind(doc?: Document): "native" | "immersive";
8
+ /**
9
+ * The player's full screen: one switch for the learner whichever way the browser does it. Native
10
+ * full screen follows the browser (Escape or the system's back gesture leave it); the immersive
11
+ * mode is left with the same button or Escape, and keeps the page behind it from scrolling.
12
+ */
13
+ export declare function useFullScreen(root: RefObject<HTMLElement | null>): {
14
+ active: boolean;
15
+ immersive: boolean;
16
+ toggle: () => void;
17
+ };
@@ -0,0 +1,83 @@
1
+ import { useCallback, useEffect, useState } from "react";
2
+ /**
3
+ * How full screen is done here: `native` asks the browser to show the element on the whole screen
4
+ * (the Fullscreen API, prefixed or not); `immersive` lays the player over the whole page itself,
5
+ * for browsers that only put videos on the screen, such as Safari on iPhone.
6
+ */
7
+ export function fullScreenKind(doc = document) {
8
+ let webkit = doc;
9
+ let element = doc.documentElement;
10
+ let enabled = doc.fullscreenEnabled === true || webkit.webkitFullscreenEnabled === true;
11
+ let canRequest = typeof element.requestFullscreen === "function" || typeof element.webkitRequestFullscreen === "function";
12
+ return enabled && canRequest ? "native" : "immersive";
13
+ }
14
+ function fullscreenElement(doc) {
15
+ return doc.fullscreenElement ?? doc.webkitFullscreenElement ?? null;
16
+ }
17
+ async function requestNative(element) {
18
+ let target = element;
19
+ if (typeof target.requestFullscreen === "function")
20
+ await target.requestFullscreen();
21
+ else
22
+ await target.webkitRequestFullscreen?.();
23
+ }
24
+ async function exitNative(doc) {
25
+ let webkit = doc;
26
+ if (typeof doc.exitFullscreen === "function")
27
+ await doc.exitFullscreen();
28
+ else
29
+ await webkit.webkitExitFullscreen?.();
30
+ }
31
+ /**
32
+ * The player's full screen: one switch for the learner whichever way the browser does it. Native
33
+ * full screen follows the browser (Escape or the system's back gesture leave it); the immersive
34
+ * mode is left with the same button or Escape, and keeps the page behind it from scrolling.
35
+ */
36
+ export function useFullScreen(root) {
37
+ let [immersive, setImmersive] = useState(false);
38
+ let [native, setNative] = useState(false);
39
+ useEffect(() => {
40
+ let sync = () => setNative(root.current != null && fullscreenElement(document) === root.current);
41
+ document.addEventListener("fullscreenchange", sync);
42
+ document.addEventListener("webkitfullscreenchange", sync);
43
+ return () => {
44
+ document.removeEventListener("fullscreenchange", sync);
45
+ document.removeEventListener("webkitfullscreenchange", sync);
46
+ };
47
+ }, [root]);
48
+ useEffect(() => {
49
+ if (!immersive)
50
+ return;
51
+ let page = document.documentElement;
52
+ let overflow = page.style.overflow;
53
+ page.style.overflow = "hidden";
54
+ let onKey = (event) => {
55
+ if (event.key === "Escape")
56
+ setImmersive(false);
57
+ };
58
+ document.addEventListener("keydown", onKey);
59
+ return () => {
60
+ page.style.overflow = overflow;
61
+ document.removeEventListener("keydown", onKey);
62
+ };
63
+ }, [immersive]);
64
+ let toggle = useCallback(() => {
65
+ let element = root.current;
66
+ if (!element)
67
+ return;
68
+ if (native) {
69
+ exitNative(document).catch(() => { });
70
+ }
71
+ else if (immersive) {
72
+ setImmersive(false);
73
+ }
74
+ else if (fullScreenKind() === "native") {
75
+ // A refusal (no user gesture, a frame without permission) falls back to the immersive mode
76
+ requestNative(element).catch(() => setImmersive(true));
77
+ }
78
+ else {
79
+ setImmersive(true);
80
+ }
81
+ }, [root, native, immersive]);
82
+ return { active: native || immersive, immersive, toggle };
83
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A phone on its side: little height, so the player's chrome shrinks and the slide's text scales
3
+ * with the height. The stylesheet has the same query for what CSS can do alone.
4
+ */
5
+ export declare const COMPACT_QUERY = "(orientation: landscape) and (max-height: 500px)";
6
+ /** Whether a media query matches, kept up to date. False where `matchMedia` is missing. */
7
+ export declare function useMediaQuery(query: string): boolean;
@@ -0,0 +1,16 @@
1
+ import { useSyncExternalStore } from "react";
2
+ /**
3
+ * A phone on its side: little height, so the player's chrome shrinks and the slide's text scales
4
+ * with the height. The stylesheet has the same query for what CSS can do alone.
5
+ */
6
+ export const COMPACT_QUERY = "(orientation: landscape) and (max-height: 500px)";
7
+ /** Whether a media query matches, kept up to date. False where `matchMedia` is missing. */
8
+ export function useMediaQuery(query) {
9
+ return useSyncExternalStore((onChange) => {
10
+ if (typeof matchMedia !== "function")
11
+ return () => { };
12
+ let list = matchMedia(query);
13
+ list.addEventListener?.("change", onChange);
14
+ return () => list.removeEventListener?.("change", onChange);
15
+ }, () => typeof matchMedia === "function" && matchMedia(query).matches, () => false);
16
+ }
@@ -1,14 +1,54 @@
1
1
  import type React from "react";
2
2
  import { type RevealState } from "../document/cues";
3
- import type { Block, ContentBlock } from "../document/schema";
3
+ import type { PartEffect } from "../document/effects";
4
+ import type { Block, BoxContentBlock, ContentBlock, ListItem, Timed } from "../document/schema";
4
5
  import type { ResolveAsset } from "../types";
5
6
  export interface BlocksContext {
6
7
  reveal: RevealState;
7
8
  resolveAsset: ResolveAsset;
8
9
  subtitles: boolean;
9
10
  onSubtitlesChange(subtitles: boolean): void;
11
+ /** What the slide's effects do to each part they target, from `getPartEffects`. */
12
+ effects?: ReadonlyMap<string, PartEffect>;
13
+ /** The learner prefers reduced motion: parts change at once. */
14
+ calm?: boolean;
15
+ /** For an editor: every part is drawn whatever its timing, marked `is-unrevealed` when it would wait. */
16
+ revealAll?: boolean;
17
+ /** For an editor: drawn at each place a block can be added, before every shown block and at the end. */
18
+ renderInsert?(place: InsertPlace): React.ReactNode;
10
19
  }
11
- export declare function Blocks({ blocks, context }: {
12
- blocks: Array<Block | ContentBlock>;
20
+ /** Where blocks stand: on the slide, in a column or in a box. */
21
+ export type InsertContainer = {
22
+ kind: "slide";
23
+ } | {
24
+ kind: "column";
25
+ id: string;
26
+ } | {
27
+ kind: "box";
28
+ id: string;
29
+ };
30
+ /** A place a block can be added: in a container, before the block at `index` (or at the end). */
31
+ export type InsertPlace = {
32
+ container: InsertContainer;
33
+ index: number;
34
+ };
35
+ export declare function Blocks({ blocks, context, container, }: {
36
+ blocks: Array<Block | ContentBlock | BoxContentBlock>;
13
37
  context: BlocksContext;
38
+ /** Where the blocks stand, for an editor's places to add one. */
39
+ container?: InsertContainer;
14
40
  }): React.JSX.Element;
41
+ type Visibility = "shown" | "pending" | "gone";
42
+ /**
43
+ * A part that has not been revealed yet keeps its place, so the slide does not jump when it
44
+ * appears. A part with `hideAt` is a passing one: it takes no place before it shows or after it
45
+ * has gone, so parts that replace each other in time stand in the same spot.
46
+ */
47
+ export declare function getVisibility(part: Timed, reveal: RevealState): Visibility;
48
+ type ItemNode = {
49
+ item: ListItem;
50
+ children: ItemNode[];
51
+ };
52
+ /** The flat items as a tree: an indented item belongs to the nearest item above it with a smaller indent. */
53
+ export declare function nestItems(items: ListItem[]): ItemNode[];
54
+ export {};
@@ -1,67 +1,155 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Button, Dialog } from "@digdir/designsystemet-react";
3
3
  import clsx from "clsx";
4
- import { useState } from "react";
4
+ import { Fragment, useState } from "react";
5
5
  import { isVisible } from "../document/cues";
6
6
  import { getLocalizedString } from "../i18n";
7
7
  import { PlayIcon } from "../media/icons";
8
8
  import { VideoPlayer } from "../media/VideoPlayer";
9
9
  import { Markdown } from "./Markdown";
10
- export function Blocks({ blocks, context }) {
11
- return (_jsx(_Fragment, { children: blocks.map((block) => (_jsx(BlockView, { block: block, context: context }, block.id))) }));
10
+ import { aspectRatio, partStyle } from "./motion";
11
+ import { usePresence } from "./RevealMotion";
12
+ export function Blocks({ blocks, context, container, }) {
13
+ let insert = container && context.renderInsert ? context.renderInsert : null;
14
+ return (_jsxs(_Fragment, { children: [blocks.map((block, index) => (_jsxs(Fragment, { children: [insert && container && block.type !== "effect" && (_jsx("div", { className: "ob-course-insert", children: insert({ container, index }) })), _jsx(BlockView, { block: block, context: context })] }, block.id))), insert && container && _jsx("div", { className: "ob-course-insert", children: insert({ container, index: blocks.length }) })] }));
12
15
  }
13
16
  /**
14
17
  * A part that has not been revealed yet keeps its place, so the slide does not jump when it
15
- * appears. A part removed by `hideAt` gives its place up to whatever comes after it.
18
+ * appears. A part with `hideAt` is a passing one: it takes no place before it shows or after it
19
+ * has gone, so parts that replace each other in time stand in the same spot.
16
20
  */
17
- function getVisibility(part, reveal) {
21
+ export function getVisibility(part, reveal) {
18
22
  if (isVisible(part, reveal))
19
23
  return "shown";
20
- if (part.hideAt && reveal.startedCues.has(part.hideAt))
21
- return "gone";
22
- return "pending";
24
+ return part.hideAt ? "gone" : "pending";
23
25
  }
24
- function partProps(part, visibility, className) {
25
- let pending = visibility === "pending";
26
+ function lookClasses(block) {
27
+ return clsx(block.caps && "ob-course-look--caps", block.shadow && "ob-course-look--shadow");
28
+ }
29
+ /** A callout and a box share one surface, coloured by `data-color` and drawn by its variant. */
30
+ function surfaceClasses(className, block) {
31
+ return clsx(className, "ob-course-surface", `ob-course-surface--${block.variant ?? "bordered"}`);
32
+ }
33
+ /**
34
+ * How a part is drawn now, or null when it is not in the page. A part that goes away fades out
35
+ * first (`leaving`); the slide lifts it out of the flow meanwhile.
36
+ */
37
+ function usePart(part, context) {
38
+ let visibility = context.revealAll ? "shown" : getVisibility(part, context.reveal);
39
+ let presence = usePresence(visibility !== "gone", context.calm ?? false);
40
+ let inPage = presence !== "absent";
41
+ /*
42
+ * The view stays mounted while its part is out of the page, so a part that comes in later is one
43
+ * whose view saw it absent: it fades in. Parts there when the slide is drawn do not.
44
+ */
45
+ let [cameLate, setCameLate] = useState(false);
46
+ let [wasInPage, setWasInPage] = useState(inPage);
47
+ if (inPage !== wasInPage) {
48
+ setWasInPage(inPage);
49
+ if (inPage)
50
+ setCameLate(true);
51
+ }
52
+ if (!inPage)
53
+ return null;
54
+ let state = presence === "leaving" ? "leaving" : visibility;
55
+ // A part that waits keeps the fade for when it shows; the fade on mount would end unseen
56
+ return { state, entering: cameLate && state !== "pending" };
57
+ }
58
+ function partProps(part, shown, className, context, canvasAspect) {
59
+ let hidden = shown.state !== "shown";
26
60
  return {
27
- className: clsx(className, pending && "is-pending"),
61
+ className: clsx(className, shown.state === "pending" && "is-pending", shown.state === "leaving" && "is-leaving", shown.entering && "is-entering", context.revealAll && !isVisible(part, context.reveal) && "is-unrevealed"),
28
62
  "data-part": part.id,
29
- "aria-hidden": pending || undefined,
30
- inert: pending || undefined,
63
+ "aria-hidden": hidden || undefined,
64
+ inert: hidden || undefined,
65
+ style: partStyle({ effect: context.effects?.get(part.id), delay: part.delay, state: shown.state, canvasAspect }),
31
66
  };
32
67
  }
33
68
  function BlockView({ block, context }) {
34
- let visibility = getVisibility(block, context.reveal);
35
- if (visibility === "gone")
69
+ let shown = usePart(block, context);
70
+ // An effect shows nothing itself: it acts on its targets through `context.effects`
71
+ if (!shown || block.type === "effect")
36
72
  return null;
37
- let props = partProps(block, visibility, `ob-course-block ob-course-block--${block.type}`);
73
+ let props = partProps(block, shown, `ob-course-block ob-course-block--${block.type}`, context);
38
74
  switch (block.type) {
39
75
  case "heading": {
40
76
  let Tag = `h${(block.level ?? 1) + 1}`;
41
- return (_jsx(Tag, { ...props, className: clsx(props.className, `ob-course-heading--${block.level ?? 1}`), children: block.text }));
77
+ return (_jsx(Tag, { ...props, className: clsx(props.className, `ob-course-heading--${block.level ?? 1}`, lookClasses(block)), children: _jsx(Markdown, { inline: true, children: block.text }) }));
42
78
  }
43
79
  case "text":
44
- return (_jsx("div", { ...props, children: _jsx(Markdown, { children: block.text }) }));
80
+ return (_jsx("div", { ...props, className: clsx(props.className, lookClasses(block)), children: _jsx(Markdown, { children: block.text }) }));
45
81
  case "list": {
46
82
  let Tag = block.ordered ? "ol" : "ul";
47
- return (_jsx(Tag, { ...props, children: block.items.map((item) => {
48
- let itemVisibility = getVisibility(item, context.reveal);
49
- if (itemVisibility === "gone")
50
- return null;
51
- return (_jsx("li", { ...partProps(item, itemVisibility, "ob-course-list-item"), children: _jsx(Markdown, { className: "ob-course-markdown--inline", children: item.text }) }, item.id));
52
- }) }));
83
+ return (_jsx(Tag, { ...props, children: _jsx(ListItems, { nodes: nestItems(block.items), context: context }) }));
53
84
  }
54
85
  case "image":
55
86
  return (_jsxs("figure", { ...props, className: clsx(props.className, block.size && `ob-course-image--${block.size}`), "data-size": block.size, children: [_jsx("img", { src: context.resolveAsset(block.src), alt: block.alt ?? "", loading: "lazy" }), block.caption && _jsx("figcaption", { children: block.caption })] }));
56
87
  case "quote":
57
88
  return (_jsxs("figure", { ...props, children: [_jsx("blockquote", { children: _jsx(Markdown, { children: block.text }) }), block.cite && _jsx("figcaption", { children: block.cite })] }));
58
89
  case "callout":
59
- return (_jsx("div", { ...props, "data-color": block.tone ?? "neutral", children: _jsx(Markdown, { children: block.text }) }));
90
+ return (_jsx("div", { ...props, className: surfaceClasses(props.className, block), "data-color": block.tone ?? "neutral", children: _jsx(Markdown, { children: block.text }) }));
91
+ case "box":
92
+ return (_jsx("div", { ...props, className: surfaceClasses(props.className, block), "data-color": block.tone ?? "neutral", children: _jsx(Blocks, { blocks: block.blocks, context: context, container: { kind: "box", id: block.id } }) }));
60
93
  case "video-link":
61
94
  return (_jsx("div", { ...props, children: _jsx(VideoLink, { block: block, context: context }) }));
95
+ case "canvas":
96
+ return (_jsx("div", { ...props, children: _jsx("div", { className: "ob-course-canvas", "data-color-scheme": "light", style: { aspectRatio: block.aspect ?? "16 / 9" }, children: block.items.map((item) => (_jsx(CanvasItemView, { item: item, aspect: aspectRatio(block.aspect), context: context }, item.id))) }) }));
62
97
  case "columns":
63
- return (_jsx("div", { ...props, children: block.columns.map((column) => (_jsx("div", { className: "ob-course-column", "data-part": column.id, children: _jsx(Blocks, { blocks: column.blocks, context: context }) }, column.id))) }));
98
+ return (_jsx("div", { ...props, children: block.columns.map((column) => (_jsx("div", { className: "ob-course-column", "data-part": column.id, style: partStyle({ effect: context.effects?.get(column.id), state: "shown" }), children: _jsx(Blocks, { blocks: column.blocks, context: context, container: { kind: "column", id: column.id } }) }, column.id))) }));
99
+ }
100
+ }
101
+ /** The flat items as a tree: an indented item belongs to the nearest item above it with a smaller indent. */
102
+ export function nestItems(items) {
103
+ let roots = [];
104
+ let path = [];
105
+ for (let item of items) {
106
+ // An item cannot be more than one level under the one above it
107
+ let depth = Math.min(item.indent ?? 0, path.length);
108
+ let node = { item, children: [] };
109
+ if (depth === 0)
110
+ roots.push(node);
111
+ else
112
+ path[depth - 1].children.push(node);
113
+ path.length = depth;
114
+ path.push(node);
115
+ }
116
+ return roots;
117
+ }
118
+ function ListItems({ nodes, context }) {
119
+ return nodes.map((node) => _jsx(ListItemView, { node: node, context: context }, node.item.id));
120
+ }
121
+ function ListItemView({ node: { item, children }, context }) {
122
+ let shown = usePart(item, context);
123
+ if (!shown)
124
+ return null;
125
+ return (_jsxs("li", { ...partProps(item, shown, "ob-course-list-item", context), children: [_jsx(Markdown, { inline: true, className: "ob-course-markdown--inline", children: item.text }), children.length > 0 && (_jsx("ul", { className: "ob-course-sublist", children: _jsx(ListItems, { nodes: children, context: context }) }))] }));
126
+ }
127
+ const PLACE_PROPERTIES = {
128
+ x: "left",
129
+ y: "top",
130
+ right: "right",
131
+ bottom: "bottom",
132
+ width: "width",
133
+ height: "height",
134
+ };
135
+ function canvasPlace(item) {
136
+ let style = {};
137
+ for (let [key, property] of Object.entries(PLACE_PROPERTIES)) {
138
+ let value = item[key];
139
+ if (value != null)
140
+ style[property] = `${value}%`;
141
+ }
142
+ return style;
143
+ }
144
+ function CanvasItemView({ item, aspect, context }) {
145
+ let shown = usePart(item, context);
146
+ if (!shown)
147
+ return null;
148
+ let props = partProps(item, shown, `ob-course-canvas__item ob-course-canvas__${item.type}`, context, aspect);
149
+ if (item.type === "image") {
150
+ return (_jsx("img", { ...props, style: { ...canvasPlace(item), ...props.style }, src: context.resolveAsset(item.src), alt: item.alt ?? "" }));
64
151
  }
152
+ return (_jsx("div", { ...props, className: clsx(props.className, item.surface && `ob-course-canvas__text--${item.surface}`), "data-color": item.tone, style: { ...canvasPlace(item), ...props.style, "--ob-course-canvas-size": item.size }, children: _jsx(Markdown, { children: item.text }) }));
65
153
  }
66
154
  function VideoLink({ block, context, }) {
67
155
  let [open, setOpen] = useState(false);
@@ -1,4 +1,10 @@
1
- export declare function Markdown({ children, className }: {
1
+ /** The tags a text may use for what Markdown has no syntax for. They take no attributes. */
2
+ export declare const TEXT_TAGS: Set<string>;
3
+ /** The colours a `<span data-color>` may name; they come from the theme. */
4
+ export declare const TEXT_TONES: Set<string>;
5
+ /** `inline`: one line without paragraphs, for a heading. */
6
+ export declare function Markdown({ children, className, inline }: {
2
7
  children: string;
3
8
  className?: string;
9
+ inline?: boolean;
4
10
  }): import("react").JSX.Element;
@@ -1,13 +1,40 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import MarkdownToJSX from "markdown-to-jsx";
3
+ import { createElement, Fragment } from "react";
4
+ /** What Markdown itself produces. */
5
+ const MARKDOWN_TAGS = new Set("p strong em del a ul ol li blockquote code pre br hr h1 h2 h3 h4 h5 h6 span div img table thead tbody tr th td input".split(" "));
6
+ /** The tags a text may use for what Markdown has no syntax for. They take no attributes. */
7
+ export const TEXT_TAGS = new Set(["big", "small", "u", "sup", "sub", "mark"]);
8
+ /** The colours a `<span data-color>` may name; they come from the theme. */
9
+ export const TEXT_TONES = new Set(["info", "success", "warning", "danger", "brand"]);
3
10
  const OPTIONS = {
4
11
  forceBlock: true,
5
- // Course text is written in the editor, not as HTML; raw tags are shown as text.
6
- disableParsingRawHTML: true,
12
+ // A single paragraph is otherwise returned bare, without the element that carries the class
13
+ forceWrapper: true,
7
14
  overrides: {
8
15
  a: { props: { target: "_blank", rel: "noreferrer" } },
9
16
  },
17
+ // Course text is Markdown, not HTML: of the tags written into it only the few in TEXT_TAGS
18
+ // are drawn, and without their attributes, plus a span that names one of the theme's colours.
19
+ // Any other tag leaves just its text.
20
+ createElement(type, props, ...children) {
21
+ let tone = type === "span" ? props?.["data-color"] : undefined;
22
+ if (typeof tone === "string" && TEXT_TONES.has(tone)) {
23
+ return createElement("span", { key: props?.key, className: "ob-course-tone", "data-color": tone }, ...children);
24
+ }
25
+ // Any other span keeps its class (the wrapper of an inline text has one) and nothing else
26
+ if (type === "span")
27
+ return createElement("span", { key: props?.key, className: props?.className }, ...children);
28
+ if (typeof type === "string" && TEXT_TAGS.has(type))
29
+ return createElement(type, { key: props?.key }, ...children);
30
+ if (typeof type === "string" && !MARKDOWN_TAGS.has(type)) {
31
+ return createElement(Fragment, { key: props?.key }, ...children);
32
+ }
33
+ return createElement(type, props, ...children);
34
+ },
10
35
  };
11
- export function Markdown({ children, className }) {
12
- return (_jsx(MarkdownToJSX, { className: className ? `ob-course-markdown ${className}` : "ob-course-markdown", options: OPTIONS, children: children }));
36
+ const INLINE_OPTIONS = { ...OPTIONS, forceBlock: false, forceInline: true };
37
+ /** `inline`: one line without paragraphs, for a heading. */
38
+ export function Markdown({ children, className, inline }) {
39
+ return (_jsx(MarkdownToJSX, { className: className ? `ob-course-markdown ${className}` : "ob-course-markdown", options: (inline ? INLINE_OPTIONS : OPTIONS), children: children }));
13
40
  }
@@ -4,7 +4,8 @@ import type { StepBodyProps } from "./StepBodyProps";
4
4
  import "./QuizStepView.css";
5
5
  /**
6
6
  * A quiz, one question at a time with feedback after each answer, graded by the server.
7
- * Done when an attempt has passed.
7
+ * Done when an attempt has passed. With `retryUntilCorrect` a wrong answer only says so and the
8
+ * learner answers the same question again; the next question comes after the right answer.
8
9
  */
9
10
  export declare function QuizStepView({ step, slideId, quiz, mode, resolveAsset, subtitles, onSubtitlesChange, onDoneChange, }: StepBodyProps & {
10
11
  step: QuizStep;