summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/viewer/player/components.media/video.subtitles.js
blob: 2bc0ac3a5b84bebd4a06d48293e047de5cef4cc0 (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
import React, { Component } from 'react'
import { connect } from 'react-redux'

import actions from 'app/actions'
import { timestampToSeconds, floatInRange } from 'app/utils'
import { parseSubtitles } from 'app/utils/transcript.utils'

export default class VideoSubtitles extends Component {
  state = {
    subtitles: [],
    current: null,
  }
  componentDidMount() {
    this.loadSubtitles()
  }
  componentDidUpdate(prevProps) {
    if (this.props.play_ts !== prevProps.play_ts) {
      this.updateCurrentSubtitle()
    }
  }
  loadSubtitles() {
    const subtitles = parseSubtitles(this.props.mediaItem, 0)
    if (subtitles) {
      this.setState({ subtitles, current: null })
    }
  }
  updateCurrentSubtitle() {
    const { play_ts } = this.props
    const current = this.state.subtitles.filter(({ start_ts, end_ts }) => (
      floatInRange(start_ts, play_ts, end_ts )
    )).slice(-1)
    if (!current.length) {
      this.setState({ current: null })
    } else {
      this.setState({ current: current[0] })
    }
  }
  render() {
    const { cc } = this.props
    const { current } = this.state
    if (!cc || !current) return <div className="video-subtitles hidden" />
    return (
      <div className="video-subtitles">
        <span>{current.lines.join(" ")}</span>
      </div>
    )
  }
}