Linting etc

This commit is contained in:
Sam Potts 2019-01-08 23:34:28 +11:00
parent 5fdc0aae66
commit 7dd7c13065
3 changed files with 324 additions and 354 deletions

View File

@ -1,32 +1,35 @@
{ {
"folders": [ "folders": [
{ {
"path": "." "path": "."
} }
], ],
"settings": { "settings": {
// Exclude from the editor // Exclude from the editor
"files.exclude": { "files.exclude": {
"**/node_modules": true "**/node_modules": true
}, },
// Exclude from search // Exclude from search
"search.exclude": { "search.exclude": {
"dist/": true, "dist/": true,
"demo/dist/": true "demo/dist/": true
}, },
// Linting // Linting
"stylelint.enable": true, "stylelint.enable": true,
"css.validate": false, "css.validate": false,
"scss.validate": false, "scss.validate": false,
"javascript.validate.enable": false, "javascript.validate.enable": false,
// Prettier // Prettier
"prettier.eslintIntegration": true, "prettier.eslintIntegration": true,
"prettier.stylelintIntegration": true, "prettier.stylelintIntegration": true,
// Formatting // Formatting
"editor.tabSize": 4, "editor.tabSize": 4,
"editor.insertSpaces": true, "editor.insertSpaces": true,
"editor.formatOnSave": true, "editor.formatOnSave": true,
// Trim on save "editor.codeActionsOnSave": {
"files.trimTrailingWhitespace": true "source.organizeImports": true
} },
} // Trim on save
"files.trimTrailingWhitespace": true
}
}

View File

