@olenbetong/synergi-course 2.1.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 (47) hide show
  1. package/es/document/cues.d.ts +1 -1
  2. package/es/document/cues.js +3 -1
  3. package/es/document/effects.d.ts +3 -3
  4. package/es/document/effects.js +3 -1
  5. package/es/document/schema.d.ts +1778 -190
  6. package/es/document/schema.js +35 -2
  7. package/es/document/validate.js +14 -18
  8. package/es/index.d.ts +3 -2
  9. package/es/index.js +1 -1
  10. package/es/player/CoursePlayer.css +18 -0
  11. package/es/player/CoursePlayer.js +3 -2
  12. package/es/player/PlayerBars.d.ts +32 -0
  13. package/es/player/PlayerBars.js +14 -0
  14. package/es/steps/Blocks.d.ts +24 -3
  15. package/es/steps/Blocks.js +14 -7
  16. package/es/steps/Markdown.js +1 -1
  17. package/es/steps/QuizStepView.d.ts +2 -1
  18. package/es/steps/QuizStepView.js +20 -10
  19. package/es/steps/SlideStepView.d.ts +1 -1
  20. package/es/steps/SlideStepView.js +14 -4
  21. package/es/steps/StepBodyProps.d.ts +9 -0
  22. package/es/steps/StepView.css +65 -7
  23. package/es/steps/StepView.d.ts +29 -3
  24. package/es/steps/StepView.js +21 -5
  25. package/es/types.d.ts +13 -2
  26. package/package.json +1 -1
  27. package/src/document/box.test.ts +234 -0
  28. package/src/document/cues.ts +2 -1
  29. package/src/document/effects.ts +4 -3
  30. package/src/document/schema.ts +44 -2
  31. package/src/document/validate.ts +15 -19
  32. package/src/index.ts +11 -1
  33. package/src/player/CoursePlayer.css +18 -0
  34. package/src/player/CoursePlayer.tsx +38 -58
  35. package/src/player/PlayerBars.tsx +106 -0
  36. package/src/steps/Blocks.tsx +55 -8
  37. package/src/steps/Box.test.tsx +95 -0
  38. package/src/steps/Markdown.test.tsx +3 -0
  39. package/src/steps/Markdown.tsx +1 -1
  40. package/src/steps/QuizStepView.test.tsx +79 -0
  41. package/src/steps/QuizStepView.tsx +34 -12
  42. package/src/steps/SlideStepView.tsx +15 -3
  43. package/src/steps/StepBodyProps.ts +9 -0
  44. package/src/steps/StepView.css +65 -7
  45. package/src/steps/StepView.test.tsx +116 -0
  46. package/src/steps/StepView.tsx +100 -19
  47. package/src/types.ts +13 -2
@@ -190,6 +190,85 @@ describe("quiz", () => {
190
190
  expect(document.querySelector("audio")).toBeNull();
191
191
  });
192
192
 
