import React, { Component } from 'react' import { connect } from 'react-redux' import actions from 'app/actions' import { clamp, timestamp } from 'app/utils' import { PlayButton, VolumeControl } from 'app/views/viewer/nav/viewer.icons' class VideoScrubber extends Component { state = { hovering: false, } constructor(props) { super(props) this.scrubberRef = React.createRef() this.handleMouseDown = this.handleMouseDown.bind(this) this.handleMouseMove = this.handleMouseMove.bind(this) this.handleMouseUp = this.handleMouseUp.bind(this) this.handleMouseEnter = this.handleMouseEnter.bind(this) this.handleMouseLeave = this.handleMouseLeave.bind(this) } componentDidMount() { window.addEventListener('mousemove', this.handleMouseMove) window.addEventListener('mouseup', this.handleMouseUp) } componentWillUnmount() { window.removeEventListener('mousemove', this.handleMouseMove) window.removeEventListener('mouseup', this.handleMouseUp) } scrub(x, scrubbing) { const { timing, start_ts, onScrub } = this.props const bounds = this.scrubberRef.current.getBoundingClientRect() const percent = clamp((x - bounds.left) / bounds.width, 0, 1) const seconds = percent * timing.duration actions.audio.seek(start_ts + seconds) onScrub({ seek: seconds, percent, seconds, scrubbing }) } handleMouseDown(e) { e.stopPropagation() this.scrub(e.pageX, true) } handleMouseMove(e) { e.stopPropagation() if (!this.props.timing.scrubbing) return this.scrub(e.pageX, true) } handleMouseUp(e) { e.stopPropagation() this.props.onScrub({ scrubbing: false }) } handleMouseEnter() { this.setState({ hovering: true }) } handleMouseLeave() { this.setState({ hovering: false }) } render() { const { playing, volume, timing } = this.props const { hovering } = this.state const timestampText = timestamp(clamp(timing.seconds, 0, timing.duration)) return (