1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/app-desktop/services/plugins/hooks/useSubmitHandler.ts

31 lines
859 B
TypeScript

import { useEffect } from 'react';
export default function(frameWindow: any, onSubmit: Function, onDismiss: Function, loadedHtmlHash: string) {
useEffect(() => {
if (!frameWindow) return () => {};
function onFormSubmit(event: any) {
event.preventDefault();
if (onSubmit) onSubmit();
}
function onKeyDown(event: any) {
if (event.key === 'Escape') {
if (onDismiss) onDismiss();
}
if (event.key === 'Enter') {
if (onSubmit) onSubmit();
}
}
frameWindow.document.addEventListener('submit', onFormSubmit);
frameWindow.document.addEventListener('keydown', onKeyDown);
return () => {
if (frameWindow) frameWindow.document.removeEventListener('submit', onFormSubmit);
if (frameWindow) frameWindow.document.removeEventListener('keydown', onKeyDown);
};
}, [frameWindow, loadedHtmlHash, onSubmit, onDismiss]);
}