-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Elephmoon
committed
May 16, 2021
1 parent
d1ef3ff
commit 54679a2
Showing
8 changed files
with
66 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,42 @@ | ||
import { User } from 'common/types'; | ||
import type { Theme, UserInfo } from 'common/types'; | ||
|
||
export type Message = | ||
| { inited: true } | ||
| { | ||
isUserInfoShown: true; | ||
user: User; | ||
} | ||
| { isUserInfoShown: false } | ||
| { scrollTo: number } | ||
| { remarkIframeHeight: number }; | ||
export type ParentMessage = { | ||
inited?: true; | ||
scrollTo?: number; | ||
remarkIframeHeight?: number; | ||
} & ( | ||
| { isUserInfoShown: true; user: UserInfo } | ||
| { isUserInfoShown: false; user?: never } | ||
| { isUserInfoShown?: never; user?: never } | ||
); | ||
|
||
export type ChildMessage = { | ||
theme?: Theme; | ||
}; | ||
|
||
type AllMessages = ParentMessage & ChildMessage; | ||
|
||
/** | ||
* Sends message to parent window | ||
* | ||
* @returns request success of fail | ||
*/ | ||
export function postMessage(data: Message): boolean { | ||
export function postMessage(data: AllMessages): boolean { | ||
if (!window.parent || window.parent === window) return false; | ||
window.parent.postMessage(JSON.stringify(data), '*'); | ||
window.parent.postMessage(data, '*'); | ||
return true; | ||
} | ||
|
||
/** | ||
* Parses data from post message that was received in iframe | ||
* | ||
* @param evt post message event | ||
* @returns | ||
*/ | ||
export function parseMessage<T>({ data }: MessageEvent<T>): T { | ||
if (typeof data !== 'object' || data === null || Array.isArray(data)) { | ||
return {} as T; | ||
} | ||
|
||
return data as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters