summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/player/player.container.js
blob: 26697f9ca9e60fa0b34ed17dd1d4cd0479ad0569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { Component } from 'react'
import { connect } from 'react-redux'

import actions from 'app/actions'
import { floatEQ, floatInRange, clamp } from 'app/utils'

import PlayerTranscript from './player.transcript'
import PlayerFullscreen from './player.fullscreen'

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) return
    this.handleTimeUpdate()
  }

  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...
    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() {
    // const { } = this.props
    const { currentSection } = this.props
    if (!currentSection) { return <div /> }
    // console.log(currentSection)
    return (
      <div className='viewer-container'>
        <PlayerTranscript section={currentSection} />
        <PlayerFullscreen />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  viewer: state.viewer,
  audio: state.audio,
  sections: state.viewer.sections,
  currentSection: state.viewer.currentSection,
  autoAdvance: state.viewer.autoAdvance,
})

export default connect(mapStateToProps)(PlayerContainer)