bloom-player 2.20.1 → 2.20.3-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/README.md +14 -1
- package/dist/bloomPlayer.BAVrzru3.js +7995 -0
- package/dist/bloomplayer.htm +1 -1
- package/lib/narration.d.ts +1 -0
- package/lib/shared.es.js +1160 -1107
- package/lib/shared.es.js.map +1 -1
- package/package.json +1 -1
- package/src/shared/dragActivityRuntime.ts +126 -45
- package/src/shared/narration.ts +119 -76
- package/src/shared/scrolling.ts +9 -3
- package/dist/bloomPlayer.CzxSC7ao.js +0 -7995
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.1",
|
|
6
|
+
"version": "2.20.3-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": "",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import { ActivityManager } from "../activities/activityManager";
|
|
16
16
|
import {
|
|
17
|
+
cancelVideoFirstFramePriming,
|
|
17
18
|
kAudioSentence,
|
|
18
19
|
playAllAudio,
|
|
19
20
|
playAllVideo,
|
|
@@ -38,26 +39,37 @@ let currentPage: HTMLElement | undefined;
|
|
|
38
39
|
// Our latest templates don't have their own change page buttons, just encourage the user
|
|
39
40
|
// to leave room for the player to add them.
|
|
40
41
|
let currentChangePageAction: (next: boolean) => void | undefined;
|
|
41
|
-
|
|
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
46
|
|
|
43
47
|
// Save the current positions of all draggables (when entering Play tab, so we can restore them when leaving).
|
|
44
48
|
const savePositions = (page: HTMLElement) => {
|
|
45
|
-
positionsToRestore =
|
|
46
|
-
page.querySelectorAll("[data-draggable-id]").forEach((elt
|
|
47
|
-
positionsToRestore.
|
|
49
|
+
positionsToRestore = new Map();
|
|
50
|
+
page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
|
|
51
|
+
positionsToRestore.set(elt.getAttribute("data-draggable-id")!, {
|
|
48
52
|
x: elt.style.left,
|
|
49
53
|
y: elt.style.top,
|
|
50
|
-
elt,
|
|
51
54
|
});
|
|
52
55
|
});
|
|
53
56
|
};
|
|
54
57
|
// Restore the positions saved by savePositions (when leaving the Play tab, or leaving this page altogether
|
|
55
58
|
// after being in that tab or when clicking the retry button during play).
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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");
|
|
61
73
|
});
|
|
62
74
|
};
|
|
63
75
|
|
|
@@ -66,7 +78,9 @@ export function adjustDraggablesForLanguage(page: HTMLElement) {
|
|
|
66
78
|
if (page.getAttribute("data-activity") !== "drag-letter-to-target") {
|
|
67
79
|
return;
|
|
68
80
|
}
|
|
69
|
-
const draggables = Array.from(
|
|
81
|
+
const draggables = Array.from(
|
|
82
|
+
page.querySelectorAll<HTMLElement>("[data-draggable-id]"),
|
|
83
|
+
);
|
|
70
84
|
draggables.shift(); // The first one is always visible.
|
|
71
85
|
draggables.forEach((draggable: HTMLElement) => {
|
|
72
86
|
const shouldBeVisible = !!(
|
|
@@ -110,7 +124,9 @@ export function prepareActivity(
|
|
|
110
124
|
|
|
111
125
|
// Set up event listeners for any change page buttons.
|
|
112
126
|
const changePageButtons = Array.from(
|
|
113
|
-
page.getElementsByClassName(
|
|
127
|
+
page.getElementsByClassName(
|
|
128
|
+
"bloom-change-page-button",
|
|
129
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
114
130
|
);
|
|
115
131
|
changePageButtons.forEach((b) =>
|
|
116
132
|
b.addEventListener("click", changePageButtonClicked),
|
|
@@ -138,7 +154,9 @@ export function prepareActivity(
|
|
|
138
154
|
// Add event listeners to draggables to start dragging.
|
|
139
155
|
targetPositions = [];
|
|
140
156
|
originalPositions = new Map<HTMLElement, { x: number; y: number }>();
|
|
141
|
-
const draggables = Array.from(
|
|
157
|
+
const draggables = Array.from(
|
|
158
|
+
page.querySelectorAll<HTMLElement>("[data-draggable-id]"),
|
|
159
|
+
);
|
|
142
160
|
const targets: HTMLElement[] = [];
|
|
143
161
|
draggables.forEach((elt: HTMLElement) => {
|
|
144
162
|
const targetId = elt.getAttribute("data-draggable-id");
|
|
@@ -180,9 +198,7 @@ export function prepareActivity(
|
|
|
180
198
|
// Ensure the first frame is visible. The transparent poster (set globally by
|
|
181
199
|
// bloom-player at book load) hides the video until playback begins. Non-draggable
|
|
182
200
|
// videos get a play+pause first-frame trick in video.ts HandlePageVisible, but
|
|
183
|
-
// draggable videos are skipped there
|
|
184
|
-
// (e.g. cold cache after a build), play() fails silently, leaving the video blank.
|
|
185
|
-
// We use a loadeddata listener so the trick runs whenever the data is available.
|
|
201
|
+
// draggable videos are skipped there, so they depend entirely on this call.
|
|
186
202
|
showVideoFirstFrameWhenReady(video);
|
|
187
203
|
}
|
|
188
204
|
});
|
|
@@ -190,7 +206,9 @@ export function prepareActivity(
|
|
|
190
206
|
// Add event listeners to (other) text items that should play audio when clicked.
|
|
191
207
|
const dontPlayWhenClicked = draggables.concat(targets);
|
|
192
208
|
const otherTextItems = Array.from(
|
|
193
|
-
page.getElementsByClassName(
|
|
209
|
+
page.getElementsByClassName(
|
|
210
|
+
"bloom-visibility-code-on",
|
|
211
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
194
212
|
).filter((e) => {
|
|
195
213
|
var top = e.closest(kLegacyCanvasElementSelector) as HTMLElement;
|
|
196
214
|
if (!top) {
|
|
@@ -209,13 +227,19 @@ export function prepareActivity(
|
|
|
209
227
|
|
|
210
228
|
// Add event listeners to check, try again, and show correct buttons.
|
|
211
229
|
const checkButtons = Array.from(
|
|
212
|
-
page.getElementsByClassName(
|
|
230
|
+
page.getElementsByClassName(
|
|
231
|
+
"check-button",
|
|
232
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
213
233
|
);
|
|
214
234
|
const tryAgainButtons = Array.from(
|
|
215
|
-
page.getElementsByClassName(
|
|
235
|
+
page.getElementsByClassName(
|
|
236
|
+
"try-again-button",
|
|
237
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
216
238
|
);
|
|
217
239
|
const showCorrectButtons = Array.from(
|
|
218
|
-
page.getElementsByClassName(
|
|
240
|
+
page.getElementsByClassName(
|
|
241
|
+
"show-correct-button",
|
|
242
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
219
243
|
);
|
|
220
244
|
|
|
221
245
|
checkButtons.forEach((elt: HTMLElement) => {
|
|
@@ -228,7 +252,9 @@ export function prepareActivity(
|
|
|
228
252
|
elt.addEventListener("click", showCorrect);
|
|
229
253
|
});
|
|
230
254
|
|
|
231
|
-
const soundItems = Array.from(
|
|
255
|
+
const soundItems = Array.from(
|
|
256
|
+
page.querySelectorAll<HTMLElement>("[data-sound]"),
|
|
257
|
+
);
|
|
232
258
|
soundItems.forEach((elt: HTMLElement) => {
|
|
233
259
|
elt.addEventListener("click", playSoundOf);
|
|
234
260
|
});
|
|
@@ -254,7 +280,11 @@ export function prepareActivity(
|
|
|
254
280
|
// Break any order-sentence element into words and
|
|
255
281
|
// randomize word order in sentence for reader to sort
|
|
256
282
|
const prepareOrderSentenceActivity = (page: HTMLElement) => {
|
|
257
|
-
Array.from(
|
|
283
|
+
Array.from(
|
|
284
|
+
page.getElementsByClassName(
|
|
285
|
+
"drag-item-order-sentence",
|
|
286
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
287
|
+
).forEach(
|
|
258
288
|
(elt: HTMLElement) => {
|
|
259
289
|
const contentElt = elt.getElementsByClassName(
|
|
260
290
|
"bloom-content1 bloom-visibility-code-on",
|
|
@@ -277,33 +307,55 @@ const prepareOrderSentenceActivity = (page: HTMLElement) => {
|
|
|
277
307
|
|
|
278
308
|
const playVideo = (e: MouseEvent) => {
|
|
279
309
|
const video = e.currentTarget as HTMLVideoElement;
|
|
310
|
+
// The user asked for real playback; a pending first-frame priming attempt
|
|
311
|
+
// must not mute or pause it.
|
|
312
|
+
cancelVideoFirstFramePriming(video);
|
|
280
313
|
video.play();
|
|
281
314
|
};
|
|
282
315
|
|
|
283
316
|
// Cleans up whatever prepareActivity() did, especially when switching to another tab.
|
|
284
317
|
// May also be useful to do when switching pages in player. If not, we may want to move
|
|
285
318
|
// 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.
|
|
286
326
|
export function undoPrepareActivity(page: HTMLElement) {
|
|
287
|
-
|
|
288
|
-
|
|
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
|
+
|
|
289
334
|
// In case we do more editing after leaving the Play tab, we don't want to restore the same positions again
|
|
290
|
-
// if we leave the page completely.
|
|
291
|
-
|
|
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
|
+
}
|
|
292
340
|
|
|
293
341
|
const changePageButtons = Array.from(
|
|
294
|
-
page.getElementsByClassName(
|
|
342
|
+
page.getElementsByClassName(
|
|
343
|
+
"bloom-change-page-button",
|
|
344
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
295
345
|
);
|
|
296
346
|
changePageButtons.forEach((b) =>
|
|
297
347
|
b.removeEventListener("click", changePageButtonClicked),
|
|
298
348
|
);
|
|
299
349
|
|
|
300
|
-
Array.from(
|
|
301
|
-
(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
)
|
|
350
|
+
Array.from(
|
|
351
|
+
page.getElementsByClassName(
|
|
352
|
+
"bloom-visibility-code-on",
|
|
353
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
354
|
+
).forEach((e) => {
|
|
355
|
+
e.removeEventListener("pointerdown", playAudioOfTarget);
|
|
356
|
+
});
|
|
305
357
|
|
|
306
|
-
page.querySelectorAll("[data-draggable-id]").forEach((elt
|
|
358
|
+
page.querySelectorAll<HTMLElement>("[data-draggable-id]").forEach((elt) => {
|
|
307
359
|
elt.removeEventListener("pointerdown", startDrag, { capture: true });
|
|
308
360
|
});
|
|
309
361
|
|
|
@@ -317,13 +369,19 @@ export function undoPrepareActivity(page: HTMLElement) {
|
|
|
317
369
|
video.classList.remove("bloom-ui-no-controls");
|
|
318
370
|
});
|
|
319
371
|
const checkButtons = Array.from(
|
|
320
|
-
page.getElementsByClassName(
|
|
372
|
+
page.getElementsByClassName(
|
|
373
|
+
"check-button",
|
|
374
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
321
375
|
);
|
|
322
376
|
const tryAgainButtons = Array.from(
|
|
323
|
-
page.getElementsByClassName(
|
|
377
|
+
page.getElementsByClassName(
|
|
378
|
+
"try-again-button",
|
|
379
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
324
380
|
);
|
|
325
381
|
const showCorrectButtons = Array.from(
|
|
326
|
-
page.getElementsByClassName(
|
|
382
|
+
page.getElementsByClassName(
|
|
383
|
+
"show-correct-button",
|
|
384
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
327
385
|
);
|
|
328
386
|
|
|
329
387
|
checkButtons.forEach((elt: HTMLElement) => {
|
|
@@ -339,13 +397,17 @@ export function undoPrepareActivity(page: HTMLElement) {
|
|
|
339
397
|
// In Bloom Player, this will have been done by other play code, since data-sound is not
|
|
340
398
|
// specific to games. But we're adding a listener for the same function, so it doesn't matter.
|
|
341
399
|
// In Bloom desktop, we need this to make clicking data-sound elements work in Play mode.
|
|
342
|
-
const soundItems = Array.from(
|
|
400
|
+
const soundItems = Array.from(
|
|
401
|
+
page.querySelectorAll<HTMLElement>("[data-sound]"),
|
|
402
|
+
);
|
|
343
403
|
soundItems.forEach((elt: HTMLElement) => {
|
|
344
404
|
elt.removeEventListener("click", playSoundOf);
|
|
345
405
|
});
|
|
346
406
|
|
|
347
407
|
Array.from(
|
|
348
|
-
page.getElementsByClassName(
|
|
408
|
+
page.getElementsByClassName(
|
|
409
|
+
"drag-item-random-sentence",
|
|
410
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
349
411
|
).forEach((elt: HTMLElement) => {
|
|
350
412
|
elt.parentElement?.removeChild(elt);
|
|
351
413
|
});
|
|
@@ -600,9 +662,9 @@ const showCorrect = (e: MouseEvent) => {
|
|
|
600
662
|
if (!currentPage) {
|
|
601
663
|
return; // huh?? but makes TS happy
|
|
602
664
|
}
|
|
603
|
-
restorePositions(); // any distractors return to start positions.
|
|
665
|
+
restorePositions(currentPage); // any distractors return to start positions.
|
|
604
666
|
currentPage
|
|
605
|
-
.querySelectorAll("[data-draggable-id]")
|
|
667
|
+
.querySelectorAll<HTMLElement>("[data-draggable-id]")
|
|
606
668
|
.forEach((elt: HTMLElement) => {
|
|
607
669
|
const targetId = elt.getAttribute("data-draggable-id");
|
|
608
670
|
const target = currentPage?.querySelector(
|
|
@@ -631,7 +693,9 @@ const showCorrect = (e: MouseEvent) => {
|
|
|
631
693
|
elt.style.top = y + "px";
|
|
632
694
|
});
|
|
633
695
|
Array.from(
|
|
634
|
-
currentPage.getElementsByClassName(
|
|
696
|
+
currentPage.getElementsByClassName(
|
|
697
|
+
"drag-item-random-sentence",
|
|
698
|
+
) as HTMLCollectionOf<HTMLElement>,
|
|
635
699
|
).forEach((container: HTMLElement) => {
|
|
636
700
|
const correctAnswer =
|
|
637
701
|
container.getAttribute("data-answer")?.split(" ") ?? [];
|
|
@@ -651,7 +715,7 @@ const showCorrect = (e: MouseEvent) => {
|
|
|
651
715
|
// Play any videos that are part of a correct answer, in document order.
|
|
652
716
|
const videoElements: HTMLVideoElement[] = [];
|
|
653
717
|
currentPage!
|
|
654
|
-
.querySelectorAll("[data-draggable-id]")
|
|
718
|
+
.querySelectorAll<HTMLElement>("[data-draggable-id]")
|
|
655
719
|
.forEach((elt: HTMLElement) => {
|
|
656
720
|
const targetId = elt.getAttribute("data-draggable-id");
|
|
657
721
|
const target = currentPage?.querySelector(
|
|
@@ -751,7 +815,9 @@ const stopDrag = (e: PointerEvent) => {
|
|
|
751
815
|
// back to its original position.
|
|
752
816
|
// Enhance: animate?
|
|
753
817
|
const page = dragTarget.closest(".bloom-page") as HTMLElement;
|
|
754
|
-
const draggables = Array.from(
|
|
818
|
+
const draggables = Array.from(
|
|
819
|
+
page.querySelectorAll<HTMLElement>("[data-draggable-id]"),
|
|
820
|
+
);
|
|
755
821
|
draggables.forEach((elt: HTMLElement) => {
|
|
756
822
|
if (elt === dragTarget) {
|
|
757
823
|
return;
|
|
@@ -818,7 +884,7 @@ export const performTryAgain = (e: MouseEvent) => {
|
|
|
818
884
|
//currently I don't think it could be set here, but make sure.
|
|
819
885
|
classSetter(page, "drag-activity-solution", false);
|
|
820
886
|
// Restore everything to the starting positions. BL-14482.
|
|
821
|
-
restorePositions();
|
|
887
|
+
restorePositions(page);
|
|
822
888
|
// If we're still playing video, e.g. a 'wrong' video, stop it.
|
|
823
889
|
stopPlayAllVideoPlayback();
|
|
824
890
|
};
|
|
@@ -944,6 +1010,19 @@ function showCorrectOrWrongItems(page: HTMLElement, correct: boolean) {
|
|
|
944
1010
|
classSetter(page, "drag-activity-wrong", !correct);
|
|
945
1011
|
|
|
946
1012
|
const playOtherStuff = () => {
|
|
1013
|
+
// This runs when the correct/wrong sound finishes, which may be after
|
|
1014
|
+
// the user has already clicked "Show Correct" or "Try Again". Those
|
|
1015
|
+
// take the page out of the state this feedback belongs to, and
|
|
1016
|
+
// "Show Correct" starts playing the solution videos, which the
|
|
1017
|
+
// playAllVideo call below would silently cancel (BL-16146). If the
|
|
1018
|
+
// page is no longer showing the state we were queued for, do nothing.
|
|
1019
|
+
if (
|
|
1020
|
+
!page.parentElement?.classList.contains(
|
|
1021
|
+
correct ? "drag-activity-correct" : "drag-activity-wrong",
|
|
1022
|
+
)
|
|
1023
|
+
) {
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
947
1026
|
const elementsMadeVisible = Array.from(
|
|
948
1027
|
page.getElementsByClassName(
|
|
949
1028
|
correct ? "drag-item-correct" : "drag-item-wrong",
|
|
@@ -1029,7 +1108,9 @@ function playSound(
|
|
|
1029
1108
|
|
|
1030
1109
|
function checkDraggables(page: HTMLElement) {
|
|
1031
1110
|
let allCorrect = true;
|
|
1032
|
-
const draggables = Array.from(
|
|
1111
|
+
const draggables = Array.from(
|
|
1112
|
+
page.querySelectorAll<HTMLElement>("[data-draggable-id]"),
|
|
1113
|
+
);
|
|
1033
1114
|
draggables.forEach((draggableToCheck: HTMLElement) => {
|
|
1034
1115
|
const targetId = draggableToCheck.getAttribute("data-draggable-id");
|
|
1035
1116
|
const target = page.querySelector(
|
package/src/shared/narration.ts
CHANGED
|
@@ -386,8 +386,8 @@ function removeHighlightClasses(element: HTMLElement) {
|
|
|
386
386
|
element.classList.remove(kDisableHighlightClass);
|
|
387
387
|
element.classList.remove(kEnableHighlightClass);
|
|
388
388
|
|
|
389
|
-
Array.from(element.children).forEach((child
|
|
390
|
-
removeHighlightClasses(child);
|
|
389
|
+
Array.from(element.children).forEach((child) => {
|
|
390
|
+
removeHighlightClasses(child as HTMLElement);
|
|
391
391
|
});
|
|
392
392
|
}
|
|
393
393
|
|
|
@@ -1266,7 +1266,7 @@ function getPageAudioElements(
|
|
|
1266
1266
|
page?: HTMLElement,
|
|
1267
1267
|
canvasToExclude?: HTMLElement,
|
|
1268
1268
|
): HTMLElement[] {
|
|
1269
|
-
return [].concat.apply(
|
|
1269
|
+
return ([] as HTMLElement[]).concat.apply(
|
|
1270
1270
|
[],
|
|
1271
1271
|
getPagePlayableDivs(page, canvasToExclude).map((x) =>
|
|
1272
1272
|
findAll(".audio-sentence", x, true),
|
|
@@ -1401,94 +1401,134 @@ export function isTransientVideoPlayFailure(reason: any): boolean {
|
|
|
1401
1401
|
return message.includes("interrupted by a call to pause()");
|
|
1402
1402
|
}
|
|
1403
1403
|
|
|
1404
|
-
//
|
|
1405
|
-
//
|
|
1406
|
-
|
|
1404
|
+
// Cancel functions for in-progress first-frame priming attempts (see
|
|
1405
|
+
// showVideoFirstFrameWhenReady below), keyed by the video being primed.
|
|
1406
|
+
const firstFramePrimingCancels = new WeakMap<HTMLVideoElement, () => void>();
|
|
1407
|
+
|
|
1408
|
+
// Anything about to start real playback of a video should call this first:
|
|
1409
|
+
// if a first-frame priming attempt is in progress on the video, it is
|
|
1410
|
+
// cancelled (restoring the video's muted state and removing the transparent
|
|
1411
|
+
// poster) so the primer can't mute or pause the real playback.
|
|
1412
|
+
export function cancelVideoFirstFramePriming(video: HTMLVideoElement) {
|
|
1413
|
+
firstFramePrimingCancels.get(video)?.();
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
// Attempt to show a video's first frame by briefly starting muted playback and
|
|
1417
|
+
// then pausing. We mute while priming because gestureless unmuted play() is
|
|
1418
|
+
// rejected by autoplay policies (iOS especially, where a past tap elsewhere on
|
|
1419
|
+
// the page grants nothing). Calling play() is also what forces browsers that
|
|
1420
|
+
// don't preload video data (again, iOS Safari) to fetch and decode the first
|
|
1421
|
+
// frame, so we must not wait for loadeddata first: on iOS it typically never
|
|
1422
|
+
// fires until something plays. (BL-16146)
|
|
1423
|
+
// The callback allows callers to decide at the moment playback starts whether
|
|
1424
|
+
// pausing is still desired (for example, page-level logic may stop pausing
|
|
1425
|
+
// after initial load).
|
|
1407
1426
|
export function showVideoFirstFrameWhenReady(
|
|
1408
1427
|
video: HTMLVideoElement,
|
|
1409
1428
|
shouldPauseAfterPlaying: () => boolean = () => true,
|
|
1410
1429
|
onAutoplayBlocked: () => void = () => showVideoAutoplayBlockedHint(video),
|
|
1411
1430
|
) {
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1431
|
+
// If something else has already started playback, don't interfere.
|
|
1432
|
+
if (!video.paused) {
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
// If an earlier priming attempt on this video is still armed (e.g. the
|
|
1436
|
+
// page was left and re-entered within the give-up window), cancel it so
|
|
1437
|
+
// there is only ever one live attempt per video — otherwise the old
|
|
1438
|
+
// attempt's listeners/timer linger, and this attempt would wrongly record
|
|
1439
|
+
// the old attempt's muting as the video's real muted state.
|
|
1440
|
+
cancelVideoFirstFramePriming(video);
|
|
1441
|
+
const wasMuted = video.muted;
|
|
1442
|
+
let giveUpTimeout: number | undefined;
|
|
1443
|
+
let done = false;
|
|
1444
|
+
// Idempotent cleanup: whatever way the priming attempt ends, restore the
|
|
1445
|
+
// video's real muted state and stop listening.
|
|
1446
|
+
const finish = () => {
|
|
1447
|
+
if (done) {
|
|
1415
1448
|
return;
|
|
1416
1449
|
}
|
|
1417
|
-
|
|
1418
|
-
//
|
|
1419
|
-
|
|
1450
|
+
done = true;
|
|
1451
|
+
// Deregister only our own cancel function, in case a newer priming
|
|
1452
|
+
// attempt has already replaced it.
|
|
1453
|
+
if (firstFramePrimingCancels.get(video) === cancel) {
|
|
1454
|
+
firstFramePrimingCancels.delete(video);
|
|
1455
|
+
}
|
|
1456
|
+
window.clearTimeout(giveUpTimeout);
|
|
1457
|
+
video.removeEventListener("playing", playingListener);
|
|
1458
|
+
video.muted = wasMuted;
|
|
1459
|
+
};
|
|
1460
|
+
// Let real-playback initiators cancel this priming attempt so it can't
|
|
1461
|
+
// mute or pause the playback they are about to start.
|
|
1462
|
+
const cancel = () => {
|
|
1463
|
+
finish();
|
|
1464
|
+
// Real playback is starting; make sure our transparent poster can't
|
|
1465
|
+
// hide it.
|
|
1466
|
+
video.removeAttribute("poster");
|
|
1467
|
+
};
|
|
1468
|
+
firstFramePrimingCancels.set(video, cancel);
|
|
1469
|
+
const playingListener = () => {
|
|
1470
|
+
hideVideoAutoplayBlockedHint(video);
|
|
1471
|
+
if (!shouldPauseAfterPlaying()) {
|
|
1472
|
+
// Real playback has taken over; restore audio and get out of the way.
|
|
1473
|
+
finish();
|
|
1474
|
+
video.removeAttribute("poster");
|
|
1420
1475
|
return;
|
|
1421
1476
|
}
|
|
1422
|
-
const
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
if (!shouldPauseAfterPlaying()) {
|
|
1426
|
-
return;
|
|
1477
|
+
const pauseIfStillWanted = () => {
|
|
1478
|
+
if (done) {
|
|
1479
|
+
return; // we already gave up or something else took over
|
|
1427
1480
|
}
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
video.pause();
|
|
1431
|
-
}
|
|
1432
|
-
};
|
|
1433
|
-
// Prefer pausing after a composited video frame, not just after a timer.
|
|
1434
|
-
// Some videos/devices can report "playing" before pixels are actually painted.
|
|
1435
|
-
const requestVideoFrameCallback = (video as any)
|
|
1436
|
-
.requestVideoFrameCallback as
|
|
1437
|
-
| ((callback: (...args: any[]) => void) => number)
|
|
1438
|
-
| undefined;
|
|
1439
|
-
if (requestVideoFrameCallback) {
|
|
1440
|
-
requestVideoFrameCallback.call(video, () => {
|
|
1441
|
-
pauseIfStillWanted();
|
|
1442
|
-
});
|
|
1443
|
-
} else {
|
|
1444
|
-
// Fallback for older browsers. This sometimes helps, but is not as reliable as the
|
|
1445
|
-
// above on browsers that implement it.
|
|
1446
|
-
setTimeout(() => {
|
|
1447
|
-
pauseIfStillWanted();
|
|
1448
|
-
}, 4);
|
|
1481
|
+
if (shouldPauseAfterPlaying()) {
|
|
1482
|
+
video.pause();
|
|
1449
1483
|
}
|
|
1484
|
+
finish();
|
|
1485
|
+
// A frame has been painted; the transparent poster (set at book
|
|
1486
|
+
// load to hide videos until then) is no longer wanted.
|
|
1487
|
+
video.removeAttribute("poster");
|
|
1450
1488
|
};
|
|
1451
|
-
video
|
|
1452
|
-
//
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
promise.catch((reason) => {
|
|
1461
|
-
window.clearTimeout(removePlayingListenerTimeout);
|
|
1462
|
-
// If play() fails (e.g., autoplay policy), remove the listener
|
|
1463
|
-
// so it won't interfere if playback starts later by other means.
|
|
1464
|
-
video.removeEventListener("playing", playingListener);
|
|
1465
|
-
// If autoplay is blocked, remove our transparent poster so a decoded
|
|
1466
|
-
// first frame can be shown when available.
|
|
1467
|
-
if (reason?.name === "NotAllowedError") {
|
|
1468
|
-
video.removeAttribute("poster");
|
|
1469
|
-
onAutoplayBlocked();
|
|
1470
|
-
}
|
|
1489
|
+
// Prefer pausing after a composited video frame, not just after a timer.
|
|
1490
|
+
// Some videos/devices can report "playing" before pixels are actually painted.
|
|
1491
|
+
const requestVideoFrameCallback = (video as any)
|
|
1492
|
+
.requestVideoFrameCallback as
|
|
1493
|
+
| ((callback: (...args: any[]) => void) => number)
|
|
1494
|
+
| undefined;
|
|
1495
|
+
if (requestVideoFrameCallback) {
|
|
1496
|
+
requestVideoFrameCallback.call(video, () => {
|
|
1497
|
+
pauseIfStillWanted();
|
|
1471
1498
|
});
|
|
1499
|
+
} else {
|
|
1500
|
+
// Fallback for older browsers. This sometimes helps, but is not as reliable as the
|
|
1501
|
+
// above on browsers that implement it.
|
|
1502
|
+
setTimeout(() => {
|
|
1503
|
+
pauseIfStillWanted();
|
|
1504
|
+
}, 4);
|
|
1472
1505
|
}
|
|
1473
1506
|
};
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1507
|
+
video.addEventListener("playing", playingListener, { once: true });
|
|
1508
|
+
// If nothing has painted after a while (very slow data, or a policy we
|
|
1509
|
+
// didn't anticipate), give up: cancel our pending muted playback, restore
|
|
1510
|
+
// audio, and remove the transparent poster so the browser can show its own
|
|
1511
|
+
// first frame when it has one, rather than leaving the video invisible.
|
|
1512
|
+
giveUpTimeout = window.setTimeout(() => {
|
|
1513
|
+
if (shouldPauseAfterPlaying() && !video.paused) {
|
|
1514
|
+
video.pause();
|
|
1515
|
+
}
|
|
1516
|
+
finish();
|
|
1517
|
+
video.removeAttribute("poster");
|
|
1518
|
+
}, 3000);
|
|
1519
|
+
hideVideoAutoplayBlockedHint(video);
|
|
1520
|
+
video.muted = true;
|
|
1521
|
+
const promise = video.play();
|
|
1522
|
+
if (promise && promise.catch) {
|
|
1523
|
+
promise.catch((reason) => {
|
|
1524
|
+
finish();
|
|
1525
|
+
// If autoplay is blocked even muted, remove our transparent poster
|
|
1526
|
+
// so a decoded first frame can be shown when available.
|
|
1527
|
+
if (reason?.name === "NotAllowedError") {
|
|
1528
|
+
video.removeAttribute("poster");
|
|
1529
|
+
onAutoplayBlocked();
|
|
1530
|
+
}
|
|
1531
|
+
});
|
|
1492
1532
|
}
|
|
1493
1533
|
}
|
|
1494
1534
|
|
|
@@ -1558,6 +1598,9 @@ function playAllVideoInternal(
|
|
|
1558
1598
|
hideVideoError(video);
|
|
1559
1599
|
hideVideoAutoplayBlockedHint(video);
|
|
1560
1600
|
setCurrentPlaybackMode(PlaybackMode.VideoPlaying);
|
|
1601
|
+
// This is real playback; a pending first-frame priming attempt must
|
|
1602
|
+
// not mute or pause it.
|
|
1603
|
+
cancelVideoFirstFramePriming(video);
|
|
1561
1604
|
// Always play each queued video from the beginning.
|
|
1562
1605
|
// Without this, a previously played element may remain at end-of-stream
|
|
1563
1606
|
// and fail to raise the expected ended event for sequencing.
|
package/src/shared/scrolling.ts
CHANGED
|
@@ -345,8 +345,14 @@ export function setupSpecialMouseTrackingForNiceScroll(
|
|
|
345
345
|
bloomPage: Element,
|
|
346
346
|
pointerEventHandler?: (e: PointerEvent) => void,
|
|
347
347
|
) {
|
|
348
|
-
bloomPage.removeEventListener(
|
|
349
|
-
|
|
348
|
+
bloomPage.removeEventListener(
|
|
349
|
+
"pointerdown",
|
|
350
|
+
listenForPointerDown as EventListener,
|
|
351
|
+
); // only want one!
|
|
352
|
+
bloomPage.addEventListener(
|
|
353
|
+
"pointerdown",
|
|
354
|
+
listenForPointerDown as EventListener,
|
|
355
|
+
);
|
|
350
356
|
if (!pointerEventHandler) {
|
|
351
357
|
return;
|
|
352
358
|
}
|
|
@@ -355,7 +361,7 @@ export function setupSpecialMouseTrackingForNiceScroll(
|
|
|
355
361
|
for (const eventName of ["pointermove", "pointerup"]) {
|
|
356
362
|
bloomPage.ownerDocument.body.addEventListener(
|
|
357
363
|
eventName,
|
|
358
|
-
pointerEventHandler,
|
|
364
|
+
pointerEventHandler as EventListener,
|
|
359
365
|
{
|
|
360
366
|
capture: true,
|
|
361
367
|
},
|