mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-04 18:42:57 -05:00
f6fc3d468c
PDF Detach Updates GitOrigin-RevId: c09c4fe37a922b041cfa1376e110a264a88177c8
69 lines
2.1 KiB
JavaScript
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,
|
|
])
|
|
}
|