2021-05-09 21:30:29 +02:00
|
|
|
import { Subject } from "rxjs";
|
2021-05-12 09:13:25 +02:00
|
|
|
import type { GoToPageEvent } from "./Api/Events/GoToPageEvent";
|
2021-05-28 00:34:40 +02:00
|
|
|
import { IframeResponseEventMap, isIframeResponseEventWrapper } from "./Api/Events/IframeEvent";
|
2021-05-12 09:13:25 +02:00
|
|
|
import type { OpenCoWebSiteEvent } from "./Api/Events/OpenCoWebSiteEvent";
|
2021-05-28 00:34:40 +02:00
|
|
|
import type { OpenTabEvent } from "./Api/Events/OpenTabEvent";
|
|
|
|
import { isUserInputChatEvent, UserInputChatEvent } from "./Api/Events/UserInputChatEvent";
|
|
|
|
|
|
|
|
export const registeredCallbacks: { [K in keyof IframeResponseEventMap]?: {
|
|
|
|
typeChecker: Function
|
|
|
|
callback: Function
|
|
|
|
} } = {}
|
2021-03-05 16:50:54 +01:00
|
|
|
|
2021-05-25 13:47:41 +02:00
|
|
|
const importType = Promise.all([
|
2021-05-28 00:24:08 +02:00
|
|
|
import("./Api/iframe/popup"),
|
2021-05-25 13:47:41 +02:00
|
|
|
import("./Api/iframe/chatmessage"),
|
|
|
|
import("./Api/iframe/zone-events")
|
|
|
|
])
|
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
type PromiseReturnType<P> = P extends Promise<infer T> ? T : P
|
|
|
|
|
|
|
|
type WorkadventureCommandClasses = PromiseReturnType<typeof importType>[number]["default"];
|
|
|
|
|
2021-05-25 13:47:41 +02:00
|
|
|
type KeysOfUnion<T> = T extends T ? keyof T : never
|
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
type ObjectWithKeyOfUnion<Key, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O[Key] : never) : never
|
|
|
|
|
|
|
|
type ApiKeys = KeysOfUnion<WorkadventureCommandClasses>;
|
|
|
|
|
|
|
|
type ObjectOfKey<Key extends ApiKeys, O = WorkadventureCommandClasses> = O extends O ? (Key extends keyof O ? O : never) : never
|
|
|
|
|
|
|
|
type ShouldAddAttribute<Key extends ApiKeys> = ObjectWithKeyOfUnion<Key>;
|
|
|
|
|
|
|
|
type WorkadventureFunctions = { [K in ApiKeys]: ObjectWithKeyOfUnion<K> extends Function ? K : never }[ApiKeys]
|
|
|
|
|
|
|
|
type WorkadventureFunctionsFilteredByRoot = { [K in WorkadventureFunctions]: ObjectOfKey<K>["addMethodsAtRoot"] extends true ? K : never }[WorkadventureFunctions]
|
|
|
|
|
|
|
|
|
|
|
|
type JustMethodKeys<T> = ({ [P in keyof T]: T[P] extends Function ? P : never })[keyof T];
|
|
|
|
type JustMethods<T> = Pick<T, JustMethodKeys<T>>;
|
|
|
|
|
|
|
|
type SubObjectTypes = {
|
|
|
|
[importCl in WorkadventureCommandClasses as importCl["subObjectIdentifier"]]: JustMethods<importCl>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type WorkAdventureApiFiles = {
|
|
|
|
[Key in WorkadventureFunctionsFilteredByRoot]: ShouldAddAttribute<Key>
|
|
|
|
} & SubObjectTypes
|
2021-05-25 13:47:41 +02:00
|
|
|
|
|
|
|
export interface WorkAdventureApi extends WorkAdventureApiFiles {
|
2021-05-28 00:34:40 +02:00
|
|
|
openTab(url: string): void;
|
|
|
|
goToPage(url: string): void;
|
|
|
|
openCoWebSite(url: string): void;
|
2021-03-25 17:12:53 +01:00
|
|
|
closeCoWebSite(): void;
|
2021-05-10 12:14:31 +02:00
|
|
|
disablePlayerControls(): void;
|
|
|
|
restorePlayerControls(): void;
|
2021-05-09 21:30:29 +02:00
|
|
|
displayBubble(): void;
|
|
|
|
removeBubble(): void;
|
2021-03-04 19:00:00 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 13:47:41 +02: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-03-06 15:26:07 +01:00
|
|
|
const userInputChatStream: Subject<UserInputChatEvent> = new Subject();
|
2021-03-09 18:05:07 +01:00
|
|
|
|
2021-03-06 15:26:07 +01:00
|
|
|
|
2021-03-09 18:51:30 +01:00
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
|
2021-03-09 18:51:30 +01:00
|
|
|
|
2021-03-04 19:00:00 +01:00
|
|
|
window.WA = {
|
2021-05-10 12:14:31 +02:00
|
|
|
disablePlayerControls(): void {
|
|
|
|
window.parent.postMessage({ 'type': 'disablePlayerControls' }, '*');
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-05-10 12:14:31 +02:00
|
|
|
restorePlayerControls(): void {
|
|
|
|
window.parent.postMessage({ 'type': 'restorePlayerControls' }, '*');
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
displayBubble(): void {
|
|
|
|
window.parent.postMessage({ 'type': 'displayBubble' }, '*');
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
removeBubble(): void {
|
|
|
|
window.parent.postMessage({ 'type': 'removeBubble' }, '*');
|
2021-03-12 16:39:29 +01:00
|
|
|
},
|
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
openTab(url: string): void {
|
2021-03-25 17:12:53 +01:00
|
|
|
window.parent.postMessage({
|
2021-05-09 21:30:29 +02:00
|
|
|
"type": 'openTab',
|
|
|
|
"data": {
|
2021-03-25 17:12:53 +01:00
|
|
|
url
|
|
|
|
} as OpenTabEvent
|
2021-05-09 21:30:29 +02:00
|
|
|
}, '*');
|
2021-03-25 17:12:53 +01:00
|
|
|
},
|
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
goToPage(url: string): void {
|
2021-03-25 17:12:53 +01:00
|
|
|
window.parent.postMessage({
|
2021-05-09 21:30:29 +02:00
|
|
|
"type": 'goToPage',
|
|
|
|
"data": {
|
2021-03-25 17:12:53 +01:00
|
|
|
url
|
|
|
|
} as GoToPageEvent
|
2021-05-09 21:30:29 +02:00
|
|
|
}, '*');
|
2021-03-25 17:12:53 +01:00
|
|
|
},
|
|
|
|
|
2021-05-28 00:34:40 +02:00
|
|
|
openCoWebSite(url: string): void {
|
2021-03-25 17:12:53 +01:00
|
|
|
window.parent.postMessage({
|
2021-05-28 00:34:40 +02:00
|
|
|
"type": 'openCoWebSite',
|
|
|
|
"data": {
|
2021-03-25 17:12:53 +01:00
|
|
|
url
|
|
|
|
} as OpenCoWebSiteEvent
|
2021-05-09 21:30:29 +02:00
|
|
|
}, '*');
|
2021-03-25 17:12:53 +01:00
|
|
|
},
|
|
|
|
|
2021-05-09 21:30:29 +02:00
|
|
|
closeCoWebSite(): void {
|
2021-03-25 17:12:53 +01:00
|
|
|
window.parent.postMessage({
|
2021-05-09 21:30:29 +02:00
|
|
|
"type": 'closeCoWebSite'
|
|
|
|
}, '*');
|
2021-03-25 17:12:53 +01:00
|
|
|
},
|
2021-05-25 13:47:41 +02:00
|
|
|
...({} as WorkAdventureApiFiles),
|
2021-03-04 19:00:00 +01:00
|
|
|
}
|
2021-03-06 15:26:07 +01:00
|
|
|
|
|
|
|
window.addEventListener('message', message => {
|
|
|
|
if (message.source !== window.parent) {
|
|
|
|
return; // Skip message in this event listener
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = message.data;
|
2021-03-08 18:57:59 +01:00
|
|
|
|
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-03-08 18:57:59 +01:00
|
|
|
if (payload.type === 'userInputChat' && isUserInputChatEvent(payloadData)) {
|
|
|
|
userInputChatStream.next(payloadData);
|
2021-03-06 15:26:07 +01:00
|
|
|
}
|
2021-03-12 16:39:29 +01:00
|
|
|
|
2021-03-06 15:26:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ...
|
2021-05-28 00:24:08 +02:00
|
|
|
});
|