-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.ts
220 lines (186 loc) · 5.93 KB
/
search.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
let currentSearchResultUIs: HTMLElement[] = [];
let currentSearchResultCommands: Command[] = [];
let selectedSearchEntryIndex = -1;
function OnSearchFocus()
{
UpdateSearchResults();
mainInput.select();
searchResults.classList.remove("hidden");
ToggleTabindexRecursive(searchResults, true);
}
function OnSearchBlur()
{
setTimeout(() => {
if (mainInput == document.activeElement) return;
searchResults.classList.add("hidden");
}, 200);
ToggleTabindexRecursive(searchResults, false);
}
function OnSearchChange()
{
UpdateSearchResults();
selectedSearchEntryIndex = -1; // Reset selection
}
function OnSearchKey(event)
{
if (selectedSearchEntryIndex > -1)
{
currentSearchResultUIs[selectedSearchEntryIndex].classList.remove("selected");
}
// Move selected
if (event.key == "ArrowUp" && selectedSearchEntryIndex < currentSearchResultUIs.length - 1)
selectedSearchEntryIndex++;
if (event.key == "ArrowDown" && selectedSearchEntryIndex > -1)
selectedSearchEntryIndex--;
if (selectedSearchEntryIndex > -1)
{
currentSearchResultUIs[selectedSearchEntryIndex].classList.add("selected");
mainInput.value = currentSearchResultCommands[selectedSearchEntryIndex].name + " ";
// Using setTimeout because cursor will be displayed incorrectly otherwise
setTimeout(() => mainInput.setSelectionRange(mainInput.value.length, mainInput.value.length), 0);
}
if (event.key == "Enter")
{
ParseCommand(mainInput.value);
}
}
function ParseCommand(content)
{
let values = content.split(" ");
let command = GetCommandByName(values[0])
if (command == null)
{
infoLeft.textContent = "Invalid command name!"
}
else
{
let ok = true;
let parameter: any = null;
if (command.type != CommandType.function)
{
if (values.length < 2)
{
infoLeft.textContent = "You need to specify an argument for this command! Example: commandname [argument]"
ok = false;
}
else
{
if (command.type == CommandType.int) [parameter, ok] = TryParseInt(values[1]);
if (command.type == CommandType.float) [parameter, ok] = TryParseFloat(values[1]);
if (command.type == CommandType.vec2)
{
if (values.length == 2)
{
let val;
[val, ok] = TryParseInt(values[1]);
parameter = new vec2(val, val);
}
else if (values.length == 3)
{
let x, y;
[x, ok] = TryParseInt(values[1]);
[y, ok] = TryParseInt(values[2]);
parameter = new vec2(x, y);
}
else
{
infoLeft.textContent = "You need to specify a vector 2 argument for this command! Example: commandname [x] [y]"
ok = false;
}
}
}
}
if (ok)
{
command.Execute(parameter);
}
}
mainInput.blur();
}
function OnKeyPressed(event: KeyboardEvent)
{
let modifier = (event.altKey? 1 : 0) * alt
+ (event.ctrlKey? 1 : 0) * control
+ (event.shiftKey? 1 : 0) * shift;
let focusedOnInput = document.activeElement instanceof HTMLInputElement;
if (modifier == 0 && focusedOnInput && event.key != "Escape") return;
let command = GetCommandByHotkey(modifier, event.key.toLowerCase());
if (command != null)
{
if (command.type == CommandType.function)
{
command.Execute(null);
}
else
{
mainInput.value = command.name + " ";
mainInput.focus();
setTimeout(() => mainInput.selectionStart = mainInput.value.length, 0);
}
event.preventDefault();
}
}
function UpdateSearchResults()
{
searchResults.innerHTML = "";
let sortList: Command[];
if (mainInput.value.trim().length == 0)
{
sortList = commandHistory;
sortList.reverse();
}
else
{
sortList = [...Command.commands];
sortList.sort((a, b) => b.GetScore(mainInput.value) - a.GetScore(mainInput.value));
sortList = sortList.filter(command => command.GetScore(mainInput.value) > 0);
if (sortList.length == 0)
{
searchResults.innerHTML = `
<div class="search-entry">There are no commands matching this query :(</div>
`
}
}
currentSearchResultUIs = [];
currentSearchResultCommands = [];
for (let i = settings.maxSearchResults - 1; i >= 0; i--)
{
if (i < sortList.length)
{
let ui = sortList[i].GetUI(i == 0);
searchResults.appendChild(ui);
currentSearchResultUIs.unshift(ui);
currentSearchResultCommands.unshift(sortList[i]);
}
}
}
function GetCommandByName(searchName: string) : Command | null
{
for (let command of Command.commands)
{
if (command.name == searchName) return command;
}
return null;
}
function GetCommandByHotkey(modifier: number, key: string) : Command | null
{
for (let command of Command.commands)
{
if (command.modifier == modifier && command.key.toLowerCase() == key.toLowerCase()) return command;
}
return null;
}
function TryParseInt(text: string): [number, boolean]
{
let res = parseInt(text, 10);
if (isNaN(res))
infoLeft.textContent = "Invalid integer!"
return [res, !isNaN(res)];
}
function TryParseFloat(text: string): [number, boolean]
{
let res = parseFloat(text);
if (isNaN(res))
infoLeft.textContent = "Invalid float!"
return [res, !isNaN(res)];
}