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);
|
|
|
|
};
|
|
|
|
}, [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-01-12 01:33:10 +02:00
|
|
|
if (!event.data || event.data.target !== 'postMessageService.message') return;
|
2020-11-27 01:31:31 +02:00
|
|
|
|
2021-01-12 01:33:10 +02:00
|
|
|
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 () => {
|
2020-11-27 01:31:31 +02:00
|
|
|
frameWindow.removeEventListener('message', onMessage_);
|
2020-11-16 18:14:26 +02:00
|
|
|
};
|
2021-01-12 01:33:10 +02:00
|
|
|
}, [frameWindow, isReady, pluginId, viewId]);
|
2020-11-16 18:14:26 +02:00
|
|
|
}
|