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

GitOrigin-RevId: c09c4fe37a922b041cfa1376e110a264a88177c8
2021-12-01 09:03:41 +00:00

69 lines
2.1 KiB
JavaScript

import { useCallback, useEffect } from 'react'
import getMeta from '../../../utils/meta'
import { useCompileContext } from '../../../shared/context/compile-context'
import { useDetachContext } from '../../../shared/context/detach-context'
import useEventListener from '../../../shared/hooks/use-event-listener'
import useDetachAction from '../../../shared/hooks/use-detach-action'
import usePreviousValue from '../../../shared/hooks/use-previous-value'
const showPdfDetach = getMeta('ol-showPdfDetach')
const debugPdfDetach = getMeta('ol-debugPdfDetach')
export default function useCompileTriggers() {
const {
startCompile,
setChangedAt,
cleanupCompileResult,
setError,
} = useCompileContext()
const { role: detachRole } = useDetachContext()
// 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)
// clear preview and recompile when the detach role is reset
const previousDetachRole = usePreviousValue(detachRole)
useEffect(() => {
if (previousDetachRole && !detachRole) {
if (debugPdfDetach) {
console.log('Recompile on reattach', { previousDetachRole, detachRole })
}
cleanupCompileResult()
setError()
startCompile()
}
}, [
cleanupCompileResult,
setError,
startCompile,
previousDetachRole,
detachRole,
])
}