一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
28 行
731 B
TypeScript
28 行
731 B
TypeScript
import { RefObject, useEffect, useRef } from 'react';
|
|
|
|
type OnMessage = (event: MessageEvent)=> void;
|
|
|
|
const useMessageHandler = (viewRef: RefObject<HTMLIFrameElement>, onMessage: OnMessage) => {
|
|
const onMessageRef = useRef(onMessage);
|
|
onMessageRef.current = onMessage;
|
|
|
|
useEffect(() => {
|
|
function onMessage_(event: MessageEvent) {
|
|
if (event.source !== viewRef.current.contentWindow) {
|
|
return;
|
|
}
|
|
|
|
onMessageRef.current(event);
|
|
}
|
|
|
|
const containerWindow = (viewRef.current.getRootNode() as Document).defaultView;
|
|
containerWindow.addEventListener('message', onMessage_);
|
|
|
|
return () => {
|
|
containerWindow.removeEventListener('message', onMessage_);
|
|
};
|
|
}, [viewRef]);
|
|
};
|
|
|
|
export default useMessageHandler;
|