@cloudnest/redxplyr 1.0.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 (127) hide show
  1. package/.editorconfig +10 -0
  2. package/.gitpod.yml +6 -0
  3. package/.node-version +1 -0
  4. package/.prettierrc +7 -0
  5. package/.stickler.yml +5 -0
  6. package/.stylelintrc.json +26 -0
  7. package/CHANGELOG.md +16 -0
  8. package/CONTRIBUTING.md +34 -0
  9. package/CONTROLS.md +49 -0
  10. package/Dockerfile +32 -0
  11. package/LICENSE.md +22 -0
  12. package/README.md +194 -0
  13. package/cspell.json +48 -0
  14. package/dist/redxplyr.css +1 -0
  15. package/dist/redxplyr.js +8801 -0
  16. package/dist/redxplyr.min.js +2 -0
  17. package/dist/redxplyr.min.js.map +1 -0
  18. package/dist/redxplyr.min.mjs +1 -0
  19. package/dist/redxplyr.min.mjs.map +1 -0
  20. package/dist/redxplyr.mjs +8793 -0
  21. package/dist/redxplyr.polyfilled.js +9294 -0
  22. package/dist/redxplyr.polyfilled.min.js +2 -0
  23. package/dist/redxplyr.polyfilled.min.js.map +1 -0
  24. package/dist/redxplyr.polyfilled.min.mjs +1 -0
  25. package/dist/redxplyr.polyfilled.min.mjs.map +1 -0
  26. package/dist/redxplyr.polyfilled.mjs +9286 -0
  27. package/dist/redxplyr.svg +1 -0
  28. package/eslint.config.mjs +39 -0
  29. package/gulpfile.js +8 -0
  30. package/package.json +114 -0
  31. package/pnpm-workspace.yaml +8 -0
  32. package/src/js/captions.js +411 -0
  33. package/src/js/config/defaults.js +459 -0
  34. package/src/js/config/states.js +10 -0
  35. package/src/js/config/types.js +34 -0
  36. package/src/js/console.js +28 -0
  37. package/src/js/controls.js +1870 -0
  38. package/src/js/fullscreen.js +305 -0
  39. package/src/js/html5.js +148 -0
  40. package/src/js/listeners.js +854 -0
  41. package/src/js/media.js +61 -0
  42. package/src/js/plugins/ads.js +647 -0
  43. package/src/js/plugins/preview-thumbnails.js +706 -0
  44. package/src/js/plugins/vimeo.js +443 -0
  45. package/src/js/plugins/youtube.js +451 -0
  46. package/src/js/plyr.d.ts +729 -0
  47. package/src/js/plyr.js +1291 -0
  48. package/src/js/plyr.polyfilled.js +13 -0
  49. package/src/js/source.js +155 -0
  50. package/src/js/storage.js +70 -0
  51. package/src/js/support.js +100 -0
  52. package/src/js/ui.js +297 -0
  53. package/src/js/utils/animation.js +33 -0
  54. package/src/js/utils/arrays.js +23 -0
  55. package/src/js/utils/browser.js +21 -0
  56. package/src/js/utils/elements.js +263 -0
  57. package/src/js/utils/events.js +116 -0
  58. package/src/js/utils/fetch.js +45 -0
  59. package/src/js/utils/i18n.js +47 -0
  60. package/src/js/utils/is.js +81 -0
  61. package/src/js/utils/load-image.js +19 -0
  62. package/src/js/utils/load-script.js +14 -0
  63. package/src/js/utils/load-sprite.js +77 -0
  64. package/src/js/utils/numbers.js +17 -0
  65. package/src/js/utils/objects.js +43 -0
  66. package/src/js/utils/promise.js +14 -0
  67. package/src/js/utils/strings.js +80 -0
  68. package/src/js/utils/style.js +148 -0
  69. package/src/js/utils/time.js +36 -0
  70. package/src/js/utils/urls.js +40 -0
  71. package/src/sass/base.scss +69 -0
  72. package/src/sass/components/badges.scss +12 -0
  73. package/src/sass/components/captions.scss +58 -0
  74. package/src/sass/components/control.scss +52 -0
  75. package/src/sass/components/controls.scss +65 -0
  76. package/src/sass/components/menus.scss +205 -0
  77. package/src/sass/components/poster.scss +27 -0
  78. package/src/sass/components/progress.scss +107 -0
  79. package/src/sass/components/sliders.scss +99 -0
  80. package/src/sass/components/times.scss +20 -0
  81. package/src/sass/components/tooltips.scss +91 -0
  82. package/src/sass/components/volume.scss +18 -0
  83. package/src/sass/lib/animation.scss +31 -0
  84. package/src/sass/lib/css-vars.scss +103 -0
  85. package/src/sass/lib/functions.scss +3 -0
  86. package/src/sass/lib/mixins.scss +82 -0
  87. package/src/sass/plugins/ads.scss +53 -0
  88. package/src/sass/plugins/preview-thumbnails/index.scss +121 -0
  89. package/src/sass/plugins/preview-thumbnails/settings.scss +17 -0
  90. package/src/sass/plyr.scss +46 -0
  91. package/src/sass/settings/badges.scss +7 -0
  92. package/src/sass/settings/breakpoints.scss +9 -0
  93. package/src/sass/settings/captions.scss +10 -0
  94. package/src/sass/settings/colors.scss +18 -0
  95. package/src/sass/settings/controls.scss +30 -0
  96. package/src/sass/settings/cosmetics.scss +5 -0
  97. package/src/sass/settings/helpers.scss +7 -0
  98. package/src/sass/settings/menus.scss +13 -0
  99. package/src/sass/settings/progress.scss +18 -0
  100. package/src/sass/settings/sliders.scss +39 -0
  101. package/src/sass/settings/tooltips.scss +11 -0
  102. package/src/sass/settings/type.scss +16 -0
  103. package/src/sass/states/fullscreen.scss +15 -0
  104. package/src/sass/types/audio.scss +61 -0
  105. package/src/sass/types/video.scss +170 -0
  106. package/src/sass/utils/animation.scss +7 -0
  107. package/src/sass/utils/hidden.scss +28 -0
  108. package/src/sprite/plyr-airplay.svg +8 -0
  109. package/src/sprite/plyr-captions-off.svg +7 -0
  110. package/src/sprite/plyr-captions-on.svg +7 -0
  111. package/src/sprite/plyr-download.svg +8 -0
  112. package/src/sprite/plyr-enter-fullscreen.svg +4 -0
  113. package/src/sprite/plyr-exit-fullscreen.svg +4 -0
  114. package/src/sprite/plyr-fast-forward.svg +3 -0
  115. package/src/sprite/plyr-logo-vimeo.svg +6 -0
  116. package/src/sprite/plyr-logo-youtube.svg +6 -0
  117. package/src/sprite/plyr-muted.svg +8 -0
  118. package/src/sprite/plyr-pause.svg +8 -0
  119. package/src/sprite/plyr-pip.svg +6 -0
  120. package/src/sprite/plyr-play.svg +5 -0
  121. package/src/sprite/plyr-restart.svg +5 -0
  122. package/src/sprite/plyr-rewind.svg +3 -0
  123. package/src/sprite/plyr-settings.svg +5 -0
  124. package/src/sprite/plyr-volume.svg +11 -0
  125. package/tasks/build.js +226 -0
  126. package/tasks/deploy.js +216 -0
  127. package/tasks/utils/publish.js +34 -0
