2021-01-12 01:33:10 +02:00
|
|
|
import PostMessageService, { MessageResponse, ResponderComponentType } from '@joplin/lib/services/PostMessageService';
|
2020-11-16 18:14:26 +02:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
2021-01-12 01:33:10 +02:00
|
|
|
export default function(frameWindow: any, isReady: boolean, pluginId: string, viewId: string, postMessage: Function) {
|
|
|
|
useEffect(() => {
|
|
|
|
PostMessageService.instance().registerResponder(ResponderComponentType.UserWebview, viewId, (message: MessageResponse) => {
|
|
|
|
postMessage('postMessageService.response', { message });
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
PostMessageService.instance().unregisterResponder(ResponderComponentType.UserWebview, viewId);
|
|
|
|
};
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2021-01-12 01:33:10 +02:00
|
|
|
}, [viewId]);
|
2020-11-27 01:31:31 +02:00
|
|
|
|
2020-11-16 18:14:26 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!frameWindow) return () => {};
|
|
|
|
|
2020-11-27 01:31:31 +02:00
|
|
|
function onMessage_(event: any) {
|
|
|
|
|
2021-11-09 17:50:50 +02:00
|
|
|
if (!event.data || !event.data.target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.data.target === 'postMessageService.registerViewMessageHandler') {
|
|
|
|
PostMessageService.instance().registerViewMessageHandler(ResponderComponentType.UserWebview, viewId, (message: MessageResponse) => {
|
|
|
|
postMessage('postMessageService.plugin_message', { message });
|
|
|
|
});
|
|
|
|
} else if (event.data.target === 'postMessageService.message') {
|
|
|
|
void PostMessageService.instance().postMessage({
|
|
|
|
pluginId,
|
|
|
|
viewId,
|
|
|
|
...event.data.message,
|
|
|
|
});
|
|
|
|
}
|
2020-11-16 18:14:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 01:31:31 +02:00
|
|
|
frameWindow.addEventListener('message', onMessage_);
|
2020-11-16 18:14:26 +02:00
|
|
|
|
|
|
|
return () => {
|
2023-01-08 13:26:36 +02:00
|
|
|
if (frameWindow?.removeEventListener) frameWindow.removeEventListener('message', onMessage_);
|
2020-11-16 18:14:26 +02:00
|
|
|
};
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2021-01-12 01:33:10 +02:00
|
|
|
}, [frameWindow, isReady, pluginId, viewId]);
|
2020-11-16 18:14:26 +02:00
|
|
|
}
|