From 4c5837b85bf316b5c8addda7ad4fc264dd4ebeca Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 9 Jul 2023 14:12:41 +0100 Subject: [PATCH] Desktop: Prevent app from crashing when changing screens Ref: https://github.com/laurent22/joplin/issues/6052#issuecomment-1627528277 --- .../services/plugins/hooks/useSubmitHandler.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/app-desktop/services/plugins/hooks/useSubmitHandler.ts b/packages/app-desktop/services/plugins/hooks/useSubmitHandler.ts index 87dd816a5..fefe21a8f 100644 --- a/packages/app-desktop/services/plugins/hooks/useSubmitHandler.ts +++ b/packages/app-desktop/services/plugins/hooks/useSubmitHandler.ts @@ -2,8 +2,10 @@ import { useEffect } from 'react'; // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied export default function(frameWindow: any, onSubmit: Function, onDismiss: Function, loadedHtmlHash: string) { + const document = frameWindow && frameWindow.document ? frameWindow.document : null; + useEffect(() => { - if (!frameWindow) return () => {}; + if (!document) return () => {}; function onFormSubmit(event: any) { event.preventDefault(); @@ -20,18 +22,18 @@ export default function(frameWindow: any, onSubmit: Function, onDismiss: Functio // Disable enter key from submitting when a text area is in focus! // https://github.com/laurent22/joplin/issues/4766 // - if (frameWindow.document.activeElement.tagName !== 'TEXTAREA') { + if (document.activeElement.tagName !== 'TEXTAREA') { if (onSubmit) onSubmit(); } } } - frameWindow.document.addEventListener('submit', onFormSubmit); - frameWindow.document.addEventListener('keydown', onKeyDown); + document.addEventListener('submit', onFormSubmit); + document.addEventListener('keydown', onKeyDown); return () => { - if (frameWindow) frameWindow.document.removeEventListener('submit', onFormSubmit); - if (frameWindow) frameWindow.document.removeEventListener('keydown', onKeyDown); + if (document) document.removeEventListener('submit', onFormSubmit); + if (document) document.removeEventListener('keydown', onKeyDown); }; - }, [frameWindow, loadedHtmlHash, onSubmit, onDismiss]); + }, [document, loadedHtmlHash, onSubmit, onDismiss]); }