extraction idea
# Conflicts: # front/src/Api/ScriptUtils.ts # front/src/iframe_api.ts
This commit is contained in:
parent
fe573893a1
commit
1a1ab30574
4 changed files with 122 additions and 15 deletions
35
front/src/Api/iframe/chatmessage.ts
Normal file
35
front/src/Api/iframe/chatmessage.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { ChatEvent } from '../Events/ChatEvent'
|
||||
import { isUserInputChatEvent, UserInputChatEvent } from '../Events/UserInputChatEvent'
|
||||
import { registerWorkadventureCommand, registerWorkadvntureCallback, sendToWorkadventure } from "./iframe-registration"
|
||||
|
||||
let chatMessageCallback: (event: string) => void | undefined
|
||||
|
||||
class WorkadvntureChatCommands {
|
||||
|
||||
sendChatMessage(message: string, author: string) {
|
||||
sendToWorkadventure({
|
||||
type: 'chat',
|
||||
data: {
|
||||
'message': message,
|
||||
'author': author
|
||||
} as ChatEvent
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to messages sent by the local user, in the chat.
|
||||
*/
|
||||
onChatMessage(callback: (message: string) => void) {
|
||||
chatMessageCallback = callback
|
||||
}
|
||||
}
|
||||
|
||||
export const commands = registerWorkadventureCommand(new WorkadvntureChatCommands())
|
||||
export const callbacks = registerWorkadvntureCallback([{
|
||||
callback: (event: UserInputChatEvent) => {
|
||||
chatMessageCallback?.(event.message)
|
||||
},
|
||||
type: "userInputChat",
|
||||
typeChecker: isUserInputChatEvent
|
||||
}])
|
||||
|
30
front/src/Api/iframe/iframe-registration.ts
Normal file
30
front/src/Api/iframe/iframe-registration.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { IframeEvent, IframeEventMap, IframeResponseEventMap } from '../Events/IframeEvent';
|
||||
import { registeredCallbacks, WorkAdventureApi } from "../../iframe_api"
|
||||
export function registerWorkadventureCommand<T>(commnds: T): T {
|
||||
const commandPrototype = Object.getPrototypeOf(commnds);
|
||||
const commandClassPropertyNames = Object.getOwnPropertyNames(commandPrototype).filter(name => name !== "constructor");
|
||||
for (const key of commandClassPropertyNames) {
|
||||
window.WA[key as keyof WorkAdventureApi] = commandPrototype[key] as never
|
||||
}
|
||||
return commnds
|
||||
}
|
||||
|
||||
|
||||
export function registerWorkadvntureCallback<T extends Function>(callbacks: Array<{
|
||||
type: keyof IframeResponseEventMap,
|
||||
typeChecker: Function,
|
||||
callback: T
|
||||
}>) {
|
||||
for (const callback of callbacks) {
|
||||
registeredCallbacks[callback.type] = {
|
||||
typeChecker: callback.typeChecker,
|
||||
callback: callback.callback
|
||||
}
|
||||
}
|
||||
return callbacks
|
||||
}
|
||||
|
||||
|
||||
export function sendToWorkadventure(content: IframeEvent<keyof IframeEventMap>) {
|
||||
window.parent.postMessage(content, "*")
|
||||
}
|
27
front/src/Api/iframe/zone-events.ts
Normal file
27
front/src/Api/iframe/zone-events.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { EnterLeaveEvent, isEnterLeaveEvent } from '../Events/EnterLeaveEvent'
|
||||
import { registerWorkadventureCommand, registerWorkadvntureCallback, sendToWorkadventure } from "./iframe-registration"
|
||||
|
||||
class WorkadventureZoneCommands {
|
||||
|
||||
onEnterZone(name: string, callback: () => void): void {
|
||||
|
||||
|
||||
}
|
||||
onLeaveZone(name: string, callback: () => void): void {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export const commands = registerWorkadventureCommand(new WorkadventureZoneCommands())
|
||||
export const callbacks = registerWorkadvntureCallback([{
|
||||
callback: (enterEvent: EnterLeaveEvent) => {
|
||||
|
||||
},
|
||||
type: "enterEvent",
|
||||
typeChecker: isEnterLeaveEvent
|
||||
},])
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue