-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
343 lines (301 loc) · 9.29 KB
/
script.js
File metadata and controls
343 lines (301 loc) · 9.29 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
function newElement(tag, attrs, parent) {
const newElem = document.createElement(tag);
parent = parent ? parent : document.body;
parent.append(newElem);
for (const key in attrs) {
newElem.setAttribute(key, attrs[key])
}
return newElem
}
// const wordChoiceBox = document.getElementById("word-choice-box")
let choiceIndex = 0;
let selectedWord = "";
const textareaBox = newElement("div");
const inputField = newElement("textarea", {style: "min-width: 90vw; margin-bottom: 15px;"}, textareaBox);
// document.body.append(inputField);
// inputField.textContent = "_";
const preventDefKeys = ["F1", "F2", "F4", "ArrowUp", "ArrowDown"];
document.addEventListener("keydown", (event) => {
console.log("event.key: ", event.key);
if (preventDefKeys.includes(event.key)) {
event.preventDefault();
}
handleKeyPress(event.key);
});
function handleKeyPress(key) {
keyTest.textContent = key;
switch (key) {
case "F4":
clearInput();
toggleVisibleById("dialpad");
toggleVisibleById("key-test");
break;
case "F2":
copyToClipboard();
break;
case "ArrowUp":
selectChoice("prev");
break;
case "ArrowDown":
selectChoice("next");
break;
default:
break;
}
}
const wordChoiceBox = newElement("div", {id:"word-choice-box", class:"word-choice-box"})
const copyButton = newElement("button");
copyButton.textContent = "Copy";
copyButton.addEventListener("click", () => {
copyToClipboard();
});
const inputInterceptor = newElement("input", {style: "max-width: 33px"});
inputInterceptor.focus();
const clearButton = newElement("button");
clearButton.textContent = "Clear";
clearButton.addEventListener("click", () => {
clearInput();
inputWordsArray = [];
renderInputWords();
});
function popByVal(list, value) {
var index = list.indexOf(value);
if (index !== -1) {
list.splice(index, 1);
}
return list;
}
// function sortedWords(wordArray) {
// const wordsInFreqList = [];
// wordsByFreq.forEach(word => {
// if (wordArray.includes(word)) {
// wordsInFreqList.push(word);
// popByVal(wordArray, word)
// }
// });
// return wordsInFreqList.concat(wordArray)
// }
function lookupWords() {
if (!inputBuffer) {
clearInput();
return
}
const wordArray = allWords[inputBuffer];
if (wordArray) {
populateChoiceBox(wordArray);
}
}
function populateChoiceBox(wordArray) {
wordChoiceBox.textContent = "";
wordArray.forEach(word => {
const newOption = newElement("div", {}, wordChoiceBox);
newOption.textContent = word;
});
if (wordArray.length > 0) {
selectChoice("start");
}
// choiceIndex = 0;
// wordChoiceBox.firstChild.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
}
function offsetIndex(list, current, offset) {
let newIndex = current + offset;
if (newIndex >= 0 && newIndex < list.length) {
return newIndex
} else if (newIndex >= 0) {
return newIndex % list.length
} else if (newIndex < 0) {
return list.length - (Math.abs(newIndex) % list.length)
}
}
function selectChoice(operation) {
if (wordChoiceBox.children.length < 1) { return }
console.log(wordChoiceBox.childNodes);
switch (operation) {
case "start":
choiceIndex = 0;
break;
case "end":
choiceIndex = wordChoiceBox.children.length - 1;
break;
case "prev":
choiceIndex = offsetIndex(wordChoiceBox.children, choiceIndex, -1);
break;
case "next":
choiceIndex = offsetIndex(wordChoiceBox.children, choiceIndex, 1);
break;
default:
choiceIndex = 0;
break;
}
const selectedChoice = wordChoiceBox.childNodes[choiceIndex];
selectedChoice.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
wordChoiceBox.childNodes.forEach(choice => {
choice.className = "";
});
selectedChoice.className = "selected-choice";
selectedWord = selectedChoice.textContent;
}
inputInterceptor.addEventListener("input", (event) => {
const inputChar = inputInterceptor.value;
inputInterceptor.value = "";
handleInput(inputChar);
});
let inputBuffer = "";
function handleInput(inputChar) {
if ("23456789".includes(inputChar)) {
inputBuffer += inputChar;
lookupWords();
} else if (inputChar === " ") {
confirmWord(selectedWord);
} else if (inputChar === "*") {
modifyPrevWord(toggleCase);
} else if (inputChar === "1") {
performBackspace();
} else if (inputChar === "#") {
showPunctuationOptions();
}
renderInputWords();
}
const placeholderize = (text, placeholderChar = "_") => placeholderChar.repeat(text.length);
function showPunctuationOptions() {
// TODO: "paginate" symbol list with repeated keypresses
// store an index of the current page, then reset upon input confirmation
const punctuationAndSymbols = [
".", "?", "!", ",", ";", ":", "'", "\"", "/", "\\", "&", "@", "#", "$",
"%", "^", "*", "(", ")", "[", "]", "{", "}"
];
populateChoiceBox(punctuationAndSymbols);
}
function performBackspace() {
if (inputBuffer.length > 0) {
inputBuffer = inputBuffer.slice(0, inputBuffer.length - 1);
lookupWords();
} else if (inputBuffer.length < 1 && inputWordsArray.length > 0) {
inputWordsArray = inputWordsArray.slice(0, inputWordsArray.length - 1);
}
renderInputWords();
}
function modifyPrevWord(modify) {
if (inputWordsArray.length < 1) { return }
const finalIndex = indexOfFinalWord();
const finalWord = inputWordsArray[finalIndex];
inputWordsArray[finalIndex] = modify(finalWord);
renderInputWords();
}
function indexOfFinalWord() {
for (let i = inputWordsArray.length -1; i >= 0; i--) {
const word = inputWordsArray[i];
if (!["", " "].includes(word)) {
return i
}
}
}
function toggleCase(text) {
// TODO: add quote / parenthesis wrapping?
const firstLetter = text.slice(0, 1);
const allCAPS = (text.toUpperCase() === text);
const capitalized = (!allCAPS && firstLetter.toUpperCase() === firstLetter)
if (capitalized && !allCAPS) {
return text.toUpperCase()
} else if (allCAPS) {
return text.toLowerCase()
} else {
return firstLetter.toUpperCase() + text.slice(1)
}
}
const prevWord = () => inputWordsArray.length > 0 ? inputWordsArray[inputWordsArray.length - 1] : "";
let inputWordsArray = []
function confirmWord(word) {
word = word === "" ? " " : word;
inputWordsArray.push(word);
// addSpace(word);
renderInputWords();
clearInput();
}
function addSpace() {
// const prefix = word.length > 1 && inputWordsArray.length > 1 ? " " : "";
const prev = prevWord();
if (prev.length > 1 || ["i", "a"].includes(prev.toLowerCase())) {
inputWordsArray.push(" ");
}
}
function renderInputWords() {
inputField.value = "";
inputWordsArray.forEach(word => {
inputField.value += word;
});
// inputField.value += ` ${placeholderize(inputBuffer)}`;
inputField.value += `${inputBuffer}`;
// console.log("inputWordsArray: ", inputWordsArray);
inputField.scrollTop = inputField.scrollHeight;
}
const cursorChar = "▏";
function blinkCursor() {
if (inputField.value.includes(cursorChar)) {
inputField.value = inputField.value.replace(cursorChar, "");
} else {
inputField.value += cursorChar;
}
}
setInterval(() => {
blinkCursor();
}, 777);
function clearInput() {
selectedWord = "";
choiceIndex = 0;
inputBuffer = "";
populateChoiceBox([]);
}
function fetchJSON(url, updateFunction) {
fetch(url)
.then(function (promise) {
return promise.json();
})
.then(function (data) {
updateFunction(data);
})
.catch(function (error) {
console.error('Error:', error);
});
}
let allWords;
let wordsByFreq;
fetchJSON("num_words_superior.json", (data) => {allWords = data});
// fetchJSON("words_by_freq.json", (data) => {wordsByFreq = data});
const buttonGroups = ["", "@", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz", ""]
const dialpad = newElement("div", {id: "dialpad", style: "position: fixed; left: 10px; bottom: 10px; user-select: none; margin-top: 20px; display: flex; flex-wrap: wrap; width: 170px; min-height: 170px;"})
for (let i = 1; i < 10; i++) {
const button = newElement("div", {style: "background-color: white; border: solid 1px black; display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 50px; min-height: 50px;"}, dialpad)
const buttonNum = newElement("h3", {style: "margin: 0px;"}, button);
buttonNum.textContent = i;
const buttonLabel = newElement("h4", {style: "margin: 0px;"}, button);
buttonLabel.textContent = buttonGroups[i];
button.addEventListener("click", () => {
handleInput(buttonNum.textContent);
inputInterceptor.focus();
});
}
toggleVisibleById("dialpad");
function toggleVisibleById(elementId) {
const element = document.getElementById(elementId);
if (element.style.visibility === "hidden") {
element.style.visibility = null;
} else {
element.style.visibility = "hidden";
}
}
function copyToClipboard() {
inputField.value = inputField.value.replace(cursorChar, "");
inputField.select();
try {
document.execCommand('copy');
keyTest.textContent = 'Copied text!';
inputInterceptor.focus();
} catch (err) {
keyTest.textContent = 'Unable to copy text';
console.error('Unable to copy text', err);
}
}
keyTest = newElement("h4", {id: "key-test"});
keyTest.textContent = "Key Test";
toggleVisibleById("key-test");