summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/editor/align/align.actions.js
blob: 65e3d467500bc1e4a7e41d8fcd27b97e2f338a76 (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
import * as types from 'app/types'
import { store } from 'app/store'
import actions from 'app/actions'
// import { session } from 'app/session'
import throttle from 'lodash.throttle'
import debounce from 'lodash.debounce'

import { ZOOM_STEPS } from 'app/constants'
import { timestampToSeconds, post } from 'app/utils'
import { cutFirstSentence } from 'app/utils/align.utils'
import { annotationFadeTimings } from 'app/utils/annotation.utils'

/* scrolling the view */

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 throttledSetZoom = throttle(zoom => dispatch => {
  setZoom(zoom)(dispatch)
}, 250, { leading: true })

/* cursor */

export const setCursor = cursor_ts => dispatch => (
  dispatch({ type: types.align.set_display_setting, key: 'cursor_ts', value: cursor_ts })
)
export const setCursorRegion = (a_ts, b_ts) => dispatch => (
  dispatch({ type: types.align.set_display_setting, key: 'cursor_region', value: { a_ts, b_ts } })
)
export const clearCursorRegion = () => dispatch => (
  dispatch({ type: types.align.set_display_setting, key: 'cursor_region', value: null })
)

/* selecting annotations in the list */

export const setSelectedAnnotation = annotation => dispatch => {
  debouncedUpdateAnnotation.flush()
  dispatch({ type: types.align.set_selected_annotation, data: annotation })
}
export const clearSelectedAnnotation = () => dispatch => {
  debouncedUpdateAnnotation.flush()
  dispatch({ type: types.align.clear_selected_annotation })
}
export const updateSelectedAnnotation = annotation => dispatch => {
  debouncedUpdateAnnotation(annotation)
  dispatch({ type: types.align.set_selected_annotation, data: { ...annotation } })
}
export const debouncedUpdateAnnotation = debounce(annotation => {
  console.log('updating annotation', annotation)
  actions.annotation.update(annotation)
}, 2000, { leading: false, trailing: true })

export const cloneSelectedAnnotation = annotation => dispatch => {
  const newAnnotation = { ...annotation }
  delete newAnnotation.id
  if (annotation.settings.fullscreen) {
    const {
      fadeInDuration, fadeOutDuration, duration,
      start_ts, end_ts, fade_in_end_ts, fade_out_start_ts,
    } = annotationFadeTimings(annotation)
    newAnnotation.start_ts += duration - fadeOutDuration - fadeInDuration
  } else {
    newAnnotation.start_ts += 1
  }
  actions.annotation.create(newAnnotation)
  .then(res => {
    console.log('cloned annotation', res.res)
    setSelectedParagraph(res.res)
  })
}

/* paragraph management */

export const setSelectedParagraph = paragraph_id => dispatch => {
  dispatch({ type: types.align.set_display_setting, key: 'selected_paragraph_id', value: paragraph_id })
}
export const clearSelectedParagraph = () => dispatch => {
  dispatch({ type: types.align.set_display_setting, key: 'selected_paragraph_id', value: -1 })
}

/* annotation form - show / hide / update */

export const showNewAnnotationForm = (start_ts, text) => dispatch => {
  const state = store.getState()
  let croppedText = state.align.annotation.start_ts
    ? state.align.annotation.text
    : cutFirstSentence(text)

  // console.log(croppedText)
  dispatch({
    type: types.align.set_temporary_annotation,
    data: {
      id: 'new',
      episode_id: state.site.episode.id,
      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 })
}
export const hideAnnotationForm = () => dispatch => {
  dispatch({
    type: types.align.set_temporary_annotation,
    data: {}
  })
}

/* insert / removing time */

export const spliceTime = start_ts => dispatch => {
  const state = store.getState()
  let duration = timestampToSeconds(prompt("How many seconds to add or remove? Enter a positive / negative number"))
  if (!duration) {
    return
  }
  console.log(start_ts, duration)
  const data = {
    start_ts, duration,
    episode_id: state.site.episode.id,
  }
  post(dispatch, types.api, 'splice', '/api/v1/annotation/splice', data)
  .then(res => {
    console.log(res)
    alert(res.count + ' records updated!')
    actions.annotation.index()
  })
}