From e699f3ca352a13ebfde8ff7711f47943b61367c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Mon, 17 Oct 2022 12:10:38 +0200 Subject: [PATCH] Merge pull request #9007 from overleaf/ta-recompile-event-body Listen for Recompile Shortcut on Body GitOrigin-RevId: 1ded1d620b35d25f1dffe27b78d8c866e2bfa1bd --- .../components/pdf-preview-detached-root.js | 7 +-- .../pdf-preview/components/shortcuts.js | 39 ----------------- .../pdf-preview/hooks/use-compile-triggers.js | 43 +++++++++++++------ 3 files changed, 33 insertions(+), 56 deletions(-) delete mode 100644 services/web/frontend/js/features/pdf-preview/components/shortcuts.js diff --git a/services/web/frontend/js/features/pdf-preview/components/pdf-preview-detached-root.js b/services/web/frontend/js/features/pdf-preview/components/pdf-preview-detached-root.js index f8ba285b03..6a5d494c23 100644 --- a/services/web/frontend/js/features/pdf-preview/components/pdf-preview-detached-root.js +++ b/services/web/frontend/js/features/pdf-preview/components/pdf-preview-detached-root.js @@ -2,7 +2,6 @@ import ReactDOM from 'react-dom' import PdfPreview from './pdf-preview' import { ContextRoot } from '../../../shared/context/root-context' import useWaitForI18n from '../../../shared/hooks/use-wait-for-i18n' -import { Shortcuts } from './shortcuts' import PdfPreviewDetachedRootSafariWarning from './pdf-preview-detached-root-safari-warning' function PdfPreviewDetachedRoot() { @@ -14,10 +13,8 @@ function PdfPreviewDetachedRoot() { return ( - - - - + + ) } diff --git a/services/web/frontend/js/features/pdf-preview/components/shortcuts.js b/services/web/frontend/js/features/pdf-preview/components/shortcuts.js deleted file mode 100644 index fc7a35a365..0000000000 --- a/services/web/frontend/js/features/pdf-preview/components/shortcuts.js +++ /dev/null @@ -1,39 +0,0 @@ -import { useCallback } from 'react' -import { useDetachCompileContext } from '../../../shared/context/detach-compile-context' -import PropTypes from 'prop-types' - -export const Shortcuts = ({ children }) => { - const { startCompile } = useDetachCompileContext() - - const handleKeyDown = useCallback( - event => { - 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 - } - } - }, - [startCompile] - ) - - return ( - // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -
- {children} -
- ) -} -Shortcuts.propTypes = { - children: PropTypes.node, -} diff --git a/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js b/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js index 620459cc04..66c2e251f0 100644 --- a/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js +++ b/services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js @@ -1,4 +1,4 @@ -import { useCallback } from 'react' +import { useCallback, useEffect } from 'react' import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-compile-context' import useEventListener from '../../../shared/hooks/use-event-listener' import useDetachAction from '../../../shared/hooks/use-detach-action' @@ -6,20 +6,39 @@ import useDetachAction from '../../../shared/hooks/use-detach-action' export default function useCompileTriggers() { const { startCompile, setChangedAt } = useCompileContext() - // recompile on key press - const startOrTriggerCompile = useDetachAction( - 'start-compile', - startCompile, - 'detacher', - 'detached' - ) - const compileHandler = useCallback( + const handleKeyDown = useCallback( event => { - startOrTriggerCompile(event.detail) + 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 + } + } }, - [startOrTriggerCompile] + [startCompile] ) - useEventListener('pdf:recompile', compileHandler) + + const handleStartCompile = useCallback(() => { + startCompile() + }, [startCompile]) + useEventListener('pdf:recompile', handleStartCompile) + + useEffect(() => { + document.body.addEventListener('keydown', handleKeyDown) + return () => { + document.body.removeEventListener('keydown', handleKeyDown) + } + }, [handleKeyDown]) // record doc changes when notified by the editor const setOrTriggerChangedAt = useDetachAction(