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' ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any -- Old code before rule was applied, Old code before rule was applied
2024-11-08 17:32:05 +02:00
export default function ( frameWindow : any , isReady : boolean , pluginId : string , viewId : string , windowId : string , postMessage : Function ) {
2021-01-12 01:33:10 +02:00
useEffect ( ( ) = > {
2024-11-08 17:32:05 +02:00
PostMessageService . instance ( ) . registerResponder ( ResponderComponentType . UserWebview , viewId , windowId , ( message : MessageResponse ) = > {
2021-01-12 01:33:10 +02:00
postMessage ( 'postMessageService.response' , { message } ) ;
} ) ;
return ( ) = > {
2024-11-08 17:32:05 +02:00
PostMessageService . instance ( ) . unregisterResponder ( ResponderComponentType . UserWebview , viewId , windowId ) ;
2021-01-12 01:33:10 +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
} , [ viewId ] ) ;
2020-11-27 01:31:31 +02:00
2020-11-16 18:14:26 +02:00
useEffect ( ( ) = > {
if ( ! frameWindow ) return ( ) = > { } ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
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 ,
2024-11-08 17:32:05 +02:00
windowId ,
2021-11-09 17:50:50 +02:00
. . . 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
2024-11-08 17:32:05 +02:00
} , [ frameWindow , isReady , pluginId , windowId , viewId ] ) ;
2020-11-16 18:14:26 +02:00
}