bloom-player 2.20.3-alpha.1 → 2.21.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/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.20.3-alpha.1",
6
+ "version": "2.21.0",
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": "",
@@ -39,37 +39,26 @@ let currentPage: HTMLElement | undefined;
39
39
  // Our latest templates don't have their own change page buttons, just encourage the user
40
40
  // to leave room for the player to add them.
41
41
  let currentChangePageAction: (next: boolean) => void | undefined;
42
- // Where each draggable was when play began, keyed by its draggable id rather than by the element
43
- // itself. The id is what lets us put a COPY of the page back to its starting state as well as the
44
- // page we prepared -- see restorePositions.
45
- let positionsToRestore = new Map<string, { x: string; y: string }>();
42
+ let positionsToRestore: { x: string; y: string; elt: HTMLElement }[] = [];
46
43
 
47
44
  // Save the current positions of all draggables (when entering Play tab, so we can restore them when leaving).
48
45
  const savePositions = (page: HTMLElement) => {
49
- positionsToRestore = new Map();
46
+ positionsToRestore = [];
50
47
  page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
51
- positionsToRestore.set(elt.getAttribute("data-draggable-id")!, {
48
+ positionsToRestore.push({
52
49
  x: elt.style.left,
53
50
  y: elt.style.top,
51
+ elt,
54
52
  });
55
53
  });
56
54
  };
57
55
  // Restore the positions saved by savePositions (when leaving the Play tab, or leaving this page altogether
58
56
  // after being in that tab or when clicking the retry button during play).
59
- // Walk the page's draggables once and ask what we recorded for each, rather than searching the page
60
- // once per recorded id: one pass instead of one per draggable, and no selector built out of an id.
61
- // (That last is worth little on its own -- getTarget and others still interpolate the same ids, so
62
- // an id needing escaping would break the activity well before it got here -- but there is no reason
63
- // to add another.)
64
- const restorePositions = (page: HTMLElement) => {
65
- page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
66
- const saved = positionsToRestore.get(
67
- elt.getAttribute("data-draggable-id")!,
68
- );
69
- if (!saved) return;
70
- elt.style.left = saved.x;
71
- elt.style.top = saved.y;
72
- elt.classList.remove("bloom-draggedToTarget");
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");
73
62
  });
74
63
  };
75
64
 
@@ -316,27 +305,12 @@ const playVideo = (e: MouseEvent) => {
316
305
  // Cleans up whatever prepareActivity() did, especially when switching to another tab.
317
306
  // May also be useful to do when switching pages in player. If not, we may want to move
318
307
  // this out of this runtime file; but it's nice to keep it with prepareActivity.
319
- //
320
- // Everything here happens to the page you pass, and only to it -- including putting the draggables
321
- // back where play found them. That matters because the page you pass is not always the page we
322
- // prepared: Bloom desktop hands us a detached COPY, because that is how a toolbox tool takes its
323
- // markup off the copy of the page being saved, while the user goes on playing. Such a copy needs
324
- // the same treatment -- a book should record where the author put the draggables, not where a
325
- // tester dragged them -- and the live page must be left alone.
326
308
  export function undoPrepareActivity(page: HTMLElement) {
327
- // Only when we are really ending the session, not cleaning a copy of a page still in play.
328
- if (page === currentPage) {
329
- stopPlayAllVideoPlayback();
330
- }
331
-
332
- restorePositions(page);
333
-
309
+ stopPlayAllVideoPlayback();
310
+ restorePositions();
334
311
  // In case we do more editing after leaving the Play tab, we don't want to restore the same positions again
335
- // if we leave the page completely. Only when the session is over, though: forgetting them
336
- // because someone cleaned a copy would leave the live page stuck wherever it was dragged.
337
- if (page === currentPage) {
338
- positionsToRestore.clear();
339
- }
312
+ // if we leave the page completely.
313
+ positionsToRestore = [];
340
314
 
341
315
  const changePageButtons = Array.from(
342
316
  page.getElementsByClassName(
@@ -662,7 +636,7 @@ const showCorrect = (e: MouseEvent) => {
662
636
  if (!currentPage) {
663
637
  return; // huh?? but makes TS happy
664
638
  }
665
- restorePositions(currentPage); // any distractors return to start positions.
639
+ restorePositions(); // any distractors return to start positions.
666
640
  currentPage
667
641
  .querySelectorAll<HTMLElement>("[data-draggable-id]")
668
642
  .forEach((elt: HTMLElement) => {
@@ -884,7 +858,7 @@ export const performTryAgain = (e: MouseEvent) => {
884
858
  //currently I don't think it could be set here, but make sure.
885
859
  classSetter(page, "drag-activity-solution", false);
886
860
  // Restore everything to the starting positions. BL-14482.
887
- restorePositions(page);
861
+ restorePositions();
888
862
  // If we're still playing video, e.g. a 'wrong' video, stop it.
889
863
  stopPlayAllVideoPlayback();
890
864
  };
@@ -1032,12 +1032,12 @@ function getPreloadPlayer(): HTMLAudioElement {
1032
1032
  }
1033
1033
 
1034
1034
  function preloadAudioForElement(element: Element): void {
1035
- const firstAudioSentence = getFirstAudioSentenceWithinElement(element);
1036
- const id = firstAudioSentence ? firstAudioSentence.id : element.id;
1035
+ const audioElement = getFirstAudioSentenceWithinElement(element) ?? element;
1036
+ const id = audioElement.id;
1037
1037
  if (preloadedAudioId === id) return; // already preloading this one
1038
1038
 
1039
1039
  const url =
1040
- currentAudioUrl(id) +
1040
+ currentAudioUrl(id, audioElement) +
1041
1041
  "?nocache=" +
1042
1042
  new Date().getTime() +
1043
1043
  "&optional=true";
@@ -1085,8 +1085,25 @@ function updatePlayerStatus() {
1085
1085
  player.setAttribute("src", src);
1086
1086
  }
1087
1087
 
1088
- function currentAudioUrl(id: string): string {
1089
- const result = urlPrefix() + "/audio/" + id + ".mp3";
1088
+ // Bloom can name a recording's file in data-audio-src on the element that carries its id,
1089
+ // e.g. data-audio-src="<id>.opus". Absent => <id>.mp3 (all older books). Like the music and
1090
+ // drag-activity sound attributes, the value goes into the URL as is, so it must be URL-safe.
1091
+ export function getNarrationFileName(
1092
+ id: string,
1093
+ element: Element | null | undefined,
1094
+ ): string {
1095
+ return element?.getAttribute("data-audio-src") || id + ".mp3";
1096
+ }
1097
+
1098
+ // Neighboring pages stay in the DOM and can reuse ids, so prefer the element we were
1099
+ // given, then one on the current page, before searching the whole document.
1100
+ export function currentAudioUrl(id: string, element?: Element | null): string {
1101
+ const audioElement =
1102
+ element ??
1103
+ currentPlayPage?.querySelector(`[id="${id}"]`) ??
1104
+ document.getElementById(id);
1105
+ const result =
1106
+ urlPrefix() + "/audio/" + getNarrationFileName(id, audioElement);
1090
1107
  return result;
1091
1108
  }
1092
1109