Repository navigation
Expand file tree
/
Copy pathstate.js
More file actions
501 lines (448 loc) · 20 KB
/
Copy pathstate.js
File metadata and controls
501 lines (448 loc) · 20 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* Pure task/category operations. No DOM, no storage.
*
* A state object looks like the persisted data:
* { tasks, completed, archived, categories, currentCategory }
* `tasks` is the stack: the last element of a category is its top task.
* Every operation mutates the state in place and returns { ok, ... }.
*/
(function (global) {
'use strict';
const DEFAULT_CATEGORIES = ['Work', 'Personal'];
const MAX_TASK_LENGTH = 280;
const MAX_CATEGORY_LENGTH = 40;
// One tomato = 25 minutes. Fixed on purpose: no setting to fiddle with.
const POMODORO_MS = 25 * 60 * 1000;
let idCounter = 0;
// New ids are strings. Older data has numeric ids (Date.now()); both are accepted.
function newId() {
try {
if (global.crypto && typeof global.crypto.randomUUID === 'function') {
return global.crypto.randomUUID();
}
} catch (e) {
// randomUUID can be unavailable outside secure contexts; use the fallback below
}
idCounter += 1;
return 't' + Date.now().toString(36) + idCounter.toString(36) + Math.random().toString(36).slice(2, 8);
}
function sameId(a, b) {
return String(a) === String(b);
}
function indexOfId(list, id) {
return list.findIndex(task => sameId(task.id, id));
}
function createState(data) {
const source = data || {};
const categories = Array.isArray(source.categories) && source.categories.length > 0
? source.categories.slice()
: DEFAULT_CATEGORIES.slice();
return {
tasks: Array.isArray(source.tasks) ? source.tasks : [],
completed: Array.isArray(source.completed) ? source.completed : [],
archived: Array.isArray(source.archived) ? source.archived : [],
categories,
currentCategory: categories.includes(source.currentCategory)
? source.currentCategory
: categories[0]
};
}
// Deep copy of the persisted shape; used as an undo snapshot.
function clone(state) {
return createState(JSON.parse(JSON.stringify(state)));
}
// ---- Queries -------------------------------------------------------
function inCategory(list, category) {
return list.filter(task => task.category === category);
}
// Index (in state.tasks) of the top task of the current category, or -1.
function topTaskIndex(state) {
for (let i = state.tasks.length - 1; i >= 0; i--) {
if (state.tasks[i].category === state.currentCategory) return i;
}
return -1;
}
function countInCategory(state, name) {
return inCategory(state.tasks, name).length
+ inCategory(state.completed, name).length
+ inCategory(state.archived, name).length;
}
// ---- Task operations -----------------------------------------------
function push(state, text) {
const trimmed = typeof text === 'string' ? text.trim() : '';
if (!trimmed) return { ok: false, error: 'Task is empty.' };
if (trimmed.length > MAX_TASK_LENGTH) {
return { ok: false, error: `Task is too long (max ${MAX_TASK_LENGTH} characters).` };
}
const task = {
text: trimmed,
id: newId(),
createdAt: new Date().toISOString(),
category: state.currentCategory
};
state.tasks.push(task);
return { ok: true, task };
}
function renameTask(state, id, text) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
const trimmed = typeof text === 'string' ? text.trim() : '';
if (!trimmed) return { ok: false, error: 'Task is empty.' };
if (trimmed.length > MAX_TASK_LENGTH) {
return { ok: false, error: `Task is too long (max ${MAX_TASK_LENGTH} characters).` };
}
state.tasks[index] = { ...state.tasks[index], text: trimmed };
return { ok: true, task: state.tasks[index] };
}
// ---- Timers --------------------------------------------------------
// A task accumulates `timeSpent` (ms). While its timer runs, `timerStartedAt` holds the start
// time, so the elapsed time is derived from the clock and survives reloads. One timer at a time.
function isRunning(task) {
return typeof task.timerStartedAt === 'string' && !Number.isNaN(Date.parse(task.timerStartedAt));
}
function elapsedMs(task, now) {
const base = typeof task.timeSpent === 'number' && task.timeSpent > 0 ? task.timeSpent : 0;
if (!isRunning(task)) return base;
const at = now === undefined ? Date.now() : now;
return base + Math.max(0, at - Date.parse(task.timerStartedAt));
}
// The task with its running time folded into timeSpent and the timer cleared.
function settled(task, now) {
if (!isRunning(task)) return task;
const { timerStartedAt, ...rest } = task;
return { ...rest, timeSpent: elapsedMs(task, now) };
}
// Time left in the current tomato. Derived from the accumulated time, so no new field is stored:
// 0 min logged -> 25:00, 12 min -> 13:00, 37 min (one full tomato + 12) -> 13:00.
function remainingMs(task, now) {
return POMODORO_MS - (elapsedMs(task, now) % POMODORO_MS);
}
// Whole tomatoes completed so far
function ripeCount(task, now) {
return Math.floor(elapsedMs(task, now) / POMODORO_MS);
}
// Paused exactly on a tomato boundary: the state a timer is left in when it ripens
function isRipe(task) {
const spent = typeof task.timeSpent === 'number' ? task.timeSpent : 0;
return !isRunning(task) && spent > 0 && spent % POMODORO_MS === 0;
}
// Stops every running timer that has passed its next 25-minute boundary (the timer never completes a task).
// `watched(task)` says whether an open page has been running this timer. If so, it ripened at the boundary
// and is recorded as exactly that, however late it is noticed (a background tab's ticks are throttled or
// frozen; that delay is the app's, not time worked). A timer met for the first time, such as one left
// running by an older version while the page was closed, keeps its full elapsed time: nothing logged is dropped.
// Returns [{ task, late, kept }]: `late` is how long after the boundary it was noticed, `kept` whether that was logged.
function settleRipe(state, now, watched) {
const at = now === undefined ? Date.now() : now;
const stopped = [];
state.tasks = state.tasks.map(task => {
if (!isRunning(task)) return task;
const base = typeof task.timeSpent === 'number' && task.timeSpent > 0 ? task.timeSpent : 0;
const boundary = (Math.floor(base / POMODORO_MS) + 1) * POMODORO_MS;
const elapsed = elapsedMs(task, at);
if (elapsed < boundary) return task;
const kept = !(typeof watched === 'function' && watched(task));
const { timerStartedAt, ...rest } = task;
const next = { ...rest, timeSpent: kept ? elapsed : boundary };
stopped.push({ task: next, late: elapsed - boundary, kept });
return next;
});
return stopped;
}
// A running timer belongs to the top task of its category. When something else becomes the top
// (an add, a move, an undo), the buried task's timer is paused. Returns the paused tasks.
function pauseBuried(state, now) {
const at = now === undefined ? Date.now() : now;
const tops = new Map();
state.tasks.forEach(task => tops.set(task.category, task));
const paused = [];
state.tasks = state.tasks.map(task => {
if (!isRunning(task) || tops.get(task.category) === task) return task;
const next = settled(task, at);
paused.push(next);
return next;
});
return paused;
}
// The first screen shows the top task of the current category, so a running timer has to be in that
// category. With `follow` (nobody chose a category: a load, another tab, an import) the current category
// moves to the running timer. Any timer still outside the current category (the user switched away from it)
// is paused with its time kept. Returns { followed, paused }.
function keepTimerInView(state, now, follow) {
const at = now === undefined ? Date.now() : now;
const running = state.tasks.filter(isRunning);
let followed = false;
if (follow && running.length > 0 && !running.some(task => task.category === state.currentCategory)
&& state.categories.includes(running[0].category)) {
state.currentCategory = running[0].category;
followed = true;
}
const paused = [];
state.tasks = state.tasks.map(task => {
if (!isRunning(task) || task.category === state.currentCategory) return task;
const next = settled(task, at);
paused.push(next);
return next;
});
return { followed, paused };
}
function runningTask(state) {
return state.tasks.find(isRunning) || null;
}
function pauseTimer(state, id, now) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
state.tasks[index] = settled(state.tasks[index], now);
return { ok: true, task: state.tasks[index] };
}
function startTimer(state, id, now) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
const at = now === undefined ? Date.now() : now;
state.tasks = state.tasks.map(task => settled(task, at));
state.tasks[index] = { ...state.tasks[index], timerStartedAt: new Date(at).toISOString() };
return { ok: true, task: state.tasks[index] };
}
function toggleTimer(state, id, now) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
return isRunning(state.tasks[index]) ? pauseTimer(state, id, now) : startTimer(state, id, now);
}
// Completes the top task of the current category (not the global top). Its timer stops.
function pop(state) {
const index = topTaskIndex(state);
if (index === -1) return { ok: false };
const task = settled(state.tasks.splice(index, 1)[0]);
const done = { ...task, completedAt: new Date().toISOString() };
state.completed.unshift(done);
return { ok: true, task: done };
}
function archiveTask(state, id) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
const task = settled(state.tasks.splice(index, 1)[0]);
const archived = { ...task, archivedAt: new Date().toISOString() };
state.archived.unshift(archived);
return { ok: true, task: archived };
}
function removeTask(state, id) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
return { ok: true, task: state.tasks.splice(index, 1)[0] };
}
// Restored tasks go to the bottom of the stack (index 0).
function restoreArchived(state, id) {
const index = indexOfId(state.archived, id);
if (index === -1) return { ok: false };
const task = state.archived.splice(index, 1)[0];
delete task.archivedAt;
state.tasks.unshift(task);
return { ok: true, task };
}
function deleteArchived(state, id) {
const index = indexOfId(state.archived, id);
if (index === -1) return { ok: false };
return { ok: true, task: state.archived.splice(index, 1)[0] };
}
function restoreCompleted(state, id) {
const index = indexOfId(state.completed, id);
if (index === -1) return { ok: false };
const task = state.completed.splice(index, 1)[0];
delete task.completedAt;
state.tasks.unshift(task);
return { ok: true, task };
}
function deleteCompleted(state, id) {
const index = indexOfId(state.completed, id);
if (index === -1) return { ok: false };
return { ok: true, task: state.completed.splice(index, 1)[0] };
}
// The stack is displayed top-first (reversed), so `insertBefore` means
// "visually above the target" = a higher index in the array.
function reorderTasks(state, draggedId, targetId, insertBefore) {
const draggedIndex = indexOfId(state.tasks, draggedId);
const targetIndex = indexOfId(state.tasks, targetId);
if (draggedIndex === -1 || targetIndex === -1 || draggedIndex === targetIndex) {
return { ok: false };
}
const dragged = state.tasks.splice(draggedIndex, 1)[0];
let insertIndex = draggedIndex < targetIndex ? targetIndex - 1 : targetIndex;
if (insertBefore) insertIndex++;
state.tasks.splice(insertIndex, 0, dragged);
return { ok: true };
}
// Swaps a task with its neighbour in the same category. 'up' moves it toward the top of the stack.
function moveTask(state, id, direction) {
const index = indexOfId(state.tasks, id);
if (index === -1) return { ok: false };
const category = state.tasks[index].category;
const step = direction === 'up' ? 1 : -1;
for (let i = index + step; i >= 0 && i < state.tasks.length; i += step) {
if (state.tasks[i].category === category) {
[state.tasks[index], state.tasks[i]] = [state.tasks[i], state.tasks[index]];
return { ok: true };
}
}
return { ok: false };
}
// 1-based position of a task counted from the top of its category's stack, or 0.
function positionFromTop(state, id) {
const index = indexOfId(state.tasks, id);
if (index === -1) return 0;
const visible = inCategory(state.tasks, state.tasks[index].category);
return visible.length - visible.findIndex(task => sameId(task.id, id));
}
// ---- Category operations -------------------------------------------
function validateCategoryName(state, name, ignoring) {
const trimmed = typeof name === 'string' ? name.trim() : '';
if (!trimmed) return { ok: false, error: 'Category name is empty.' };
if (trimmed.length > MAX_CATEGORY_LENGTH) {
return { ok: false, error: `Category name is too long (max ${MAX_CATEGORY_LENGTH} characters).` };
}
const lower = trimmed.toLowerCase();
const clash = state.categories.some(c => c !== ignoring && c.toLowerCase() === lower);
if (clash) return { ok: false, error: 'Category already exists!' };
return { ok: true, name: trimmed };
}
function createCategory(state, name) {
const checked = validateCategoryName(state, name, null);
if (!checked.ok) return checked;
state.categories.push(checked.name);
state.currentCategory = checked.name;
return { ok: true, name: checked.name };
}
function renameCategory(state, oldName, newName) {
const index = state.categories.indexOf(oldName);
if (index === -1) return { ok: false };
const checked = validateCategoryName(state, newName, oldName);
if (!checked.ok) return checked;
state.categories[index] = checked.name;
[state.tasks, state.completed, state.archived].forEach(list => {
list.forEach(task => {
if (task.category === oldName) task.category = checked.name;
});
});
if (state.currentCategory === oldName) state.currentCategory = checked.name;
return { ok: true, name: checked.name };
}
// Removes the category together with every task in it.
function deleteCategory(state, name) {
const index = state.categories.indexOf(name);
if (index === -1) return { ok: false };
if (state.categories.length === 1) {
return { ok: false, error: 'Cannot delete the last category!' };
}
state.categories.splice(index, 1);
state.tasks = state.tasks.filter(task => task.category !== name);
state.completed = state.completed.filter(task => task.category !== name);
state.archived = state.archived.filter(task => task.category !== name);
if (state.currentCategory === name) state.currentCategory = state.categories[0];
return { ok: true };
}
function switchCategory(state, name) {
if (!state.categories.includes(name)) return { ok: false };
state.currentCategory = name;
return { ok: true };
}
function reorderCategories(state, draggedName, targetName, insertBefore) {
const draggedIndex = state.categories.indexOf(draggedName);
const targetIndex = state.categories.indexOf(targetName);
if (draggedIndex === -1 || targetIndex === -1 || draggedIndex === targetIndex) {
return { ok: false };
}
const dragged = state.categories.splice(draggedIndex, 1)[0];
let insertIndex = draggedIndex < targetIndex ? targetIndex - 1 : targetIndex;
if (!insertBefore) insertIndex++;
state.categories.splice(insertIndex, 0, dragged);
return { ok: true };
}
// ---- Formatting ----------------------------------------------------
function formatTime(value, now) {
if (value === undefined || value === null || value === '') return 'Unknown time';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return 'Unknown time';
const diff = (now === undefined ? Date.now() : now) - date.getTime();
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (minutes < 1) return 'Just now';
if (minutes < 60) return `${minutes}m ago`;
if (hours < 24) return `${hours}h ago`;
if (days < 7) return `${days}d ago`;
return date.toLocaleDateString();
}
// Live clock: m:ss under an hour, h:mm:ss from then on
function formatClock(ms) {
const total = Math.max(0, Math.floor(ms / 1000));
const h = Math.floor(total / 3600);
const m = Math.floor((total % 3600) / 60);
const sec = total % 60;
const two = n => String(n).padStart(2, '0');
return h > 0 ? `${h}:${two(m)}:${two(sec)}` : `${m}:${two(sec)}`;
}
// Countdown face: always mm:ss
function formatRemaining(ms) {
const total = Math.max(0, Math.ceil(ms / 1000));
const two = n => String(n).padStart(2, '0');
return `${two(Math.floor(total / 60))}:${two(total % 60)}`;
}
// Compact summary for finished work: 45s, 12m, 1h 05m
function formatDuration(ms) {
const total = Math.max(0, Math.round(ms / 1000));
if (total < 60) return `${total}s`;
const h = Math.floor(total / 3600);
const m = Math.round((total % 3600) / 60);
if (h === 0) return `${m}m`;
return `${h}h ${String(m).padStart(2, '0')}m`;
}
const api = {
DEFAULT_CATEGORIES,
MAX_TASK_LENGTH,
MAX_CATEGORY_LENGTH,
POMODORO_MS,
newId,
sameId,
createState,
clone,
inCategory,
topTaskIndex,
countInCategory,
push,
renameTask,
isRunning,
elapsedMs,
remainingMs,
ripeCount,
isRipe,
settleRipe,
pauseBuried,
keepTimerInView,
runningTask,
startTimer,
pauseTimer,
toggleTimer,
formatClock,
formatRemaining,
formatDuration,
pop,
archiveTask,
removeTask,
restoreArchived,
deleteArchived,
restoreCompleted,
deleteCompleted,
reorderTasks,
moveTask,
positionFromTop,
validateCategoryName,
createCategory,
renameCategory,
deleteCategory,
switchCategory,
reorderCategories,
formatTime
};
global.StackTask = global.StackTask || {};
global.StackTask.State = api;
if (typeof module !== 'undefined' && module.exports) module.exports = api;
})(typeof window !== 'undefined' ? window : globalThis);