@@ -0,0 +1,706 @@
1
+ import { createElement } from '../utils/elements';
2
+ import { once } from '../utils/events';
3
+ import fetch from '../utils/fetch';
4
+ import is from '../utils/is';
5
+ import { clamp } from '../utils/numbers';
6
+ import { formatTime } from '../utils/time';
7
+
8
+ // Arg: vttDataString example: "WEBVTT\n\n1\n00:00:05.000 --> 00:00:10.000\n1080p-00001.jpg"
9
+ function parseVtt(vttDataString) {
10
+ const processedList = [];
11
+ const frames = vttDataString.split(/\r\n\r\n|\n\n|\r\r/);
12
+
13
+ frames.forEach((frame) => {
14
+ const result = {};
15
+ const lines = frame.split(/\r\n|\n|\r/);
16
+
17
+ lines.forEach((line) => {
18
+ if (!is.number(result.startTime)) {
19
+ // The line with start and end times on it is the first line of interest
20
+ const matchTimes = line.match(
21
+ /(\d{2})?:?(\d{2}):(\d{2}).(\d{2,3})( ?--> ?)(\d{2})?:?(\d{2}):(\d{2}).(\d{2,3})/,
22
+ ); // Note that this currently ignores caption formatting directives that are optionally on the end of this line - fine for non-captions VTT
23
+
24
+ if (matchTimes) {
25
+ result.startTime
26
+ = Number(matchTimes[1] || 0) * 60 * 60
27
+ + Number(matchTimes[2]) * 60
28
+ + Number(matchTimes[3])
29
+ + Number(`0.${matchTimes[4]}`);
30
+ result.endTime
31
+ = Number(matchTimes[6] || 0) * 60 * 60
32
+ + Number(matchTimes[7]) * 60
33
+ + Number(matchTimes[8])
34
+ + Number(`0.${matchTimes[9]}`);
35
+ }
36
+ }
37
+ else if (!is.empty(line.trim()) && is.empty(result.text)) {
38
+ // If we already have the startTime, then we're definitely up to the text line(s)
39
+ const lineSplit = line.trim().split('#xywh=');
40
+ [result.text] = lineSplit;
41
+
42
+ // If there's content in lineSplit[1], then we have sprites. If not, then it's just one frame per image
43
+ if (lineSplit[1]) {
44
+ [result.x, result.y, result.w, result.h] = lineSplit[1].split(',');
45
+ }
46
+ }
47
+ });
48
+
49
+ if (result.text) {
50
+ processedList.push(result);
51
+ }
52
+ });
53
+
54
+ return processedList;
55
+ }
56
+
57
+ /**
58
+ * Preview thumbnails for seek hover and scrubbing
59
+ * Seeking: Hover over the seek bar (desktop only): shows a small preview container above the seek bar
60
+ * Scrubbing: Click and drag the seek bar (desktop and mobile): shows the preview image over the entire video, as if the video is scrubbing at very high speed
61
+ *
62
+ * Notes:
63
+ * - Thumbs are set via JS settings on Plyr init, not HTML5 'track' property. Using the track property would be a bit gross, because it doesn't support custom 'kinds'. kind=metadata might be used for something else, and we want to allow multiple thumbnails tracks. Tracks must have a unique combination of 'kind' and 'label'. We would have to do something like kind=metadata,label=thumbnails1 / kind=metadata,label=thumbnails2. Square peg, round hole
64
+ * - VTT info: the image URL is relative to the VTT, not the current document. But if the url starts with a slash, it will naturally be relative to the current domain. https://support.jwplayer.com/articles/how-to-add-preview-thumbnails
65
+ * - This implementation uses multiple separate img elements. Other implementations use background-image on one element. This would be nice and simple, but Firefox and Safari have flickering issues with replacing backgrounds of larger images. It seems that YouTube perhaps only avoids this because they don't have the option for high-res previews (even the fullscreen ones, when mousedown/seeking). Images appear over the top of each other, and previous ones are discarded once the new ones have been rendered
66
+ */
67
+
68
+ function fitRatio(ratio, outer) {
69
+ const targetRatio = outer.width / outer.height;
70
+ const result = {};
71
+ if (ratio > targetRatio) {
72
+ result.width = outer.width;
73
+ result.height = (1 / ratio) * outer.width;
74
+ }
75
+ else {
76
+ result.height = outer.height;
77
+ result.width = ratio * outer.height;
78
+ }
79
+
80
+ return result;
81
+ }
82
+
83
+ class PreviewThumbnails {
84
+ /**
85
+ * PreviewThumbnails constructor.
86
+ * @param {Plyr} player
87
+ * @return {PreviewThumbnails}
88
+ */
89
+ constructor(player) {
90
+ this.player = player;
91
+ this.thumbnails = [];
92
+ this.loaded = false;
93
+ this.lastMouseMoveTime = Date.now();
94
+ this.mouseDown = false;
95
+ this.loadedImages = [];
96
+
97
+ this.elements = {
98
+ thumb: {},
99
+ scrubbing: {},
100
+ };
101
+
102
+ this.load();
103
+ }
104
+
105
+ get enabled() {
106
+ return this.player.isHTML5 && this.player.isVideo && this.player.config.previewThumbnails.enabled;
107
+ }
108
+
109
+ load = () => {
110
+ // Toggle the regular seek tooltip
111
+ if (this.player.elements.display.seekTooltip) {
112
+ this.player.elements.display.seekTooltip.hidden = this.enabled;
113
+ }
114
+
115
+ if (!this.enabled) return;
116
+
117
+ this.getThumbnails().then(() => {
118
+ if (!this.enabled) {
119
+ return;
120
+ }
121
+
122
+ // Render DOM elements
123
+ this.render();
124
+
125
+ // Check to see if thumb container size was specified manually in CSS
126
+ this.determineContainerAutoSizing();
127
+
128
+ // Set up listeners
129
+ this.listeners();
130
+
131
+ this.loaded = true;
132
+ });
133
+ };
134
+
135
+ // Download VTT files and parse them
136
+ getThumbnails = () => {
137
+ return new Promise((resolve) => {
138
+ const { src } = this.player.config.previewThumbnails;
139
+
140
+ if (is.empty(src)) {
141
+ throw new Error('Missing previewThumbnails.src config attribute');
142
+ }
143
+
144
+ // Resolve promise
145
+ const sortAndResolve = () => {
146
+ // Sort smallest to biggest (e.g., [120p, 480p, 1080p])
147
+ this.thumbnails.sort((x, y) => x.height - y.height);
148
+
149
+ this.player.debug.log('Preview thumbnails', this.thumbnails);
150
+
151
+ resolve();
152
+ };
153
+
154
+ // Via callback()
155
+ if (is.function(src)) {
156
+ src((thumbnails) => {
157
+ this.thumbnails = thumbnails;
158
+ sortAndResolve();
159
+ });
160
+ }
161
+ // VTT urls
162
+ else {
163
+ // If string, convert into single-element list
164
+ const urls = is.string(src) ? [src] : src;
165
+ // Loop through each src URL. Download and process the VTT file, storing the resulting data in this.thumbnails
166
+ const promises = urls.map(u => this.getThumbnail(u));
167
+ // Resolve
168
+ Promise.all(promises).then(sortAndResolve);
169
+ }
170
+ });
171
+ };
172
+
173
+ // Process individual VTT file
174
+ getThumbnail = (url) => {
175
+ return new Promise((resolve) => {
176
+ fetch(url, undefined, this.player.config.previewThumbnails.withCredentials).then((response) => {
177
+ const thumbnail = {
178
+ frames: parseVtt(response),
179
+ height: null,
180
+ urlPrefix: '',
181
+ };
182
+
183
+ // If the URLs don't start with '/', then we need to set their relative path to be the location of the VTT file
184
+ // If the URLs do start with '/', then they obviously don't need a prefix, so it will remain blank
185
+ // If the thumbnail URLs start with with none of '/', 'http://' or 'https://', then we need to set their relative path to be the location of the VTT file
186
+ if (
187
+ !thumbnail.frames[0].text.startsWith('/')
188
+ && !thumbnail.frames[0].text.startsWith('http://')
189
+ && !thumbnail.frames[0].text.startsWith('https://')
190
+ ) {
191
+ thumbnail.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1);
192
+ }
193
+
194
+ // Download the first frame, so that we can determine/set the height of this thumbnailsDef
195
+ const tempImage = new Image();
196
+
197
+ tempImage.onload = () => {
198
+ thumbnail.height = tempImage.naturalHeight;
199
+ thumbnail.width = tempImage.naturalWidth;
200
+
201
+ this.thumbnails.push(thumbnail);
202
+
203
+ resolve();
204
+ };
205
+
206
+ tempImage.src = thumbnail.urlPrefix + thumbnail.frames[0].text;
207
+ });
208
+ });
209
+ };
210
+
211
+ startMove = (event) => {
212
+ if (!this.loaded) return;
213
+
214
+ if (!is.event(event) || !['touchmove', 'mousemove'].includes(event.type)) return;
215
+
216
+ // Wait until media has a duration
217
+ if (!this.player.media.duration) return;
218
+
219
+ if (event.type === 'touchmove') {
220
+ // Calculate seek hover position as approx video seconds
221
+ this.seekTime = this.player.media.duration * (this.player.elements.inputs.seek.value / 100);
222
+ }
223
+ else {
224
+ // Calculate seek hover position as approx video seconds
225
+ const clientRect = this.player.elements.progress.getBoundingClientRect();
226
+ const percentage = (100 / clientRect.width) * (event.pageX - clientRect.left);
227
+ this.seekTime = this.player.media.duration * (percentage / 100);
228
+
229
+ if (this.seekTime < 0) {
230
+ // The mousemove fires for 10+px out to the left
231
+ this.seekTime = 0;
232
+ }
233
+
234
+ if (this.seekTime > this.player.media.duration - 1) {
235
+ // Took 1 second off the duration for safety, because different players can disagree on the real duration of a video
236
+ this.seekTime = this.player.media.duration - 1;
237
+ }
238
+
239
+ this.mousePosX = event.pageX;
240
+
241
+ // Set time text inside image container
242
+ this.elements.thumb.time.textContent = formatTime(this.seekTime);
243
+
244
+ // Get marker point for time
245
+ const point = this.player.config.markers?.points?.find(({ time: t }) => t === Math.round(this.seekTime));
246
+
247
+ // Append the point label to the tooltip
248
+ if (point) {
249
+ // this.elements.thumb.time.innerText.concat('\n');
250
+ this.elements.thumb.time.insertAdjacentHTML('afterbegin', `${point.label}<br>`);
251
+ }
252
+ }
253
+
254
+ // Download and show image
255
+ this.showImageAtCurrentTime();
256
+ };
257
+
258
+ endMove = () => {
259
+ this.toggleThumbContainer(false, true);
260
+ };
261
+
262
+ startScrubbing = (event) => {
263
+ // Only act on left mouse button (0), or touch device (event.button does not exist or is false)
264
+ if (is.nullOrUndefined(event.button) || event.button === false || event.button === 0) {
265
+ this.mouseDown = true;
266
+
267
+ // Wait until media has a duration
268
+ if (this.player.media.duration) {
269
+ this.toggleScrubbingContainer(true);
270
+ this.toggleThumbContainer(false, true);
271
+
272
+ // Download and show image
273
+ this.showImageAtCurrentTime();
274
+ }
275
+ }
276
+ };
277
+
278
+ endScrubbing = () => {
279
+ this.mouseDown = false;
280
+
281
+ // Hide scrubbing preview. But wait until the video has successfully seeked before hiding the scrubbing preview
282
+ if (Math.ceil(this.lastTime) === Math.ceil(this.player.media.currentTime)) {
283
+ // The video was already seeked/loaded at the chosen time - hide immediately
284
+ this.toggleScrubbingContainer(false);
285
+ }
286
+ else {
287
+ // The video hasn't seeked yet. Wait for that
288
+ once.call(this.player, this.player.media, 'timeupdate', () => {
289
+ // Re-check mousedown - we might have already started scrubbing again
290
+ if (!this.mouseDown) {
291
+ this.toggleScrubbingContainer(false);
292
+ }
293
+ });
294
+ }
295
+ };
296
+
297
+ /**
298
+ * Setup hooks for Plyr and window events
299
+ */
300
+ listeners = () => {
301
+ // Hide thumbnail preview - on mouse click, mouse leave (in listeners.js for now), and video play/seek. All four are required, e.g., for buffering
302
+ this.player.on('play', () => {
303
+ this.toggleThumbContainer(false, true);
304
+ });
305
+
306
+ this.player.on('seeked', () => {
307
+ this.toggleThumbContainer(false);
308
+ });
309
+
310
+ this.player.on('timeupdate', () => {
311
+ this.lastTime = this.player.media.currentTime;
312
+ });
313
+ };
314
+
315
+ /**
316
+ * Create HTML elements for image containers
317
+ */
318
+ render = () => {
319
+ // Create HTML element: plyr__preview-thumbnail-container
320
+ this.elements.thumb.container = createElement('div', {
321
+ class: this.player.config.classNames.previewThumbnails.thumbContainer,
322
+ });
323
+
324
+ // Wrapper for the image for styling
325
+ this.elements.thumb.imageContainer = createElement('div', {
326
+ class: this.player.config.classNames.previewThumbnails.imageContainer,
327
+ });
328
+ this.elements.thumb.container.appendChild(this.elements.thumb.imageContainer);
329
+
330
+ // Create HTML element, parent+span: time text (e.g., 01:32:00)
331
+ const timeContainer = createElement('div', {
332
+ class: this.player.config.classNames.previewThumbnails.timeContainer,
333
+ });
334
+
335
+ this.elements.thumb.time = createElement('span', {}, '00:00');
336
+ timeContainer.appendChild(this.elements.thumb.time);
337
+
338
+ this.elements.thumb.imageContainer.appendChild(timeContainer);
339
+
340
+ // Inject the whole thumb
341
+ if (is.element(this.player.elements.progress)) {
342
+ this.player.elements.progress.appendChild(this.elements.thumb.container);
343
+ }
344
+
345
+ // Create HTML element: plyr__preview-scrubbing-container
346
+ this.elements.scrubbing.container = createElement('div', {
347
+ class: this.player.config.classNames.previewThumbnails.scrubbingContainer,
348
+ });
349
+
350
+ this.player.elements.wrapper.appendChild(this.elements.scrubbing.container);
351
+ };
352
+
353
+ destroy = () => {
354
+ if (this.elements.thumb.container) {
355
+ this.elements.thumb.container.remove();
356
+ }
357
+ if (this.elements.scrubbing.container) {
358
+ this.elements.scrubbing.container.remove();
359
+ }
360
+ };
361
+
362
+ showImageAtCurrentTime = () => {
363
+ if (this.mouseDown) {
364
+ this.setScrubbingContainerSize();
365
+ }
366
+ else {
367
+ this.setThumbContainerSizeAndPos();
368
+ }
369
+
370
+ // Find the desired thumbnail index
371
+ // TODO: Handle a video longer than the thumbs where thumbNum is null
372
+ const thumbNum = this.thumbnails[0].frames.findIndex(
373
+ frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime,
374
+ );
375
+ const hasThumb = thumbNum >= 0;
376
+ let qualityIndex = 0;
377
+
378
+ // Show the thumb container if we're not scrubbing
379
+ if (!this.mouseDown) {
380
+ this.toggleThumbContainer(hasThumb);
381
+ }
382
+
383
+ // No matching thumb found
384
+ if (!hasThumb) {
385
+ return;
386
+ }
387
+
388
+ // Check to see if we've already downloaded higher quality versions of this image
389
+ this.thumbnails.forEach((thumbnail, index) => {
390
+ if (this.loadedImages.includes(thumbnail.frames[thumbNum].text)) {
391
+ qualityIndex = index;
392
+ }
393
+ });
394
+
395
+ // Only proceed if either thumb num or thumbfilename has changed
396
+ if (thumbNum !== this.showingThumb) {
397
+ this.showingThumb = thumbNum;
398
+ this.loadImage(qualityIndex);
399
+ }
400
+ };
401
+
402
+ // Show the image that's currently specified in this.showingThumb
403
+ loadImage = (qualityIndex = 0) => {
404
+ const thumbNum = this.showingThumb;
405
+ const thumbnail = this.thumbnails[qualityIndex];
406
+ const { urlPrefix } = thumbnail;
407
+ const frame = thumbnail.frames[thumbNum];
408
+ const thumbFilename = thumbnail.frames[thumbNum].text;
409
+ const thumbUrl = urlPrefix + thumbFilename;
410
+
411
+ if (!this.currentImageElement || this.currentImageElement.dataset.filename !== thumbFilename) {
412
+ // If we're already loading a previous image, remove its onload handler - we don't want it to load after this one
413
+ // Only do this if not using sprites. Without sprites we really want to show as many images as possible, as a best-effort
414
+ if (this.loadingImage && this.usingSprites) {
415
+ this.loadingImage.onload = null;
416
+ }
417
+
418
+ // We're building and adding a new image. In other implementations of similar functionality (YouTube), background image
419
+ // is instead used. But this causes issues with larger images in Firefox and Safari - switching between background
420
+ // images causes a flicker. Putting a new image over the top does not
421
+ const previewImage = new Image();
422
+ previewImage.src = thumbUrl;
423
+ previewImage.dataset.index = thumbNum;
424
+ previewImage.dataset.filename = thumbFilename;
425
+ this.showingThumbFilename = thumbFilename;
426
+
427
+ this.player.debug.log(`Loading image: ${thumbUrl}`);
428
+
429
+ // For some reason, passing the named function directly causes it to execute immediately. So I've wrapped it in an anonymous function...
430
+ previewImage.onload = () => this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true);
431
+ this.loadingImage = previewImage;
432
+ this.removeOldImages(previewImage);
433
+ }
434
+ else {
435
+ // Update the existing image
436
+ this.showImage(this.currentImageElement, frame, qualityIndex, thumbNum, thumbFilename, false);
437
+ this.currentImageElement.dataset.index = thumbNum;
438
+ this.removeOldImages(this.currentImageElement);
439
+ }
440
+ };
441
+
442
+ showImage = (previewImage, frame, qualityIndex, thumbNum, thumbFilename, newImage = true) => {
443
+ this.player.debug.log(
444
+ `Showing thumb: ${thumbFilename}. num: ${thumbNum}. qual: ${qualityIndex}. newimg: ${newImage}`,
445
+ );
446
+ this.setImageSizeAndOffset(previewImage, frame);
447
+
448
+ if (newImage) {
449
+ this.currentImageContainer.appendChild(previewImage);
450
+ this.currentImageElement = previewImage;
451
+
452
+ if (!this.loadedImages.includes(thumbFilename)) {
453
+ this.loadedImages.push(thumbFilename);
454
+ }
455
+ }
456
+
457
+ // Preload images before and after the current one
458
+ // Show higher quality of the same frame
459
+ // Each step here has a short time delay, and only continues if still hovering/seeking the same spot. This is to protect slow connections from overloading
460
+ this.preloadNearby(thumbNum, true)
461
+ .then(this.preloadNearby(thumbNum, false))
462
+ .then(this.getHigherQuality(qualityIndex, previewImage, frame, thumbFilename));
463
+ };
464
+
465
+ // Remove all preview images that aren't the designated current image
466
+ removeOldImages = (currentImage) => {
467
+ // Get a list of all images, convert it from a DOM list to an array
468
+ Array.from(this.currentImageContainer.children).forEach((image) => {
469
+ if (image.tagName.toLowerCase() !== 'img') {
470
+ return;
471
+ }
472
+
473
+ const removeDelay = this.usingSprites ? 500 : 1000;
474
+
475
+ if (image.dataset.index !== currentImage.dataset.index && !image.dataset.deleting) {
476
+ // Wait 200ms, as the new image can take some time to show on certain browsers (even though it was downloaded before showing). This will prevent flicker, and show some generosity towards slower clients
477
+ // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function
478
+
479
+ image.dataset.deleting = true;
480
+
481
+ // This has to be set before the timeout - to prevent issues switching between hover and scrub
482
+ const { currentImageContainer } = this;
483
+
484
+ setTimeout(() => {
485
+ currentImageContainer.removeChild(image);
486
+ this.player.debug.log(`Removing thumb: ${image.dataset.filename}`);
487
+ }, removeDelay);
488
+ }
489
+ });
490
+ };
491
+
492
+ // Preload images before and after the current one. Only if the user is still hovering/seeking the same frame
493
+ // This will only preload the lowest quality
494
+ preloadNearby = (thumbNum, forward = true) => {
495
+ return new Promise((resolve) => {
496
+ setTimeout(() => {
497
+ const oldThumbFilename = this.thumbnails[0].frames[thumbNum].text;
498
+
499
+ if (this.showingThumbFilename === oldThumbFilename) {
500
+ // Find the nearest thumbs with different filenames. Sometimes it'll be the next index, but in the case of sprites, it might be 100+ away
501
+ let thumbnailsClone;
502
+ if (forward) {
503
+ thumbnailsClone = this.thumbnails[0].frames.slice(thumbNum);
504
+ }
505
+ else {
506
+ thumbnailsClone = this.thumbnails[0].frames.slice(0, thumbNum).reverse();
507
+ }
508
+
509
+ let foundOne = false;
510
+
511
+ thumbnailsClone.forEach((frame) => {
512
+ const newThumbFilename = frame.text;
513
+
514
+ if (newThumbFilename !== oldThumbFilename) {
515
+ // Found one with a different filename. Make sure it hasn't already been loaded on this page visit
516
+ if (!this.loadedImages.includes(newThumbFilename)) {
517
+ foundOne = true;
518
+ this.player.debug.log(`Preloading thumb filename: ${newThumbFilename}`);
519
+
520
+ const { urlPrefix } = this.thumbnails[0];
521
+ const thumbURL = urlPrefix + newThumbFilename;
522
+ const previewImage = new Image();
523
+ previewImage.src = thumbURL;
524
+ previewImage.onload = () => {
525
+ this.player.debug.log(`Preloaded thumb filename: ${newThumbFilename}`);
526
+ if (!this.loadedImages.includes(newThumbFilename)) this.loadedImages.push(newThumbFilename);
527
+
528
+ // We don't resolve until the thumb is loaded
529
+ resolve();
530
+ };
531
+ }
532
+ }
533
+ });
534
+
535
+ // If there are none to preload then we want to resolve immediately
536
+ if (!foundOne) {
537
+ resolve();
538
+ }
539
+ }
540
+ }, 300);
541
+ });
542
+ };
543
+
544
+ // If user has been hovering current image for half a second, look for a higher quality one
545
+ getHigherQuality = (currentQualityIndex, previewImage, frame, thumbFilename) => {
546
+ if (currentQualityIndex < this.thumbnails.length - 1) {
547
+ // Only use the higher quality version if it's going to look any better - if the current thumb is of a lower pixel density than the thumbnail container
548
+ let previewImageHeight = previewImage.naturalHeight;
549
+
550
+ if (this.usingSprites) {
551
+ previewImageHeight = frame.h;
552
+ }
553
+
554
+ if (previewImageHeight < this.thumbContainerHeight) {
555
+ // Recurse back to the loadImage function - show a higher quality one, but only if the viewer is on this frame for a while
556
+ setTimeout(() => {
557
+ // Make sure the mouse hasn't already moved on and started hovering at another image
558
+ if (this.showingThumbFilename === thumbFilename) {
559
+ this.player.debug.log(`Showing higher quality thumb for: ${thumbFilename}`);
560
+ this.loadImage(currentQualityIndex + 1);
561
+ }
562
+ }, 300);
563
+ }
564
+ }
565
+ };
566
+
567
+ get currentImageContainer() {
568
+ return this.mouseDown ? this.elements.scrubbing.container : this.elements.thumb.imageContainer;
569
+ }
570
+
571
+ get usingSprites() {
572
+ return Object.keys(this.thumbnails[0].frames[0]).includes('w');
573
+ }
574
+
575
+ get thumbAspectRatio() {
576
+ if (this.usingSprites) {
577
+ return this.thumbnails[0].frames[0].w / this.thumbnails[0].frames[0].h;
578
+ }
579
+
580
+ return this.thumbnails[0].width / this.thumbnails[0].height;
581
+ }
582
+
583
+ get thumbContainerHeight() {
584
+ if (this.mouseDown) {
585
+ const { height } = fitRatio(this.thumbAspectRatio, {
586
+ width: this.player.media.clientWidth,
587
+ height: this.player.media.clientHeight,
588
+ });
589
+ return height;
590
+ }
591
+
592
+ // If css is used this needs to return the css height for sprites to work (see setImageSizeAndOffset)
593
+ if (this.sizeSpecifiedInCSS) {
594
+ return this.elements.thumb.imageContainer.clientHeight;
595
+ }
596
+
597
+ return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio / 4);
598
+ }
599
+
600
+ get currentImageElement() {
601
+ return this.mouseDown ? this.currentScrubbingImageElement : this.currentThumbnailImageElement;
602
+ }
603
+
604
+ set currentImageElement(element) {
605
+ if (this.mouseDown) {
606
+ this.currentScrubbingImageElement = element;
607
+ }
608
+ else {
609
+ this.currentThumbnailImageElement = element;
610
+ }
611
+ }
612
+
613
+ toggleThumbContainer = (toggle = false, clearShowing = false) => {
614
+ const className = this.player.config.classNames.previewThumbnails.thumbContainerShown;
615
+ this.elements.thumb.container.classList.toggle(className, toggle);
616
+
617
+ if (!toggle && clearShowing) {
618
+ this.showingThumb = null;
619
+ this.showingThumbFilename = null;
620
+ }
621
+ };
622
+
623
+ toggleScrubbingContainer = (toggle = false) => {
624
+ const className = this.player.config.classNames.previewThumbnails.scrubbingContainerShown;
625
+ this.elements.scrubbing.container.classList.toggle(className, toggle);
626
+
627
+ if (!toggle) {
628
+ this.showingThumb = null;
629
+ this.showingThumbFilename = null;
630
+ }
631
+ };
632
+
633
+ determineContainerAutoSizing = () => {
634
+ if (this.elements.thumb.imageContainer.clientHeight > 20 || this.elements.thumb.imageContainer.clientWidth > 20) {
635
+ // This will prevent auto sizing in this.setThumbContainerSizeAndPos()
636
+ this.sizeSpecifiedInCSS = true;
637
+ }
638
+ };
639
+
640
+ // Set the size to be about a quarter of the size of video. Unless option dynamicSize === false, in which case it needs to be set in CSS
641
+ setThumbContainerSizeAndPos = () => {
642
+ const { imageContainer } = this.elements.thumb;
643
+
644
+ if (!this.sizeSpecifiedInCSS) {
645
+ const thumbWidth = Math.floor(this.thumbContainerHeight * this.thumbAspectRatio);
646
+ imageContainer.style.height = `${this.thumbContainerHeight}px`;
647
+ imageContainer.style.width = `${thumbWidth}px`;
648
+ }
649
+ else if (imageContainer.clientHeight > 20 && imageContainer.clientWidth < 20) {
650
+ const thumbWidth = Math.floor(imageContainer.clientHeight * this.thumbAspectRatio);
651
+ imageContainer.style.width = `${thumbWidth}px`;
652
+ }
653
+ else if (imageContainer.clientHeight < 20 && imageContainer.clientWidth > 20) {
654
+ const thumbHeight = Math.floor(imageContainer.clientWidth / this.thumbAspectRatio);
655
+ imageContainer.style.height = `${thumbHeight}px`;
656
+ }
657
+
658
+ this.setThumbContainerPos();
659
+ };
660
+
661
+ setThumbContainerPos = () => {
662
+ const scrubberRect = this.player.elements.progress.getBoundingClientRect();
663
+ const containerRect = this.player.elements.container.getBoundingClientRect();
664
+ const { container } = this.elements.thumb;
665
+ // Find the lowest and highest desired left-position, so we don't slide out the side of the video container
666
+ const min = containerRect.left - scrubberRect.left + 10;
667
+ const max = containerRect.right - scrubberRect.left - container.clientWidth - 10;
668
+ // Set preview container position to: mousepos, minus seekbar.left, minus half of previewContainer.clientWidth
669
+ const position = this.mousePosX - scrubberRect.left - container.clientWidth / 2;
670
+ const clamped = clamp(position, min, max);
671
+
672
+ // Move the popover position
673
+ container.style.left = `${clamped}px`;
674
+
675
+ // The arrow can follow the cursor
676
+ container.style.setProperty('--preview-arrow-offset', `${position - clamped}px`);
677
+ };
678
+
679
+ // Can't use 100% width, in case the video is a different aspect ratio to the video container
680
+ setScrubbingContainerSize = () => {
681
+ const { width, height } = fitRatio(this.thumbAspectRatio, {
682
+ width: this.player.media.clientWidth,
683
+ height: this.player.media.clientHeight,
684
+ });
685
+ this.elements.scrubbing.container.style.width = `${width}px`;
686
+ this.elements.scrubbing.container.style.height = `${height}px`;
687
+ };
688
+
689
+ // Sprites need to be offset to the correct location
690
+ setImageSizeAndOffset = (previewImage, frame) => {
691
+ if (!this.usingSprites) return;
692
+
693
+ // Find difference between height and preview container height
694
+ const multiplier = this.thumbContainerHeight / frame.h;
695
+
696
+ previewImage.style.height = `${previewImage.naturalHeight * multiplier}px`;
697
+
698
+ previewImage.style.width = `${previewImage.naturalWidth * multiplier}px`;
699
+
700
+ previewImage.style.left = `-${frame.x * multiplier}px`;
701
+
702
+ previewImage.style.top = `-${frame.y * multiplier}px`;
703
+ };
704
+ }
705
+
706
+ export default PreviewThumbnails;