2022-03-31 07:22:36 -04:00
|
|
|
import { useCallback } from 'react'
|
2021-11-30 09:54:14 -05:00
|
|
|
import getMeta from '../../../utils/meta'
|
2022-03-31 07:22:36 -04:00
|
|
|
import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-compile-context'
|
2021-11-30 09:54:14 -05:00
|
|
|
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() {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { startCompile, setChangedAt } = useCompileContext()
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|