overleaf/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js
Alf Eaton bd64a167f4 [cm6] Wrap compile log annotations in a lint source (#8451)
GitOrigin-RevId: d404560313cbb99c7a06453bc3b7d2374e86598a
2022-06-22 08:03:47 +00:00

41 lines
1.4 KiB
JavaScript

import { useCallback } from 'react'
import getMeta from '../../../utils/meta'
import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-compile-context'
import useEventListener from '../../../shared/hooks/use-event-listener'
import useDetachAction from '../../../shared/hooks/use-detach-action'
const showPdfDetach = getMeta('ol-showPdfDetach')
export default function useCompileTriggers() {
const { startCompile, setChangedAt } = useCompileContext()
// recompile on key press
const startOrTriggerCompile = useDetachAction(
'start-compile',
startCompile,
'detacher',
'detached'
)
const compileHandler = useCallback(
event => {
showPdfDetach
? startOrTriggerCompile(event.detail)
: startCompile(event.detail)
},
[startOrTriggerCompile, startCompile]
)
useEventListener('pdf:recompile', compileHandler)
// record doc changes when notified by the editor
const setOrTriggerChangedAt = useDetachAction(
'set-changed-at',
setChangedAt,
'detacher',
'detached'
)
const setChangedAtHandler = useCallback(() => {
showPdfDetach ? setOrTriggerChangedAt(Date.now()) : setChangedAt(Date.now())
}, [setOrTriggerChangedAt, setChangedAt])
useEventListener('doc:changed', setChangedAtHandler)
useEventListener('doc:saved', setChangedAtHandler) // TODO: store this separately?
}