-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
161 lines (136 loc) · 4.21 KB
/
app.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
(function() {
const $game = document.querySelector('#game')
const $timer = document.querySelector('#timer')
const $complete = document.querySelector('#complete')
const $congrats = document.querySelector('#congrats')
const $message = document.querySelector('#message')
const converter = new showdown.Converter()
const timer = new Timer()
const challenges = getChallenges()
let completedChallenges = 0
disableClick()
function startGame() {
$game.innerHTML = ''
challenges.forEach(challenge => {
$game.appendChild(createChallenge(challenge))
})
$game.appendChild(createFinalChallenge())
setTimeout(() => {
window.scrollTo({
top: 0,
behavior: "smooth"
})
}, 200)
}
function createChallenge(challenge) {
const lines = Math.max(
countLines(challenge.start),
countLines(challenge.end)
)
const $container = document.createElement('article')
$container.className = 'hero content is-fullheight'
$container.innerHTML = `
<div className="hero-body">
<div className="container">
<form class="columns">
<fieldset class="column">
<h1>${challenge.title}</h1>
${markdown(challenge.description)}
</fieldset>
<fieldset class="column ${challenge.width || ''}">
<pre>${challenge.end}</pre>
<textarea class="textarea" rows="${lines - 1}" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">${challenge.start}</textarea>
</fieldset>
</form>
</div>
</div>
`
$container.querySelector('.textarea').addEventListener('keyup', (e) => {
const currentValue = e.target.value
const valueMatch = challenge.end.trim() === currentValue.trim()
const isSuccess = e.target.classList.contains('is-success')
if (valueMatch && !isSuccess) {
e.target.classList.add('is-success')
++completedChallenges
updateComplete()
}
else if (!valueMatch && isSuccess) {
e.target.classList.remove('is-success')
--completedChallenges
updateComplete()
}
})
return $container
}
function createFinalChallenge() {
const $container = document.createElement('footer')
$container.className = `hero content is-fullheight`
$container.innerHTML = `
<div class="hero-body">
<div class="container is-size-4">
<p>hit <button type="submit" class="kbd">enter</button> to complete</p>
</div>
</div>
`
return $container
}
function markdown(content) {
return converter.makeHtml(content)
}
function countLines(text) {
return (text.match(/\n/g) || []).length
}
function disableClick() {
document.querySelector('#click-mask').addEventListener('mousedown', (e) => {
e.preventDefault()
// TODO: minus points
})
}
function startTimer() {
$timer.innerHTML = `00:00`
timer.start()
timer.reset()
timer.addEventListener('secondsUpdated', (e) => {
$timer.innerHTML = timer.getTimeValues().toString(['minutes', 'seconds'])
})
updateComplete()
}
function stopTimer() {
timer.stop()
}
function updateComplete() {
$complete.innerHTML = `${completedChallenges} of ${challenges.length}`
}
function alertMessage(message = '') {
$message.innerText = message
if (message) {
setTimeout(alertMessage, 3000)
}
}
document.querySelector('#start-here button').addEventListener('blur', (e) => {
if (timer.getTimeValues().toString() === `00:00:00`) {
startTimer()
}
})
$game.addEventListener('submit', (e) => {
e.preventDefault()
if (completedChallenges === challenges.length) {
endGame()
}
else {
const challengesRemaining = challenges.length - completedChallenges
const challengePlural = challengesRemaining > 1 ? `challenges` : `challenge`
alertMessage(`${challengesRemaining} ${challengePlural} remaining!`)
}
})
function endGame() {
stopTimer()
setTimeout(() => {
window.scrollTo({
top: $congrats.getBoundingClientRect().top + window.scrollY,
behavior: "smooth"
})
}, 200)
}
startGame()
}())