Repository navigation
Expand file tree
/
Copy pathreplay.js
More file actions
296 lines (277 loc) · 8.28 KB
/
Copy pathreplay.js
File metadata and controls
296 lines (277 loc) · 8.28 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
/*
TODO:
Export replay as video
Export replay as typing test file?
.ttr file extension (stands for typing test record)
Should just be json, but fields should be specified by some format
metadata field with rules, website source, mode, name of typist
data field should be a list of objects, like monkeytype replay uses
signature or verfication field should be able to check file validity with server
And add ability to upload file to watch replay
*/
import config from "./config";
import * as Sound from "./sound";
let wordsList = [];
let replayData = [];
let replayStartTime = 0;
let replayRecording = true;
let wordPos = 0;
let curPos = 0;
let targetWordPos = 0;
let targetCurPos = 0;
let timeoutList = [];
let stopwatchList = [];
const toggleButton = document.getElementById("playpauseReplayButton")
.children[0];
function replayGetWordsList(wordsListFromScript) {
wordsList = wordsListFromScript;
}
function initializeReplayPrompt() {
const replayWordsElement = document.getElementById("replayWords");
replayWordsElement.innerHTML = "";
let wordCount = 0;
replayData.forEach((item) => {
//trim wordsList for timed tests
if (item.action === "backWord") {
wordCount--;
} else if (
item.action === "submitCorrectWord" ||
item.action === "submitErrorWord"
) {
wordCount++;
}
});
wordsList.forEach((item, i) => {
if (i > wordCount) return;
let x = document.createElement("div");
x.className = "word";
for (i = 0; i < item.length; i++) {
let letter = document.createElement("LETTER");
letter.innerHTML = item[i];
x.appendChild(letter);
}
replayWordsElement.appendChild(x);
});
}
export function pauseReplay() {
timeoutList.forEach((item) => {
clearTimeout(item);
});
timeoutList = [];
stopwatchList.forEach((item) => {
clearTimeout(item);
});
stopwatchList = [];
targetCurPos = curPos;
targetWordPos = wordPos;
toggleButton.className = "fas fa-play";
toggleButton.parentNode.setAttribute("aria-label", "Resume replay");
}
function playSound(error = false) {
if (error) {
if (config.playSoundOnError) {
Sound.playError();
} else {
Sound.playClick();
}
} else {
Sound.playClick();
}
}
function handleDisplayLogic(item, nosound = false) {
let activeWord = document.getElementById("replayWords").children[wordPos];
if (item.action === "correctLetter") {
if (!nosound) playSound();
activeWord.children[curPos].classList.add("correct");
curPos++;
} else if (item.action === "incorrectLetter") {
if (!nosound) playSound(true);
let myElement;
if (curPos >= activeWord.children.length) {
//if letter is an extra
myElement = document.createElement("letter");
myElement.classList.add("extra");
myElement.innerHTML = item.letter;
activeWord.appendChild(myElement);
}
myElement = activeWord.children[curPos];
myElement.classList.add("incorrect");
curPos++;
} else if (item.action === "deleteLetter") {
if (!nosound) playSound();
let myElement = activeWord.children[curPos - 1];
if (myElement.classList.contains("extra")) {
myElement.remove();
} else {
myElement.className = "";
}
curPos--;
} else if (item.action === "submitCorrectWord") {
if (!nosound) playSound();
wordPos++;
curPos = 0;
} else if (item.action === "submitErrorWord") {
if (!nosound) playSound(true);
activeWord.classList.add("error");
wordPos++;
curPos = 0;
} else if (item.action === "clearWord") {
if (!nosound) playSound();
let promptWord = document.createElement("div");
let wordArr = wordsList[wordPos].split("");
wordArr.forEach((letter) => {
promptWord.innerHTML += `<letter>${letter}</letter>`;
});
activeWord.innerHTML = promptWord.innerHTML;
curPos = 0;
} else if (item.action === "backWord") {
if (!nosound) playSound();
wordPos--;
activeWord = document.getElementById("replayWords").children[wordPos];
curPos = activeWord.children.length;
while (activeWord.children[curPos - 1].className === "") curPos--;
activeWord.classList.remove("error");
}
}
function loadOldReplay() {
let startingIndex = 0;
curPos = 0;
wordPos = 0;
replayData.forEach((item, i) => {
if (
wordPos < targetWordPos ||
(wordPos === targetWordPos && curPos < targetCurPos)
) {
//quickly display everything up to the target
handleDisplayLogic(item, true);
startingIndex = i + 1;
}
});
const time = Math.floor(replayData[startingIndex].time / 1000);
$("#replayStopwatch").text(time + "s");
return startingIndex;
}
function toggleReplayDisplay() {
if ($("#resultReplay").stop(true, true).hasClass("hidden")) {
initializeReplayPrompt();
loadOldReplay();
//show
if (!$("#watchReplayButton").hasClass("loaded")) {
$("#words").html(
`<div class="preloader"><i class="fas fa-fw fa-spin fa-circle-notch"></i></div>`
);
$("#resultReplay")
.removeClass("hidden")
.css("display", "none")
.slideDown(250);
} else {
$("#resultReplay")
.removeClass("hidden")
.css("display", "none")
.slideDown(250);
}
} else {
//hide
if (toggleButton.parentNode.getAttribute("aria-label") != "Start replay") {
pauseReplay();
}
$("#resultReplay").slideUp(250, () => {
$("#resultReplay").addClass("hidden");
});
}
}
function startReplayRecording() {
if (!$("#resultReplay").stop(true, true).hasClass("hidden")) {
//hide replay display if user left it open
toggleReplayDisplay();
}
$("#replayStopwatch").text(0 + "s");
replayData = [];
replayStartTime = performance.now();
replayRecording = true;
targetCurPos = 0;
targetWordPos = 0;
}
function stopReplayRecording() {
replayRecording = false;
}
function addReplayEvent(action, letter = undefined) {
if (replayRecording === false) {
return;
}
let timeDelta = performance.now() - replayStartTime;
if (action === "incorrectLetter" || action === "correctLetter") {
replayData.push({ action: action, letter: letter, time: timeDelta });
} else {
replayData.push({ action: action, time: timeDelta });
}
}
function playReplay() {
curPos = 0;
wordPos = 0;
toggleButton.className = "fas fa-pause";
toggleButton.parentNode.setAttribute("aria-label", "Pause replay");
initializeReplayPrompt();
let startingIndex = loadOldReplay();
let lastTime = replayData[startingIndex].time;
let swTime = Math.round(lastTime / 1000); //starting time
const swEndTime = Math.round(replayData[replayData.length - 1].time / 1000);
while (swTime <= swEndTime) {
const time = swTime;
stopwatchList.push(
setTimeout(() => {
$("#replayStopwatch").text(time + "s");
}, time * 1000 - lastTime)
);
swTime++;
}
replayData.forEach((item, i) => {
if (i < startingIndex) return;
timeoutList.push(
setTimeout(() => {
handleDisplayLogic(item);
}, item.time - lastTime)
);
});
timeoutList.push(
setTimeout(() => {
//after the replay has finished, this will run
t
5A44
argetCurPos = 0;
targetWordPos = 0;
toggleButton.className = "fas fa-play";
toggleButton.parentNode.setAttribute("aria-label", "Start replay");
}, replayData[replayData.length - 1].time - lastTime)
);
}
$(".pageTest #playpauseReplayButton").click(async (event) => {
if (toggleButton.className === "fas fa-play") {
playReplay();
} else if (toggleButton.className === "fas fa-pause") {
pauseReplay();
}
});
$("#replayWords").on("click", "letter", (event) => {
//allows user to click on the place they want to start their replay at
pauseReplay();
const replayWords = document.querySelector("#replayWords");
const words = [...replayWords.children];
targetWordPos = words.indexOf(event.target.parentNode);
const letters = [...words[targetWordPos].children];
targetCurPos = letters.indexOf(event.target);
initializeReplayPrompt();
loadOldReplay();
});
$(document).on("keypress", "#watchReplayButton", (event) => {
if (event.keyCode == 13) {
toggleReplayDisplay();
}
});
$(document.body).on("click", "#watchReplayButton", () => {
toggleReplayDisplay();
});
export {
startReplayRecording,
stopReplayRecording,
addReplayEvent,
replayGetWordsList,
};