From 8f27611911d9e6b4c6012e1af44b6c14cf6eaffb Mon Sep 17 00:00:00 2001 From: James Date: Mon, 12 Nov 2018 15:55:26 +1100 Subject: Preview seek/scrubbing thumbnails --- src/js/config/defaults.js | 7 + src/js/plugins/previewThumbnails.js | 499 ++++++++++++++++++++++++++++++++ src/js/plyr.js | 6 + src/sass/plugins/previewThumbnails.scss | 70 +++++ src/sass/plyr.scss | 1 + 5 files changed, 583 insertions(+) create mode 100644 src/js/plugins/previewThumbnails.js create mode 100644 src/sass/plugins/previewThumbnails.scss (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 95de6951..70f12a80 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -342,6 +342,8 @@ const defaults = { loading: 'plyr--loading', hover: 'plyr--hover', tooltip: 'plyr__tooltip', + previewThumbnailContainer: 'plyr__preview-thumbnail-container', + previewScrubbingContainer: 'plyr__preview-scrubbing-container', cues: 'plyr__cues', hidden: 'plyr__sr-only', hideControls: 'plyr--hide-controls', @@ -395,6 +397,11 @@ const defaults = { enabled: false, publisherId: '', }, + + // Preview Thumbnails plugin + previewThumbnails: { + enabled: false, + } }; export default defaults; diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js new file mode 100644 index 00000000..1993ae96 --- /dev/null +++ b/src/js/plugins/previewThumbnails.js @@ -0,0 +1,499 @@ +import { formatTime } from '../utils/time'; +import { on, once, toggleListener, triggerEvent } from '../utils/events'; +import { createElement, emptyElement, getAttributesFromSelector, getElement, getElements, hasClass, matches, removeElement, setAttributes, setFocus, toggleClass, toggleHidden } from '../utils/elements'; +import fetch from '../utils/fetch'; + +/** + * Preview thumbnails for seek hover and scrubbing + * Seeking: Hover over the seek bar (desktop only): shows a small preview container above the seek bar + * 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 + * + * 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 + * - 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 + */ + +class PreviewThumbnails { + /** + * PreviewThumbnails constructor. + * @param {object} player + * @return {PreviewThumbnails} + */ + constructor(player) { + this.player = player; + this.thumbnailsDefs = []; + this.showingThumb = null; // Index of the currently displayed thumbnail + this.lastMousemoveEventTime = Date.now(); + this.mouseDown = false; + this.imageShowCounter = 0; + this.imageTryShowCounter = 0; + + if (this.enabled) { + this.load(); + } + } + + get enabled() { + return ( + this.player.isHTML5 && + this.player.isVideo && + this.player.config.previewThumbnails.enabled + ); + } + + load() { + this.getThumbnailsDefs() + .then(() => { + // Initiate DOM listeners so that our preview thumbnails can be used + this.listeners(); + + // Build HTML DOM elements + this.elements(); + + // Turn off the regular seek tooltip + this.player.config.tooltips.seek = false; + + // Check to see if thumb container size was specified manually in CSS + this.determineContainerAutoSizing(); + }); + } + + // Download VTT files and parse them + getThumbnailsDefs() { + return new Promise((resolve, reject) => { + if (!this.player.config.previewThumbnails.src) { + throw new Error('Missing previewThumbnails.src config attribute'); + } + + // previewThumbnails.src can be string or list. If string, convert into single-element list + const configSrc = this.player.config.previewThumbnails.src + const urls = typeof configSrc === 'string' ? [configSrc] : configSrc + const promises = []; + + // Loop through each src url. Download and process the VTT file, storing the resulting data in this.thumbnailsDefs + for (const url of urls) { + promises.push(this.getThumbnailDef(url)); + } + + Promise.all(promises) + .then(() => { + // Sort smallest to biggest (e.g., [120p, 480p, 1080p]) + this.thumbnailsDefs.sort((x, y) => x.height - y.height) + this.player.debug.log('Preview thumbnails: thumbnailsDefs: ' + JSON.stringify(this.thumbnailsDefs, null, 4)) + + resolve() + }); + }) + } + + // Process individual VTT file + getThumbnailDef (url) { + return new Promise((resolve, reject) => { + fetch(url) + .then(response => { + const thumbnailsDef = { + frames: this.parseVtt(response), + height: null, + 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 do start with '/', then they obviously don't need a prefix, so it will remain blank + if (!thumbnailsDef.frames[0].text.startsWith('/')) { + thumbnailsDef.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1); + } + + // Download the first frame, so that we can determine/set the height of this thumbnailsDef + const tempImage = new Image(); + tempImage.src = thumbnailsDef.urlPrefix + thumbnailsDef.frames[0].text; + tempImage.onload = () => { + thumbnailsDef.height = tempImage.naturalHeight; + + this.thumbnailsDefs.push(thumbnailsDef); + + resolve(); + } + }) + }) + } + + /** + * Setup hooks for Plyr and window events + */ + listeners() { + // Mouse hover over seek bar + on.call( + this.player, + this.player.elements.progress, + 'mousemove', + event => { + // Wait until media has a duration + if (this.player.media.duration) { + // 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 + this.player.elements.display.previewThumbnailTimeText.innerText = formatTime(this.seekTime); + + // Download and show image + this.showImageAtCurrentTime(); + } + } + ); + + // Touch device seeking - performs same function as above + on.call( + this.player, + this.player.elements.progress, + 'touchmove', + event => { + // 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 + this.showImageAtCurrentTime(); + } + } + ); + + // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering + on.call( + this.player, + this.player.elements.progress, + 'mouseleave click', + () => { + this.hideThumbContainer(); + } + ); + this.player.on('play', () => { + this.hideThumbContainer(); + }); + this.player.on('seeked', () => { + this.hideThumbContainer(); + }); + + // Show scrubbing preview + on.call( + this.player, + this.player.elements.progress, + 'mousedown touchstart', + () => { + this.mouseDown = true; + this.showScrubbingContainer(); + this.hideThumbContainer(); + } + ); + 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(); + } + } + ); + } + } + ); + } + + /** + * Create HTML elements for image containers + */ + elements() { + // Create HTML element: plyr__preview-thumbnail-container + const previewThumbnailContainer = createElement( + 'div', + { + class: this.player.config.classNames.previewThumbnailContainer, + }, + ); + + this.player.elements.progress.appendChild(previewThumbnailContainer); + this.player.elements.display.previewThumbnailContainer = previewThumbnailContainer; + + const timeText = createElement( + 'span', + {}, + '00:00', + ); + + this.player.elements.display.previewThumbnailContainer.appendChild(timeText); + this.player.elements.display.previewThumbnailTimeText = timeText; + + // Create HTML element: plyr__preview-scrubbing-container + const previewScrubbingContainer = createElement( + 'div', + { + class: this.player.config.classNames.previewScrubbingContainer, + }, + ); + + this.player.elements.wrapper.appendChild(previewScrubbingContainer); + this.player.elements.display.previewScrubbingContainer = previewScrubbingContainer; + } + + showImageAtCurrentTime () { + if (!this.mouseDown) { + this.showThumbContainer(); + } + + this.setThumbContainerSizeAndPos(); + + // Check when we last loaded an image - don't show more than one new one every 500ms + if (this.lastMousemoveEventTime < Date.now() - 150) { + this.lastMousemoveEventTime = Date.now(); + + // Find the first thumbnail that's after `time`. Note `this.seekTime+1` - we're actually looking 1 second ahead, because it's more likely then that the viewer will actually get to see the preview frame in the actual video. This hack should be removed if we ever choose to make it seek to the nearest thumb time + const thumbNum = this.thumbnailsDefs[0].frames.findIndex(frame => this.seekTime+1 >= frame.startTime && this.seekTime <= frame.endTime); + + // Only show if the thumbnail to show is different to last time + if (thumbNum !== this.showingThumb) { + this.showingThumb = thumbNum; + this.showImage(); + } + } else { + // Set a timeout so that we always fire this function once after the mouse stops moving. If not for this, the mouse preview would often be a bit stale + if (!this.mousemoveEventTimeout) { + this.mousemoveEventTimeout = setTimeout(() => { + // Don't follow through after the timeout if it's since been hidden + if (this.player.elements.display.previewThumbnailContainer.style.opacity === 1) { + this.showImageAtCurrentTime(); + this.mousemoveEventTimeout = null; + } + }, 200) + } + } + } + + // Show the image that's currently specified in this.showingThumb + showImage (qualityIndex = 0) { + this.imageTryShowCounter += 1; + const localImageTryShowCounter = this.imageTryShowCounter; + let thumbNum = this.showingThumb; + + if (thumbNum === this.thumbnailsDefs[qualityIndex].frames.length) { + // It can attempt to preview up to 5 seconds out past the end of the video. So we'll just show the last frame + thumbNum -= 1; + this.showingThumb = thumbNum; + } + + this.player.debug.log(`Preview thumbnails: showing thumbnum: ${thumbNum}: ${JSON.stringify(this.thumbnailsDefs[qualityIndex].frames[thumbNum])}`); + + const thumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; + const urlPrefix = this.thumbnailsDefs[qualityIndex].urlPrefix; + const thumbURL = urlPrefix + thumbFilename; + + // 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(); + previewImage.src = thumbURL; + previewImage.setAttribute('data-thumbnum', thumbNum); + + previewImage.onload = () => { + // Many images are loaded within milliseconds of each other. An earlier one might be the last one to finish loading. Make sure we don't show an images out of order + if (localImageTryShowCounter >= this.imageShowCounter) { + this.imageShowCounter = localImageTryShowCounter; + + this.currentContainer.appendChild(previewImage); + + // Now that this one is showing, start pre-loading a batch of nearby images. But only if this isn't a revisit + // this.preloadNearbyImages(thumbNum); + this.thumbnailsDefs[qualityIndex].frames[thumbNum].loaded = true + + this.removeOldImages(); + + // Look for a higher quality version of the same frame + if (qualityIndex < this.thumbnailsDefs.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 + let previewContainerHeight = this.player.elements.display.previewThumbnailContainer.clientHeight; + if (this.mouseDown) previewContainerHeight = this.player.elements.display.previewScrubbingContainer.clientHeight; + // Adjust for HiDPI screen + if (window.devicePixelRatio) previewContainerHeight *= window.devicePixelRatio; + + if (previewImage.naturalHeight < previewContainerHeight) { + // Recurse this function - show a higher quality one, but only if the viewer is on this frame for a while + setTimeout(() => { + // Make sure the mouse hasn't already moved on and started hovering at another frame + if (this.showingThumb === thumbNum) { + this.showImage(qualityIndex + 1); + } + }, 150) + } + } + } + } + } + + // Not using this -- Preloading looked like maybe a good idea, but it seems to actually cause more trouble than it solves. Slow connections get really backed up. Fast connections don't really need it + // If we were to try using this again, we might need to look at not starting a second preload while another is still going? + preloadNearbyImages(thumbNum, amountToPreload=30) { + const actualShowingThumb = [...this.currentContainer.children].reverse()[0].getAttribute('data-thumbnum'); + if (actualShowingThumb && Number(actualShowingThumb) === this.showingThumb) { + let startNum = thumbNum - amountToPreload/2; + let endNum = thumbNum + amountToPreload/2; + if (startNum < 0) startNum = 0; + if (endNum > this.thumbnailsDefs[0].frames.length - 1) endNum = this.thumbnailsDefs[0].frames.length - 1; + + for (let i = startNum; i <= endNum; i++) { + if (!this.thumbnailsDefs[0].frames[i].loaded) { + this.player.debug.log('Thumbnail previews: preloading: ' + i); + + const thumbFilename = this.thumbnailsDefs[0].frames[i].text; + const urlPrefix = this.thumbnailsDefs[0].urlPrefix; + const thumbURL = urlPrefix + thumbFilename; + + // 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(); + previewImage.src = thumbURL; + + // Set loaded attribute. This will prevent us from wasting CPU constantly trying to preload images that we already have loaded + this.thumbnailsDefs[0].frames[i].loaded = true; + } + } + } + } + + removeOldImages() { + // Get a list of all images, and reverse it - so that we can start from the end and delete all except for the most recent + const allImages = [...this.currentContainer.children].reverse(); + + // Start at the third image image - so we leave the last two images. Leaving only one might result in flickering if the newest one hasn't finished rendering yet + for (let i = 2; i < allImages.length; i++) { + if (allImages[i].tagName === 'IMG') { + this.currentContainer.removeChild(allImages[i]); + } + } + } + + get currentContainer() { + if (this.mouseDown) { + return this.player.elements.display.previewScrubbingContainer; + } else { + return this.player.elements.display.previewThumbnailContainer; + } + } + + showThumbContainer() { + this.player.elements.display.previewThumbnailContainer.style.opacity = 1; + } + hideThumbContainer() { + this.player.elements.display.previewThumbnailContainer.style.opacity = 0; + } + + showScrubbingContainer() { + this.player.elements.display.previewScrubbingContainer.style.opacity = 1; + } + hideScrubbingContainer() { + this.player.elements.display.previewScrubbingContainer.style.opacity = 0; + } + + determineContainerAutoSizing() { + if (this.player.elements.display.previewThumbnailContainer.clientHeight > 20) { + this.sizeSpecifiedInCSS = true; // This will prevent auto sizing in this.setThumbContainerSizeAndPos() + } + } + + // 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 + setThumbContainerSizeAndPos() { + // if (this.player.config.previewThumbnails.autoSize) { + if (!this.sizeSpecifiedInCSS) { + const videoAspectRatio = this.player.media.videoWidth / this.player.media.videoHeight; + const thumbHeight = this.player.elements.container.clientHeight / 4; + const thumbWidth = thumbHeight * videoAspectRatio; + this.player.elements.display.previewThumbnailContainer.style.height = `${thumbHeight}px`; + this.player.elements.display.previewThumbnailContainer.style.width = `${thumbWidth}px`; + } + + this.setThumbContainerPos(); + } + + setThumbContainerPos() { + const seekbarRect = this.player.elements.progress.getBoundingClientRect(); + const plyrRect = this.player.elements.container.getBoundingClientRect(); + 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 + const minVal = (plyrRect.left - seekbarRect.left + 10); + const maxVal = (plyrRect.right - seekbarRect.left - (previewContainer.clientWidth) - 10); + + // Set preview container position to: mousepos, minus seekbar.left, minus half of previewContainer.clientWidth + let previewPos = this.mousePosX - seekbarRect.left - (previewContainer.clientWidth / 2); + if (previewPos < minVal) { + previewPos = minVal; + } + if (previewPos > maxVal) { + previewPos = maxVal; + } + previewContainer.style.left = previewPos + 'px'; + } + + // 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) { + result.text = line.trim() + } else { + result.text += '\n' + line.trim() + } + } + } + } + + if (result.text) { + processedList.push(result) + } + } + + return processedList + } +} + +export default PreviewThumbnails; diff --git a/src/js/plyr.js b/src/js/plyr.js index daebdadc..49fc7c5a 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -15,6 +15,7 @@ import Fullscreen from './fullscreen'; import Listeners from './listeners'; import media from './media'; import Ads from './plugins/ads'; +import PreviewThumbnails from './plugins/previewThumbnails'; import source from './source'; import Storage from './storage'; import support from './support'; @@ -306,6 +307,11 @@ class Plyr { // Seek time will be recorded (in listeners.js) so we can prevent hiding controls for a few seconds after seek this.lastSeekTime = 0; + + // Setup preview thumbnails if enabled + if (this.config.previewThumbnails.enabled) { + this.previewThumbnails = new PreviewThumbnails(this); + } } // --------------------------------------- diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss new file mode 100644 index 00000000..92702e39 --- /dev/null +++ b/src/sass/plugins/previewThumbnails.scss @@ -0,0 +1,70 @@ +// -------------------------------------------------------------- +// Preview Thumbnails +// -------------------------------------------------------------- + +.plyr__preview-thumbnail-container { + 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-radius: 0px; + bottom: 100%; + box-shadow: $plyr-tooltip-shadow; + left: 50%; + line-height: 1.3; + margin-bottom: $plyr-tooltip-padding * 2; + opacity: 0; + pointer-events: none; + position: absolute; + transition: opacity 0.2s 0.1s ease; + white-space: nowrap; + z-index: 2; + + img { + position: absolute; + left: 0px; + right: 0px; + top: 0px; + bottom: 0px; + margin: auto; + height: 100%; + width: 100%; + border-radius: 0px; + } + + // Seek time text + span { + position: absolute; + bottom: 0px; + z-index: 3; + transform: translate(-50%,0); + background-color: rgba(0,0,0,0.55); + color: rgba(255,255,255,1); + padding: 4px 6px 3px 6px; + font-size: $plyr-font-size-small; + font-weight: $plyr-font-weight-regular; + } +} + +.plyr__preview-scrubbing-container { + position: absolute; + left: 0px; + right: 0px; + top: 0px; + bottom: 0px; + height: 100%; + width: 100%; + z-index: 1; + transition: opacity 0.3s 0.3s ease; + filter: blur(1px); + + img { + position: absolute; + left: 0px; + right: 0px; + top: 0px; + bottom: 0px; + margin: auto; + height: 100%; + width: 100%; + object-fit: contain; + } +} diff --git a/src/sass/plyr.scss b/src/sass/plyr.scss index 3d824f7d..468c534c 100644 --- a/src/sass/plyr.scss +++ b/src/sass/plyr.scss @@ -42,6 +42,7 @@ @import 'states/fullscreen'; @import 'plugins/ads'; +@import 'plugins/previewThumbnails'; @import 'utils/animation'; @import 'utils/hidden'; -- cgit v1.2.3 From 0e181133c1570a85f0117ad0b8ac5c0008e57a88 Mon Sep 17 00:00:00 2001 From: Raccoon Date: Mon, 12 Nov 2018 15:37:46 +0800 Subject: Calling customized controls function with proper arguments --- src/js/controls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/js/controls.js b/src/js/controls.js index 4f453e6a..f414f6d6 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -1587,7 +1587,7 @@ const controls = { // If function, run it and use output if (is.function(this.config.controls)) { - this.config.controls = this.config.controls.call(this.props); + this.config.controls = this.config.controls.call(this, props); } // Convert falsy controls to empty array (primarily for empty strings) -- cgit v1.2.3 From 80813b0406e47459f7002504a4d9a9ddbfe1ccfd Mon Sep 17 00:00:00 2001 From: Guru Prasad Srinivasa Date: Mon, 19 Nov 2018 22:57:07 -0500 Subject: Replaced calls to player.restart() and player.togglePlay() with proxy(...) to ensure that custom handlers are called --- src/js/listeners.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/listeners.js b/src/js/listeners.js index dd6e2adb..5c93299c 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -386,10 +386,10 @@ class Listeners { } if (player.ended) { - player.restart(); - player.play(); + this.proxy(event, player.restart, 'restart'); + this.proxy(event, player.togglePlay, 'play'); } else { - player.togglePlay(); + this.proxy(event, player.togglePlay, 'play'); } }); } -- cgit v1.2.3 From df5b7a008d4c09e384f24443f84f95ae9ce7fd0a Mon Sep 17 00:00:00 2001 From: Samuel Elgozi Date: Thu, 22 Nov 2018 19:35:01 +0200 Subject: Fix: buffer progress bar transition on webkit The transition was set on the wrong pseudo element for WebKit browsers. --- src/sass/components/progress.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/sass/components/progress.scss b/src/sass/components/progress.scss index 16992808..f28a19ca 100644 --- a/src/sass/components/progress.scss +++ b/src/sass/components/progress.scss @@ -42,13 +42,13 @@ &::-webkit-progress-bar { background: transparent; - transition: width 0.2s ease; } &::-webkit-progress-value { background: currentColor; border-radius: 100px; min-width: $plyr-range-track-height; + transition: width 0.2s ease; } // Mozilla -- cgit v1.2.3 From a0303969c2c6875790d600444f357b12bdb09b50 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 8 Dec 2018 16:50:44 +1100 Subject: Fix for error when mime type not specified (fixes #1274) --- src/js/html5.js | 13 +++++++++++-- src/js/support.js | 12 ++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/js/html5.js b/src/js/html5.js index 0876211a..3266a58e 100644 --- a/src/js/html5.js +++ b/src/js/html5.js @@ -5,6 +5,7 @@ import support from './support'; import { removeElement } from './utils/elements'; import { triggerEvent } from './utils/events'; +import is from './utils/is'; const html5 = { getSources() { @@ -14,8 +15,16 @@ const html5 = { const sources = Array.from(this.media.querySelectorAll('source')); - // Filter out unsupported sources - return sources.filter(source => support.mime.call(this, source.getAttribute('type'))); + // Filter out unsupported sources (if type is specified) + return sources.filter(source => { + const type = source.getAttribute('type'); + + if (is.empty(type)) { + return true; + } + + return support.mime.call(this, type); + }); }, // Get quality levels diff --git a/src/js/support.js b/src/js/support.js index 9257df13..81965867 100644 --- a/src/js/support.js +++ b/src/js/support.js @@ -68,9 +68,13 @@ const support = { // Check for mime type support against a player instance // Credits: http://diveintohtml5.info/everything.html // Related: http://www.leanbackplayer.com/test/h5mt.html - mime(inputType) { - const [mediaType] = inputType.split('/'); - let type = inputType; + mime(input) { + if (is.empty(input)) { + return false; + } + + const [mediaType] = input.split('/'); + let type = input; // Verify we're using HTML5 and there's no media type mismatch if (!this.isHTML5 || mediaType !== this.type) { @@ -79,7 +83,7 @@ const support = { // Add codec if required if (Object.keys(defaultCodecs).includes(type)) { - type += `; codecs="${defaultCodecs[inputType]}"`; + type += `; codecs="${defaultCodecs[input]}"`; } try { -- cgit v1.2.3 From 3fb85664d22d4bc3b6d91d72a8f9e31877960ff6 Mon Sep 17 00:00:00 2001 From: Guru Prasad Srinivasa Date: Sat, 8 Dec 2018 00:57:47 -0500 Subject: Updated restart logic to call play instead of togglePlay --- src/js/listeners.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/js/listeners.js b/src/js/listeners.js index 5c93299c..f073f5cb 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -387,7 +387,7 @@ class Listeners { if (player.ended) { this.proxy(event, player.restart, 'restart'); - this.proxy(event, player.togglePlay, 'play'); + this.proxy(event, player.play, 'play'); } else { this.proxy(event, player.togglePlay, 'play'); } -- cgit v1.2.3 From e948bfd585a7ad0472c612eeda1f8fe8bbcaa3cb Mon Sep 17 00:00:00 2001 From: James Date: Thu, 13 Dec 2018 20:39:39 +1100 Subject: Preview seek: jpeg sprites + much more - Allow jpeg sprites - much snappier and more accurate - Fixed bug: right clicking the seek bar sticks on mousedown - Fixed bug: moving the mouse really quickly results in not updating the thumb - Fixed bug: if you mousedown but don't move mouse, it shows a stale image in the scrubbing container - Fixed bug: very first image shows as 0px - Fixed bug: stretches images when video isn't same aspect as player --- src/js/plugins/previewThumbnails.js | 388 ++++++++++++++++++++------------ src/sass/plugins/previewThumbnails.scss | 18 +- 2 files changed, 250 insertions(+), 156 deletions(-) (limited to 'src') diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 1993ae96..f1fe376d 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -23,11 +23,9 @@ class PreviewThumbnails { constructor(player) { this.player = player; this.thumbnailsDefs = []; - this.showingThumb = null; // Index of the currently displayed thumbnail this.lastMousemoveEventTime = Date.now(); this.mouseDown = false; - this.imageShowCounter = 0; - this.imageTryShowCounter = 0; + this.loadedImages = []; if (this.enabled) { this.load(); @@ -43,6 +41,9 @@ class PreviewThumbnails { } load() { + // Turn off the regular seek tooltip + this.player.config.tooltips.seek = false; + this.getThumbnailsDefs() .then(() => { // Initiate DOM listeners so that our preview thumbnails can be used @@ -51,9 +52,6 @@ class PreviewThumbnails { // Build HTML DOM elements this.elements(); - // Turn off the regular seek tooltip - this.player.config.tooltips.seek = false; - // Check to see if thumb container size was specified manually in CSS this.determineContainerAutoSizing(); }); @@ -88,7 +86,7 @@ class PreviewThumbnails { } // Process individual VTT file - getThumbnailDef (url) { + getThumbnailDef(url) { return new Promise((resolve, reject) => { fetch(url) .then(response => { @@ -109,6 +107,7 @@ class PreviewThumbnails { tempImage.src = thumbnailsDef.urlPrefix + thumbnailsDef.frames[0].text; tempImage.onload = () => { thumbnailsDef.height = tempImage.naturalHeight; + thumbnailsDef.width = tempImage.naturalWidth; this.thumbnailsDefs.push(thumbnailsDef); @@ -170,14 +169,14 @@ class PreviewThumbnails { this.player.elements.progress, 'mouseleave click', () => { - this.hideThumbContainer(); + this.hideThumbContainer(true); } ); this.player.on('play', () => { - this.hideThumbContainer(); + this.hideThumbContainer(true); }); this.player.on('seeked', () => { - this.hideThumbContainer(); + this.hideThumbContainer(false); }); // Show scrubbing preview @@ -185,10 +184,19 @@ class PreviewThumbnails { this.player, this.player.elements.progress, 'mousedown touchstart', - () => { - this.mouseDown = true; - this.showScrubbingContainer(); - this.hideThumbContainer(); + event => { + // Only act on left mouse button (0) + if (event.button === 0) { + this.mouseDown = true; + // Wait until media has a duration + if (this.player.media.duration) { + this.showScrubbingContainer(); + this.hideThumbContainer(false); + + // Download and show image + this.showImageAtCurrentTime(); + } + } } ); on.call( @@ -264,134 +272,145 @@ class PreviewThumbnails { this.player.elements.display.previewScrubbingContainer = previewScrubbingContainer; } - showImageAtCurrentTime () { - if (!this.mouseDown) { + showImageAtCurrentTime() { + if (this.mouseDown) { + this.setScrubbingContainerSize(); + } else { this.showThumbContainer(); + this.setThumbContainerSizeAndPos(); } - this.setThumbContainerSizeAndPos(); - + // // TODO: move this logic to // Check when we last loaded an image - don't show more than one new one every 500ms - if (this.lastMousemoveEventTime < Date.now() - 150) { - this.lastMousemoveEventTime = Date.now(); + // if (this.lastMousemoveEventTime < Date.now() - 150) { + // this.lastMousemoveEventTime = Date.now(); - // Find the first thumbnail that's after `time`. Note `this.seekTime+1` - we're actually looking 1 second ahead, because it's more likely then that the viewer will actually get to see the preview frame in the actual video. This hack should be removed if we ever choose to make it seek to the nearest thumb time - const thumbNum = this.thumbnailsDefs[0].frames.findIndex(frame => this.seekTime+1 >= frame.startTime && this.seekTime <= frame.endTime); + // Find the desired thumbnail index + const thumbNum = this.thumbnailsDefs[0].frames.findIndex(frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime); + let qualityIndex = 0; - // Only show if the thumbnail to show is different to last time + // Check to see if we've already downloaded higher quality versions of this image + for (let i = 1; i < this.thumbnailsDefs.length; i++) { + if (this.loadedImages.includes(this.thumbnailsDefs[i].frames[thumbNum].text)) { + qualityIndex = i; + } + } + + // Only proceed if either thumbnum or thumbfilename has changed if (thumbNum !== this.showingThumb) { this.showingThumb = thumbNum; - this.showImage(); + this.loadImage(qualityIndex); } - } else { - // Set a timeout so that we always fire this function once after the mouse stops moving. If not for this, the mouse preview would often be a bit stale - if (!this.mousemoveEventTimeout) { - this.mousemoveEventTimeout = setTimeout(() => { - // Don't follow through after the timeout if it's since been hidden - if (this.player.elements.display.previewThumbnailContainer.style.opacity === 1) { - this.showImageAtCurrentTime(); - this.mousemoveEventTimeout = null; - } - }, 200) - } - } + + // } else { + // // Set a timeout so that we always fire this function once after the mouse stops moving. If not for this, the mouse preview would often be a bit stale + // if (this.mousemoveEventTimeout) { + // clearTimeout(this.mousemoveEventTimeout); + // } + // this.mousemoveEventTimeout = setTimeout(() => { + // // Don't follow through after the timeout if it's since been hidden + // if (this.player.elements.display.previewThumbnailContainer.style.opacity === '1') { + // console.log('show on timer') + // this.showImageAtCurrentTime(true); + // this.mousemoveEventTimeout = null; + // } + // }, 200) + // } } // Show the image that's currently specified in this.showingThumb - showImage (qualityIndex = 0) { - this.imageTryShowCounter += 1; - const localImageTryShowCounter = this.imageTryShowCounter; + loadImage(qualityIndex = 0) { let thumbNum = this.showingThumb; - if (thumbNum === this.thumbnailsDefs[qualityIndex].frames.length) { - // It can attempt to preview up to 5 seconds out past the end of the video. So we'll just show the last frame - thumbNum -= 1; - this.showingThumb = thumbNum; - } - this.player.debug.log(`Preview thumbnails: showing thumbnum: ${thumbNum}: ${JSON.stringify(this.thumbnailsDefs[qualityIndex].frames[thumbNum])}`); + const frame = this.thumbnailsDefs[qualityIndex].frames[thumbNum]; const thumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; const urlPrefix = this.thumbnailsDefs[qualityIndex].urlPrefix; const thumbURL = urlPrefix + thumbFilename; - // 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(); - previewImage.src = thumbURL; - previewImage.setAttribute('data-thumbnum', thumbNum); - - previewImage.onload = () => { - // Many images are loaded within milliseconds of each other. An earlier one might be the last one to finish loading. Make sure we don't show an images out of order - if (localImageTryShowCounter >= this.imageShowCounter) { - this.imageShowCounter = localImageTryShowCounter; - - this.currentContainer.appendChild(previewImage); - - // Now that this one is showing, start pre-loading a batch of nearby images. But only if this isn't a revisit - // this.preloadNearbyImages(thumbNum); - this.thumbnailsDefs[qualityIndex].frames[thumbNum].loaded = true - - this.removeOldImages(); - - // Look for a higher quality version of the same frame - if (qualityIndex < this.thumbnailsDefs.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 - let previewContainerHeight = this.player.elements.display.previewThumbnailContainer.clientHeight; - if (this.mouseDown) previewContainerHeight = this.player.elements.display.previewScrubbingContainer.clientHeight; - // Adjust for HiDPI screen - if (window.devicePixelRatio) previewContainerHeight *= window.devicePixelRatio; - - if (previewImage.naturalHeight < previewContainerHeight) { - // Recurse this function - show a higher quality one, but only if the viewer is on this frame for a while - setTimeout(() => { - // Make sure the mouse hasn't already moved on and started hovering at another frame - if (this.showingThumb === thumbNum) { - this.showImage(qualityIndex + 1); - } - }, 150) - } - } - } + // console.log('loading: ' + thumbFilename + '. num: ' + thumbNum + '. qual: ' + qualityIndex); + + 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 + // 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 + if (this.loadingImage && this.usingJpegSprites) 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 + const previewImage = new Image(); + previewImage.src = thumbURL; + previewImage.setAttribute('data-thumbnum', thumbNum); + previewImage.setAttribute('data-thumbfilename', thumbFilename); + // this.showingThumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; + 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... + previewImage.onload = () => this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true); + this.loadingImage = previewImage; + this.removeOldImages(previewImage); + } else { + // Update the existing image + this.showImage(this.currentImageElement, frame, qualityIndex, thumbNum, thumbFilename, false); + this.currentImageElement.setAttribute('data-thumbnum', thumbNum); + this.removeOldImages(this.currentImageElement); } } - // Not using this -- Preloading looked like maybe a good idea, but it seems to actually cause more trouble than it solves. Slow connections get really backed up. Fast connections don't really need it - // If we were to try using this again, we might need to look at not starting a second preload while another is still going? - preloadNearbyImages(thumbNum, amountToPreload=30) { - const actualShowingThumb = [...this.currentContainer.children].reverse()[0].getAttribute('data-thumbnum'); - if (actualShowingThumb && Number(actualShowingThumb) === this.showingThumb) { - let startNum = thumbNum - amountToPreload/2; - let endNum = thumbNum + amountToPreload/2; - if (startNum < 0) startNum = 0; - if (endNum > this.thumbnailsDefs[0].frames.length - 1) endNum = this.thumbnailsDefs[0].frames.length - 1; - - for (let i = startNum; i <= endNum; i++) { - if (!this.thumbnailsDefs[0].frames[i].loaded) { - this.player.debug.log('Thumbnail previews: preloading: ' + i); - - const thumbFilename = this.thumbnailsDefs[0].frames[i].text; - const urlPrefix = this.thumbnailsDefs[0].urlPrefix; - const thumbURL = urlPrefix + thumbFilename; - - // 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(); - previewImage.src = thumbURL; - - // Set loaded attribute. This will prevent us from wasting CPU constantly trying to preload images that we already have loaded - this.thumbnailsDefs[0].frames[i].loaded = true; - } - } + showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, newImage = true) { + // console.log('newimage: ' + newImage) + console.log('showing: ' + thumbFilename + '. num: ' + thumbNum + '. qual: ' + qualityIndex + '. newimg: ' + newImage); + this.setImageSizeAndOffset(previewImage, frame); + + if (newImage) { + this.currentContainer.appendChild(previewImage); + + this.currentImageElement = previewImage; + // this.removeOldImages(previewImage); + + if (!this.loadedImages.includes(thumbFilename)) this.loadedImages.push(thumbFilename); + } + + // Look for a higher quality version of the same frame + if (qualityIndex < this.thumbnailsDefs.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 + // let previewContainerHeight = this.player.elements.display.previewThumbnailContainer.clientHeight; + // if (this.mouseDown) previewContainerHeight = this.player.elements.display.previewScrubbingContainer.clientHeight; + // // Adjust for HiDPI screen + // if (window.devicePixelRatio) previewContainerHeight *= window.devicePixelRatio; + + // if (previewImage.naturalHeight < previewContainerHeight) { + // Recurse this function - show a higher quality one, but only if the viewer is on this frame for a while + setTimeout(() => { + // Make sure the mouse hasn't already moved on and started hovering at another frame + // TODO: need to use filename instead of thumbnum, but need to use latest thumbnum instead of old thumbnum + // if (this.showingThumb === thumbNum) { + console.log(`${this.showingThumbFilename} ${thumbFilename}`) + if (this.showingThumbFilename === thumbFilename) { + // console.log('showing higher qual') + this.loadImage(qualityIndex + 1); + } + }, 500) + // } } } - removeOldImages() { + removeOldImages(currentImage) { // Get a list of all images, and reverse it - so that we can start from the end and delete all except for the most recent - const allImages = [...this.currentContainer.children].reverse(); - - // Start at the third image image - so we leave the last two images. Leaving only one might result in flickering if the newest one hasn't finished rendering yet - for (let i = 2; i < allImages.length; i++) { - if (allImages[i].tagName === 'IMG') { - this.currentContainer.removeChild(allImages[i]); + const allImages = [...this.currentContainer.children]; + + for (let image of allImages) { + if (image.tagName === 'IMG') { + const removeDelay = this.usingJpegSprites ? 200 : 1000; + + 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 + // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function + image.setAttribute('data-deleting', 'true'); + setTimeout(() => { + this.currentContainer.removeChild(image); + // console.log('removing: ' + image.getAttribute('data-thumbfilename')); + }, removeDelay) + } } } } @@ -404,11 +423,58 @@ class PreviewThumbnails { } } + get usingJpegSprites() { + if (this.thumbnailsDefs[0].frames[0].w) { + return true; + } else { + return false; + } + } + + get thumbAspectRatio() { + if (this.usingJpegSprites) { + return this.thumbnailsDefs[0].frames[0].w / this.thumbnailsDefs[0].frames[0].h; + } else { + return this.thumbnailsDefs[0].width / this.thumbnailsDefs[0].height; + } + } + + get thumbContainerHeight() { + if (this.mouseDown) { + // return this.player.elements.container.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 + } else { + // return this.player.elements.container.clientHeight / 4; + return this.player.media.clientWidth / this.thumbAspectRatio / 4; + } + } + + get currentImageElement() { + if (this.mouseDown) { + return this.currentScrubbingImageElement; + } else { + return this.currentThumbnailImageElement; + } + } + set currentImageElement(element) { + if (this.mouseDown) { + this.currentScrubbingImageElement = element; + } else { + this.currentThumbnailImageElement = element; + } + } + showThumbContainer() { this.player.elements.display.previewThumbnailContainer.style.opacity = 1; } - hideThumbContainer() { + hideThumbContainer(clearShowing = false) { this.player.elements.display.previewThumbnailContainer.style.opacity = 0; + + if (clearShowing) { + this.showingThumb = null; + this.showingThumbFilename = null; + } } showScrubbingContainer() { @@ -416,6 +482,8 @@ class PreviewThumbnails { } hideScrubbingContainer() { this.player.elements.display.previewScrubbingContainer.style.opacity = 0; + this.showingThumb = null; + this.showingThumbFilename = null; } determineContainerAutoSizing() { @@ -426,12 +494,9 @@ class PreviewThumbnails { // 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 setThumbContainerSizeAndPos() { - // if (this.player.config.previewThumbnails.autoSize) { if (!this.sizeSpecifiedInCSS) { - const videoAspectRatio = this.player.media.videoWidth / this.player.media.videoHeight; - const thumbHeight = this.player.elements.container.clientHeight / 4; - const thumbWidth = thumbHeight * videoAspectRatio; - this.player.elements.display.previewThumbnailContainer.style.height = `${thumbHeight}px`; + const thumbWidth = this.thumbContainerHeight * this.thumbAspectRatio; + this.player.elements.display.previewThumbnailContainer.style.height = `${this.thumbContainerHeight}px`; this.player.elements.display.previewThumbnailContainer.style.width = `${thumbWidth}px`; } @@ -458,41 +523,68 @@ class PreviewThumbnails { previewContainer.style.left = previewPos + 'px'; } + // Can't use 100% width, in case the video is a different aspect ratio to the video container + setScrubbingContainerSize() { + 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 + } + + // Jpeg sprites need to be offset to the correct location + setImageSizeAndOffset(previewImage, frame) { + if (this.usingJpegSprites) { + // Find difference between jpeg height and preview container height + const heightMulti = this.thumbContainerHeight / frame.h; + + previewImage.style.height = `${previewImage.naturalHeight * heightMulti}px`; + previewImage.style.width = `${previewImage.naturalWidth * heightMulti}px`; + previewImage.style.left = `-${Math.ceil(frame.x * heightMulti)}px`; + 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/) + 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 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 + 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) { - result.text = line.trim() - } else { - result.text += '\n' + line.trim() - } + 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) + if (result.text) { + processedList.push(result); + } } - } - return processedList + return processedList; } } diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss index 92702e39..044048eb 100644 --- a/src/sass/plugins/previewThumbnails.scss +++ b/src/sass/plugins/previewThumbnails.scss @@ -18,15 +18,16 @@ white-space: nowrap; z-index: 2; + overflow: hidden; + img { position: absolute; left: 0px; - right: 0px; top: 0px; - bottom: 0px; - margin: auto; - height: 100%; + height: 100%; // Non-jpeg-sprite images are 100%. Jpeg sprites will have their size applied by javascript width: 100%; + max-height: none; + max-width: none; border-radius: 0px; } @@ -52,19 +53,20 @@ bottom: 0px; height: 100%; width: 100%; + margin: auto; // Required when video is different dimensions to container (e.g., fullscreen) + overflow: hidden; z-index: 1; - transition: opacity 0.3s 0.3s ease; + transition: opacity 0.3s ease; filter: blur(1px); img { position: absolute; left: 0px; - right: 0px; top: 0px; - bottom: 0px; - margin: auto; height: 100%; width: 100%; + max-height: none; + max-width: none; object-fit: contain; } } -- cgit v1.2.3 From 11618353ea0f35138d147261ed0fcc76e4e042a1 Mon Sep 17 00:00:00 2001 From: Omar Khatib Date: Thu, 13 Dec 2018 17:30:10 +0100 Subject: support Youtube noCookie Mode --- src/js/config/defaults.js | 2 ++ src/js/config/types.js | 2 +- src/js/plugins/youtube.js | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index c3f97eee..dc2e469d 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -395,6 +395,8 @@ const defaults = { enabled: false, publisherId: '', }, + + noCookie: false, }; export default defaults; diff --git a/src/js/config/types.js b/src/js/config/types.js index c9d50937..e0ccdaff 100644 --- a/src/js/config/types.js +++ b/src/js/config/types.js @@ -19,7 +19,7 @@ export const types = { */ export function getProviderByUrl(url) { // YouTube - if (/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/.test(url)) { + if (/^(https?:\/\/)?(www\.)?(youtube\.com|youtube-nocookie\.com|youtu\.?be)\/.+$/.test(url)) { return providers.youtube; } diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 73175c14..7d488814 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -153,10 +153,12 @@ const youtube = { // https://developers.google.com/youtube/iframe_api_reference player.embed = new window.YT.Player(id, { videoId, + host: player.config.noCookie ? 'https://www.youtube-nocookie.com' : undefined, // Only show controls if not fully supported playerVars: { autoplay: player.config.autoplay ? 1 : 0, // Autoplay hl: player.config.hl, // iframe interface language controls: player.supported.ui ? 0 : 1, // Only show controls if not fully supported + rel: 0, // No related vids showinfo: 0, // Hide info iv_load_policy: 3, // Hide annotations -- cgit v1.2.3 From 88ffd0f138b3db1e16dd689d9070dcd8fed4a845 Mon Sep 17 00:00:00 2001 From: Omar Khatib Date: Thu, 13 Dec 2018 17:31:15 +0100 Subject: remove comment --- src/js/plugins/youtube.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 7d488814..ea52df00 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -153,7 +153,7 @@ const youtube = { // https://developers.google.com/youtube/iframe_api_reference player.embed = new window.YT.Player(id, { videoId, - host: player.config.noCookie ? 'https://www.youtube-nocookie.com' : undefined, // Only show controls if not fully supported + host: player.config.noCookie ? 'https://www.youtube-nocookie.com' : undefined, playerVars: { autoplay: player.config.autoplay ? 1 : 0, // Autoplay hl: player.config.hl, // iframe interface language -- cgit v1.2.3 From 279f0519053143c43f84d1b0e3511593d26533ae Mon Sep 17 00:00:00 2001 From: James Date: Fri, 14 Dec 2018 12:50:29 +1100 Subject: Preview seek: image preloading + tweaks/fixes - Preloads neighbouring images after showing current image - Re-fixed bug: if you mousedown but don't move mouse, it shows a stale image in the scrubbing container - Fixed bug: mobile device correctly detect touch --- src/js/plugins/previewThumbnails.js | 170 +++++++++++++++++++++--------------- 1 file changed, 100 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index f1fe376d..71fdf0c7 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -185,13 +185,13 @@ class PreviewThumbnails { this.player.elements.progress, 'mousedown touchstart', event => { - // Only act on left mouse button (0) - if (event.button === 0) { + // Only act on left mouse button (0), or touch device (!event.button) + if (!event.button || event.button === 0) { this.mouseDown = true; // Wait until media has a duration if (this.player.media.duration) { this.showScrubbingContainer(); - this.hideThumbContainer(false); + this.hideThumbContainer(true); // Download and show image this.showImageAtCurrentTime(); @@ -280,57 +280,33 @@ class PreviewThumbnails { this.setThumbContainerSizeAndPos(); } - // // TODO: move this logic to - // Check when we last loaded an image - don't show more than one new one every 500ms - // if (this.lastMousemoveEventTime < Date.now() - 150) { - // this.lastMousemoveEventTime = Date.now(); + // Find the desired thumbnail index + const thumbNum = this.thumbnailsDefs[0].frames.findIndex(frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime); + let qualityIndex = 0; - // Find the desired thumbnail index - const thumbNum = this.thumbnailsDefs[0].frames.findIndex(frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime); - let qualityIndex = 0; - - // Check to see if we've already downloaded higher quality versions of this image - for (let i = 1; i < this.thumbnailsDefs.length; i++) { - if (this.loadedImages.includes(this.thumbnailsDefs[i].frames[thumbNum].text)) { - qualityIndex = i; - } - } - - // Only proceed if either thumbnum or thumbfilename has changed - if (thumbNum !== this.showingThumb) { - this.showingThumb = thumbNum; - this.loadImage(qualityIndex); + // Check to see if we've already downloaded higher quality versions of this image + for (let i = 1; i < this.thumbnailsDefs.length; i++) { + if (this.loadedImages.includes(this.thumbnailsDefs[i].frames[thumbNum].text)) { + qualityIndex = i; } + } - // } else { - // // Set a timeout so that we always fire this function once after the mouse stops moving. If not for this, the mouse preview would often be a bit stale - // if (this.mousemoveEventTimeout) { - // clearTimeout(this.mousemoveEventTimeout); - // } - // this.mousemoveEventTimeout = setTimeout(() => { - // // Don't follow through after the timeout if it's since been hidden - // if (this.player.elements.display.previewThumbnailContainer.style.opacity === '1') { - // console.log('show on timer') - // this.showImageAtCurrentTime(true); - // this.mousemoveEventTimeout = null; - // } - // }, 200) - // } + // Only proceed if either thumbnum or thumbfilename has changed + if (thumbNum !== this.showingThumb) { + this.showingThumb = thumbNum; + this.loadImage(qualityIndex); + } } // Show the image that's currently specified in this.showingThumb loadImage(qualityIndex = 0) { let thumbNum = this.showingThumb; - this.player.debug.log(`Preview thumbnails: showing thumbnum: ${thumbNum}: ${JSON.stringify(this.thumbnailsDefs[qualityIndex].frames[thumbNum])}`); - const frame = this.thumbnailsDefs[qualityIndex].frames[thumbNum]; const thumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; const urlPrefix = this.thumbnailsDefs[qualityIndex].urlPrefix; const thumbURL = urlPrefix + thumbFilename; - // console.log('loading: ' + thumbFilename + '. num: ' + thumbNum + '. qual: ' + qualityIndex); - 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 // 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 @@ -341,7 +317,6 @@ class PreviewThumbnails { previewImage.src = thumbURL; previewImage.setAttribute('data-thumbnum', thumbNum); previewImage.setAttribute('data-thumbfilename', thumbFilename); - // this.showingThumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; 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... @@ -357,41 +332,22 @@ class PreviewThumbnails { } showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, newImage = true) { - // console.log('newimage: ' + newImage) - console.log('showing: ' + thumbFilename + '. num: ' + thumbNum + '. qual: ' + qualityIndex + '. newimg: ' + newImage); + this.player.debug.log('Showing thumb: ' + thumbFilename + '. num: ' + thumbNum + '. qual: ' + qualityIndex + '. newimg: ' + newImage); this.setImageSizeAndOffset(previewImage, frame); if (newImage) { this.currentContainer.appendChild(previewImage); - this.currentImageElement = previewImage; - // this.removeOldImages(previewImage); if (!this.loadedImages.includes(thumbFilename)) this.loadedImages.push(thumbFilename); } - // Look for a higher quality version of the same frame - if (qualityIndex < this.thumbnailsDefs.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 - // let previewContainerHeight = this.player.elements.display.previewThumbnailContainer.clientHeight; - // if (this.mouseDown) previewContainerHeight = this.player.elements.display.previewScrubbingContainer.clientHeight; - // // Adjust for HiDPI screen - // if (window.devicePixelRatio) previewContainerHeight *= window.devicePixelRatio; - - // if (previewImage.naturalHeight < previewContainerHeight) { - // Recurse this function - show a higher quality one, but only if the viewer is on this frame for a while - setTimeout(() => { - // Make sure the mouse hasn't already moved on and started hovering at another frame - // TODO: need to use filename instead of thumbnum, but need to use latest thumbnum instead of old thumbnum - // if (this.showingThumb === thumbNum) { - console.log(`${this.showingThumbFilename} ${thumbFilename}`) - if (this.showingThumbFilename === thumbFilename) { - // console.log('showing higher qual') - this.loadImage(qualityIndex + 1); - } - }, 500) - // } - } + // Preload images before and after the current one + // Show higher quality of the same frame + // 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 + this.preloadNearby(thumbNum, true) + .then(this.preloadNearby(thumbNum, false)) + .then(this.getHigherQuality(qualityIndex, previewImage, frame, thumbFilename)); } removeOldImages(currentImage) { @@ -400,21 +356,95 @@ class PreviewThumbnails { for (let image of allImages) { if (image.tagName === 'IMG') { - const removeDelay = this.usingJpegSprites ? 200 : 1000; + const removeDelay = this.usingJpegSprites ? 500 : 1000; 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 // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function 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 + setTimeout(() => { - this.currentContainer.removeChild(image); - // console.log('removing: ' + image.getAttribute('data-thumbfilename')); + currentContainer.removeChild(image); + this.player.debug.log('Removing thumb: ' + image.getAttribute('data-thumbfilename')); }, removeDelay) } } } } + // 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 + preloadNearby(thumbNum, forward = true) { + return new Promise((resolve, reject) => { + setTimeout(() => { + const oldThumbFilename = this.thumbnailsDefs[0].frames[thumbNum].text; + + 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 + let thumbnailsDefsCopy + if (forward) { + thumbnailsDefsCopy = this.thumbnailsDefs[0].frames.slice(thumbNum); + } else { + thumbnailsDefsCopy = this.thumbnailsDefs[0].frames.slice(0, thumbNum).reverse(); + } + + let foundOne = false; + + for (const frame of thumbnailsDefsCopy) { + const newThumbFilename = frame.text; + + if (newThumbFilename !== oldThumbFilename) { + // Found one with a different filename. Make sure it hasn't already been loaded on this page visit + if (!this.loadedImages.includes(newThumbFilename)) { + foundOne = true; + this.player.debug.log('Preloading thumb filename: ' + newThumbFilename); + + const urlPrefix = this.thumbnailsDefs[0].urlPrefix; + const thumbURL = urlPrefix + newThumbFilename; + + const previewImage = new Image(); + previewImage.src = thumbURL; + previewImage.onload = () => { + this.player.debug.log('Preloaded thumb filename: ' + newThumbFilename); + if (!this.loadedImages.includes(newThumbFilename)) this.loadedImages.push(newThumbFilename); + + // We don't resolve until the thumb is loaded + resolve() + }; + } + + break; + } + } + + // If there are none to preload then we want to resolve immediately + if (!foundOne) resolve(); + } + }, 300) + }) + } + + // If user has been hovering current image for half a second, look for a higher quality one + getHigherQuality(currentQualityIndex, previewImage, frame, thumbFilename) { + if (currentQualityIndex < this.thumbnailsDefs.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 + let previewImageHeight = previewImage.naturalHeight; + if (this.usingJpegSprites) previewImageHeight = frame.h; + + 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 + setTimeout(() => { + // Make sure the mouse hasn't already moved on and started hovering at another image + if (this.showingThumbFilename === thumbFilename) { + this.player.debug.log('Showing higher quality thumb for: ' + thumbFilename) + this.loadImage(currentQualityIndex + 1); + } + }, 300) + } + } + } + get currentContainer() { if (this.mouseDown) { return this.player.elements.display.previewScrubbingContainer; -- cgit v1.2.3 From d97257a5a93707fbbdc150226c9fbee8feb8aedb Mon Sep 17 00:00:00 2001 From: James Date: Sat, 15 Dec 2018 11:32:50 +1100 Subject: Preview seek: Edge+IE11 fixes - Fixed bug: Edge seek errors: Replaced array spread with Array.from() - Fixed IE11 bug: seek time was offset to the left. Required an extra container div to facilitate this --- src/js/config/defaults.js | 7 +++++-- src/js/plugins/previewThumbnails.js | 21 ++++++++++++++++----- src/sass/plugins/previewThumbnails.scss | 19 ++++++++++++------- 3 files changed, 33 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 70f12a80..6ac21f23 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -342,8 +342,6 @@ const defaults = { loading: 'plyr--loading', hover: 'plyr--hover', tooltip: 'plyr__tooltip', - previewThumbnailContainer: 'plyr__preview-thumbnail-container', - previewScrubbingContainer: 'plyr__preview-scrubbing-container', cues: 'plyr__cues', hidden: 'plyr__sr-only', hideControls: 'plyr--hide-controls', @@ -376,6 +374,11 @@ const defaults = { active: 'plyr--airplay-active', }, tabFocus: 'plyr__tab-focus', + previewThumbnails: { + thumbnailContainer: 'plyr__preview-thumbnail-container', + scrubbingContainer: 'plyr__preview-scrubbing-container', + timeTextContainer: 'plyr__preview-time-text-container', + }, }, // Embed attributes diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 71fdf0c7..7ae077e6 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -244,27 +244,37 @@ class PreviewThumbnails { const previewThumbnailContainer = createElement( 'div', { - class: this.player.config.classNames.previewThumbnailContainer, + class: this.player.config.classNames.previewThumbnails.thumbnailContainer, }, ); this.player.elements.progress.appendChild(previewThumbnailContainer); this.player.elements.display.previewThumbnailContainer = previewThumbnailContainer; + // Create HTML element, parent+span: time text (e.g., 01:32:00) + const timeTextContainer = createElement( + 'div', + { + class: this.player.config.classNames.previewThumbnails.timeTextContainer + }, + ); + + this.player.elements.display.previewThumbnailContainer.appendChild(timeTextContainer); + const timeText = createElement( 'span', {}, '00:00', ); - this.player.elements.display.previewThumbnailContainer.appendChild(timeText); + timeTextContainer.appendChild(timeText); this.player.elements.display.previewThumbnailTimeText = timeText; // Create HTML element: plyr__preview-scrubbing-container const previewScrubbingContainer = createElement( 'div', { - class: this.player.config.classNames.previewScrubbingContainer, + class: this.player.config.classNames.previewThumbnails.scrubbingContainer, }, ); @@ -350,9 +360,10 @@ class PreviewThumbnails { .then(this.getHigherQuality(qualityIndex, previewImage, frame, thumbFilename)); } + // Remove all preview images that aren't the designated current image removeOldImages(currentImage) { - // Get a list of all images, and reverse it - so that we can start from the end and delete all except for the most recent - const allImages = [...this.currentContainer.children]; + // Get a list of all images, convert it from a DOM list to an array + const allImages = Array.from(this.currentContainer.children); for (let image of allImages) { if (image.tagName === 'IMG') { diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss index 044048eb..4de90667 100644 --- a/src/sass/plugins/previewThumbnails.scss +++ b/src/sass/plugins/previewThumbnails.scss @@ -32,16 +32,21 @@ } // Seek time text - span { + .plyr__preview-time-text-container { position: absolute; bottom: 0px; + left: 0px; + right: 0px; + margin-bottom: 2px; z-index: 3; - transform: translate(-50%,0); - background-color: rgba(0,0,0,0.55); - color: rgba(255,255,255,1); - padding: 4px 6px 3px 6px; - font-size: $plyr-font-size-small; - font-weight: $plyr-font-weight-regular; + + span { + background-color: rgba(0,0,0,0.55); + color: rgba(255,255,255,1); + padding: 4px 6px 3px 6px; + font-size: $plyr-font-size-small; + font-weight: $plyr-font-weight-regular; + } } } -- cgit v1.2.3 From 6d9d315ca741f5472902c0f961591fdff9bbf946 Mon Sep 17 00:00:00 2001 From: Jimmy Jia Date: Thu, 20 Dec 2018 13:05:14 -0500 Subject: fix: Use Math.trunc instead of parseInt --- src/js/utils/time.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/utils/time.js b/src/js/utils/time.js index 7c9860fd..2deccf65 100644 --- a/src/js/utils/time.js +++ b/src/js/utils/time.js @@ -5,9 +5,9 @@ import is from './is'; // Time helpers -export const getHours = value => parseInt((value / 60 / 60) % 60, 10); -export const getMinutes = value => parseInt((value / 60) % 60, 10); -export const getSeconds = value => parseInt(value % 60, 10); +export const getHours = value => Math.trunc((value / 60 / 60) % 60, 10); +export const getMinutes = value => Math.trunc((value / 60) % 60, 10); +export const getSeconds = value => Math.trunc(value % 60, 10); // Format time to UI friendly string export function formatTime(time = 0, displayHours = false, inverted = false) { -- cgit v1.2.3 From 1c79ce70c9a3eb46c8a8a319aee840c65a5571cd Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 6 Jan 2019 14:09:49 +1100 Subject: Update youtube.js --- src/js/plugins/youtube.js | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index ea52df00..0cc8fd1d 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -158,7 +158,6 @@ const youtube = { autoplay: player.config.autoplay ? 1 : 0, // Autoplay hl: player.config.hl, // iframe interface language controls: player.supported.ui ? 0 : 1, // Only show controls if not fully supported - rel: 0, // No related vids showinfo: 0, // Hide info iv_load_policy: 3, // Hide annotations -- cgit v1.2.3 From 7dd7c13065212ffed67f6f2ac34d4a96c565ef60 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 8 Jan 2019 23:34:28 +1100 Subject: Linting etc --- src/js/plugins/previewThumbnails.js | 558 +++++++++++++++----------------- src/sass/plugins/previewThumbnails.scss | 59 ++-- 2 files changed, 292 insertions(+), 325 deletions(-) (limited to 'src') diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 7ae077e6..fd1733d2 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -1,7 +1,56 @@ -import { formatTime } from '../utils/time'; -import { on, once, toggleListener, triggerEvent } from '../utils/events'; -import { createElement, emptyElement, getAttributesFromSelector, getElement, getElements, hasClass, matches, removeElement, setAttributes, setFocus, toggleClass, toggleHidden } from '../utils/elements'; +import { createElement } from '../utils/elements'; +import { on, once } from '../utils/events'; 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 @@ -11,7 +60,7 @@ import fetch from '../utils/fetch'; * 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 * - 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 { @@ -22,7 +71,7 @@ class PreviewThumbnails { */ constructor(player) { this.player = player; - this.thumbnailsDefs = []; + this.thumbnails = []; this.lastMousemoveEventTime = Date.now(); this.mouseDown = false; this.loadedImages = []; @@ -33,88 +82,80 @@ class PreviewThumbnails { } get enabled() { - return ( - this.player.isHTML5 && - this.player.isVideo && - this.player.config.previewThumbnails.enabled - ); + return this.player.isHTML5 && this.player.isVideo && this.player.config.previewThumbnails.enabled; } load() { // Turn off the regular seek tooltip this.player.config.tooltips.seek = false; - this.getThumbnailsDefs() - .then(() => { - // Initiate DOM listeners so that our preview thumbnails can be used - this.listeners(); + this.getThumbnails().then(() => { + // Initiate DOM listeners so that our preview thumbnails can be used + this.listeners(); - // Build HTML DOM elements - this.elements(); + // Build HTML DOM elements + this.elements(); - // Check to see if thumb container size was specified manually in CSS - this.determineContainerAutoSizing(); - }); + // Check to see if thumb container size was specified manually in CSS + this.determineContainerAutoSizing(); + }); } // Download VTT files and parse them - getThumbnailsDefs() { - return new Promise((resolve, reject) => { + getThumbnails() { + return new Promise(resolve => { if (!this.player.config.previewThumbnails.src) { throw new Error('Missing previewThumbnails.src config attribute'); } // previewThumbnails.src can be string or list. If string, convert into single-element list - const configSrc = this.player.config.previewThumbnails.src - const urls = typeof configSrc === 'string' ? [configSrc] : configSrc - const promises = []; + const { src } = this.player.config.previewThumbnails; + const urls = is.string(src) ? [src] : src; - // Loop through each src url. Download and process the VTT file, storing the resulting data in this.thumbnailsDefs - for (const url of urls) { - promises.push(this.getThumbnailDef(url)); - } + // Loop through each src url. Download and process the VTT file, storing the resulting data in this.thumbnails + const promises = urls.map(u => this.getThumbnail(u)); - Promise.all(promises) - .then(() => { - // Sort smallest to biggest (e.g., [120p, 480p, 1080p]) - this.thumbnailsDefs.sort((x, y) => x.height - y.height) - this.player.debug.log('Preview thumbnails: thumbnailsDefs: ' + JSON.stringify(this.thumbnailsDefs, null, 4)) + Promise.all(promises).then(() => { + // Sort smallest to biggest (e.g., [120p, 480p, 1080p]) + this.thumbnails.sort((x, y) => x.height - y.height); + this.player.debug.log(`Preview thumbnails: thumbnails: ${JSON.stringify(this.thumbnails, null, 4)}`); - resolve() - }); - }) + resolve(); + }); + }); } // Process individual VTT file - getThumbnailDef(url) { - return new Promise((resolve, reject) => { - fetch(url) - .then(response => { - const thumbnailsDef = { - frames: this.parseVtt(response), - height: null, - 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 do start with '/', then they obviously don't need a prefix, so it will remain blank - if (!thumbnailsDef.frames[0].text.startsWith('/')) { - thumbnailsDef.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1); - } + getThumbnail(url) { + return new Promise(resolve => { + fetch(url).then(response => { + const thumbnail = { + frames: parseVtt(response), + height: null, + 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 do start with '/', then they obviously don't need a prefix, so it will remain blank + if (!thumbnail.frames[0].text.startsWith('/')) { + thumbnail.urlPrefix = url.substring(0, url.lastIndexOf('/') + 1); + } - // Download the first frame, so that we can determine/set the height of this thumbnailsDef - const tempImage = new Image(); - tempImage.src = thumbnailsDef.urlPrefix + thumbnailsDef.frames[0].text; - tempImage.onload = () => { - thumbnailsDef.height = tempImage.naturalHeight; - thumbnailsDef.width = tempImage.naturalWidth; + // Download the first frame, so that we can determine/set the height of this thumbnailsDef + const tempImage = new Image(); - 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() { // Mouse hover over seek bar - on.call( - this.player, - this.player.elements.progress, - 'mousemove', - event => { - // Wait until media has a duration - if (this.player.media.duration) { - // 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 - this.player.elements.display.previewThumbnailTimeText.innerText = formatTime(this.seekTime); + on.call(this.player, this.player.elements.progress, 'mousemove', event => { + // Wait until media has a duration + if (this.player.media.duration) { + // 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) { + // 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 - on.call( - this.player, - this.player.elements.progress, - 'touchmove', - event => { - // 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 - this.showImageAtCurrentTime(); - } + on.call(this.player, this.player.elements.progress, 'touchmove', () => { + // 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 + this.showImageAtCurrentTime(); } - ); + }); // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering - on.call( - this.player, - this.player.elements.progress, - 'mouseleave click', - () => { - this.hideThumbContainer(true); - } - ); + on.call(this.player, this.player.elements.progress, 'mouseleave click', () => { + this.hideThumbContainer(true); + }); this.player.on('play', () => { this.hideThumbContainer(true); }); @@ -180,60 +215,40 @@ class PreviewThumbnails { }); // Show scrubbing preview - on.call( - this.player, - this.player.elements.progress, - 'mousedown touchstart', - event => { - // Only act on left mouse button (0), or touch device (!event.button) - if (!event.button || event.button === 0) { - this.mouseDown = true; - // Wait until media has a duration - if (this.player.media.duration) { - this.showScrubbingContainer(); - this.hideThumbContainer(true); - - // Download and show image - this.showImageAtCurrentTime(); - } + on.call(this.player, this.player.elements.progress, 'mousedown touchstart', event => { + // Only act on left mouse button (0), or touch device (!event.button) + if (!event.button || event.button === 0) { + this.mouseDown = true; + // Wait until media has a duration + if (this.player.media.duration) { + this.showScrubbingContainer(); + this.hideThumbContainer(true); + + // Download and show image + 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() { // Create HTML element: plyr__preview-thumbnail-container - const previewThumbnailContainer = createElement( - 'div', - { - class: this.player.config.classNames.previewThumbnails.thumbnailContainer, - }, - ); + const previewThumbnailContainer = createElement('div', { + class: this.player.config.classNames.previewThumbnails.thumbnailContainer, + }); this.player.elements.progress.appendChild(previewThumbnailContainer); this.player.elements.display.previewThumbnailContainer = previewThumbnailContainer; // Create HTML element, parent+span: time text (e.g., 01:32:00) - const timeTextContainer = createElement( - 'div', - { - class: this.player.config.classNames.previewThumbnails.timeTextContainer - }, - ); + const timeTextContainer = createElement('div', { + class: this.player.config.classNames.previewThumbnails.timeTextContainer, + }); this.player.elements.display.previewThumbnailContainer.appendChild(timeTextContainer); - const timeText = createElement( - 'span', - {}, - '00:00', - ); + const timeText = createElement('span', {}, '00:00'); timeTextContainer.appendChild(timeText); this.player.elements.display.previewThumbnailTimeText = timeText; // Create HTML element: plyr__preview-scrubbing-container - const previewScrubbingContainer = createElement( - 'div', - { - class: this.player.config.classNames.previewThumbnails.scrubbingContainer, - }, - ); + const previewScrubbingContainer = createElement('div', { + class: this.player.config.classNames.previewThumbnails.scrubbingContainer, + }); this.player.elements.wrapper.appendChild(previewScrubbingContainer); this.player.elements.display.previewScrubbingContainer = previewScrubbingContainer; @@ -291,15 +293,17 @@ class PreviewThumbnails { } // 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; // Check to see if we've already downloaded higher quality versions of this image - for (let i = 1; i < this.thumbnailsDefs.length; i++) { - if (this.loadedImages.includes(this.thumbnailsDefs[i].frames[thumbNum].text)) { - qualityIndex = i; + this.thumbnails.forEach((thumbnail, index) => { + if (this.loadedImages.includes(thumbnail.frames[thumbNum].text)) { + qualityIndex = index; } - } + }); // Only proceed if either thumbnum or thumbfilename has changed if (thumbNum !== this.showingThumb) { @@ -310,17 +314,20 @@ class PreviewThumbnails { // Show the image that's currently specified in this.showingThumb loadImage(qualityIndex = 0) { - let thumbNum = this.showingThumb; - - const frame = this.thumbnailsDefs[qualityIndex].frames[thumbNum]; - const thumbFilename = this.thumbnailsDefs[qualityIndex].frames[thumbNum].text; - const urlPrefix = this.thumbnailsDefs[qualityIndex].urlPrefix; + const thumbNum = this.showingThumb; + const thumbnail = this.thumbnails[qualityIndex]; + const { urlPrefix } = thumbnail; + const frame = thumbnail.frames[thumbNum]; + const thumbFilename = thumbnail.frames[thumbNum].text; 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 - // 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 - if (this.loadingImage && this.usingJpegSprites) this.loadingImage.onload = null; + // 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.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 const previewImage = new Image(); @@ -330,7 +337,8 @@ class PreviewThumbnails { 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... - previewImage.onload = () => this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true); + previewImage.onload = () => + this.showImage(previewImage, frame, qualityIndex, thumbNum, thumbFilename, true); this.loadingImage = previewImage; this.removeOldImages(previewImage); } else { @@ -342,14 +350,18 @@ class PreviewThumbnails { } 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); if (newImage) { this.currentContainer.appendChild(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 @@ -363,95 +375,100 @@ class PreviewThumbnails { // Remove all preview images that aren't the designated current image removeOldImages(currentImage) { // Get a list of all images, convert it from a DOM list to an array - const allImages = Array.from(this.currentContainer.children); - - for (let image of allImages) { + Array.from(this.currentContainer.children).forEach(image => { 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 // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function 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(() => { currentContainer.removeChild(image); - this.player.debug.log('Removing thumb: ' + image.getAttribute('data-thumbfilename')); - }, removeDelay) + this.player.debug.log(`Removing thumb: ${image.getAttribute('data-thumbfilename')}`); + }, removeDelay); } } - } + }); } // 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 preloadNearby(thumbNum, forward = true) { - return new Promise((resolve, reject) => { + return new Promise(resolve => { setTimeout(() => { - const oldThumbFilename = this.thumbnailsDefs[0].frames[thumbNum].text; + const oldThumbFilename = this.thumbnails[0].frames[thumbNum].text; 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 - let thumbnailsDefsCopy + // 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 thumbnailsClone; if (forward) { - thumbnailsDefsCopy = this.thumbnailsDefs[0].frames.slice(thumbNum); + thumbnailsClone = this.thumbnails[0].frames.slice(thumbNum); } else { - thumbnailsDefsCopy = this.thumbnailsDefs[0].frames.slice(0, thumbNum).reverse(); + thumbnailsClone = this.thumbnails[0].frames.slice(0, thumbNum).reverse(); } let foundOne = false; - for (const frame of thumbnailsDefsCopy) { + thumbnailsClone.forEach(frame => { const newThumbFilename = frame.text; if (newThumbFilename !== oldThumbFilename) { // Found one with a different filename. Make sure it hasn't already been loaded on this page visit if (!this.loadedImages.includes(newThumbFilename)) { 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 previewImage = new Image(); previewImage.src = thumbURL; previewImage.onload = () => { - this.player.debug.log('Preloaded thumb filename: ' + newThumbFilename); - if (!this.loadedImages.includes(newThumbFilename)) this.loadedImages.push(newThumbFilename); + this.player.debug.log(`Preloaded thumb filename: ${newThumbFilename}`); + if (!this.loadedImages.includes(newThumbFilename)) + this.loadedImages.push(newThumbFilename); // 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 (!foundOne) resolve(); + if (!foundOne) { + resolve(); + } } - }, 300) - }) + }, 300); + }); } // If user has been hovering current image for half a second, look for a higher quality one 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 let previewImageHeight = previewImage.naturalHeight; - if (this.usingJpegSprites) previewImageHeight = frame.h; + + if (this.usingSprites) { + previewImageHeight = frame.h; + } 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 setTimeout(() => { // Make sure the mouse hasn't already moved on and started hovering at another image 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); } - }, 300) + }, 300); } } } @@ -459,25 +476,19 @@ class PreviewThumbnails { get currentContainer() { if (this.mouseDown) { return this.player.elements.display.previewScrubbingContainer; - } else { - return this.player.elements.display.previewThumbnailContainer; } + return this.player.elements.display.previewThumbnailContainer; } - get usingJpegSprites() { - if (this.thumbnailsDefs[0].frames[0].w) { - return true; - } else { - return false; - } + get usingSprites() { + return Object.keys(this.thumbnails[0].frames[0]).includes('w'); } get thumbAspectRatio() { - if (this.usingJpegSprites) { - return this.thumbnailsDefs[0].frames[0].w / this.thumbnailsDefs[0].frames[0].h; - } else { - return this.thumbnailsDefs[0].width / this.thumbnailsDefs[0].height; + if (this.usingSprites) { + return this.thumbnails[0].frames[0].w / this.thumbnails[0].frames[0].h; } + return this.thumbnails[0].width / this.thumbnails[0].height; } get thumbContainerHeight() { @@ -485,19 +496,18 @@ class PreviewThumbnails { // return this.player.elements.container.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 - } 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() { if (this.mouseDown) { return this.currentScrubbingImageElement; - } else { - return this.currentThumbnailImageElement; } + return this.currentThumbnailImageElement; } + set currentImageElement(element) { if (this.mouseDown) { this.currentScrubbingImageElement = element; @@ -509,6 +519,7 @@ class PreviewThumbnails { showThumbContainer() { this.player.elements.display.previewThumbnailContainer.style.opacity = 1; } + hideThumbContainer(clearShowing = false) { this.player.elements.display.previewThumbnailContainer.style.opacity = 0; @@ -521,6 +532,7 @@ class PreviewThumbnails { showScrubbingContainer() { this.player.elements.display.previewScrubbingContainer.style.opacity = 1; } + hideScrubbingContainer() { this.player.elements.display.previewScrubbingContainer.style.opacity = 0; this.showingThumb = null; @@ -550,30 +562,31 @@ class PreviewThumbnails { 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 - const minVal = (plyrRect.left - seekbarRect.left + 10); - const maxVal = (plyrRect.right - seekbarRect.left - (previewContainer.clientWidth) - 10); + const minVal = plyrRect.left - seekbarRect.left + 10; + const maxVal = plyrRect.right - seekbarRect.left - previewContainer.clientWidth - 10; // 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) { previewPos = minVal; } if (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 setScrubbingContainerSize() { 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) { - if (this.usingJpegSprites) { - // Find difference between jpeg height and preview container height + if (this.usingSprites) { + // Find difference between height and preview container height const heightMulti = this.thumbContainerHeight / frame.h; 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 } } - - // 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; diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss index 4de90667..24cf99bb 100644 --- a/src/sass/plugins/previewThumbnails.scss +++ b/src/sass/plugins/previewThumbnails.scss @@ -3,75 +3,74 @@ // -------------------------------------------------------------- .plyr__preview-thumbnail-container { - 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-radius: 0px; + 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-radius: 2px; bottom: 100%; box-shadow: $plyr-tooltip-shadow; left: 50%; line-height: 1.3; margin-bottom: $plyr-tooltip-padding * 2; opacity: 0; + overflow: hidden; pointer-events: none; position: absolute; transition: opacity 0.2s 0.1s ease; white-space: nowrap; z-index: 2; - overflow: hidden; - img { - position: absolute; - left: 0px; - top: 0px; + border-radius: 2px; 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-width: none; - border-radius: 0px; + position: absolute; + top: 0; + width: 100%; } // Seek time text .plyr__preview-time-text-container { - position: absolute; - bottom: 0px; - left: 0px; - right: 0px; + bottom: 0; + left: 0; margin-bottom: 2px; + position: absolute; + right: 0; z-index: 3; span { - background-color: rgba(0,0,0,0.55); - color: rgba(255,255,255,1); - padding: 4px 6px 3px 6px; + background-color: rgba(0, 0, 0, 0.55); + color: rgba(255, 255, 255, 1); font-size: $plyr-font-size-small; font-weight: $plyr-font-weight-regular; + padding: 4px 6px 3px; } } } .plyr__preview-scrubbing-container { - position: absolute; - left: 0px; - right: 0px; - top: 0px; - bottom: 0px; + bottom: 0; + filter: blur(1px); height: 100%; - width: 100%; + left: 0; margin: auto; // Required when video is different dimensions to container (e.g., fullscreen) overflow: hidden; - z-index: 1; + position: absolute; + right: 0; + top: 0; transition: opacity 0.3s ease; - filter: blur(1px); - + width: 100%; + z-index: 1; + img { - position: absolute; - left: 0px; - top: 0px; height: 100%; - width: 100%; + left: 0; max-height: none; max-width: none; object-fit: contain; + position: absolute; + top: 0; + width: 100%; } } -- cgit v1.2.3 From 6782737009bec028b393dbfb8c9897cd0c6df48f Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Mon, 14 Jan 2019 00:33:48 +1100 Subject: Fullscreen fixes --- src/js/config/defaults.js | 2 +- src/js/controls.js | 29 ++++++++++++----- src/js/fullscreen.js | 26 +++++++++++++--- src/js/listeners.js | 79 +++++++++++++++++++++++++++++++++++++++++++---- src/js/plugins/vimeo.js | 49 ++++------------------------- src/js/plugins/youtube.js | 9 ++---- src/js/plyr.js | 2 +- src/js/utils/browser.js | 1 + src/js/utils/style.js | 40 ++++++++++++++++++++++++ 9 files changed, 168 insertions(+), 69 deletions(-) create mode 100644 src/js/utils/style.js (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 5b86cceb..ae873eda 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -108,7 +108,7 @@ const defaults = { // Fullscreen settings fullscreen: { enabled: true, // Allow fullscreen? - fallback: true, // Fallback for vintage browsers + fallback: true, // Fallback using full viewport/window iosNative: false, // Use the native fullscreen in iOS (disables custom controls) }, diff --git a/src/js/controls.js b/src/js/controls.js index f414f6d6..953ca293 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -9,7 +9,20 @@ import support from './support'; import { repaint, transitionEndEvent } from './utils/animation'; import { dedupe } from './utils/arrays'; import browser from './utils/browser'; -import { createElement, emptyElement, getAttributesFromSelector, getElement, getElements, hasClass, matches, removeElement, setAttributes, setFocus, toggleClass, toggleHidden } from './utils/elements'; +import { + createElement, + emptyElement, + getAttributesFromSelector, + getElement, + getElements, + hasClass, + matches, + removeElement, + setAttributes, + setFocus, + toggleClass, + toggleHidden, +} from './utils/elements'; import { off, on } from './utils/events'; import i18n from './utils/i18n'; import is from './utils/is'; @@ -667,7 +680,7 @@ const controls = { } // Set CSS custom property - range.style.setProperty('--value', `${range.value / range.max * 100}%`); + range.style.setProperty('--value', `${(range.value / range.max) * 100}%`); }, // Update hover tooltip for seeking @@ -699,7 +712,7 @@ const controls = { // Determine percentage, if already visible if (is.event(event)) { - percent = 100 / clientRect.width * (event.pageX - clientRect.left); + percent = (100 / clientRect.width) * (event.pageX - clientRect.left); } else if (hasClass(this.elements.display.seekTooltip, visible)) { percent = parseFloat(this.elements.display.seekTooltip.style.left, 10); } else { @@ -714,7 +727,7 @@ const controls = { } // Display the time a click would seek to - controls.updateTimeDisplay.call(this, this.elements.display.seekTooltip, this.duration / 100 * percent); + controls.updateTimeDisplay.call(this, this.elements.display.seekTooltip, (this.duration / 100) * percent); // Set position this.elements.display.seekTooltip.style.left = `${percent}%`; @@ -1674,15 +1687,17 @@ const controls = { .filter(Boolean) .forEach(button => { if (is.array(button) || is.nodeList(button)) { - Array.from(button).filter(Boolean).forEach(addProperty); + Array.from(button) + .filter(Boolean) + .forEach(addProperty); } else { addProperty(button); } }); } - // Edge sometimes doesn't finish the paint so force a redraw - if (window.navigator.userAgent.includes('Edge')) { + // Edge sometimes doesn't finish the paint so force a repaint + if (browser.isEdge) { repaint(target); } diff --git a/src/js/fullscreen.js b/src/js/fullscreen.js index 9c21b82a..c86bf877 100644 --- a/src/js/fullscreen.js +++ b/src/js/fullscreen.js @@ -94,6 +94,9 @@ class Fullscreen { // Scroll position this.scrollPosition = { x: 0, y: 0 }; + // Force the use of 'full window/browser' rather than fullscreen + this.forceFallback = player.config.fullscreen.fallback === 'force'; + // Register event listeners // Handle event (incase user presses escape etc) on.call( @@ -130,6 +133,11 @@ class Fullscreen { ); } + // If we're actually using native + get usingNative() { + return Fullscreen.native && !this.forceFallback; + } + // Get the prefix for handlers static get prefix() { // No prefix @@ -174,7 +182,7 @@ class Fullscreen { } // Fallback using classname - if (!Fullscreen.native) { + if (!Fullscreen.native || this.forceFallback) { return hasClass(this.target, this.player.config.classNames.fullscreen.fallback); } @@ -193,7 +201,17 @@ class Fullscreen { // Update UI update() { if (this.enabled) { - this.player.debug.log(`${Fullscreen.native ? 'Native' : 'Fallback'} fullscreen enabled`); + let mode; + + if (this.forceFallback) { + mode = 'Fallback (forced)'; + } else if (Fullscreen.native) { + mode = 'Native'; + } else { + mode = 'Fallback'; + } + + this.player.debug.log(`${mode} fullscreen enabled`); } else { this.player.debug.log('Fullscreen not supported and fallback disabled'); } @@ -211,7 +229,7 @@ class Fullscreen { // iOS native fullscreen doesn't need the request step if (browser.isIos && this.player.config.fullscreen.iosNative) { this.target.webkitEnterFullscreen(); - } else if (!Fullscreen.native) { + } else if (!Fullscreen.native || this.forceFallback) { toggleFallback.call(this, true); } else if (!this.prefix) { this.target.requestFullscreen(); @@ -230,7 +248,7 @@ class Fullscreen { if (browser.isIos && this.player.config.fullscreen.iosNative) { this.target.webkitExitFullscreen(); this.player.play(); - } else if (!Fullscreen.native) { + } else if (!Fullscreen.native || this.forceFallback) { toggleFallback.call(this, false); } else if (!this.prefix) { (document.cancelFullScreen || document.exitFullscreen).call(document); diff --git a/src/js/listeners.js b/src/js/listeners.js index f073f5cb..ae9277a5 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -7,8 +7,9 @@ import ui from './ui'; import { repaint } from './utils/animation'; import browser from './utils/browser'; import { getElement, getElements, matches, toggleClass, toggleHidden } from './utils/elements'; -import { on, once, toggleListener, triggerEvent } from './utils/events'; +import { off, on, once, toggleListener, triggerEvent } from './utils/events'; import is from './utils/is'; +import { setAspectRatio } from './utils/style'; class Listeners { constructor(player) { @@ -164,7 +165,7 @@ class Listeners { // Escape is handle natively when in full screen // So we only need to worry about non native - if (!player.fullscreen.enabled && player.fullscreen.active && code === 27) { + if (code === 27 && !player.fullscreen.usingNative && player.fullscreen.active) { player.fullscreen.toggle(); } @@ -261,10 +262,10 @@ class Listeners { // Container listeners container() { const { player } = this; - const { elements } = player; + const { config, elements, timers } = player; // Keyboard shortcuts - if (!player.config.keyboard.global && player.config.keyboard.focused) { + if (!config.keyboard.global && config.keyboard.focused) { on.call(player, elements.container, 'keydown keyup', this.handleKey, false); } @@ -294,12 +295,78 @@ class Listeners { } // Clear timer - clearTimeout(player.timers.controls); + clearTimeout(timers.controls); // Set new timer to prevent flicker when seeking - player.timers.controls = setTimeout(() => ui.toggleControls.call(player, false), delay); + timers.controls = setTimeout(() => ui.toggleControls.call(player, false), delay); }, ); + + // Force edge to repaint on exit fullscreen + // TODO: Fix weird bug where Edge doesn't re-draw when exiting fullscreen + /* if (browser.isEdge) { + on.call(player, elements.container, 'exitfullscreen', () => { + setTimeout(() => repaint(elements.container), 100); + }); + } */ + + // Set a gutter for Vimeo + const setGutter = (ratio, padding, toggle) => { + if (!player.isVimeo) { + return; + } + + const target = player.elements.wrapper.firstChild; + const [, height] = ratio.split(':').map(Number); + const [videoWidth, videoHeight] = player.embed.ratio.split(':').map(Number); + + target.style.maxWidth = toggle ? `${(height / videoHeight) * videoWidth}px` : null; + target.style.margin = toggle ? '0 auto' : null; + }; + + // Resize on fullscreen change + const setPlayerSize = measure => { + // If we don't need to measure the viewport + if (!measure) { + return setAspectRatio.call(player); + } + + const rect = elements.container.getBoundingClientRect(); + const { width, height } = rect; + + return setAspectRatio.call(player, `${width}:${height}`); + }; + + const resized = () => { + window.clearTimeout(timers.resized); + timers.resized = window.setTimeout(setPlayerSize, 50); + }; + + on.call(player, elements.container, 'enterfullscreen exitfullscreen', event => { + const { target, usingNative } = player.fullscreen; + + // Ignore for iOS native + if (!player.isEmbed || target !== elements.container) { + return; + } + + const isEnter = event.type === 'enterfullscreen'; + + // Set the player size when entering fullscreen to viewport size + const { padding, ratio } = setPlayerSize(isEnter); + + // Set Vimeo gutter + setGutter(ratio, padding, isEnter); + + // If not using native fullscreen, we need to check for resizes of viewport + if (!usingNative) { + if (isEnter) { + on.call(player, window, 'resize', resized); + } else { + off.call(player, window, 'resize', resized); + } + } + }); } // Listen for media events diff --git a/src/js/plugins/vimeo.js b/src/js/plugins/vimeo.js index 2d9ba6e2..c0bcf8af 100644 --- a/src/js/plugins/vimeo.js +++ b/src/js/plugins/vimeo.js @@ -11,6 +11,7 @@ import fetch from '../utils/fetch'; import is from '../utils/is'; import loadScript from '../utils/loadScript'; import { format, stripHTML } from '../utils/strings'; +import { setAspectRatio } from '../utils/style'; import { buildUrlParams } from '../utils/urls'; // Parse Vimeo ID from URL @@ -27,13 +28,6 @@ function parseId(url) { return url.match(regex) ? RegExp.$2 : url; } -// Get aspect ratio for dimensions -function getAspectRatio(width, height) { - const getRatio = (w, h) => (h === 0 ? w : getRatio(h, w % h)); - const ratio = getRatio(width, height); - return `${width / ratio}:${height / ratio}`; -} - // Set playback state and trigger change (only on actual change) function assurePlaybackState(play) { if (play && !this.embed.hasPlayed) { @@ -51,7 +45,7 @@ const vimeo = { toggleClass(this.elements.wrapper, this.config.classNames.embed, true); // Set intial ratio - vimeo.setAspectRatio.call(this); + setAspectRatio.call(this); // Load the API if not already if (!is.object(window.Vimeo)) { @@ -67,22 +61,6 @@ const vimeo = { } }, - // Set aspect ratio - // For Vimeo we have an extra 300% height
to hide the standard controls and UI - setAspectRatio(input) { - const [x, y] = (is.string(input) ? input : this.config.ratio).split(':').map(Number); - const padding = (100 / x) * y; - vimeo.padding = padding; - this.elements.wrapper.style.paddingBottom = `${padding}%`; - - if (this.supported.ui) { - const height = 240; - const offset = (height - padding) / (height / 50); - - this.media.style.transform = `translateY(-${offset}%)`; - } - }, - // API Ready ready() { const player = this; @@ -91,7 +69,7 @@ const vimeo = { const options = { loop: player.config.loop.active, autoplay: player.autoplay, - // muted: player.muted, + muted: player.muted, byline: false, portrait: false, title: false, @@ -300,8 +278,9 @@ const vimeo = { // Set aspect ratio based on video size Promise.all([player.embed.getVideoWidth(), player.embed.getVideoHeight()]).then(dimensions => { - vimeo.ratio = getAspectRatio(dimensions[0], dimensions[1]); - vimeo.setAspectRatio.call(this, vimeo.ratio); + const [width, height] = dimensions; + player.embed.ratio = `${width}:${height}`; + setAspectRatio.call(this, player.embed.ratio); }); // Set autopause @@ -405,22 +384,6 @@ const vimeo = { triggerEvent.call(player, player.media, 'error'); }); - // Set height/width on fullscreen - player.on('enterfullscreen exitfullscreen', event => { - const { target } = player.fullscreen; - - // Ignore for iOS native - if (target !== player.elements.container) { - return; - } - - const toggle = event.type === 'enterfullscreen'; - const [x, y] = vimeo.ratio.split(':').map(Number); - const dimension = x > y ? 'width' : 'height'; - - target.style[dimension] = toggle ? `${vimeo.padding}%` : null; - }); - // Rebuild UI setTimeout(() => ui.build.call(player), 0); }, diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 0cc8fd1d..9d47aa53 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -10,6 +10,7 @@ import is from '../utils/is'; import loadImage from '../utils/loadImage'; import loadScript from '../utils/loadScript'; import { format, generateId } from '../utils/strings'; +import { setAspectRatio } from '../utils/style'; // Parse YouTube ID from URL function parseId(url) { @@ -38,7 +39,7 @@ const youtube = { toggleClass(this.elements.wrapper, this.config.classNames.embed, true); // Set aspect ratio - youtube.setAspectRatio.call(this); + setAspectRatio.call(this); // Setup API if (is.object(window.YT) && is.function(window.YT.Player)) { @@ -98,12 +99,6 @@ const youtube = { } }, - // Set aspect ratio - setAspectRatio() { - const ratio = this.config.ratio.split(':'); - this.elements.wrapper.style.paddingBottom = `${100 / ratio[0] * ratio[1]}%`; - }, - // API ready ready() { const player = this; diff --git a/src/js/plyr.js b/src/js/plyr.js index 872eb927..1059ce05 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -263,7 +263,7 @@ class Plyr { // Wrap media if (!is.element(this.elements.container)) { - this.elements.container = createElement('div'); + this.elements.container = createElement('div', { tabindex: 0 }); wrap(this.media, this.elements.container); } diff --git a/src/js/utils/browser.js b/src/js/utils/browser.js index d574f683..11705074 100644 --- a/src/js/utils/browser.js +++ b/src/js/utils/browser.js @@ -5,6 +5,7 @@ const browser = { isIE: /* @cc_on!@ */ false || !!document.documentMode, + isEdge: window.navigator.userAgent.includes('Edge'), isWebkit: 'WebkitAppearance' in document.documentElement.style && !/Edge/.test(navigator.userAgent), isIPhone: /(iPhone|iPod)/gi.test(navigator.platform), isIos: /(iPad|iPhone|iPod)/gi.test(navigator.platform), diff --git a/src/js/utils/style.js b/src/js/utils/style.js new file mode 100644 index 00000000..a8eb393b --- /dev/null +++ b/src/js/utils/style.js @@ -0,0 +1,40 @@ +// ========================================================================== +// Style utils +// ========================================================================== + +import is from './is'; + +/* function reduceAspectRatio(width, height) { + const getRatio = (w, h) => (h === 0 ? w : getRatio(h, w % h)); + const ratio = getRatio(width, height); + return `${width / ratio}:${height / ratio}`; +} */ + +// Set aspect ratio for responsive container +export function setAspectRatio(input) { + let ratio = input; + + if (!is.string(ratio) && !is.nullOrUndefined(this.embed)) { + ({ ratio } = this.embed); + } + + if (!is.string(ratio)) { + ({ ratio } = this.config); + } + + const [x, y] = ratio.split(':').map(Number); + const padding = (100 / x) * y; + + this.elements.wrapper.style.paddingBottom = `${padding}%`; + + // For Vimeo we have an extra
to hide the standard controls and UI + if (this.isVimeo && this.supported.ui) { + const height = 240; + const offset = (height - padding) / (height / 50); + this.media.style.transform = `translateY(-${offset}%)`; + } + + return { padding, ratio }; +} + +export default { setAspectRatio }; -- cgit v1.2.3 From f927d26ce7150a12422a28e0c32edbb399632571 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Thu, 17 Jan 2019 11:37:19 +1100 Subject: v3.4.8 - Calling customized controls function with proper arguments (thanks @a60814billy) --- src/js/config/defaults.js | 2 +- src/js/plyr.js | 2 +- src/js/plyr.polyfilled.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index c3f97eee..0b190b05 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.4.7/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.4.8/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/plyr.js b/src/js/plyr.js index c8154429..2a8a75f9 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.4.7 +// plyr.js v3.4.8 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index ac6d1c28..42207a1e 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.4.7 +// plyr.js v3.4.8 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== -- cgit v1.2.3 From 4ab8a54a11285366bdeb1f7a13b10981dd1f7ab2 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Mon, 21 Jan 2019 00:32:20 +1100 Subject: Preview design tweaks --- src/js/config/defaults.js | 11 +- src/js/plugins/previewThumbnails.js | 173 +++++++++++++++++--------------- src/sass/plugins/previewThumbnails.scss | 65 +++++++----- 3 files changed, 142 insertions(+), 107 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index ae873eda..d0521afd 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -375,9 +375,12 @@ const defaults = { }, tabFocus: 'plyr__tab-focus', previewThumbnails: { - thumbnailContainer: 'plyr__preview-thumbnail-container', - scrubbingContainer: 'plyr__preview-scrubbing-container', - timeTextContainer: 'plyr__preview-time-text-container', + // Tooltip thumbs + thumbContainer: 'plyr__preview-thumb', + imageContainer: 'plyr__preview-thumb__image-container', + timeContainer: 'plyr__preview-thumb__time-container', + // Scrubber + scrubbingContainer: 'plyr__preview-scrubber', }, }, @@ -400,7 +403,7 @@ const defaults = { enabled: false, publisherId: '', }, - + // YouTube nocookies mode noCookie: false, diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index fd1733d2..9dd37e15 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -76,6 +76,11 @@ class PreviewThumbnails { this.mouseDown = false; this.loadedImages = []; + this.elements = { + thumb: {}, + scrubber: {}, + }; + if (this.enabled) { this.load(); } @@ -93,8 +98,8 @@ class PreviewThumbnails { // Initiate DOM listeners so that our preview thumbnails can be used this.listeners(); - // Build HTML DOM elements - this.elements(); + // Render DOM elements + this.render(); // Check to see if thumb container size was specified manually in CSS this.determineContainerAutoSizing(); @@ -118,7 +123,7 @@ class PreviewThumbnails { Promise.all(promises).then(() => { // Sort smallest to biggest (e.g., [120p, 480p, 1080p]) this.thumbnails.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', this.thumbnails); resolve(); }); @@ -184,7 +189,7 @@ class PreviewThumbnails { this.mousePosX = event.pageX; // Set time text inside image container - this.player.elements.display.previewThumbnailTimeText.innerText = formatTime(this.seekTime); + this.elements.thumb.time.innerText = formatTime(this.seekTime); // Download and show image this.showImageAtCurrentTime(); @@ -254,34 +259,37 @@ class PreviewThumbnails { /** * Create HTML elements for image containers */ - elements() { + render() { // Create HTML element: plyr__preview-thumbnail-container - const previewThumbnailContainer = createElement('div', { - class: this.player.config.classNames.previewThumbnails.thumbnailContainer, + this.elements.thumb.container = createElement('div', { + class: this.player.config.classNames.previewThumbnails.thumbContainer, }); - this.player.elements.progress.appendChild(previewThumbnailContainer); - this.player.elements.display.previewThumbnailContainer = previewThumbnailContainer; + // Wrapper for the image for styling + this.elements.thumb.imageContainer = createElement('div', { + class: this.player.config.classNames.previewThumbnails.imageContainer, + }); + this.elements.thumb.container.appendChild(this.elements.thumb.imageContainer); // Create HTML element, parent+span: time text (e.g., 01:32:00) - const timeTextContainer = createElement('div', { - class: this.player.config.classNames.previewThumbnails.timeTextContainer, + const timeContainer = createElement('div', { + class: this.player.config.classNames.previewThumbnails.timeContainer, }); - this.player.elements.display.previewThumbnailContainer.appendChild(timeTextContainer); + this.elements.thumb.time = createElement('span', {}, '00:00'); + timeContainer.appendChild(this.elements.thumb.time); - const timeText = createElement('span', {}, '00:00'); + this.elements.thumb.container.appendChild(timeContainer); - timeTextContainer.appendChild(timeText); - this.player.elements.display.previewThumbnailTimeText = timeText; + // Inject the whole thumb + this.player.elements.progress.appendChild(this.elements.thumb.container); // Create HTML element: plyr__preview-scrubbing-container - const previewScrubbingContainer = createElement('div', { + this.elements.scrubber.container = createElement('div', { class: this.player.config.classNames.previewThumbnails.scrubbingContainer, }); - this.player.elements.wrapper.appendChild(previewScrubbingContainer); - this.player.elements.display.previewScrubbingContainer = previewScrubbingContainer; + this.player.elements.wrapper.appendChild(this.elements.scrubber.container); } showImageAtCurrentTime() { @@ -319,23 +327,24 @@ class PreviewThumbnails { const { urlPrefix } = thumbnail; const frame = thumbnail.frames[thumbNum]; 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.dataset.filename !== thumbFilename) { // 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 sprites. Without sprites we really want to show as many images as possible, as a best-effort - if (this.loadingImage && this.usingSprites) 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 const previewImage = new Image(); - previewImage.src = thumbURL; - previewImage.setAttribute('data-thumbnum', thumbNum); - previewImage.setAttribute('data-thumbfilename', thumbFilename); + previewImage.src = thumbUrl; + previewImage.dataset.index = thumbNum; + previewImage.dataset.filename = thumbFilename; this.showingThumbFilename = thumbFilename; + this.player.debug.log(`Loading image: ${thumbUrl}`); + // 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); @@ -344,7 +353,7 @@ class PreviewThumbnails { } else { // Update the existing image this.showImage(this.currentImageElement, frame, qualityIndex, thumbNum, thumbFilename, false); - this.currentImageElement.setAttribute('data-thumbnum', thumbNum); + this.currentImageElement.dataset.index = thumbNum; this.removeOldImages(this.currentImageElement); } } @@ -356,7 +365,7 @@ class PreviewThumbnails { this.setImageSizeAndOffset(previewImage, frame); if (newImage) { - this.currentContainer.appendChild(previewImage); + this.currentImageContainer.appendChild(previewImage); this.currentImageElement = previewImage; if (!this.loadedImages.includes(thumbFilename)) { @@ -375,24 +384,23 @@ class PreviewThumbnails { // Remove all preview images that aren't the designated current image removeOldImages(currentImage) { // Get a list of all images, convert it from a DOM list to an array - Array.from(this.currentContainer.children).forEach(image => { - if (image.tagName === 'IMG') { - const removeDelay = this.usingSprites ? 500 : 1000; - - 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 - // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function - image.setAttribute('data-deleting', 'true'); - const { currentContainer } = this; // This has to be set before the timeout - to prevent issues switching between hover and scrub - - setTimeout(() => { - currentContainer.removeChild(image); - this.player.debug.log(`Removing thumb: ${image.getAttribute('data-thumbfilename')}`); - }, removeDelay); - } + Array.from(this.currentImageContainer.children).forEach(image => { + if (image.tagName.toLowerCase() !== 'img') { + return; + } + + const removeDelay = this.usingSprites ? 500 : 1000; + + if (image.dataset.index !== currentImage.dataset.index && !image.dataset.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 + // First set attribute 'deleting' to prevent multi-handling of this on repeat firing of this function + image.dataset.deleting = true; + const { currentImageContainer } = this; // This has to be set before the timeout - to prevent issues switching between hover and scrub + + setTimeout(() => { + currentImageContainer.removeChild(image); + this.player.debug.log(`Removing thumb: ${image.dataset.filename}`); + }, removeDelay); } }); } @@ -473,11 +481,12 @@ class PreviewThumbnails { } } - get currentContainer() { + get currentImageContainer() { if (this.mouseDown) { - return this.player.elements.display.previewScrubbingContainer; + return this.elements.scrubber.container; } - return this.player.elements.display.previewThumbnailContainer; + + return this.elements.thumb.imageContainer; } get usingSprites() { @@ -488,23 +497,23 @@ class PreviewThumbnails { if (this.usingSprites) { return this.thumbnails[0].frames[0].w / this.thumbnails[0].frames[0].h; } + return this.thumbnails[0].width / this.thumbnails[0].height; } get thumbContainerHeight() { if (this.mouseDown) { - // return this.player.elements.container.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 Math.floor(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.elements.container.clientHeight / 4; - return this.player.media.clientWidth / this.thumbAspectRatio / 4; + + return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio / 4); } get currentImageElement() { if (this.mouseDown) { return this.currentScrubbingImageElement; } + return this.currentThumbnailImageElement; } @@ -517,11 +526,11 @@ class PreviewThumbnails { } showThumbContainer() { - this.player.elements.display.previewThumbnailContainer.style.opacity = 1; + this.elements.thumb.container.style.opacity = 1; } hideThumbContainer(clearShowing = false) { - this.player.elements.display.previewThumbnailContainer.style.opacity = 0; + this.elements.thumb.container.style.opacity = 0; if (clearShowing) { this.showingThumb = null; @@ -530,27 +539,28 @@ class PreviewThumbnails { } showScrubbingContainer() { - this.player.elements.display.previewScrubbingContainer.style.opacity = 1; + this.elements.scrubber.container.style.opacity = 1; } hideScrubbingContainer() { - this.player.elements.display.previewScrubbingContainer.style.opacity = 0; + this.elements.scrubber.container.style.opacity = 0; this.showingThumb = null; this.showingThumbFilename = null; } determineContainerAutoSizing() { - if (this.player.elements.display.previewThumbnailContainer.clientHeight > 20) { - this.sizeSpecifiedInCSS = true; // This will prevent auto sizing in this.setThumbContainerSizeAndPos() + if (this.elements.thumb.imageContainer.clientHeight > 20) { + // This will prevent auto sizing in this.setThumbContainerSizeAndPos() + this.sizeSpecifiedInCSS = true; } } // 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 setThumbContainerSizeAndPos() { if (!this.sizeSpecifiedInCSS) { - const thumbWidth = this.thumbContainerHeight * this.thumbAspectRatio; - this.player.elements.display.previewThumbnailContainer.style.height = `${this.thumbContainerHeight}px`; - this.player.elements.display.previewThumbnailContainer.style.width = `${thumbWidth}px`; + const thumbWidth = Math.floor(this.thumbContainerHeight * this.thumbAspectRatio); + this.elements.thumb.imageContainer.style.height = `${this.thumbContainerHeight}px`; + this.elements.thumb.imageContainer.style.width = `${thumbWidth}px`; } this.setThumbContainerPos(); @@ -559,41 +569,46 @@ class PreviewThumbnails { setThumbContainerPos() { const seekbarRect = this.player.elements.progress.getBoundingClientRect(); const plyrRect = this.player.elements.container.getBoundingClientRect(); - const previewContainer = this.player.elements.display.previewThumbnailContainer; + const { container } = this.elements.thumb; // 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 maxVal = plyrRect.right - seekbarRect.left - previewContainer.clientWidth - 10; + const maxVal = plyrRect.right - seekbarRect.left - container.clientWidth - 10; // 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 - container.clientWidth / 2; + if (previewPos < minVal) { previewPos = minVal; } + if (previewPos > maxVal) { previewPos = maxVal; } - previewContainer.style.left = `${previewPos}px`; + + container.style.left = `${previewPos}px`; } // Can't use 100% width, in case the video is a different aspect ratio to the video container setScrubbingContainerSize() { - 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.elements.scrubber.container.style.width = `${this.player.media.clientWidth}px`; + // Can't use media.clientHeight - html5 video goes big and does black bars above and below + this.elements.scrubber.container.style.height = `${this.player.media.clientWidth / this.thumbAspectRatio}px`; } // Sprites need to be offset to the correct location setImageSizeAndOffset(previewImage, frame) { - if (this.usingSprites) { - // Find difference between height and preview container height - const heightMulti = this.thumbContainerHeight / frame.h; - - previewImage.style.height = `${previewImage.naturalHeight * heightMulti}px`; - previewImage.style.width = `${previewImage.naturalWidth * heightMulti}px`; - previewImage.style.left = `-${Math.ceil(frame.x * heightMulti)}px`; - previewImage.style.top = `-${frame.y * heightMulti}px`; // todo: might need to round this one up too + if (!this.usingSprites) { + return; } + + // Find difference between height and preview container height + const heightMulti = this.thumbContainerHeight / frame.h; + + previewImage.style.height = `${Math.floor(previewImage.naturalHeight * heightMulti)}px`; + previewImage.style.width = `${Math.floor(previewImage.naturalWidth * heightMulti)}px`; + previewImage.style.left = `-${Math.ceil(frame.x * heightMulti)}px`; + previewImage.style.top = `-${frame.y * heightMulti}px`; // TODO: might need to round this one up too } } diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss index 24cf99bb..b62e1021 100644 --- a/src/sass/plugins/previewThumbnails.scss +++ b/src/sass/plugins/previewThumbnails.scss @@ -2,54 +2,71 @@ // Preview Thumbnails // -------------------------------------------------------------- -.plyr__preview-thumbnail-container { - 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-radius: 2px; +.plyr__preview-thumb { + background-color: $plyr-tooltip-bg; + border-radius: 3px; bottom: 100%; box-shadow: $plyr-tooltip-shadow; - left: 50%; - line-height: 1.3; margin-bottom: $plyr-tooltip-padding * 2; opacity: 0; - overflow: hidden; + padding: 3px; pointer-events: none; position: absolute; transition: opacity 0.2s 0.1s ease; - white-space: nowrap; z-index: 2; - img { - border-radius: 2px; - height: 100%; // Non-jpeg-sprite images are 100%. Jpeg sprites will have their size applied by javascript - left: 0; - max-height: none; - max-width: none; + // The background triangle + &::before { + border-left: $plyr-tooltip-arrow-size solid transparent; + border-right: $plyr-tooltip-arrow-size solid transparent; + border-top: $plyr-tooltip-arrow-size solid $plyr-tooltip-bg; + bottom: -$plyr-tooltip-arrow-size; + content: ''; + height: 0; + left: 50%; position: absolute; - top: 0; - width: 100%; + transform: translateX(-50%); + width: 0; + z-index: 2; + } + + &__image-container { + border-radius: 2px; + overflow: hidden; + position: relative; + z-index: 0; + + img { + height: 100%; // Non-jpeg-sprite images are 100%. Jpeg sprites will have their size applied by javascript + left: 0; + max-height: none; + max-width: none; + position: absolute; + top: 0; + width: 100%; + } } // Seek time text - .plyr__preview-time-text-container { - bottom: 0; + &__time-container { + bottom: 6px; left: 0; - margin-bottom: 2px; position: absolute; right: 0; + white-space: nowrap; z-index: 3; span { background-color: rgba(0, 0, 0, 0.55); - color: rgba(255, 255, 255, 1); - font-size: $plyr-font-size-small; - font-weight: $plyr-font-weight-regular; - padding: 4px 6px 3px; + border-radius: 2px; + color: #fff; + font-size: $plyr-font-size-time; + padding: 3px 6px; } } } -.plyr__preview-scrubbing-container { +.plyr__preview-scrubber { bottom: 0; filter: blur(1px); height: 100%; -- cgit v1.2.3 From 263e88f6b3c752f6414a83717e81f7beee03319a Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Mon, 21 Jan 2019 00:39:28 +1100 Subject: Comments --- src/js/plugins/previewThumbnails.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 9dd37e15..973e377c 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -336,7 +336,9 @@ class PreviewThumbnails { 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(); previewImage.src = thumbUrl; previewImage.dataset.index = thumbNum; @@ -395,7 +397,8 @@ class PreviewThumbnails { // 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 image.dataset.deleting = true; - const { currentImageContainer } = this; // This has to be set before the timeout - to prevent issues switching between hover and scrub + // This has to be set before the timeout - to prevent issues switching between hover and scrub + const { currentImageContainer } = this; setTimeout(() => { currentImageContainer.removeChild(image); -- cgit v1.2.3 From c577eb01cea0cb2c742a8cbd10909f63b869cd4e Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 22 Jan 2019 16:24:46 +1100 Subject: Style tweaks for preview plugin --- src/js/config/defaults.js | 6 ++- src/js/plugins/previewThumbnails.js | 68 ++++++++++++++++----------------- src/sass/plugins/previewThumbnails.scss | 17 ++++++++- 3 files changed, 52 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index d0521afd..14019ca2 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -377,10 +377,12 @@ const defaults = { previewThumbnails: { // Tooltip thumbs thumbContainer: 'plyr__preview-thumb', + thumbContainerShown: 'plyr__preview-thumb--is-shown', imageContainer: 'plyr__preview-thumb__image-container', timeContainer: 'plyr__preview-thumb__time-container', - // Scrubber - scrubbingContainer: 'plyr__preview-scrubber', + // Scrubbing + scrubbingContainer: 'plyr__preview-scrubbing', + scrubbingContainerShown: 'plyr__preview-scrubbing--is-shown', }, }, diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 973e377c..053875c7 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -66,7 +66,7 @@ const parseVtt = vttDataString => { class PreviewThumbnails { /** * PreviewThumbnails constructor. - * @param {object} player + * @param {Plyr} player * @return {PreviewThumbnails} */ constructor(player) { @@ -78,7 +78,7 @@ class PreviewThumbnails { this.elements = { thumb: {}, - scrubber: {}, + scrubbing: {}, }; if (this.enabled) { @@ -210,13 +210,13 @@ class PreviewThumbnails { // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering on.call(this.player, this.player.elements.progress, 'mouseleave click', () => { - this.hideThumbContainer(true); + this.toggleThumbContainer(false, true); }); this.player.on('play', () => { - this.hideThumbContainer(true); + this.toggleThumbContainer(false, true); }); this.player.on('seeked', () => { - this.hideThumbContainer(false); + this.toggleThumbContainer(false); }); // Show scrubbing preview @@ -226,8 +226,8 @@ class PreviewThumbnails { this.mouseDown = true; // Wait until media has a duration if (this.player.media.duration) { - this.showScrubbingContainer(); - this.hideThumbContainer(true); + this.toggleScrubbingContainer(true); + this.toggleThumbContainer(false, true); // Download and show image this.showImageAtCurrentTime(); @@ -243,13 +243,13 @@ class PreviewThumbnails { // 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(); + this.toggleScrubbingContainer(false); } 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(); + this.toggleScrubbingContainer(false); } }); } @@ -285,18 +285,18 @@ class PreviewThumbnails { this.player.elements.progress.appendChild(this.elements.thumb.container); // Create HTML element: plyr__preview-scrubbing-container - this.elements.scrubber.container = createElement('div', { + this.elements.scrubbing.container = createElement('div', { class: this.player.config.classNames.previewThumbnails.scrubbingContainer, }); - this.player.elements.wrapper.appendChild(this.elements.scrubber.container); + this.player.elements.wrapper.appendChild(this.elements.scrubbing.container); } showImageAtCurrentTime() { if (this.mouseDown) { this.setScrubbingContainerSize(); } else { - this.showThumbContainer(); + this.toggleThumbContainer(true); this.setThumbContainerSizeAndPos(); } @@ -486,7 +486,7 @@ class PreviewThumbnails { get currentImageContainer() { if (this.mouseDown) { - return this.elements.scrubber.container; + return this.elements.scrubbing.container; } return this.elements.thumb.imageContainer; @@ -506,7 +506,8 @@ class PreviewThumbnails { get thumbContainerHeight() { if (this.mouseDown) { - return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio); // Can't use media.clientHeight - html5 video goes big and does black bars above and below + // Can't use media.clientHeight - HTML5 video goes big and does black bars above and below + return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio); } return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio / 4); @@ -528,27 +529,24 @@ class PreviewThumbnails { } } - showThumbContainer() { - this.elements.thumb.container.style.opacity = 1; - } - - hideThumbContainer(clearShowing = false) { - this.elements.thumb.container.style.opacity = 0; + toggleThumbContainer(toggle = false, clearShowing = false) { + const className = this.player.config.classNames.previewThumbnails.thumbContainerShown; + this.elements.thumb.container.classList.toggle(className, toggle); - if (clearShowing) { + if (!toggle && clearShowing) { this.showingThumb = null; this.showingThumbFilename = null; } } - showScrubbingContainer() { - this.elements.scrubber.container.style.opacity = 1; - } + toggleScrubbingContainer(toggle = false) { + const className = this.player.config.classNames.previewThumbnails.scrubbingContainerShown; + this.elements.scrubbing.container.classList.toggle(className, toggle); - hideScrubbingContainer() { - this.elements.scrubber.container.style.opacity = 0; - this.showingThumb = null; - this.showingThumbFilename = null; + if (!toggle) { + this.showingThumb = null; + this.showingThumbFilename = null; + } } determineContainerAutoSizing() { @@ -594,9 +592,9 @@ class PreviewThumbnails { // Can't use 100% width, in case the video is a different aspect ratio to the video container setScrubbingContainerSize() { - this.elements.scrubber.container.style.width = `${this.player.media.clientWidth}px`; + this.elements.scrubbing.container.style.width = `${this.player.media.clientWidth}px`; // Can't use media.clientHeight - html5 video goes big and does black bars above and below - this.elements.scrubber.container.style.height = `${this.player.media.clientWidth / this.thumbAspectRatio}px`; + this.elements.scrubbing.container.style.height = `${this.player.media.clientWidth / this.thumbAspectRatio}px`; } // Sprites need to be offset to the correct location @@ -606,12 +604,12 @@ class PreviewThumbnails { } // Find difference between height and preview container height - const heightMulti = this.thumbContainerHeight / frame.h; + const multiplier = this.thumbContainerHeight / frame.h; - previewImage.style.height = `${Math.floor(previewImage.naturalHeight * heightMulti)}px`; - previewImage.style.width = `${Math.floor(previewImage.naturalWidth * heightMulti)}px`; - previewImage.style.left = `-${Math.ceil(frame.x * heightMulti)}px`; - previewImage.style.top = `-${frame.y * heightMulti}px`; // TODO: might need to round this one up too + previewImage.style.height = `${Math.floor(previewImage.naturalHeight * multiplier)}px`; + previewImage.style.width = `${Math.floor(previewImage.naturalWidth * multiplier)}px`; + previewImage.style.left = `-${frame.x * multiplier}px`; + previewImage.style.top = `-${frame.y * multiplier}px`; } } diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss index b62e1021..8fb0132d 100644 --- a/src/sass/plugins/previewThumbnails.scss +++ b/src/sass/plugins/previewThumbnails.scss @@ -12,9 +12,16 @@ padding: 3px; pointer-events: none; position: absolute; - transition: opacity 0.2s 0.1s ease; + transform: translate(0, 10px) scale(0.8); + transform-origin: 50% 100%; + transition: transform 0.2s 0.1s ease, opacity 0.2s 0.1s ease; z-index: 2; + &--is-shown { + opacity: 1; + transform: translate(0, 0) scale(1); + } + // The background triangle &::before { border-left: $plyr-tooltip-arrow-size solid transparent; @@ -31,6 +38,7 @@ } &__image-container { + background: $plyr-color-heather; border-radius: 2px; overflow: hidden; position: relative; @@ -66,12 +74,13 @@ } } -.plyr__preview-scrubber { +.plyr__preview-scrubbing { bottom: 0; filter: blur(1px); height: 100%; left: 0; margin: auto; // Required when video is different dimensions to container (e.g., fullscreen) + opacity: 0; overflow: hidden; position: absolute; right: 0; @@ -90,4 +99,8 @@ top: 0; width: 100%; } + + &--is-shown { + opacity: 1; + } } -- cgit v1.2.3 From 052e426810d504a01beb05c8bb34f83e190a0679 Mon Sep 17 00:00:00 2001 From: Christian Gambardella Date: Thu, 24 Jan 2019 12:07:01 +0100 Subject: Adds options for vimeo plugin #1316 This adds replaces hard coded vimeo options with options that can be passed to the Plyr instance when initializing. --- src/js/config/defaults.js | 9 +++++++++ src/js/plugins/vimeo.js | 10 +++++----- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 14019ca2..1891074d 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -413,6 +413,15 @@ const defaults = { previewThumbnails: { enabled: false, }, + + // Vimeo plugin + vimeo: { + byline: false, + portrait: false, + title: false, + speed: true, + transparent: false, + }, }; export default defaults; diff --git a/src/js/plugins/vimeo.js b/src/js/plugins/vimeo.js index c0bcf8af..5fcd9ac8 100644 --- a/src/js/plugins/vimeo.js +++ b/src/js/plugins/vimeo.js @@ -70,11 +70,11 @@ const vimeo = { loop: player.config.loop.active, autoplay: player.autoplay, muted: player.muted, - byline: false, - portrait: false, - title: false, - speed: true, - transparent: 0, + byline: player.config.vimeo.byline, + portrait: player.config.vimeo.portrait, + title: player.config.vimeo.title, + speed: player.config.vimeo.speed, + transparent: player.config.vimeo.transparent === true ? 1 : 0, gesture: 'media', playsinline: !this.config.fullscreen.iosNative, }; -- cgit v1.2.3 From c44351507f0a06578a1fee10185117e7df64ece9 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 26 Jan 2019 16:31:47 +1100 Subject: Plugin tweaks for ads and previews --- src/js/config/defaults.js | 1 + src/js/plugins/ads.js | 29 +++++++++++++---- src/js/plugins/previewThumbnails.js | 9 ++++++ src/sass/plugins/previewThumbnails.scss | 56 ++++++++++++++++++++------------- src/sass/settings/sliders.scss | 4 +-- 5 files changed, 69 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 14019ca2..1bb940f8 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -404,6 +404,7 @@ const defaults = { ads: { enabled: false, publisherId: '', + tagUrl: '', }, // YouTube nocookies mode diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index 375fdc13..c643c5dd 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -22,7 +22,7 @@ class Ads { */ constructor(player) { this.player = player; - this.publisherId = player.config.ads.publisherId; + this.config = player.config.ads; this.playing = false; this.initialized = false; this.elements = { @@ -49,8 +49,13 @@ class Ads { } get enabled() { + const { config } = this; + return ( - this.player.isHTML5 && this.player.isVideo && this.player.config.ads.enabled && !is.empty(this.publisherId) + this.player.isHTML5 && + this.player.isVideo && + config.enabled && + (!is.empty(config.publisherId) || is.url(config.tagUrl)) ); } @@ -95,8 +100,14 @@ class Ads { this.setupIMA(); } - // Build the default tag URL + // Build the tag URL get tagUrl() { + const { config } = this; + + if (is.url(config.tagUrl)) { + return config.tagUrl; + } + const params = { AV_PUBLISHERID: '58c25bb0073ef448b1087ad6', AV_CHANNELID: '5a0458dc28a06145e4519d21', @@ -233,7 +244,7 @@ class Ads { const seekElement = this.player.elements.progress; if (is.element(seekElement)) { - const cuePercentage = 100 / this.player.duration * cuePoint; + const cuePercentage = (100 / this.player.duration) * cuePoint; const cue = createElement('span', { class: this.player.config.classNames.cues, }); @@ -273,6 +284,7 @@ class Ads { // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED) // don't have ad object associated const ad = event.getAd(); + const adData = event.getAdData(); // Proxy event const dispatchEvent = type => { @@ -368,6 +380,12 @@ class Ads { dispatchEvent(event.type); break; + case google.ima.AdEvent.Type.LOG: + if (adData.adError) { + this.player.debug.warn(`Non-fatal ad error: ${adData.adError.getMessage()}`); + } + break; + default: break; } @@ -396,9 +414,8 @@ class Ads { this.loader.contentComplete(); }); - this.player.on('seeking', () => { + this.player.on('timeupdate', () => { time = this.player.currentTime; - return time; }); this.player.on('seeked', () => { diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 053875c7..57b9265f 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -301,11 +301,20 @@ class PreviewThumbnails { } // Find the desired thumbnail index + // TODO: Handle a video longer than the thumbs where thumbNum is null const thumbNum = this.thumbnails[0].frames.findIndex( frame => this.seekTime >= frame.startTime && this.seekTime <= frame.endTime, ); + const hasThumb = thumbNum >= 0; let qualityIndex = 0; + this.toggleThumbContainer(hasThumb); + + // No matching thumb found + if (!hasThumb) { + return; + } + // Check to see if we've already downloaded higher quality versions of this image this.thumbnails.forEach((thumbnail, index) => { if (this.loadedImages.includes(thumbnail.frames[thumbNum].text)) { diff --git a/src/sass/plugins/previewThumbnails.scss b/src/sass/plugins/previewThumbnails.scss index 8fb0132d..02a2f619 100644 --- a/src/sass/plugins/previewThumbnails.scss +++ b/src/sass/plugins/previewThumbnails.scss @@ -2,14 +2,26 @@ // Preview Thumbnails // -------------------------------------------------------------- +$plyr-preview-padding: $plyr-tooltip-padding !default; +$plyr-preview-bg: $plyr-tooltip-bg !default; +$plyr-preview-radius: $plyr-tooltip-radius !default; +$plyr-preview-shadow: $plyr-tooltip-shadow !default; +$plyr-preview-arrow-size: $plyr-tooltip-arrow-size !default; +$plyr-preview-image-bg: $plyr-color-heather !default; +$plyr-preview-time-font-size: $plyr-font-size-time !default; +$plyr-preview-time-padding: 3px 6px !default; +$plyr-preview-time-bg: rgba(0, 0, 0, 0.55); +$plyr-preview-time-color: #fff; +$plyr-preview-time-bottom-offset: 6px; + .plyr__preview-thumb { - background-color: $plyr-tooltip-bg; + background-color: $plyr-preview-bg; border-radius: 3px; bottom: 100%; - box-shadow: $plyr-tooltip-shadow; - margin-bottom: $plyr-tooltip-padding * 2; + box-shadow: $plyr-preview-shadow; + margin-bottom: $plyr-preview-padding * 2; opacity: 0; - padding: 3px; + padding: $plyr-preview-radius; pointer-events: none; position: absolute; transform: translate(0, 10px) scale(0.8); @@ -24,10 +36,10 @@ // The background triangle &::before { - border-left: $plyr-tooltip-arrow-size solid transparent; - border-right: $plyr-tooltip-arrow-size solid transparent; - border-top: $plyr-tooltip-arrow-size solid $plyr-tooltip-bg; - bottom: -$plyr-tooltip-arrow-size; + border-left: $plyr-preview-arrow-size solid transparent; + border-right: $plyr-preview-arrow-size solid transparent; + border-top: $plyr-preview-arrow-size solid $plyr-preview-bg; + bottom: -$plyr-preview-arrow-size; content: ''; height: 0; left: 50%; @@ -38,14 +50,14 @@ } &__image-container { - background: $plyr-color-heather; - border-radius: 2px; + background: $plyr-preview-image-bg; + border-radius: ($plyr-preview-radius - 1px); overflow: hidden; position: relative; z-index: 0; img { - height: 100%; // Non-jpeg-sprite images are 100%. Jpeg sprites will have their size applied by javascript + height: 100%; // Non sprite images are 100%. Sprites will have their size applied by JavaScript left: 0; max-height: none; max-width: none; @@ -57,7 +69,7 @@ // Seek time text &__time-container { - bottom: 6px; + bottom: $plyr-preview-time-bottom-offset; left: 0; position: absolute; right: 0; @@ -65,11 +77,11 @@ z-index: 3; span { - background-color: rgba(0, 0, 0, 0.55); - border-radius: 2px; - color: #fff; - font-size: $plyr-font-size-time; - padding: 3px 6px; + background-color: $plyr-preview-time-bg; + border-radius: ($plyr-preview-radius - 1px); + color: $plyr-preview-time-color; + font-size: $plyr-preview-time-font-size; + padding: $plyr-preview-time-padding; } } } @@ -79,7 +91,7 @@ filter: blur(1px); height: 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) opacity: 0; overflow: hidden; position: absolute; @@ -89,6 +101,10 @@ width: 100%; z-index: 1; + &--is-shown { + opacity: 1; + } + img { height: 100%; left: 0; @@ -99,8 +115,4 @@ top: 0; width: 100%; } - - &--is-shown { - opacity: 1; - } } diff --git a/src/sass/settings/sliders.scss b/src/sass/settings/sliders.scss index 86d8de54..6ac053b0 100644 --- a/src/sass/settings/sliders.scss +++ b/src/sass/settings/sliders.scss @@ -6,13 +6,13 @@ $plyr-range-thumb-active-shadow-width: 3px !default; // Thumb -$plyr-range-thumb-height: 14px !default; +$plyr-range-thumb-height: 13px !default; $plyr-range-thumb-bg: #fff !default; $plyr-range-thumb-border: 2px solid transparent !default; $plyr-range-thumb-shadow: 0 1px 1px rgba(#000, 0.15), 0 0 0 1px rgba($plyr-color-gunmetal, 0.2) !default; // Track -$plyr-range-track-height: 4px !default; +$plyr-range-track-height: 5px !default; $plyr-range-max-height: ($plyr-range-thumb-active-shadow-width * 2) + $plyr-range-thumb-height !default; // Fill -- cgit v1.2.3 From 8b57104f8396c4110f217c854099243d8d04ae20 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 26 Jan 2019 17:17:27 +1100 Subject: Docs for preview thumbs --- src/js/config/defaults.js | 1 + src/js/plugins/previewThumbnails.js | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index b12cd8d2..8aaf0166 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -413,6 +413,7 @@ const defaults = { // Preview Thumbnails plugin previewThumbnails: { enabled: false, + src: '', }, }; diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 57b9265f..3832be5c 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -109,20 +109,22 @@ class PreviewThumbnails { // Download VTT files and parse them getThumbnails() { return new Promise(resolve => { - if (!this.player.config.previewThumbnails.src) { + const { src } = this.player.config.previewThumbnails; + + if (is.empty(src)) { throw new Error('Missing previewThumbnails.src config attribute'); } - // previewThumbnails.src can be string or list. If string, convert into single-element list - const { src } = this.player.config.previewThumbnails; + // If string, convert into single-element list const urls = is.string(src) ? [src] : src; - // Loop through each src url. Download and process the VTT file, storing the resulting data in this.thumbnails + // Loop through each src URL. Download and process the VTT file, storing the resulting data in this.thumbnails const promises = urls.map(u => this.getThumbnail(u)); Promise.all(promises).then(() => { // Sort smallest to biggest (e.g., [120p, 480p, 1080p]) this.thumbnails.sort((x, y) => x.height - y.height); + this.player.debug.log('Preview thumbnails', this.thumbnails); resolve(); -- cgit v1.2.3 From 1d51b287014697701b78c883f70c9963f4253d3c Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 26 Jan 2019 22:45:47 +1100 Subject: Tweaks --- src/js/config/defaults.js | 12 ++++++++--- src/js/plugins/previewThumbnails.js | 4 ++-- src/js/plugins/vimeo.js | 28 ++++++++++++++------------ src/js/plugins/youtube.js | 40 ++++++++++++++++++------------------- src/js/plyr.js | 2 +- src/js/utils/events.js | 8 ++++---- 6 files changed, 51 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 650bcd2a..0044d409 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -407,9 +407,6 @@ const defaults = { tagUrl: '', }, - // YouTube nocookies mode - noCookie: false, - // Preview Thumbnails plugin previewThumbnails: { enabled: false, @@ -424,6 +421,15 @@ const defaults = { speed: true, transparent: false, }, + + // YouTube plugin + youtube: { + noCookie: false, // Whether to use an alternative version of YouTube without cookies + rel: 0, // No related vids + showinfo: 0, // Hide info + iv_load_policy: 3, // Hide annotations + modestbranding: 1, // Hide logos as much as possible (they still show one in the corner when paused) + }, }; export default defaults; diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 3832be5c..2bb9cedc 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -223,8 +223,8 @@ class PreviewThumbnails { // Show scrubbing preview on.call(this.player, this.player.elements.progress, 'mousedown touchstart', event => { - // Only act on left mouse button (0), or touch device (!event.button) - if (!event.button || event.button === 0) { + // Only act on left mouse button (0), or touch device (event.button is false) + if (event.button === false || event.button === 0) { this.mouseDown = true; // Wait until media has a duration if (this.player.media.duration) { diff --git a/src/js/plugins/vimeo.js b/src/js/plugins/vimeo.js index 5fcd9ac8..a7664e73 100644 --- a/src/js/plugins/vimeo.js +++ b/src/js/plugins/vimeo.js @@ -10,6 +10,7 @@ import { triggerEvent } from '../utils/events'; import fetch from '../utils/fetch'; import is from '../utils/is'; import loadScript from '../utils/loadScript'; +import { extend } from '../utils/objects'; import { format, stripHTML } from '../utils/strings'; import { setAspectRatio } from '../utils/style'; import { buildUrlParams } from '../utils/urls'; @@ -64,21 +65,22 @@ const vimeo = { // API Ready ready() { const player = this; + const config = player.config.vimeo; // Get Vimeo params for the iframe - const options = { - loop: player.config.loop.active, - autoplay: player.autoplay, - muted: player.muted, - byline: player.config.vimeo.byline, - portrait: player.config.vimeo.portrait, - title: player.config.vimeo.title, - speed: player.config.vimeo.speed, - transparent: player.config.vimeo.transparent === true ? 1 : 0, - gesture: 'media', - playsinline: !this.config.fullscreen.iosNative, - }; - const params = buildUrlParams(options); + const params = buildUrlParams( + extend( + {}, + { + loop: player.config.loop.active, + autoplay: player.autoplay, + muted: player.muted, + gesture: 'media', + playsinline: !this.config.fullscreen.iosNative, + }, + config, + ), + ); // Get the source URL or ID let source = player.media.getAttribute('src'); diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 9d47aa53..0bd232e0 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -9,6 +9,7 @@ import fetch from '../utils/fetch'; import is from '../utils/is'; import loadImage from '../utils/loadImage'; import loadScript from '../utils/loadScript'; +import { extend } from '../utils/objects'; import { format, generateId } from '../utils/strings'; import { setAspectRatio } from '../utils/style'; @@ -144,30 +145,29 @@ const youtube = { }) .catch(() => {}); + const config = player.config.youtube; + // Setup instance // https://developers.google.com/youtube/iframe_api_reference player.embed = new window.YT.Player(id, { videoId, - host: player.config.noCookie ? 'https://www.youtube-nocookie.com' : undefined, - playerVars: { - autoplay: player.config.autoplay ? 1 : 0, // Autoplay - hl: player.config.hl, // iframe interface language - controls: player.supported.ui ? 0 : 1, // Only show controls if not fully supported - rel: 0, // No related vids - showinfo: 0, // Hide info - iv_load_policy: 3, // Hide annotations - modestbranding: 1, // Hide logos as much as possible (they still show one in the corner when paused) - disablekb: 1, // Disable keyboard as we handle it - playsinline: 1, // Allow iOS inline playback - - // Tracking for stats - // origin: window ? `${window.location.protocol}//${window.location.host}` : null, - widget_referrer: window ? window.location.href : null, - - // Captions are flaky on YouTube - cc_load_policy: player.captions.active ? 1 : 0, - cc_lang_pref: player.config.captions.language, - }, + host: config.noCookie ? 'https://www.youtube-nocookie.com' : undefined, + playerVars: extend( + {}, + { + autoplay: player.config.autoplay ? 1 : 0, // Autoplay + hl: player.config.hl, // iframe interface language + controls: player.supported.ui ? 0 : 1, // Only show controls if not fully supported + disablekb: 1, // Disable keyboard as we handle it + playsinline: !player.config.fullscreen.iosNative ? 1 : 0, // Allow iOS inline playback + // Captions are flaky on YouTube + cc_load_policy: player.captions.active ? 1 : 0, + cc_lang_pref: player.config.captions.language, + // Tracking for stats + widget_referrer: window ? window.location.href : null, + }, + config, + ), events: { onError(event) { // YouTube may fire onError twice, so only handle it once diff --git a/src/js/plyr.js b/src/js/plyr.js index 674ffb3c..927eb33d 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -188,7 +188,7 @@ class Plyr { // YouTube requires the playsinline in the URL if (this.isYouTube) { this.config.playsinline = truthy.includes(url.searchParams.get('playsinline')); - this.config.hl = url.searchParams.get('hl'); // TODO: Should this be setting language? + this.config.youtube.hl = url.searchParams.get('hl'); // TODO: Should this be setting language? } else { this.config.playsinline = true; } diff --git a/src/js/utils/events.js b/src/js/utils/events.js index 9f734f04..d304c312 100644 --- a/src/js/utils/events.js +++ b/src/js/utils/events.js @@ -73,10 +73,10 @@ export function off(element, events = '', callback, passive = true, capture = fa // Bind once-only event handler export function once(element, events = '', callback, passive = true, capture = false) { - function onceCallback(...args) { + const onceCallback = (...args) => { off(element, events, onceCallback, passive, capture); callback.apply(this, args); - } + }; toggleListener.call(this, element, events, onceCallback, true, passive, capture); } @@ -114,7 +114,7 @@ export function unbindListeners() { // Run method when / if player is ready export function ready() { - return new Promise( - resolve => (this.ready ? setTimeout(resolve, 0) : on.call(this, this.elements.container, 'ready', resolve)), + return new Promise(resolve => + this.ready ? setTimeout(resolve, 0) : on.call(this, this.elements.container, 'ready', resolve), ).then(() => {}); } -- cgit v1.2.3 From 32e8cce5277c6a55bcdf683558b3357c9c514310 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 27 Jan 2019 01:08:44 +1100 Subject: Fullscreen fix --- src/sass/lib/mixins.scss | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src') diff --git a/src/sass/lib/mixins.scss b/src/sass/lib/mixins.scss index e015ffee..554c66a5 100644 --- a/src/sass/lib/mixins.scss +++ b/src/sass/lib/mixins.scss @@ -65,11 +65,6 @@ width: 100%; } - .plyr__video-embed { - // Revert overflow change - overflow: visible; - } - // Vimeo requires some different styling &.plyr--vimeo .plyr__video-wrapper { height: 0; -- cgit v1.2.3 From fa4868a26da7f433df98fff97f8d0acb7e33ce4a Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 29 Jan 2019 21:33:16 +1100 Subject: Fix listeners for preview thumbs when changing source --- src/js/listeners.js | 36 ++++++++ src/js/plugins/previewThumbnails.js | 169 +++++++++++++++++++----------------- src/js/source.js | 7 +- 3 files changed, 131 insertions(+), 81 deletions(-) (limited to 'src') diff --git a/src/js/listeners.js b/src/js/listeners.js index ae9277a5..68a83d0b 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -740,6 +740,42 @@ class Listeners { controls.updateSeekTooltip.call(player, event), ); + // Preview thumbnails plugin + // TODO: Really need to work on some sort of plug-in wide event bus or pub-sub for this + this.bind(elements.progress, 'mousemove touchmove', event => { + const { previewThumbnails } = player; + + if (previewThumbnails && previewThumbnails.loaded) { + previewThumbnails.startMove(event); + } + }); + + // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering + this.bind(elements.progress, 'mouseleave click', () => { + const { previewThumbnails } = player; + + if (previewThumbnails && previewThumbnails.loaded) { + previewThumbnails.endMove(false, true); + } + }); + + // Show scrubbing preview + this.bind(elements.progress, 'mousedown touchstart', event => { + const { previewThumbnails } = player; + + if (previewThumbnails && previewThumbnails.loaded) { + previewThumbnails.startScrubbing(event); + } + }); + + this.bind(elements.progress, 'mouseup touchend', event => { + const { previewThumbnails } = player; + + if (previewThumbnails && previewThumbnails.loaded) { + previewThumbnails.endScrubbing(event); + } + }); + // Polyfill for lower fill in for webkit if (browser.isWebkit) { Array.from(getElements.call(player, 'input[type="range"]')).forEach(element => { diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 2bb9cedc..6f6b87f5 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -1,5 +1,5 @@ import { createElement } from '../utils/elements'; -import { on, once } from '../utils/events'; +import { once } from '../utils/events'; import fetch from '../utils/fetch'; import is from '../utils/is'; import { formatTime } from '../utils/time'; @@ -72,7 +72,8 @@ class PreviewThumbnails { constructor(player) { this.player = player; this.thumbnails = []; - this.lastMousemoveEventTime = Date.now(); + this.loaded = false; + this.lastMouseMoveTime = Date.now(); this.mouseDown = false; this.loadedImages = []; @@ -81,9 +82,7 @@ class PreviewThumbnails { scrubbing: {}, }; - if (this.enabled) { - this.load(); - } + this.load(); } get enabled() { @@ -91,18 +90,23 @@ class PreviewThumbnails { } load() { - // Turn off the regular seek tooltip - this.player.config.tooltips.seek = false; + // Togglethe regular seek tooltip + if (this.player.elements.display.seekTooltip) { + this.player.elements.display.seekTooltip.hidden = this.enabled; + } - this.getThumbnails().then(() => { - // Initiate DOM listeners so that our preview thumbnails can be used - this.listeners(); + if (!this.enabled) { + return; + } + this.getThumbnails().then(() => { // Render DOM elements this.render(); // Check to see if thumb container size was specified manually in CSS this.determineContainerAutoSizing(); + + this.loaded = true; }); } @@ -165,96 +169,101 @@ class PreviewThumbnails { }); } - /** - * Setup hooks for Plyr and window events - */ - listeners() { - // Mouse hover over seek bar - on.call(this.player, this.player.elements.progress, 'mousemove', event => { - // Wait until media has a duration - if (this.player.media.duration) { - // 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) { - // The mousemove fires for 10+px out to the left - this.seekTime = 0; - } + startMove(event) { + if (!this.loaded) { + return; + } - 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; - } + if (!is.event(event) || !['touchmove', 'mousemove'].includes(event.type)) { + return; + } - this.mousePosX = event.pageX; + // Wait until media has a duration + if (!this.player.media.duration) { + return; + } - // Set time text inside image container - this.elements.thumb.time.innerText = formatTime(this.seekTime); + if (event.type === 'touchmove') { + // Calculate seek hover position as approx video seconds + this.seekTime = this.player.media.duration * (this.player.elements.inputs.seek.value / 100); + } else { + // 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) { + // 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; } - }); - // Touch device seeking - performs same function as above - on.call(this.player, this.player.elements.progress, 'touchmove', () => { + this.mousePosX = event.pageX; + + // Set time text inside image container + this.elements.thumb.time.innerText = formatTime(this.seekTime); + } + + // Download and show image + this.showImageAtCurrentTime(); + } + + endMove() { + this.toggleThumbContainer(false, true); + } + + startScrubbing(event) { + // Only act on left mouse button (0), or touch device (event.button is false) + if (event.button === false || event.button === 0) { + this.mouseDown = true; // 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); + this.toggleScrubbingContainer(true); + this.toggleThumbContainer(false, true); // Download and show image this.showImageAtCurrentTime(); } - }); + } + } - // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering - on.call(this.player, this.player.elements.progress, 'mouseleave click', () => { - this.toggleThumbContainer(false, true); - }); + finishScrubbing() { + this.mouseDown = false; + + // Hide scrubbing preview. But wait until the video has successfully seeked before hiding the scrubbing preview + if (Math.ceil(this.lastTime) === Math.ceil(this.player.media.currentTime)) { + // The video was already seeked/loaded at the chosen time - hide immediately + this.toggleScrubbingContainer(false); + } 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.toggleScrubbingContainer(false); + } + }); + } + } + + /** + * Setup hooks for Plyr and window events + */ + listeners() { + // 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 this.player.on('play', () => { this.toggleThumbContainer(false, true); }); + this.player.on('seeked', () => { this.toggleThumbContainer(false); }); - // Show scrubbing preview - on.call(this.player, this.player.elements.progress, 'mousedown touchstart', event => { - // Only act on left mouse button (0), or touch device (event.button is false) - if (event.button === false || event.button === 0) { - this.mouseDown = true; - // Wait until media has a duration - if (this.player.media.duration) { - this.toggleScrubbingContainer(true); - this.toggleThumbContainer(false, true); - - // Download and show image - 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.toggleScrubbingContainer(false); - } 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.toggleScrubbingContainer(false); - } - }); - } + this.player.on('timeupdate', () => { + this.lastTime = this.player.media.currentTime; }); } diff --git a/src/js/source.js b/src/js/source.js index 337c949c..0173cc9e 100644 --- a/src/js/source.js +++ b/src/js/source.js @@ -125,11 +125,16 @@ const source = { ui.build.call(this); } + // Load HTML5 sources if (this.isHTML5) { - // Load HTML5 sources this.media.load(); } + // Reload thumbnails + if (this.previewThumbnails) { + this.previewThumbnails.load(); + } + // Update the fullscreen support this.fullscreen.update(); }, -- cgit v1.2.3 From c730866efe49def69315c23a158fc90e56ad06cb Mon Sep 17 00:00:00 2001 From: Robert Kraig Date: Wed, 30 Jan 2019 12:05:12 -0800 Subject: Updating accessibility attribute to progressbar aXe complains this is a non-compatible attribute to be set as `role="presentation"` --- src/js/controls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/js/controls.js b/src/js/controls.js index f414f6d6..98939f6d 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -334,7 +334,7 @@ const controls = { min: 0, max: 100, value: 0, - role: 'presentation', + role: 'progressbar', 'aria-hidden': true, }, attributes, -- cgit v1.2.3 From eb628c8e4f109bb6aa4dc9196ee8f075092b225e Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Fri, 1 Feb 2019 00:24:48 +1100 Subject: Ads bug fixes --- src/js/config/defaults.js | 2 +- src/js/listeners.js | 14 -------------- src/js/plugins/ads.js | 19 ++++++++++--------- src/js/plugins/previewThumbnails.js | 2 +- src/js/plyr.js | 7 ++++++- src/js/plyr.polyfilled.js | 2 +- src/js/utils/is.js | 2 ++ 7 files changed, 21 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 0044d409..977e8cce 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.4.8/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.0-beta.3/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/listeners.js b/src/js/listeners.js index 68a83d0b..3c65b824 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -414,20 +414,6 @@ class Listeners { // Loading state on.call(player, player.media, 'waiting canplay seeked playing', event => ui.checkLoading.call(player, event)); - // If autoplay, then load advertisement if required - // TODO: Show some sort of loading state while the ad manager loads else there's a delay before ad shows - on.call(player, player.media, 'playing', () => { - if (!player.ads) { - return; - } - - // If ads are enabled, wait for them first - if (player.ads.enabled && !player.ads.initialized) { - // Wait for manager response - player.ads.managerPromise.then(() => player.ads.play()).catch(() => player.play()); - } - }); - // Click video if (player.supported.ui && player.config.clickToPlay && !player.isAudio) { // Re-fetch the wrapper diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index c643c5dd..74e21d3c 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -136,6 +136,7 @@ class Ads { this.elements.container = createElement('div', { class: this.player.config.classNames.ads, }); + this.player.elements.container.appendChild(this.elements.container); // So we can run VPAID2 @@ -144,9 +145,11 @@ class Ads { // Set language google.ima.settings.setLocale(this.player.config.ads.language); - // We assume the adContainer is the video container of the plyr element - // that will house the ads - this.elements.displayContainer = new google.ima.AdDisplayContainer(this.elements.container); + // Set playback for iOS10+ + google.ima.settings.setDisableCustomPlaybackForIOS10Plus(this.player.config.playsinline); + + // We assume the adContainer is the video container of the plyr element that will house the ads + this.elements.displayContainer = new google.ima.AdDisplayContainer(this.elements.container, this.player.media); // Request video ads to be pre-loaded this.requestAds(); @@ -488,10 +491,8 @@ class Ads { // Ad is stopped this.playing = false; - // Play our video - if (this.player.currentTime < this.player.duration) { - this.player.play(); - } + // Play video + this.player.media.play(); } /** @@ -501,11 +502,11 @@ class Ads { // Show the advertisement container this.elements.container.style.zIndex = 3; - // Ad is playing. + // Ad is playing this.playing = true; // Pause our video. - this.player.pause(); + this.player.media.pause(); } /** diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 6f6b87f5..834d16f2 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -231,7 +231,7 @@ class PreviewThumbnails { } } - finishScrubbing() { + endScrubbing() { this.mouseDown = false; // Hide scrubbing preview. But wait until the video has successfully seeked before hiding the scrubbing preview diff --git a/src/js/plyr.js b/src/js/plyr.js index 927eb33d..26a1ae3b 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.4.8 +// plyr.js v3.5.0-beta.3 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== @@ -353,6 +353,11 @@ class Plyr { return null; } + // Intecept play with ads + if (this.ads && this.ads.enabled) { + this.ads.managerPromise.then(() => this.ads.play()).catch(() => this.media.play()); + } + // Return the promise (for HTML5) return this.media.play(); } diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index 42207a1e..310ce458 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.4.8 +// plyr.js v3.5.0-beta.3 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/utils/is.js b/src/js/utils/is.js index ab28f2ab..b005cd31 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -19,6 +19,7 @@ const isEvent = input => instanceOf(input, Event); const isKeyboardEvent = input => instanceOf(input, KeyboardEvent); const isCue = input => instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); const isTrack = input => instanceOf(input, TextTrack) || (!isNullOrUndefined(input) && isString(input.kind)); +const isPromise = input => instanceOf(input, Promise); const isEmpty = input => isNullOrUndefined(input) || @@ -65,6 +66,7 @@ export default { keyboardEvent: isKeyboardEvent, cue: isCue, track: isTrack, + promise: isPromise, url: isUrl, empty: isEmpty, }; -- cgit v1.2.3 From dbd2136bac1ba3f80c438284628a018f880dc033 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Thu, 7 Feb 2019 23:45:19 +1100 Subject: Fix for cue points missing --- src/js/plugins/ads.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index 74e21d3c..6efd3295 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -240,6 +240,23 @@ class Ads { // Get the cue points for any mid-rolls by filtering out the pre- and post-roll this.cuePoints = this.manager.getCuePoints(); + // Set volume to match player + this.manager.setVolume(this.player.volume); + + // Add listeners to the required events + // Advertisement error events + this.manager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error)); + + // Advertisement regular events + Object.keys(google.ima.AdEvent.Type).forEach(type => { + this.manager.addEventListener(google.ima.AdEvent.Type[type], event => this.onAdEvent(event)); + }); + + // Resolve our adsManager + this.trigger('loaded'); + } + + addCuePoints() { // Add advertisement cue's within the time line if available if (!is.empty(this.cuePoints)) { this.cuePoints.forEach(cuePoint => { @@ -258,21 +275,6 @@ class Ads { } }); } - - // Set volume to match player - this.manager.setVolume(this.player.volume); - - // Add listeners to the required events - // Advertisement error events - this.manager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error)); - - // Advertisement regular events - Object.keys(google.ima.AdEvent.Type).forEach(type => { - this.manager.addEventListener(google.ima.AdEvent.Type[type], event => this.onAdEvent(event)); - }); - - // Resolve our adsManager - this.trigger('loaded'); } /** @@ -412,7 +414,10 @@ class Ads { const { container } = this.player.elements; let time; - // Add listeners to the required events + this.player.on('canplay', () => { + this.addCuePoints(); + }); + this.player.on('ended', () => { this.loader.contentComplete(); }); -- cgit v1.2.3 From 0189e90fce151a94a47d0f3f7bbbc8290c6ad4cd Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 12 Feb 2019 13:55:45 +1100 Subject: Fix deployment --- src/js/config/defaults.js | 2 +- src/js/plyr.js | 2 +- src/js/plyr.polyfilled.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 977e8cce..bd39d615 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.5.0-beta.3/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.0-beta.4/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/plyr.js b/src/js/plyr.js index 26a1ae3b..ebc3d1de 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.5.0-beta.3 +// plyr.js v3.5.0-beta.4 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index 310ce458..a5e25b6b 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.5.0-beta.3 +// plyr.js v3.5.0-beta.4 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== -- cgit v1.2.3 From 153b8dc6bb96fdba8340a523c8828a72a832fcdf Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 19 Feb 2019 00:19:25 +1100 Subject: Added RangeTouch, updated Shr lib in demo --- src/js/controls.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/js/controls.js b/src/js/controls.js index 953ca293..c09a28c5 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -3,6 +3,7 @@ // TODO: This needs to be split into smaller files and cleaned up // ========================================================================== +import RangeTouch from 'rangetouch'; import captions from './captions'; import html5 from './html5'; import support from './support'; @@ -334,6 +335,9 @@ const controls = { // Set the fill for webkit now controls.updateRangeFill.call(this, input); + // Improve support on touch devices + RangeTouch.setup(input); + return input; }, -- cgit v1.2.3 From 44d3a17870949e828e5b1a4619a30dfcb626a174 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 19 Feb 2019 01:17:08 +1100 Subject: Fix links --- src/js/config/defaults.js | 2 +- src/js/plyr.js | 2 +- src/js/plyr.polyfilled.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index bd39d615..a3859638 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.5.0-beta.4/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.0-beta.5/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/plyr.js b/src/js/plyr.js index ebc3d1de..90bf00c8 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.5.0-beta.4 +// plyr.js v3.5.0-beta.5 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index a5e25b6b..92af952e 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.5.0-beta.4 +// plyr.js v3.5.0-beta.5 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== -- cgit v1.2.3 From 80990c98c81fcbd494bc4a14522167d9bd88341a Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 19 Feb 2019 01:25:39 +1100 Subject: Deployed v3.5.0 --- src/js/config/defaults.js | 2 +- src/js/plyr.js | 2 +- src/js/plyr.polyfilled.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index a3859638..6939c66b 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.5.0-beta.5/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.0/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/plyr.js b/src/js/plyr.js index 90bf00c8..b0d3d311 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.5.0-beta.5 +// plyr.js v3.5.0 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index 92af952e..26a1eeae 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.5.0-beta.5 +// plyr.js v3.5.0 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== -- cgit v1.2.3 From 438ebe501320ac731aadcd94e7239a8778407b4a Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 23 Feb 2019 13:07:35 +1100 Subject: Jsdoc updates --- src/js/plugins/ads.js | 16 ++++++------- src/js/plyr.js | 66 +++++++++++++++++++++++++-------------------------- src/js/utils/urls.js | 4 ++-- 3 files changed, 43 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index 6efd3295..c9256b0e 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -17,7 +17,7 @@ import { buildUrlParams } from '../utils/urls'; class Ads { /** * Ads constructor. - * @param {object} player + * @param {Object} player * @return {Ads} */ constructor(player) { @@ -198,7 +198,7 @@ class Ads { /** * Update the ad countdown - * @param {boolean} start + * @param {Boolean} start */ pollCountdown(start = false) { if (!start) { @@ -559,7 +559,7 @@ class Ads { /** * Handles callbacks after an ad event was invoked - * @param {string} event - Event type + * @param {String} event - Event type */ trigger(event, ...args) { const handlers = this.events[event]; @@ -575,8 +575,8 @@ class Ads { /** * Add event listeners - * @param {string} event - Event type - * @param {function} callback - Callback for when event occurs + * @param {String} event - Event type + * @param {Function} callback - Callback for when event occurs * @return {Ads} */ on(event, callback) { @@ -594,8 +594,8 @@ class Ads { * The advertisement has 12 seconds to get its things together. We stop this timer when the * advertisement is playing, or when a user action is required to start, then we clear the * timer on ad ready - * @param {number} time - * @param {string} from + * @param {Number} time + * @param {String} from */ startSafetyTimer(time, from) { this.player.debug.log(`Safety timer invoked from: ${from}`); @@ -608,7 +608,7 @@ class Ads { /** * Clear our safety timer(s) - * @param {string} from + * @param {String} from */ clearSafetyTimer(from) { if (!is.nullOrUndefined(this.safetyTimer)) { diff --git a/src/js/plyr.js b/src/js/plyr.js index ebc3d1de..41a249bb 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -403,7 +403,7 @@ class Plyr { /** * Toggle playback based on current status - * @param {boolean} input + * @param {Boolean} input */ togglePlay(input) { // Toggle based on current state if nothing passed @@ -437,7 +437,7 @@ class Plyr { /** * Rewind - * @param {number} seekTime - how far to rewind in seconds. Defaults to the config.seekTime + * @param {Number} seekTime - how far to rewind in seconds. Defaults to the config.seekTime */ rewind(seekTime) { this.currentTime = this.currentTime - (is.number(seekTime) ? seekTime : this.config.seekTime); @@ -445,7 +445,7 @@ class Plyr { /** * Fast forward - * @param {number} seekTime - how far to fast forward in seconds. Defaults to the config.seekTime + * @param {Number} seekTime - how far to fast forward in seconds. Defaults to the config.seekTime */ forward(seekTime) { this.currentTime = this.currentTime + (is.number(seekTime) ? seekTime : this.config.seekTime); @@ -453,7 +453,7 @@ class Plyr { /** * Seek to a time - * @param {number} input - where to seek to in seconds. Defaults to 0 (the start) + * @param {Number} input - where to seek to in seconds. Defaults to 0 (the start) */ set currentTime(input) { // Bail if media duration isn't available yet @@ -523,7 +523,7 @@ class Plyr { /** * Set the player volume - * @param {number} value - must be between 0 and 1. Defaults to the value from local storage and config.volume if not set in storage + * @param {Number} value - must be between 0 and 1. Defaults to the value from local storage and config.volume if not set in storage */ set volume(value) { let volume = value; @@ -574,7 +574,7 @@ class Plyr { /** * Increase volume - * @param {boolean} step - How much to decrease by (between 0 and 1) + * @param {Boolean} step - How much to decrease by (between 0 and 1) */ increaseVolume(step) { const volume = this.media.muted ? 0 : this.volume; @@ -583,7 +583,7 @@ class Plyr { /** * Decrease volume - * @param {boolean} step - How much to decrease by (between 0 and 1) + * @param {Boolean} step - How much to decrease by (between 0 and 1) */ decreaseVolume(step) { this.increaseVolume(-step); @@ -591,7 +591,7 @@ class Plyr { /** * Set muted state - * @param {boolean} mute + * @param {Boolean} mute */ set muted(mute) { let toggle = mute; @@ -643,7 +643,7 @@ class Plyr { /** * Set playback speed - * @param {number} speed - the speed of playback (0.5-2.0) + * @param {Number} speed - the speed of playback (0.5-2.0) */ set speed(input) { let speed = null; @@ -690,7 +690,7 @@ class Plyr { /** * Set playback quality * Currently HTML5 & YouTube only - * @param {number} input - Quality level + * @param {Number} input - Quality level */ set quality(input) { const config = this.config.quality; @@ -740,7 +740,7 @@ class Plyr { /** * Toggle loop * TODO: Finish fancy new logic. Set the indicator on load as user may pass loop as config - * @param {boolean} input - Whether to loop or not + * @param {Boolean} input - Whether to loop or not */ set loop(input) { const toggle = is.boolean(input) ? input : this.config.loop.active; @@ -800,7 +800,7 @@ class Plyr { /** * Set new media source - * @param {object} input - The new source object (see docs) + * @param {Object} input - The new source object (see docs) */ set source(input) { source.change.call(this, input); @@ -824,7 +824,7 @@ class Plyr { /** * Set the poster image for a video - * @param {input} - the URL for the new poster image + * @param {String} input - the URL for the new poster image */ set poster(input) { if (!this.isVideo) { @@ -848,7 +848,7 @@ class Plyr { /** * Set the autoplay state - * @param {boolean} input - Whether to autoplay or not + * @param {Boolean} input - Whether to autoplay or not */ set autoplay(input) { const toggle = is.boolean(input) ? input : this.config.autoplay; @@ -864,7 +864,7 @@ class Plyr { /** * Toggle captions - * @param {boolean} input - Whether to enable captions + * @param {Boolean} input - Whether to enable captions */ toggleCaptions(input) { captions.toggle.call(this, input, false); @@ -872,7 +872,7 @@ class Plyr { /** * Set the caption track by index - * @param {number} - Caption index + * @param {Number} - Caption index */ set currentTrack(input) { captions.set.call(this, input, false); @@ -889,7 +889,7 @@ class Plyr { /** * Set the wanted language for captions * Since tracks can be added later it won't update the actual caption track until there is a matching track - * @param {string} - Two character ISO language code (e.g. EN, FR, PT, etc) + * @param {String} - Two character ISO language code (e.g. EN, FR, PT, etc) */ set language(input) { captions.setLanguage.call(this, input, false); @@ -962,7 +962,7 @@ class Plyr { /** * Toggle the player controls - * @param {boolean} [toggle] - Whether to show the controls + * @param {Boolean} [toggle] - Whether to show the controls */ toggleControls(toggle) { // Don't toggle if missing UI support or if it's audio @@ -995,8 +995,8 @@ class Plyr { /** * Add event listeners - * @param {string} event - Event type - * @param {function} callback - Callback for when event occurs + * @param {String} event - Event type + * @param {Function} callback - Callback for when event occurs */ on(event, callback) { on.call(this, this.elements.container, event, callback); @@ -1004,8 +1004,8 @@ class Plyr { /** * Add event listeners once - * @param {string} event - Event type - * @param {function} callback - Callback for when event occurs + * @param {String} event - Event type + * @param {Function} callback - Callback for when event occurs */ once(event, callback) { once.call(this, this.elements.container, event, callback); @@ -1013,8 +1013,8 @@ class Plyr { /** * Remove event listeners - * @param {string} event - Event type - * @param {function} callback - Callback for when event occurs + * @param {String} event - Event type + * @param {Function} callback - Callback for when event occurs */ off(event, callback) { off(this.elements.container, event, callback); @@ -1024,8 +1024,8 @@ class Plyr { * Destroy an instance * Event listeners are removed when elements are removed * http://stackoverflow.com/questions/12528049/if-a-dom-element-is-removed-are-its-listeners-also-removed-from-memory - * @param {function} callback - Callback for when destroy is complete - * @param {boolean} soft - Whether it's a soft destroy (for source changes etc) + * @param {Function} callback - Callback for when destroy is complete + * @param {Boolean} soft - Whether it's a soft destroy (for source changes etc) */ destroy(callback, soft = false) { if (!this.ready) { @@ -1124,7 +1124,7 @@ class Plyr { /** * Check for support for a mime type (HTML5 only) - * @param {string} type - Mime type + * @param {String} type - Mime type */ supports(type) { return support.mime.call(this, type); @@ -1132,9 +1132,9 @@ class Plyr { /** * Check for support - * @param {string} type - Player type (audio/video) - * @param {string} provider - Provider (html5/youtube/vimeo) - * @param {bool} inline - Where player has `playsinline` sttribute + * @param {String} type - Player type (audio/video) + * @param {String} provider - Provider (html5/youtube/vimeo) + * @param {Boolean} inline - Where player has `playsinline` sttribute */ static supported(type, provider, inline) { return support.check(type, provider, inline); @@ -1142,8 +1142,8 @@ class Plyr { /** * Load an SVG sprite into the page - * @param {string} url - URL for the SVG sprite - * @param {string} [id] - Unique ID + * @param {String} url - URL for the SVG sprite + * @param {String} [id] - Unique ID */ static loadSprite(url, id) { return loadSprite(url, id); @@ -1152,7 +1152,7 @@ class Plyr { /** * Setup multiple instances * @param {*} selector - * @param {object} options + * @param {Object} options */ static setup(selector, options = {}) { let targets = null; diff --git a/src/js/utils/urls.js b/src/js/utils/urls.js index 3ebe622e..843c6aa6 100644 --- a/src/js/utils/urls.js +++ b/src/js/utils/urls.js @@ -6,8 +6,8 @@ import is from './is'; /** * Parse a string to a URL object - * @param {string} input - the URL to be parsed - * @param {boolean} safe - failsafe parsing + * @param {String} input - the URL to be parsed + * @param {Boolean} safe - failsafe parsing */ export function parseUrl(input, safe = true) { let url = input; -- cgit v1.2.3 From 215fc3677af16e08ffa351b20a4b5b15719df307 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sat, 23 Feb 2019 13:14:01 +1100 Subject: v3.5.1 --- src/js/config/defaults.js | 2 +- src/js/plyr.js | 2 +- src/js/plyr.polyfilled.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 6939c66b..a1eb50a7 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.5.0/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.1/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/plyr.js b/src/js/plyr.js index 8317a4d5..c9b51423 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.5.0 +// plyr.js v3.5.1 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index 26a1eeae..d9a8c0b1 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.5.0 +// plyr.js v3.5.1 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== -- cgit v1.2.3 From 4c3bf25b8a6228b8b48a28f351df1ae2523e6d19 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Sun, 24 Feb 2019 12:10:20 +1100 Subject: Fixed issue where the preview thumbnail was present while scrubbing --- src/js/config/defaults.js | 2 +- src/js/plugins/previewThumbnails.js | 7 +++++-- src/js/plyr.js | 2 +- src/js/plyr.polyfilled.js | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index a1eb50a7..82809511 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -60,7 +60,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.5.1/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.2/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/previewThumbnails.js index 834d16f2..bd7a6bbd 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/previewThumbnails.js @@ -220,6 +220,7 @@ class PreviewThumbnails { // Only act on left mouse button (0), or touch device (event.button is false) if (event.button === false || event.button === 0) { this.mouseDown = true; + // Wait until media has a duration if (this.player.media.duration) { this.toggleScrubbingContainer(true); @@ -307,7 +308,6 @@ class PreviewThumbnails { if (this.mouseDown) { this.setScrubbingContainerSize(); } else { - this.toggleThumbContainer(true); this.setThumbContainerSizeAndPos(); } @@ -319,7 +319,10 @@ class PreviewThumbnails { const hasThumb = thumbNum >= 0; let qualityIndex = 0; - this.toggleThumbContainer(hasThumb); + // Show the thumb container if we're not scrubbing + if (!this.mouseDown) { + this.toggleThumbContainer(hasThumb); + } // No matching thumb found if (!hasThumb) { diff --git a/src/js/plyr.js b/src/js/plyr.js index c9b51423..0d3d1674 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.5.1 +// plyr.js v3.5.2 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index d9a8c0b1..8623e41a 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.5.1 +// plyr.js v3.5.2 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== -- cgit v1.2.3 From e4acff4f8de2527e5a8f585fc46569347d7a8cf1 Mon Sep 17 00:00:00 2001 From: Arslan Javed Date: Thu, 7 Mar 2019 12:54:07 +0500 Subject: Update poster src --- src/js/plugins/youtube.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 0bd232e0..d5972c80 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -130,7 +130,7 @@ const youtube = { player.media = replaceElement(container, player.media); // Id to poster wrapper - const posterSrc = format => `https://img.youtube.com/vi/${videoId}/${format}default.jpg`; + const posterSrc = format => `https://i.ytimg.com/vi/${videoId}/${format}default.jpg`; // Check thumbnail images in order of quality, but reject fallback thumbnails (120px wide) loadImage(posterSrc('maxres'), 121) // Higest quality and unpadded -- cgit v1.2.3