@olenbetong/synergi-course 2.0.0 → 2.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/es/chrome/CategoryHeader.css +28 -0
- package/es/completion/CourseCompletion.css +53 -0
- package/es/completion/CourseCompletion.d.ts +32 -0
- package/es/completion/CourseCompletion.js +60 -0
- package/es/document/cues.d.ts +51 -6
- package/es/document/cues.js +115 -14
- package/es/document/effects.d.ts +30 -0
- package/es/document/effects.js +74 -0
- package/es/document/schema.d.ts +924 -136
- package/es/document/schema.js +139 -5
- package/es/document/validate.d.ts +2 -1
- package/es/document/validate.js +73 -2
- package/es/index.d.ts +6 -2
- package/es/index.js +6 -2
- package/es/media/Media.css +16 -0
- package/es/media/icons.d.ts +1 -0
- package/es/media/icons.js +3 -0
- package/es/player/CoursePlayer.css +58 -0
- package/es/player/CoursePlayer.js +27 -13
- package/es/player/useFullScreen.d.ts +17 -0
- package/es/player/useFullScreen.js +83 -0
- package/es/player/useMediaQuery.d.ts +7 -0
- package/es/player/useMediaQuery.js +16 -0
- package/es/steps/Blocks.d.ts +20 -1
- package/es/steps/Blocks.js +103 -22
- package/es/steps/Markdown.d.ts +7 -1
- package/es/steps/Markdown.js +31 -4
- package/es/steps/RevealMotion.d.ts +29 -0
- package/es/steps/RevealMotion.js +55 -0
- package/es/steps/SlideStepView.d.ts +5 -3
- package/es/steps/SlideStepView.js +38 -15
- package/es/steps/StepBodyProps.d.ts +7 -1
- package/es/steps/StepView.css +328 -12
- package/es/steps/StepView.d.ts +8 -2
- package/es/steps/StepView.js +2 -2
- package/es/steps/motion.d.ts +59 -0
- package/es/steps/motion.js +136 -0
- package/es/transitions/Transitions.css +114 -0
- package/es/transitions/plan.d.ts +66 -0
- package/es/transitions/plan.js +185 -0
- package/es/transitions/playStepTransition.d.ts +23 -0
- package/es/transitions/playStepTransition.js +131 -0
- package/es/types.d.ts +3 -1
- package/package.json +4 -4
- package/src/chrome/CategoryHeader.css +28 -0
- package/src/completion/CourseCompletion.css +53 -0
- package/src/completion/CourseCompletion.test.tsx +45 -0
- package/src/completion/CourseCompletion.tsx +98 -0
- package/src/document/cues.ts +142 -13
- package/src/document/document.test.ts +1 -1
- package/src/document/effects.ts +105 -0
- package/src/document/reveal.test.ts +263 -0
- package/src/document/schema.ts +161 -5
- package/src/document/validate.ts +77 -3
- package/src/index.ts +35 -0
- package/src/media/Media.css +16 -0
- package/src/media/icons.tsx +8 -0
- package/src/player/CoursePlayer.css +58 -0
- package/src/player/CoursePlayer.test.tsx +70 -6
- package/src/player/CoursePlayer.tsx +68 -17
- package/src/player/fullScreen.test.tsx +70 -0
- package/src/player/useFullScreen.ts +91 -0
- package/src/player/useMediaQuery.ts +21 -0
- package/src/steps/Blocks.test.ts +34 -0
- package/src/steps/Blocks.tsx +166 -26
- package/src/steps/Markdown.test.tsx +43 -0
- package/src/steps/Markdown.tsx +42 -4
- package/src/steps/Presence.test.tsx +82 -0
- package/src/steps/RevealMotion.ts +66 -0
- package/src/steps/SlideStepView.tsx +67 -17
- package/src/steps/StepBodyProps.ts +7 -1
- package/src/steps/StepView.css +328 -12
- package/src/steps/StepView.test.tsx +41 -0
- package/src/steps/StepView.tsx +9 -1
- package/src/steps/motion.test.ts +98 -0
- package/src/steps/motion.ts +178 -0
- package/src/steps/visibility.test.ts +24 -0
- package/src/transitions/Transitions.css +114 -0
- package/src/transitions/plan.ts +258 -0
- package/src/transitions/playStepTransition.ts +162 -0
- package/src/transitions/transitions.test.ts +274 -0
- package/src/types.ts +3 -1
- package/es/Transitions.css +0 -55
- package/es/Transitions.d.ts +0 -10
- package/es/Transitions.js +0 -24
- package/src/Transitions.css +0 -55
- package/src/Transitions.ts +0 -35
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { CourseCompletion, hasCompletionText, sanitizeCompletionHtml } from "./CourseCompletion";
|
|
5
|
+
|
|
6
|
+
describe("sanitizeCompletionHtml", () => {
|
|
7
|
+
it("keeps ordinary markup", () => {
|
|
8
|
+
let html = '<p class="text-center"><big>Gratulerer!</big> Les <a href="/hms">mer</a>.</p><img src="/x.png" alt="">';
|
|
9
|
+
expect(sanitizeCompletionHtml(html)).toBe(html);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("removes scripts, embedded objects and event handlers", () => {
|
|
13
|
+
let html =
|
|
14
|
+
'<p onclick="alert(1)">Hei</p><script>alert(2)</script><object data="/x"></object><img src="/x.png" onerror="alert(3)">';
|
|
15
|
+
expect(sanitizeCompletionHtml(html)).toBe('<p>Hei</p><img src="/x.png">');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("removes script links, also when the scheme is broken up", () => {
|
|
19
|
+
let html =
|
|
20
|
+
'<a href="javascript:alert(1)">a</a><a href=" java\tscript:alert(1)">b</a><a href="data:text/html,x">c</a>';
|
|
21
|
+
expect(sanitizeCompletionHtml(html)).toBe("<a>a</a><a>b</a><a>c</a>");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("keeps image data", () => {
|
|
25
|
+
let html = '<img src="data:image/png;base64,AAAA">';
|
|
26
|
+
expect(sanitizeCompletionHtml(html)).toBe(html);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("CourseCompletion", () => {
|
|
31
|
+
it("shows the default text when the completion text is blank", () => {
|
|
32
|
+
render(<CourseCompletion title="HMS" html={" \n "} emptyText="Du har fullført kurset." />);
|
|
33
|
+
expect(screen.getByText("Du har fullført kurset.")).toBeInTheDocument();
|
|
34
|
+
expect(hasCompletionText(" ")).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("shows the completion text, sanitized when asked", () => {
|
|
38
|
+
let { container } = render(
|
|
39
|
+
<CourseCompletion title="HMS" html={'<p onclick="x()">Gratulerer!</p>'} emptyText="Standard" sanitize />,
|
|
40
|
+
);
|
|
41
|
+
expect(screen.getByText("Gratulerer!")).toBeInTheDocument();
|
|
42
|
+
expect(container.querySelector("[onclick]")).toBeNull();
|
|
43
|
+
expect(screen.queryByText("Standard")).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Heading, Paragraph } from "@digdir/designsystemet-react";
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
|
|
6
|
+
import "./CourseCompletion.css";
|
|
7
|
+
|
|
8
|
+
export interface CourseCompletionProps {
|
|
9
|
+
/** The course's title, the screen's heading. */
|
|
10
|
+
title: ReactNode;
|
|
11
|
+
/** The course's completion text (HTML written by the course editors). Blank counts as none. */
|
|
12
|
+
html?: string | null;
|
|
13
|
+
/** What the learner reads when the course has no completion text. */
|
|
14
|
+
emptyText: ReactNode;
|
|
15
|
+
/** The buttons under the text (the diploma, back to the course list). */
|
|
16
|
+
actions?: ReactNode;
|
|
17
|
+
/**
|
|
18
|
+
* Strip scripts, event handlers, embedded frames and `javascript:` links before showing the
|
|
19
|
+
* text. For an editor's preview of text being written; the player shows the saved text as it is.
|
|
20
|
+
*/
|
|
21
|
+
sanitize?: boolean;
|
|
22
|
+
className?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Whether the completion text has anything to show; the course feed treats blank text as none. */
|
|
26
|
+
export function hasCompletionText(html: string | null | undefined): html is string {
|
|
27
|
+
return !!html?.trim();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const REMOVED_ELEMENTS = "script, iframe, frame, frameset, object, embed, base, meta, link, form";
|
|
31
|
+
const URL_ATTRIBUTES = new Set(["href", "src", "action", "formaction", "xlink:href", "srcdoc"]);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The completion text without anything that runs code: script and frame elements, `on*`
|
|
35
|
+
* attributes and `javascript:` (or `data:` outside images) URLs. Parsed in an inert document, so
|
|
36
|
+
* nothing in it runs or loads while it is cleaned.
|
|
37
|
+
*/
|
|
38
|
+
export function sanitizeCompletionHtml(html: string): string {
|
|
39
|
+
let document = new DOMParser().parseFromString(`<body>${html}</body>`, "text/html");
|
|
40
|
+
for (let element of document.body.querySelectorAll(REMOVED_ELEMENTS)) element.remove();
|
|
41
|
+
for (let element of document.body.querySelectorAll("*")) {
|
|
42
|
+
// A copy, since removing attributes changes the live list
|
|
43
|
+
for (let attribute of Array.from(element.attributes)) {
|
|
44
|
+
let name = attribute.name.toLowerCase();
|
|
45
|
+
if (name.startsWith("on")) {
|
|
46
|
+
element.removeAttribute(attribute.name);
|
|
47
|
+
} else if (URL_ATTRIBUTES.has(name)) {
|
|
48
|
+
// Browsers skip control characters and spaces in a scheme ("java\tscript:")
|
|
49
|
+
let value = [...attribute.value]
|
|
50
|
+
.filter((char) => char.charCodeAt(0) > 32)
|
|
51
|
+
.join("")
|
|
52
|
+
.toLowerCase();
|
|
53
|
+
let imageData = value.startsWith("data:image/") && name === "src" && element.tagName === "IMG";
|
|
54
|
+
if (
|
|
55
|
+
name === "srcdoc" ||
|
|
56
|
+
value.startsWith("javascript:") ||
|
|
57
|
+
value.startsWith("vbscript:") ||
|
|
58
|
+
(value.startsWith("data:") && !imageData)
|
|
59
|
+
) {
|
|
60
|
+
element.removeAttribute(attribute.name);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return document.body.innerHTML;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The screen a learner sees when the course is completed: the title, the course's completion
|
|
70
|
+
* text (or the default text) and the actions. The completion texts were written for the old
|
|
71
|
+
* full-page completion article; the stylesheet keeps them inside the panel.
|
|
72
|
+
*/
|
|
73
|
+
export function CourseCompletion({ title, html, emptyText, actions, sanitize, className }: CourseCompletionProps) {
|
|
74
|
+
let shown = useMemo(() => {
|
|
75
|
+
if (!hasCompletionText(html)) return null;
|
|
76
|
+
return sanitize ? sanitizeCompletionHtml(html) : html;
|
|
77
|
+
}, [html, sanitize]);
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div className={clsx("ob-course-completion", className)}>
|
|
81
|
+
<div className="ob-course-completion__panel">
|
|
82
|
+
<Heading level={1} data-size="xl">
|
|
83
|
+
{title}
|
|
84
|
+
</Heading>
|
|
85
|
+
{shown !== null ? (
|
|
86
|
+
<div
|
|
87
|
+
className="ob-course-completion__text ds-paragraph"
|
|
88
|
+
// oxlint-disable-next-line react/no-danger -- written by the course editors; sanitize for previews
|
|
89
|
+
dangerouslySetInnerHTML={{ __html: shown }}
|
|
90
|
+
/>
|
|
91
|
+
) : (
|
|
92
|
+
<Paragraph data-size="lg">{emptyText}</Paragraph>
|
|
93
|
+
)}
|
|
94
|
+
{actions && <div className="ob-course-completion__actions">{actions}</div>}
|
|
95
|
+
</div>
|
|
96
|
+
</div>
|
|
97
|
+
);
|
|
98
|
+
}
|
package/src/document/cues.ts
CHANGED
|
@@ -34,13 +34,14 @@ export function getStartedCueIds(cues: Cue[], time: number): Set<string> {
|
|
|
34
34
|
return started;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
/** Every block and
|
|
37
|
+
/** Every block, list item and canvas item of a slide in document order, columns flattened. */
|
|
38
38
|
export function getTimedParts(blocks: Block[]): Timed[] {
|
|
39
39
|
let parts: Timed[] = [];
|
|
40
40
|
|
|
41
41
|
function add(block: Block) {
|
|
42
42
|
parts.push(block);
|
|
43
43
|
if (block.type === "list") parts.push(...block.items);
|
|
44
|
+
if (block.type === "canvas") parts.push(...block.items);
|
|
44
45
|
if (block.type === "columns") {
|
|
45
46
|
for (let column of block.columns) column.blocks.forEach(add);
|
|
46
47
|
}
|
|
@@ -50,8 +51,12 @@ export function getTimedParts(blocks: Block[]): Timed[] {
|
|
|
50
51
|
return parts;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The ids of the parts that wait for a press of Next, in the order they are shown. None on a
|
|
56
|
+
* narrated slide: there only the narration shows and hides parts, and Next goes to the next step.
|
|
57
|
+
*/
|
|
58
|
+
export function getRevealSteps(blocks: Block[], narrated = false): string[] {
|
|
59
|
+
if (narrated) return [];
|
|
55
60
|
return getTimedParts(blocks)
|
|
56
61
|
.filter((part) => part.reveal === "step")
|
|
57
62
|
.map((part) => part.id);
|
|
@@ -64,24 +69,148 @@ export interface RevealState {
|
|
|
64
69
|
revealSteps: string[];
|
|
65
70
|
/** From `getStartedCueIds`. */
|
|
66
71
|
startedCues: Set<string>;
|
|
72
|
+
/**
|
|
73
|
+
* From `getRevealLeaders`: the part each `reveal: "with-previous"` part follows. Left out, such a
|
|
74
|
+
* part is there from the start.
|
|
75
|
+
*/
|
|
76
|
+
leaders?: ReadonlyMap<string, Timed>;
|
|
77
|
+
/**
|
|
78
|
+
* The slide has narration. `reveal: "step"` then waits for nothing: a part with only that shows
|
|
79
|
+
* from the start, and one that also has `revealAt` follows its cue.
|
|
80
|
+
*/
|
|
81
|
+
narrated?: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Whether a part waits to be shown by anything: Next, a cue, or the part it goes with. */
|
|
85
|
+
export function hasTrigger(part: Timed) {
|
|
86
|
+
return part.reveal != null || part.revealAt != null;
|
|
67
87
|
}
|
|
68
88
|
|
|
69
89
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* without sound.
|
|
90
|
+
* The part each `reveal: "with-previous"` part follows: the nearest part above it in document
|
|
91
|
+
* order that waits to be shown itself. A with-previous part with none is not in the map.
|
|
73
92
|
*/
|
|
74
|
-
export function
|
|
75
|
-
|
|
93
|
+
export function getRevealLeaders(blocks: Block[]): Map<string, Timed> {
|
|
94
|
+
let leaders = new Map<string, Timed>();
|
|
95
|
+
let previous: Timed | undefined;
|
|
96
|
+
for (let part of getTimedParts(blocks)) {
|
|
97
|
+
if (part.reveal === "with-previous" && previous) leaders.set(part.id, previous);
|
|
98
|
+
if (hasTrigger(part)) previous = part;
|
|
99
|
+
}
|
|
100
|
+
return leaders;
|
|
101
|
+
}
|
|
76
102
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!
|
|
103
|
+
/** Whether what a part waits for has happened, its `hideAt` aside. */
|
|
104
|
+
export function isDue(part: Timed, state: RevealState, seen = new Set<string>()): boolean {
|
|
105
|
+
if (!hasTrigger(part)) return true;
|
|
80
106
|
|
|
81
|
-
if (
|
|
107
|
+
if (part.reveal === "step") {
|
|
108
|
+
// On a narrated slide Next shows nothing: only a cue can hold the part back
|
|
109
|
+
if (state.narrated) return part.revealAt == null || state.startedCues.has(part.revealAt);
|
|
82
110
|
let index = state.revealSteps.indexOf(part.id);
|
|
83
111
|
if (index !== -1 && index < state.stepsShown) return true;
|
|
84
112
|
}
|
|
113
|
+
if (part.revealAt != null && state.startedCues.has(part.revealAt)) return true;
|
|
114
|
+
|
|
115
|
+
if (part.reveal === "with-previous") {
|
|
116
|
+
let leader = state.leaders?.get(part.id);
|
|
117
|
+
if (!leader) return true;
|
|
118
|
+
// The map only points up the document, but a hand-made one must not loop
|
|
119
|
+
if (seen.has(part.id)) return false;
|
|
120
|
+
seen.add(part.id);
|
|
121
|
+
return isDue(leader, state, seen);
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The reveal state of a slide after `stepsShown` presses of Next, with the narration at
|
|
128
|
+
* `startedCues`. On a narrated slide presses count for nothing; the cues decide.
|
|
129
|
+
*/
|
|
130
|
+
export function getRevealState(
|
|
131
|
+
blocks: Block[],
|
|
132
|
+
stepsShown: number,
|
|
133
|
+
startedCues: Set<string>,
|
|
134
|
+
narrated = false,
|
|
135
|
+
): RevealState {
|
|
136
|
+
return {
|
|
137
|
+
stepsShown: narrated ? 0 : stepsShown,
|
|
138
|
+
revealSteps: getRevealSteps(blocks, narrated),
|
|
139
|
+
startedCues,
|
|
140
|
+
leaders: getRevealLeaders(blocks),
|
|
141
|
+
narrated,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface RevealNavigation {
|
|
146
|
+
/** The `stepsShown` a press of Next goes to, or null when no press would change what is shown. */
|
|
147
|
+
next: number | null;
|
|
148
|
+
/** The `stepsShown` a press of Back goes to, or null when going back would change nothing. */
|
|
149
|
+
previous: number | null;
|
|
150
|
+
/** How many presses change what is shown, from the first one; the count the stepper shows. */
|
|
151
|
+
presses: number;
|
|
152
|
+
/** How many of those are behind the learner. */
|
|
153
|
+
pressed: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Where Next and Back take a slide, so that every press shows something: a press that would change
|
|
158
|
+
* nothing is skipped. What is shown is every part's visibility, effects included. Back lands on
|
|
159
|
+
* the first press that shows what it goes back to, so Next and Back meet at the same places. On a
|
|
160
|
+
* narrated slide there are no presses: Next and Back go to the next and the previous step.
|
|
161
|
+
*/
|
|
162
|
+
export function getRevealNavigation(
|
|
163
|
+
blocks: Block[],
|
|
164
|
+
stepsShown: number,
|
|
165
|
+
startedCues: Set<string>,
|
|
166
|
+
narrated = false,
|
|
167
|
+
): RevealNavigation {
|
|
168
|
+
let parts = getTimedParts(blocks);
|
|
169
|
+
let total = getRevealSteps(blocks, narrated).length;
|
|
170
|
+
let at = Math.max(0, Math.min(stepsShown, total));
|
|
171
|
+
let shown = (count: number) => {
|
|
172
|
+
let state = getRevealState(blocks, count, startedCues, narrated);
|
|
173
|
+
return parts.map((part) => (isVisible(part, state) ? "1" : "0")).join("");
|
|
174
|
+
};
|
|
175
|
+
let states = Array.from({ length: total + 1 }, (_value, count) => shown(count));
|
|
176
|
+
|
|
177
|
+
let next: number | null = null;
|
|
178
|
+
for (let count = at + 1; count <= total; count++) {
|
|
179
|
+
if (states[count] !== states[at]) {
|
|
180
|
+
next = count;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let previous: number | null = null;
|
|
186
|
+
for (let count = at - 1; count >= 0; count--) {
|
|
187
|
+
if (states[count] !== states[at]) {
|
|
188
|
+
previous = count;
|
|
189
|
+
while (previous > 0 && states[previous - 1] === states[previous]) previous -= 1;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let presses = 0;
|
|
195
|
+
let pressed = 0;
|
|
196
|
+
for (let count = 1; count <= total; count++) {
|
|
197
|
+
if (states[count] === states[count - 1]) continue;
|
|
198
|
+
presses += 1;
|
|
199
|
+
if (count <= at) pressed += 1;
|
|
200
|
+
}
|
|
201
|
+
return { next, previous, presses, pressed };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Whether a block or list item shows right now: what it waits for has happened (Next on a slide
|
|
206
|
+
* without narration, its cue, or the part it goes with), and its `hideAt` cue has not started.
|
|
207
|
+
*/
|
|
208
|
+
export function isVisible(part: Timed, state: RevealState): boolean {
|
|
209
|
+
if (part.hideAt && state.startedCues.has(part.hideAt)) return false;
|
|
210
|
+
return isDue(part, state);
|
|
211
|
+
}
|
|
85
212
|
|
|
86
|
-
|
|
213
|
+
/** Whether a step is a slide with narration, where only the narration shows and hides parts. */
|
|
214
|
+
export function isNarratedSlide(step: Step | null | undefined) {
|
|
215
|
+
return step?.type === "slide" && step.narration != null;
|
|
87
216
|
}
|
|
@@ -144,7 +144,7 @@ describe("additions to version 2", () => {
|
|
|
144
144
|
it("refuses an unknown size, tone, layout or advance", () => {
|
|
145
145
|
let step = structuredClone(full[0]) as SlideStep;
|
|
146
146
|
(step.blocks[1] as { size: string }).size = "huge";
|
|
147
|
-
(step.blocks[2] as { tone: string }).tone = "
|
|
147
|
+
(step.blocks[2] as { tone: string }).tone = "purple";
|
|
148
148
|
(step as { layout: string }).layout = "wide";
|
|
149
149
|
(step as { advance: string }).advance = "always";
|
|
150
150
|
expect(issuesOf(step).map((issue) => issue.path)).toEqual(["layout", "advance", "blocks.1.size", "blocks.2.tone"]);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { isVisible, type RevealState } from "./cues";
|
|
2
|
+
import type { Block, ContentBlock, EffectBlock } from "./schema";
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_EFFECT_DURATION = 300;
|
|
5
|
+
|
|
6
|
+
/** What the effects on one part add up to right now, and how the part changes into it. */
|
|
7
|
+
export interface PartEffect {
|
|
8
|
+
/** In the slide's text size, or for a canvas item in percent of the canvas. */
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
scale: number;
|
|
12
|
+
/** `null` while no shown effect sets one. */
|
|
13
|
+
opacity: number | null;
|
|
14
|
+
/** Milliseconds before the change starts: the delay of the effect that came, 0 for one that went. */
|
|
15
|
+
delay: number;
|
|
16
|
+
duration: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Every effect block of a slide in document order, those in columns included. */
|
|
20
|
+
export function getEffectBlocks(blocks: Array<Block | ContentBlock>): EffectBlock[] {
|
|
21
|
+
let effects: EffectBlock[] = [];
|
|
22
|
+
for (let block of blocks) {
|
|
23
|
+
if (block.type === "effect") effects.push(block);
|
|
24
|
+
if (block.type === "columns") {
|
|
25
|
+
for (let column of block.columns) effects.push(...getEffectBlocks(column.blocks));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return effects;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function effectTargets(effect: EffectBlock): string[] {
|
|
32
|
+
return typeof effect.target === "string" ? [effect.target] : effect.target;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The ids of the effects that are shown, and so act on their targets, in this state. */
|
|
36
|
+
export function getActiveEffects(blocks: Block[], state: RevealState): Set<string> {
|
|
37
|
+
return new Set(
|
|
38
|
+
getEffectBlocks(blocks)
|
|
39
|
+
.filter((effect) => isVisible(effect, state))
|
|
40
|
+
.map((effect) => effect.id),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The state of every part an effect targets, whether or not an effect is shown now, so a part
|
|
46
|
+
* goes back with a transition when its effects stop. Moves add up, scales multiply, and the last
|
|
47
|
+
* shown opacity in document order wins.
|
|
48
|
+
*
|
|
49
|
+
* `previouslyActive` (the ids from `getActiveEffects` before this change) picks the timing: of the
|
|
50
|
+
* effects on a part that came or went, the last in the document decides. A part whose effects did
|
|
51
|
+
* not change keeps the timing of its last shown effect.
|
|
52
|
+
*/
|
|
53
|
+
export function getPartEffects(
|
|
54
|
+
blocks: Block[],
|
|
55
|
+
state: RevealState,
|
|
56
|
+
previouslyActive?: ReadonlySet<string>,
|
|
57
|
+
): Map<string, PartEffect> {
|
|
58
|
+
let parts = new Map<string, PartEffect>();
|
|
59
|
+
let effects = getEffectBlocks(blocks);
|
|
60
|
+
let effectIds = new Set(effects.map((effect) => effect.id));
|
|
61
|
+
let decided = new Set<string>();
|
|
62
|
+
let active = getActiveEffects(blocks, state);
|
|
63
|
+
|
|
64
|
+
// Last first, so the first effect met for a part is the one that decides its timing
|
|
65
|
+
for (let effect of [...effects].reverse()) {
|
|
66
|
+
let on = active.has(effect.id);
|
|
67
|
+
let changed = previouslyActive != null && previouslyActive.has(effect.id) !== on;
|
|
68
|
+
|
|
69
|
+
for (let target of effectTargets(effect)) {
|
|
70
|
+
if (target === effect.id || effectIds.has(target)) continue;
|
|
71
|
+
let part = parts.get(target);
|
|
72
|
+
if (!part) {
|
|
73
|
+
part = { x: 0, y: 0, scale: 1, opacity: null, delay: 0, duration: DEFAULT_EFFECT_DURATION };
|
|
74
|
+
parts.set(target, part);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!decided.has(target) && (changed || (on && !changedOnPart(target, effects, active, previouslyActive)))) {
|
|
78
|
+
decided.add(target);
|
|
79
|
+
part.delay = on ? (effect.delay ?? 0) : 0;
|
|
80
|
+
part.duration = effect.duration ?? DEFAULT_EFFECT_DURATION;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!on) continue;
|
|
84
|
+
part.x += effect.move?.x ?? 0;
|
|
85
|
+
part.y += effect.move?.y ?? 0;
|
|
86
|
+
part.scale *= effect.scale ?? 1;
|
|
87
|
+
// Walking backwards, the first opacity met is the last in the document
|
|
88
|
+
if (part.opacity === null && effect.opacity != null) part.opacity = effect.opacity;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return parts;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Whether any effect on the part came or went, which then decides the timing over a still one. */
|
|
95
|
+
function changedOnPart(
|
|
96
|
+
target: string,
|
|
97
|
+
effects: EffectBlock[],
|
|
98
|
+
active: Set<string>,
|
|
99
|
+
previouslyActive: ReadonlySet<string> | undefined,
|
|
100
|
+
) {
|
|
101
|
+
if (!previouslyActive) return false;
|
|
102
|
+
return effects.some(
|
|
103
|
+
(effect) => effectTargets(effect).includes(target) && previouslyActive.has(effect.id) !== active.has(effect.id),
|
|
104
|
+
);
|
|
105
|
+
}
|