4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 dragonblocks.Chat = class
30 this.lines = dragonblocks.settings.chat.lines;
35 dragonblocks.keyHandler.down("t", _ => {
36 dragonblocks.chat.open();
39 dragonblocks.keyHandler.down("/", event => {
40 dragonblocks.chat.open();
41 document.getElementById("dragonblocks.chat.input").value = "/";
47 let display = document.body.appendChild(document.createElement("div"));
48 display.id = "dragonblocks.chat";
49 display.style.position = "fixed";
50 display.style.top = "0px";
51 display.style.left = "0px";
52 display.style.backgroundColor = "black";
53 display.style.opacity = "0.7";
54 display.style.height = 23 * this.lines + "px";
55 display.style.fontSize = "20px";
56 display.style.color = "white";
57 display.style.width = "100%";
58 display.style.fontFamily = "monospace";
59 display.style.overflowY = "scroll";
60 display.style.scrollbarWidth = "none";
61 display.style.visibility = "hidden";
63 let input = document.body.appendChild(document.createElement("input"));
64 input.id = "dragonblocks.chat.input";
65 input.style.position = "fixed";
66 input.style.top = 23 * this.lines + "px";
67 input.style.left = "0px";
68 input.style.backgroundColor = "black";
69 input.style.border = "none";
70 input.style.outline = "none";
71 input.style.opacity = "0.7";
72 input.style.fontSize = "20px";
73 input.style.color = "white";
74 input.style.height = "23px";
75 input.style.width = "100%";
76 input.style.caretWidth = "30px";
77 input.style.caretHeight = "20px";
78 input.style.fontFamily = "monospace";
79 input.style.visibility = "hidden";
83 input.addEventListener("keydown", event => {
86 let message = event.srcElement.value;
87 event.srcElement.value = "";
92 self.input[self.input.length - 1] = message;
104 event.srcElement.value = self.historyUp();
108 event.srcElement.value = self.historyDown();
113 input.addEventListener("input", event => {
114 self.input[self.input.length - 1] = event.srcElement.value;
120 dragonblocks.keyHandler.lockAll();
122 document.getElementById("dragonblocks.chat").style.visibility = "visible";
124 let input = document.getElementById("dragonblocks.chat.input");
125 input.style.visibility = "visible";
132 dragonblocks.keyHandler.unlockAll();
135 document.getElementById("dragonblocks.chat").style.visibility = "hidden";
137 let input = document.getElementById("dragonblocks.chat.input");
138 input.style.visibility = "hidden";
146 if (text.startsWith("!HTML"))
147 text = text.replace("!HTML", "");
149 text = dblib.htmlEntities(text);
153 let display = document.getElementById("dragonblocks.chat");
154 display.innerHTML += text;
155 display.lastChild.scrollIntoView();
160 for (let func of dragonblocks.onChatMessageCallbacks)
161 if (func(input) == false)
171 if (this.input[this.input.length + this.history] == undefined)
174 return this.input[this.input.length + this.history];
181 if (this.input[this.input.length + this.history] == undefined)
184 return this.input[this.input.length + this.history];
189 document.getElementById("dragonblocks.chat").innerHTML = "<br>".repeat(this.lines);
193 dragonblocks.chat = new dragonblocks.Chat();
195 dragonblocks.chatMessage = msg => {
196 dragonblocks.chat.write(msg);
199 dragonblocks.onChatMessageCallbacks = [];
201 dragonblocks.registerOnChatMessage = func => {
202 dragonblocks.onChatMessageCallbacks.push(func);