diff options
Diffstat (limited to 'animism-align/frontend/app/utils')
| -rw-r--r-- | animism-align/frontend/app/utils/align.utils.js | 60 | ||||
| -rw-r--r-- | animism-align/frontend/app/utils/annotation.utils.js | 0 | ||||
| -rw-r--r-- | animism-align/frontend/app/utils/index.js | 11 |
3 files changed, 71 insertions, 0 deletions
diff --git a/animism-align/frontend/app/utils/align.utils.js b/animism-align/frontend/app/utils/align.utils.js new file mode 100644 index 0000000..f64c5c8 --- /dev/null +++ b/animism-align/frontend/app/utils/align.utils.js @@ -0,0 +1,60 @@ +import { ZOOM_STEPS } from 'app/constants' +import { clamp } from 'app/utils' +import actions from 'app/actions' + +import { HEADER_MARGIN, INNER_HEIGHT } from 'app/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 => { + const indexes = [ + text.indexOf('. '), + text.indexOf('? '), + text.indexOf('! '), + text.indexOf('." '), + text.indexOf('?" '), + text.indexOf('!" '), + text.indexOf('.” '), + text.indexOf('?” '), + text.indexOf('!” '), + ] + + return indexes.reduce((a, b) => { + if (b < 0) return a + return Math.min(a, b) + }, Infinity) + 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 +} diff --git a/animism-align/frontend/app/utils/annotation.utils.js b/animism-align/frontend/app/utils/annotation.utils.js new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/animism-align/frontend/app/utils/annotation.utils.js diff --git a/animism-align/frontend/app/utils/index.js b/animism-align/frontend/app/utils/index.js index c2dd464..273c0ef 100644 --- a/animism-align/frontend/app/utils/index.js +++ b/animism-align/frontend/app/utils/index.js @@ -70,6 +70,17 @@ export const timestamp = (n = 0, fps = 1, ms = false) => { return (n % 60) + ':' + s } +export const timestampToSeconds = time_str => { + const time_str_parts = time_str.trim().split(":").map(s => parseFloat(s)) + if (time_str_parts.length === 3) { + return (time_str_parts[0] * 60 + time_str_parts[1]) * 60 + time_str_parts[2] + } + if (time_str_parts.length === 2) { + return time_str_parts[0] * 60 + time_str_parts[1] + } + return time_str_parts[0] +} + export const percent = n => (n * 100).toFixed(1) + '%' export const px = (n, w) => Math.round(n * w) + 'px' |
