-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
299 lines (246 loc) · 7.76 KB
/
index.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const EVALUATE_KEY = 13;
const NAVIGATE_UP_KEY = 38;
const NAVIGATE_DOWN_KEY = 40;
let output = document.getElementById("output");
let inputText = document.getElementById("input-text");
let inputHint = document.getElementById("input-hint");
let inputHighlighting = document.getElementById("highlighting");
let input = document.getElementById("input");
let settingsIcon = document.getElementById("settings-icon");
let toast = document.getElementById("toast");
let history = [];
let navigation = 0;
const invoke = window.__TAURI__.invoke;
async function get_settings() {
settings = await invoke("get_settings");
}
let settings;
get_settings().then(async () => {
if (typeof settings["global_inputs"] === typeof "") {
let split = settings["global_inputs"].split("\n");
for (let i = 0; i < split.length; i++) {
await evaluateFendWithTimeout(split[i], 500);
}
}
});
window.__TAURI__.event.listen('settings-closed', () => {
settingsIcon.style.opacity = "";
get_settings();
invoke("save_settings").catch(x => set_toast("Error saving settings: " + x, "error"));
});
invoke("setup_exchanges");
async function evaluateFendWithTimeout(input, timeout) {
return invoke("fend_prompt", {"value": input, "timeout": timeout});
}
async function evaluateFendPreviewWithTimeout(input, timeout) {
return invoke("fend_preview_prompt", {"value": input, "timeout": timeout});
}
const setHintInnerText = x => {
inputHint.innerText = x;
};
function open_settings() {
settingsIcon.style.opacity = "1.0";
invoke("open_settings");
}
function set_toast(text, type) {
toast.innerText = text;
// Add the "show" class to DIV
toast.className = "show " + type;
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ toast.className = ""; }, 3000);
}
async function commands(event) {
if (!event.ctrlKey) {
return;
}
if ((event.key === "w" && settings["ctrl_w_closes"]) || (event.key === "d" && settings["ctrl_d_closes"])) {
invoke("quit");
} else if (event.key === "c" && document.getSelection().isCollapsed) {
let clipboard_text;
if (settings["ctrl_c_behavior"] === "prev_result") {
clipboard_text = output.lastChild.innerText;
if (clipboard_text === undefined) {
return;
}
} else if (settings["ctrl_c_behavior"] === "hint") {
clipboard_text = inputHint.innerText;
} else {
clipboard_text = inputText.value;
}
invoke("copy_to_clipboard", {"value": clipboard_text});
set_toast("Copied to clipboard!", "note");
} else if (event.key === "s") {
let history_segment;
if (settings["save_back_count"] < 0) {
history_segment = history;
} else {
history_segment = history.slice(-settings["save_back_count"]);
}
invoke("save_to_file", {"input": history_segment}).then(x => {
if (x) {
set_toast("Successfully saved file!", "ok");
} else {
set_toast("Cancelled!", "note")
}
}).catch(x => set_toast("Error saving file: " + x, "error"));
} else if (event.key === "o") {
let inputs = await invoke("load_from_file").then(x => {
console.log(x);
if (x[1]) {
set_toast("Cancelled!", "note");
}
return x[0];
}).catch(x => {
set_toast("Error loading file: " + x, "error");
return [];
});
if (inputs.length === 0) {
return;
}
for (let i = 0; i < inputs.length; i++) {
await evaluateFendWithTimeout(inputs[i], 500);
}
set_toast("Finished loading file!", "ok");
}
}
async function evaluate(event) {
// allow multiple lines to be entered if shift, ctrl
// or meta is held, otherwise evaluate the expression
if (!(event.keyCode === EVALUATE_KEY && !event.shiftKey && !event.ctrlKey && !event.metaKey)) {
return;
}
event.preventDefault();
if (inputText.value === "clear") {
output.innerHTML = "";
setInputText("");
return;
}
let request = document.createElement("p");
let result = document.createElement("p");
request.innerText = "> " + inputText.value;
if (isInputFilled()) {
history.push(inputText.value);
}
navigateEnd();
const setResultInnerText = x => {
result.innerText = x;
};
evaluateFendWithTimeout(inputText.value, 500).then(setResultInnerText, setResultInnerText).finally(() => {
setInputText("");
output.appendChild(request);
output.appendChild(result);
result.scrollIntoView();
});
}
function navigate(event) {
if (![NAVIGATE_UP_KEY, NAVIGATE_DOWN_KEY].includes(event.keyCode)) {
return;
}
if (navigation > 0) {
if (NAVIGATE_UP_KEY === event.keyCode) {
event.preventDefault();
navigateBackwards();
}
else if (NAVIGATE_DOWN_KEY === event.keyCode) {
event.preventDefault();
navigateForwards();
}
} else if (!isInputFilled() && history.length > 0 && NAVIGATE_UP_KEY === event.keyCode) {
event.preventDefault();
navigateBegin();
}
if (navigation > 0) {
navigateSet();
}
updateReplicatedText();
updateHint();
}
function navigateBackwards() {
navigation += 1;
if (navigation > history.length) {
navigation = history.length;
}
}
function navigateForwards() {
navigation -= 1;
if (navigation < 1) {
navigateEnd();
navigateClear();
}
}
function navigateBegin() {
navigation = 1;
}
function navigateEnd() {
navigation = 0;
}
function navigateSet() {
setInputText(history[history.length - navigation]);
}
function navigateClear() {
setInputText("");
}
function focus() {
// allow the user to select text for copying and
// pasting, but if text is deselected (collapsed)
// refocus the input field
if (document.activeElement !== inputText && document.getSelection().isCollapsed) {
inputText.focus();
}
}
function update() {
navigateEnd();
updateHint();
updateReplicatedText();
}
async function autocomplete(event) {
if (event.key !== "Tab" || event.shiftKey || event.ctrlKey || event.metaKey) {
return;
}
let hint = await invoke("fend_completion", {"value": inputText.value});
if (hint != null) {
setInputText(inputText.value + hint);
focus(); // A bit of a hack to avoid unfocusing after autocompleting but whatever.
}
}
async function updateReplicatedText() {
inputText.parentNode.dataset.replicatedValue = inputText.value;
let hint = await invoke("fend_completion", {"value": inputText.value});
if (hint == null) {
hint = '';
} else {
hint = '<span class="input-hint">' + hint + '</span>';
}
inputHighlighting.innerHTML =
'<span class="input-base">' + inputText.value + '</span>'
+ hint;
}
async function updateHint() {
evaluateFendPreviewWithTimeout(inputText.value, 100).then(x => {
inputHint.className = "valid-hint";
setHintInnerText(x);
}, x => {
inputHint.className = "error-hint";
setHintInnerText(x);
})
}
function isInputFilled() {
return inputText.value.length > 0;
}
function setInputText(x) {
inputText.value = x;
updateHint();
updateReplicatedText();
}
async function load() {
const keydown = x => {
autocomplete(x);
navigate(x);
evaluate(x);
commands(x);
}
inputText.addEventListener('input', update);
inputText.addEventListener('keydown', keydown);
document.addEventListener('click', focus);
}
window.onload = load;