-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
207 lines (175 loc) · 5.45 KB
/
script.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
const wrapper = document.querySelector(".wrapper");
const topButtons = document.querySelectorAll(".top-button");
const sentence = document.querySelector("#sentence");
const translateButton = document.querySelector("#translateButton");
const trynewButton = document.querySelector("#trynew");
const reveal = document.querySelector("#revealresult");
const result = document.querySelector("#result");
let elms;
let elmsLength;
let currentTabIndex = 1;
let maxTababale;
sentence.focus();
async function translate(text) {
const apiKey = guesser.apiKey //Your Deepl api key;
const res = await fetch(
`https://api-free.deepl.com/v2/translate?auth_key=${apiKey}&text=${encodeURIComponent(
text
)}&target_lang=de&formality=less`
);
const json = await res.json();
return json.translations[0].text.split(" ");
}
document.onkeydown = function (e) {
e = e || window.event;
if (e.key === "Backspace" && document.activeElement !== sentence) {
previousInput()
} else if (e.key === "ArrowRight") {
nextInput()
} else if (e.key === "ArrowLeft") {
previousInput()
}
};
function previousInput() {
currentTabIndex = Math.max(1, currentTabIndex - 1);
selectInput(currentTabIndex);
}
function nextInput() {
currentTabIndex = Math.min(elms.length, currentTabIndex + 1);
selectInput(currentTabIndex);
}
translateButton.addEventListener("click", async (e) => {
await startNewSentence(sentence.value);
reveal.style.display = "inline";
translateButton.setAttribute("disabled", true);
});
trynewButton.addEventListener("click", (e) => {
sentence.removeAttribute("disabled");
sentence.value = "";
sentence.focus();
translateButton.removeAttribute("disabled");
wrapper.innerHTML = "";
result.style.display = "";
reveal.style.display = "";
setResult([""]);
});
reveal.addEventListener("click", (_) => {
result.style.display = "block";
});
topButtons.forEach((b) => {
b.addEventListener("mousedown", (e) => {
e.preventDefault();
});
});
function selectInput(tabindex) {
document.querySelector(`input[tabindex='${tabindex}']`)?.focus();
}
function insertWord(word) {
const wordWrapper = document.createElement("div");
wordWrapper.classList.add("word-wrapper");
const revealedLetterIndex = getRandomInt(0, word.length);
[...word].forEach((letter, i) => {
const input = document.createElement("input");
input.classList.add("character-input");
input.setAttribute("data-letter", letter);
input.setAttribute("maxlength", 1);
input.setAttribute("type", "text");
const isPunctuation = !/[a-zäöüß0-9]/i.test(letter);
if (revealedLetterIndex === i || isPunctuation) {
insertLetter(input, letter, isPunctuation);
}
wordWrapper.append(input);
});
wrapper.append(wordWrapper);
}
function insertLetter(input, letter, isPunctuation) {
input.setAttribute("disabled", true);
input.style.color = "white";
input.value = letter;
if (isPunctuation) {
input.style.border = "none";
input.style.background = "transparent";
}
}
const handleInput = (e, elms) => {
if (/[a-zäöüß0-9]/i.test(e.target.value)) {
currentTabIndex = Math.min(elmsLength + 1, currentTabIndex + 1);
selectInput(currentTabIndex);
}
};
const handleFocus = (e) => {
if (e.target.value !== "") {
e.target.select();
}
currentTabIndex = +e.target.getAttribute("tabIndex");
};
function setTabIndexes() {
document.querySelectorAll(".wrapper input").forEach((i) => {
i.removeAttribute("tabIndex");
i.removeEventListener("focus", handleFocus);
});
elms = document.querySelectorAll(".wrapper input:not(:disabled)");
elmsLength = elms.length;
maxTababale = elms.length;
elms.forEach((input, i) => {
input.tabIndex = i + 1;
});
elms.forEach((i) => {
i.addEventListener("input", handleInput);
i.addEventListener("focus", handleFocus);
});
elms[0].focus();
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function getWords() {
const words = Array.from(document.querySelectorAll(".word-wrapper"));
let sentence = "";
words.forEach((w) => {
w.querySelectorAll("input").forEach((i) => {
sentence += i.value;
});
sentence += " ";
});
console.log(sentence);
}
function setResult(words) {
document.querySelector("#result").textContent = words.join(" ");
}
function revealLetter() {
const inputs = Array.from(
wrapper.querySelectorAll("input:not(:disabled)")
).filter((i) => i.value === "");
const randomInt = getRandomInt(0, inputs.length);
const randomInput = inputs[randomInt];
const inputLetter = randomInput.getAttribute("data-letter");
insertLetter(randomInput, inputLetter, false);
setTabIndexes();
}
function checkWords() {
wrapper.querySelectorAll("input").forEach((i) => {
const [value, expectedValue] = [
i.value.toLocaleLowerCase(),
i.getAttribute("data-letter").toLocaleLowerCase(),
];
if (!i.disabled) {
if (value !== expectedValue && value !== "") {
i.style.borderColor = "red";
i.style.color = "red";
} else if (value === expectedValue) {
i.style.borderColor = "lightgreen";
i.style.color = "lightgreen";
}
}
});
}
async function startNewSentence(theSentence) {
const words = await translate(theSentence);
sentence.setAttribute("disabled", true);
words.forEach((w) => insertWord(w));
setResult(words);
setTabIndexes();
}