Files
remark42/frontend/app/utils/postMessage.ts
T
Pavel MineevandUmputun 3068dfe637 post messages from iframe
- fix user info opening
- remove iframe height checks by interval
- update height by mutation observer events
- rename events
2021-06-03 00:56:16 -05:00

62 lines
1.3 KiB
TypeScript

import type { Theme, UserInfo } from 'common/types';
type ParentMessage = {
inited?: true;
scrollTo?: number;
height?: number;
profile?: UserInfo | null;
};
type ChildMessage = {
clickOutside?: true;
hash?: string;
title?: string;
theme?: Theme;
};
type AllMessages = ChildMessage & ParentMessage;
/**
* Sends message to parent window
*
* @returns request success of fail
*/
export function postMessageToParent(data: ParentMessage): boolean {
if (!window.parent || window.parent === window) {
return false;
}
window.parent.postMessage(data, '*');
return true;
}
/**
* Sends message to target iframe
*
* @param target iframe to send data
* @param data that will be send to iframe
* @returns request success of fail
*/
export function postMessageToIframe(target: HTMLIFrameElement, data: ChildMessage): boolean {
if (!target?.contentWindow) {
return false;
}
target.contentWindow.postMessage(data, '*');
return true;
}
/**
* Parses data from post message that was received in iframe
*
* @param evt post message event
* @returns
*/
export function parseMessage({ data }: MessageEvent): AllMessages {
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
return {} as AllMessages;
}
return data as AllMessages;
}