| | |
| | | /** |
| | | * 设置全局函数,用于 Child 注册 |
| | | */ |
| | | export const registerChildFunction = () => { |
| | | /** @description 把 Child 的函数注册过来, 方便自己调用 */ |
| | | (window as any).register = (funObj: any) => { |
| | | console.log('register children function obj', funObj); |
| | | (window as any).childCallObj = funObj; |
| | | }; |
| | | }; |
| | | |
| | | /** |
| | | * 返回子 iframe 调用功能列表 |
| | | */ |
| | | export const getChildCallObj = () => { |
| | | return (window as any)?.childCallObj ?? {}; |
| | | const notifyFunc = (type, jsonArgs) => { |
| | | console.log('message from parent🚀 ~ type:', type); |
| | | // 执行AI通知消息 |
| | | }; |
| | | |
| | | /** |
| | | * 返回一个对象,给 child 调用 |
| | | * 此对象包含多个函数 |
| | | * @returns |
| | | */ |
| | | const parentCallObj = { |
| | | /** @description 测试函数 */ |
| | | test: (msg: string) => { |
| | | console.log('test 函数来自 parent', msg); |
| | | }, |
| | | }; |
| | | /** |
| | | * 往 windows 上加函数,给 child 调用 |
| | | */ |
| | | export const setParentFunction = () => { |
| | | let child: any; |
| | | for (let index = 0; index < window.frames.length; index++) { |
| | | const frame = window.frames[index]; |
| | | if ((frame as any).register) { |
| | | child = frame; |
| | | break; |
| | | export class ChildRegister { |
| | | static childCallObj = null; |
| | | static registerNotifyFunction() { |
| | | for (let index = 0; index < window.frames.length; index++) { |
| | | const frame = window.frames[index]; |
| | | if ((frame as any).registerNotifyFunction) { |
| | | ChildRegister.childCallObj = (frame as any).registerNotifyFunction(notifyFunc); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (!child) return; |
| | | |
| | | child.register?.(parentCallObj); |
| | | }; |
| | | } |