-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
pomodoro.html
359 lines (334 loc) · 11.7 KB
/
pomodoro.html
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pomodoro Timer</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem;
background-color: #f0f0f0;
}
.container {
text-align: center;
background-color: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
box-sizing: border-box;
}
#timer {
font-size: 4rem;
margin-bottom: 1rem;
}
button, select, input {
font-size: 1rem;
padding: 0.5rem 1rem;
margin: 0.5rem;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
#startBtn {
background-color: #4CAF50;
color: white;
}
#startBtn:hover {
background-color: #45a049;
}
#resetBtn, #endBtn {
background-color: #f44336;
color: white;
}
#resetBtn:hover, #endBtn:hover {
background-color: #da190b;
}
select {
background-color: #3498db;
color: white;
-webkit-appearance: none;
padding: 0.5rem 1rem;
margin: 0.5rem;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
select:hover {
background-color: #2980b9;
}
#goalInput {
width: calc(100% - 1.5em);
padding: 0.5rem;
margin-bottom: 1rem;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
#sessionLog {
width: 100%;
max-width: 800px;
margin-top: 2rem;
overflow-x: auto;
}
#sessionLog table {
width: 100%;
border-collapse: collapse;
}
#sessionLog th, #sessionLog td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
#sessionLog th {
background-color: #f2f2f2;
}
#jsonOutput {
width: 100%;
height: 200px;
margin-top: 2rem;
font-family: monospace;
}
.deleteBtn {
background-color: transparent;
border: none;
color: #f44336;
cursor: pointer;
font-size: 1.2rem;
padding: 0.2rem 0.5rem;
}
.deleteBtn:hover {
color: #da190b;
}
</style>
</head>
<body>
<div class="container">
<h1>Pomodoro Timer</h1>
<input type="text" id="goalInput" placeholder="Enter your goal for this session" required>
<div id="timer">25:00</div>
<select id="durationSelect">
<option value="5">5 minutes</option>
<option value="10">10 minutes</option>
<option value="15">15 minutes</option>
<option value="20">20 minutes</option>
<option value="25" selected>25 minutes</option>
<option value="30">30 minutes</option>
<option value="45">45 minutes</option>
<option value="60">60 minutes</option>
</select>
<br>
<button id="startBtn">Start</button>
<button id="resetBtn">Reset</button>
<button id="endBtn">End Session</button>
</div>
<div id="sessionLog">
<h2>Session Log</h2>
<table>
<thead>
<tr>
<th>Goal</th>
<th>Start Time</th>
<th>End Time</th>
<th>Duration</th>
<th>Pauses</th>
<th>Action</th>
</tr>
</thead>
<tbody id="sessionLogBody"></tbody>
</table>
</div>
<textarea id="jsonOutput" readonly></textarea>
<script>
const timerDisplay = document.getElementById('timer');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const endBtn = document.getElementById('endBtn');
const durationSelect = document.getElementById('durationSelect');
const goalInput = document.getElementById('goalInput');
const sessionLogBody = document.getElementById('sessionLogBody');
const jsonOutput = document.getElementById('jsonOutput');
let startTime;
let timeLeft = 25 * 60; // Default to 25 minutes in seconds
let isRunning = false;
let duration = 25 * 60; // Default duration in seconds
let currentSession = null;
let sessions = [];
// Check if localStorage is available
function isLocalStorageAvailable() {
try {
localStorage.setItem('test', 'test');
localStorage.removeItem('test');
return true;
} catch (e) {
return false;
}
}
// Load sessions from storage
function loadSessions() {
if (isLocalStorageAvailable()) {
const storedSessions = localStorage.getItem('pomodoroSessions');
if (storedSessions) {
sessions = JSON.parse(storedSessions);
}
} else {
console.warn('localStorage is not available. Session data will not persist.');
}
updateSessionLog();
}
loadSessions();
function formatDate(date) {
return new Date(date).toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true
}).replace(',', '');
}
function updateDisplay() {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
function startTimer() {
if (!isRunning) {
if (!goalInput.value) {
alert('Please enter a goal for this session.');
return;
}
isRunning = true;
startBtn.textContent = 'Pause';
durationSelect.disabled = true;
goalInput.disabled = true;
if (!currentSession) {
currentSession = {
goal: goalInput.value,
startTime: new Date().toISOString(),
endTime: null,
duration: 0,
pauses: []
};
} else {
currentSession.pauses[currentSession.pauses.length - 1].resumeTime = new Date().toISOString();
}
startTime = Date.now() - ((duration - timeLeft) * 1000);
requestAnimationFrame(updateTimer);
} else {
isRunning = false;
startBtn.textContent = 'Resume';
currentSession.pauses.push({
pauseTime: new Date().toISOString(),
resumeTime: null
});
}
}
function updateTimer() {
if (isRunning) {
const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
timeLeft = duration - elapsedTime;
if (timeLeft <= 0) {
timeLeft = 0;
isRunning = false;
startBtn.textContent = 'Start';
durationSelect.disabled = false;
goalInput.disabled = false;
endSession();
alert('Pomodoro session complete!');
} else {
requestAnimationFrame(updateTimer);
}
updateDisplay();
}
}
function resetTimer() {
isRunning = false;
duration = parseInt(durationSelect.value) * 60;
timeLeft = duration;
updateDisplay();
startBtn.textContent = 'Start';
durationSelect.disabled = false;
goalInput.disabled = false;
currentSession = null;
}
function changeDuration() {
if (!isRunning) {
duration = parseInt(durationSelect.value) * 60;
timeLeft = duration;
updateDisplay();
}
}
function endSession() {
if (currentSession) {
currentSession.endTime = new Date().toISOString();
currentSession.duration = Math.round((new Date(currentSession.endTime) - new Date(currentSession.startTime)) / 1000);
sessions.unshift(currentSession);
updateSessionLog();
saveSessions();
resetTimer();
}
}
function updateSessionLog() {
sessionLogBody.innerHTML = '';
sessions.forEach((session, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${session.goal}</td>
<td>${formatDate(session.startTime)}</td>
<td>${session.endTime ? formatDate(session.endTime) : 'In progress'}</td>
<td>${formatDuration(session.duration)}</td>
<td>${formatPauses(session.pauses)}</td>
<td><button class="deleteBtn" data-index="${index}">❌</button></td>
`;
sessionLogBody.appendChild(row);
});
jsonOutput.value = JSON.stringify(sessions, null, 2);
// Add event listeners to delete buttons
document.querySelectorAll('.deleteBtn').forEach(btn => {
btn.addEventListener('click', deleteSession);
});
}
function deleteSession(event) {
const index = parseInt(event.target.getAttribute('data-index'));
sessions.splice(index, 1);
updateSessionLog();
saveSessions();
}
function formatDuration(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}m ${remainingSeconds}s`;
}
function formatPauses(pauses) {
return pauses.map(pause =>
`${formatDate(pause.pauseTime)} - ${pause.resumeTime ? formatDate(pause.resumeTime) : 'Not resumed'}`
).join('<br>');
}
function saveSessions() {
if (isLocalStorageAvailable()) {
localStorage.setItem('pomodoroSessions', JSON.stringify(sessions));
}
}
startBtn.addEventListener('click', startTimer);
resetBtn.addEventListener('click', resetTimer);
endBtn.addEventListener('click', endSession);
durationSelect.addEventListener('change', changeDuration);
// Keyboard shortcut
document.addEventListener('keydown', (e) => {
if (e.code === 'Space' && document.activeElement !== goalInput) {
e.preventDefault();
startTimer();
}
});
updateDisplay();
</script>
</body>
</html>