]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
f550fa1cbbbd1ea9fa309a60d0d5e549dd5aacf0
[vopamoi] / vopamoi.ts
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 document.getElementById("tasks")!.style.display = "none";
211 while (true) {
212 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
213 if (entry === null) {
214 break;
215 }
216 this.apply(entry);
217 next_log_index++;
218 }
219 document.getElementById("tasks")!.style.display = "";
220 },
221 };
222 }
223 const log = Log();
224
225 function UI() {
226 const undoLog: string[][] = [];
227 const redoLog: string[][] = [];
228 function perform(forward: string, reverse: string) {
229 undoLog.push([reverse, forward]);
230 return log.recordAndApply(`${clock.now()} ${forward}`);
231 }
232 return {
233 addTask: function (description: string): Element {
234 const now = clock.now();
235 undoLog.push([`State ${now} deleted`, `State ${now} todo`]);
236 return <Element>log.recordAndApply(`${now} Create ${description}`);
237 },
238 addTag: function (createTimestamp: string, tag: string) {
239 return perform(`Tag ${createTimestamp} ${tag}`, `Untag ${createTimestamp} ${tag}`);
240 },
241 edit: function (createTimestamp: string, newDescription: string, oldDescription: string) {
242 return perform(`Edit ${createTimestamp} ${newDescription}`, `Edit ${createTimestamp} ${oldDescription}`);
243 },
244 removeTag: function (createTimestamp: string, tag: string) {
245 return perform(`Untag ${createTimestamp} ${tag}`, `Tag ${createTimestamp} ${tag}`);
246 },
247 setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
248 return perform(`Priority ${createTimestamp} ${newPriority}`, `Priority ${createTimestamp} ${oldPriority}`);
249 },
250 setState: function (createTimestamp: string, newState: string, oldState: string) {
251 return perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`);
252 },
253 undo: function () {
254 const entry = undoLog.pop();
255 if (entry) {
256 redoLog.push(entry);
257 return log.recordAndApply(`${clock.now()} ${entry[0]}`);
258 }
259 },
260 redo: function () {
261 const entry = redoLog.pop();
262 if (entry) {
263 undoLog.push(entry);
264 return log.recordAndApply(`${clock.now()} ${entry[1]}`);
265 }
266 },
267 };
268 }
269 const ui = UI();
270
271 enum CommitOrAbort {
272 Commit,
273 Abort,
274 }
275
276 function BrowserUI() {
277 const viewColors: { [key: string]: string } = {
278 all: "Gold",
279 cancelled: "Red",
280 deleted: "Black",
281 done: "LawnGreen",
282 "someday-maybe": "DeepSkyBlue",
283 todo: "White",
284 waiting: "MediumOrchid",
285 };
286 var currentTagView: string | null = null;
287 var currentViewState = "todo";
288 var taskFocusedBeforeJumpingToInput: HTMLElement | null = null;
289 var lastTagNameEntered = "";
290 return {
291 addTask: function (event: KeyboardEvent) {
292 const input = <HTMLInputElement>document.getElementById("taskName");
293 if (input.value.match(/^ *$/)) return;
294 const task = ui.addTask(input.value);
295 if (currentViewState === "todo" || currentViewState === "all") {
296 task instanceof HTMLElement && task.focus();
297 } else if (this.returnFocusAfterInput()) {
298 } else {
299 this.firstVisibleTask()?.focus();
300 }
301 input.value = "";
302 if (event.getModifierState("Control")) {
303 this.makeBottomPriority(task);
304 }
305 },
306
307 beginEdit: function (event: Event) {
308 const task = this.currentTask();
309 if (!task) return;
310 const input = document.createElement("input");
311 const desc = task.getElementsByClassName("desc")[0];
312 const oldDescription = desc.textContent!;
313 task.setAttribute("data-description", oldDescription);
314 input.value = oldDescription;
315 input.addEventListener("blur", this.completeEdit, { once: true });
316 desc.textContent = "";
317 task.insertBefore(input, task.firstChild);
318 input.focus();
319 event.preventDefault();
320 },
321
322 beginTagEdit: function (event: Event) {
323 const task = this.currentTask();
324 if (!task) return;
325 const input = document.createElement("input");
326 input.classList.add("tag");
327 input.addEventListener("blur", this.completeTagEdit, { once: true });
328 input.value = lastTagNameEntered;
329 task.appendChild(input);
330 input.focus();
331 input.select();
332 event.preventDefault();
333 },
334
335 completeEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) {
336 const input = event.target as HTMLInputElement;
337 const task = input.parentElement!;
338 const desc = task.getElementsByClassName("desc")[0];
339 const oldDescription = task.getAttribute("data-description")!;
340 const newDescription = input.value;
341 input.removeEventListener("blur", this.completeEdit);
342 task.removeChild(input);
343 task.removeAttribute("data-description");
344 task.focus();
345 if (resolution === CommitOrAbort.Abort || newDescription.match(/^ *$/) || newDescription === oldDescription) {
346 desc.textContent = oldDescription;
347 } else {
348 ui.edit(task.getAttribute("data-created")!, newDescription, oldDescription);
349 }
350 },
351
352 completeTagEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) {
353 const input = event.target as HTMLInputElement;
354 const task = input.parentElement!;
355 const newTagName = input.value;
356 input.removeEventListener("blur", this.completeTagEdit);
357 task.removeChild(input);
358 task.focus();
359 if (resolution === CommitOrAbort.Commit && !newTagName.match(/^ *$/) && !Model.hasTag(task, newTagName)) {
360 ui.addTag(task.getAttribute("data-created")!, newTagName);
361 lastTagNameEntered = newTagName;
362 }
363 },
364
365 currentTag: function (): Element | null {
366 var target = document.activeElement;
367 if (!target) return null;
368 if (target.classList.contains("task")) {
369 const tags = target.getElementsByClassName("tag");
370 target = tags[tags.length - 1];
371 }
372 if (!target || !target.classList.contains("tag")) return null;
373 return target;
374 },
375
376 currentTask: function (): HTMLElement | null {
377 var target = document.activeElement;
378 if (!target) return null;
379 if (target.classList.contains("tag")) target = target.parentElement!;
380 return target as HTMLElement;
381 },
382
383 firstVisibleTask: function (root: Element | null = null) {
384 if (root === null) root = document.body;
385 for (const task of root.getElementsByClassName("task")) {
386 const state = task.getAttribute("data-state");
387 if (
388 task instanceof HTMLElement &&
389 (state === currentViewState || (currentViewState === "all" && state !== "deleted")) &&
390 !task.classList.contains("hide")
391 ) {
392 return task;
393 }
394 }
395 },
396
397 focusTaskNameInput: function (event: Event) {
398 taskFocusedBeforeJumpingToInput = this.currentTask();
399 document.getElementById("taskName")!.focus();
400 window.scroll(0, 0);
401 event.preventDefault();
402 },
403
404 visibleTaskAtOffset(task: Element, offset: number): Element {
405 var cursor: Element | null = task;
406 var valid_cursor = cursor;
407 const increment = offset / Math.abs(offset);
408 while (true) {
409 cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
410 if (!cursor || !(cursor instanceof HTMLElement)) break;
411 const state = cursor.getAttribute("data-state")!;
412 if (
413 (state === currentViewState || (currentViewState === "all" && state !== "deleted")) &&
414 !cursor.classList.contains("hide")
415 ) {
416 offset -= increment;
417 valid_cursor = cursor;
418 }
419 if (Math.abs(offset) < 0.5) break;
420 }
421 return valid_cursor;
422 },
423
424 jumpCursor: function (position: number) {
425 const first = this.firstVisibleTask();
426 if (!first) return;
427 const dest = this.visibleTaskAtOffset(first, position - 1);
428 if (dest instanceof HTMLElement) dest.focus();
429 },
430
431 makeBottomPriority: function (task: Element | null = null) {
432 if (!task) task = this.currentTask();
433 if (!task) return;
434 this.setPriority(task, document.getElementById("tasks")!.lastElementChild, null);
435 },
436
437 makeTopPriority: function (task: Element | null = null) {
438 if (!task) task = this.currentTask();
439 if (!task) return;
440 ui.setPriority(task.getAttribute("data-created")!, clock.now(), Model.getPriority(task));
441 task instanceof HTMLElement && task.focus();
442 },
443
444 moveCursorLeft: function () {
445 const active = this.currentTask();
446 if (!active) return false;
447 if (active.parentElement!.classList.contains("task")) {
448 active.parentElement!.focus();
449 }
450 },
451
452 moveCursorRight: function () {
453 const active = this.currentTask();
454 if (!active) return false;
455 (this.firstVisibleTask(active) as HTMLElement | null)?.focus();
456 },
457
458 moveCursorVertically: function (offset: number): boolean {
459 const active = this.currentTask();
460 if (!active) return false;
461 const dest = this.visibleTaskAtOffset(active, offset);
462 if (dest !== active && dest instanceof HTMLElement) {
463 dest.focus();
464 return true;
465 }
466 return false;
467 },
468
469 moveTask: function (offset: number) {
470 const active = this.currentTask();
471 if (!active) return;
472 const dest = this.visibleTaskAtOffset(active, offset);
473 if (dest === active) return; // Already extremal
474 var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
475 if (onePastDest == dest) onePastDest = null; // Will become extremal
476 if (offset > 0) {
477 this.setPriority(active, dest, onePastDest);
478 } else {
479 this.setPriority(active, onePastDest, dest);
480 }
481 },
482
483 removeTag: function () {
484 const target = this.currentTag();
485 if (!target) return;
486 ui.removeTag(target.parentElement!.getAttribute("data-created")!, target.textContent!);
487 },
488
489 resetTagView: function () {
490 currentTagView = null;
491 const taskList = document.getElementById("tasks")!;
492 for (const task of Array.from(document.getElementsByClassName("task"))) {
493 task.classList.remove("hide");
494 if (task.parentElement !== taskList) {
495 Model.insertInPriorityOrder(task, taskList);
496 }
497 }
498 },
499
500 resetView: function () {
501 this.setView("todo");
502 this.resetTagView();
503 },
504
505 returnFocusAfterInput: function (): boolean {
506 if (taskFocusedBeforeJumpingToInput) {
507 taskFocusedBeforeJumpingToInput.focus();
508 return true;
509 }
510 return false;
511 },
512
513 // Change task's priority to be between other tasks a and b.
514 setPriority: function (task: Element, a: Element | null, b: Element | null) {
515 const aPriority = a === null ? clock.now() : Model.getPriority(a);
516 const bPriority = b === null ? 0 : Model.getPriority(b);
517 console.assert(aPriority > bPriority, aPriority, ">", bPriority);
518 const span = aPriority - bPriority;
519 const newPriority = bPriority + 0.1 * span + 0.8 * span * Math.random();
520 console.assert(aPriority > newPriority && newPriority > bPriority, aPriority, ">", newPriority, ">", bPriority);
521 const newPriorityRounded = Math.round(newPriority);
522 const okToRound = aPriority > newPriorityRounded && newPriorityRounded > bPriority;
523 ui.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task));
524 task instanceof HTMLElement && task.focus();
525 },
526
527 setState: function (newState: string) {
528 const task = this.currentTask();
529 if (!task) return;
530 const oldState = task.getAttribute("data-state")!;
531 if (newState === oldState) return;
532 const createTimestamp = task.getAttribute("data-created")!;
533 if (currentViewState !== "all" || newState == "deleted") {
534 this.moveCursorVertically(1) || this.moveCursorVertically(-1);
535 }
536 return ui.setState(createTimestamp, newState, oldState);
537 },
538
539 setTagView: function (tag: string | null = null) {
540 if (tag === null) {
541 const target = this.currentTag();
542 if (!target) return;
543 tag = target.textContent!;
544 }
545
546 if (currentTagView !== null) {
547 this.resetTagView();
548 }
549
550 const tasksWithTag = new Map();
551 for (const task of document.getElementsByClassName("task")) {
552 if (Model.hasTag(task, tag)) {
553 tasksWithTag.set(task.getElementsByClassName("desc")[0].textContent, [Model.getPriority(task), task]);
554 }
555 }
556
557 function highestPrioritySuperTask(t: Element) {
558 var maxPriority = -1;
559 var superTask = null;
560 for (const child of t.getElementsByClassName("tag")) {
561 const e = tasksWithTag.get(child.textContent);
562 if (e !== undefined && e[0] > maxPriority) {
563 maxPriority = e[0];
564 superTask = e[1];
565 }
566 }
567 return superTask;
568 }
569
570 for (const task of Array.from(document.getElementsByClassName("task"))) {
571 if (Model.hasTag(task, tag)) {
572 task.classList.remove("hide");
573 } else {
574 const superTask = highestPrioritySuperTask(task);
575 if (superTask !== null) {
576 Model.insertInPriorityOrder(task, superTask);
577 } else {
578 task.classList.add("hide");
579 }
580 }
581 }
582
583 currentTagView = tag;
584 },
585
586 setView: function (state: string) {
587 const sheet = (document.getElementById("viewStyle") as HTMLStyleElement).sheet!;
588 if (state === "all") {
589 sheet.insertRule(`.task[data-state=deleted] { display: none }`);
590 } else {
591 sheet.insertRule(`.task:not([data-state=${state}]) { display: none }`);
592 }
593 sheet.insertRule(`:root { --view-state-indicator-color: ${viewColors[state]}; }`);
594 sheet.removeRule(2);
595 sheet.removeRule(2);
596 currentViewState = state;
597 if (this.currentTask()?.getAttribute("data-state") !== state) {
598 this.firstVisibleTask()?.focus();
599 }
600 },
601
602 setUntaggedView: function () {
603 if (currentTagView !== null) {
604 this.resetTagView();
605 }
606 for (const task of document.getElementsByClassName("task")) {
607 if (task.getElementsByClassName("tag").length === 0) {
608 task.classList.remove("hide");
609 } else {
610 task.classList.add("hide");
611 }
612 }
613 },
614
615 undo: function () {
616 const ret = ui.undo();
617 if (ret && ret instanceof HTMLElement) ret.focus();
618 },
619 redo: function () {
620 const ret = ui.redo();
621 if (ret && ret instanceof HTMLElement) ret.focus();
622 },
623 };
624 }
625 const browserUI = BrowserUI();
626
627 const scrollIncrement = 60;
628 enum InputState {
629 Root,
630 S,
631 V,
632 VS,
633 }
634 var inputState = InputState.Root;
635 var inputCount: number | null = null;
636
637 function handleKey(event: any) {
638 if (["Alt", "Control", "Meta", "Shift"].includes(event.key)) return;
639 if (event.target.tagName === "INPUT") {
640 if (event.target.id === "taskName") {
641 if (event.key == "Enter") return browserUI.addTask(event);
642 if (event.key == "Escape") return browserUI.returnFocusAfterInput();
643 } else if (event.target.classList.contains("tag")) {
644 if (event.key == "Enter") return browserUI.completeTagEdit(event);
645 if (event.key == "Escape") return browserUI.completeTagEdit(event, CommitOrAbort.Abort);
646 } else {
647 if (event.key == "Enter") return browserUI.completeEdit(event);
648 if (event.key == "Escape") return browserUI.completeEdit(event, CommitOrAbort.Abort);
649 }
650 } else {
651 if (inputState === InputState.Root) {
652 if ("0" <= event.key && event.key <= "9") {
653 return (inputCount = (inputCount ?? 0) * 10 + parseInt(event.key));
654 }
655 try {
656 if (event.ctrlKey) {
657 if (event.key == "e") return window.scrollBy(0, (inputCount ?? 1) * scrollIncrement);
658 if (event.key == "y") return window.scrollBy(0, (inputCount ?? 1) * -scrollIncrement);
659 } else {
660 if (event.key == "h") return browserUI.moveCursorLeft();
661 if (event.key == "l") return browserUI.moveCursorRight();
662 if (event.key == "j") return browserUI.moveCursorVertically(inputCount ?? 1);
663 if (event.key == "k") return browserUI.moveCursorVertically(-(inputCount ?? 1));
664 if (event.key == "J") return browserUI.moveTask(inputCount ?? 1);
665 if (event.key == "K") return browserUI.moveTask(-(inputCount ?? 1));
666 if (event.key == "G") return browserUI.jumpCursor(inputCount ?? MAX_SAFE_INTEGER);
667 if (event.key == "T") return browserUI.makeTopPriority();
668 if (event.key == "n") return browserUI.focusTaskNameInput(event);
669 if (event.key == "c") return browserUI.setState("cancelled");
670 if (event.key == "d") return browserUI.setState("done");
671 if (event.key == "q") return browserUI.setState("todo");
672 if (event.key == "s") return (inputState = InputState.S);
673 if (event.key == "w") return browserUI.setState("waiting");
674 if (event.key == "X") return browserUI.setState("deleted");
675 if (event.key == "x") return browserUI.removeTag();
676 if (event.key == "u") return browserUI.undo();
677 if (event.key == "r") return browserUI.redo();
678 if (event.key == "e") return browserUI.beginEdit(event);
679 if (event.key == "t") return browserUI.beginTagEdit(event);
680 if (event.key == "v") return (inputState = InputState.V);
681 }
682 } finally {
683 inputCount = null;
684 }
685 } else if (inputState === InputState.S) {
686 inputState = InputState.Root;
687 if (event.key == "m") return browserUI.setState("someday-maybe");
688 } else if (inputState === InputState.V) {
689 inputState = InputState.Root;
690 if (event.key == "a") return browserUI.setView("all");
691 if (event.key == "c") return browserUI.setView("cancelled");
692 if (event.key == "d") return browserUI.setView("done");
693 if (event.key == "i") return browserUI.setUntaggedView();
694 if (event.key == "p") return browserUI.setTagView("Project");
695 if (event.key == "q") return browserUI.setView("todo");
696 if (event.key == "s") return (inputState = InputState.VS);
697 if (event.key == "T") return browserUI.resetTagView();
698 if (event.key == "t") return browserUI.setTagView();
699 if (event.key == "u") return browserUI.setUntaggedView();
700 if (event.key == "v") return browserUI.resetView();
701 if (event.key == "w") return browserUI.setView("waiting");
702 if (event.key == "x") return browserUI.setView("deleted");
703 } else if (inputState === InputState.VS) {
704 inputState = InputState.Root;
705 if (event.key == "m") return browserUI.setView("someday-maybe");
706 }
707 }
708 }
709
710 function browserInit() {
711 log.replay();
712 browserUI.firstVisibleTask()?.focus();
713 document.body.addEventListener("keydown", handleKey, { capture: false });
714 }