summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/align/align.util.js
blob: d7b4c39f643bf3aaf45876bd61e17566add412e3 (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
import { ZOOM_STEPS } from './constants'
import { clamp } from '../../util'
import actions from '../../actions'

import { HEADER_MARGIN, INNER_HEIGHT } from './constants'

export const positionToTime = (y, { start_ts, zoom, duration }) => {
  y -= HEADER_MARGIN
  const secondsPerPixel = ZOOM_STEPS[zoom] * 0.1
  const widthTimeDuration = INNER_HEIGHT * secondsPerPixel
  const timeMin = start_ts
  const timeMax = Math.min(start_ts + widthTimeDuration, duration)
  const timeWidth = timeMax - timeMin
  return clamp(y * secondsPerPixel + start_ts, 0, timeMax)
}

export const timeToPosition = (ts, { start_ts, zoom, duration }) => {
  const secondsPerPixel = ZOOM_STEPS[zoom] * 0.1
  const widthTimeDuration = INNER_HEIGHT * secondsPerPixel
  const timeMin = start_ts
  const timeMax = Math.min(start_ts + widthTimeDuration, duration)
  const timeWidth = timeMax - timeMin
  const timeHalfHeight = INNER_HEIGHT * secondsPerPixel / 2
  if (ts < timeMin - timeHalfHeight) {
    return -9999
  }
  if (ts > timeMax) {
    return -9999
  }
  return (ts - timeMin) / timeWidth * INNER_HEIGHT
}

export 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
}

export 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
}