-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathApp.js
More file actions
executable file
·210 lines (195 loc) · 5.43 KB
/
App.js
File metadata and controls
executable file
·210 lines (195 loc) · 5.43 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
import React, { Component } from 'react';
import clsx from 'clsx';
// import logo from './logo.svg';
import './App.css';
const pad = (n) => (n < 10)? `0${n}` : n;
class App extends Component {
constructor(props) {
super(props);
this.state = {
t: 0,
tStart: 0,
paused: true,
mode: 'stopwatch',
fullscreen: false,
adjusting: false,
editing: null, // minute, second, null
showCursor: false,
}
this.timer = null;
}
componentDidMount() {
this.timer = setInterval(() => {
this.tick();
}, 500);
window.addEventListener('keydown', this.handleKeyDown);
}
componentWillMount() {
clearInterval(this.timer);
window.removeEventListener('keydown', this.handleKeyDown);
}
tick() {
const { mode, paused, showCursor, editing, tStart } = this.state;
if (editing) {
this.setState({ showCursor: !showCursor });
}
if (paused) return;
this.setState((prevState) => {
const t = prevState.t + (mode === 'countdown' ? -1 : 1) * 0.5;
if (t <= 0) {
return {
t: mode === 'countdown' ? tStart : 0,
paused: true,
}
} else {
return {
t,
}
}
});
}
toggleFullScreen = () => {
const { fullscreen } = this.state;
if (!fullscreen) {
document.documentElement.requestFullscreen();
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
}
}
this.setState({ fullscreen: !fullscreen });
}
resetTimer = () => {
const { mode, tStart } = this.state;
this.setState({
t: mode === 'countdown' ? tStart : 0,
paused: true
});
}
switchMode = (mode) => {
this.setState({
mode: mode || (this.state.mode === 'stopwatch' ? 'countdown' : 'stopwatch'),
tStart: (mode === 'countdown' || this.state.mode === 'stopwatch') ? this.state.t : 0
})
}
pauseTimer = () => {
this.setState({
paused: !this.state.paused,
editing: false,
})
}
toggleEditing = () => {
const { editing } = this.state;
this.setState({
editing: editing ? null : 'second',
});
}
handleCursorMove(direction) {
const state = { ...this.state };
state.paused = true;
switch (direction) {
case 'up':
case 'down':
if (!state.editing) {
state.editing = 'second';
}
state.t += (direction === 'up' ? 1 : -1) * (state.editing === 'second' ? 1 : 60);
if (state.t < 0) {
state.t = 0;
}
break;
case 'left':
state.editing = 'minute';
break;
case 'right':
state.editing = 'second';
break;
default:
break;
}
state.tStart = state.mode === 'countdown' ? state.t : 0;
this.setState(state);
}
handleKeyDown = (event) => {
switch (event.key) {
case 'F':
case 'f':
this.toggleFullScreen();
break;
case 'R':
case 'r':
this.resetTimer();
break;
case 'S':
case 's':
this.switchMode();
break;
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
this.handleCursorMove(event.key.toLowerCase().replace('arrow', ''))
break;
case 'Enter':
this.toggleEditing();
break;
case ' ':
this.pauseTimer();
break;
default:
break;
}
}
render() {
const { t, paused, editing, mode, showCursor, fullscreen } = this.state;
const second = parseInt(t % 60);
const minute = parseInt((t - second) / 60);
return (
<div className="App">
<div
className={clsx('clock', { 'show-cursor': showCursor })}
onDoubleClick={() => this.toggleFullScreen()}
>
<span className={clsx('time minute', { editing: editing === 'minute' })}>{pad(minute)}</span>
:
<span className={clsx('time second', { editing: editing === 'second' })}>{pad(second)}</span>
</div>
<ul className="tips">
<li>
<button onClick={this.toggleFullScreen}>F</button>
-
<span className="tip">{fullscreen ? 'exit': 'enter'} fullscreen</span>
</li>
<li>
<button onClick={() => this.handleCursorMove('left')}>←</button>
<button onClick={() => this.handleCursorMove('right')}>→</button>
<button onClick={() => this.handleCursorMove('up')}>↑</button>
<button onClick={() => this.handleCursorMove('down')}>↓</button>
-
<span className="tip">edit timer</span>
</li>
<li>
<button onClick={this.resetTimer}>R</button>
-
<span className="tip">reset timer</span>
</li>
<li>
<button onClick={this.switchMode}>S</button>
-
{mode === 'countdown' ?
<span className="tip"><span>countdown ✓</span> or <button onClick={() => this.switchMode('stopwatch')}>stopwatch</button></span>
:
<span className="tip"><button onClick={() => this.switchMode('countdown')}>countdown</button> or <span>stopwatch ✓</span></span>
}
</li>
<li>
<button onClick={this.pauseTimer}>Space</button>
-
<span className="tip">{paused ? 'start' : 'pause'} timer</span>
</li>
</ul>
</div>
);
}
}
export default App;