@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,9 +1,15 @@
1
- import { Alert } from "@digdir/designsystemet-react";
1
+ import { Alert, Button } from "@digdir/designsystemet-react";
2
+ import clsx from "clsx";
2
3
  import { type ReactNode, useLayoutEffect, useState } from "react";
3
4
  import { CategoryHeader } from "../chrome/CategoryHeader";
5
+ import type { RevealNavigation } from "../document/cues";
4
6
  import type { Step } from "../document/schema";
5
- import { getLocalizedString } from "../i18n";
6
- import type { PlayerBranding, PlayerCategory, PlayerMode, QuizAdapter, ResolveAsset } from "../types";
7
+ import { ProgressBar } from "../chrome/ProgressBar";
8
+ import { getLocalizedString, localize } from "../i18n";
9
+ import { CloseIcon } from "../media/icons";
10
+ import { LanguageMenu } from "../player/LanguageMenu";
11
+ import { PlayerControls, PlayerHeader } from "../player/PlayerBars";
12
+ import type { PlayerBranding, PlayerCategory, PlayerLanguage, PlayerMode, QuizAdapter, ResolveAsset } from "../types";
7
13
  import { PdfStepView } from "./PdfStepView";
8
14
  import { QuizStepView } from "./QuizStepView";
9
15
  import { SlideStepView } from "./SlideStepView";
@@ -11,6 +17,7 @@ import type { StepBodyProps } from "./StepBodyProps";
11
17
  import { VideoStepView } from "./VideoStepView";
12
18
 
13
19
  import "./StepView.css";
20
+ import "../player/CoursePlayer.css";
14
21
 
15
22
  /** Picks the view for a step's type. A missing document is shown as not available, and counts as done. */
