bloom-player 2.22.0 → 2.23.0-alpha.1

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A library for displaying Bloom books in iframes or WebViews",
4
4
  "author": "SIL Global",
5
5
  "license": "MIT",
6
- "version": "2.22.0",
6
+ "version": "2.23.0-alpha.1",
7
7
  "packageManager": "pnpm@11.1.2",
8
8
  "private": false,
9
9
  "// sideeffects might need to be ['*.css'] to avoid 'shaking' our CSS, if we ever get tree shaking working": "",
@@ -15,11 +15,17 @@
15
15
  import { ActivityManager } from "../activities/activityManager";
16
16
  import {
17
17
  cancelVideoFirstFramePriming,
18
+ hideVideoAutoplayBlockedHint,
19
+ hideVideoError,
18
20
  kAudioSentence,
19
21
  playAllAudio,
20
22
  playAllVideo,
23
+ startRecordingHighlightingFixesFor,
24
+ stopRecordingHighlightingFixes,
25
+ removeNarrationHighlights,
21
26
  showVideoFirstFrameWhenReady,
22
27
  stopPlayAllVideoPlayback,
28
+ undoHighlightingFixes,
23
29
  urlPrefix,
24
30
  } from "./narration";
25
31
 
@@ -39,26 +45,37 @@ let currentPage: HTMLElement | undefined;
39
45
  // Our latest templates don't have their own change page buttons, just encourage the user
40
46
  // to leave room for the player to add them.
41
47
  let currentChangePageAction: (next: boolean) => void | undefined;
42
- let positionsToRestore: { x: string; y: string; elt: HTMLElement }[] = [];
48
+ // Where each draggable was when play began, keyed by its draggable id rather than by the element
49
+ // itself. The id is what lets us put a COPY of the page back to its starting state as well as the
50
+ // page we prepared -- see restorePositions.
51
+ let positionsToRestore = new Map<string, { x: string; y: string }>();
43
52
 
44
53
  // Save the current positions of all draggables (when entering Play tab, so we can restore them when leaving).
45
54
  const savePositions = (page: HTMLElement) => {
46
- positionsToRestore = [];
55
+ positionsToRestore = new Map();
47
56
  page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
48
- positionsToRestore.push({
57
+ positionsToRestore.set(elt.getAttribute("data-draggable-id")!, {
49
58
  x: elt.style.left,
50
59
  y: elt.style.top,
51
- elt,
52
60
  });
53
61
  });
54
62
  };
55
63
  // Restore the positions saved by savePositions (when leaving the Play tab, or leaving this page altogether
56
64
  // after being in that tab or when clicking the retry button during play).