193
+ describe("must answer correctly to continue", () => {
194
+ // q1 is right with Green, q2 with both Helmet and Gloves; a wrong answer comes back without an explanation.
195
+ function retryAdapter(start: Partial<QuizAttempt> = {}): QuizAdapter {
196
+ return {
197
+ start: vi.fn(async () => ({ ...attempt, passPercent: null, retryUntilCorrect: true, ...start })),
198
+ answer: vi.fn(async (_slideId: number, _attemptId: string, questionId: string, optionIds: string[]) => {
199
+ let correct = questionId === "q1" ? optionIds.join() === "o11" : [...optionIds].sort().join() === "o21,o22";
200
+ return { correct, explanation: correct ? `Explained ${questionId}.` : null };
201
+ }),
202
+ finish: vi.fn(async () =>
203
+ result({ passed: true, percent: 100, correctCount: 2, passPercent: null, retryUntilCorrect: true }),
204
+ ),
205
+ };
206
+ }
207
+
208
+ it("asks again after a wrong answer and shows the explanation only after the right one", async () => {
209
+ let quiz = retryAdapter();
210
+ renderQuiz(quiz);
211
+
212
+ await click(button("Start the quiz"));
213
+ await click(screen.getByRole("radio", { name: "Red" }));
214
+ await click(button("Answer"));
215
+ expect(screen.getByText("Not correct. Try again.")).toBeInTheDocument();
216
+ expect(screen.queryByText("Explained q1.")).not.toBeInTheDocument();
217
+ expect(screen.queryByRole("button", { name: "Next question" })).not.toBeInTheDocument();
218
+ expect(screen.getByRole("radio", { name: "Green" })).toBeEnabled();
219
+
220
+ // Choosing another answer clears the old verdict.
221
+ await click(screen.getByRole("radio", { name: "Green" }));
222
+ expect(screen.queryByText("Not correct. Try again.")).not.toBeInTheDocument();
223
+ await click(button("Answer"));
224
+ expect(screen.getByText("Correct")).toBeInTheDocument();
225
+ expect(screen.getByText("Explained q1.")).toBeInTheDocument();
226
+ expect(quiz.answer).toHaveBeenNthCalledWith(1, 7, "a1", "q1", ["o12"]);
227
+ expect(quiz.answer).toHaveBeenNthCalledWith(2, 7, "a1", "q1", ["o11"]);
228
+ await click(button("Next question"));
229
+
230
+ await click(screen.getByRole("checkbox", { name: "Helmet" }));
231
+ await click(button("Answer"));
232
+ expect(screen.getByText("Not correct. Try again.")).toBeInTheDocument();
233
+ expect(screen.queryByRole("button", { name: "See the result" })).not.toBeInTheDocument();
234
+ await click(screen.getByRole("checkbox", { name: "Gloves" }));
235
+ await click(button("Answer"));
236
+ expect(quiz.answer).toHaveBeenLastCalledWith(7, "a1", "q2", ["o21", "o22"]);
237
+ await click(button("See the result"));
238
+
239
+ expect(screen.getByText("You passed the quiz")).toBeInTheDocument();
240
+ expect(screen.getByText("You answered every question correctly.")).toBeInTheDocument();
241
+ expect(screen.queryByText(/%/)).not.toBeInTheDocument();
242
+ expect(button("Next")).toBeEnabled();
243
+ });
244
+
245
+ it("never shows an explanation that comes with a wrong answer", async () => {
246
+ let quiz = retryAdapter();
247
+ quiz.answer = vi.fn(async () => ({ correct: false, explanation: "Green is right." }));
248
+ renderQuiz(quiz);
249
+
250
+ await click(button("Start the quiz"));
251
+ await click(screen.getByRole("radio", { name: "Red" }));
252
+ await click(button("Answer"));
253
+ expect(screen.getByText("Not correct. Try again.")).toBeInTheDocument();
254
+ expect(screen.queryByText("Green is right.")).not.toBeInTheDocument();
255
+ });
256
+
257
+ it("resumes at a question that was last answered wrong", async () => {
258
+ // The server only marks a correct answer as answered in this mode.
259
+ let quiz = retryAdapter({
260
+ questions: [
261
+ { ...attempt.questions[0], answered: true },
262
+ { ...attempt.questions[1], answered: false },
263
+ ],
264
+ });
265
+ renderQuiz(quiz);
266
+
267
+ await click(button("Start the quiz"));
268
+ expect(screen.getByText("Question 2 of 2")).toBeInTheDocument();
269
+ });
270
+ });
271
+
193
272
  it("shows a quiz passed before as passed", async () => {
194
273
  let quiz = quizAdapter([], { alreadyPassed: true });
195
274
  renderQuiz(quiz);
@@ -18,7 +18,8 @@ type Phase =
18
18
 
19
19
  /**
20
20
  * A quiz, one question at a time with feedback after each answer, graded by the server.
21
- * Done when an attempt has passed.
21
+ * Done when an attempt has passed. With `retryUntilCorrect` a wrong answer only says so and the
22
+ * learner answers the same question again; the next question comes after the right answer.
22
23
  */
23
24
  export function QuizStepView({
24
25
  step,
@@ -138,7 +139,10 @@ export function QuizStepView({
138
139
  let question = attempt.questions[index];
139
140
  let nextIndex = attempt.questions.findIndex((candidate, at) => at > index && !candidate.answered);
140
141
  let isLast = nextIndex === -1;
141
- let answered = feedback != null;
142
+ let retry = attempt.retryUntilCorrect === true;
143
+ // A wrong answer in a retry quiz leaves the question open for another answer.
144
+ let answered = feedback != null && (feedback.correct || !retry);
145
+ let tryAgain = feedback != null && !answered;
142
146
  let inputName = `ob-course-quiz-${slideId}-${question.questionId}`;
143
147
 
144
148
  let answer = () =>
@@ -146,6 +150,13 @@ export function QuizStepView({
146
150
  setFeedback(await quiz.answer(slideId, attempt.attemptId, question.questionId, selected));
147
151
  });
148
152
 
153
+ let toggle = (optionId: string, checked: boolean) => {
154
+ // Changing the answer after "try again" clears the old verdict, so it is not read as being about the new choice.
155
+ if (tryAgain) setFeedback(null);
156
+ if (!question.multiple) setSelected([optionId]);
157
+ else setSelected((current) => (checked ? [...current, optionId] : current.filter((id) => id !== optionId)));
158
+ };
159
+
149
160
  let next = () => {
150
161
  if (!isLast) {
151
162
  setSelected([]);
@@ -159,11 +170,6 @@ export function QuizStepView({
159
170
  });
160
171
  };
161
172
 
162
- let toggle = (optionId: string, checked: boolean) => {
163
- if (!question.multiple) setSelected([optionId]);
164
- else setSelected((current) => (checked ? [...current, optionId] : current.filter((id) => id !== optionId)));
165
- };
166
-
167
173
  return (
168
174
  <div className="ob-course-form-step ob-course-quiz">
169
175
  <Paragraph data-size="sm" className="ob-course-hint">
@@ -194,13 +200,21 @@ export function QuizStepView({
194
200
  );
195
201
  })}
196
202
  </Fieldset>
197
- {feedback && (
198
- <Alert data-color={feedback.correct ? "success" : "danger"} className="ob-course-quiz__feedback">
203
+ {tryAgain ? (
204
+ <Alert data-color="danger" className="ob-course-quiz__feedback">
199
205
  <Heading level={3} data-size="2xs">
200
- {feedback.correct ? getLocalizedString("Correct") : getLocalizedString("Not correct")}
206
+ {getLocalizedString("Not correct. Try again.")}
201
207
  </Heading>
202
- {feedback.explanation && <Markdown>{feedback.explanation}</Markdown>}
203
208
  </Alert>
209
+ ) : (
210
+ feedback && (
211
+ <Alert data-color={feedback.correct ? "success" : "danger"} className="ob-course-quiz__feedback">
212
+ <Heading level={3} data-size="2xs">
213
+ {feedback.correct ? getLocalizedString("Correct") : getLocalizedString("Not correct")}
214
+ </Heading>
215
+ {feedback.explanation && <Markdown>{feedback.explanation}</Markdown>}
216
+ </Alert>
217
+ )
204
218
  )}
205
219
  {error}
206
220
  <div>
@@ -219,6 +233,7 @@ export function QuizStepView({
219
233
  }
220
234
 
221
235
  let { result } = phase;
236
+ let retryResult = result != null && (result.retryUntilCorrect ?? phase.attempt.retryUntilCorrect) === true;
222
237
 
223
238
  return (
224
239
  <div className="ob-course-form-step ob-course-quiz">
@@ -226,7 +241,14 @@ export function QuizStepView({
226
241
  <Heading level={2} data-size="md" ref={focusRef} tabIndex={-1}>
227
242
  {passed ? getLocalizedString("You passed the quiz") : getLocalizedString("You did not pass the quiz")}
228
243
  </Heading>
229
- {result ? (
244
+ {retryResult ? (
245
+ // Every question had to be right before the next, so a score or a percent would say nothing.
246
+ <Paragraph>
247
+ {passed
248
+ ? getLocalizedString("You answered every question correctly.")
249
+ : getLocalizedString("Answer every question correctly to pass.")}
250
+ </Paragraph>
251
+ ) : result ? (
230
252
  <>
231
253
  <Paragraph className="ob-course-quiz__score">
232
254
  {localize`You answered ${result.correctCount} of ${result.questionCount} questions correctly (${Math.round(result.percent)} %).`}
@@ -25,6 +25,8 @@ export function SlideStepView({
25
25
  onDoneChange,
26
26
  onMediaEnded,
27
27
  onRevealNavigation,
28
+ revealAll = false,
29
+ renderInsert,
28
30
  }: StepBodyProps & { step: SlideStep }) {
29
31
  let narration = step.narration;
30
32
  // On a narrated slide only the narration shows and hides parts; Next goes to the next step
@@ -71,7 +73,8 @@ export function SlideStepView({
71
73
  let activeKey = [...active].join("\u0000");
72
74
  let [effectsSeen, setEffectsSeen] = useState({ key: activeKey, active, previous: active });
73
75
  if (effectsSeen.key !== activeKey) setEffectsSeen({ key: activeKey, active, previous: effectsSeen.active });
74
- let effects = getPartEffects(step.blocks, reveal, effectsSeen.previous);
76
+ // An editor that draws every part shows them where they are laid out, not moved by an effect
77
+ let effects = revealAll ? undefined : getPartEffects(step.blocks, reveal, effectsSeen.previous);
75
78
 
76
79
  let calm = prefersCalm();
77
80
 
@@ -79,7 +82,16 @@ export function SlideStepView({
79
82
  .map((part) => `${part.id}:${getVisibility(part, reveal)}`)
80
83
  .join("|");
81
84
 
82
- let context: BlocksContext = { reveal, resolveAsset, subtitles, onSubtitlesChange, effects, calm };
85
+ let context: BlocksContext = {
86
+ reveal,
87
+ resolveAsset,
88
+ subtitles,
89
+ onSubtitlesChange,
90
+ effects,
91
+ calm,
92
+ revealAll,
93
+ renderInsert,
94
+ };
83
95
 
84
96
  let background = step.background ? resolveAsset(step.background) : undefined;
85
97
  // A banner shows the image as a band of its own instead of behind the whole slide
@@ -87,7 +99,7 @@ export function SlideStepView({
87
99
  if (banner) background = undefined;
88
100
  let content = (
89
101
  <div className="ob-course-slide__content">
90
- <Blocks blocks={step.blocks} context={context} />
102
+ <Blocks blocks={step.blocks} context={context} container={{ kind: "slide" }} />
91
103
  </div>
92
104
  );
93
105
 
@@ -1,4 +1,6 @@
1
+ import type { ReactNode } from "react";
1
2
  import type { RevealNavigation } from "../document/cues";
3
+ import type { InsertPlace } from "./Blocks";
2
4
  import type { PlayerMode, QuizAdapter, ResolveAsset } from "../types";
3
5
 
4
6
  /** What the player hands every step type. */
@@ -20,4 +22,11 @@ export interface StepBodyProps {
20
22
  onMediaEnded?(): void;
21
23
  /** A slide reports where Next and Back go from here, and the presses that show something. */
22
24
  onRevealNavigation?(navigation: RevealNavigation): void;
25
+ /**
26
+ * For an editor: every part of a slide is drawn, whatever its timing, so each can be reached,
27
+ * and the effects are left off. A part that would not be shown yet carries `is-unrevealed`.
28
+ */
29
+ revealAll?: boolean;
30
+ /** For an editor: a node drawn at each place on a slide where a block can be added. */
31
+ renderInsert?(place: InsertPlace): ReactNode;
23
32
  }
@@ -38,6 +38,14 @@
38
38
 
39
39
  /* Blocks */
40
40
 
41
+ /*
42
+ * An editor's place to add a block (`renderInsert`): the node it draws is laid out as a block would
43
+ * be. The rules for a slide's first heading skip it, so a title looks the same while it is edited.
44
+ */
45
+ .ob-course-insert {
46
+ display: contents;
47
+ }
48
+
41
49
  .ob-course-slide {
42
50
  flex: 1;
43
51
  display: flex;
@@ -116,7 +124,7 @@
116
124
  }
117
125
 
118
126
  /* The first heading is the title whatever its level, in the company's colour. */
119
- .ob-course-slide__content > .ob-course-block--heading:first-child {
127
+ .ob-course-slide__content > .ob-course-block--heading:nth-child(1 of :not(.ob-course-insert)) {
120
128
  font-size: clamp(2em, 1.2em + 4cqi, 3.6em);
121
129
  color: var(--ds-color-brand-text-default);
122
130
  }
@@ -148,7 +156,7 @@
148
156
  box-shadow: none;
149
157
  text-shadow: 0 2px 12px rgb(0 0 0 / 0.6);
150
158
 
151
- > .ob-course-block--heading:first-child {
159
+ > .ob-course-block--heading:nth-child(1 of :not(.ob-course-insert)) {
152
160
  color: inherit;
153
161
  }
154
162
  }
@@ -212,7 +220,7 @@
212
220
  text-align: center;
213
221
  text-shadow: 0.05em 0.05em rgb(0 0 0 / 0.5);
214
222
 
215
- > .ob-course-block--heading:first-child {
223
+ > .ob-course-block--heading:nth-child(1 of :not(.ob-course-insert)) {
216
224
  font-size: clamp(2em, 1.2em + 4cqi, 3.6em);
217
225
  }
218
226
 
@@ -284,6 +292,7 @@
284
292
  .ob-course-block--list,
285
293
  .ob-course-block--quote,
286
294
  .ob-course-block--callout,
295
+ .ob-course-block--box,
287
296
  .ob-course-block--video-link,
288
297
  .ob-course-list-item
289
298
  ) {
@@ -297,6 +306,7 @@
297
306
  .ob-course-block--list,
298
307
  .ob-course-block--quote,
299
308
  .ob-course-block--callout,
309
+ .ob-course-block--box,
300
310
  .ob-course-block--video-link,
301
311
  .ob-course-list-item
302
312
  ) {
@@ -463,8 +473,11 @@
463
473
  object-fit: contain;
464
474
  }
465
475
 
466
- /* `data-color` picks the Designsystemet colour scale the tokens below resolve to. */
467
- .ob-course-block--callout {
476
+ /*
477
+ * A callout or a box. `data-color` picks the Designsystemet colour scale the tokens below resolve
478
+ * to; every theme maps `brand` to its own scale the way it maps the severity colours.
479
+ */
480
+ .ob-course-surface {
468
481
  padding: 0.9em 1.1em;
469
482
  border: var(--ds-border-width-default, 1px) solid var(--ds-color-border-subtle);
470
483
  border-inline-start: 0.35em solid var(--ds-color-base-default);
@@ -478,6 +491,51 @@
478
491
  }
479
492
  }
480
493
 
494
+ /*
495
+ * The tone's base colour with its contrast colour on it, the pair the theme guarantees can be read.
496
+ * What has a colour of its own inside takes the contrast colour instead, since its own would vanish
497
+ * on a surface of the same scale.
498
+ */
499
+ .ob-course-surface--filled {
500
+ border-color: var(--ds-color-base-default);
501
+ background: var(--ds-color-base-default);
502
+ color: var(--ds-color-base-contrast-default);
503
+ /* A title page's shadow is for words on a picture, not on a solid colour */
504
+ text-shadow: none;
505
+
506
+ .ob-course-tone {
507
+ color: inherit;
508
+ font-weight: 600;
509
+ }
510
+
511
+ .ob-course-block--quote blockquote {
512
+ border-inline-start-color: currentColor;
513
+ }
514
+
515
+ figcaption,
516
+ .ob-course-block--quote figcaption,
517
+ .ob-course-block--image figcaption {
518
+ color: var(--ds-color-base-contrast-subtle);
519
+ }
520
+
521
+ /* The video link's outlined button would be the surface's colour on itself */
522
+ .ob-course-block--video-link .ds-button {
523
+ --dsc-button-background: var(--ds-color-surface-default);
524
+ --dsc-button-background--hover: var(--ds-color-surface-hover);
525
+ --dsc-button-background--active: var(--ds-color-surface-active);
526
+ --dsc-button-border-color: transparent;
527
+ --dsc-button-color: var(--ds-color-text-default);
528
+ }
529
+ }
530
+
531
+ /* The parts of a box stand like the parts of a column, and reveal the same way. */
532
+ .ob-course-block--box {
533
+ display: flex;
534
+ flex-direction: column;
535
+ gap: 0.9em;
536
+ min-width: 0;
537
+ }
538
+
481
539
  .ob-course-block--quote {
482
540
  blockquote {
483
541
  margin: 0;
@@ -677,8 +735,8 @@
677
735
  gap: 0.6em;
678
736
  }
679
737
 
680
- .ob-course-slide--title .ob-course-slide__content > .ob-course-block--heading:first-child,
681
- .ob-course-banner .ob-course-slide__content > .ob-course-block--heading:first-child {
738
+ .ob-course-slide--title .ob-course-slide__content > .ob-course-block--heading:nth-child(1 of :not(.ob-course-insert)),
739
+ .ob-course-banner .ob-course-slide__content > .ob-course-block--heading:nth-child(1 of :not(.ob-course-insert)) {
682
740
  font-size: clamp(1.5em, 1em + 3cqi, 2.4em);
683
741
  }
684
742
 
@@ -75,4 +75,120 @@ describe("StepView", () => {
75
75
  expect(layer?.style.backgroundImage).toContain("/media/course/bg.jpg");
76
76
  expect(layer?.contains(container.querySelector(".ob-course-slide__content"))).toBe(false);
77
77
  });
78
+
79
+ describe("revealAll, for an editor", () => {
80
+ let timed: Step = {
81
+ schemaVersion: 2,
82
+ type: "slide",
83
+ blocks: [
84
+ { id: "h", type: "heading", text: "Shown" },
85
+ { id: "t", type: "text", text: "On Next", reveal: "step" },
86
+ { id: "c", type: "text", text: "Passing", revealAt: "c1", hideAt: "c2" },
87
+ ],
88
+ };
89
+
90
+ it("hides the parts that wait, as the player does, when it is off", () => {
91
+ let { container } = render(<StepView step={timed} resolveAsset={resolveAsset} stepsShown={0} />);
92
+
93
+ expect(container.querySelector('[data-part="t"]')?.classList.contains("is-pending")).toBe(true);
94
+ expect(container.querySelector('[data-part="c"]')).toBeNull();
95
+ expect(container.querySelector(".is-unrevealed")).toBeNull();
96
+ });
97
+
98
+ it("draws every part and marks the ones that would wait", () => {
99
+ let { container } = render(<StepView step={timed} resolveAsset={resolveAsset} stepsShown={0} revealAll />);
100
+
101
+ let waiting = container.querySelector('[data-part="t"]');
102
+ expect(waiting?.classList.contains("is-unrevealed")).toBe(true);
103
+ expect(waiting?.classList.contains("is-pending")).toBe(false);
104
+ expect(waiting?.hasAttribute("inert")).toBe(false);
105
+ expect(container.querySelector('[data-part="c"]')?.classList.contains("is-unrevealed")).toBe(true);
106
+ expect(container.querySelector('[data-part="h"]')?.classList.contains("is-unrevealed")).toBe(false);
107
+ });
108
+ });
109
+
110
+ describe("renderInsert, for an editor", () => {
111
+ let nested: Step = {
112
+ schemaVersion: 2,
113
+ type: "slide",
114
+ blocks: [
115
+ { id: "h", type: "heading", text: "Title" },
116
+ { id: "e", type: "effect", target: "h", scale: 1.2 },
117
+ {
118
+ id: "cols",
119
+ type: "columns",
120
+ columns: [
121
+ { id: "c1", blocks: [{ id: "t", type: "text", text: "Left" }] },
122
+ { id: "c2", blocks: [] },
123
+ ],
124
+ },
125
+ { id: "b", type: "box", blocks: [] },
126
+ ],
127
+ };
128
+
129
+ function places(step: Step) {
130
+ let { container } = render(
131
+ <StepView
132
+ step={step}
133
+ resolveAsset={resolveAsset}
134
+ renderInsert={({ container, index }) => (
135
+ <i data-insert={`${container.kind}${"id" in container ? `:${container.id}` : ""}@${index}`} />
136
+ )}
137
+ />,
138
+ );
139
+ return [...container.querySelectorAll("[data-insert]")].map((element) => element.getAttribute("data-insert"));
140
+ }
141
+
142
+ it("draws a place before every drawn block and at the end of each container, in reading order", () => {
143
+ expect(places(nested)).toEqual([
144
+ "slide@0",
145
+ "slide@2",
146
+ "column:c1@0",
147
+ "column:c1@1",
148
+ "column:c2@0",
149
+ "slide@3",
150
+ "box:b@0",
151
+ "slide@4",
152
+ ]);
153
+ });
154
+
155
+ it("draws nothing without it", () => {
156
+ let { container } = render(<StepView step={nested} resolveAsset={resolveAsset} />);
157
+ expect(container.querySelector("[data-insert]")).toBeNull();
158
+ });
159
+ });
160
+
161
+ describe("courseChrome, for an editor", () => {
162
+ it("draws the player's bars around the step, inert and hidden from screen readers", () => {
163
+ let { container } = render(
164
+ <StepView step={slide} resolveAsset={resolveAsset} courseChrome={{ title: "Safety", position: 2, count: 5 }} />,
165
+ );
166
+
167
+ let player = container.querySelector(".ob-course-player")!;
168
+ let header = player.querySelector(":scope > .ob-course-player__header")!;
169
+ let controls = player.querySelector(":scope > .ob-course-player__controls")!;
170
+ expect(header.textContent).toContain("Safety");
171
+ expect(header.hasAttribute("inert")).toBe(true);
172
+ expect(header.getAttribute("aria-hidden")).toBe("true");
173
+ expect(controls.hasAttribute("inert")).toBe(true);
174
+ expect(controls.textContent).toContain("2 / 5");
175
+ expect(player.querySelector(":scope > .ob-course-step [data-part='h']")).not.toBeNull();
176
+ expect(player.querySelector(".ob-course-step--standalone")).toBeNull();
177
+ });
178
+
179
+ it("says Complete on the last step and takes the slim size when compact", () => {
180
+ let { container } = render(
181
+ <StepView
182
+ step={slide}
183
+ resolveAsset={resolveAsset}
184
+ courseChrome={{ title: "Safety", position: 5, count: 5, compact: true }}
185
+ />,
186
+ );
187
+
188
+ let player = container.querySelector(".ob-course-player")!;
189
+ expect(player.classList.contains("is-compact")).toBe(true);
190
+ expect(player.querySelector(".ob-course-player__controls")?.textContent).toContain("Complete");
191
+ expect(player.querySelector(".ob-course-player__header")?.getAttribute("data-size")).toBe("sm");
192
+ });
193
+ });
78
194
  });
@@ -1,10 +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";
4
5
  import type { RevealNavigation } from "../document/cues";
5
6
  import type { Step } from "../document/schema";
6
- import { getLocalizedString } from "../i18n";
7
- 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";
8
13
  import { PdfStepView } from "./PdfStepView";
9
14
  import { QuizStepView } from "./QuizStepView";
10
15
  import { SlideStepView } from "./SlideStepView";
@@ -12,6 +17,7 @@ import type { StepBodyProps } from "./StepBodyProps";
12
17
  import { VideoStepView } from "./VideoStepView";
13
18
 
14
19
  import "./StepView.css";
20
+ import "../player/CoursePlayer.css";
15
21
 
16
22
  /** Picks the view for a step's type. A missing document is shown as not available, and counts as done. */
17
23
  export function StepBody({ step, ...props }: StepBodyProps & { step: Step | null }) {
@@ -83,9 +89,35 @@ export interface StepViewProps {
83
89
  * four product logos; given without a logo, the company's name.
84
90
  */
85
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;
104
+ }
105
+
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;
86
118
  }
87
119
 
88
- /** One step on its own, without the course chrome: for previewing a step while it is edited. */
120
+ /** One step on its own, for previewing a step while it is edited, with or without the player's chrome. */
89
121
  export function StepView({
90
122
  step,
91
123
  resolveAsset,
@@ -96,25 +128,74 @@ export function StepView({
96
128
  category,
97
129
  branding,
98
130
  onRevealNavigation,
131
+ revealAll,
132
+ renderInsert,
133
+ courseChrome,
99
134
  }: StepViewProps) {
100
135
  let [subtitles, setSubtitles] = useState(true);
101
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;
102
165
  return (
103
- <div className="ob-course-step ob-course-step--standalone">
104
- <StepFrame category={category} branding={branding}>
105
- <StepBody
106
- step={step}
107
- slideId={slideId}
108
- mode={mode}
109
- resolveAsset={resolveAsset}
110
- quiz={quiz}
111
- stepsShown={stepsShown ?? Number.POSITIVE_INFINITY}
112
- subtitles={subtitles}
113
- onSubtitlesChange={setSubtitles}
114
- onDoneChange={ignore}
115
- onRevealNavigation={onRevealNavigation}
116
- />
117
- </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
+ />
118
199
  </div>
119
200
  );
120
201
  }