@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.
- 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 +117 -14
- package/es/document/effects.d.ts +30 -0
- package/es/document/effects.js +76 -0
- package/es/document/schema.d.ts +2722 -346
- package/es/document/schema.js +172 -5
- package/es/document/validate.d.ts +2 -1
- package/es/document/validate.js +83 -16
- package/es/index.d.ts +8 -3
- 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 +76 -0
- package/es/player/CoursePlayer.js +28 -13
- package/es/player/PlayerBars.d.ts +32 -0
- package/es/player/PlayerBars.js +14 -0
- 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 +43 -3
- package/es/steps/Blocks.js +114 -26
- package/es/steps/Markdown.d.ts +7 -1
- package/es/steps/Markdown.js +31 -4
- package/es/steps/QuizStepView.d.ts +2 -1
- package/es/steps/QuizStepView.js +20 -10
- 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 +43 -10
- package/es/steps/StepBodyProps.d.ts +16 -1
- package/es/steps/StepView.css +390 -16
- package/es/steps/StepView.d.ts +36 -4
- package/es/steps/StepView.js +21 -5
- 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 +16 -3
- 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/box.test.ts +234 -0
- package/src/document/cues.ts +143 -13
- package/src/document/document.test.ts +1 -1
- package/src/document/effects.ts +106 -0
- package/src/document/reveal.test.ts +263 -0
- package/src/document/schema.ts +203 -5
- package/src/document/validate.ts +88 -18
- package/src/index.ts +46 -1
- package/src/media/Media.css +16 -0
- package/src/media/icons.tsx +8 -0
- package/src/player/CoursePlayer.css +76 -0
- package/src/player/CoursePlayer.test.tsx +70 -6
- package/src/player/CoursePlayer.tsx +91 -60
- package/src/player/PlayerBars.tsx +106 -0
- 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 +219 -32
- package/src/steps/Box.test.tsx +95 -0
- package/src/steps/Markdown.test.tsx +46 -0
- package/src/steps/Markdown.tsx +42 -4
- package/src/steps/Presence.test.tsx +82 -0
- package/src/steps/QuizStepView.test.tsx +79 -0
- package/src/steps/QuizStepView.tsx +34 -12
- package/src/steps/RevealMotion.ts +66 -0
- package/src/steps/SlideStepView.tsx +74 -12
- package/src/steps/StepBodyProps.ts +16 -1
- package/src/steps/StepView.css +390 -16
- package/src/steps/StepView.test.tsx +157 -0
- package/src/steps/StepView.tsx +108 -19
- 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 +16 -3
- 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,59 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
import type { PartEffect } from "../document/effects";
|
|
3
|
+
/** The reveal fade, in milliseconds: parts fade in, and passing parts fade out, this fast. */
|
|
4
|
+
export declare const FADE_MS = 300;
|
|
5
|
+
/** How long a part takes to glide to the place a change of the slide pushed it to. */
|
|
6
|
+
export declare const GLIDE_MS = 300;
|
|
7
|
+
/** Where a part stands, relative to the slide, and whether it can be seen there. */
|
|
8
|
+
export interface PartBox {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
/** Neither waiting to be shown nor fading out, itself or by a part around it. */
|
|
14
|
+
shown: boolean;
|
|
15
|
+
/** The nearest part around it, if any. */
|
|
16
|
+
parent: string | null;
|
|
17
|
+
}
|
|
18
|
+
export interface Glide {
|
|
19
|
+
id: string;
|
|
20
|
+
/** How far the part was from where it is now: it starts there and glides to nothing. */
|
|
21
|
+
dx: number;
|
|
22
|
+
dy: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The parts that a change pushed to another place, with how far, so they can glide there instead
|
|
26
|
+
* of jumping. A part that just appeared fades in where it is, and a part that cannot be seen has
|
|
27
|
+
* nothing to show. A part inside one that glides only glides by what it moved within it, so the
|
|
28
|
+
* two do not add up to too much. `after` lists parts around a part before the part itself, as the
|
|
29
|
+
* document does.
|
|
30
|
+
*/
|
|
31
|
+
export declare function getGlides(before: ReadonlyMap<string, PartBox>, after: ReadonlyMap<string, PartBox>, threshold?: number): Glide[];
|
|
32
|
+
/** Every part in the slide with its box. Sizes are layout sizes, without an effect's scale. */
|
|
33
|
+
export declare function measureParts(root: HTMLElement): Map<string, PartBox>;
|
|
34
|
+
/**
|
|
35
|
+
* Lifts the parts that started to fade out out of the flow, onto the box they had before the
|
|
36
|
+
* change, so the part coming in their place takes it at once and nothing jumps when they go.
|
|
37
|
+
*/
|
|
38
|
+
export declare function pinLeaving(root: HTMLElement, before: ReadonlyMap<string, PartBox>): void;
|
|
39
|
+
/** Puts a part that came back while it was fading out into the flow again. */
|
|
40
|
+
export declare function unpinReturned(root: HTMLElement): void;
|
|
41
|
+
/** Starts the glides on the elements; `composite: "add"` keeps an effect's own move under them. */
|
|
42
|
+
export declare function runGlides(root: HTMLElement, glides: Glide[]): void;
|
|
43
|
+
/** Width to height from a canvas's `aspect`, e.g. `16/9`. */
|
|
44
|
+
export declare function aspectRatio(aspect: string | undefined): number;
|
|
45
|
+
export type PartState = "shown" | "pending" | "leaving";
|
|
46
|
+
/**
|
|
47
|
+
* The inline style a part's effects and reveal delay give it. A move on a canvas item is in percent
|
|
48
|
+
* of the canvas (`cqi` is a percent of its width; the height is the width over the aspect), on any
|
|
49
|
+
* other part in the slide's text size. The transition lists the reveal fade too, since an inline
|
|
50
|
+
* transition replaces the stylesheet's.
|
|
51
|
+
*/
|
|
52
|
+
export declare function partStyle({ effect, delay, state, canvasAspect, }: {
|
|
53
|
+
effect?: PartEffect;
|
|
54
|
+
/** The part's `delay`: how long after it is due it shows. */
|
|
55
|
+
delay?: number;
|
|
56
|
+
state: PartState;
|
|
57
|
+
/** Set for an item on a canvas: the canvas's width over its height. */
|
|
58
|
+
canvasAspect?: number;
|
|
59
|
+
}): CSSProperties;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/** The reveal fade, in milliseconds: parts fade in, and passing parts fade out, this fast. */
|
|
2
|
+
export const FADE_MS = 300;
|
|
3
|
+
/** How long a part takes to glide to the place a change of the slide pushed it to. */
|
|
4
|
+
export const GLIDE_MS = 300;
|
|
5
|
+
/**
|
|
6
|
+
* The parts that a change pushed to another place, with how far, so they can glide there instead
|
|
7
|
+
* of jumping. A part that just appeared fades in where it is, and a part that cannot be seen has
|
|
8
|
+
* nothing to show. A part inside one that glides only glides by what it moved within it, so the
|
|
9
|
+
* two do not add up to too much. `after` lists parts around a part before the part itself, as the
|
|
10
|
+
* document does.
|
|
11
|
+
*/
|
|
12
|
+
export function getGlides(before, after, threshold = 0.5) {
|
|
13
|
+
let glides = [];
|
|
14
|
+
let moved = new Map();
|
|
15
|
+
for (let [id, box] of after) {
|
|
16
|
+
let old = before.get(id);
|
|
17
|
+
if (!old || !old.shown || !box.shown)
|
|
18
|
+
continue;
|
|
19
|
+
let dx = old.x - box.x;
|
|
20
|
+
let dy = old.y - box.y;
|
|
21
|
+
let ancestor = box.parent;
|
|
22
|
+
while (ancestor && !moved.has(ancestor))
|
|
23
|
+
ancestor = after.get(ancestor)?.parent ?? null;
|
|
24
|
+
let carried = ancestor ? moved.get(ancestor) : { dx: 0, dy: 0 };
|
|
25
|
+
let own = { dx: dx - carried.dx, dy: dy - carried.dy };
|
|
26
|
+
if (Math.abs(own.dx) < threshold && Math.abs(own.dy) < threshold)
|
|
27
|
+
continue;
|
|
28
|
+
moved.set(id, { dx, dy });
|
|
29
|
+
glides.push({ id, ...own });
|
|
30
|
+
}
|
|
31
|
+
return glides;
|
|
32
|
+
}
|
|
33
|
+
/** Every part in the slide with its box. Sizes are layout sizes, without an effect's scale. */
|
|
34
|
+
export function measureParts(root) {
|
|
35
|
+
let origin = root.getBoundingClientRect();
|
|
36
|
+
let boxes = new Map();
|
|
37
|
+
for (let element of root.querySelectorAll("[data-part]")) {
|
|
38
|
+
let rect = element.getBoundingClientRect();
|
|
39
|
+
let parent = element.parentElement?.closest("[data-part]");
|
|
40
|
+
boxes.set(element.dataset.part, {
|
|
41
|
+
x: rect.left - origin.left,
|
|
42
|
+
y: rect.top - origin.top,
|
|
43
|
+
width: element.offsetWidth,
|
|
44
|
+
height: element.offsetHeight,
|
|
45
|
+
shown: !element.closest(".is-pending, .is-leaving"),
|
|
46
|
+
parent: parent && root.contains(parent) ? parent.dataset.part : null,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return boxes;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Lifts the parts that started to fade out out of the flow, onto the box they had before the
|
|
53
|
+
* change, so the part coming in their place takes it at once and nothing jumps when they go.
|
|
54
|
+
*/
|
|
55
|
+
export function pinLeaving(root, before) {
|
|
56
|
+
let origin = root.getBoundingClientRect();
|
|
57
|
+
for (let element of root.querySelectorAll(".is-leaving[data-part]")) {
|
|
58
|
+
if (element.dataset.pinned != null || element.parentElement?.closest(".is-leaving"))
|
|
59
|
+
continue;
|
|
60
|
+
let box = before.get(element.dataset.part);
|
|
61
|
+
if (!box || getComputedStyle(element).position === "absolute")
|
|
62
|
+
continue;
|
|
63
|
+
element.dataset.pinned = "";
|
|
64
|
+
Object.assign(element.style, {
|
|
65
|
+
position: "absolute",
|
|
66
|
+
margin: "0",
|
|
67
|
+
width: `${box.width}px`,
|
|
68
|
+
height: `${box.height}px`,
|
|
69
|
+
top: "0px",
|
|
70
|
+
left: "0px",
|
|
71
|
+
});
|
|
72
|
+
// Where top and left 0 put it depends on the positioned box around it, so measure and correct
|
|
73
|
+
let now = element.getBoundingClientRect();
|
|
74
|
+
element.style.top = `${box.y - (now.top - origin.top)}px`;
|
|
75
|
+
element.style.left = `${box.x - (now.left - origin.left)}px`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Puts a part that came back while it was fading out into the flow again. */
|
|
79
|
+
export function unpinReturned(root) {
|
|
80
|
+
for (let element of root.querySelectorAll("[data-pinned]:not(.is-leaving)")) {
|
|
81
|
+
delete element.dataset.pinned;
|
|
82
|
+
for (let property of ["position", "margin", "width", "height", "top", "left"]) {
|
|
83
|
+
element.style.removeProperty(property);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** Starts the glides on the elements; `composite: "add"` keeps an effect's own move under them. */
|
|
88
|
+
export function runGlides(root, glides) {
|
|
89
|
+
for (let glide of glides) {
|
|
90
|
+
let element = root.querySelector(`[data-part="${CSS.escape(glide.id)}"]`);
|
|
91
|
+
if (typeof element?.animate !== "function")
|
|
92
|
+
continue;
|
|
93
|
+
element.animate([{ translate: `${glide.dx}px ${glide.dy}px` }, { translate: "0px 0px" }], {
|
|
94
|
+
duration: GLIDE_MS,
|
|
95
|
+
easing: "ease",
|
|
96
|
+
composite: "add",
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Width to height from a canvas's `aspect`, e.g. `16/9`. */
|
|
101
|
+
export function aspectRatio(aspect) {
|
|
102
|
+
let [width, height] = (aspect ?? "16/9").split("/").map((value) => Number(value.trim()));
|
|
103
|
+
return width > 0 && height > 0 ? width / height : 16 / 9;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The inline style a part's effects and reveal delay give it. A move on a canvas item is in percent
|
|
107
|
+
* of the canvas (`cqi` is a percent of its width; the height is the width over the aspect), on any
|
|
108
|
+
* other part in the slide's text size. The transition lists the reveal fade too, since an inline
|
|
109
|
+
* transition replaces the stylesheet's.
|
|
110
|
+
*/
|
|
111
|
+
export function partStyle({ effect, delay, state, canvasAspect, }) {
|
|
112
|
+
let style = {};
|
|
113
|
+
let transitions = [];
|
|
114
|
+
if (effect) {
|
|
115
|
+
let x = canvasAspect != null ? `${effect.x}cqi` : `calc(${effect.x} * var(--ob-course-em))`;
|
|
116
|
+
let y = canvasAspect != null ? `calc(${effect.y}cqi / ${canvasAspect})` : `calc(${effect.y} * var(--ob-course-em))`;
|
|
117
|
+
style.translate = `${x} ${y}`;
|
|
118
|
+
style.scale = String(effect.scale);
|
|
119
|
+
// A filter, so the effect's opacity multiplies with the reveal fade instead of replacing it
|
|
120
|
+
style.filter = `opacity(${effect.opacity ?? 1})`;
|
|
121
|
+
let timing = `${effect.duration}ms ease ${effect.delay}ms`;
|
|
122
|
+
transitions.push(`translate ${timing}`, `scale ${timing}`, `filter ${timing}`);
|
|
123
|
+
}
|
|
124
|
+
let wait = state === "shown" ? (delay ?? 0) : 0;
|
|
125
|
+
if (wait > 0) {
|
|
126
|
+
// Only on the way in: a part that is hidden again goes at once
|
|
127
|
+
transitions.push(`opacity ${FADE_MS}ms ease ${wait}ms`, `visibility 0s linear ${wait}ms`);
|
|
128
|
+
style.animationDelay = `${wait}ms`;
|
|
129
|
+
}
|
|
130
|
+
else if (transitions.length > 0) {
|
|
131
|
+
transitions.push(`opacity ${FADE_MS}ms ease`);
|
|
132
|
+
}
|
|
133
|
+
if (transitions.length > 0)
|
|
134
|
+
style.transition = transitions.join(", ");
|
|
135
|
+
return style;
|
|
136
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Step, StepTransition } from "../document/schema";
|
|
2
|
+
import type { PlayerCategory } from "../types";
|
|
3
|
+
export type TransitionDirection = "forward" | "backward";
|
|
4
|
+
export type TransitionSide = "old" | "new";
|
|
5
|
+
/** A step on one side of a change: its document, and the category band above it. */
|
|
6
|
+
export interface TransitionStep {
|
|
7
|
+
document: Step | null;
|
|
8
|
+
category?: PlayerCategory | null;
|
|
9
|
+
}
|
|
10
|
+
export type TransitionTarget = {
|
|
11
|
+
kind: "step";
|
|
12
|
+
} | {
|
|
13
|
+
kind: "content";
|
|
14
|
+
} | {
|
|
15
|
+
kind: "background";
|
|
16
|
+
} | {
|
|
17
|
+
kind: "header";
|
|
18
|
+
} | {
|
|
19
|
+
kind: "part";
|
|
20
|
+
id: string;
|
|
21
|
+
};
|
|
22
|
+
/** An element to name before its side of the change is captured. */
|
|
23
|
+
export interface NamedTarget {
|
|
24
|
+
side: TransitionSide;
|
|
25
|
+
target: TransitionTarget;
|
|
26
|
+
name: string;
|
|
27
|
+
}
|
|
28
|
+
export interface TransitionAnimation {
|
|
29
|
+
/** One of the keyframes in `Transitions.css`. */
|
|
30
|
+
keyframes: string;
|
|
31
|
+
delay: number;
|
|
32
|
+
duration: number;
|
|
33
|
+
/** `add` for movement and scale, so several items on one target move together. */
|
|
34
|
+
composition: "replace" | "add";
|
|
35
|
+
}
|
|
36
|
+
/** What one snapshot does: animate, vanish at once (`hidden`) or stand still (`shown`). */
|
|
37
|
+
export interface SnapshotRule {
|
|
38
|
+
pseudo: TransitionSide;
|
|
39
|
+
name: string;
|
|
40
|
+
animations: TransitionAnimation[] | "hidden" | "shown";
|
|
41
|
+
}
|
|
42
|
+
export interface TransitionPlan {
|
|
43
|
+
names: NamedTarget[];
|
|
44
|
+
rules: SnapshotRule[];
|
|
45
|
+
/** Milliseconds until the last animation ends. 0 means there is nothing to animate. */
|
|
46
|
+
duration: number;
|
|
47
|
+
css: string;
|
|
48
|
+
}
|
|
49
|
+
export declare const DEFAULT_TRANSITION_DURATION = 300;
|
|
50
|
+
/** A cross-fade of the step: what a step without `transitions` does. */
|
|
51
|
+
export declare const DEFAULT_TRANSITIONS: readonly StepTransition[];
|
|
52
|
+
export declare const STEP_NAME = "ob-course-step";
|
|
53
|
+
export declare const HEADER_NAME = "ob-course-category";
|
|
54
|
+
/** The transitions a step runs: its own list, or the cross-fade when it has none. */
|
|
55
|
+
export declare function getStepTransitions(step: Step | null | undefined): readonly StepTransition[];
|
|
56
|
+
/** The keyframes of one item. Zoom is two animations, so its fade and its scale combine with others. */
|
|
57
|
+
export declare function getAnimations(transition: StepTransition, direction: TransitionDirection): TransitionAnimation[];
|
|
58
|
+
/**
|
|
59
|
+
* Plans a change from `from` to `to`. Either side may be missing: the outgoing step's `exit` items
|
|
60
|
+
* and the incoming step's `enter` items run, and what has no item changes at once.
|
|
61
|
+
*/
|
|
62
|
+
export declare function planStepTransition({ from, to, direction, }: {
|
|
63
|
+
from?: TransitionStep | null;
|
|
64
|
+
to?: TransitionStep | null;
|
|
65
|
+
direction?: TransitionDirection;
|
|
66
|
+
}): TransitionPlan;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Turns a change of step into what a view transition needs: which elements get a
|
|
3
|
+
* `view-transition-name` on each side of the change, and the animations of their snapshots.
|
|
4
|
+
* Pure, so the rules can be tested without a browser; `playStepTransition` applies them.
|
|
5
|
+
*/
|
|
6
|
+
export const DEFAULT_TRANSITION_DURATION = 300;
|
|
7
|
+
/** A cross-fade of the step: what a step without `transitions` does. */
|
|
8
|
+
export const DEFAULT_TRANSITIONS = [
|
|
9
|
+
{ on: "exit", effect: "fade" },
|
|
10
|
+
{ on: "enter", effect: "fade" },
|
|
11
|
+
];
|
|
12
|
+
export const STEP_NAME = "ob-course-step";
|
|
13
|
+
export const HEADER_NAME = "ob-course-category";
|
|
14
|
+
/** The transitions a step runs: its own list, or the cross-fade when it has none. */
|
|
15
|
+
export function getStepTransitions(step) {
|
|
16
|
+
return step?.transitions ?? DEFAULT_TRANSITIONS;
|
|
17
|
+
}
|
|
18
|
+
function targetKey(transition) {
|
|
19
|
+
return transition.target ?? "step";
|
|
20
|
+
}
|
|
21
|
+
function toTarget(key) {
|
|
22
|
+
if (key === "step" || key === "content" || key === "background")
|
|
23
|
+
return { kind: key };
|
|
24
|
+
return { kind: "part", id: key };
|
|
25
|
+
}
|
|
26
|
+
/** A part id as the tail of a CSS identifier; the counter in front keeps names unique. */
|
|
27
|
+
function sanitize(id) {
|
|
28
|
+
return id.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
29
|
+
}
|
|
30
|
+
function mirror(edge, direction) {
|
|
31
|
+
if (direction === "forward")
|
|
32
|
+
return edge;
|
|
33
|
+
if (edge === "left")
|
|
34
|
+
return "right";
|
|
35
|
+
if (edge === "right")
|
|
36
|
+
return "left";
|
|
37
|
+
return edge;
|
|
38
|
+
}
|
|
39
|
+
/** The keyframes of one item. Zoom is two animations, so its fade and its scale combine with others. */
|
|
40
|
+
export function getAnimations(transition, direction) {
|
|
41
|
+
let way = transition.on === "enter" ? "in" : "out";
|
|
42
|
+
let delay = transition.delay ?? 0;
|
|
43
|
+
let duration = transition.duration ?? DEFAULT_TRANSITION_DURATION;
|
|
44
|
+
switch (transition.effect) {
|
|
45
|
+
case "fade":
|
|
46
|
+
return [{ keyframes: `ob-course-fade-${way}`, delay, duration, composition: "replace" }];
|
|
47
|
+
case "zoom":
|
|
48
|
+
return [
|
|
49
|
+
{ keyframes: `ob-course-scale-${way}`, delay, duration, composition: "add" },
|
|
50
|
+
{ keyframes: `ob-course-fade-${way}`, delay, duration, composition: "replace" },
|
|
51
|
+
];
|
|
52
|
+
case "slide": {
|
|
53
|
+
let edge = mirror(transition.from ?? (transition.on === "enter" ? "right" : "left"), direction);
|
|
54
|
+
let keyframes = transition.on === "enter" ? `ob-course-slide-in-from-${edge}` : `ob-course-slide-out-to-${edge}`;
|
|
55
|
+
return [{ keyframes, delay, duration, composition: "add" }];
|
|
56
|
+
}
|
|
57
|
+
case "none":
|
|
58
|
+
// A jump at the delay: hidden until then on enter, there until then on exit
|
|
59
|
+
return [{ keyframes: `ob-course-fade-${way}`, delay, duration: 0, composition: "replace" }];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function sameCategory(a, b) {
|
|
63
|
+
return a != null && b != null && a.title === b.title && (a.subTitle ?? "") === (b.subTitle ?? "");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The band stays put between steps of one category. Into another category the old band slides out
|
|
67
|
+
* and the new one in, sideways in the direction of travel; a band with nothing on the other side
|
|
68
|
+
* comes in from or leaves to the top.
|
|
69
|
+
*/
|
|
70
|
+
function headerRules(from, to, direction) {
|
|
71
|
+
let oldBand = from?.category ?? null;
|
|
72
|
+
let newBand = to?.category ?? null;
|
|
73
|
+
let names = [];
|
|
74
|
+
let rules = [];
|
|
75
|
+
if (oldBand)
|
|
76
|
+
names.push({ side: "old", target: { kind: "header" }, name: HEADER_NAME });
|
|
77
|
+
if (newBand)
|
|
78
|
+
names.push({ side: "new", target: { kind: "header" }, name: HEADER_NAME });
|
|
79
|
+
if (sameCategory(oldBand, newBand)) {
|
|
80
|
+
rules.push({ pseudo: "old", name: HEADER_NAME, animations: "hidden" });
|
|
81
|
+
rules.push({ pseudo: "new", name: HEADER_NAME, animations: "shown" });
|
|
82
|
+
return { names, rules };
|
|
83
|
+
}
|
|
84
|
+
let both = oldBand != null && newBand != null;
|
|
85
|
+
let slide = (on) => getAnimations({ on, effect: "slide", from: both ? undefined : "top" }, both ? direction : "forward");
|
|
86
|
+
if (oldBand)
|
|
87
|
+
rules.push({ pseudo: "old", name: HEADER_NAME, animations: slide("exit") });
|
|
88
|
+
if (newBand)
|
|
89
|
+
rules.push({ pseudo: "new", name: HEADER_NAME, animations: slide("enter") });
|
|
90
|
+
return { names, rules };
|
|
91
|
+
}
|
|
92
|
+
/** The names and rules of one side's own transitions. */
|
|
93
|
+
function sideRules(step, side, direction) {
|
|
94
|
+
let on = side === "old" ? "exit" : "enter";
|
|
95
|
+
let prefix = side === "old" ? "ob-course-exit" : "ob-course-enter";
|
|
96
|
+
let items = getStepTransitions(step.document).filter((transition) => transition.on === on);
|
|
97
|
+
let byTarget = new Map();
|
|
98
|
+
for (let item of items) {
|
|
99
|
+
let key = targetKey(item);
|
|
100
|
+
byTarget.set(key, [...(byTarget.get(key) ?? []), item]);
|
|
101
|
+
}
|
|
102
|
+
let names = [{ side, target: { kind: "step" }, name: STEP_NAME }];
|
|
103
|
+
let rules = [];
|
|
104
|
+
let count = 0;
|
|
105
|
+
for (let [key, list] of byTarget) {
|
|
106
|
+
let name = STEP_NAME;
|
|
107
|
+
if (key !== "step") {
|
|
108
|
+
let target = toTarget(key);
|
|
109
|
+
name = target.kind === "part" ? `${prefix}-${count++}-${sanitize(target.id)}` : `${prefix}-${key}`;
|
|
110
|
+
names.push({ side, target, name });
|
|
111
|
+
}
|
|
112
|
+
// The animation that ends last is listed last, so it decides a property both animate
|
|
113
|
+
let animations = list
|
|
114
|
+
.flatMap((item) => getAnimations(item, direction))
|
|
115
|
+
.sort((a, b) => a.delay + a.duration - (b.delay + b.duration));
|
|
116
|
+
rules.push({ pseudo: side, name, animations });
|
|
117
|
+
}
|
|
118
|
+
// Without an item of its own the step box changes at once, around whatever does animate
|
|
119
|
+
if (!byTarget.has("step"))
|
|
120
|
+
rules.push({ pseudo: side, name: STEP_NAME, animations: side === "old" ? "hidden" : "shown" });
|
|
121
|
+
return { names, rules };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Plans a change from `from` to `to`. Either side may be missing: the outgoing step's `exit` items
|
|
125
|
+
* and the incoming step's `enter` items run, and what has no item changes at once.
|
|
126
|
+
*/
|
|
127
|
+
export function planStepTransition({ from, to, direction = "forward", }) {
|
|
128
|
+
let names = [];
|
|
129
|
+
let rules = [];
|
|
130
|
+
for (let [step, side] of [
|
|
131
|
+
[from, "old"],
|
|
132
|
+
[to, "new"],
|
|
133
|
+
]) {
|
|
134
|
+
if (!step)
|
|
135
|
+
continue;
|
|
136
|
+
let result = sideRules(step, side, direction);
|
|
137
|
+
names.push(...result.names);
|
|
138
|
+
rules.push(...result.rules);
|
|
139
|
+
}
|
|
140
|
+
let header = headerRules(from ?? null, to ?? null, direction);
|
|
141
|
+
names.push(...header.names);
|
|
142
|
+
rules.push(...header.rules);
|
|
143
|
+
let duration = 0;
|
|
144
|
+
for (let rule of rules) {
|
|
145
|
+
if (typeof rule.animations === "string")
|
|
146
|
+
continue;
|
|
147
|
+
for (let animation of rule.animations)
|
|
148
|
+
duration = Math.max(duration, animation.delay + animation.duration);
|
|
149
|
+
}
|
|
150
|
+
return { names, rules, duration, css: toCss(rules) };
|
|
151
|
+
}
|
|
152
|
+
function snapshotCss(rule) {
|
|
153
|
+
let selector = `::view-transition-${rule.pseudo}(${rule.name})`;
|
|
154
|
+
if (rule.animations === "hidden")
|
|
155
|
+
return `${selector} { animation: none; opacity: 0; }`;
|
|
156
|
+
if (rule.animations === "shown")
|
|
157
|
+
return `${selector} { animation: none; opacity: 1; mix-blend-mode: normal; }`;
|
|
158
|
+
let list = rule.animations;
|
|
159
|
+
let join = (values) => values.join(", ");
|
|
160
|
+
return [
|
|
161
|
+
`${selector} {`,
|
|
162
|
+
`\tanimation-name: ${join(list.map((a) => a.keyframes))};`,
|
|
163
|
+
`\tanimation-duration: ${join(list.map((a) => `${a.duration}ms`))};`,
|
|
164
|
+
`\tanimation-delay: ${join(list.map((a) => `${a.delay}ms`))};`,
|
|
165
|
+
`\tanimation-timing-function: ease;`,
|
|
166
|
+
`\tanimation-fill-mode: both;`,
|
|
167
|
+
`\tanimation-composition: ${join(list.map((a) => a.composition))};`,
|
|
168
|
+
// Not the cross-fade blend: the effects of one side need not add up with the other's
|
|
169
|
+
`\tmix-blend-mode: normal;`,
|
|
170
|
+
`}`,
|
|
171
|
+
].join("\n");
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The stylesheet of one change. Groups do not move or resize, since every target stays in its
|
|
175
|
+
* place, and the rest of the page changes at once.
|
|
176
|
+
*/
|
|
177
|
+
function toCss(rules) {
|
|
178
|
+
let groups = [...new Set(rules.map((rule) => rule.name))];
|
|
179
|
+
return [
|
|
180
|
+
"::view-transition-old(root) { animation: none; opacity: 0; }",
|
|
181
|
+
"::view-transition-new(root) { animation: none; mix-blend-mode: normal; }",
|
|
182
|
+
...groups.map((name) => `::view-transition-group(${name}) { animation: none; }`),
|
|
183
|
+
...rules.map(snapshotCss),
|
|
184
|
+
].join("\n");
|
|
185
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "./Transitions.css";
|
|
2
|
+
import type { Step } from "../document/schema";
|
|
3
|
+
import { type TransitionDirection, type TransitionStep } from "./plan";
|
|
4
|
+
export interface StepTransitionOptions {
|
|
5
|
+
/** The step's box (`.ob-course-step`) or an element around it. Only elements inside it are named. */
|
|
6
|
+
scope: Element | null | undefined;
|
|
7
|
+
/** The step that leaves; its `exit` transitions run. Left out, nothing leaves. */
|
|
8
|
+
from?: TransitionStep | Step | null;
|
|
9
|
+
/** The step that comes; its `enter` transitions run. Left out, nothing comes. */
|
|
10
|
+
to?: TransitionStep | Step | null;
|
|
11
|
+
direction?: TransitionDirection;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Runs `run`, the change of step, inside a view transition: the outgoing step's `exit`
|
|
15
|
+
* transitions animate what it showed, the incoming step's `enter` transitions what it shows, and
|
|
16
|
+
* the category band stays or slides. Names are set on the elements inside `scope` only for as
|
|
17
|
+
* long as the transition runs, so other steps on the page (an editor's previews) are never part
|
|
18
|
+
* of it. A transition still running is skipped first. Without the API, or when the learner
|
|
19
|
+
* prefers reduced motion, `run` is called at once.
|
|
20
|
+
*
|
|
21
|
+
* Resolves when the transition has finished.
|
|
22
|
+
*/
|
|
23
|
+
export declare function playStepTransition(run: () => void, options: StepTransitionOptions): Promise<void>;
|