-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
109 lines (83 loc) · 2.75 KB
/
script.js
File metadata and controls
109 lines (83 loc) · 2.75 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
const filename = "4000-most-common-english-words.csv";
const poolSize = 1000;
const textLength = 10;
let currentIndex = 0;
async function getWords(n) {
try {
const response = await fetch(filename);
const text = await response.text();
const words = text
.split("\n")
.map(line => line.trim())
.filter(line => line.length > 0);
return words.slice(0, n);
} catch (error) {
return `text not found: ${error.message}`;
}
}
async function getText() {
const words = await getWords(poolSize);
if (typeof words === "string") return words;
const result =[];
while (result.length < textLength) {
let shuffled = [...words].sort(() => Math.random() - 0.5);
if (words.length > 1 && result.length > 0) {
const lastWord = result[result.length - 1];
if (shuffled[0] === lastWord) {
shuffled = [shuffled[1], shuffled[0], ...shuffled.slice(2)];
}
}
const remaining = textLength - result.length;
result.push(...shuffled.slice(0, remaining));
}
return result;
}
async function loadText() {
const result = await getText();
const fullText = result.join(" ");
currentIndex = 0;
const textEl = document.getElementById("text");
textEl.innerHTML = fullText
.split("")
.map((char, i) => `<span id="char-${i}">${char === " " ? " " : char}</span>`)
.join("");
// CREATE THE FLOATING CURSOR ONCE
const cursorEl = document.createElement("div");
cursorEl.id = "cursor";
textEl.appendChild(cursorEl);
updateCursor();
}
function updateCursor() {
const cursor = document.getElementById("cursor");
const span = document.getElementById(`char-${currentIndex}`);
if (span && cursor) {
cursor.style.left = span.offsetLeft + "px";
cursor.style.top = span.offsetTop + "px";
cursor.style.height = span.offsetHeight + "px";
}
else if (!span && cursor && currentIndex > 0) {
const lastSpan = document.getElementById(`char-${currentIndex - 1}`);
if (lastSpan) {
cursor.style.left = (lastSpan.offsetLeft + lastSpan.offsetWidth) + "px";
}
}
}
window.addEventListener("resize", updateCursor);
document.addEventListener("keydown", (e) => {
if (e.key === " ") e.preventDefault();
if (e.key.length !== 1) return;
const span = document.getElementById(`char-${currentIndex}`);
if (!span) return;
const expected = span.innerText === "\u00a0" ? " " : span.innerText;
if (e.key === expected) {
span.style.color = "black";
currentIndex++;
// MOVE THE INDEPENDENT CURSOR (No classList.add happening here!)
updateCursor();
}
});
document.getElementById("restart").addEventListener("click", loadText);
document.getElementById("restart").addEventListener("keydown", (e) => {
if (e.key === "Enter") loadText();
});
loadText();