bloom-player 2.22.0-alpha.2 → 2.22.0-alpha.3

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-alpha.2",
6
+ "version": "2.22.0-alpha.3",
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
 
@@ -121,6 +127,8 @@ export function prepareActivity(
121
127
  );
122
128
  // not sure we need this in BP, but definitely for when Bloom desktop goes to another tab.
123
129
  savePositions(page);
130
+ // So that undoPrepareActivity can undo what playing the page's audio does to its text.
131
+ startRecordingHighlightingFixesFor(page);
124
132
 
125
133
  // Set up event listeners for any change page buttons.
126
134
  const changePageButtons = Array.from(
@@ -316,6 +324,12 @@ const playVideo = (e: MouseEvent) => {
316
324
  // Cleans up whatever prepareActivity() did, especially when switching to another tab.
317
325
  // May also be useful to do when switching pages in player. If not, we may want to move
318
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.
319
333
  //
320
334
  // Everything here happens to the page you pass, and only to it -- including putting the draggables
321
335
  // back where play found them. That matters because the page you pass is not always the page we
@@ -330,12 +344,16 @@ export function undoPrepareActivity(page: HTMLElement) {
330
344
  }
331
345
 
332
346
  restorePositions(page);
347
+ undoHighlightingFixes(page);
348
+ removeNarrationHighlights(page);
333
349
 
334
350
  // In case we do more editing after leaving the Play tab, we don't want to restore the same positions again
335
351
  // if we leave the page completely. Only when the session is over, though: forgetting them
336
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.
337
354
  if (page === currentPage) {
338
355
  positionsToRestore.clear();
356
+ stopRecordingHighlightingFixes();
339
357
  }
340
358
 
341
359
  const changePageButtons = Array.from(
@@ -357,6 +375,10 @@ export function undoPrepareActivity(page: HTMLElement) {
357
375
 
358
376
  page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
359
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");
360
382
  });
361
383
 
362
384
  Array.from(page.getElementsByTagName("img")).forEach((img: HTMLElement) => {
@@ -367,6 +389,8 @@ export function undoPrepareActivity(page: HTMLElement) {
367
389
  videos.forEach((video) => {
368
390
  video.removeEventListener("pointerdown", playVideo);
369
391
  video.classList.remove("bloom-ui-no-controls");
392
+ hideVideoError(video);
393
+ hideVideoAutoplayBlockedHint(video);
370
394
  });
371
395
  const checkButtons = Array.from(
372
396
  page.getElementsByClassName(
@@ -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);