forked from briano1905/typings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
459 lines (414 loc) · 14.4 KB
/
main.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Get document element
const textDisplay = document.querySelector('#text-display');
const inputField = document.querySelector('#input-field');
// Initialize typing mode variables
let typingMode = 'wordcount';
let wordCount;
let timeCount;
// Initialize dynamic variables
let randomWords = [];
let wordList = [];
let currentWord = 0;
let correctKeys = 0;
let startDate = 0;
let timer;
let timerActive = false;
let punctuation = false;
// Get cookies
getCookie('theme') === '' ? setTheme('light') : setTheme(getCookie('theme'));
getCookie('language') === '' ? setLanguage('english') : setLanguage(getCookie('language'));
getCookie('wordCount') === '' ? setWordCount(50) : setWordCount(getCookie('wordCount'));
getCookie('timeCount') === '' ? setTimeCount(60) : setTimeCount(getCookie('timeCount'));
getCookie('typingMode') === '' ? setTypingMode('wordcount') : setTypingMode(getCookie('typingMode'));
getCookie('punctuation') === '' ? setPunctuation('false') : setPunctuation(getCookie('punctuation'));
// Find a list of words and display it to textDisplay
function setText(e) {
e = e || window.event;
var keepWordList = e && e.shiftKey;
// Reset
if (!keepWordList) {
wordList = [];
}
currentWord = 0;
correctKeys = 0;
inputField.value = '';
timerActive = false;
clearTimeout(timer);
textDisplay.style.display = 'block';
inputField.className = '';
switch (typingMode) {
case 'wordcount':
textDisplay.style.height = 'auto';
textDisplay.innerHTML = '';
if (!keepWordList) {
wordList = [];
while (wordList.length < wordCount) {
const randomWord = randomWords[Math.floor(Math.random() * randomWords.length)];
if (wordList[wordList.length - 1] !== randomWord || wordList[wordList.length - 1] === undefined || getCookie('language') === 'dots') {
wordList.push(randomWord);
}
}
}
break;
case 'time':
textDisplay.style.height = '3.2rem';
document.querySelector(`#tc-${timeCount}`).innerHTML = timeCount;
textDisplay.innerHTML = '';
if (!keepWordList) {
wordList = [];
for (i = 0; i < 500; i++) {
let n = Math.floor(Math.random() * randomWords.length);
wordList.push(randomWords[n]);
}
}
}
if (punctuation) addPunctuations();
showText();
inputField.focus();
}
function addPunctuations() {
if (wordList[0] !== undefined) {
// Capitalize first word
wordList[0] = wordList[0][0].toUpperCase() + wordList[0].slice(1);
// Add comma, fullstop, question mark, exclamation mark, semicolon. Capitalize the next word
for (i = 0; i < wordList.length; i++) {
const ran = Math.random();
if (i < wordList.length - 1) {
if (ran < 0.03) {
wordList[i] += ',';
} else if (ran < 0.05) {
wordList[i] += '.';
wordList[i + 1] = wordList[i + 1][0].toUpperCase() + wordList[i + 1].slice(1);
} else if (ran < 0.06) {
wordList[i] += '?';
wordList[i + 1] = wordList[i + 1][0].toUpperCase() + wordList[i + 1].slice(1);
} else if (ran < 0.07) {
wordList[i] += '!';
wordList[i + 1] = wordList[i + 1][0].toUpperCase() + wordList[i + 1].slice(1);
} else if (ran < 0.08) {
wordList[i] += ';';
}
}
}
wordList[wordList.length - 1] += '.';
// Add quotation marks
}
}
// Display text to textDisplay
function showText() {
wordList.forEach(word => {
let span = document.createElement('span');
span.innerHTML = word + ' ';
textDisplay.appendChild(span);
});
textDisplay.firstChild.classList.add('highlight');
}
// Key is pressed in input field
inputField.addEventListener('keydown', e => {
// Add wrong class to input field
switch (typingMode) {
case 'wordcount':
if (currentWord < wordList.length) inputFieldClass();
case 'time':
if (timerActive) inputFieldClass();
}
function inputFieldClass() {
if (e.key >= 'a' && e.key <= 'z' || (e.key === `'` || e.key === ',' || e.key === '.' || e.key === ';')) {
let inputWordSlice = inputField.value + e.key;
let currentWordSlice = wordList[currentWord].slice(0, inputWordSlice.length);
inputField.className = inputWordSlice === currentWordSlice ? '' : 'wrong';
} else if (e.key === 'Backspace') {
let inputWordSlice = e.ctrlKey ? '' : inputField.value.slice(0, inputField.value.length - 1);
let currentWordSlice = wordList[currentWord].slice(0, inputWordSlice.length);
inputField.className = inputWordSlice === currentWordSlice ? '' : 'wrong';
} else if (e.key === ' ') {
inputField.className = '';
}
}
// If it is the first character entered
if (currentWord === 0 && inputField.value === '') {
switch (typingMode) {
case 'wordcount':
startDate = Date.now();
break;
case 'time':
if (!timerActive) {
startTimer(timeCount);
timerActive = true;
}
function startTimer(time) {
if (time > 0) {
document.querySelector(`#tc-${timeCount}`).innerHTML = time;
timer = setTimeout(() => {
time--;
startTimer(time);
}, 1000);
} else {
timerActive = false;
textDisplay.style.display = 'none';
inputField.className = '';
document.querySelector(`#tc-${timeCount}`).innerHTML = timeCount;
showResult();
}
}
}
}
// If it is the space key check the word and add correct/wrong class
if (e.key === ' ') {
e.preventDefault();
if (inputField.value !== '') {
// Scroll down text when reach new line
if (typingMode === 'time') {
const currentWordPosition = textDisplay.childNodes[currentWord].getBoundingClientRect();
const nextWordPosition = textDisplay.childNodes[currentWord + 1].getBoundingClientRect();
if (currentWordPosition.top < nextWordPosition.top) {
for (i = 0; i < currentWord + 1; i++) textDisplay.childNodes[i].style.display = 'none';
}
}
// If it is not the last word increment currentWord,
if (currentWord < wordList.length - 1) {
if (inputField.value === wordList[currentWord]) {
textDisplay.childNodes[currentWord].classList.add('correct');
correctKeys += wordList[currentWord].length + 1;
} else {
textDisplay.childNodes[currentWord].classList.add('wrong');
}
textDisplay.childNodes[currentWord + 1].classList.add('highlight');
} else if (currentWord === wordList.length - 1) {
textDisplay.childNodes[currentWord].classList.add('wrong');
showResult();
}
inputField.value = '';
currentWord++;
}
// Else if it is the last word and input word is correct show the result
} else if (currentWord === wordList.length - 1) {
if (inputField.value + e.key === wordList[currentWord]) {
textDisplay.childNodes[currentWord].classList.add('correct');
correctKeys += wordList[currentWord].length;
currentWord++;
showResult();
}
}
});
// Calculate and display result
function showResult() {
let words, minute, acc;
switch (typingMode) {
case 'wordcount':
words = correctKeys / 5;
minute = (Date.now() - startDate) / 1000 / 60;
let totalKeys = -1;
wordList.forEach(e => (totalKeys += e.length + 1));
acc = Math.floor((correctKeys / totalKeys) * 100);
break;
case 'time':
words = correctKeys / 5;
minute = timeCount / 60;
let sumKeys = -1;
for (i = 0; i < currentWord; i++) {
sumKeys += wordList[i].length + 1;
}
acc = acc = Math.min(Math.floor((correctKeys / sumKeys) * 100), 100);
}
let wpm = Math.floor(words / minute);
document.querySelector('#right-wing').innerHTML = `WPM: ${wpm} / ACC: ${acc}`;
}
// Command actions
document.addEventListener('keydown', e => {
// Modifiers Windows: [Alt], Mac: [Cmd + Ctrl]
if (e.altKey || (e.metaKey && e.ctrlKey)) {
// [mod + t] => Change the theme
if (e.key === 't') {
setTheme(inputField.value);
}
// [mod + l] => Change the language
if (e.key === 'l') {
setLanguage(inputField.value);
}
// [mod + m] => Change the typing mode
if (e.key === 'm') {
setTypingMode(inputField.value);
}
// [mod + p] => Change punctuation active
if (e.key === 'p') {
setPunctuation(inputField.value);
}
} else if (!document.querySelector('#theme-center').classList.contains('hidden')) {
if (e.key === 'Escape'){
hideThemeCenter();
inputField.focus();
}
} else if (e.key === 'Escape') {
setText(e);
}
});
function setTheme(_theme) {
const theme = _theme.toLowerCase();
fetch(`themes/${theme}.css`)
.then(response => {
if (response.status === 200) {
response
.text()
.then(css => {
setCookie('theme', theme, 90);
document.querySelector('#theme').setAttribute('href', `themes/${theme}.css`);
setText();
})
.catch(err => console.error(err));
} else {
console.log(`theme ${theme} is undefine`);
}
})
.catch(err => console.error(err));
}
function setLanguage(_lang) {
const lang = _lang.toLowerCase();
fetch('texts/random.json')
.then(response => response.json())
.then(json => {
if (typeof json[lang] !== 'undefined') {
randomWords = json[lang];
setCookie('language', lang, 90);
if (lang === "arabic") {
textDisplay.style.direction = "rtl"
inputField.style.direction = "rtl"
} else {
textDisplay.style.direction = "ltr"
inputField.style.direction = "ltr"
}
setText();
} else {
console.error(`language ${lang} is undefine`);
}
})
.catch(err => console.error(err));
}
function setTypingMode(_mode) {
const mode = _mode.toLowerCase();
switch (mode) {
case 'wordcount':
typingMode = mode;
setCookie('typingMode', mode, 90);
document.querySelector('#word-count').style.display = 'inline';
document.querySelector('#time-count').style.display = 'none';
setText();
break;
case 'time':
typingMode = mode;
setCookie('typingMode', mode, 90);
document.querySelector('#word-count').style.display = 'none';
document.querySelector('#time-count').style.display = 'inline';
setText();
break;
default:
console.error(`mode ${mode} is undefine`);
}
}
function setPunctuation(_punc) {
const punc = _punc.toLowerCase();
if (punc === 'true') {
punctuation = true;
setCookie('punctuation', true, 90);
setText();
} else if (punc === 'false') {
punctuation = false;
setCookie('punctuation', false, 90);
setText();
}
}
function setWordCount(wc) {
setCookie('wordCount', wc, 90);
wordCount = wc;
document.querySelectorAll('#word-count > span').forEach(e => (e.style.borderBottom = ''));
document.querySelector(`#wc-${wordCount}`).style.borderBottom = '2px solid';
setText();
}
function setTimeCount(tc) {
setCookie('timeCount', tc, 90);
timeCount = tc;
document.querySelectorAll('#time-count > span').forEach(e => {
e.style.borderBottom = '';
e.innerHTML = e.id.substring(3, 6);
});
document.querySelector(`#tc-${timeCount}`).style.borderBottom = '2px solid';
setText();
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
var expires = 'expires=' + d.toUTCString();
document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}
function getCookie(cname) {
var name = cname + '=';
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
showAllThemes();
function showAllThemes(){
fetch(`themes/theme-list.json`)
.then(response => {
if (response.status === 200) {
response
.text()
.then(body => {
let themes = JSON.parse(body);
let keys = Object.keys(themes);
let i;
for(i = 0;i < keys.length; i ++){
let theme = document.createElement('div');
theme.setAttribute('class', 'theme-button');
theme.setAttribute('onClick', `setTheme('${keys[i]}')`);
theme.setAttribute('id', keys[i]);
// set tabindex to current theme index + 4 for the test page
theme.setAttribute('tabindex', i + 5);
theme.addEventListener('keydown', e => {
if (e.key === 'Enter') {
setTheme(theme.id);
inputField.focus();
}
})
if(themes[keys[i]]['customHTML'] != undefined){
theme.style.background = themes[keys[i]]['background'];
theme.innerHTML = themes[keys[i]]['customHTML']
}else{
theme.textContent = keys[i];
theme.style.background = themes[keys[i]]['background'];
theme.style.color = themes[keys[i]]['color'];
}
document.getElementById('theme-area').appendChild(theme);
}
})
.catch(err => console.error(err));
} else {
console.log(`Cant find theme-list.json`);
}
})
.catch(err => console.error(err));
}
// enter to open theme area
document.getElementById('show-themes').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
showThemeCenter();
inputField.focus();
}
});
function showThemeCenter() {
document.getElementById('theme-center').classList.remove('hidden');
document.getElementById('command-center').classList.add('hidden');
}
function hideThemeCenter() {
document.getElementById('theme-center').classList.add('hidden');
document.getElementById('command-center').classList.remove('hidden');
}