-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
41 lines (34 loc) · 881 Bytes
/
index.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
let isInitialized = false
let entries = []
const keypress = key => {
for (let entry of entries) {
if (entry.word[entry.progress] !== key) {
entry.progress = 0
}
if (entry.word[entry.progress] === key) {
entry.progress += 1
if (entry.progress >= entry.word.length) {
entry.progress = 0
if (typeof entry.cb === 'function') {
entry.cb()
}
}
}
}
}
class TypeTrigger {
// This is mainly used for testing, but could be useful in application code as well
static type (word) {
word.split('').forEach(character => {
keypress(character)
})
}
static register (word, cb) {
if (!isInitialized) {
window.document.addEventListener('keypress', e => { keypress(e.key) })
isInitialized = true
}
entries.push({ word, cb, progress: 0 })
}
}
export default TypeTrigger