2021-05-28 02:28:11 +02:00
|
|
|
import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent";
|
2021-05-28 13:10:30 +02:00
|
|
|
import Bubble from "./Api/iframe/Bubble";
|
|
|
|
import chatmessage from "./Api/iframe/chatmessage";
|
|
|
|
import CoWebsite from "./Api/iframe/CoWebsite";
|
2021-05-28 02:28:11 +02:00
|
|
|
import type { IframeCallback } from './Api/iframe/IframeApiContribution';
|
2021-05-28 13:10:30 +02:00
|
|
|
import Navigation from "./Api/iframe/Navigation";
|
|
|
|
import Player from "./Api/iframe/Player";
|
|
|
|
import popupApi from "./Api/iframe/popup";
|
|
|
|
import Sound from "./Api/iframe/Sound";
|
|
|
|
import zoneRvents from "./Api/iframe/zone-events";
|
2021-05-28 01:59:43 +02:00
|
|
|
import type { WorkAdventureApi } from './iframe_api.d';
|
2021-05-28 01:14:10 +02:00
|
|
|
|
2021-05-28 02:28:11 +02:00
|
|
|
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
|
2021-03-05 16:50:54 +01:00
|
|
|
|
2021-05-28 13:10:30 +02:00
|
|
|
const apis = [
|
|
|
|
popupApi, Navigation, Player, Bubble,
|
|
|
|
chatmessage, Sound, zoneRvents, CoWebsite
|
|
|
|
]
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-05-28 13:10:30 +02:00
|
|
|
export type WorkadventureImport = typeof apis
|
2021-03-04 19:00:00 +01:00
|
|
|
|
2021-03-05 16:50:54 +01:00
|
|
|
declare global {
|
2021-03-25 17:12:53 +01:00
|
|
|
|
2021-05-25 13:47:41 +02:00
|
|
|
interface Window {
|
|
|
|
WA: WorkAdventureApi
|
|
|
|
}
|
|
|
|
let WA: WorkAdventureApi
|
2021-03-05 16:50:54 +01:00
|
|
|
}
|
2021-03-04 19:00:00 +01:00
|
|
|
|
2021-05-28 13:10:30 +02:00
|
|
|
const wa: Partial<WorkAdventureApi> = {}
|
|
|
|
for (const apiImport of apis) {
|
|
|
|
const commandPrototype = Object.getPrototypeOf(apiImport);
|
|
|
|
const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor");
|
|
|
|
const importObject: Partial<WorkAdventureApi> = {}
|
|
|
|
for (const prop of commandClassPropertyNames) {
|
|
|
|
const apiImportKey = prop as keyof typeof apiImport;
|
|
|
|
if (typeof apiImport[apiImportKey] === "function") {
|
|
|
|
importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never
|
2021-05-28 01:44:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 13:10:30 +02:00
|
|
|
wa[apiImport.subObjectIdentifier] = importObject as never
|
|
|
|
if (apiImport.addMethodsAtRoot) {
|
|
|
|
Object.assign(wa, importObject)
|
|
|
|
}
|
2021-03-04 19:00:00 +01:00
|
|
|
}
|
2021-03-06 15:26:07 +01:00
|
|
|
|
2021-05-28 13:10:30 +02:00
|
|
|
window.WA = Object.assign({}, wa) as WorkAdventureApi
|
2021-05-28 01:44:38 +02:00
|
|
|
|
2021-05-28 02:28:11 +02:00
|
|
|
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
2021-03-06 15:26:07 +01:00
|
|
|
if (message.source !== window.parent) {
|
|
|
|
return; // Skip message in this event listener
|
|
|
|
}
|
|
|
|
const payload = message.data;
|
2021-05-09 21:30:29 +02:00
|
|
|
console.debug(payload);
|
2021-03-08 18:57:59 +01:00
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
if (isIframeResponseEventWrapper(payload)) {
|
2021-03-08 18:57:59 +01:00
|
|
|
const payloadData = payload.data;
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-05-28 02:28:11 +02:00
|
|
|
const callback = registeredCallbacks[payload.type] as IframeCallback<T> | undefined
|
|
|
|
if (callback?.typeChecker(payloadData)) {
|
|
|
|
callback?.callback(payloadData)
|
2021-03-06 15:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
|
|
|
});
|