1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
package client;
import client.Main.getEl;
import haxe.Timer;
import js.Browser.document;
import js.Browser.window;
import js.html.Element;
import js.html.Event;
import js.html.InputElement;
import js.html.KeyboardEvent;
class ChatInput {
public final chatline:InputElement;
public final commandsWrap:Element;
public final commandsList:Element;
public final inputWithHistory:InputWithHistory;
final main:Main;
var filteredCommands:Array<Commands.CommandInfo> = [];
var selectedCommandIndex = -1;
public function new(main:Main) {
this.main = main;
chatline = getEl("#chatline");
commandsWrap = getEl("#chat-commands-wrap");
commandsList = getEl("#chat-commands-list");
setupFocusHandler();
setupAutocomplete();
inputWithHistory = new InputWithHistory(chatline, 50, onEnter, onInterceptKeyDown);
}
function setupFocusHandler():Void {
chatline.onfocus = e -> {
if (Utils.isIOS()) {
final startY = 0;
Timer.delay(() -> {
window.scrollBy(0, -(window.scrollY - startY));
getEl("#video").scrollTop = 0;
main.scrollChatToEnd();
}, 100);
} else if (Utils.isTouch()) {
main.scrollChatToEnd();
}
};
}
function setupAutocomplete():Void {
commandsList.addEventListener("mousemove", e -> {
commandsList.classList.remove("keyboard-nav");
});
chatline.addEventListener("input", e -> {
final val = chatline.value;
if (val.startsWith("/")) {
filteredCommands = Commands.filterForAutocomplete(val);
selectedCommandIndex = filteredCommands.length > 0 ? 0 : -1;
renderCommands();
} else {
commandsWrap.style.display = "none";
selectedCommandIndex = -1;
}
});
chatline.addEventListener("blur", e -> {
commandsWrap.style.display = "none";
});
}
function renderCommands():Void {
commandsList.innerHTML = "";
if (filteredCommands.length == 0) {
commandsWrap.style.display = "none";
return;
}
final parsed = Commands.parse(chatline.value);
final currentArgIndex = parsed.currentArgIndex;
for (i in 0...filteredCommands.length) {
final cmd = filteredCommands[i];
final item = document.createDivElement();
item.className = "chat-command-item" + (i == selectedCommandIndex ? " active" : "");
item.onclick = e -> {
chatline.value = cmd.name + (cmd.args > 0 ? " " : "");
chatline.focus();
if (cmd.args == 0) {
commandsWrap.style.display = "none";
selectedCommandIndex = -1;
} else {
chatline.dispatchEvent(new Event("input"));
}
return;
};
item.onmouseenter = e -> {
if (!commandsList.classList.contains("keyboard-nav")) {
selectedCommandIndex = i;
for (j in 0...commandsList.children.length) {
final child = commandsList.children[j];
if (j == i) child.classList.add("active");
else child.classList.remove("active");
}
}
};
final nameDiv = document.createDivElement();
nameDiv.className = "chat-command-item-name";
nameDiv.innerText = cmd.name.toString();
final descDiv = document.createDivElement();
descDiv.className = "chat-command-item-desc";
descDiv.innerHTML = Commands.formatDescWithHighlight(Lang.get(cmd.lang), currentArgIndex);
item.appendChild(nameDiv);
item.appendChild(descDiv);
commandsList.appendChild(item);
}
commandsWrap.style.display = "";
if (selectedCommandIndex >= 0 && commandsList.children.length > selectedCommandIndex) {
final selectedEl:Element = cast commandsList.children[selectedCommandIndex];
untyped selectedEl.scrollIntoView({block: "center"});
}
}
function onEnter(value:String):Bool {
if (main.handleCommands(value)) return true;
main.send({
type: Message,
message: {
clientName: "",
text: value
}
});
if (Utils.isTouch()) chatline.blur();
commandsWrap.style.display = "none";
return true;
}
function onInterceptKeyDown(e:KeyboardEvent):Bool {
if (commandsWrap.style.display == "none") return false;
final key:KeyCode = cast e.keyCode;
switch key {
case Up:
commandsList.classList.add("keyboard-nav");
if (selectedCommandIndex > 0) selectedCommandIndex--;
else selectedCommandIndex = filteredCommands.length - 1;
renderCommands();
e.preventDefault();
return true;
case Down:
commandsList.classList.add("keyboard-nav");
if (selectedCommandIndex < filteredCommands.length - 1) selectedCommandIndex++;
else selectedCommandIndex = 0;
renderCommands();
e.preventDefault();
return true;
case Return, Tab:
if (selectedCommandIndex < 0 || selectedCommandIndex >= filteredCommands.length) {
return false;
}
final selectedCmd = filteredCommands[selectedCommandIndex];
final val = chatline.value;
final hasSpace = val.indexOf(" ") >= 0;
if (key == Return) {
if (hasSpace
|| (selectedCmd.args == 0
&& val.trim().toLowerCase() == selectedCmd.name.toString())) {
return false;
}
}
if (!hasSpace) {
chatline.value = selectedCmd.name + (selectedCmd.args > 0 ? " " : "");
}
e.preventDefault();
chatline.dispatchEvent(new Event("input"));
return true;
case Escape:
commandsWrap.style.display = "none";
return false;
case _:
return false;
}
}
}
|