60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
/**
|
||
* iframe 与宿主页通信消息类型常量
|
||
*
|
||
* 消息格式:{ type: MESSAGE_TYPE, ...其他字段 }
|
||
* 发送方式:window.parent.postMessage(message, "*")
|
||
*/
|
||
|
||
// 发送给宿主页的消息类型
|
||
export const POST_MESSAGE_TYPES = {
|
||
// 全屏切换
|
||
FULLSCREEN: "fullscreen",
|
||
// 选择预定义 skill
|
||
SELECT_SKILL: "selectSkill",
|
||
// 打开 skill 选择对话框
|
||
OPEN_SKILL_DIALOG: "openSkillDialog",
|
||
} as const;
|
||
|
||
// 接收来自宿主页的消息类型
|
||
export const RECEIVE_MESSAGE_TYPES = {
|
||
// 选中的 skill 数据
|
||
SELECTED_SKILL: "selectedSkill",
|
||
} as const;
|
||
|
||
// 消息类型
|
||
export type PostMessageType =
|
||
(typeof POST_MESSAGE_TYPES)[keyof typeof POST_MESSAGE_TYPES];
|
||
export type ReceiveMessageType =
|
||
(typeof RECEIVE_MESSAGE_TYPES)[keyof typeof RECEIVE_MESSAGE_TYPES];
|
||
|
||
// 消息数据类型
|
||
export interface FullscreenMessage {
|
||
type: typeof POST_MESSAGE_TYPES.FULLSCREEN;
|
||
fullscreen: boolean;
|
||
}
|
||
|
||
export interface SelectSkillMessage {
|
||
type: typeof POST_MESSAGE_TYPES.SELECT_SKILL;
|
||
skill_id: string;
|
||
}
|
||
|
||
export interface OpenSkillDialogMessage {
|
||
type: typeof POST_MESSAGE_TYPES.OPEN_SKILL_DIALOG;
|
||
openSkillDialog: true;
|
||
}
|
||
|
||
export interface SelectedSkillMessage {
|
||
type: typeof RECEIVE_MESSAGE_TYPES.SELECTED_SKILL;
|
||
id: string | number;
|
||
title: string;
|
||
}
|
||
|
||
// 发送消息的辅助函数
|
||
export function sendToParent(
|
||
message: FullscreenMessage | SelectSkillMessage | OpenSkillDialogMessage,
|
||
): void {
|
||
if (window.parent !== window) {
|
||
window.parent.postMessage(message, "*");
|
||
}
|
||
}
|