summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/align/containers/timeline.container.js
blob: a924eaf48b31a346e0a6b81a6e14b7b0f57092c5 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { Component } from 'react'
// import { Link } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import actions from '../../../actions'
// import * as alignActions from '../align.actions'

import Annotations from '../containers/annotations.container'
import Waveform from '../components/timeline/waveform.component'
import Ticks from '../components/timeline/ticks.component'
import Cursor from '../components/timeline/cursor.component'
import PlayButton from '../components/player/playButton.component'
import PlayCursor from '../components/timeline/playCursor.component'

import { WAVEFORM_SIZE, ZOOM_STEPS, INNER_HEIGHT } from '../constants'
import { clamp } from '../../../util'
import { positionToTime } from '../align.util'

class Timeline extends Component {
  constructor(props){
    super(props)
    this.handleKeydown = this.handleKeydown.bind(this)
    this.handleMouseMove = this.handleMouseMove.bind(this)
    this.handleWheel = this.handleWheel.bind(this)
    this.handleContainerClick = this.handleContainerClick.bind(this)
    this.handleTimelineClick = this.handleTimelineClick.bind(this)
  }
  componentDidMount() {
    this.bind()
  }
  componentWillUnmount() {
    this.unbind()
  }
  shouldComponentUpdate(nextProps) {
    return (
      nextProps.timeline !== this.props.timeline ||
      nextProps.annotation !== this.props.annotation
    )
  }
  bind() {
    document.addEventListener('keydown', this.handleKeydown)
  }
  unbind() {
    document.removeEventListener('keydown', this.handleKeydown)
  }
  handleKeydown(e) {
    if (document.activeElement !== document.body) {
      return
    }
    // console.log(e.keyCode)
    if (e.metaKey && this.props.selectedAnnotation.id) {
      const { selectedAnnotation } = this.props
      switch (e.keyCode) {
        case 38: // up
          e.preventDefault()
          selectedAnnotation.start_ts = Math.max(selectedAnnotation.start_ts - (e.shiftKey ? 1 : 0.1), 0)
          actions.align.updateSelectedAnnotation(selectedAnnotation)
          actions.audio.seek(selectedAnnotation.start_ts)
          actions.align.setCursor(selectedAnnotation.start_ts)
          break
        case 40: // down
          e.preventDefault()
          selectedAnnotation.start_ts += e.shiftKey ? 1 : 0.1
          actions.align.updateSelectedAnnotation(selectedAnnotation)
          actions.audio.seek(selectedAnnotation.start_ts)
          actions.align.setCursor(selectedAnnotation.start_ts)
          break
      }
      return
    }
    if (e.shiftKey) {
      switch (e.keyCode) {
        case 187: // plus
          actions.align.setZoom(this.props.timeline.zoom - 1)
          break
        case 189: // minus
          actions.align.setZoom(this.props.timeline.zoom + 1)
          break
      }
    } else {
      // console.log(e.keyCode)
      switch (e.keyCode) {
        case 27: // escape
          actions.align.hideAnnotationForm()
          break
        case 65: // A - add
          e.preventDefault()
          actions.align.showNewAnnotationForm(this.props.audio.play_ts, this.props.text)
          break
        case 32: // spacebar
          actions.audio.toggle()
          break
        case 38: // up
          actions.audio.jump(- ZOOM_STEPS[this.props.timeline.zoom] * 0.1)
          break
        case 40: // down
          actions.audio.jump(ZOOM_STEPS[this.props.timeline.zoom] * 0.1)
          break
      }
    }
  }
  handleWheel(e) {
    let { start_ts, zoom, duration } = this.props.timeline
    let { deltaY } = e
    let secondsPerPixel = ZOOM_STEPS[zoom] * 0.1 // 0.1 sec / step
    let widthTimeDuration = INNER_HEIGHT * secondsPerPixel // secs per pixel
    start_ts += Math.round((deltaY) * ZOOM_STEPS[zoom])
    start_ts = clamp(start_ts, 0, Math.max(0, duration - widthTimeDuration / 2))
    if (e.shiftKey) {
      if (Math.abs(deltaY) < 2) return
      if (e.deltaY > 0) {
        actions.align.throttledSetZoom(this.props.timeline.zoom + 1)
      } else {
        actions.align.throttledSetZoom(this.props.timeline.zoom - 1)
      }
    } else if (e.altKey) {
      actions.audio.jump(e.deltaY * ZOOM_STEPS[zoom])
    } else {
      actions.align.setScrollPosition(start_ts)
    }
  }
  handleMouseMove(e) {
    const cursor_ts = positionToTime(e.pageY, this.props.timeline)
    actions.align.setCursor(cursor_ts)
  }
  handleContainerClick(e) {
    actions.align.clearSelectedAnnotation()
    actions.align.clearSelectedParagraph()
  }
  handleTimelineClick(e) {
    const play_ts = positionToTime(e.pageY, this.props.timeline)
    if (e.pageX < WAVEFORM_SIZE * 0.67) {
      actions.audio.seek(play_ts)      
    } else {
      actions.align.showNewAnnotationForm(play_ts, this.props.text)
    }
  }
  render() {
    return (
      <div
        className='timeline'
        onClick={this.handleContainerClick}
        onWheel={this.handleWheel}
        onMouseMove={this.handleMouseMove}
      >
        <div className='timelineColumn'>
          <Waveform onClick={this.handleTimelineClick} />
          <Ticks timeline={this.props.timeline} />
          <Cursor timeline={this.props.timeline} annotation={this.props.annotation} />
        </div>
        <Annotations timeline={this.props.timeline} />
        <PlayCursor />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  timeline: state.align.timeline,
  annotation: state.align.annotation,
  selectedAnnotation: state.align.selectedAnnotation,
  audio: state.audio,
  text: state.site.text,
})

const mapDispatchToProps = dispatch => ({
  // alignActions: bindActionCreators({ ...alignActions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(Timeline)