summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/player/player.transcript.js
blob: cfb252deab7cc342caae11af1c6166a756dcd8a9 (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
import React, { Component } from 'react'
// import { Link } from 'react-router-dom'
// import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import actions from 'app/actions'
import { isHandheld, xor, timestampToSeconds } from 'app/utils'

import ParagraphList from 'app/views/editor/paragraph/components/paragraph.list'
import { inlineComponents } from './components.inline'

class PlayerTranscript extends Component {
  constructor(props){
    super(props)
    this.handleAnnotationClick = this.handleAnnotationClick.bind(this)
    this.handleParagraphDoubleClick = this.handleParagraphDoubleClick.bind(this)
    this.handleScroll = this.handleScroll.bind(this)
    this.containerRef = React.createRef()
  }

  componentDidUpdate(prevProps) {
    if (this.props.section !== prevProps.section) {
      this.containerRef.current.scrollTo(0, 0)
      setTimeout(() => {
        this.containerRef.current.scrollTo(0, 0)
      }, 20)
    }
    if (
      this.props.currentSection.index === 0 &&
      this.props.isFullscreen &&
      this.props.isFullscreen !== prevProps.isFullscreen
    ) {
      console.log('fullscreen started for the first time')
      this.scrollToTopOfSection()
    }
  }

  scrollToTopOfSection() {
    const { current } = this.containerRef
    console.log('scrollToTopOfSection', current)
    if (!current) return
    const heading = current.querySelector('.section_heading')
    let offset = heading ? heading.offsetTop : 0
    console.log(heading, offset)
    if (offset) {
      current.scrollTo(0, offset)
    }
  }

  handleAnnotationClick(e, paragraph, annotation) {
    // console.log(annotation)
    if (annotation.type === 'footnote') {
      return actions.viewer.openFootnote(annotation)
    }
    if (annotation.settings.override_start_ts) {
      const ts = timestampToSeconds(annotation.settings.override_start_ts)
      if (ts) {
        actions.audio.seek(ts)
      }
    } else {
      actions.audio.seek(annotation.start_ts)
    }
    actions.audio.play()
  }
  
  handleParagraphDoubleClick(e, paragraph) {
  }
  
  handleScroll(e) {
    const { viewer } = this.props
    if (isHandheld) {
      const isScrolledPastIntro = this.containerRef.current.scrollTop > 100
      if (xor(viewer.navGradient, isScrolledPastIntro)) {
        // console.log('toggle nav gradient', isScrolledPastIntro)
        actions.viewer.toggleNavGradient(isScrolledPastIntro)
      }
    }
    if (viewer.growlOpen && !viewer.atEndOfSection) {
      actions.viewer.closeGrowl()
      if (viewer.currentSection.index === 0) {
        actions.audio.play()
      }
    }
  }

  render() {
    const { paragraphs, color, inlineParagraphCount } = this.props.currentSection
    const className = "player-transcript " + color + " " + (
      inlineParagraphCount > 2 ? 'scrollable' : 'not-scrollable'
    )
    // console.log(this.props.section)
    return (
      <div
        className={className}
        ref={this.containerRef}
        onScroll={this.handleScroll}
      >
        <div className='content'>
          <ParagraphList
            paragraphs={paragraphs}
            paragraphElementLookup={inlineComponents}
            currentSection={this.props.section}
            onAnnotationClick={this.handleAnnotationClick}
            onParagraphDoubleClick={this.handleParagraphDoubleClick}
          />
        </div>
      </div>
    )
  }
}

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

export default connect(mapStateToProps)(PlayerTranscript)