summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/paragraph/containers/paragraphList.container.js
blob: 600eaad5d53e7c38b2eef39846b98bd6e658a0be (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
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import actions from '../../../actions'
import { ParagraphElementLookup } from '../components/paragraph.types'

const floatLT = (a,b) => ((a*10|0) < (b*10|0))
const floatLTE = (a,b) => ((a*10|0) === (b*10|0) || floatLT(a,b))

class ParagraphList extends Component {
  state = {
    paragraphs: [],
    currentParagraph: -1,
    currentAnnotation: -1,
  }
  constructor(props) {
    super(props)
    this.onAnnotationClick = this.onAnnotationClick.bind(this)
    this.onParagraphDoubleClick = this.onParagraphDoubleClick.bind(this)
  }
  componentDidMount() {
    this.build()
  }
  componentDidUpdate(prevProps) {
    if (this.props.audio.play_ts === prevProps.audio.play_ts) return
    this.setCurrentParagraph()
  }
  setCurrentParagraph() {
    const { play_ts } = this.props.audio
    const insideParagraph = this.state.paragraphs.some(paragraph => {
      if (floatLTE(paragraph.start_ts, play_ts) && floatLT(play_ts, paragraph.end_ts)) {
        this.setCurrentAnnotation(paragraph, play_ts)
        return true
      }
      return false
    })
    if (!insideParagraph) {
      this.setState({
        currentParagraph: -1,
        currentAnnotation: -1,
      })
    }
  }
  setCurrentAnnotation(paragraph, play_ts) {
    const { id: currentParagraph, annotations } = paragraph
    let currentAnnotation
    let annotation
    let i = 0
    let len = annotations.length
    for (let i = 0; i < len - 1; i++) {
      if (floatLT(play_ts, annotations[i+1].start_ts)) {
        currentAnnotation = annotations[i].id
        break
      }
    }
    if (!currentAnnotation) {
      currentAnnotation = annotations[len-1].id
    }
    this.setState({ currentParagraph, currentAnnotation })
  }
  build() {
    const { order: annotationOrder, lookup: annotationLookup } = this.props.annotation
    const { lookup: paragraphLookup } = this.props.paragraph
    let currentParagraph = {}
    const paragraphs = []
    annotationOrder.forEach((annotation_id, i) => {
      const annotation = annotationLookup[annotation_id]
      const paragraph = paragraphLookup[annotation.paragraph_id]
      if (annotation.paragraph_id !== currentParagraph.id) {
        const paragraph_type = getParagraphType(annotation, paragraph)
        currentParagraph = {
          id: annotation.paragraph_id || ('index_' + i),
          type: paragraph_type,
          start_ts: annotation.start_ts,
          end_ts: 0,
          annotations: [],
        }
        paragraphs.push(currentParagraph)
      }
      if (annotation.type === 'paragraph_end') {
        currentParagraph.end_ts = annotation.start_ts
      } else {
        currentParagraph.annotations.push(annotation)
      }
    })
    for (let i = 0; i < (paragraphs.length - 1); i++) {
      console.log(paragraphs[i].end_ts)
      if (!paragraphs[i].end_ts) {
        paragraphs[i].end_ts = paragraphs[i+1].start_ts - 0.1
      }
    }
    console.log(paragraphs)
    this.setState({ paragraphs })
  }
  onAnnotationClick(e, paragraph, annotation){
    actions.audio.seek(annotation.start_ts)
  }
  onParagraphDoubleClick(e, paragraph) {
    //
  }
  render() {
    const { paragraphs, currentParagraph, currentAnnotation } = this.state
    return (
      <div className='paragraphs'>
        <div className='content'>
          {paragraphs.map(paragraph => {
            if (paragraph.type in ParagraphElementLookup) {
              const ParagraphElement = ParagraphElementLookup[paragraph.type]
              return (
                <ParagraphElement
                  key={paragraph.id}
                  paragraph={paragraph}
                  selectedParagraph={paragraph.id === currentParagraph}
                  selectedAnnotation={paragraph.id === currentParagraph && currentAnnotation}
                  onAnnotationClick={this.onAnnotationClick}
                  onDoubleClick={this.onParagraphDoubleClick}
                />
              )
            } else {
              return <div key={paragraph.id}>{'(empty)'}</div>
            }
          })}
        </div>
      </div>
    )
  }
}

const getParagraphType = (annotation, paragraph) => {
  if (!paragraph) {
    return annotation.type
  }
  return paragraph.type
}

const mapStateToProps = state => ({
  paragraph: state.paragraph.index,
  annotation: state.annotation.index,
  audio: state.audio,
})

const mapDispatchToProps = dispatch => ({
})

export default connect(mapStateToProps, mapDispatchToProps)(ParagraphList)