@ -1,7 +1,56 @@
import { formatTime } from '../utils/time'; import { createElement } from '../utils/elements';
import { on, once, toggleListener, triggerEvent } from '../utils/events'; import { on, once } from '../utils/events';
import { createElement, emptyElement, getAttributesFromSelector, getElement, getElements, hasClass, matches, removeElement, setAttributes, setFocus, toggleClass, toggleHidden } from '../utils/elements';
import fetch from '../utils/fetch'; import fetch from '../utils/fetch';
import is from '../utils/is';
import { formatTime } from '../utils/time';
// Arg: vttDataString example: "WEBVTT\n\n1\n00:00:05.000 --> 00:00:10.000\n1080p-00001.jpg"
const parseVtt = vttDataString => {
const processedList = [];
const frames = vttDataString.split(/\r\n\r\n|\n\n|\r\r/);
frames.forEach(frame => {
const result = {};
const lines = frame.split(/\r\n|\n|\r/);
lines.forEach(line => {
if (!is.number(result.startTime)) {
// The line with start and end times on it is the first line of interest
const matchTimes = line.match(
/([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2,3})( ?--> ?)([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2,3})/,
); // Note that this currently ignores caption formatting directives that are optionally on the end of this line - fine for non-captions VTT
if (matchTimes) {
result.startTime =
Number(matchTimes[1]) * 60 * 60 +
Number(matchTimes[2]) * 60 +
Number(matchTimes[3]) +
Number(`0.${matchTimes[4]}`);
result.endTime =
Number(matchTimes[6]) * 60 * 60 +
Number(matchTimes[7]) * 60 +
Number(matchTimes[8]) +
Number(`0.${matchTimes[9]}`);
}
} else if (!is.empty(line.trim()) && is.empty(result.text)) {
// If we already have the startTime, then we're definitely up to the text line(s)
const lineSplit = line.trim().split('#xywh=');
[result.text] = lineSplit;
// If there's content in lineSplit[1], then we have sprites. If not, then it's just one frame per image
if (lineSplit[1]) {
[result.x, result.y, result.w, result.h] = lineSplit[1].split(',');
}
}
});
if (result.text) {
processedList.push(result);
}
});
return processedList;
};
/** /**
* Preview thumbnails for seek hover and scrubbing * Preview thumbnails for seek hover and scrubbing
@ -11,7 +60,7 @@ import fetch from '../utils/fetch';
* Notes: * Notes:
* - 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 * - 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
* - 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 * - 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
* - 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 * - 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
*/ */
class PreviewThumbnails { class PreviewThumbnails {
@ -22,7 +71,7 @@ class PreviewThumbnails {
*/ */
constructor(player) { constructor(player) {
this.player = player; this.player = player;
this.thumbnailsDefs = []; this.thumbnails = [];
this.lastMousemoveEventTime = Date.now(); this.lastMousemoveEventTime = Date.now();
this.mouseDown = false; this.mouseDown = false;
this.loadedImages = []; this.loadedImages = [];
@ -33,88 +82,80 @@ class PreviewThumbnails {
} }
get enabled() { get enabled() {
return ( return this.player.isHTML5 && this.player.isVideo && this.player.config.previewThumbnails.enabled;
this.player.isHTML5 &&
this.player.isVideo &&
this.player.config.previewThumbnails.enabled
);
} }
load() { load() {
// Turn off the regular seek tooltip // Turn off the regular seek tooltip
this.player.config.tooltips.seek = false; this.player.config.tooltips.seek = false;
this.getThumbnailsDefs() this.getThumbnails().then(() => {
.then(() => { // Initiate DOM listeners so that our preview thumbnails can be used
// Initiate DOM listeners so that our preview thumbnails can be used this.listeners();
this.listeners();
// Build HTML DOM elements // Build HTML DOM elements
this.elements(); this.elements();
// Check to see if thumb container size was specified manually in CSS // Check to see if thumb container size was specified manually in CSS
this.determineContainerAutoSizing(); this.determineContainerAutoSizing();
}); });
} }
// Download VTT files and parse them // Download VTT files and parse them
getThumbnailsDefs() { getThumbnails() {
return new Promise((resolve, reject) => { return new Promise(resolve => {
if (!this.player.config.previewThumbnails.src) { if (!this.player.config.previewThumbnails.src) {
throw new Error('Missing previewThumbnails.src config attribute'); throw new Error('Missing previewThumbnails.src config attribute');
} }
// previewThumbnails.src can be string or list. If string, convert into single-element list // previewThumbnails.src can be string or list. If string, convert into single-element list
const configSrc = this.player.config.previewThumbnails.src const { src } = this.player.config.previewThumbnails;
const urls = typeof configSrc === 'string' ? [configSrc] : configSrc const urls = is.string(src) ? [src] : src;
const promises = [];
// Loop through each src url. Download and process the VTT file, storing the resulting data in this.thumbnailsDefs // Loop through each src url. Download and process the VTT file, storing the resulting data in this.thumbnails
for (const url of urls) { const promises = urls.map(u => this.getThumbnail(u));
promises.push(this.getThumbnailDef(url));
}
Promise.all(promises) Promise.all(promises).then(() => {
.then(() => { // Sort smallest to biggest (e.g., [120p, 480p, 1080p])
// Sort smallest to biggest (e.g., [120p, 480p, 1080p]) this.thumbnails.sort((x, y) => x.height - y.height);
this.thumbnailsDefs.sort((x, y) => x.height - y.height) this.player.debug.log(`Preview thumbnails: thumbnails: ${JSON.stringify(this.thumbnails, null, 4)}`);
this.player.debug.log('Preview thumbnails: thumbnailsDefs: ' + JSON.stringify(this.thumbnailsDefs, null, 4))
resolve() resolve();
}); });
}) });
} }
// Process individual VTT file // Process individual VTT file
getThumbnailDef(url) { getThumbnail(url) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
fetch(url) fetch(url).then(response => {
.then(response => { const thumbnail = {
const thumbnailsDef = { frames: parseVtt(response),
frames: this.parseVtt(response), height: null,
height: null, urlPrefix: '',
urlPrefix: '', };
};
// If the URLs don't start with '/', then we need to set their relative path to be the location of the VTT file // If the URLs don't start with '/', then we need to set their relative path to be the location of the VTT file
// If the URLs do start with '/', then they obviously don't need a prefix, so it will remain blank // If the URLs do start with '/', then they obviously don't need a prefix, so it will remain blank
if (!thumbnailsDef.frames[0].text.startsWith('/')) { if (!thumbnail.frames[0].text.startsWith('/')) {
thumbnailsDef.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1); thumbnail.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1);
} }
// Download the first frame, so that we can determine/set the height of this thumbnailsDef // Download the first frame, so that we can determine/set the height of this thumbnailsDef
const tempImage = new Image(); const tempImage = new Image();
tempImage.src = thumbnailsDef.urlPrefix + thumbnailsDef.frames[0].text;
tempImage.onload = () => {
thumbnailsDef.height = tempImage.naturalHeight;
thumbnailsDef.width = tempImage.naturalWidth;
this.thumbnailsDefs.push(thumbnailsDef); tempImage.onload = () => {
thumbnail.height = tempImage.naturalHeight;
thumbnail.width = tempImage.naturalWidth;
resolve(); this.thumbnails.push(thumbnail);
}
}) resolve();
}) };
tempImage.src = thumbnail.urlPrefix + thumbnail.frames[0].text;
});
});
} }
/** /**
@ -122,56 +163,50 @@ class PreviewThumbnails {
*/ */
listeners() { listeners() {
// Mouse hover over seek bar // Mouse hover over seek bar
on.call( on.call(this.player, this.player.elements.progress, 'mousemove', event => {
this.player, // Wait until media has a duration
this.player.elements.progress, if (this.player.media.duration) {
'mousemove', // Calculate seek hover position as approx video seconds
event => { const clientRect = this.player.elements.progress.getBoundingClientRect();
// Wait until media has a duration const percentage = (100 / clientRect.width) * (event.pageX - clientRect.left);
if (this.player.media.duration) { this.seekTime = this.player.media.duration * (percentage / 100);
// Calculate seek hover position as approx video seconds
const clientRect = this.player.elements.progress.getBoundingClientRect();
const percentage = 100 / clientRect.width * (event.pageX - clientRect.left);
this.seekTime = this.player.media.duration * (percentage / 100);
if (this.seekTime < 0) this.seekTime = 0; // The mousemove fires for 10+px out to the left
if (this.seekTime > this.player.media.duration - 1) this.seekTime = this.player.media.duration - 1; // Took 1 second off the duration for safety, because different players can disagree on the real duration of a video
this.mousePosX = event.pageX;
// Set time text inside image container if (this.seekTime < 0) {
this.player.elements.display.previewThumbnailTimeText.innerText = formatTime(this.seekTime); // The mousemove fires for 10+px out to the left
this.seekTime = 0;
// Download and show image
this.showImageAtCurrentTime();
} }
if (this.seekTime > this.player.media.duration - 1) {
// Took 1 second off the duration for safety, because different players can disagree on the real duration of a video
this.seekTime = this.player.media.duration - 1;
}
this.mousePosX = event.pageX;
// Set time text inside image container
this.player.elements.display.previewThumbnailTimeText.innerText = formatTime(this.seekTime);
// Download and show image
this.showImageAtCurrentTime();
} }
); });
// Touch device seeking - performs same function as above // Touch device seeking - performs same function as above
on.call( on.call(this.player, this.player.elements.progress, 'touchmove', () => {
this.player, // Wait until media has a duration
this.player.elements.progress, if (this.player.media.duration) {
'touchmove', // Calculate seek hover position as approx video seconds
event => { this.seekTime = this.player.media.duration * (this.player.elements.inputs.seek.value / 100);
// Wait until media has a duration
if (this.player.media.duration) {
// Calculate seek hover position as approx video seconds
this.seekTime = this.player.media.duration * (this.player.elements.inputs.seek.value / 100);
// Download and show image // Download and show image
this.showImageAtCurrentTime(); this.showImageAtCurrentTime();
}
} }
); });
// Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering
on.call( on.call(this.player, this.player.elements.progress, 'mouseleave click', () => {
this.player, this.hideThumbContainer(true);
this.player.elements.progress, });
'mouseleave click',
() => {
this.hideThumbContainer(true);
}
);
this.player.on('play', () => { this.player.on('play', () => {
this.hideThumbContainer(true); this.hideThumbContainer(true);
}); });
@ -180,60 +215,40 @@ class PreviewThumbnails {
}); });
// Show scrubbing preview // Show scrubbing preview
on.call( on.call(this.player, this.player.elements.progress, 'mousedown touchstart', event => {
this.player, // Only act on left mouse button (0), or touch device (!event.button)
this.player.elements.progress, if (!event.button || event.button === 0) {
'mousedown touchstart', this.mouseDown = true;
event => { // Wait until media has a duration
// Only act on left mouse button (0), or touch device (!event.button) if (this.player.media.duration) {
if (!event.button || event.button === 0) { this.showScrubbingContainer();
this.mouseDown = true; this.hideThumbContainer(true);
// Wait until media has a duration
if (this.player.media.duration) {
this.showScrubbingContainer();
this.hideThumbContainer(true);
// Download and show image // Download and show image
this.showImageAtCurrentTime(); this.showImageAtCurrentTime();
}
}
});
on.call(this.player, this.player.media, 'timeupdate', () => {
this.timeAtLastTimeupdate = this.player.media.currentTime;
});
on.call(this.player, this.player.elements.progress, 'mouseup touchend', () => {
this.mouseDown = false;
// Hide scrubbing preview. But wait until the video has successfully seeked before hiding the scrubbing preview
if (Math.ceil(this.timeAtLastTimeupdate) === Math.ceil(this.player.media.currentTime)) {
// The video was already seeked/loaded at the chosen time - hide immediately
this.hideScrubbingContainer();
} else {
// The video hasn't seeked yet. Wait for that
once.call(this.player, this.player.media, 'timeupdate', () => {
// Re-check mousedown - we might have already started scrubbing again
if (!this.mouseDown) {
this.hideScrubbingContainer();
} }
} });
} }
); });
on.call(
this.player,
this.player.media,
'timeupdate',
() => {
this.timeAtLastTimeupdate = this.player.media.currentTime;
}
);
on.call(
this.player,
this.player.elements.progress,
'mouseup touchend',
() => {
this.mouseDown = false;
// Hide scrubbing preview. But wait until the video has successfully seeked before hiding the scrubbing preview
if (Math.ceil(this.timeAtLastTimeupdate) === Math.ceil(this.player.media.currentTime)) {
// The video was already seeked/loaded at the chosen time - hide immediately
this.hideScrubbingContainer();
} else {
// The video hasn't seeked yet. Wait for that
once.call(
this.player,
this.player.media,
'timeupdate',
() => {
// Re-check mousedown - we might have already started scrubbing again
if (!this.mouseDown) {
this.hideScrubbingContainer();
}
}
);
}
}
);
} }
/** /**
@ -241,42 +256,29 @@ class PreviewThumbnails {
*/ */
elements() { elements() {
// Create HTML element: plyr__preview-thumbnail-container // Create HTML element: plyr__preview-thumbnail-container
const previewThumbnailContainer = createElement( const previewThumbnailContainer = createElement('div', {
'div', class: this.player.config.classNames.previewThumbnails.thumbnailContainer,
{ });
class: this.player.config.classNames.previewThumbnails.thumbnailContainer,
},
);
this.player.elements.progress.appendChild(previewThumbnailContainer); this.player.elements.progress.appendChild(previewThumbnailContainer);
this.player.elements.display.previewThumbnailContainer = previewThumbnailContainer; this.player.elements.display.previewThumbnailContainer = previewThumbnailContainer;
// Create HTML element, parent+span: time text (e.g., 01:32:00) // Create HTML element, parent+span: time text (e.g., 01:32:00)
const timeTextContainer = createElement( const timeTextContainer = createElement('div', {
'div', class: this.player.config.classNames.previewThumbnails.timeTextContainer,
{ });
class: this.player.config.classNames.previewThumbnails.timeTextContainer
},
);
this.player.elements.display.previewThumbnailContainer.appendChild(timeTextContainer); this.player.elements.display.previewThumbnailContainer.appendChild(timeTextContainer);
const timeText = createElement( const timeText = createElement('span', {}, '00:00');
'span',
{},
'00:00',
);
timeTextContainer.appendChild(timeText); timeTextContainer.appendChild(timeText);
this.player.elements.display.previewThumbnailTimeText = timeText; this.player.elements.display.previewThumbnailTimeText = timeText;
// Create HTML element: plyr__preview-scrubbing-container // Create HTML element: plyr__preview-scrubbing-container
const previewScrubbingContainer = createElement( const previewScrubbingContainer = createElement('div', {
'div', class: this.player.config.classNames.previewThumbnails.scrubbingContainer,
{ });
class: this.player.config.classNames.previewThumbnails.scrubbingContainer,
},
);
this.player.elements.wrapper.appendChild(previewScrubbingContainer); this.player.elements.wrapper.appendChild(previewScrubbingContainer);
this.player.elements.display.previewScrubbingContainer = previewScrubbingContainer; this.player.elements.display.previewScrubbingContainer = previewScrubbingContainer;
@ -291,15 +293,17 @@ class PreviewThumbnails {
} }
// Find the desired thumbnail index // Find the desired thumbnail index
const thumbNum = this.thumbnailsDefs[0].frames.findIndex(frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime); const thumbNum = this.thumbnails[0].frames.findIndex(
frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime,
);
let qualityIndex = 0; let qualityIndex = 0;
// Check to see if we've already downloaded higher quality versions of this image // Check to see if we've already downloaded higher quality versions of this image
for (let i = 1; i < this.thumbnailsDefs.length; i++) { this.thumbnails.forEach((thumbnail, index) => {
if (this.loadedImages.includes(this.thumbnailsDefs[i].frames[thumbNum].text)) { if (this.loadedImages.includes(thumbnail.frames[thumbNum].text)) {
qualityIndex = i; qualityIndex = index;
} }
} });
// Only proceed if either thumbnum or thumbfilename has changed // Only proceed if either thumbnum or thumbfilename has changed
if (thumbNum !== this.showingThumb) { if (thumbNum !== this.showingThumb) {
@ -310,17 +314,20 @@ class PreviewThumbnails {
// Show the image that's currently specified in this.showingThumb // Show the image that's currently specified in this.showingThumb
loadImage(qualityIndex = 0) { loadImage(qualityIndex = 0) {
let thumbNum = this.showingThumb; const thumbNum = this.showingThumb;
const thumbnail = this.thumbnails[qualityIndex];
const frame = this.thumbnailsDefs[qualityIndex].frames[thumbNum]; const { urlPrefix } = thumbnail;
const thumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; const frame = thumbnail.frames[thumbNum];
const urlPrefix = this.thumbnailsDefs[qualityIndex].urlPrefix; const thumbFilename = thumbnail.frames[thumbNum].text;
const thumbURL = urlPrefix + thumbFilename; const thumbURL = urlPrefix + thumbFilename;
if (!this.currentImageElement || this.currentImageElement.getAttribute('data-thumbfilename') !== thumbFilename) { if (
!this.currentImageElement ||
this.currentImageElement.getAttribute('data-thumbfilename') !== thumbFilename
) {
// If we're already loading a previous image, remove its onload handler - we don't want it to load after this one // If we're already loading a previous image, remove its onload handler - we don't want it to load after this one
// Only do this if not using jpeg sprites. Without jpeg sprites we really want to show as many images as possible, as a best-effort // Only do this if not using sprites. Without sprites we really want to show as many images as possible, as a best-effort
if (this.loadingImage && this.usingJpegSprites) this.loadingImage.onload = null; if (this.loadingImage && this.usingSprites) this.loadingImage.onload = null;
// We're building and adding a new image. In other implementations of similar functionality (Youtube), background image is instead used. But this causes issues with larger images in Firefox and Safari - switching between background images causes a flicker. Putting a new image over the top does not // We're building and adding a new image. In other implementations of similar functionality (Youtube), background image is instead used. But this causes issues with larger images in Firefox and Safari - switching between background images causes a flicker. Putting a new image over the top does not
const previewImage = new Image(); const previewImage = new Image();
@ -330,7 +337,8 @@ class PreviewThumbnails {
this.showingThumbFilename = thumbFilename; this.showingThumbFilename = thumbFilename;
// For some reason, passing the named function directly causes it to execute immediately. So I've wrapped it in an anonymous function... // For some reason, passing the named function directly causes it to execute immediately. So I've wrapped it in an anonymous function...
previewImage.onload = () => this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true); previewImage.onload = () =>
this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true);
this.loadingImage = previewImage; this.loadingImage = previewImage;
this.removeOldImages(previewImage); this.removeOldImages(previewImage);
} else { } else {
@ -342,14 +350,18 @@ class PreviewThumbnails {
} }
showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, newImage = true) { showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, newImage = true) {
this.player.debug.log('Showing thumb: ' + thumbFilename + '. num: ' + thumbNum + '. qual: ' + qualityIndex + '. newimg: ' + newImage); this.player.debug.log(
`Showing thumb: ${thumbFilename}. num: ${thumbNum}. qual: ${qualityIndex}. newimg: ${newImage}`,
);
this.setImageSizeAndOffset(previewImage, frame); this.setImageSizeAndOffset(previewImage, frame);
if (newImage) { if (newImage) {
this.currentContainer.appendChild(previewImage); this.currentContainer.appendChild(previewImage);
this.currentImageElement = previewImage; this.currentImageElement = previewImage;
if (!this.loadedImages.includes(thumbFilename)) this.loadedImages.push(thumbFilename); if (!this.loadedImages.includes(thumbFilename)) {
this.loadedImages.push(thumbFilename);
}
} }
// Preload images before and after the current one // Preload images before and after the current one
@ -363,95 +375,100 @@ class PreviewThumbnails {
// Remove all preview images that aren't the designated current image // Remove all preview images that aren't the designated current image
removeOldImages(currentImage) { removeOldImages(currentImage) {
// Get a list of all images, convert it from a DOM list to an array // Get a list of all images, convert it from a DOM list to an array
const allImages = Array.from(this.currentContainer.children); Array.from(this.currentContainer.children).forEach(image => {
for (let image of allImages) {
if (image.tagName === 'IMG') { if (image.tagName === 'IMG') {
const removeDelay = this.usingJpegSprites ? 500 : 1000; const removeDelay = this.usingSprites ? 500 : 1000;
if (image.getAttribute('data-thumbnum') !== currentImage.getAttribute('data-thumbnum') && !image.getAttribute('data-deleting')) { if (
image.getAttribute('data-thumbnum') !== currentImage.getAttribute('data-thumbnum') &&
!image.getAttribute('data-deleting')
) {
// 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 // 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
// First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function
image.setAttribute('data-deleting', 'true'); image.setAttribute('data-deleting', 'true');
const currentContainer = this.currentContainer; // This has to be set before the timeout - to prevent issues switching between hover and scrub const { currentContainer } = this; // This has to be set before the timeout - to prevent issues switching between hover and scrub
setTimeout(() => { setTimeout(() => {
currentContainer.removeChild(image); currentContainer.removeChild(image);
this.player.debug.log('Removing thumb: ' + image.getAttribute('data-thumbfilename')); this.player.debug.log(`Removing thumb: ${image.getAttribute('data-thumbfilename')}`);
}, removeDelay) }, removeDelay);
} }
} }
} });
} }
// Preload images before and after the current one. Only if the user is still hovering/seeking the same frame // Preload images before and after the current one. Only if the user is still hovering/seeking the same frame
// This will only preload the lowest quality // This will only preload the lowest quality
preloadNearby(thumbNum, forward = true) { preloadNearby(thumbNum, forward = true) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
setTimeout(() => { setTimeout(() => {
const oldThumbFilename = this.thumbnailsDefs[0].frames[thumbNum].text; const oldThumbFilename = this.thumbnails[0].frames[thumbNum].text;
if (this.showingThumbFilename === oldThumbFilename) { if (this.showingThumbFilename === oldThumbFilename) {
// Find the nearest thumbs with different filenames. Sometimes it'll be the next index, but in the case of jpeg sprites, it might be 100+ away // 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
let thumbnailsDefsCopy let thumbnailsClone;
if (forward) { if (forward) {
thumbnailsDefsCopy = this.thumbnailsDefs[0].frames.slice(thumbNum); thumbnailsClone = this.thumbnails[0].frames.slice(thumbNum);
} else { } else {
thumbnailsDefsCopy = this.thumbnailsDefs[0].frames.slice(0, thumbNum).reverse(); thumbnailsClone = this.thumbnails[0].frames.slice(0, thumbNum).reverse();
} }
let foundOne = false; let foundOne = false;
for (const frame of thumbnailsDefsCopy) { thumbnailsClone.forEach(frame => {
const newThumbFilename = frame.text; const newThumbFilename = frame.text;
if (newThumbFilename !== oldThumbFilename) { if (newThumbFilename !== oldThumbFilename) {
// Found one with a different filename. Make sure it hasn't already been loaded on this page visit // Found one with a different filename. Make sure it hasn't already been loaded on this page visit
if (!this.loadedImages.includes(newThumbFilename)) { if (!this.loadedImages.includes(newThumbFilename)) {
foundOne = true; foundOne = true;
this.player.debug.log('Preloading thumb filename: ' + newThumbFilename); this.player.debug.log(`Preloading thumb filename: ${newThumbFilename}`);
const urlPrefix = this.thumbnailsDefs[0].urlPrefix; const { urlPrefix } = this.thumbnails[0];
const thumbURL = urlPrefix + newThumbFilename; const thumbURL = urlPrefix + newThumbFilename;
const previewImage = new Image(); const previewImage = new Image();
previewImage.src = thumbURL; previewImage.src = thumbURL;
previewImage.onload = () => { previewImage.onload = () => {
this.player.debug.log('Preloaded thumb filename: ' + newThumbFilename); this.player.debug.log(`Preloaded thumb filename: ${newThumbFilename}`);
if (!this.loadedImages.includes(newThumbFilename)) this.loadedImages.push(newThumbFilename); if (!this.loadedImages.includes(newThumbFilename))
this.loadedImages.push(newThumbFilename);
// We don't resolve until the thumb is loaded // We don't resolve until the thumb is loaded
resolve() resolve();
}; };
} }
break;
} }
} });
// If there are none to preload then we want to resolve immediately // If there are none to preload then we want to resolve immediately
if (!foundOne) resolve(); if (!foundOne) {
resolve();
}
} }
}, 300) }, 300);
}) });
} }
// If user has been hovering current image for half a second, look for a higher quality one // If user has been hovering current image for half a second, look for a higher quality one
getHigherQuality(currentQualityIndex, previewImage, frame, thumbFilename) { getHigherQuality(currentQualityIndex, previewImage, frame, thumbFilename) {
if (currentQualityIndex < this.thumbnailsDefs.length - 1) { if (currentQualityIndex < this.thumbnails.length - 1) {
// 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 // 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
let previewImageHeight = previewImage.naturalHeight; let previewImageHeight = previewImage.naturalHeight;
if (this.usingJpegSprites) previewImageHeight = frame.h;
if (this.usingSprites) {
previewImageHeight = frame.h;
}
if (previewImageHeight < this.thumbContainerHeight) { if (previewImageHeight < this.thumbContainerHeight) {
// Recurse back to the loadImage function - show a higher quality one, but only if the viewer is on this frame for a while // Recurse back to the loadImage function - show a higher quality one, but only if the viewer is on this frame for a while
setTimeout(() => { setTimeout(() => {
// Make sure the mouse hasn't already moved on and started hovering at another image // Make sure the mouse hasn't already moved on and started hovering at another image
if (this.showingThumbFilename === thumbFilename) { if (this.showingThumbFilename === thumbFilename) {
this.player.debug.log('Showing higher quality thumb for: ' + thumbFilename) this.player.debug.log(`Showing higher quality thumb for: ${thumbFilename}`);
this.loadImage(currentQualityIndex + 1); this.loadImage(currentQualityIndex + 1);
} }
}, 300) }, 300);
} }
} }
} }
@ -459,25 +476,19 @@ class PreviewThumbnails {
get currentContainer() { get currentContainer() {
if (this.mouseDown) { if (this.mouseDown) {
return this.player.elements.display.previewScrubbingContainer; return this.player.elements.display.previewScrubbingContainer;
} else {
return this.player.elements.display.previewThumbnailContainer;
} }
return this.player.elements.display.previewThumbnailContainer;
} }
get usingJpegSprites() { get usingSprites() {
if (this.thumbnailsDefs[0].frames[0].w) { return Object.keys(this.thumbnails[0].frames[0]).includes('w');
return true;
} else {
return false;
}
} }
get thumbAspectRatio() { get thumbAspectRatio() {
if (this.usingJpegSprites) { if (this.usingSprites) {
return this.thumbnailsDefs[0].frames[0].w / this.thumbnailsDefs[0].frames[0].h; return this.thumbnails[0].frames[0].w / this.thumbnails[0].frames[0].h;
} else {
return this.thumbnailsDefs[0].width / this.thumbnailsDefs[0].height;
} }
return this.thumbnails[0].width / this.thumbnails[0].height;
} }
get thumbContainerHeight() { get thumbContainerHeight() {
@ -485,19 +496,18 @@ class PreviewThumbnails {
// return this.player.elements.container.clientHeight; // return this.player.elements.container.clientHeight;
// return this.player.media.clientHeight; // return this.player.media.clientHeight;
return this.player.media.clientWidth / this.thumbAspectRatio; // Can't use media.clientHeight - html5 video goes big and does black bars above and below return this.player.media.clientWidth / this.thumbAspectRatio; // Can't use media.clientHeight - html5 video goes big and does black bars above and below
} else {
// return this.player.elements.container.clientHeight / 4;
return this.player.media.clientWidth / this.thumbAspectRatio / 4;
} }
// return this.player.elements.container.clientHeight / 4;
return this.player.media.clientWidth / this.thumbAspectRatio / 4;
} }
get currentImageElement() { get currentImageElement() {
if (this.mouseDown) { if (this.mouseDown) {
return this.currentScrubbingImageElement; return this.currentScrubbingImageElement;
} else {
return this.currentThumbnailImageElement;
} }
return this.currentThumbnailImageElement;
} }
set currentImageElement(element) { set currentImageElement(element) {
if (this.mouseDown) { if (this.mouseDown) {
this.currentScrubbingImageElement = element; this.currentScrubbingImageElement = element;
@ -509,6 +519,7 @@ class PreviewThumbnails {
showThumbContainer() { showThumbContainer() {
this.player.elements.display.previewThumbnailContainer.style.opacity = 1; this.player.elements.display.previewThumbnailContainer.style.opacity = 1;
} }
hideThumbContainer(clearShowing = false) { hideThumbContainer(clearShowing = false) {
this.player.elements.display.previewThumbnailContainer.style.opacity = 0; this.player.elements.display.previewThumbnailContainer.style.opacity = 0;
@ -521,6 +532,7 @@ class PreviewThumbnails {
showScrubbingContainer() { showScrubbingContainer() {
this.player.elements.display.previewScrubbingContainer.style.opacity = 1; this.player.elements.display.previewScrubbingContainer.style.opacity = 1;
} }
hideScrubbingContainer() { hideScrubbingContainer() {
this.player.elements.display.previewScrubbingContainer.style.opacity = 0; this.player.elements.display.previewScrubbingContainer.style.opacity = 0;
this.showingThumb = null; this.showingThumb = null;
@ -550,30 +562,31 @@ class PreviewThumbnails {
const previewContainer = this.player.elements.display.previewThumbnailContainer; const previewContainer = this.player.elements.display.previewThumbnailContainer;
// Find the lowest and highest desired left-position, so we don't slide out the side of the video container // Find the lowest and highest desired left-position, so we don't slide out the side of the video container
const minVal = (plyrRect.left - seekbarRect.left + 10); const minVal = plyrRect.left - seekbarRect.left + 10;
const maxVal = (plyrRect.right - seekbarRect.left - (previewContainer.clientWidth) - 10); const maxVal = plyrRect.right - seekbarRect.left - previewContainer.clientWidth - 10;
// Set preview container position to: mousepos, minus seekbar.left, minus half of previewContainer.clientWidth // Set preview container position to: mousepos, minus seekbar.left, minus half of previewContainer.clientWidth
let previewPos = this.mousePosX - seekbarRect.left - (previewContainer.clientWidth / 2); let previewPos = this.mousePosX - seekbarRect.left - previewContainer.clientWidth / 2;
if (previewPos < minVal) { if (previewPos < minVal) {
previewPos = minVal; previewPos = minVal;
} }
if (previewPos > maxVal) { if (previewPos > maxVal) {
previewPos = maxVal; previewPos = maxVal;
} }
previewContainer.style.left = previewPos + 'px'; previewContainer.style.left = `${previewPos}px`;
} }
// Can't use 100% width, in case the video is a different aspect ratio to the video container // Can't use 100% width, in case the video is a different aspect ratio to the video container
setScrubbingContainerSize() { setScrubbingContainerSize() {
this.player.elements.display.previewScrubbingContainer.style.width = `${this.player.media.clientWidth}px`; this.player.elements.display.previewScrubbingContainer.style.width = `${this.player.media.clientWidth}px`;
this.player.elements.display.previewScrubbingContainer.style.height = `${this.player.media.clientWidth/this.thumbAspectRatio}px`; // Can't use media.clientHeight - html5 video goes big and does black bars above and below this.player.elements.display.previewScrubbingContainer.style.height = `${this.player.media.clientWidth /
this.thumbAspectRatio}px`; // Can't use media.clientHeight - html5 video goes big and does black bars above and below
} }
// Jpeg sprites need to be offset to the correct location // Sprites need to be offset to the correct location
setImageSizeAndOffset(previewImage, frame) { setImageSizeAndOffset(previewImage, frame) {
if (this.usingJpegSprites) { if (this.usingSprites) {
// Find difference between jpeg height and preview container height // Find difference between height and preview container height
const heightMulti = this.thumbContainerHeight / frame.h; const heightMulti = this.thumbContainerHeight / frame.h;
previewImage.style.height = `${previewImage.naturalHeight * heightMulti}px`; previewImage.style.height = `${previewImage.naturalHeight * heightMulti}px`;
@ -582,51 +595,6 @@ class PreviewThumbnails {
previewImage.style.top = `-${frame.y * heightMulti}px`; // todo: might need to round this one up too previewImage.style.top = `-${frame.y * heightMulti}px`; // todo: might need to round this one up too
} }
} }
// Arg: vttDataString example: "WEBVTT\n\n1\n00:00:05.000 --> 00:00:10.000\n1080p-00001.jpg"
parseVtt(vttDataString) {
const processedList = [];
const frames = vttDataString.split(/\r\n\r\n|\n\n|\r\r/);
for (const frame of frames) {
const result = {};
for (const line of frame.split(/\r\n|\n|\r/)) {
if (result.startTime == null) {
// The line with start and end times on it is the first line of interest
const matchTimes = line.match(/([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2,3})( ?--> ?)([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2,3})/) // Note that this currently ignores caption formatting directives that are optionally on the end of this line - fine for non-captions VTT
if (matchTimes) {
result.startTime = Number(matchTimes[1]) * 60 * 60 + Number(matchTimes[2]) * 60 + Number(matchTimes[3]) + Number("0." + matchTimes[4])
result.endTime = Number(matchTimes[6]) * 60 * 60 + Number(matchTimes[7]) * 60 + Number(matchTimes[8]) + Number("0." + matchTimes[9])
}
} else {
// If we already have the startTime, then we're definitely up to the text line(s)
if (line.trim().length > 0) {
if (!result.text) {
const lineSplit = line.trim().split('#xywh=');
result.text = lineSplit[0];
// If there's content in lineSplit[1], then we have jpeg sprites. If not, then it's just one frame per jpeg
if (lineSplit[1]) {
const xywh = lineSplit[1].split(',');
result.x = xywh[0];
result.y = xywh[1];
result.w = xywh[2];
result.h = xywh[3];
}
}
}
}
}
if (result.text) {
processedList.push(result);
}
}
return processedList;
}
} }
export default PreviewThumbnails; export default PreviewThumbnails;

View File

@ -3,75 +3,74 @@
// -------------------------------------------------------------- // --------------------------------------------------------------
.plyr__preview-thumbnail-container { .plyr__preview-thumbnail-container {
background-color: rgba(0,0,0,0.5); background-color: rgba(0, 0, 0, 0.5);
border: 1px solid rgba(0,0,0,0); // The background colour above applies to the area under the border - so appears to be a border of 0.5 opacity black border: 1px solid rgba(0, 0, 0, 0); // The background colour above applies to the area under the border - so appears to be a border of 0.5 opacity black
border-radius: 0px; border-radius: 2px;
bottom: 100%; bottom: 100%;
box-shadow: $plyr-tooltip-shadow; box-shadow: $plyr-tooltip-shadow;
left: 50%; left: 50%;
line-height: 1.3; line-height: 1.3;
margin-bottom: $plyr-tooltip-padding * 2; margin-bottom: $plyr-tooltip-padding * 2;
opacity: 0; opacity: 0;
overflow: hidden;
pointer-events: none; pointer-events: none;
position: absolute; position: absolute;
transition: opacity 0.2s 0.1s ease; transition: opacity 0.2s 0.1s ease;
white-space: nowrap; white-space: nowrap;
z-index: 2; z-index: 2;
overflow: hidden;
img { img {
position: absolute; border-radius: 2px;
left: 0px;
top: 0px;
height: 100%; // Non-jpeg-sprite images are 100%. Jpeg sprites will have their size applied by javascript height: 100%; // Non-jpeg-sprite images are 100%. Jpeg sprites will have their size applied by javascript
width: 100%; left: 0;
max-height: none; max-height: none;
max-width: none; max-width: none;
border-radius: 0px; position: absolute;
top: 0;
width: 100%;
} }
// Seek time text // Seek time text
.plyr__preview-time-text-container { .plyr__preview-time-text-container {
position: absolute; bottom: 0;
bottom: 0px; left: 0;
left: 0px;
right: 0px;
margin-bottom: 2px; margin-bottom: 2px;
position: absolute;
right: 0;
z-index: 3; z-index: 3;
span { span {
background-color: rgba(0,0,0,0.55); background-color: rgba(0, 0, 0, 0.55);
color: rgba(255,255,255,1); color: rgba(255, 255, 255, 1);
padding: 4px 6px 3px 6px;
font-size: $plyr-font-size-small; font-size: $plyr-font-size-small;
font-weight: $plyr-font-weight-regular; font-weight: $plyr-font-weight-regular;
padding: 4px 6px 3px;
} }
} }
} }
.plyr__preview-scrubbing-container { .plyr__preview-scrubbing-container {
position: absolute; bottom: 0;
left: 0px; filter: blur(1px);
right: 0px;
top: 0px;
bottom: 0px;
height: 100%; height: 100%;
width: 100%; left: 0;
margin: auto; // Required when video is different dimensions to container (e.g., fullscreen) margin: auto; // Required when video is different dimensions to container (e.g., fullscreen)
overflow: hidden; overflow: hidden;
z-index: 1; position: absolute;
right: 0;
top: 0;
transition: opacity 0.3s ease; transition: opacity 0.3s ease;
filter: blur(1px); width: 100%;
z-index: 1;
img { img {
position: absolute;
left: 0px;
top: 0px;
height: 100%; height: 100%;
width: 100%; left: 0;
max-height: none; max-height: none;
max-width: none; max-width: none;
object-fit: contain; object-fit: contain;
position: absolute;
top: 0;
width: 100%;
} }
} }