Clean up code and add better comments
This commit is contained in:
@@ -158,16 +158,18 @@ module.exports = class PlaybackController {
|
|||||||
else this.state.playing.jumpToTime = time
|
else this.state.playing.jumpToTime = time
|
||||||
}
|
}
|
||||||
|
|
||||||
preview (time, x) {
|
// Show video preview
|
||||||
if (!Number.isFinite(time)) {
|
preview (x) {
|
||||||
console.error('Tried to skip to a non-finite time ' + time)
|
if (!Number.isFinite(x)) {
|
||||||
|
console.error('Tried to preview a non-finite position ' + x)
|
||||||
return console.trace()
|
return console.trace()
|
||||||
}
|
}
|
||||||
this.state.playing.preview = { time, x }
|
this.state.playing.previewXCoord = x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hide video preview
|
||||||
clearPreview () {
|
clearPreview () {
|
||||||
this.state.playing.preview = null
|
this.state.playing.previewXCoord = null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change playback speed. 1 = faster, -1 = slower
|
// Change playback speed. 1 = faster, -1 = slower
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ const dispatchHandlers = {
|
|||||||
previousTrack: () => controllers.playback().previousTrack(),
|
previousTrack: () => controllers.playback().previousTrack(),
|
||||||
skip: (time) => controllers.playback().skip(time),
|
skip: (time) => controllers.playback().skip(time),
|
||||||
skipTo: (time) => controllers.playback().skipTo(time),
|
skipTo: (time) => controllers.playback().skipTo(time),
|
||||||
preview: (time, x) => controllers.playback().preview(time, x),
|
preview: (x) => controllers.playback().preview(x),
|
||||||
clearPreview: () => controllers.playback().clearPreview(),
|
clearPreview: () => controllers.playback().clearPreview(),
|
||||||
changePlaybackRate: (dir) => controllers.playback().changePlaybackRate(dir),
|
changePlaybackRate: (dir) => controllers.playback().changePlaybackRate(dir),
|
||||||
changeVolume: (delta) => controllers.playback().changeVolume(delta),
|
changeVolume: (delta) => controllers.playback().changeVolume(delta),
|
||||||
|
|||||||
@@ -728,12 +728,10 @@ function renderPlayerControls (state) {
|
|||||||
|
|
||||||
// Handles a scrub hover (preview another position in the video)
|
// Handles a scrub hover (preview another position in the video)
|
||||||
function handleScrubPreview (e) {
|
function handleScrubPreview (e) {
|
||||||
|
// Only show for videos
|
||||||
if (!e.clientX || state.playing.type !== 'video') return
|
if (!e.clientX || state.playing.type !== 'video') return
|
||||||
dispatch('mediaMouseMoved')
|
dispatch('mediaMouseMoved')
|
||||||
const windowWidth = document.querySelector('body').clientWidth
|
dispatch('preview', e.clientX)
|
||||||
const fraction = e.clientX / windowWidth
|
|
||||||
const position = fraction * state.playing.duration /* seconds */
|
|
||||||
dispatch('preview', position, e.clientX)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearPreview (e) {
|
function clearPreview (e) {
|
||||||
@@ -779,27 +777,36 @@ function renderPlayerControls (state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderPreview (state) {
|
function renderPreview (state) {
|
||||||
let { preview } = state.playing
|
const { previewXCoord = null } = state.playing
|
||||||
if (!preview) {
|
|
||||||
preview = { x: 0, time: 0, hide: true }
|
// Calculate time from x-coord as fraction of track width
|
||||||
}
|
const windowWidth = document.querySelector('body').clientWidth
|
||||||
|
const fraction = previewXCoord / windowWidth
|
||||||
|
const time = fraction * state.playing.duration /* seconds */
|
||||||
|
|
||||||
const height = 70
|
const height = 70
|
||||||
let width
|
let width = 0
|
||||||
|
|
||||||
const previewEl = document.querySelector('video#preview')
|
const previewEl = document.querySelector('video#preview')
|
||||||
if (previewEl !== null && !preview.hide) {
|
if (previewEl !== null && previewXCoord !== null) {
|
||||||
previewEl.currentTime = preview.time
|
previewEl.currentTime = time
|
||||||
|
|
||||||
|
// Auto adjust width to maintain video aspect ratio
|
||||||
width = Math.floor((previewEl.videoWidth / previewEl.videoHeight) * height)
|
width = Math.floor((previewEl.videoWidth / previewEl.videoHeight) * height)
|
||||||
}
|
}
|
||||||
|
|
||||||
const windowWidth = document.querySelector('body').clientWidth
|
// Center preview window on mouse cursor,
|
||||||
const xPos = Math.min(Math.max(preview.x - (width / 2), 5), windowWidth - width - 5)
|
// while avoiding falling off the left or right edges
|
||||||
|
const xPos = Math.min(Math.max(previewXCoord - (width / 2), 5), windowWidth - width - 5)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key='preview'
|
key='preview' style={{
|
||||||
style={{ position: 'absolute', bottom: 50, left: xPos, display: preview.hide && 'none' }}
|
position: 'absolute',
|
||||||
|
bottom: 50,
|
||||||
|
left: xPos,
|
||||||
|
display: previewXCoord == null && 'none' // Hide preview when XCoord unset
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<div style={{ width, height, backgroundColor: 'black' }}>
|
<div style={{ width, height, backgroundColor: 'black' }}>
|
||||||
<video
|
<video
|
||||||
@@ -813,7 +820,7 @@ function renderPreview (state) {
|
|||||||
textAlign: 'center', margin: 5, textShadow: '0 0 2px rgba(0,0,0,.5)', color: '#eee'
|
textAlign: 'center', margin: 5, textShadow: '0 0 2px rgba(0,0,0,.5)', color: '#eee'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{formatTime(preview.time, state.playing.duration)}
|
{formatTime(time, state.playing.duration)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user