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 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 showNewTimestampForm = (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, text: croppedText, type: 'sentence', } }) } const cutFirstSentence = text => { const textToCrop = text.trim().replace("\n", " ").split("\n")[0] let cropIndex = textToCrop.indexOf('. ') + 1 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 hideTimestampForm = () => dispatch => { dispatch({ type: types.align.set_temporary_annotation, data: {} }) }