16
23
  export function StepBody({ step, ...props }: StepBodyProps & { step: Step | null }) {
@@ -68,8 +75,13 @@ export interface StepViewProps {
68
75
  quiz?: QuizAdapter;
69
76
  /** The slide the step belongs to, passed to the quiz adapter. */
70
77
  slideId?: number;
71
- /** How many `reveal: "step"` parts to show. Every part is shown when left out. */
78
+ /** How many `reveal: "step"` parts count as pressed. Every part is shown when left out. */
72
79
  stepsShown?: number;
80
+ /**
81
+ * Where Next and Back would take a slide from `stepsShown`, skipping presses that show nothing
82
+ * new, and how many presses show something: for a stepper that counts the way the player does.
83
+ */
84
+ onRevealNavigation?(navigation: RevealNavigation): void;
73
85
  /** The band above the step naming its part of the course, as the player shows it. None when left out. */
74
86
  category?: PlayerCategory | null;
75
87
  /**
@@ -77,9 +89,35 @@ export interface StepViewProps {
77
89
  * four product logos; given without a logo, the company's name.
78
90
  */
79
91
  branding?: PlayerBranding | null;
92
+ /**
93
+ * For an editor: draws every part of a slide whatever its timing, without effects, and marks the
94
+ * parts that would not be shown yet with `is-unrevealed`. Off (the default) it is the player.
95
+ */
96
+ revealAll?: boolean;
97
+ /** For an editor: a node drawn at each place on a slide where a block can be added. */
98
+ renderInsert?: StepBodyProps["renderInsert"];
99
+ /**
100
+ * For an editor: the player's header, progress bar and buttons around the step, as a learner has
101
+ * them, so the step gets the room it has in the player. They are drawn but cannot be used.
102
+ */
103
+ courseChrome?: SampleChrome | null;
80
104
  }
81
105
 
82
- /** One step on its own, without the course chrome: for previewing a step while it is edited. */
106
+ /** What the player's chrome shows around a previewed step. */
107
+ export interface SampleChrome {
108
+ /** The course's title, in the header. */
109
+ title: string;
110
+ /** The step's place in the course, from 1. */
111
+ position: number;
112
+ count: number;
113
+ /** The course's languages: the header has the language menu when there are several. */
114
+ languages?: PlayerLanguage[];
115
+ cultureId?: string | null;
116
+ /** The slim chrome of a phone on its side. */
117
+ compact?: boolean;
118
+ }
119
+
120
+ /** One step on its own, for previewing a step while it is edited, with or without the player's chrome. */
83
121
  export function StepView({
84
122
  step,
85
123
  resolveAsset,
@@ -89,24 +127,75 @@ export function StepView({
89
127
  stepsShown,
90
128
  category,
91
129
  branding,
130
+ onRevealNavigation,
131
+ revealAll,
132
+ renderInsert,
133
+ courseChrome,
92
134
  }: StepViewProps) {
93
135
  let [subtitles, setSubtitles] = useState(true);
94
136
 
137
+ let frame = (
138
+ <StepFrame category={category} branding={branding}>
139
+ <StepBody
140
+ step={step}
141
+ slideId={slideId}
142
+ mode={mode}
143
+ resolveAsset={resolveAsset}
144
+ quiz={quiz}
145
+ stepsShown={stepsShown ?? Number.POSITIVE_INFINITY}
146
+ subtitles={subtitles}
147
+ onSubtitlesChange={setSubtitles}
148
+ onDoneChange={ignore}
149
+ onRevealNavigation={onRevealNavigation}
150
+ revealAll={revealAll}
151
+ renderInsert={renderInsert}
152
+ />
153
+ </StepFrame>
154
+ );
155
+
156
+ if (!courseChrome) return <div className="ob-course-step ob-course-step--standalone">{frame}</div>;
157
+ return <SampleChromeView chrome={courseChrome}>{frame}</SampleChromeView>;
158
+ }
159
+
160
+ /** The player's own bars around a step, inert: a tester sees them but cannot move through the course. */
161
+ function SampleChromeView({ chrome, children }: { chrome: SampleChrome; children: ReactNode }) {
162
+ let { title, position, count, languages = [], cultureId = null, compact = false } = chrome;
163
+ let size = compact ? ("sm" as const) : undefined;
164
+ let last = position >= count;
95
165
  return (
96
- <div className="ob-course-step ob-course-step--standalone">
97
- <StepFrame category={category} branding={branding}>
98
- <StepBody
99
- step={step}
100
- slideId={slideId}
101
- mode={mode}
102
- resolveAsset={resolveAsset}
103
- quiz={quiz}
104
- stepsShown={stepsShown ?? Number.POSITIVE_INFINITY}
105
- subtitles={subtitles}
106
- onSubtitlesChange={setSubtitles}
107
- onDoneChange={ignore}
108
- />
109
- </StepFrame>
166
+ <div className={clsx("ob-course-player", compact && "is-compact")} data-mode="preview">
167
+ <PlayerHeader
168
+ title={title}
169
+ size={size}
170
+ fullScreenLabel={getLocalizedString("Full screen")}
171
+ inert
172
+ end={
173
+ <Button
174
+ variant="tertiary"
175
+ data-size="sm"
176
+ icon
177
+ aria-label={getLocalizedString("Close the course")}
178
+ title={getLocalizedString("Close the course")}
179
+ >
180
+ <CloseIcon />
181
+ </Button>
182
+ }
183
+ >
184
+ {languages.length > 1 && <LanguageMenu languages={languages} cultureId={cultureId} onChange={ignore} />}
185
+ </PlayerHeader>
186
+ <ProgressBar length={count} currentIndex={position - 1} />
187
+ <section className="ob-course-step" aria-label={localize`Step ${position} of ${count}`}>
188
+ {children}
189
+ </section>
190
+ <PlayerControls
191
+ size={size}
192
+ position={position}
193
+ count={count}
194
+ backDisabled={position <= 1}
195
+ nextLabel={last ? getLocalizedString("Complete") : getLocalizedString("Next")}
196
+ nextArrow={!last}
197
+ inert
198
+ />
110
199
  </div>
111
200
  );
112
201
  }
@@ -0,0 +1,98 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { aspectRatio, getGlides, type PartBox, partStyle } from "./motion";
3
+
4
+ function box(x: number, y: number, extra: Partial<PartBox> = {}): PartBox {
5
+ return { x, y, width: 100, height: 20, shown: true, parent: null, ...extra };
6
+ }
7
+
8
+ describe("getGlides", () => {
9
+ it("glides the parts that were pushed, from where they were", () => {
10
+ let before = new Map([
11
+ ["h", box(0, 0)],
12
+ ["t", box(0, 30)],
13
+ ]);
14
+ let after = new Map([
15
+ ["h", box(0, 0)],
16
+ ["new", box(0, 30)],
17
+ ["t", box(0, 60)],
18
+ ]);
19
+ expect(getGlides(before, after)).toEqual([{ id: "t", dx: 0, dy: -30 }]);
20
+ });
21
+
22
+ it("leaves out parts that just appeared, cannot be seen, or barely moved", () => {
23
+ let before = new Map([
24
+ ["a", box(0, 0, { shown: false })],
25
+ ["b", box(0, 10)],
26
+ ["c", box(0, 20)],
27
+ ]);
28
+ let after = new Map([
29
+ ["a", box(0, 40)],
30
+ ["b", box(0, 50, { shown: false })],
31
+ ["c", box(0, 20.3)],
32
+ ]);
33
+ expect(getGlides(before, after)).toEqual([]);
34
+ });
35
+
36
+ it("glides a part inside a gliding one only by what it moved within it", () => {
37
+ let before = new Map([
38
+ ["l", box(0, 0)],
39
+ ["i1", box(10, 0, { parent: "l" })],
40
+ ["i2", box(10, 20, { parent: "l" })],
41
+ ]);
42
+ let after = new Map([
43
+ ["l", box(0, 50)],
44
+ ["i0", box(10, 50, { parent: "l" })],
45
+ ["i1", box(10, 70, { parent: "l" })],
46
+ ["i2", box(10, 90, { parent: "l" })],
47
+ ]);
48
+ expect(getGlides(before, after)).toEqual([
49
+ { id: "l", dx: 0, dy: -50 },
50
+ { id: "i1", dx: 0, dy: -20 },
51
+ { id: "i2", dx: 0, dy: -20 },
52
+ ]);
53
+ });
54
+
55
+ it("measures a nested part against the nearest part around it that glides", () => {
56
+ let before = new Map([
57
+ ["cols", box(0, 0)],
58
+ ["left", box(0, 0, { parent: "cols" })],
59
+ ["t", box(0, 0, { parent: "left" })],
60
+ ]);
61
+ let after = new Map([
62
+ ["cols", box(0, 40)],
63
+ ["left", box(0, 40, { parent: "cols" })],
64
+ ["t", box(0, 40, { parent: "left" })],
65
+ ]);
66
+ expect(getGlides(before, after)).toEqual([{ id: "cols", dx: 0, dy: -40 }]);
67
+ });
68
+ });
69
+
70
+ describe("partStyle", () => {
71
+ let effect = { x: 2, y: -1, scale: 0.5, opacity: 0.4, delay: 100, duration: 600 };
72
+
73
+ it("moves a part in the slide's text size, with the effect's timing", () => {
74
+ let style = partStyle({ effect, state: "shown" });
75
+ expect(style.translate).toBe("calc(2 * var(--ob-course-em)) calc(-1 * var(--ob-course-em))");
76
+ expect(style.scale).toBe("0.5");
77
+ expect(style.filter).toBe("opacity(0.4)");
78
+ expect(style.transition).toBe(
79
+ "translate 600ms ease 100ms, scale 600ms ease 100ms, filter 600ms ease 100ms, opacity 300ms ease",
80
+ );
81
+ });
82
+
83
+ it("moves a canvas item in percent of the canvas", () => {
84
+ let style = partStyle({ effect, state: "shown", canvasAspect: 2 });
85
+ expect(style.translate).toBe("2cqi calc(-1cqi / 2)");
86
+ expect(aspectRatio("4 / 3")).toBeCloseTo(4 / 3);
87
+ expect(aspectRatio(undefined)).toBeCloseTo(16 / 9);
88
+ });
89
+
90
+ it("holds a delayed part back on the way in only", () => {
91
+ expect(partStyle({ delay: 500, state: "shown" })).toEqual({
92
+ transition: "opacity 300ms ease 500ms, visibility 0s linear 500ms",
93
+ animationDelay: "500ms",
94
+ });
95
+ expect(partStyle({ delay: 500, state: "pending" })).toEqual({});
96
+ expect(partStyle({ state: "shown" })).toEqual({});
97
+ });
98
+ });
@@ -0,0 +1,178 @@
1
+ import type { CSSProperties } from "react";
2
+ import type { PartEffect } from "../document/effects";
3
+
4
+ /** The reveal fade, in milliseconds: parts fade in, and passing parts fade out, this fast. */
5
+ export const FADE_MS = 300;
6
+ /** How long a part takes to glide to the place a change of the slide pushed it to. */
7
+ export const GLIDE_MS = 300;
8
+
9
+ /** Where a part stands, relative to the slide, and whether it can be seen there. */
10
+ export interface PartBox {
11
+ x: number;
12
+ y: number;
13
+ width: number;
14
+ height: number;
15
+ /** Neither waiting to be shown nor fading out, itself or by a part around it. */
16
+ shown: boolean;
17
+ /** The nearest part around it, if any. */
18
+ parent: string | null;
19
+ }
20
+
21
+ export interface Glide {
22
+ id: string;
23
+ /** How far the part was from where it is now: it starts there and glides to nothing. */
24
+ dx: number;
25
+ dy: number;
26
+ }
27
+
28
+ /**
29
+ * The parts that a change pushed to another place, with how far, so they can glide there instead
30
+ * of jumping. A part that just appeared fades in where it is, and a part that cannot be seen has
31
+ * nothing to show. A part inside one that glides only glides by what it moved within it, so the
32
+ * two do not add up to too much. `after` lists parts around a part before the part itself, as the
33
+ * document does.
34
+ */
35
+ export function getGlides(before: ReadonlyMap<string, PartBox>, after: ReadonlyMap<string, PartBox>, threshold = 0.5) {
36
+ let glides: Glide[] = [];
37
+ let moved = new Map<string, { dx: number; dy: number }>();
38
+
39
+ for (let [id, box] of after) {
40
+ let old = before.get(id);
41
+ if (!old || !old.shown || !box.shown) continue;
42
+
43
+ let dx = old.x - box.x;
44
+ let dy = old.y - box.y;
45
+ let ancestor = box.parent;
46
+ while (ancestor && !moved.has(ancestor)) ancestor = after.get(ancestor)?.parent ?? null;
47
+ let carried = ancestor ? moved.get(ancestor)! : { dx: 0, dy: 0 };
48
+
49
+ let own = { dx: dx - carried.dx, dy: dy - carried.dy };
50
+ if (Math.abs(own.dx) < threshold && Math.abs(own.dy) < threshold) continue;
51
+ moved.set(id, { dx, dy });
52
+ glides.push({ id, ...own });
53
+ }
54
+ return glides;
55
+ }
56
+
57
+ /** Every part in the slide with its box. Sizes are layout sizes, without an effect's scale. */
58
+ export function measureParts(root: HTMLElement): Map<string, PartBox> {
59
+ let origin = root.getBoundingClientRect();
60
+ let boxes = new Map<string, PartBox>();
61
+ for (let element of root.querySelectorAll<HTMLElement>("[data-part]")) {
62
+ let rect = element.getBoundingClientRect();
63
+ let parent = element.parentElement?.closest<HTMLElement>("[data-part]");
64
+ boxes.set(element.dataset.part!, {
65
+ x: rect.left - origin.left,
66
+ y: rect.top - origin.top,
67
+ width: element.offsetWidth,
68
+ height: element.offsetHeight,
69
+ shown: !element.closest(".is-pending, .is-leaving"),
70
+ parent: parent && root.contains(parent) ? parent.dataset.part! : null,
71
+ });
72
+ }
73
+ return boxes;
74
+ }
75
+
76
+ /**
77
+ * Lifts the parts that started to fade out out of the flow, onto the box they had before the
78
+ * change, so the part coming in their place takes it at once and nothing jumps when they go.
79
+ */
80
+ export function pinLeaving(root: HTMLElement, before: ReadonlyMap<string, PartBox>) {
81
+ let origin = root.getBoundingClientRect();
82
+ for (let element of root.querySelectorAll<HTMLElement>(".is-leaving[data-part]")) {
83
+ if (element.dataset.pinned != null || element.parentElement?.closest(".is-leaving")) continue;
84
+ let box = before.get(element.dataset.part!);
85
+ if (!box || getComputedStyle(element).position === "absolute") continue;
86
+
87
+ element.dataset.pinned = "";
88
+ Object.assign(element.style, {
89
+ position: "absolute",
90
+ margin: "0",
91
+ width: `${box.width}px`,
92
+ height: `${box.height}px`,
93
+ top: "0px",
94
+ left: "0px",
95
+ });
96
+ // Where top and left 0 put it depends on the positioned box around it, so measure and correct
97
+ let now = element.getBoundingClientRect();
98
+ element.style.top = `${box.y - (now.top - origin.top)}px`;
99
+ element.style.left = `${box.x - (now.left - origin.left)}px`;
100
+ }
101
+ }
102
+
103
+ /** Puts a part that came back while it was fading out into the flow again. */
104
+ export function unpinReturned(root: HTMLElement) {
105
+ for (let element of root.querySelectorAll<HTMLElement>("[data-pinned]:not(.is-leaving)")) {
106
+ delete element.dataset.pinned;
107
+ for (let property of ["position", "margin", "width", "height", "top", "left"]) {
108
+ element.style.removeProperty(property);
109
+ }
110
+ }
111
+ }
112
+
113
+ /** Starts the glides on the elements; `composite: "add"` keeps an effect's own move under them. */
114
+ export function runGlides(root: HTMLElement, glides: Glide[]) {
115
+ for (let glide of glides) {
116
+ let element = root.querySelector<HTMLElement>(`[data-part="${CSS.escape(glide.id)}"]`);
117
+ if (typeof element?.animate !== "function") continue;
118
+ element.animate([{ translate: `${glide.dx}px ${glide.dy}px` }, { translate: "0px 0px" }], {
119
+ duration: GLIDE_MS,
120
+ easing: "ease",
121
+ composite: "add",
122
+ });
123
+ }
124
+ }
125
+
126
+ /** Width to height from a canvas's `aspect`, e.g. `16/9`. */
127
+ export function aspectRatio(aspect: string | undefined) {
128
+ let [width, height] = (aspect ?? "16/9").split("/").map((value) => Number(value.trim()));
129
+ return width > 0 && height > 0 ? width / height : 16 / 9;
130
+ }
131
+
132
+ export type PartState = "shown" | "pending" | "leaving";
133
+
134
+ /**
135
+ * The inline style a part's effects and reveal delay give it. A move on a canvas item is in percent
136
+ * of the canvas (`cqi` is a percent of its width; the height is the width over the aspect), on any
137
+ * other part in the slide's text size. The transition lists the reveal fade too, since an inline
138
+ * transition replaces the stylesheet's.
139
+ */
140
+ export function partStyle({
141
+ effect,
142
+ delay,
143
+ state,
144
+ canvasAspect,
145
+ }: {
146
+ effect?: PartEffect;
147
+ /** The part's `delay`: how long after it is due it shows. */
148
+ delay?: number;
149
+ state: PartState;
150
+ /** Set for an item on a canvas: the canvas's width over its height. */
151
+ canvasAspect?: number;
152
+ }): CSSProperties {
153
+ let style: CSSProperties = {};
154
+ let transitions: string[] = [];
155
+
156
+ if (effect) {
157
+ let x = canvasAspect != null ? `${effect.x}cqi` : `calc(${effect.x} * var(--ob-course-em))`;
158
+ let y = canvasAspect != null ? `calc(${effect.y}cqi / ${canvasAspect})` : `calc(${effect.y} * var(--ob-course-em))`;
159
+ style.translate = `${x} ${y}`;
160
+ style.scale = String(effect.scale);
161
+ // A filter, so the effect's opacity multiplies with the reveal fade instead of replacing it
162
+ style.filter = `opacity(${effect.opacity ?? 1})`;
163
+ let timing = `${effect.duration}ms ease ${effect.delay}ms`;
164
+ transitions.push(`translate ${timing}`, `scale ${timing}`, `filter ${timing}`);
165
+ }
166
+
167
+ let wait = state === "shown" ? (delay ?? 0) : 0;
168
+ if (wait > 0) {
169
+ // Only on the way in: a part that is hidden again goes at once
170
+ transitions.push(`opacity ${FADE_MS}ms ease ${wait}ms`, `visibility 0s linear ${wait}ms`);
171
+ style.animationDelay = `${wait}ms`;
172
+ } else if (transitions.length > 0) {
173
+ transitions.push(`opacity ${FADE_MS}ms ease`);
174
+ }
175
+
176
+ if (transitions.length > 0) style.transition = transitions.join(", ");
177
+ return style;
178
+ }
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { getVisibility } from "./Blocks";
3
+
4
+ const NONE = { stepsShown: 0, revealSteps: [], startedCues: new Set<string>() };
5
+ const AFTER = (...cues: string[]) => ({ ...NONE, startedCues: new Set(cues) });
6
+
7
+ describe("getVisibility", () => {
8
+ it("keeps the place of a part that waits to be shown", () => {
9
+ expect(getVisibility({ id: "a", revealAt: "c1" }, NONE)).toBe("pending");
10
+ expect(getVisibility({ id: "a", revealAt: "c1" }, AFTER("c1"))).toBe("shown");
11
+ });
12
+
13
+ it("gives a passing part no place before it shows or after it has gone", () => {
14
+ let scene = { id: "s2", revealAt: "c1", hideAt: "c2" };
15
+ expect(getVisibility(scene, NONE)).toBe("gone");
16
+ expect(getVisibility(scene, AFTER("c1"))).toBe("shown");
17
+ expect(getVisibility(scene, AFTER("c1", "c2"))).toBe("gone");
18
+ });
19
+
20
+ it("shows a part that only goes away from the start", () => {
21
+ expect(getVisibility({ id: "s1", hideAt: "c1" }, NONE)).toBe("shown");
22
+ expect(getVisibility({ id: "s1", hideAt: "c1" }, AFTER("c1"))).toBe("gone");
23
+ });
24
+ });
@@ -0,0 +1,114 @@
1
+ /*
2
+ * The keyframes a step change is built from. Each animates one property of its own (`opacity`,
3
+ * `translate` or `scale`, never `transform`), so a fade, a slide and a zoom on one snapshot run
4
+ * together. Which snapshot runs which, with what delay and duration, is written per change by
5
+ * `playStepTransition`.
6
+ */
7
+
8
+ @keyframes ob-course-fade-in {
9
+ from {
10
+ opacity: 0;
11
+ }
12
+ to {
13
+ opacity: 1;
14
+ }
15
+ }
16
+
17
+ @keyframes ob-course-fade-out {
18
+ from {
19
+ opacity: 1;
20
+ }
21
+ to {
22
+ opacity: 0;
23
+ }
24
+ }
25
+
26
+ @keyframes ob-course-scale-in {
27
+ from {
28
+ scale: 0.5;
29
+ }
30
+ to {
31
+ scale: 1;
32
+ }
33
+ }
34
+
35
+ @keyframes ob-course-scale-out {
36
+ from {
37
+ scale: 1;
38
+ }
39
+ to {
40
+ scale: 0.5;
41
+ }
42
+ }
43
+
44
+ @keyframes ob-course-slide-in-from-left {
45
+ from {
46
+ translate: -100% 0;
47
+ }
48
+ to {
49
+ translate: 0 0;
50
+ }
51
+ }
52
+
53
+ @keyframes ob-course-slide-in-from-right {
54
+ from {
55
+ translate: 100% 0;
56
+ }
57
+ to {
58
+ translate: 0 0;
59
+ }
60
+ }
61
+
62
+ @keyframes ob-course-slide-in-from-top {
63
+ from {
64
+ translate: 0 -100%;
65
+ }
66
+ to {
67
+ translate: 0 0;
68
+ }
69
+ }
70
+
71
+ @keyframes ob-course-slide-in-from-bottom {
72
+ from {
73
+ translate: 0 100%;
74
+ }
75
+ to {
76
+ translate: 0 0;
77
+ }
78
+ }
79
+
80
+ @keyframes ob-course-slide-out-to-left {
81
+ from {
82
+ translate: 0 0;
83
+ }
84
+ to {
85
+ translate: -100% 0;
86
+ }
87
+ }
88
+
89
+ @keyframes ob-course-slide-out-to-right {
90
+ from {
91
+ translate: 0 0;
92
+ }
93
+ to {
94
+ translate: 100% 0;
95
+ }
96
+ }
97
+
98
+ @keyframes ob-course-slide-out-to-top {
99
+ from {
100
+ translate: 0 0;
101
+ }
102
+ to {
103
+ translate: 0 -100%;
104
+ }
105
+ }
106
+
107
+ @keyframes ob-course-slide-out-to-bottom {
108
+ from {
109
+ translate: 0 0;
110
+ }
111
+ to {
112
+ translate: 0 100%;
113
+ }
114
+ }