2021-05-28 00:34:40 +02:00
|
|
|
import type { ChatEvent } from '../Events/ChatEvent'
|
2021-05-25 13:47:41 +02:00
|
|
|
import { isUserInputChatEvent, UserInputChatEvent } from '../Events/UserInputChatEvent'
|
2021-05-28 00:24:08 +02:00
|
|
|
import { apiCallback, IframeApiContribution, sendToWorkadventure } from './IframeApiContribution'
|
2021-05-25 13:47:41 +02:00
|
|
|
|
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
class WorkadvntureChatCommands extends IframeApiContribution<WorkadvntureChatCommands> {
|
|
|
|
readonly subObjectIdentifier = 'chat'
|
|
|
|
|
|
|
|
readonly addMethodsAtRoot = true
|
|
|
|
|
|
|
|
chatMessageCallback?: (event: string) => void
|
|
|
|
|
|
|
|
callbacks = [apiCallback({
|
|
|
|
callback: (event: UserInputChatEvent) => {
|
|
|
|
this.chatMessageCallback?.(event.message)
|
|
|
|
},
|
|
|
|
type: "userInputChat",
|
|
|
|
typeChecker: isUserInputChatEvent
|
|
|
|
})]
|
|
|
|
|
2021-05-25 13:47:41 +02:00
|
|
|
|
|
|
|
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) {
|
2021-05-28 00:24:08 +02:00
|
|
|
this.chatMessageCallback = callback
|
2021-05-25 13:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
export default new WorkadvntureChatCommands()
|