57
- const restorePositions = () => {
58
- positionsToRestore.forEach((p) => {
59
- p.elt.style.left = p.x;
60
- p.elt.style.top = p.y;
61
- p.elt.classList.remove("bloom-draggedToTarget");
65
+ // Walk the page's draggables once and ask what we recorded for each, rather than searching the page
66
+ // once per recorded id: one pass instead of one per draggable, and no selector built out of an id.
67
+ // (That last is worth little on its own -- getTarget and others still interpolate the same ids, so
68
+ // an id needing escaping would break the activity well before it got here -- but there is no reason
69
+ // to add another.)
70
+ const restorePositions = (page: HTMLElement) => {
71
+ page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
72
+ const saved = positionsToRestore.get(
73
+ elt.getAttribute("data-draggable-id")!,
74
+ );
75
+ if (!saved) return;
76
+ elt.style.left = saved.x;
77
+ elt.style.top = saved.y;
78
+ elt.classList.remove("bloom-draggedToTarget");
62
79
  });
63
80
  };
64
81
 
@@ -110,6 +127,8 @@ export function prepareActivity(
110
127
  );
111
128
  // not sure we need this in BP, but definitely for when Bloom desktop goes to another tab.
112
129
  savePositions(page);
130
+ // So that undoPrepareActivity can undo what playing the page's audio does to its text.
131
+ startRecordingHighlightingFixesFor(page);
113
132
 
114
133
  // Set up event listeners for any change page buttons.
115
134
  const changePageButtons = Array.from(
@@ -305,12 +324,37 @@ const playVideo = (e: MouseEvent) => {
305
324
  // Cleans up whatever prepareActivity() did, especially when switching to another tab.
306
325
  // May also be useful to do when switching pages in player. If not, we may want to move
307
326
  // this out of this runtime file; but it's nice to keep it with prepareActivity.
327
+ // It also removes what playing the page left on it: the mark on an item being dragged, the
328
+ // narration's highlighting and the extra spans it split sentences into, and video error messages.
329
+ // Each of these is removed explicitly, rather than by putting back a snapshot of the page taken
330
+ // when play began: play may one day want to record things worth keeping (whether the reader got
331
+ // the answer first time, say), and a snapshot would wipe those out along with the play markup.
332
+ // So anything new that play adds to the page needs its own line here.
333
+ //
334
+ // Everything here happens to the page you pass, and only to it -- including putting the draggables
335
+ // back where play found them. That matters because the page you pass is not always the page we
336
+ // prepared: Bloom desktop hands us a detached COPY, because that is how a toolbox tool takes its
337
+ // markup off the copy of the page being saved, while the user goes on playing. Such a copy needs
338
+ // the same treatment -- a book should record where the author put the draggables, not where a
339
+ // tester dragged them -- and the live page must be left alone.
308
340
  export function undoPrepareActivity(page: HTMLElement) {
309
- stopPlayAllVideoPlayback();
310
- restorePositions();
341
+ // Only when we are really ending the session, not cleaning a copy of a page still in play.
342
+ if (page === currentPage) {
343
+ stopPlayAllVideoPlayback();
344
+ }
345
+
346
+ restorePositions(page);
347
+ undoHighlightingFixes(page);
348
+ removeNarrationHighlights(page);
349
+
311
350
  // In case we do more editing after leaving the Play tab, we don't want to restore the same positions again
312
- // if we leave the page completely.
313
- positionsToRestore = [];
351
+ // if we leave the page completely. Only when the session is over, though: forgetting them
352
+ // because someone cleaned a copy would leave the live page stuck wherever it was dragged.
353
+ // The same goes for the record of what narration did to the page's sentences.
354
+ if (page === currentPage) {
355
+ positionsToRestore.clear();
356
+ stopRecordingHighlightingFixes();
357
+ }
314
358
 
315
359
  const changePageButtons = Array.from(
316
360
  page.getElementsByClassName(
@@ -331,6 +375,10 @@ export function undoPrepareActivity(page: HTMLElement) {
331
375
 
332
376
  page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
333
377
  elt.removeEventListener("pointerdown", startDrag, { capture: true });
378
+ elt.style.removeProperty("touch-action");
379
+ // bloom-ui-dragging is there if this is a copy taken in the middle of a drag.
380
+ // restorePositions only takes bloom-draggedToTarget off draggables it recorded.
381
+ elt.classList.remove("bloom-ui-dragging", "bloom-draggedToTarget");
334
382
  });
335
383
 
336
384
  Array.from(page.getElementsByTagName("img")).forEach((img: HTMLElement) => {
@@ -341,6 +389,8 @@ export function undoPrepareActivity(page: HTMLElement) {
341
389
  videos.forEach((video) => {
342
390
  video.removeEventListener("pointerdown", playVideo);
343
391
  video.classList.remove("bloom-ui-no-controls");
392
+ hideVideoError(video);
393
+ hideVideoAutoplayBlockedHint(video);
344
394
  });
345
395
  const checkButtons = Array.from(
346
396
  page.getElementsByClassName(
@@ -636,7 +686,7 @@ const showCorrect = (e: MouseEvent) => {
636
686
  if (!currentPage) {
637
687
  return; // huh?? but makes TS happy
638
688
  }
639
- restorePositions(); // any distractors return to start positions.
689
+ restorePositions(currentPage); // any distractors return to start positions.
640
690
  currentPage
641
691
  .querySelectorAll<HTMLElement>("[data-draggable-id]")
642
692
  .forEach((elt: HTMLElement) => {
@@ -858,7 +908,7 @@ export const performTryAgain = (e: MouseEvent) => {
858
908
  //currently I don't think it could be set here, but make sure.
859
909
  classSetter(page, "drag-activity-solution", false);
860
910
  // Restore everything to the starting positions. BL-14482.
861
- restorePositions();
911
+ restorePositions(page);
862
912
  // If we're still playing video, e.g. a 'wrong' video, stop it.
863
913
  stopPlayAllVideoPlayback();
864
914
  };
@@ -897,6 +947,21 @@ export function setDefaultSoundUrls(
897
947
  // Attribute used to mark audio elements preloaded for correct/wrong sounds.
898
948
  const kPreloadSoundAttr = "data-preload-sound";
899
949
 
950
+ // Get the URL to play a sound from. If addPrefix is true, soundFile is the value of a
951
+ // data-sound, data-correct-sound or data-wrong-sound attribute: a file in the book's audio
952
+ // folder. Bloom stores that as the plain file name, NOT URL-encoded (BL-16669), so we must
953
+ // encode it here, when it becomes part of a URL. Otherwise a '#' in the name starts a fragment
954
+ // ("bird#1.mp3" is asked for as "bird", BL-16177) and a '%' starts an escape ("beep%41.mp3" is
955
+ // asked for as "beepA.mp3", BL-16688), and the sound silently never plays. Encoding the whole
956
+ // name is safe because it is a bare file name, with no '/' of its own to preserve.
957
+ // If addPrefix is false, soundFile is one of the default sound URLs, already a URL.
958
+ function getSoundUrl(soundFile: string, addPrefix: boolean): string {
959
+ if (!addPrefix) {
960
+ return soundFile;
961
+ }
962
+ return urlPrefix() + "/audio/" + encodeURIComponent(soundFile);
963
+ }
964
+
900
965
  // Remove any audio elements previously added by preloadSoundsForActivity.
901
966
  // Called from undoPrepareActivity and ActivityContext.stop().
902
967
  export function cleanupActivitySounds(page: HTMLElement): void {
@@ -924,7 +989,7 @@ export function preloadSoundsForActivity(page: HTMLElement): void {
924
989
  }
925
990
  if (!soundFile) return;
926
991
 
927
- const url = (addPrefix ? urlPrefix() + "/audio/" : "") + soundFile;
992
+ const url = getSoundUrl(soundFile, addPrefix);
928
993
  // Avoid duplicate preloads (e.g. if prepareActivity is called more than once)
929
994
  const already = Array.from(
930
995
  page.querySelectorAll<HTMLAudioElement>(`audio[${kPreloadSoundAttr}]`),
@@ -1020,7 +1085,7 @@ function playSound(
1020
1085
  addPrefix = true,
1021
1086
  then?: () => void,
1022
1087
  ) {
1023
- const url = (addPrefix ? urlPrefix() + "/audio/" : "") + soundFile;
1088
+ const url = getSoundUrl(soundFile, addPrefix);
1024
1089
 
1025
1090
  // Reuse a preloaded element if one was prepared by preloadSoundsForActivity.
1026
1091
  // This avoids re-downloading the file and removes the delay before playback starts.
@@ -362,6 +362,8 @@ function fixHighlighting(audioElements: HTMLElement[]) {
362
362
  audioElement,
363
363
  true,
364
364
  );
365
+ // Before changing any of them, so that a nested span is recorded as it was, too.
366
+ matches.forEach(recordBeforeHighlightingFix);
365
367
  matches.forEach((element) => {
366
368
  // Remove all existing highlight classes from element and element's descendants.
367
369
  // These shouldn't be in the dom as the editor is supposed to clean them up,
@@ -381,6 +383,78 @@ function fixHighlighting(audioElements: HTMLElement[]) {
381
383
  });
382
384
  }
383
385
 
386
+ // While a drag activity's page is being played (see prepareActivity in dragActivityRuntime.ts), the
387
+ // markup each sentence on it had before fixHighlighting changed it, keyed by the sentence's id. The id
388
+ // is what lets undoHighlightingFixes put back a COPY of the page as well as the page itself: Bloom
389
+ // desktop saves a copy while the tester goes on playing, and fixHighlighting both splits the text
390
+ // into extra spans and strips the book's own highlight classes, neither of which belongs in the book.
391
+ let pageWhoseHighlightingFixesAreRecorded: HTMLElement | undefined;
392
+ const htmlBeforeHighlightingFixes = new Map<string, string>();
393
+
394
+ // From now on, whenever fixHighlighting is about to change a sentence on 'page' for the first time,
395
+ // record what it was. Forgets anything recorded before.
396
+ export function startRecordingHighlightingFixesFor(page: HTMLElement) {
397
+ pageWhoseHighlightingFixesAreRecorded = page;
398
+ htmlBeforeHighlightingFixes.clear();
399
+ }
400
+
401
+ // Stop recording, and forget what was recorded.
402
+ export function stopRecordingHighlightingFixes() {
403
+ pageWhoseHighlightingFixesAreRecorded = undefined;
404
+ htmlBeforeHighlightingFixes.clear();
405
+ }
406
+
407
+ function recordBeforeHighlightingFix(element: HTMLElement) {
408
+ if (
409
+ !element.id ||
410
+ htmlBeforeHighlightingFixes.has(element.id) || // keep the markup from before the first fix
411
+ !pageWhoseHighlightingFixesAreRecorded?.contains(element)
412
+ ) {
413
+ return;
414
+ }
415
+ htmlBeforeHighlightingFixes.set(element.id, element.outerHTML);
416
+ }
417
+
418
+ // Put each recorded sentence in 'root' (the page being played, or a copy of it) back as it was
419
+ // before fixHighlighting changed it. A sentence whose id is not unique in 'root' is left as it is:
420
+ // the record cannot say which of them it came from, and putting one's text into the other would
421
+ // lose words, which is worse than keeping the extra highlight spans.
422
+ export function undoHighlightingFixes(root: HTMLElement) {
423
+ if (htmlBeforeHighlightingFixes.size === 0) {
424
+ return; // the usual case: nothing on the page has been read aloud
425
+ }
426
+ // Walk the spans once and look each up, rather than building a selector out of an id.
427
+ const spans = Array.from(root.querySelectorAll<HTMLElement>("span[id]"));
428
+ const countOfId = new Map<string, number>();
429
+ spans.forEach((span) =>
430
+ countOfId.set(span.id, (countOfId.get(span.id) ?? 0) + 1),
431
+ );
432
+ spans.forEach((span) => {
433
+ const original = htmlBeforeHighlightingFixes.get(span.id);
434
+ // A span inside one already put back is no longer in 'root', and was put back with it.
435
+ if (
436
+ original === undefined ||
437
+ countOfId.get(span.id) !== 1 ||
438
+ !root.contains(span)
439
+ ) {
440
+ return;
441
+ }
442
+ span.outerHTML = original;
443
+ });
444
+ }
445
+
446
+ // Remove from 'root' the classes narration adds to show what is playing.
447
+ export function removeNarrationHighlights(root: HTMLElement) {
448
+ const classes = [
449
+ "ui-audioCurrent",
450
+ "ui-audioCurrentImg",
451
+ kSuppressHighlightClass,
452
+ ];
453
+ root.querySelectorAll(classes.map((c) => "." + c).join(",")).forEach(
454
+ (element) => element.classList.remove(...classes),
455
+ );
456
+ }
457
+
384
458
  // Remove all existing highlight classes from element and element's descendants.
385
459
  function removeHighlightClasses(element: HTMLElement) {
386
460
  element.classList.remove(kDisableHighlightClass);