]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
f127da9102c36e1ceb4b2497a71cd0cbc5b58418
[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 const Model = {
17 createTask: function (timestamp: string, description: string) {
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
20 task.setAttribute("tabindex", "0");
21 task.setAttribute("data-created", timestamp);
22 return task;
23 },
24
25 addTask: function (timestamp: string, description: string) {
26 document.body.appendChild(this.createTask(timestamp, description)).focus();
27 },
28
29 moveCursor: function (offset: number): boolean {
30 var active = document.activeElement;
31 if (offset === 1 && active) {
32 active = active.nextElementSibling;
33 }
34 if (offset === -1 && active) {
35 active = active.previousElementSibling;
36 }
37 if (active && active instanceof HTMLElement) {
38 active.focus();
39 return true;
40 }
41 return false;
42 },
43 };
44
45 const Log = (function () {
46 var next_log_index = 0;
47 return {
48 apply: function (entry: string) {
49 const [timestamp, command, data] = splitN(entry, " ", 2);
50 if (command == "Create") {
51 Model.addTask(timestamp, data);
52 }
53 },
54
55 record: function (entry: string) {
56 window.localStorage.setItem(`${next_log_index++}`, entry);
57 },
58
59 recordAndApply: function (entry: string) {
60 this.record(entry);
61 this.apply(entry);
62 },
63
64 replay: function () {
65 while (true) {
66 const entry = window.localStorage.getItem(`${next_log_index}`);
67 if (entry === null) {
68 break;
69 }
70 this.apply(entry);
71 next_log_index++;
72 }
73 },
74 };
75 })();
76
77 const UI = {
78 addTask: function (description: string) {
79 Log.recordAndApply(`${Date.now()} Create ${description}`);
80 },
81 };
82
83 const BrowserUI = {
84 addTask: function (form: any) {
85 if (form.taskName.value) {
86 UI.addTask(form.taskName.value);
87 form.taskName.value = "";
88 }
89 return false;
90 },
91 };
92
93 function focusTaskNameInput(event: any) {
94 document.getElementById("taskName")!.focus();
95 event.preventDefault();
96 }
97
98 function handleKey(event: any) {
99 if (event.target.tagName !== "INPUT") {
100 if (event.key == "j") return Model.moveCursor(1);
101 if (event.key == "k") return Model.moveCursor(-1);
102 if (event.key == "c") return focusTaskNameInput(event);
103 }
104 }
105
106 function browserInit() {
107 document.body.addEventListener("keydown", handleKey, { capture: false });
108 Log.replay();
109 }