overleaf/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js
Timothée Alby 3c01402bbd Merge pull request #7034 from overleaf/ta-pdf-detach-full
PDF Detach v2

GitOrigin-RevId: 3deb76474185f9176cde23ab32ef51b90df6e8e9
2022-04-05 12:19:23 +00:00

41 lines
1.3 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)
}