summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/align/align.actions.js
blob: b8195840c24b0af0d3dace92bee194f94a837f9e (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
import * as types from '../../types'
import { store, history, dispatch } from '../../store'
import { api, post, pad, preloadImage } from '../../util'
import actions from '../../actions'
import { session } from '../../session'
import throttle from 'lodash.throttle'

import { ZOOM_STEPS } from './constants'

export const setScrollPosition = start_ts => dispatch => (
  dispatch({ type: types.align.set_display_setting, key: 'start_ts', value: start_ts })
)

export const setZoom = zoom => dispatch => {
  if (0 <= zoom && zoom < ZOOM_STEPS.length) {
    dispatch({ type: types.align.set_display_setting, key: 'zoom', value: zoom })
  }
}

export const setSelectedAnnotation = annotation_id => dispatch => {
  dispatch({ type: types.align.set_display_setting, key: 'selected_annotation_id', value: annotation_id })
}

export const clearSelectedAnnotation = annotation_id => dispatch => {
  dispatch({ type: types.align.set_display_setting, key: 'selected_annotation_id', value: -1 })
}

export const throttledSetZoom = throttle(zoom => dispatch => {
  setZoom(zoom)(dispatch)
}, 250, { leading: true })

export const setCursor = cursor_ts => dispatch => (
  dispatch({ type: types.align.set_display_setting, key: 'cursor_ts', value: cursor_ts })
)

export const showNewAnnotationForm = (start_ts, text) => dispatch => {
  let croppedText;
  if (store.getState().align.annotation.start_ts) {
    croppedText = store.getState().align.annotation.text
  } else {
    croppedText = cutFirstSentence(text)
  }
  console.log(croppedText)
  dispatch({
    type: types.align.set_temporary_annotation,
    data: {
      id: 'new',
      start_ts,
      end_ts: 0.0,
      text: croppedText,
      type: 'sentence',
      settings: {},
    }
  })
}
export const showEditAnnotationForm = (annotation) => dispatch => {
  dispatch({
    type: types.align.set_temporary_annotation,
    data: annotation,
  })
}

export const updateAnnotationForm = (key, value) => dispatch => {
  dispatch({ type: types.align.update_temporary_annotation, key, value })
}
export const updateAnnotationSettings = (key, value) => dispatch => {
  dispatch({ type: types.align.update_temporary_annotation_settings, key, value })
}

const getFirstPunctuationMarkIndex = text => {
  return Math.min(
      text.indexOf('. '),
      text.indexOf('? '),
      text.indexOf('! '),
      text.indexOf('." '),
      text.indexOf('?" '),
      text.indexOf('!" '),
      text.indexOf('.” '),
      text.indexOf('?” '),
      text.indexOf('!” '),
    ) + 1
}
const cutFirstSentence = text => {
  const textToCrop = text.trim().replace("\n", " ").split("\n")[0]
  let cropIndex = getFirstPunctuationMarkIndex(textToCrop)
  if (!cropIndex) cropIndex = textToCrop.length
  const croppedText = textToCrop.substr(0, cropIndex).trim()
  const updatedText = text.trim().replace(croppedText, '').trim()
  actions.site.updateText(updatedText)
  return croppedText
}

export const hideAnnotationForm = () => dispatch => {
  dispatch({
    type: types.align.set_temporary_annotation,
    data: {}
  })
}