Repository navigation
Expand file tree
/
Copy pathtype.js
More file actions
61 lines (52 loc) · 1.41 KB
/
Copy pathtype.js
File metadata and controls
61 lines (52 loc) · 1.41 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
const writeChar = (element, char) => {
element.dispatchEvent(
new KeyboardEvent("keydown", {
key: char.charCodeAt(0),
char,
shiftKey: true,
bubbles: true
})
);
element.dispatchEvent(
new KeyboardEvent("keypress", {
key: char.charCodeAt(0),
char,
shiftKey: true,
bubbles: true
})
);
document.execCommand("insertText", true, char);
element.dispatchEvent(
new KeyboardEvent("keyup", {
key: char.charCodeAt(0),
char,
shiftKey: true,
bubbles: true
})
);
};
// Globally keep track of the paragraph index
let contentIndex = 0;
const writeOnElement = (element, state) => {
// Assuming words are 5 char in average
const cpm = state.wpm * 5;
if (cpm < 1) {
// We don't type if the user sets the typing to 0 or a negative number
clearInterval(interval);
interval = null;
return;
}
const timeToWrite = 60 / cpm;
if (interval) {
clearInterval(interval);
interval = null;
}
interval = setInterval(() => {
writeChar(element, lorem.text[contentIndex]);
++contentIndex;
if (contentIndex >= lorem.text.length) {
// loop through the same text
contentIndex = 0;
}
}, timeToWrite * 1000);
};