diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 42cc34e1..0da0099f 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -43,13 +43,10 @@ type SubObjectTypes = { [importCl in WorkadventureCommandClasses as importCl["subObjectIdentifier"]]: JustMethods; }; -type WorkAdventureApiFiles = { +type WorkAdventureApi = { [Key in WorkadventureFunctionsFilteredByRoot]: ShouldAddAttribute } & SubObjectTypes -export interface WorkAdventureApi extends WorkAdventureApiFiles { - -} declare global { @@ -59,11 +56,31 @@ declare global { let WA: WorkAdventureApi } +async function populateWa(): Promise { + const wa: Partial = {} + for (const apiImport of await importType) { + const classInstance = apiImport.default + const commandPrototype = Object.getPrototypeOf(classInstance); + const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor"); + const importObject: Partial = {} + for (const prop of commandClassPropertyNames) { + const apiImportKey = prop as keyof typeof classInstance; + if (typeof classInstance[apiImportKey] === "function") { + importObject[apiImportKey as keyof WorkAdventureApi] = commandPrototype[apiImportKey] as never + } + } + wa[classInstance.subObjectIdentifier] = importObject as never + if (classInstance.addMethodsAtRoot) { + Object.assign(wa, importObject) + } + } -window.WA = { - ...({} as WorkAdventureApiFiles), + window.WA = Object.assign({}, wa) as WorkAdventureApi } + +populateWa() + window.addEventListener('message', message => { if (message.source !== window.parent) { return; // Skip message in this event listener