summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/transcript/transcript.container.js
blob: f5c6c034e8627f01cd48c99c28140b9473b80e4f (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, { Component } from 'react'
import { connect } from 'react-redux'

import actions from 'app/actions'

import { toArray, floatInRange } from 'app/utils'
import oktween from 'app/utils/oktween'
import ParagraphList from 'app/views/editor/paragraph/components/paragraph.list'
import { transcriptElementLookup } from './components'

class Transcript extends Component {
  state = {
    built: false,
    paragraphs: [],
    windowHeight: 0.0,
    autoscrolling: false,
  }

  constructor(props){
    super(props)
    this.handleClose = this.handleClose.bind(this)
    this.handleAnnotationClick = this.handleAnnotationClick.bind(this)
    this.handleParagraphDoubleClick = this.handleParagraphDoubleClick.bind(this)
    this.handleMouseWheel = this.handleMouseWheel.bind(this)
    this.containerRef = React.createRef()
    this.lastScroll = 0
  }

  componentDidMount() {
    actions.transcript.buildAllParagraphs()
    this.setState({
      built: true,
      windowHeight: window.innerHeight * 0.6 - 48,
    })
  }

  componentDidUpdate(prevProps, prevState) {
    if (this.state.built !== prevState.built) {
      setTimeout(() => {
        this.cacheScrollPositions()
      }, 1000)
    }
    if (!this.state.autoscrolling) {
      if (this.props.viewer.currentSection !== prevProps.viewer.currentSection) {
        this.updateScrollPosition(this.props.viewer.currentSection.start_ts, true)
      } else {
        this.updateScrollPosition(this.props.play_ts, false)
      }
    }
  }

  cacheScrollPositions() {
    let paragraphs = toArray(this.containerRef.current.querySelectorAll('.content > div')).map(el => {
      const isHeading = el.classList.contains('section_heading')
      const isMedia = el.dataset.media === 'true'
      const scrollTop = isHeading ? el.offsetTop : el.offsetTop - 16 // 1.5rem
      const start_ts = parseFloat(el.dataset.startts)
      let end_ts = parseFloat(el.dataset.endts)
      if (!start_ts || !end_ts) return null
      if (end_ts < start_ts) {
        end_ts = start_ts + 0.5
      }
      return { start_ts, end_ts, scrollTop }
    }).filter(el => !!el)
    this.setState({ paragraphs })
  }

  updateScrollPosition(play_ts, forceScroll) {
    if (this.state.autoscrolling) return
    const { paragraphs, windowHeight, currentParagraph } = this.state
    const scrollTop = this.containerRef.current.scrollTop
    const now = Date.now()
    if (!forceScroll && currentParagraph && floatInRange(currentParagraph.start_ts, play_ts, currentParagraph.end_ts)) {
      return
    }
    let nextParagraph;
    const insideParagraph = paragraphs.some(paragraph => {
      if (floatInRange(paragraph.start_ts, play_ts, paragraph.end_ts)) {
        nextParagraph = paragraph
        return true
      }
      return false
    })
    // the paragraph updated!
    if (insideParagraph && nextParagraph) {
      // first check if we need to scroll.
      // if we're not being forced and the next paragraph is in view, dont scroll
      if (!forceScroll && floatInRange(scrollTop, nextParagraph.scrollTop, scrollTop + windowHeight)) {
        this.setState({ currentParagraph: nextParagraph })
        return
      }
      // ok, so we wanna scroll.
      // is the transcript closed? then we can scroll immediately!
      if (!this.props.viewer.transcript) {
        this.setState({ currentParagraph: nextParagraph })
        this.scrollImmediate(nextParagraph.scrollTop)
        return
      }

      // console.log(nextParagraph.scrollTop)
      // if the last scroll event was within a second ago, suppress auto-scroll to avoid it bouncing around
      if ((now - this.lastScroll) < 1200) {
        this.setState({ currentParagraph: nextParagraph })
        return
      }
      // before i was checking this criteria.. if the distance we wanna scroll is less than a window height
      // Math.abs(scrollTop - nextParagraph.scrollTop) < this.state.windowHeight
      this.lastScroll = now
      this.scrollToParagraph(scrollTop, nextParagraph.scrollTop)
      this.setState({ currentParagraph: nextParagraph, autoscrolling: true })
    }
  }

  scrollToParagraph(scrollFrom, scrollTo) {
    // suppress if already scrolling
    if (this.state.autoscrolling) return
    // console.log('autoscrolling!', scrollFrom, scrollTo)
    oktween.add({
      from: { scrollTop: scrollFrom },
      to: { scrollTop: scrollTo },
      duration: 1000,
      easing: oktween.easing.quad_in_out,
      update: obj => {
        this.scrollImmediate(obj.scrollTop)
        this.containerRef.current.scrollTo(0, obj.scrollTop)
      },
      finished: tween => {
        this.scrollImmediate(tween.obj.scrollTop)
        this.setState({ autoscrolling: false })
      }
    })
  }

  scrollImmediate(pos) {
    this.containerRef.current.scrollTo(0, pos)
  }

  handleAnnotationClick(e, paragraph, annotation) {
    if (annotation.type === 'footnote') {
      return actions.viewer.openFootnote(annotation)
    }
    actions.viewer.seekToTimestamp(annotation.start_ts)
    this.updateScrollPosition(annotation.start_ts + 0.1, annotation.type === 'section_heading')
  }
  
  handleParagraphDoubleClick(e, paragraph) {
    return
  }
  
  handleClose() {
    actions.viewer.hideComponent('transcript')
  }

  handleMouseWheel(e) {
    e.stopPropagation()
    this.lastScroll = Date.now()
  }

  render() {
    const { viewer, paragraphs } = this.props
    // console.log(paragraphs.map(p => p.annotations.filter(a => a.type === 'footnote')).filter(p => p.length))
    return (
      <div className="transcript">
        <div
          className='content'
          ref={this.containerRef}
          onWheel={this.handleMouseWheel}
        >
          <ParagraphList
            paragraphs={paragraphs}
            paragraphElementLookup={transcriptElementLookup}
            onAnnotationClick={this.handleAnnotationClick}
            onParagraphDoubleClick={this.handleParagraphDoubleClick}
          />
        </div>
        <div
          className='close'
          onClick={this.handleClose}
        />
      </div>
    )
  }
}

/*
  <a className='footer' href='#'>
    Download PDF
  </a>
*/

const mapStateToProps = state => ({
  viewer: state.viewer,
  play_ts: state.audio.play_ts,
  paragraphs: state.paragraph.paragraphs,
})

export default connect(mapStateToProps)(Transcript)