this should make one file only
This commit is contained in:
parent
8a1376e966
commit
ed39043bbe
2
front/src/iframe_api.d.ts
vendored
2
front/src/iframe_api.d.ts
vendored
@ -1,7 +1,7 @@
|
|||||||
import type { WorkadventureImport } from './iframe_api';
|
import type { WorkadventureImport } from './iframe_api';
|
||||||
|
|
||||||
type PromiseReturnType<P> = P extends Promise<infer T> ? T : P;
|
type PromiseReturnType<P> = P extends Promise<infer T> ? T : P;
|
||||||
type WorkadventureCommandClasses = PromiseReturnType<WorkadventureImport>[number]["default"];
|
type WorkadventureCommandClasses = PromiseReturnType<WorkadventureImport>[number];
|
||||||
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
||||||
type ObjectWithKeyOfUnion<Key, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O[Key] : never) : never;
|
type ObjectWithKeyOfUnion<Key, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O[Key] : never) : never;
|
||||||
type ApiKeys = KeysOfUnion<WorkadventureCommandClasses>;
|
type ApiKeys = KeysOfUnion<WorkadventureCommandClasses>;
|
||||||
|
@ -1,22 +1,23 @@
|
|||||||
import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent";
|
import { IframeResponseEvent, IframeResponseEventMap, isIframeResponseEventWrapper, TypedMessageEvent } from "./Api/Events/IframeEvent";
|
||||||
|
import Bubble from "./Api/iframe/Bubble";
|
||||||
|
import chatmessage from "./Api/iframe/chatmessage";
|
||||||
|
import CoWebsite from "./Api/iframe/CoWebsite";
|
||||||
import type { IframeCallback } from './Api/iframe/IframeApiContribution';
|
import type { IframeCallback } from './Api/iframe/IframeApiContribution';
|
||||||
|
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";
|
||||||
import type { WorkAdventureApi } from './iframe_api.d';
|
import type { WorkAdventureApi } from './iframe_api.d';
|
||||||
|
|
||||||
|
|
||||||
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
|
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: IframeCallback<K> } = {}
|
||||||
|
|
||||||
const importType = Promise.all([
|
const apis = [
|
||||||
import("./Api/iframe/popup"),
|
popupApi, Navigation, Player, Bubble,
|
||||||
import("./Api/iframe/chatmessage"),
|
chatmessage, Sound, zoneRvents, CoWebsite
|
||||||
import("./Api/iframe/Sound"),
|
]
|
||||||
import("./Api/iframe/zone-events"),
|
|
||||||
import("./Api/iframe/Navigation"),
|
|
||||||
import("./Api/iframe/CoWebsite"),
|
|
||||||
import("./Api/iframe/Player"),
|
|
||||||
import("./Api/iframe/Bubble")
|
|
||||||
])
|
|
||||||
|
|
||||||
export type WorkadventureImport = typeof importType
|
export type WorkadventureImport = typeof apis
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
|
||||||
@ -26,30 +27,24 @@ declare global {
|
|||||||
let WA: WorkAdventureApi
|
let WA: WorkAdventureApi
|
||||||
}
|
}
|
||||||
|
|
||||||
async function populateWa(): Promise<void> {
|
const wa: Partial<WorkAdventureApi> = {}
|
||||||
const wa: Partial<WorkAdventureApi> = {}
|
for (const apiImport of apis) {
|
||||||
for (const apiImport of await importType) {
|
const commandPrototype = Object.getPrototypeOf(apiImport);
|
||||||
const classInstance = apiImport.default
|
|
||||||
const commandPrototype = Object.getPrototypeOf(classInstance);
|
|
||||||
const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor");
|
const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor");
|
||||||
const importObject: Partial<WorkAdventureApi> = {}
|
const importObject: Partial<WorkAdventureApi> = {}
|
||||||
for (const prop of commandClassPropertyNames) {
|
for (const prop of commandClassPropertyNames) {
|
||||||
const apiImportKey = prop as keyof typeof classInstance;
|
const apiImportKey = prop as keyof typeof apiImport;
|
||||||
if (typeof classInstance[apiImportKey] === "function") {
|
if (typeof apiImport[apiImportKey] === "function") {
|
||||||
importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never
|
importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wa[classInstance.subObjectIdentifier] = importObject as never
|
wa[apiImport.subObjectIdentifier] = importObject as never
|
||||||
if (classInstance.addMethodsAtRoot) {
|
if (apiImport.addMethodsAtRoot) {
|
||||||
Object.assign(wa, importObject)
|
Object.assign(wa, importObject)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
window.WA = Object.assign({}, wa) as WorkAdventureApi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.WA = Object.assign({}, wa) as WorkAdventureApi
|
||||||
populateWa()
|
|
||||||
|
|
||||||
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
window.addEventListener('message', <T extends keyof IframeResponseEventMap>(message: TypedMessageEvent<IframeResponseEvent<T>>) => {
|
||||||
if (message.source !== window.parent) {
|
if (message.source !== window.parent) {
|
||||||
|
Loading…
Reference in New Issue
Block a user