1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Prevent app from crashing when changing screens

Ref: https://github.com/laurent22/joplin/issues/6052#issuecomment-1627528277
This commit is contained in:
Laurent Cozic 2023-07-09 14:12:41 +01:00
parent ecd79ef7e4
commit 4c5837b85b

View File

@ -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]);
}