@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
|
@@ -252,16 +252,84 @@ async function endMedia(selector: "audio" | "video") {
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
describe("narration the learner may skip", () => {
|
|
255
|
-
it("is done
|
|
255
|
+
it("is done at once: Next goes to the next step, and parts marked for Next show from the start", async () => {
|
|
256
256
|
let optional = {
|
|
257
257
|
...revealSlide,
|
|
258
258
|
narration: { media: { src: "/media/course/a.m4a" }, required: false },
|
|
259
259
|
} as Step;
|
|
260
260
|
renderPlayer({ steps: steps(optional, plainSlide), progress: adapter(() => 1) });
|
|
261
261
|
|
|
262
|
+
expect(part("one")).not.toHaveAttribute("aria-hidden");
|
|
263
|
+
expect(part("two")).not.toHaveAttribute("aria-hidden");
|
|
264
|
+
expect(next()).toBeEnabled();
|
|
262
265
|
await press(next());
|
|
266
|
+
expect(screen.getByText("The end")).toBeInTheDocument();
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe("a narrated slide", () => {
|
|
271
|
+
let narrated: Step = {
|
|
272
|
+
schemaVersion: 2,
|
|
273
|
+
type: "slide",
|
|
274
|
+
narration: {
|
|
275
|
+
media: { src: "/media/course/a.m4a" },
|
|
276
|
+
cues: [
|
|
277
|
+
{ id: "c1", start: 2 },
|
|
278
|
+
{ id: "c2", start: 5 },
|
|
279
|
+
],
|
|
280
|
+
},
|
|
281
|
+
blocks: [
|
|
282
|
+
{ id: "s1", type: "text", text: "One", hideAt: "c1" },
|
|
283
|
+
{ id: "s2", type: "text", text: "Two", reveal: "step", revealAt: "c1", hideAt: "c2" },
|
|
284
|
+
{ id: "with", type: "text", text: "With two", reveal: "with-previous" },
|
|
285
|
+
{ id: "dim", type: "effect", target: "s1", opacity: 0.5, revealAt: "c1" },
|
|
286
|
+
],
|
|
287
|
+
};
|
|
288
|
+
let shown = (id: string) => document.querySelector(`[data-part="${id}"]:not(.is-leaving, .is-pending)`) !== null;
|
|
289
|
+
let at = async (seconds: number) => {
|
|
290
|
+
let audio = document.querySelector("audio")!;
|
|
291
|
+
await act(async () => {
|
|
292
|
+
audio.currentTime = seconds;
|
|
293
|
+
fireEvent(audio, new Event("timeupdate"));
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
it("waits for the narration before Next goes on, and presses reveal nothing", async () => {
|
|
298
|
+
renderPlayer({ steps: steps(narrated, plainSlide), progress: adapter(() => 1) });
|
|
299
|
+
|
|
300
|
+
expect(next()).toBeDisabled();
|
|
301
|
+
expect(screen.getByText("Finish this part to continue.")).toBeInTheDocument();
|
|
263
302
|
await press(next());
|
|
303
|
+
expect([shown("s1"), shown("s2"), shown("with")]).toEqual([true, false, false]);
|
|
304
|
+
|
|
305
|
+
await endMedia("audio");
|
|
264
306
|
expect(next()).toBeEnabled();
|
|
307
|
+
await press(next());
|
|
308
|
+
expect(screen.getByText("The end")).toBeInTheDocument();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("shows and hides the parts on their cues, with-previous parts and effects included", async () => {
|
|
312
|
+
renderPlayer({ steps: steps(narrated, plainSlide), mode: "preview" });
|
|
313
|
+
expect(shown("s1")).toBe(true);
|
|
314
|
+
expect(part("s1")).not.toHaveStyle({ filter: "opacity(0.5)" });
|
|
315
|
+
|
|
316
|
+
await at(3);
|
|
317
|
+
expect([shown("s1"), shown("s2"), shown("with")]).toEqual([false, true, true]);
|
|
318
|
+
await at(6);
|
|
319
|
+
expect([shown("s2"), shown("with")]).toEqual([false, true]);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("goes straight to the other steps with Next and Back, and starts over at the start of the narration", async () => {
|
|
323
|
+
renderPlayer({ steps: steps(plainSlide, narrated, plainSlide), mode: "preview", initialIndex: 1 });
|
|
324
|
+
await at(3);
|
|
325
|
+
await press(back());
|
|
326
|
+
expect(screen.getByText("1 / 3")).toBeInTheDocument();
|
|
327
|
+
|
|
328
|
+
await press(next());
|
|
329
|
+
expect(screen.getByText("2 / 3")).toBeInTheDocument();
|
|
330
|
+
expect([shown("s1"), shown("s2")]).toEqual([true, false]);
|
|
331
|
+
await press(next());
|
|
332
|
+
expect(screen.getByText("3 / 3")).toBeInTheDocument();
|
|
265
333
|
});
|
|
266
334
|
});
|
|
267
335
|
|
|
@@ -381,14 +449,10 @@ describe("moving on by itself", () => {
|
|
|
381
449
|
expect(screen.getByText("2 / 2")).toBeInTheDocument();
|
|
382
450
|
});
|
|
383
451
|
|
|
384
|
-
it("
|
|
452
|
+
it("moves on when the narration ends, whatever the slide marks for Next: the narration shows its parts", async () => {
|
|
385
453
|
renderPlayer({ steps: steps(autoSlide, plainSlide), progress: adapter(() => 1) });
|
|
386
454
|
|
|
387
455
|
await endMedia("audio");
|
|
388
|
-
await press(next());
|
|
389
|
-
await press(next());
|
|
390
|
-
expect(screen.getByText("1 / 2")).toBeInTheDocument();
|
|
391
|
-
await press(next());
|
|
392
456
|
expect(screen.getByText("2 / 2")).toBeInTheDocument();
|
|
393
457
|
});
|
|
394
458
|
});
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import clsx from "clsx";
|
|
1
2
|
import { Alert, Button } from "@digdir/designsystemet-react";
|
|
2
3
|
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
4
|
import { useSwipeable } from "react-swipeable";
|
|
4
5
|
import { ProgressBar } from "../chrome/ProgressBar";
|
|
5
|
-
import { getRevealSteps } from "../document/cues";
|
|
6
|
+
import { getRevealSteps, isNarratedSlide, type RevealNavigation } from "../document/cues";
|
|
6
7
|
import { getLocalizedString, localize } from "../i18n";
|
|
7
|
-
import {
|
|
8
|
+
import { CloseIcon, ExitFullscreenIcon } from "../media/icons";
|
|
8
9
|
import { StepBody, StepFrame } from "../steps/StepView";
|
|
9
|
-
import {
|
|
10
|
+
import { playStepTransition } from "../transitions/playStepTransition";
|
|
10
11
|
import type {
|
|
11
12
|
PlayerBranding,
|
|
12
13
|
PlayerCourse,
|
|
@@ -17,7 +18,10 @@ import type {
|
|
|
17
18
|
ResolveAsset,
|
|
18
19
|
} from "../types";
|
|
19
20
|
import { LanguageMenu } from "./LanguageMenu";
|
|
21
|
+
import { PlayerControls, PlayerHeader } from "./PlayerBars";
|
|
20
22
|
import { StartScreen } from "./StartScreen";
|
|
23
|
+
import { useFullScreen } from "./useFullScreen";
|
|
24
|
+
import { COMPACT_QUERY, useMediaQuery } from "./useMediaQuery";
|
|
21
25
|
import { useNavigationKeys } from "./useNavigationKeys";
|
|
22
26
|
|
|
23
27
|
import "../steps/StepView.css";
|
|
@@ -50,7 +54,8 @@ export interface CoursePlayerProps {
|
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
function countReveals(step: PlayerStep | undefined) {
|
|
53
|
-
|
|
57
|
+
let document = step?.document;
|
|
58
|
+
return document?.type === "slide" ? getRevealSteps(document.blocks, isNarratedSlide(document)).length : 0;
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
function advancesByItself(step: PlayerStep | undefined) {
|
|
@@ -94,11 +99,21 @@ export function CoursePlayer({
|
|
|
94
99
|
let unlocked = preview ? lastIndex : Math.max(serverUnlocked, unlockedIndex);
|
|
95
100
|
let isLast = current === lastIndex;
|
|
96
101
|
let revealCount = countReveals(step);
|
|
97
|
-
let pendingReveal = stepsShown < revealCount;
|
|
98
102
|
|
|
99
|
-
|
|
103
|
+
// The main language is stored as no language, so a learner who is under way has chosen it
|
|
104
|
+
let underWay = initialIndex > 0 || unlockedIndex > 0;
|
|
105
|
+
let showStart = !preview && !languageChosen && !underWay && course.cultureId == null && course.languages.length > 1;
|
|
100
106
|
let stepKey = step ? `${step.slideId}:${course.cultureId ?? ""}` : "";
|
|
101
107
|
|
|
108
|
+
// Where Next and Back go within a slide, from the slide itself: it knows the narration's time
|
|
109
|
+
let [navigation, setNavigation] = useState<{ stepKey: string; navigation: RevealNavigation } | null>(null);
|
|
110
|
+
let onRevealNavigation = useCallback(
|
|
111
|
+
(reported: RevealNavigation) => setNavigation({ stepKey, navigation: reported }),
|
|
112
|
+
[stepKey],
|
|
113
|
+
);
|
|
114
|
+
let reveals = navigation?.stepKey === stepKey ? navigation.navigation : null;
|
|
115
|
+
let pendingReveal = reveals ? reveals.next !== null : stepsShown < revealCount;
|
|
116
|
+
|
|
102
117
|
// Steps below the frontier were done before, and can be walked through freely.
|
|
103
118
|
let gated = !preview && current >= unlocked;
|
|
104
119
|
let minDuration = gated ? (step?.minDuration ?? 0) : 0;
|
|
@@ -124,7 +139,7 @@ export function CoursePlayer({
|
|
|
124
139
|
let go = useCallback(
|
|
125
140
|
(target: number, direction: "forward" | "backward") => {
|
|
126
141
|
movedRef.current = true;
|
|
127
|
-
|
|
142
|
+
void playStepTransition(
|
|
128
143
|
() => {
|
|
129
144
|
setIndex(target);
|
|
130
145
|
setStepsShown(direction === "backward" ? countReveals(steps[target]) : 0);
|
|
@@ -135,10 +150,10 @@ export function CoursePlayer({
|
|
|
135
150
|
setShownAt(time);
|
|
136
151
|
setNow(time);
|
|
137
152
|
},
|
|
138
|
-
{ direction },
|
|
153
|
+
{ scope: stageRef.current, from: steps[current], to: steps[target], direction },
|
|
139
154
|
);
|
|
140
155
|
},
|
|
141
|
-
[steps],
|
|
156
|
+
[steps, current],
|
|
142
157
|
);
|
|
143
158
|
|
|
144
159
|
async function complete() {
|
|
@@ -184,7 +199,7 @@ export function CoursePlayer({
|
|
|
184
199
|
function next() {
|
|
185
200
|
if (busy || completed || showStart || !step) return;
|
|
186
201
|
if (pendingReveal) {
|
|
187
|
-
setStepsShown(
|
|
202
|
+
setStepsShown(reveals?.next ?? stepsShown + 1);
|
|
188
203
|
return;
|
|
189
204
|
}
|
|
190
205
|
if (!canLeave) return;
|
|
@@ -195,8 +210,8 @@ export function CoursePlayer({
|
|
|
195
210
|
|
|
196
211
|
function back() {
|
|
197
212
|
if (busy || showStart) return;
|
|
198
|
-
if (stepsShown > 0 && revealCount > 0) {
|
|
199
|
-
setStepsShown(
|
|
213
|
+
if (reveals ? reveals.previous !== null : stepsShown > 0 && revealCount > 0) {
|
|
214
|
+
setStepsShown(reveals?.previous ?? Math.min(stepsShown, revealCount) - 1);
|
|
200
215
|
return;
|
|
201
216
|
}
|
|
202
217
|
if (current > 0) go(current - 1, "backward");
|
|
@@ -253,32 +268,64 @@ export function CoursePlayer({
|
|
|
253
268
|
else if (!done) hint = getLocalizedString("Finish this part to continue.");
|
|
254
269
|
else if (remaining > 0) hint = localize`You can continue in ${remaining} seconds.`;
|
|
255
270
|
|
|
271
|
+
let fullScreen = useFullScreen(rootRef);
|
|
272
|
+
// On a phone on its side the header and the buttons take the small size, to leave the slide the height
|
|
273
|
+
let chromeSize = useMediaQuery(COMPACT_QUERY) ? ("sm" as const) : undefined;
|
|
274
|
+
let fullScreenLabel = fullScreen.active ? getLocalizedString("Leave full screen") : getLocalizedString("Full screen");
|
|
275
|
+
|
|
256
276
|
let nextLabel = isLast && !pendingReveal ? getLocalizedString("Complete") : getLocalizedString("Next");
|
|
257
277
|
|
|
258
278
|
return (
|
|
259
|
-
<div
|
|
260
|
-
|
|
261
|
-
|
|
279
|
+
<div
|
|
280
|
+
className={clsx(
|
|
281
|
+
"ob-course-player",
|
|
282
|
+
fullScreen.active && "is-full-screen",
|
|
283
|
+
fullScreen.immersive && "is-immersive",
|
|
284
|
+
)}
|
|
285
|
+
data-mode={mode}
|
|
286
|
+
ref={rootRef}
|
|
287
|
+
>
|
|
288
|
+
{fullScreen.active && (
|
|
289
|
+
<Button
|
|
290
|
+
variant="secondary"
|
|
291
|
+
data-size="sm"
|
|
292
|
+
icon
|
|
293
|
+
className="ob-course-player__leave-full-screen"
|
|
294
|
+
onClick={fullScreen.toggle}
|
|
295
|
+
aria-label={fullScreenLabel}
|
|
296
|
+
title={fullScreenLabel}
|
|
297
|
+
>
|
|
298
|
+
<ExitFullscreenIcon />
|
|
299
|
+
</Button>
|
|
300
|
+
)}
|
|
301
|
+
<PlayerHeader
|
|
302
|
+
title={course.title}
|
|
303
|
+
size={chromeSize}
|
|
304
|
+
fullScreenLabel={fullScreenLabel}
|
|
305
|
+
onFullScreen={fullScreen.toggle}
|
|
306
|
+
end={
|
|
307
|
+
onExit && (
|
|
308
|
+
<Button
|
|
309
|
+
variant="tertiary"
|
|
310
|
+
data-size="sm"
|
|
311
|
+
icon
|
|
312
|
+
onClick={onExit}
|
|
313
|
+
aria-label={getLocalizedString("Close the course")}
|
|
314
|
+
title={getLocalizedString("Close the course")}
|
|
315
|
+
>
|
|
316
|
+
<CloseIcon />
|
|
317
|
+
</Button>
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
>
|
|
262
321
|
{onCultureChange && course.languages.length > 1 && !showStart && (
|
|
263
322
|
<LanguageMenu
|
|
264
323
|
languages={course.languages}
|
|
265
|
-
cultureId={course.cultureId}
|
|
324
|
+
cultureId={course.cultureId ?? course.mainCultureId ?? null}
|
|
266
325
|
onChange={(cultureId) => onCultureChange(cultureId)}
|
|
267
326
|
/>
|
|
268
327
|
)}
|
|
269
|
-
|
|
270
|
-
<Button
|
|
271
|
-
variant="tertiary"
|
|
272
|
-
data-size="sm"
|
|
273
|
-
icon
|
|
274
|
-
onClick={onExit}
|
|
275
|
-
aria-label={getLocalizedString("Close the course")}
|
|
276
|
-
title={getLocalizedString("Close the course")}
|
|
277
|
-
>
|
|
278
|
-
<CloseIcon />
|
|
279
|
-
</Button>
|
|
280
|
-
)}
|
|
281
|
-
</header>
|
|
328
|
+
</PlayerHeader>
|
|
282
329
|
<ProgressBar length={steps.length} currentIndex={showStart ? -1 : current} />
|
|
283
330
|
{showStart ? (
|
|
284
331
|
<StartScreen course={course} onChoose={chooseLanguage} />
|
|
@@ -308,40 +355,24 @@ export function CoursePlayer({
|
|
|
308
355
|
onSubtitlesChange={setSubtitles}
|
|
309
356
|
onDoneChange={setDone}
|
|
310
357
|
onMediaEnded={onMediaEnded}
|
|
358
|
+
onRevealNavigation={onRevealNavigation}
|
|
311
359
|
/>
|
|
312
360
|
</StepFrame>
|
|
313
361
|
</section>
|
|
314
|
-
<
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
<span className="ob-course-player__note">
|
|
329
|
-
<span role="status" className="ob-course-player__message">
|
|
330
|
-
{message}
|
|
331
|
-
</span>
|
|
332
|
-
{hint && <span className="ob-course-player__hint">{hint}</span>}
|
|
333
|
-
</span>
|
|
334
|
-
</div>
|
|
335
|
-
<Button
|
|
336
|
-
onClick={next}
|
|
337
|
-
loading={busy}
|
|
338
|
-
disabled={busy || completed || (!pendingReveal && !canLeave)}
|
|
339
|
-
aria-label={nextLabel}
|
|
340
|
-
>
|
|
341
|
-
<span className="ob-course-player__button-text">{nextLabel}</span>
|
|
342
|
-
{!(isLast && !pendingReveal) && <ArrowRightIcon />}
|
|
343
|
-
</Button>
|
|
344
|
-
</footer>
|
|
362
|
+
<PlayerControls
|
|
363
|
+
size={chromeSize}
|
|
364
|
+
position={current + 1}
|
|
365
|
+
count={steps.length}
|
|
366
|
+
message={message}
|
|
367
|
+
hint={hint}
|
|
368
|
+
backDisabled={busy || (current === 0 && (reveals ? reveals.previous === null : stepsShown === 0))}
|
|
369
|
+
nextDisabled={busy || completed || (!pendingReveal && !canLeave)}
|
|
370
|
+
busy={busy}
|
|
371
|
+
nextLabel={nextLabel}
|
|
372
|
+
nextArrow={!(isLast && !pendingReveal)}
|
|
373
|
+
onBack={back}
|
|
374
|
+
onNext={next}
|
|
375
|
+
/>
|
|
345
376
|
</>
|
|
346
377
|
)}
|
|
347
378
|
</div>
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Button } from "@digdir/designsystemet-react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { getLocalizedString } from "../i18n";
|
|
4
|
+
import { ArrowLeftIcon, ArrowRightIcon, FullscreenIcon } from "../media/icons";
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* The player's header and buttons, apart from the player, so an editor's preview draws the same
|
|
8
|
+
* bars around a step as the learner gets, and the step is left exactly the room it has there.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export function PlayerHeader({
|
|
12
|
+
title,
|
|
13
|
+
size,
|
|
14
|
+
fullScreenLabel,
|
|
15
|
+
onFullScreen,
|
|
16
|
+
children,
|
|
17
|
+
end,
|
|
18
|
+
inert,
|
|
19
|
+
}: {
|
|
20
|
+
title: string;
|
|
21
|
+
/** Drawn but not operable, and hidden from screen readers: a preview's sample. */
|
|
22
|
+
inert?: boolean;
|
|
23
|
+
/** `sm` on a phone on its side, to leave the step the height. */
|
|
24
|
+
size?: "sm";
|
|
25
|
+
fullScreenLabel: string;
|
|
26
|
+
onFullScreen?(): void;
|
|
27
|
+
/** Buttons between the title and the full screen button, such as the language menu. */
|
|
28
|
+
children?: ReactNode;
|
|
29
|
+
/** After the full screen button, such as closing the course. */
|
|
30
|
+
end?: ReactNode;
|
|
31
|
+
}) {
|
|
32
|
+
return (
|
|
33
|
+
<header className="ob-course-player__header" data-size={size} inert={inert} aria-hidden={inert || undefined}>
|
|
34
|
+
<p className="ob-course-player__title">{title}</p>
|
|
35
|
+
{children}
|
|
36
|
+
<Button
|
|
37
|
+
variant="tertiary"
|
|
38
|
+
data-size="sm"
|
|
39
|
+
icon
|
|
40
|
+
onClick={onFullScreen}
|
|
41
|
+
aria-label={fullScreenLabel}
|
|
42
|
+
title={fullScreenLabel}
|
|
43
|
+
>
|
|
44
|
+
<FullscreenIcon />
|
|
45
|
+
</Button>
|
|
46
|
+
{end}
|
|
47
|
+
</header>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function PlayerControls({
|
|
52
|
+
size,
|
|
53
|
+
position,
|
|
54
|
+
count,
|
|
55
|
+
message,
|
|
56
|
+
hint,
|
|
57
|
+
backDisabled,
|
|
58
|
+
nextDisabled,
|
|
59
|
+
busy,
|
|
60
|
+
nextLabel,
|
|
61
|
+
nextArrow,
|
|
62
|
+
onBack,
|
|
63
|
+
onNext,
|
|
64
|
+
inert,
|
|
65
|
+
}: {
|
|
66
|
+
size?: "sm";
|
|
67
|
+
/** Drawn but not operable, and hidden from screen readers: a preview's sample. */
|
|
68
|
+
inert?: boolean;
|
|
69
|
+
/** The step shown, from 1. */
|
|
70
|
+
position: number;
|
|
71
|
+
count: number;
|
|
72
|
+
message?: string | null;
|
|
73
|
+
hint?: string | null;
|
|
74
|
+
backDisabled?: boolean;
|
|
75
|
+
nextDisabled?: boolean;
|
|
76
|
+
busy?: boolean;
|
|
77
|
+
nextLabel: string;
|
|
78
|
+
/** The arrow on Next; left out on Complete. */
|
|
79
|
+
nextArrow: boolean;
|
|
80
|
+
onBack?(): void;
|
|
81
|
+
onNext?(): void;
|
|
82
|
+
}) {
|
|
83
|
+
return (
|
|
84
|
+
<footer className="ob-course-player__controls" data-size={size} inert={inert} aria-hidden={inert || undefined}>
|
|
85
|
+
<Button variant="secondary" onClick={onBack} disabled={backDisabled} aria-label={getLocalizedString("Back")}>
|
|
86
|
+
<ArrowLeftIcon />
|
|
87
|
+
<span className="ob-course-player__button-text">{getLocalizedString("Back")}</span>
|
|
88
|
+
</Button>
|
|
89
|
+
<div className="ob-course-player__status">
|
|
90
|
+
<span className="ob-course-player__count">
|
|
91
|
+
{position} / {count}
|
|
92
|
+
</span>
|
|
93
|
+
<span className="ob-course-player__note">
|
|
94
|
+
<span role="status" className="ob-course-player__message">
|
|
95
|
+
{message}
|
|
96
|
+
</span>
|
|
97
|
+
{hint && <span className="ob-course-player__hint">{hint}</span>}
|
|
98
|
+
</span>
|
|
99
|
+
</div>
|
|
100
|
+
<Button onClick={onNext} loading={busy} disabled={nextDisabled} aria-label={nextLabel}>
|
|
101
|
+
<span className="ob-course-player__button-text">{nextLabel}</span>
|
|
102
|
+
{nextArrow && <ArrowRightIcon />}
|
|
103
|
+
</Button>
|
|
104
|
+
</footer>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { act, render, screen } from "@testing-library/react";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import type { PlayerCourse, PlayerStep } from "../types";
|
|
4
|
+
import { CoursePlayer } from "./CoursePlayer";
|
|
5
|
+
import { fullScreenKind } from "./useFullScreen";
|
|
6
|
+
|
|
7
|
+
let course: PlayerCourse = { title: "Safety", cultureId: "nb-NO", languages: [{ cultureId: "nb-NO", name: "Norsk" }] };
|
|
8
|
+
let steps: PlayerStep[] = [
|
|
9
|
+
{
|
|
10
|
+
slideId: 1,
|
|
11
|
+
category: { title: "Intro" },
|
|
12
|
+
document: { schemaVersion: 2, type: "slide", blocks: [{ id: "h", type: "heading", text: "Hello" }] },
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
function renderPlayer() {
|
|
17
|
+
return render(<CoursePlayer course={course} steps={steps} resolveAsset={(src) => src} mode="preview" />);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let player = () => document.querySelector<HTMLElement>(".ob-course-player")!;
|
|
21
|
+
|
|
22
|
+
describe("full screen", () => {
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
vi.restoreAllMocks();
|
|
25
|
+
Reflect.deleteProperty(document, "fullscreenEnabled");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("uses the browser's full screen where it has one, and the immersive mode where it does not", () => {
|
|
29
|
+
let doc = { fullscreenEnabled: true, documentElement: { requestFullscreen() {} } } as unknown as Document;
|
|
30
|
+
expect(fullScreenKind(doc)).toBe("native");
|
|
31
|
+
let webkit = { webkitFullscreenEnabled: true, documentElement: { webkitRequestFullscreen() {} } };
|
|
32
|
+
expect(fullScreenKind(webkit as unknown as Document)).toBe("native");
|
|
33
|
+
// Safari on iPhone: the API is there for videos only, so it is not enabled for other elements
|
|
34
|
+
let iphone = { fullscreenEnabled: false, documentElement: {} } as unknown as Document;
|
|
35
|
+
expect(fullScreenKind(iphone)).toBe("immersive");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("lays the player over the page without the API, and leaves it with the corner button or Escape", async () => {
|
|
39
|
+
renderPlayer();
|
|
40
|
+
expect(player()).not.toHaveClass("is-full-screen");
|
|
41
|
+
|
|
42
|
+
await act(async () => screen.getByRole("button", { name: "Full screen" }).click());
|
|
43
|
+
expect(player()).toHaveClass("is-full-screen", "is-immersive");
|
|
44
|
+
expect(document.documentElement.style.overflow).toBe("hidden");
|
|
45
|
+
|
|
46
|
+
let leave = screen.getAllByRole("button", { name: "Leave full screen" });
|
|
47
|
+
expect(leave.some((button) => button.classList.contains("ob-course-player__leave-full-screen"))).toBe(true);
|
|
48
|
+
await act(async () => leave[0].click());
|
|
49
|
+
expect(player()).not.toHaveClass("is-full-screen");
|
|
50
|
+
expect(document.documentElement.style.overflow).toBe("");
|
|
51
|
+
|
|
52
|
+
await act(async () => screen.getByRole("button", { name: "Full screen" }).click());
|
|
53
|
+
await act(async () => {
|
|
54
|
+
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
|
|
55
|
+
});
|
|
56
|
+
expect(player()).not.toHaveClass("is-full-screen");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("asks the browser for full screen on the player where it can", async () => {
|
|
60
|
+
Object.defineProperty(document, "fullscreenEnabled", { value: true, configurable: true });
|
|
61
|
+
let request = vi.fn(async () => {});
|
|
62
|
+
Object.defineProperty(HTMLElement.prototype, "requestFullscreen", { value: request, configurable: true });
|
|
63
|
+
renderPlayer();
|
|
64
|
+
|
|
65
|
+
await act(async () => screen.getByRole("button", { name: "Full screen" }).click());
|
|
66
|
+
expect(request).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(request.mock.contexts[0]).toBe(player());
|
|
68
|
+
Reflect.deleteProperty(HTMLElement.prototype, "requestFullscreen");
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { type RefObject, useCallback, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
/** The prefixed names Safari and older WebKit browsers still use. */
|
|
4
|
+
type WebKitDocument = Document & {
|
|
5
|
+
webkitFullscreenEnabled?: boolean;
|
|
6
|
+
webkitFullscreenElement?: Element | null;
|
|
7
|
+
webkitExitFullscreen?: () => Promise<void> | void;
|
|
8
|
+
};
|
|
9
|
+
type WebKitElement = Element & { webkitRequestFullscreen?: () => Promise<void> | void };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* How full screen is done here: `native` asks the browser to show the element on the whole screen
|
|
13
|
+
* (the Fullscreen API, prefixed or not); `immersive` lays the player over the whole page itself,
|
|
14
|
+
* for browsers that only put videos on the screen, such as Safari on iPhone.
|
|
15
|
+
*/
|
|
16
|
+
export function fullScreenKind(doc: Document = document): "native" | "immersive" {
|
|
17
|
+
let webkit = doc as WebKitDocument;
|
|
18
|
+
let element = doc.documentElement as WebKitElement;
|
|
19
|
+
let enabled = doc.fullscreenEnabled === true || webkit.webkitFullscreenEnabled === true;
|
|
20
|
+
let canRequest =
|
|
21
|
+
typeof element.requestFullscreen === "function" || typeof element.webkitRequestFullscreen === "function";
|
|
22
|
+
return enabled && canRequest ? "native" : "immersive";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function fullscreenElement(doc: Document) {
|
|
26
|
+
return doc.fullscreenElement ?? (doc as WebKitDocument).webkitFullscreenElement ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function requestNative(element: Element) {
|
|
30
|
+
let target = element as WebKitElement;
|
|
31
|
+
if (typeof target.requestFullscreen === "function") await target.requestFullscreen();
|
|
32
|
+
else await target.webkitRequestFullscreen?.();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function exitNative(doc: Document) {
|
|
36
|
+
let webkit = doc as WebKitDocument;
|
|
37
|
+
if (typeof doc.exitFullscreen === "function") await doc.exitFullscreen();
|
|
38
|
+
else await webkit.webkitExitFullscreen?.();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The player's full screen: one switch for the learner whichever way the browser does it. Native
|
|
43
|
+
* full screen follows the browser (Escape or the system's back gesture leave it); the immersive
|
|
44
|
+
* mode is left with the same button or Escape, and keeps the page behind it from scrolling.
|
|
45
|
+
*/
|
|
46
|
+
export function useFullScreen(root: RefObject<HTMLElement | null>) {
|
|
47
|
+
let [immersive, setImmersive] = useState(false);
|
|
48
|
+
let [native, setNative] = useState(false);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
let sync = () => setNative(root.current != null && fullscreenElement(document) === root.current);
|
|
52
|
+
document.addEventListener("fullscreenchange", sync);
|
|
53
|
+
document.addEventListener("webkitfullscreenchange", sync);
|
|
54
|
+
return () => {
|
|
55
|
+
document.removeEventListener("fullscreenchange", sync);
|
|
56
|
+
document.removeEventListener("webkitfullscreenchange", sync);
|
|
57
|
+
};
|
|
58
|
+
}, [root]);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (!immersive) return;
|
|
62
|
+
let page = document.documentElement;
|
|
63
|
+
let overflow = page.style.overflow;
|
|
64
|
+
page.style.overflow = "hidden";
|
|
65
|
+
let onKey = (event: KeyboardEvent) => {
|
|
66
|
+
if (event.key === "Escape") setImmersive(false);
|
|
67
|
+
};
|
|
68
|
+
document.addEventListener("keydown", onKey);
|
|
69
|
+
return () => {
|
|
70
|
+
page.style.overflow = overflow;
|
|
71
|
+
document.removeEventListener("keydown", onKey);
|
|
72
|
+
};
|
|
73
|
+
}, [immersive]);
|
|
74
|
+
|
|
75
|
+
let toggle = useCallback(() => {
|
|
76
|
+
let element = root.current;
|
|
77
|
+
if (!element) return;
|
|
78
|
+
if (native) {
|
|
79
|
+
exitNative(document).catch(() => {});
|
|
80
|
+
} else if (immersive) {
|
|
81
|
+
setImmersive(false);
|
|
82
|
+
} else if (fullScreenKind() === "native") {
|
|
83
|
+
// A refusal (no user gesture, a frame without permission) falls back to the immersive mode
|
|
84
|
+
requestNative(element).catch(() => setImmersive(true));
|
|
85
|
+
} else {
|
|
86
|
+
setImmersive(true);
|
|
87
|
+
}
|
|
88
|
+
}, [root, native, immersive]);
|
|
89
|
+
|
|
90
|
+
return { active: native || immersive, immersive, toggle };
|
|
91
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A phone on its side: little height, so the player's chrome shrinks and the slide's text scales
|
|
5
|
+
* with the height. The stylesheet has the same query for what CSS can do alone.
|
|
6
|
+
*/
|
|
7
|
+
export const COMPACT_QUERY = "(orientation: landscape) and (max-height: 500px)";
|
|
8
|
+
|
|
9
|
+
/** Whether a media query matches, kept up to date. False where `matchMedia` is missing. */
|
|
10
|
+
export function useMediaQuery(query: string) {
|
|
11
|
+
return useSyncExternalStore(
|
|
12
|
+
(onChange) => {
|
|
13
|
+
if (typeof matchMedia !== "function") return () => {};
|
|
14
|
+
let list = matchMedia(query);
|
|
15
|
+
list.addEventListener?.("change", onChange);
|
|
16
|
+
return () => list.removeEventListener?.("change", onChange);
|
|
17
|
+
},
|
|
18
|
+
() => typeof matchMedia === "function" && matchMedia(query).matches,
|
|
19
|
+
() => false,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { nestItems } from "./Blocks";
|
|
3
|
+
|
|
4
|
+
describe("nestItems", () => {
|
|
5
|
+
it("puts an indented item under the nearest item above it with a smaller indent", () => {
|
|
6
|
+
let tree = nestItems([
|
|
7
|
+
{ id: "a", text: "A" },
|
|
8
|
+
{ id: "a1", text: "A1", indent: 1 },
|
|
9
|
+
{ id: "a1x", text: "A1x", indent: 2 },
|
|
10
|
+
{ id: "a2", text: "A2", indent: 1 },
|
|
11
|
+
{ id: "b", text: "B" },
|
|
12
|
+
]);
|
|
13
|
+
let ids = (nodes: typeof tree): unknown => nodes.map((node) => [node.item.id, ids(node.children)]);
|
|
14
|
+
expect(ids(tree)).toEqual([
|
|
15
|
+
[
|
|
16
|
+
"a",
|
|
17
|
+
[
|
|
18
|
+
["a1", [["a1x", []]]],
|
|
19
|
+
["a2", []],
|
|
20
|
+
],
|
|
21
|
+
],
|
|
22
|
+
["b", []],
|
|
23
|
+
]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("never nests an item deeper than one level under the item above it", () => {
|
|
27
|
+
let tree = nestItems([
|
|
28
|
+
{ id: "x", text: "X", indent: 2 },
|
|
29
|
+
{ id: "y", text: "Y", indent: 2 },
|
|
30
|
+
]);
|
|
31
|
+
expect(tree.map((node) => node.item.id)).toEqual(["x"]);
|
|
32
|
+
expect(tree[0].children.map((node) => node.item.id)).toEqual(["y"]);
|
|
33
|
+
});
|
|
34
|
+
});
|