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