2022-10-17 06:10:38 -04:00
|
|
|
import { useCallback, useEffect } from 'react'
|
2021-11-30 09:54:14 -05:00
|
|
|
import useEventListener from '../../../shared/hooks/use-event-listener'
|
|
|
|
import useDetachAction from '../../../shared/hooks/use-detach-action'
|
|
|
|
|
2022-11-28 06:57:54 -05:00
|
|
|
export const startCompileKeypress = event => {
|
|
|
|
if (event.shiftKey || event.altKey) {
|
|
|
|
return false
|
|
|
|
}
|
2021-11-30 09:54:14 -05:00
|
|
|
|
2022-11-28 06:57:54 -05:00
|
|
|
if (event.ctrlKey) {
|
|
|
|
// Ctrl+s / Ctrl+Enter / Ctrl+.
|
|
|
|
if (event.key === 's' || event.key === 'Enter' || event.key === '.') {
|
|
|
|
return true
|
|
|
|
}
|
2023-09-01 07:04:32 -04:00
|
|
|
|
|
|
|
// Ctrl+s with Caps-Lock on
|
|
|
|
if (event.key === 'S' && !event.shiftKey) {
|
|
|
|
return true
|
|
|
|
}
|
2022-11-28 06:57:54 -05:00
|
|
|
} else if (event.metaKey) {
|
|
|
|
// Cmd+s / Cmd+Enter
|
|
|
|
if (event.key === 's' || event.key === 'Enter') {
|
|
|
|
return true
|
|
|
|
}
|
2023-09-01 07:04:32 -04:00
|
|
|
|
|
|
|
// Cmd+s with Caps-Lock on
|
|
|
|
if (event.key === 'S' && !event.shiftKey) {
|
|
|
|
return true
|
|
|
|
}
|
2022-11-28 06:57:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 04:56:24 -04:00
|
|
|
export default function useCompileTriggers(
|
|
|
|
startCompile,
|
|
|
|
setChangedAt,
|
|
|
|
setSavedAt
|
|
|
|
) {
|
2022-10-17 06:10:38 -04:00
|
|
|
const handleKeyDown = useCallback(
|
2021-11-30 09:54:14 -05:00
|
|
|
event => {
|
2022-11-28 06:57:54 -05:00
|
|
|
if (startCompileKeypress(event)) {
|
|
|
|
event.preventDefault()
|
|
|
|
startCompile()
|
2022-10-17 06:10:38 -04:00
|
|
|
}
|
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)
|
2023-04-14 04:56:24 -04:00
|
|
|
|
|
|
|
// record when the server acknowledges saving changes
|
|
|
|
const setOrTriggerSavedAt = useDetachAction(
|
|
|
|
'set-saved-at',
|
|
|
|
setSavedAt,
|
|
|
|
'detacher',
|
|
|
|
'detached'
|
|
|
|
)
|
|
|
|
const setSavedAtHandler = useCallback(() => {
|
|
|
|
setOrTriggerSavedAt(Date.now())
|
|
|
|
}, [setOrTriggerSavedAt])
|
|
|
|
useEventListener('doc:saved', setSavedAtHandler)
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|