-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcloze_game.js
164 lines (142 loc) · 4.46 KB
/
cloze_game.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
function initClozeGame({
sentence,
sentenceToRead = sentence,
gameContainer,
isGerman = false,
showOverlay = true
}) {
gameContainer.classList.add("tappable");
if (showOverlay) {
const overlay = document.createElement("div");
overlay.id = "overlay";
overlay.textContent = "Anzeigen";
overlay.onclick = () => {
overlay.classList.add("hidden");
};
gameContainer.appendChild(overlay);
}
// remove HTML tags from sentence
sentence = sentence.replace(/<[^>]+>/g, "");
const wordRegex = /([\p{L}0-9-()/]+[’']?|\*.+?\*)/gu;
let words = Array.from(sentence.matchAll(wordRegex), (match) => match[0]);
const sentenceContainer = document.createElement("div");
sentenceContainer.id = "sentence-cloze";
if (isGerman) {
sentenceContainer.style.fontStyle = "italic";
}
gameContainer.appendChild(sentenceContainer);
const hr = document.createElement("hr");
gameContainer.appendChild(hr);
const wordButtonsContainer = document.createElement("div");
wordButtonsContainer.id = "word-buttons";
wordButtonsContainer.classList.add("button-container");
gameContainer.appendChild(wordButtonsContainer);
const sentenceParts = sentence.split(wordRegex);
sentenceParts.forEach((part) => {
if (words.includes(part)) {
const span = document.createElement("span");
span.classList.add("cloze");
span.classList.add("spoiler");
if (part.includes("*")) {
span.classList.add("word-highlight");
span.innerText = part.replaceAll("*", "");
part = "*";
} else {
span.innerText = part;
}
span.dataset.word = part;
sentenceContainer.appendChild(span);
span.onclick = () => {
checkWord(
wordButtonsContainer.querySelector(
`.button[data-word="${part}"]:not(.disabled)`
),
true
);
span.onclick = null;
};
const prevEl = span.previousSibling;
if (
prevEl &&
((prevEl.nodeType === Node.TEXT_NODE && prevEl.data === " ") ||
prevEl.data === "")
) {
span.classList.add("no-space");
}
} else {
sentenceContainer.appendChild(document.createTextNode(part));
}
});
const sortedWords = [...words].sort((a, b) =>
a
.replace("*", "")
.localeCompare(b.replace("*", ""), "de", { sensitivity: "base" })
);
let hasStarButton = false;
sortedWords.forEach((word) => {
const wordButton = document.createElement("div");
if (word.includes("*")) {
if (hasStarButton) {
return;
}
hasStarButton = true;
wordButton.innerText = "★";
wordButton.dataset.word = "*";
} else {
wordButton.innerText = word;
wordButton.dataset.word = word;
}
wordButton.classList.add("button");
wordButton.classList.add("word-button");
wordButton.onclick = () => checkWord(wordButton);
wordButtonsContainer.appendChild(wordButton);
});
let preloadAudio = true;
let preloadCountdown = 2; // preload audio after 2 words
function checkWord(button, force = false) {
const word = button.dataset.word;
const clozeSpans = document.querySelectorAll(".cloze.spoiler");
const clozeSpanIndex = Array.from(clozeSpans).findIndex(
(span) => span.dataset.word === word
);
if (clozeSpanIndex === -1) {
return;
}
if (force || clozeSpanIndex === 0) {
const clozeSpan = clozeSpans[clozeSpanIndex];
clozeSpan.classList.remove("spoiler");
button.classList.add("disabled");
button.disabled = true;
preloadCountdown--;
if (options.autoPlaySentenceOnClozeFinish && preloadAudio && preloadCountdown <= 0) {
preloadAudio = false;
fetchAudio({ text: sentenceToRead });
}
clearErrorOutlines();
if (word === "*") {
document
.querySelectorAll(".cloze.spoiler[data-word='*']")
.forEach((cloze) => {
cloze.classList.remove("spoiler");
});
}
if (document.querySelectorAll(".cloze.spoiler").length === 0) {
finish();
}
} else {
button.classList.add("error");
}
}
function clearErrorOutlines() {
document.querySelectorAll(".word-button").forEach((button) => {
button.classList.remove("error");
});
}
function finish() {
gameContainer.classList.add("finished");
gameContainer.classList.remove("tappable");
if (options.autoPlaySentenceOnClozeFinish) {
playAudio({text: sentenceToRead});
}
}
}