]>
Commit | Line | Data |
---|---|---|
1 | // Typescript doesn't know about MAX_SAFE_INTEGER?? This was supposed to be | |
2 | // fixed in typescript 2.0.1 in 2016, but is not working for me in typescript | |
3 | // 4.2.4 in 2022. :( https://github.com/microsoft/TypeScript/issues/9937 | |
4 | //const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER; | |
5 | const MAX_SAFE_INTEGER = 9007199254740991; | |
6 | ||
7 | // A sane split that splits N *times*, leaving the last chunk unsplit. | |
8 | function splitN(str: string, delimiter: string, limit: number = MAX_SAFE_INTEGER): string[] { | |
9 | if (limit < 1) { | |
10 | return [str]; | |
11 | } | |
12 | const at = str.indexOf(delimiter); | |
13 | return at === -1 ? [str] : [str.substring(0, at)].concat(splitN(str.substring(at + delimiter.length), delimiter, limit - 1)); | |
14 | } | |
15 | ||
16 | // A clock that never goes backwards; monotonic. | |
17 | function Clock() { | |
18 | var previousNow = Date.now(); | |
19 | return { | |
20 | now: function (): number { | |
21 | const now = Date.now(); | |
22 | if (now > previousNow) { | |
23 | previousNow = now; | |
24 | return now; | |
25 | } | |
26 | return ++previousNow; | |
27 | }, | |
28 | }; | |
29 | } | |
30 | const clock = Clock(); | |
31 | ||
32 | // Returns a promise for a hue based on a hash of the string | |
33 | function hashHue(str: string) { | |
34 | // Using crypto for this is overkill | |
35 | return crypto.subtle.digest("SHA-256", new TextEncoder().encode(str)).then((buf) => (new Uint16Array(buf)[0] * 360) / 2 ** 16); | |
36 | } | |
37 | ||
38 | function Model() { | |
39 | return { | |
40 | addTask: function (timestamp: string, description: string): Element { | |
41 | const task = document.createElement("div"); | |
42 | const desc = document.createElement("span"); | |
43 | desc.textContent = description; | |
44 | desc.classList.add("desc"); | |
45 | task.appendChild(desc); | |
46 | task.classList.add("task"); | |
47 | task.setAttribute("tabindex", "0"); | |
48 | task.setAttribute("id", timestamp); | |
49 | task.setAttribute("data-state", "todo"); | |
50 | const tasks = document.getElementById("tasks")!; | |
51 | tasks.insertBefore(task, tasks.firstElementChild); | |
52 | return task; | |
53 | }, | |
54 | ||
55 | addTag: function (createTimestamp: string, tagName: string): Element | null { | |
56 | const task = this.getTask(createTimestamp); | |
57 | if (!task) return null; | |
58 | const existingTag = this.hasTag(task, tagName); | |
59 | if (existingTag) return existingTag; | |
60 | const tag = document.createElement("span"); | |
61 | tag.appendChild(document.createTextNode(tagName)); | |
62 | tag.classList.add("tag"); | |
63 | tag.setAttribute("tabindex", "0"); | |
64 | hashHue(tagName).then((hue) => (tag.style.backgroundColor = `hsl(${hue},90%,45%)`)); | |
65 | for (const child of task.getElementsByClassName("tag")) { | |
66 | if (tagName > child.textContent!) { | |
67 | task.insertBefore(tag, child); | |
68 | return tag; | |
69 | } | |
70 | } | |
71 | task.insertBefore(tag, task.getElementsByClassName("desc")[0]!); | |
72 | return tag; | |
73 | }, | |
74 | ||
75 | edit: function (createTimestamp: string, newDescription: string): Element | null { | |
76 | const target = this.getTask(createTimestamp); | |
77 | if (!target) return null; | |
78 | if (target.hasAttribute("data-description")) { | |
79 | // Oh no: An edit has arrived from a replica while a local edit is in progress. | |
80 | const input = target.getElementsByTagName("input")[0]!; | |
81 | if ( | |
82 | input.value === target.getAttribute("data-description") && | |
83 | input.selectionStart === input.value.length && | |
84 | input.selectionEnd === input.value.length | |
85 | ) { | |
86 | // No local changes have actually been made yet. Change the contents of the edit box! | |
87 | input.value = newDescription; | |
88 | } else { | |
89 | // No great options. | |
90 | // Prefer not to interrupt the local user's edit. | |
91 | // The remote edit is mostly lost; this mostly becomes last-write-wins. | |
92 | target.setAttribute("data-description", newDescription); | |
93 | } | |
94 | } else { | |
95 | target.getElementsByClassName("desc")[0].textContent = newDescription; | |
96 | } | |
97 | return target; | |
98 | }, | |
99 | ||
100 | editContent: function (createTimestamp: string, newContent: string): Element | null { | |
101 | const target = this.getTask(createTimestamp); | |
102 | if (!target) return null; | |
103 | if (target.hasAttribute("data-content")) { | |
104 | // Oh no: An edit has arrived from a replica while a local edit is in progress. | |
105 | const input = target.getElementsByTagName("textarea")[0]!; | |
106 | if ( | |
107 | input.value === target.getAttribute("data-content") && | |
108 | input.selectionStart === input.value.length && | |
109 | input.selectionEnd === input.value.length | |
110 | ) { | |
111 | // No local changes have actually been made yet. Change the contents of the edit box! | |
112 | input.value = newContent; | |
113 | } else { | |
114 | // No great options. | |
115 | // Prefer not to interrupt the local user's edit. | |
116 | // The remote edit is mostly lost; this mostly becomes last-write-wins. | |
117 | target.setAttribute("data-content", newContent); | |
118 | } | |
119 | } else { | |
120 | var content = target.getElementsByClassName("content")[0]; | |
121 | if (!content) { | |
122 | content = document.createElement("div"); | |
123 | content.classList.add("content"); | |
124 | target.appendChild(content); | |
125 | } | |
126 | content.textContent = newContent; | |
127 | } | |
128 | return target; | |
129 | }, | |
130 | ||
131 | hasTag: function (task: Element, tag: string): Element | null { | |
132 | for (const child of task.getElementsByClassName("tag")) { | |
133 | if (child.textContent === tag) { | |
134 | return child; | |
135 | } | |
136 | } | |
137 | return null; | |
138 | }, | |
139 | ||
140 | getPriority: function (task: Element): number { | |
141 | if (task.hasAttribute("data-priority")) { | |
142 | return parseFloat(task.getAttribute("data-priority")!); | |
143 | } | |
144 | return parseFloat(task.getAttribute("id")!); | |
145 | }, | |
146 | ||
147 | getTask: function (createTimestamp: string) { | |
148 | return document.getElementById(createTimestamp); | |
149 | }, | |
150 | ||
151 | insertInPriorityOrder: function (task: Element, dest: Element) { | |
152 | const priority = this.getPriority(task); | |
153 | for (const t of dest.children) { | |
154 | if (t !== task && this.getPriority(t) < priority) { | |
155 | dest.insertBefore(task, t); | |
156 | return; | |
157 | } | |
158 | } | |
159 | dest.appendChild(task); | |
160 | }, | |
161 | ||
162 | removeTag: function (createTimestamp: string, tagName: string) { | |
163 | const task = this.getTask(createTimestamp); | |
164 | if (!task) return null; | |
165 | const tag = this.hasTag(task, tagName); | |
166 | if (!tag) return; | |
167 | task.removeChild(tag); | |
168 | if (task instanceof HTMLElement) task.focus(); | |
169 | }, | |
170 | ||
171 | setPriority: function (createTimestamp: string, priority: number): Element | null { | |
172 | const target = this.getTask(createTimestamp); | |
173 | if (!target) return null; | |
174 | target.setAttribute("data-priority", `${priority}`); | |
175 | this.insertInPriorityOrder(target, target.parentElement!); | |
176 | return target; | |
177 | }, | |
178 | ||
179 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { | |
180 | const task = this.getTask(createTimestamp); | |
181 | if (!task) return; | |
182 | task.setAttribute("data-state", state); | |
183 | var date = task.getElementsByClassName("statedate")[0]; | |
184 | if (state === "todo") { | |
185 | task.removeChild(date); | |
186 | return; | |
187 | } | |
188 | if (!date) { | |
189 | date = document.createElement("span"); | |
190 | date.classList.add("statedate"); | |
191 | task.insertBefore(date, task.firstChild); | |
192 | } | |
193 | const d = new Date(parseInt(stateTimestamp)); | |
194 | date.textContent = `${d.getFullYear()}-${`${d.getMonth() + 1}`.padStart(2, "0")}-${`${d.getDate()}`.padStart(2, "0")}`; | |
195 | }, | |
196 | }; | |
197 | } | |
198 | const model = Model(); | |
199 | ||
200 | function Log(prefix: string = "vp-") { | |
201 | var next_log_index = 0; | |
202 | return { | |
203 | apply: function (entry: string) { | |
204 | const [timestamp, command, data] = splitN(entry, " ", 2); | |
205 | if (command == "Create") { | |
206 | return model.addTask(timestamp, data); | |
207 | } | |
208 | if (command == "Edit") { | |
209 | const [createTimestamp, description] = splitN(data, " ", 1); | |
210 | return model.edit(createTimestamp, description); | |
211 | } | |
212 | if (command == "EditContent") { | |
213 | const [createTimestamp, content] = splitN(data, " ", 1); | |
214 | return model.editContent(createTimestamp, content); | |
215 | } | |
216 | if (command == "Priority") { | |
217 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
218 | return model.setPriority(createTimestamp, parseFloat(newPriority)); | |
219 | } | |
220 | if (command == "State") { | |
221 | const [createTimestamp, state] = splitN(data, " ", 1); | |
222 | return model.setState(timestamp, createTimestamp, state); | |
223 | } | |
224 | if (command == "Tag") { | |
225 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
226 | return model.addTag(createTimestamp, tag); | |
227 | } | |
228 | if (command == "Untag") { | |
229 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
230 | return model.removeTag(createTimestamp, tag); | |
231 | } | |
232 | }, | |
233 | ||
234 | record: function (entry: string) { | |
235 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); | |
236 | }, | |
237 | ||
238 | recordAndApply: function (entry: string) { | |
239 | this.record(entry); | |
240 | return this.apply(entry); | |
241 | }, | |
242 | ||
243 | replay: function () { | |
244 | document.getElementById("tasks")!.style.display = "none"; | |
245 | while (true) { | |
246 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); | |
247 | if (entry === null) { | |
248 | break; | |
249 | } | |
250 | this.apply(entry); | |
251 | next_log_index++; | |
252 | } | |
253 | document.getElementById("tasks")!.style.display = ""; | |
254 | }, | |
255 | }; | |
256 | } | |
257 | const log = Log(); | |
258 | ||
259 | function UI() { | |
260 | const undoLog: string[][] = []; | |
261 | const redoLog: string[][] = []; | |
262 | function perform(forward: string, reverse: string) { | |
263 | undoLog.push([reverse, forward]); | |
264 | return log.recordAndApply(`${clock.now()} ${forward}`); | |
265 | } | |
266 | return { | |
267 | addTask: function (description: string): Element { | |
268 | const now = clock.now(); | |
269 | undoLog.push([`State ${now} deleted`, `State ${now} todo`]); | |
270 | return <Element>log.recordAndApply(`${now} Create ${description}`); | |
271 | }, | |
272 | addTag: function (createTimestamp: string, tag: string) { | |
273 | return perform(`Tag ${createTimestamp} ${tag}`, `Untag ${createTimestamp} ${tag}`); | |
274 | }, | |
275 | edit: function (createTimestamp: string, newDescription: string, oldDescription: string) { | |
276 | return perform(`Edit ${createTimestamp} ${newDescription}`, `Edit ${createTimestamp} ${oldDescription}`); | |
277 | }, | |
278 | editContent: function (createTimestamp: string, newContent: string, oldContent: string) { | |
279 | return perform(`EditContent ${createTimestamp} ${newContent}`, `EditContent ${createTimestamp} ${oldContent}`); | |
280 | }, | |
281 | removeTag: function (createTimestamp: string, tag: string) { | |
282 | return perform(`Untag ${createTimestamp} ${tag}`, `Tag ${createTimestamp} ${tag}`); | |
283 | }, | |
284 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { | |
285 | return perform(`Priority ${createTimestamp} ${newPriority}`, `Priority ${createTimestamp} ${oldPriority}`); | |
286 | }, | |
287 | setState: function (createTimestamp: string, newState: string, oldState: string) { | |
288 | return perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`); | |
289 | }, | |
290 | undo: function () { | |
291 | const entry = undoLog.pop(); | |
292 | if (entry) { | |
293 | redoLog.push(entry); | |
294 | return log.recordAndApply(`${clock.now()} ${entry[0]}`); | |
295 | } | |
296 | }, | |
297 | redo: function () { | |
298 | const entry = redoLog.pop(); | |
299 | if (entry) { | |
300 | undoLog.push(entry); | |
301 | return log.recordAndApply(`${clock.now()} ${entry[1]}`); | |
302 | } | |
303 | }, | |
304 | }; | |
305 | } | |
306 | const ui = UI(); | |
307 | ||
308 | enum CommitOrAbort { | |
309 | Commit, | |
310 | Abort, | |
311 | } | |
312 | ||
313 | function BrowserUI() { | |
314 | const viewColors: { [key: string]: string } = { | |
315 | all: "Gold", | |
316 | cancelled: "Red", | |
317 | deleted: "Black", | |
318 | done: "LawnGreen", | |
319 | "someday-maybe": "DeepSkyBlue", | |
320 | todo: "White", | |
321 | waiting: "MediumOrchid", | |
322 | }; | |
323 | var currentTagFilter: string | null = null; | |
324 | var currentViewState = "todo"; | |
325 | var taskFocusedBeforeJumpingToInput: HTMLElement | null = null; | |
326 | var lastTagNameEntered = ""; | |
327 | return { | |
328 | addTask: function (event: KeyboardEvent) { | |
329 | const input = <HTMLInputElement>document.getElementById("taskName"); | |
330 | if (input.value.match(/^ *$/)) return; | |
331 | const task = ui.addTask(input.value); | |
332 | if (currentViewState === "todo" || currentViewState === "all") { | |
333 | task instanceof HTMLElement && task.focus(); | |
334 | } else if (this.returnFocusAfterInput()) { | |
335 | } else { | |
336 | this.firstVisibleTask()?.focus(); | |
337 | } | |
338 | input.value = ""; | |
339 | if (event.getModifierState("Control")) { | |
340 | this.makeBottomPriority(task); | |
341 | } | |
342 | }, | |
343 | ||
344 | beginEdit: function (event: Event) { | |
345 | const task = this.currentTask(); | |
346 | if (!task) return; | |
347 | const input = document.createElement("input"); | |
348 | const desc = task.getElementsByClassName("desc")[0]; | |
349 | const oldDescription = desc.textContent!; | |
350 | task.setAttribute("data-description", oldDescription); | |
351 | input.value = oldDescription; | |
352 | input.addEventListener("blur", this.completeEdit, { once: true }); | |
353 | desc.textContent = ""; | |
354 | task.insertBefore(input, task.firstChild); | |
355 | input.focus(); | |
356 | event.preventDefault(); | |
357 | }, | |
358 | ||
359 | beginEditContent: function (event: Event) { | |
360 | const task = this.currentTask(); | |
361 | if (!task) return; | |
362 | const input = document.createElement("textarea"); | |
363 | const content = task.getElementsByClassName("content")[0]; | |
364 | const oldContent = content?.textContent ?? ""; | |
365 | task.setAttribute("data-content", oldContent); | |
366 | input.value = oldContent; | |
367 | input.addEventListener("blur", this.completeContentEdit, { once: true }); | |
368 | if (content) content.textContent = ""; | |
369 | task.appendChild(input); | |
370 | input.focus(); | |
371 | event.preventDefault(); | |
372 | }, | |
373 | ||
374 | beginTagEdit: function (event: Event) { | |
375 | const task = this.currentTask(); | |
376 | if (!task) return; | |
377 | const input = document.createElement("input"); | |
378 | input.classList.add("tag"); | |
379 | input.addEventListener("blur", this.completeTagEdit, { once: true }); | |
380 | input.value = lastTagNameEntered; | |
381 | task.appendChild(input); | |
382 | input.focus(); | |
383 | input.select(); | |
384 | event.preventDefault(); | |
385 | }, | |
386 | ||
387 | completeEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { | |
388 | const input = event.target as HTMLInputElement; | |
389 | const task = input.parentElement!; | |
390 | const desc = task.getElementsByClassName("desc")[0]; | |
391 | const oldDescription = task.getAttribute("data-description")!; | |
392 | const newDescription = input.value; | |
393 | input.removeEventListener("blur", this.completeEdit); | |
394 | task.removeChild(input); | |
395 | task.removeAttribute("data-description"); | |
396 | task.focus(); | |
397 | if (resolution === CommitOrAbort.Abort || newDescription.match(/^ *$/) || newDescription === oldDescription) { | |
398 | desc.textContent = oldDescription; | |
399 | } else { | |
400 | ui.edit(task.getAttribute("id")!, newDescription, oldDescription); | |
401 | } | |
402 | }, | |
403 | ||
404 | completeContentEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { | |
405 | const input = event.target as HTMLInputElement; | |
406 | const task = input.parentElement!; | |
407 | const content = task.getElementsByClassName("content")[0]; | |
408 | const oldContent = task.getAttribute("data-content")!; | |
409 | const newContent = input.value; | |
410 | input.removeEventListener("blur", this.completeContentEdit); | |
411 | task.removeChild(input); | |
412 | task.removeAttribute("data-content"); | |
413 | task.focus(); | |
414 | if (resolution === CommitOrAbort.Abort || newContent === oldContent) { | |
415 | if (content) content.textContent = oldContent; | |
416 | } else { | |
417 | ui.editContent(task.getAttribute("id")!, newContent, oldContent); | |
418 | } | |
419 | }, | |
420 | ||
421 | completeTagEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { | |
422 | const input = event.target as HTMLInputElement; | |
423 | const task = input.parentElement!; | |
424 | const newTagName = input.value; | |
425 | input.removeEventListener("blur", this.completeTagEdit); | |
426 | task.removeChild(input); | |
427 | task.focus(); | |
428 | if (resolution === CommitOrAbort.Commit && !newTagName.match(/^ *$/) && !model.hasTag(task, newTagName)) { | |
429 | ui.addTag(task.getAttribute("id")!, newTagName); | |
430 | lastTagNameEntered = newTagName; | |
431 | } | |
432 | }, | |
433 | ||
434 | currentTag: function (): Element | null { | |
435 | var target = document.activeElement; | |
436 | if (!target) return null; | |
437 | if (target.classList.contains("task")) { | |
438 | const tags = target.getElementsByClassName("tag"); | |
439 | target = tags[tags.length - 1]; | |
440 | } | |
441 | if (!target || !target.classList.contains("tag")) return null; | |
442 | return target; | |
443 | }, | |
444 | ||
445 | currentTask: function (): HTMLElement | null { | |
446 | var target = document.activeElement; | |
447 | if (!target) return null; | |
448 | if (target.classList.contains("tag")) target = target.parentElement!; | |
449 | if (!target.classList.contains("task")) return null; | |
450 | return target as HTMLElement; | |
451 | }, | |
452 | ||
453 | firstVisibleTask: function (root: Element | null = null) { | |
454 | if (root === null) root = document.body; | |
455 | for (const task of root.getElementsByClassName("task")) { | |
456 | const state = task.getAttribute("data-state"); | |
457 | if ( | |
458 | task instanceof HTMLElement && | |
459 | (state === currentViewState || (currentViewState === "all" && state !== "deleted")) && | |
460 | !task.classList.contains("hide") | |
461 | ) { | |
462 | return task; | |
463 | } | |
464 | } | |
465 | }, | |
466 | ||
467 | focusTaskNameInput: function (event: Event) { | |
468 | taskFocusedBeforeJumpingToInput = this.currentTask(); | |
469 | document.getElementById("taskName")!.focus(); | |
470 | window.scroll(0, 0); | |
471 | event.preventDefault(); | |
472 | }, | |
473 | ||
474 | visibleTaskAtOffset(task: Element, offset: number): Element { | |
475 | var cursor: Element | null = task; | |
476 | var valid_cursor = cursor; | |
477 | const increment = offset / Math.abs(offset); | |
478 | while (true) { | |
479 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
480 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
481 | const state = cursor.getAttribute("data-state")!; | |
482 | if ( | |
483 | (state === currentViewState || (currentViewState === "all" && state !== "deleted")) && | |
484 | !cursor.classList.contains("hide") | |
485 | ) { | |
486 | offset -= increment; | |
487 | valid_cursor = cursor; | |
488 | } | |
489 | if (Math.abs(offset) < 0.5) break; | |
490 | } | |
491 | return valid_cursor; | |
492 | }, | |
493 | ||
494 | jumpCursor: function (position: number) { | |
495 | const first = this.firstVisibleTask(); | |
496 | if (!first) return; | |
497 | const dest = this.visibleTaskAtOffset(first, position - 1); | |
498 | if (dest instanceof HTMLElement) dest.focus(); | |
499 | }, | |
500 | ||
501 | makeBottomPriority: function (task: Element | null = null) { | |
502 | if (!task) task = this.currentTask(); | |
503 | if (!task) return; | |
504 | this.setPriority(task, document.getElementById("tasks")!.lastElementChild, null); | |
505 | }, | |
506 | ||
507 | makeTopPriority: function (task: Element | null = null) { | |
508 | if (!task) task = this.currentTask(); | |
509 | if (!task) return; | |
510 | ui.setPriority(task.getAttribute("id")!, clock.now(), model.getPriority(task)); | |
511 | task instanceof HTMLElement && task.focus(); | |
512 | }, | |
513 | ||
514 | moveCursorLeft: function () { | |
515 | const active = this.currentTask(); | |
516 | if (!active) return false; | |
517 | if (active.parentElement!.classList.contains("task")) { | |
518 | active.parentElement!.focus(); | |
519 | } | |
520 | }, | |
521 | ||
522 | moveCursorRight: function () { | |
523 | const active = this.currentTask(); | |
524 | if (!active) return false; | |
525 | (this.firstVisibleTask(active) as HTMLElement | null)?.focus(); | |
526 | }, | |
527 | ||
528 | moveCursorVertically: function (offset: number): boolean { | |
529 | let active = this.currentTask(); | |
530 | if (!active) { | |
531 | this.firstVisibleTask()?.focus(); | |
532 | active = this.currentTask(); | |
533 | } | |
534 | if (!active) return false; | |
535 | const dest = this.visibleTaskAtOffset(active, offset); | |
536 | if (dest !== active && dest instanceof HTMLElement) { | |
537 | dest.focus(); | |
538 | return true; | |
539 | } | |
540 | return false; | |
541 | }, | |
542 | ||
543 | moveTask: function (offset: number) { | |
544 | const active = this.currentTask(); | |
545 | if (!active) return; | |
546 | const dest = this.visibleTaskAtOffset(active, offset); | |
547 | if (dest === active) return; // Already extremal | |
548 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
549 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
550 | if (offset > 0) { | |
551 | this.setPriority(active, dest, onePastDest); | |
552 | } else { | |
553 | this.setPriority(active, onePastDest, dest); | |
554 | } | |
555 | }, | |
556 | ||
557 | removeTag: function () { | |
558 | const target = this.currentTag(); | |
559 | if (!target) return; | |
560 | ui.removeTag(target.parentElement!.getAttribute("id")!, target.textContent!); | |
561 | }, | |
562 | ||
563 | resetTagView: function () { | |
564 | currentTagFilter = null; | |
565 | this.setTitle(); | |
566 | const taskList = document.getElementById("tasks")!; | |
567 | for (const task of Array.from(document.getElementsByClassName("task"))) { | |
568 | task.classList.remove("hide"); | |
569 | if (task.parentElement !== taskList) { | |
570 | model.insertInPriorityOrder(task, taskList); | |
571 | } | |
572 | } | |
573 | }, | |
574 | ||
575 | resetView: function () { | |
576 | this.setView("todo"); | |
577 | this.resetTagView(); | |
578 | }, | |
579 | ||
580 | returnFocusAfterInput: function (): boolean { | |
581 | if (taskFocusedBeforeJumpingToInput) { | |
582 | taskFocusedBeforeJumpingToInput.focus(); | |
583 | return true; | |
584 | } | |
585 | return false; | |
586 | }, | |
587 | ||
588 | // Change task's priority to be between other tasks a and b. | |
589 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
590 | const aPriority = a === null ? clock.now() : model.getPriority(a); | |
591 | const bPriority = b === null ? 0 : model.getPriority(b); | |
592 | console.assert(aPriority > bPriority, aPriority, ">", bPriority); | |
593 | const span = aPriority - bPriority; | |
594 | const newPriority = bPriority + 0.1 * span + 0.8 * span * Math.random(); | |
595 | console.assert(aPriority > newPriority && newPriority > bPriority, aPriority, ">", newPriority, ">", bPriority); | |
596 | const newPriorityRounded = Math.round(newPriority); | |
597 | const okToRound = aPriority > newPriorityRounded && newPriorityRounded > bPriority; | |
598 | ui.setPriority(task.getAttribute("id")!, okToRound ? newPriorityRounded : newPriority, model.getPriority(task)); | |
599 | task instanceof HTMLElement && task.focus(); | |
600 | }, | |
601 | ||
602 | setState: function (newState: string) { | |
603 | const task = this.currentTask(); | |
604 | if (!task) return; | |
605 | const oldState = task.getAttribute("data-state")!; | |
606 | if (newState === oldState) return; | |
607 | const createTimestamp = task.getAttribute("id")!; | |
608 | if (currentViewState !== "all" || newState == "deleted") { | |
609 | this.moveCursorVertically(1) || this.moveCursorVertically(-1); | |
610 | } | |
611 | return ui.setState(createTimestamp, newState, oldState); | |
612 | }, | |
613 | ||
614 | setTagFilter: function (tag: string) { | |
615 | if (currentTagFilter !== null) { | |
616 | this.resetTagView(); | |
617 | } | |
618 | ||
619 | const tasksWithTag = new Map(); | |
620 | for (const task of document.getElementsByClassName("task")) { | |
621 | if (model.hasTag(task, tag)) { | |
622 | tasksWithTag.set(task.getElementsByClassName("desc")[0].textContent, [model.getPriority(task), task]); | |
623 | } | |
624 | } | |
625 | ||
626 | function highestPrioritySuperTask(t: Element) { | |
627 | var maxPriority = -1; | |
628 | var superTask = null; | |
629 | for (const child of t.getElementsByClassName("tag")) { | |
630 | const e = tasksWithTag.get(child.textContent); | |
631 | if (e !== undefined && e[0] > maxPriority) { | |
632 | maxPriority = e[0]; | |
633 | superTask = e[1]; | |
634 | } | |
635 | } | |
636 | return superTask; | |
637 | } | |
638 | ||
639 | for (const task of Array.from(document.getElementsByClassName("task"))) { | |
640 | if (model.hasTag(task, tag)) { | |
641 | task.classList.remove("hide"); | |
642 | } else { | |
643 | const superTask = highestPrioritySuperTask(task); | |
644 | if (superTask !== null) { | |
645 | model.insertInPriorityOrder(task, superTask); | |
646 | } else { | |
647 | task.classList.add("hide"); | |
648 | } | |
649 | } | |
650 | } | |
651 | ||
652 | currentTagFilter = tag; | |
653 | this.setTitle(); | |
654 | }, | |
655 | ||
656 | setTagView: function (tag: string | null = null) { | |
657 | if (tag === null) { | |
658 | const target = this.currentTag(); | |
659 | if (!target) return; | |
660 | tag = target.textContent!; | |
661 | } | |
662 | this.setTagFilter(tag); | |
663 | }, | |
664 | ||
665 | setTitle: function () { | |
666 | document.title = "Vopamoi: " + currentViewState + (currentTagFilter ? ": " + currentTagFilter : ""); | |
667 | }, | |
668 | ||
669 | setView: function (state: string) { | |
670 | const sheet = (document.getElementById("viewStyle") as HTMLStyleElement).sheet!; | |
671 | if (state === "all") { | |
672 | sheet.insertRule(`.task[data-state=deleted] { display: none }`); | |
673 | } else { | |
674 | sheet.insertRule(`.task:not([data-state=${state}]) { display: none }`); | |
675 | } | |
676 | sheet.insertRule(`:root { --view-state-indicator-color: ${viewColors[state]}; }`); | |
677 | sheet.removeRule(2); | |
678 | sheet.removeRule(2); | |
679 | currentViewState = state; | |
680 | this.setTitle(); | |
681 | if (this.currentTask()?.getAttribute("data-state") !== state) { | |
682 | this.firstVisibleTask()?.focus(); | |
683 | } | |
684 | }, | |
685 | ||
686 | setUntaggedView: function () { | |
687 | if (currentTagFilter !== null) { | |
688 | this.resetTagView(); | |
689 | } | |
690 | for (const task of document.getElementsByClassName("task")) { | |
691 | if (task.getElementsByClassName("tag").length === 0) { | |
692 | task.classList.remove("hide"); | |
693 | } else { | |
694 | task.classList.add("hide"); | |
695 | } | |
696 | } | |
697 | }, | |
698 | ||
699 | undo: function () { | |
700 | const ret = ui.undo(); | |
701 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
702 | }, | |
703 | redo: function () { | |
704 | const ret = ui.redo(); | |
705 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
706 | }, | |
707 | }; | |
708 | } | |
709 | const browserUI = BrowserUI(); | |
710 | ||
711 | const scrollIncrement = 60; | |
712 | enum InputState { | |
713 | Root, | |
714 | S, | |
715 | V, | |
716 | VS, | |
717 | } | |
718 | var inputState = InputState.Root; | |
719 | var inputCount: number | null = null; | |
720 | ||
721 | function handleKey(event: any) { | |
722 | if (["Alt", "Control", "Meta", "Shift"].includes(event.key)) return; | |
723 | if (event.target.tagName === "TEXTAREA") { | |
724 | if (event.key == "Enter" && event.ctrlKey) return browserUI.completeContentEdit(event); | |
725 | if (event.key == "Escape") return browserUI.completeContentEdit(event, CommitOrAbort.Abort); | |
726 | } else if (event.target.tagName === "INPUT") { | |
727 | if (event.target.id === "taskName") { | |
728 | if (event.key == "Enter") return browserUI.addTask(event); | |
729 | if (event.key == "Escape") return browserUI.returnFocusAfterInput(); | |
730 | } else if (event.target.classList.contains("tag")) { | |
731 | if (event.key == "Enter") return browserUI.completeTagEdit(event); | |
732 | if (event.key == "Escape") return browserUI.completeTagEdit(event, CommitOrAbort.Abort); | |
733 | } else { | |
734 | if (event.key == "Enter") return browserUI.completeEdit(event); | |
735 | if (event.key == "Escape") return browserUI.completeEdit(event, CommitOrAbort.Abort); | |
736 | } | |
737 | } else { | |
738 | if (inputState === InputState.Root) { | |
739 | if ("0" <= event.key && event.key <= "9") { | |
740 | return (inputCount = (inputCount ?? 0) * 10 + parseInt(event.key)); | |
741 | } | |
742 | try { | |
743 | if (event.ctrlKey) { | |
744 | if (event.key == "e") return window.scrollBy(0, (inputCount ?? 1) * scrollIncrement); | |
745 | if (event.key == "y") return window.scrollBy(0, (inputCount ?? 1) * -scrollIncrement); | |
746 | } else { | |
747 | if (event.key == "h") return browserUI.moveCursorLeft(); | |
748 | if (event.key == "l") return browserUI.moveCursorRight(); | |
749 | if (event.key == "j") return browserUI.moveCursorVertically(inputCount ?? 1); | |
750 | if (event.key == "k") return browserUI.moveCursorVertically(-(inputCount ?? 1)); | |
751 | if (event.key == "J") return browserUI.moveTask(inputCount ?? 1); | |
752 | if (event.key == "K") return browserUI.moveTask(-(inputCount ?? 1)); | |
753 | if (event.key == "G") return browserUI.jumpCursor(inputCount ?? MAX_SAFE_INTEGER); | |
754 | if (event.key == "T") return browserUI.makeTopPriority(); | |
755 | if (event.key == "n") return browserUI.focusTaskNameInput(event); | |
756 | if (event.key == "c") return browserUI.setState("cancelled"); | |
757 | if (event.key == "d") return browserUI.setState("done"); | |
758 | if (event.key == "q") return browserUI.setState("todo"); | |
759 | if (event.key == "s") return (inputState = InputState.S); | |
760 | if (event.key == "w") return browserUI.setState("waiting"); | |
761 | if (event.key == "X") return browserUI.setState("deleted"); | |
762 | if (event.key == "x") return browserUI.removeTag(); | |
763 | if (event.key == "u") return browserUI.undo(); | |
764 | if (event.key == "r") return browserUI.redo(); | |
765 | if (event.key == "E") return browserUI.beginEditContent(event); | |
766 | if (event.key == "e") return browserUI.beginEdit(event); | |
767 | if (event.key == "t") return browserUI.beginTagEdit(event); | |
768 | if (event.key == "v") return (inputState = InputState.V); | |
769 | } | |
770 | } finally { | |
771 | inputCount = null; | |
772 | } | |
773 | } else if (inputState === InputState.S) { | |
774 | inputState = InputState.Root; | |
775 | if (event.key == "m") return browserUI.setState("someday-maybe"); | |
776 | } else if (inputState === InputState.V) { | |
777 | inputState = InputState.Root; | |
778 | if (event.key == "a") return browserUI.setView("all"); | |
779 | if (event.key == "c") return browserUI.setView("cancelled"); | |
780 | if (event.key == "d") return browserUI.setView("done"); | |
781 | if (event.key == "i") return browserUI.setUntaggedView(); | |
782 | if (event.key == "p") return browserUI.setTagView("Project"); | |
783 | if (event.key == "q") return browserUI.setView("todo"); | |
784 | if (event.key == "s") return (inputState = InputState.VS); | |
785 | if (event.key == "T") return browserUI.resetTagView(); | |
786 | if (event.key == "t") return browserUI.setTagView(); | |
787 | if (event.key == "u") return browserUI.setUntaggedView(); | |
788 | if (event.key == "v") return browserUI.resetView(); | |
789 | if (event.key == "w") return browserUI.setView("waiting"); | |
790 | if (event.key == "x") return browserUI.setView("deleted"); | |
791 | } else if (inputState === InputState.VS) { | |
792 | inputState = InputState.Root; | |
793 | if (event.key == "m") return browserUI.setView("someday-maybe"); | |
794 | } | |
795 | } | |
796 | } | |
797 | ||
798 | function browserInit() { | |
799 | log.replay(); | |
800 | browserUI.setTitle(); | |
801 | browserUI.firstVisibleTask()?.focus(); | |
802 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
803 | } |