import React, { Component } from 'react' import { connect } from 'react-redux' import actions from 'app/actions' import { floatInRange, clamp } from 'app/utils' import { preloadSectionImages } from 'app/utils/image.utils' import PlayerTranscript from './player.transcript' import PlayerFullscreen from './player.fullscreen' import VideoModal from '../modals/modals.video' class PlayerContainer extends Component { constructor(props) { super(props) this.handleKeyDown = this.handleKeyDown.bind(this) } componentDidMount() { // console.log(this.props.sections) const { sections } = this.props actions.viewer.setCurrentSection(sections[0], sections[1]) document.addEventListener('keydown', this.handleKeyDown) } componentWillUnmount() { document.removeEventListener('keydown', this.handleKeyDown) } handleKeyDown(e) { if (document.activeElement !== document.body) { return } const { currentSection, audio, viewer } = this.props const { play_ts } = audio let start_ts = 0 let end_ts = 0 if (currentSection) { start_ts = currentSection.start_ts end_ts = currentSection.end_ts } // console.log(e.keyCode) switch (e.keyCode) { case 32: // spacebar e.preventDefault() actions.audio.toggle() break case 27: e.preventDefault() if (viewer.vitrineModal.open) { actions.viewer.closeVitrineModal() } break case 37: // left case 38: // up e.preventDefault() if (viewer.vitrineModal.open) { actions.viewer.vitrineGo(-1) } else { actions.audio.seek(clamp(play_ts - 5.0, start_ts, end_ts)) } break case 39: // right case 40: // down e.preventDefault() if (viewer.vitrineModal.open) { actions.viewer.vitrineGo(+1) } else { actions.audio.seek(clamp(play_ts + 5.0, start_ts, end_ts)) } break } } componentDidUpdate(prevProps) { if (this.props.audio.play_ts !== prevProps.audio.play_ts) { this.handleTimeUpdate() } if (this.props.currentSection !== prevProps.currentSection) { this.preloader && this.preloader.cancel() this.preloader = preloadSectionImages(this.props.currentSection, this.props.mediaLookup) } } handleTimeUpdate() { const { audio, currentSection, autoAdvance } = this.props const { play_ts } = audio // const { play_ts, seek_ts } = audio // const didSeek = floatEQ(play_ts, seek_ts) const inCurrentSection = floatInRange(currentSection.start_ts, play_ts, currentSection.end_ts) // console.log('inCurrentSection?', inCurrentSection) // console.log('didSeek?', didSeek) // if the current TS isn't in the same section as the current one... // console.log(currentSection.start_ts, play_ts, currentSection.end_ts) if (inCurrentSection) { return } // at end of section () if (autoAdvance) { actions.viewer.setSectionFromTimestamp(play_ts) } else { console.log(">> Reached end of section") actions.viewer.reachedEndOfSection(currentSection) } } render() { if (!this.props.currentSection) { return
} // console.log(currentSection) return (