2020-09-21 00:34:25 +02:00
|
|
|
import {GameScene} from "../Phaser/Game/GameScene";
|
|
|
|
|
2020-09-20 19:31:24 +02:00
|
|
|
const Quill = require("quill");
|
|
|
|
|
2020-09-20 17:12:27 +02:00
|
|
|
import {HtmlUtils} from "../WebRtc/HtmlUtils";
|
2020-09-16 21:50:04 +02:00
|
|
|
import {Connection, GlobalMessageInterface} from "../Connection";
|
2020-09-21 00:34:25 +02:00
|
|
|
import {UserInputManager} from "../Phaser/UserInput/UserInputManager";
|
2020-09-16 21:50:04 +02:00
|
|
|
|
|
|
|
export const CLASS_CONSOLE_MESSAGE = 'main-console';
|
|
|
|
export const INPUT_CONSOLE_MESSAGE = 'input-send-text';
|
|
|
|
export const UPLOAD_CONSOLE_MESSAGE = 'input-upload-music';
|
|
|
|
export const BUTTON_CONSOLE_SEND = 'button-send';
|
|
|
|
export const INPUT_TYPE_CONSOLE = 'input-type';
|
|
|
|
|
|
|
|
export const AUDIO_TYPE = 'audio';
|
|
|
|
export const MESSAGE_TYPE = 'message';
|
|
|
|
|
|
|
|
export class ConsoleGlobalMessageManager {
|
|
|
|
|
|
|
|
private Connection: Connection;
|
2020-09-19 01:08:56 +02:00
|
|
|
private divMainConsole: HTMLDivElement;
|
|
|
|
private buttonMainConsole: HTMLDivElement;
|
|
|
|
private activeConsole: boolean = false;
|
2020-09-21 00:34:25 +02:00
|
|
|
private userInputManager!: UserInputManager;
|
2020-09-16 21:50:04 +02:00
|
|
|
|
2020-09-21 00:34:25 +02:00
|
|
|
constructor(Connection: Connection, userInputManager : UserInputManager) {
|
2020-09-16 21:50:04 +02:00
|
|
|
this.Connection = Connection;
|
2020-09-19 01:08:56 +02:00
|
|
|
this.buttonMainConsole = document.createElement('div');
|
2020-09-20 19:31:24 +02:00
|
|
|
this.buttonMainConsole.classList.add('console');
|
2020-09-19 01:08:56 +02:00
|
|
|
this.divMainConsole = document.createElement('div');
|
2020-09-21 00:34:25 +02:00
|
|
|
this.userInputManager = userInputManager;
|
2020-09-16 21:50:04 +02:00
|
|
|
this.initialise();
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:08:56 +02:00
|
|
|
initialise() {
|
2020-09-20 17:12:27 +02:00
|
|
|
const buttonText = document.createElement('p');
|
2020-09-16 21:50:04 +02:00
|
|
|
buttonText.innerText = 'Console';
|
|
|
|
|
2020-09-19 01:08:56 +02:00
|
|
|
this.buttonMainConsole.appendChild(buttonText);
|
|
|
|
this.buttonMainConsole.addEventListener('click', () => {
|
|
|
|
if(this.activeConsole){
|
|
|
|
this.disabled();
|
|
|
|
}else{
|
|
|
|
this.active();
|
|
|
|
}
|
|
|
|
});
|
2020-09-16 21:50:04 +02:00
|
|
|
|
2020-09-19 01:08:56 +02:00
|
|
|
this.divMainConsole.className = CLASS_CONSOLE_MESSAGE;
|
|
|
|
this.divMainConsole.appendChild(this.buttonMainConsole);
|
|
|
|
|
|
|
|
this.createTextMessagePart();
|
2020-09-16 21:50:04 +02:00
|
|
|
|
|
|
|
const mainSectionDiv = HtmlUtils.getElementByIdOrFail<HTMLDivElement>('main-container');
|
2020-09-19 01:08:56 +02:00
|
|
|
mainSectionDiv.appendChild(this.divMainConsole);
|
|
|
|
}
|
|
|
|
|
|
|
|
createTextMessagePart(){
|
2020-09-20 19:31:24 +02:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = INPUT_CONSOLE_MESSAGE;
|
2020-09-19 01:08:56 +02:00
|
|
|
|
2020-09-20 19:31:24 +02:00
|
|
|
const buttonSend = document.createElement('button');
|
2020-09-19 01:08:56 +02:00
|
|
|
buttonSend.innerText = 'Envoyer';
|
2020-09-21 00:34:25 +02:00
|
|
|
buttonSend.classList.add('btn');
|
2020-09-19 01:08:56 +02:00
|
|
|
buttonSend.addEventListener('click', (event: MouseEvent) => {
|
|
|
|
this.sendMessage();
|
|
|
|
this.disabled();
|
|
|
|
});
|
2020-09-21 00:34:25 +02:00
|
|
|
const buttonDiv = document.createElement('div');
|
|
|
|
buttonDiv.classList.add('btn-action');
|
|
|
|
buttonDiv.appendChild(buttonSend)
|
2020-09-19 01:08:56 +02:00
|
|
|
|
|
|
|
const typeConsole = document.createElement('input');
|
|
|
|
typeConsole.id = INPUT_TYPE_CONSOLE;
|
|
|
|
typeConsole.value = MESSAGE_TYPE;
|
|
|
|
typeConsole.type = 'hidden';
|
2020-09-20 19:31:24 +02:00
|
|
|
|
|
|
|
const section = document.createElement('section');
|
|
|
|
section.appendChild(div);
|
2020-09-21 00:34:25 +02:00
|
|
|
section.appendChild(buttonDiv);
|
|
|
|
section.appendChild(typeConsole);
|
2020-09-20 19:31:24 +02:00
|
|
|
this.divMainConsole.appendChild(section);
|
|
|
|
|
|
|
|
//TODO refactor
|
|
|
|
setTimeout(() => {
|
|
|
|
const toolbarOptions = [
|
|
|
|
['bold', 'italic', 'underline', 'strike'], // toggled buttons
|
|
|
|
['blockquote', 'code-block'],
|
|
|
|
|
|
|
|
[{ 'header': 1 }, { 'header': 2 }], // custom button values
|
|
|
|
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
|
|
|
|
[{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript
|
|
|
|
[{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent
|
|
|
|
[{ 'direction': 'rtl' }], // text direction
|
|
|
|
|
|
|
|
[{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
|
|
|
|
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
|
|
|
|
|
|
|
|
[{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
|
|
|
|
[{ 'font': [] }],
|
|
|
|
[{ 'align': [] }],
|
|
|
|
|
|
|
|
['clean'],
|
|
|
|
|
|
|
|
['link', 'image', 'video']
|
|
|
|
// remove formatting button
|
|
|
|
];
|
|
|
|
|
|
|
|
let quill = new Quill(`#${INPUT_CONSOLE_MESSAGE}`, {
|
|
|
|
theme: 'snow',
|
|
|
|
modules: {
|
|
|
|
toolbar: toolbarOptions
|
|
|
|
},
|
|
|
|
});
|
2020-09-21 00:34:25 +02:00
|
|
|
|
2020-09-20 19:31:24 +02:00
|
|
|
}, 1000);
|
2020-09-16 21:50:04 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 01:08:56 +02:00
|
|
|
sendMessage(){
|
|
|
|
const inputType = HtmlUtils.getElementByIdOrFail<HTMLInputElement>(INPUT_TYPE_CONSOLE);
|
|
|
|
if(AUDIO_TYPE !== inputType.value && MESSAGE_TYPE !== inputType.value){
|
2020-09-16 21:50:04 +02:00
|
|
|
throw "Error event type";
|
|
|
|
}
|
2020-09-20 19:01:21 +02:00
|
|
|
if(AUDIO_TYPE === inputType.value){
|
2020-09-20 17:12:27 +02:00
|
|
|
return this.sendAudioMessage();
|
|
|
|
}
|
|
|
|
return this.sendTextMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendTextMessage(){
|
|
|
|
const inputText = HtmlUtils.getElementByIdOrFail<HTMLTextAreaElement>(INPUT_CONSOLE_MESSAGE);
|
2020-09-16 21:50:04 +02:00
|
|
|
let GlobalMessage : GlobalMessageInterface = {
|
|
|
|
id: 1,
|
2020-09-19 01:08:56 +02:00
|
|
|
message: inputText.value,
|
2020-09-20 17:12:27 +02:00
|
|
|
type: MESSAGE_TYPE
|
2020-09-16 21:50:04 +02:00
|
|
|
};
|
2020-09-19 01:08:56 +02:00
|
|
|
inputText.value = '';
|
2020-09-16 21:50:04 +02:00
|
|
|
this.Connection.emitGlobalMessage(GlobalMessage);
|
|
|
|
}
|
2020-09-19 01:08:56 +02:00
|
|
|
|
2020-09-20 17:12:27 +02:00
|
|
|
private async sendAudioMessage(){
|
2020-09-20 19:01:21 +02:00
|
|
|
const inputAudio = HtmlUtils.getElementByIdOrFail<HTMLInputElement>(UPLOAD_CONSOLE_MESSAGE);
|
|
|
|
const selectedFile = inputAudio.files ? inputAudio.files[0] : null;
|
|
|
|
if(!selectedFile){
|
|
|
|
throw 'no file selected';
|
|
|
|
}
|
|
|
|
|
|
|
|
const fd = new FormData();
|
|
|
|
fd.append('file', selectedFile);
|
|
|
|
let res = await this.Connection.uploadAudio(fd);
|
2020-09-20 17:12:27 +02:00
|
|
|
|
|
|
|
let GlobalMessage : GlobalMessageInterface = {
|
|
|
|
id: res.id,
|
2020-09-20 19:01:21 +02:00
|
|
|
message: res.path,
|
2020-09-20 17:12:27 +02:00
|
|
|
type: MESSAGE_TYPE
|
|
|
|
};
|
|
|
|
inputAudio.value = '';
|
|
|
|
this.Connection.emitGlobalMessage(GlobalMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-19 01:08:56 +02:00
|
|
|
active(){
|
2020-09-21 00:34:25 +02:00
|
|
|
this.userInputManager.clearAllInputKeyboard();
|
2020-09-19 01:08:56 +02:00
|
|
|
this.activeConsole = true;
|
|
|
|
this.divMainConsole.style.top = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
disabled(){
|
2020-09-21 00:34:25 +02:00
|
|
|
this.userInputManager.initKeyBoardEvent();
|
2020-09-19 01:08:56 +02:00
|
|
|
this.activeConsole = false;
|
|
|
|
this.divMainConsole.style.top = '-80%';
|
|
|
|
}
|
2020-09-16 21:50:04 +02:00
|
|
|
}
|