2020-11-16 18:14:26 +02:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-16 18:14:26 +02:00
|
|
|
export default function(frameWindow: any, onSubmit: Function, onDismiss: Function, loadedHtmlHash: string) {
|
2023-07-09 15:12:41 +02:00
|
|
|
const document = frameWindow && frameWindow.document ? frameWindow.document : null;
|
|
|
|
|
2020-11-16 18:14:26 +02:00
|
|
|
useEffect(() => {
|
2023-07-09 15:12:41 +02:00
|
|
|
if (!document) return () => {};
|
2020-11-16 18:14:26 +02:00
|
|
|
|
|
|
|
function onFormSubmit(event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
if (onSubmit) onSubmit();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onKeyDown(event: any) {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
if (onDismiss) onDismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key === 'Enter') {
|
2021-04-08 11:31:26 +02:00
|
|
|
//
|
|
|
|
// Disable enter key from submitting when a text area is in focus!
|
|
|
|
// https://github.com/laurent22/joplin/issues/4766
|
|
|
|
//
|
2023-07-09 15:12:41 +02:00
|
|
|
if (document.activeElement.tagName !== 'TEXTAREA') {
|
2021-04-08 11:31:26 +02:00
|
|
|
if (onSubmit) onSubmit();
|
|
|
|
}
|
2020-11-16 18:14:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:12:41 +02:00
|
|
|
document.addEventListener('submit', onFormSubmit);
|
|
|
|
document.addEventListener('keydown', onKeyDown);
|
2020-11-16 18:14:26 +02:00
|
|
|
|
|
|
|
return () => {
|
2023-07-09 15:12:41 +02:00
|
|
|
if (document) document.removeEventListener('submit', onFormSubmit);
|
|
|
|
if (document) document.removeEventListener('keydown', onKeyDown);
|
2020-11-16 18:14:26 +02:00
|
|
|
};
|
2023-07-09 15:12:41 +02:00
|
|
|
}, [document, loadedHtmlHash, onSubmit, onDismiss]);
|
2020-11-16 18:14:26 +02:00
|
|
|
}
|