summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/align/components/annotations/annotation.form.js
blob: 7b1918ab46c527fe7f1c7994212c1c38ce8e3aa8 (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
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 { ANNOTATION_SELECT_OPTIONS, MEDIA_ANNOTATION_TYPES } from 'app/constants'
import { timestamp } from 'app/utils'
import { timeToPosition } from 'app/utils/align.utils'
import { Select } from 'app/common'

import { annotationFormLookup } from './annotationForms'

const annotationTextTypes = new Set([
  'sentence', 'section_heading', 'heading_text', 'pullquote_credit', 'footnote', 'text_plate',
])

class AnnotationForm extends Component {
  constructor(props){
    super(props)
    this.handleChange = this.handleChange.bind(this)
    this.handleSelect = this.handleSelect.bind(this)
    this.handleSettingsChange = this.handleSettingsChange.bind(this)
    this.handleSettingsSelect = this.handleSettingsSelect.bind(this)
    this.handleKeyDown = this.handleKeyDown.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
    this.handleDestroy = this.handleDestroy.bind(this)
    this.textareaRef = React.createRef()
  }
  componentDidMount() {
    if (this.textareaRef && this.textareaRef.current) {
      this.textareaRef.current.focus()
    }
  }
  handleKeyDown(e) {
    if (e.keyCode === 27) { // escape
      actions.align.hideAnnotationForm()
      return      
    }
    // console.log(e.keyCode)
    if (!e.metaKey && !e.ctrlKey) return
    let { start_ts } = this.props.annotation
    switch (e.keyCode) {
      case 38: // up
        e.preventDefault()
        start_ts -= 0.1
        actions.align.updateAnnotationForm('start_ts', Math.max(0, start_ts))
        actions.audio.seek(start_ts)
        actions.align.setCursor(start_ts)
        break
      case 40: // down
        e.preventDefault()
        start_ts += 0.1
        actions.align.updateAnnotationForm('start_ts', Math.max(0, start_ts))
        actions.audio.seek(start_ts)
        actions.align.setCursor(start_ts)
        break
      case 83: // ctrl-S
        e.preventDefault()
        this.handleSubmit()
      default:
        break
    }
  }
  handleChange(e) {
    const { name, value } = e.target
    this.handleSelect(name, value)
  }
  handleSelect(name, value) {
    actions.align.updateAnnotationForm(name, value)
  }
  handleSettingsChange(e) {
    const { name, value } = e.target
    this.handleSettingsSelect(name, value)
  }
  handleSettingsSelect(name, value) {
    if (name.indexOf('_id') !== -1) value = parseInt(value) || 0
    actions.align.updateAnnotationSettings(name, value)
  }
  handleSubmit() {
    const { annotation } = this.props
    if (annotation.type === 'paragraph_end') {
      annotation.text = ''
    }
    if (annotation.type in MEDIA_ANNOTATION_TYPES) {
      if (!annotation.settings.media_id) return
      annotation.text = ''
    }
    if (annotation.id === 'new') {
      delete annotation.id
      actions.annotation.create(annotation)
        .then(response => {
          console.log(response)
          actions.align.hideAnnotationForm()
        })
    } else {
      actions.annotation.update(annotation)
        .then(response => {
          console.log(response)
          actions.align.hideAnnotationForm()
        })
    }
  }
  handleDestroy() {
    const { annotation } = this.props
    if (annotation.id === 'new') {
      actions.align.hideAnnotationForm()
    } else {
      actions.annotation.destroy(annotation)
        .then(response => {
          console.log(response)
          actions.align.hideAnnotationForm()
        })
    }
  }
  render() {
    const { timeline, annotation } = this.props
    return (
      <div
        className='annotationForm'
        style={{
          top: timeToPosition(annotation.start_ts, timeline),
        }}
      >
        {this.renderButtons()}
        {annotationTextTypes.has(annotation.type) && this.renderTextarea()}
        {(annotation.type in annotationFormLookup) && this.renderElementForm()}
      </div>
    )
  }
  renderButtons() {
    const { annotation } = this.props
    return (
      <div className='row buttons'>
        <div>
          <Select
            name='type'
            selected={annotation.type}
            options={ANNOTATION_SELECT_OPTIONS}
            defaultOption='text'
            onChange={this.handleSelect}
          />
          <div className='ts'>{timestamp(annotation.start_ts, 1, true)}</div>
        </div>
        <div>
          {annotation.id !== 'new' && <button onClick={this.handleDestroy}>Delete</button>}
          <button onClick={this.handleSubmit}>Save</button>
        </div>
      </div>
    )
  }
  renderTextarea() {
    const { annotation } = this.props
    return (
      <div className='textarea'>
        <textarea
          name='text'
          value={annotation.text}
          onKeyDown={this.handleKeyDown}
          onChange={this.handleChange}
          ref={this.textareaRef}
        />
      </div>
    )
  }
  renderElementForm() {
    const { annotation, media } = this.props
    const AnnotationFormElement = annotationFormLookup[annotation.type]
    return (
      <AnnotationFormElement
        annotation={annotation}
        media={media}
        handleSettingsChange={this.handleSettingsChange}
        handleSettingsSelect={this.handleSettingsSelect}
      />
    )
  }
}

const mapStateToProps = state => ({
  annotation: state.align.annotation,
  timeline: state.align.timeline,
  media: state.media.index,
})

const mapDispatchToProps = dispatch => ({
})

export default connect(mapStateToProps, mapDispatchToProps)(AnnotationForm)