script api can add menu commands

# Conflicts:
#	front/src/Api/IframeListener.ts
#	front/src/iframe_api.ts
This commit is contained in:
jonny 2021-04-27 11:59:22 +02:00
parent ca628d9a90
commit fafaabb6e7
5 changed files with 84 additions and 5 deletions

View file

@ -9,6 +9,8 @@ import {ClosePopupEvent} from "./Api/Events/ClosePopupEvent";
import {OpenTabEvent} from "./Api/Events/OpenTabEvent";
import {GoToPageEvent} from "./Api/Events/GoToPageEvent";
import {OpenCoWebSiteEvent} from "./Api/Events/OpenCoWebSiteEvent";
import { isMenuItemClickedEvent } from './Api/Events/MenuItemClickedEvent';
import { MenuItemRegisterEvent } from './Api/Events/MenuItemRegisterEvent';
interface WorkAdventureApi {
sendChatMessage(message: string, author: string): void;
@ -24,6 +26,7 @@ interface WorkAdventureApi {
restorePlayerControl() : void;
displayBubble() : void;
removeBubble() : void;
registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void): void
}
declare global {
@ -40,7 +43,7 @@ const enterStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subj
const leaveStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
const popups: Map<number, Popup> = new Map<number, Popup>();
const popupCallbacks: Map<number, Map<number, ButtonClickedCallback>> = new Map<number, Map<number, ButtonClickedCallback>>();
const menuCallbacks: Map<string, (command: string) => void> = new Map()
let popupId = 0;
interface ButtonDescriptor {
/**
@ -172,6 +175,16 @@ window.WA = {
popups.set(popupId, popup)
return popup;
},
registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void) {
menuCallbacks.set(commandDescriptor, callback);
window.parent.postMessage({
'type': 'registerMenuCommand',
'data': {
menutItem: commandDescriptor
} as MenuItemRegisterEvent
}, '*');
},
/**
* Listen to messages sent by the local user, in the chat.
*/
@ -224,8 +237,12 @@ window.addEventListener('message', message => {
if (callback) {
callback(popup);
}
} else if (payload.type == "menuItemClicked" && isMenuItemClickedEvent(payload.data)) {
const callback = menuCallbacks.get(payload.data.menuItem);
if (callback) {
callback(payload.data.menuItem)
}
}
}
// ...