2022-10-17 06:10:38 -04:00
|
|
|
import { useCallback, useEffect } from 'react'
|
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'
|
|
|
|
|
|
|
|
export default function useCompileTriggers() {
|
2022-03-31 07:22:36 -04:00
|
|
|
const { startCompile, setChangedAt } = useCompileContext()
|
2021-11-30 09:54:14 -05:00
|
|
|
|
2022-10-17 06:10:38 -04:00
|
|
|
const handleKeyDown = useCallback(
|
2021-11-30 09:54:14 -05:00
|
|
|
event => {
|
2022-10-17 06:10:38 -04:00
|
|
|
if (event.metaKey) {
|
|
|
|
switch (event.key) {
|
|
|
|
case 's':
|
|
|
|
case 'Enter':
|
|
|
|
event.preventDefault()
|
|
|
|
startCompile()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else if (event.ctrlKey) {
|
|
|
|
switch (event.key) {
|
|
|
|
case '.':
|
|
|
|
event.preventDefault()
|
|
|
|
startCompile()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 09:54:14 -05:00
|
|
|
},
|
2022-10-17 06:10:38 -04:00
|
|
|
[startCompile]
|
2021-11-30 09:54:14 -05:00
|
|
|
)
|
2022-10-17 06:10:38 -04:00
|
|
|
|
|
|
|
const handleStartCompile = useCallback(() => {
|
|
|
|
startCompile()
|
|
|
|
}, [startCompile])
|
|
|
|
useEventListener('pdf:recompile', handleStartCompile)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.body.addEventListener('keydown', handleKeyDown)
|
|
|
|
return () => {
|
|
|
|
document.body.removeEventListener('keydown', handleKeyDown)
|
|
|
|
}
|
|
|
|
}, [handleKeyDown])
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
// record doc changes when notified by the editor
|
|
|
|
const setOrTriggerChangedAt = useDetachAction(
|
|
|
|
'set-changed-at',
|
|
|
|
setChangedAt,
|
|
|
|
'detacher',
|
|
|
|
'detached'
|
|
|
|
)
|
|
|
|
const setChangedAtHandler = useCallback(() => {
|
2022-08-18 04:14:44 -04:00
|
|
|
setOrTriggerChangedAt(Date.now())
|
|
|
|
}, [setOrTriggerChangedAt])
|
2021-11-30 09:54:14 -05:00
|
|
|
useEventListener('doc:changed', setChangedAtHandler)
|
2022-06-21 05:58:56 -04:00
|
|
|
useEventListener('doc:saved', setChangedAtHandler) // TODO: store this separately?
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|