]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
5e645b8f0fb4a849d943b0abbde9537b97475b6d
[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 addTask: function (timestamp: string, description: string): Element {
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
20 task.setAttribute("class", "task");
21 task.setAttribute("tabindex", "0");
22 task.setAttribute("data-created", timestamp);
23 document.getElementById("tasks")!.appendChild(task);
24 task.focus();
25 return task;
26 },
27
28 getPriority: function (task: Element): number {
29 if (task.hasAttribute("data-priority")) {
30 return parseFloat(task.getAttribute("data-priority")!);
31 }
32 return parseFloat(task.getAttribute("data-created")!);
33 },
34
35 getTask: function (createTimestamp: string) {
36 for (const task of document.getElementsByClassName("task")) {
37 if (task.getAttribute("data-created") === createTimestamp) {
38 return task;
39 }
40 }
41 },
42
43 setPriority: function (createTimestamp: string, priority: number) {
44 const target = this.getTask(createTimestamp);
45 if (!target) return;
46 target.setAttribute("data-priority", `${priority}`);
47 for (const task of document.getElementsByClassName("task")) {
48 if (task !== target && this.getPriority(task) > priority) {
49 task.parentElement!.insertBefore(target, task);
50 target instanceof HTMLElement && target.focus();
51 return;
52 }
53 }
54 document.getElementById("tasks")!.appendChild(target);
55 target instanceof HTMLElement && target.focus();
56 },
57
58 setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
59 const task = this.getTask(createTimestamp);
60 if (task) {
61 task.setAttribute(`data-${state}`, stateTimestamp);
62 if (task instanceof HTMLElement) {
63 task.style.display = "none"; // Until view filtering
64 }
65 }
66 },
67 };
68
69 function Log(prefix: string = "vp-") {
70 var next_log_index = 0;
71 return {
72 apply: function (entry: string) {
73 const [timestamp, command, data] = splitN(entry, " ", 2);
74 if (command == "Create") {
75 return Model.addTask(timestamp, data);
76 }
77 if (command == "State") {
78 const [createTimestamp, state] = splitN(data, " ", 1);
79 return Model.setState(timestamp, createTimestamp, state);
80 }
81 if (command == "Priority") {
82 const [createTimestamp, newPriority] = splitN(data, " ", 1);
83 return Model.setPriority(createTimestamp, parseFloat(newPriority));
84 }
85 },
86
87 record: function (entry: string) {
88 window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
89 },
90
91 recordAndApply: function (entry: string) {
92 this.record(entry);
93 return this.apply(entry);
94 },
95
96 replay: function () {
97 while (true) {
98 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
99 if (entry === null) {
100 break;
101 }
102 this.apply(entry);
103 next_log_index++;
104 }
105 },
106 };
107 }
108 const log = Log();
109
110 const UI = {
111 addTask: function (description: string): Element {
112 return <Element>log.recordAndApply(`${Date.now()} Create ${description}`);
113 },
114 setPriority: function (createTimestamp: string, priority: number) {
115 return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${priority}`);
116 },
117 setState: function (createTimestamp: string, state: string) {
118 return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`);
119 },
120 };
121
122 const BrowserUI = {
123 addTask: function (event: KeyboardEvent) {
124 const input = <HTMLInputElement>document.getElementById("taskName");
125 if (input.value) {
126 const task = UI.addTask(input.value);
127 input.value = "";
128 if (event.getModifierState("Control")) {
129 this.setPriority(task, null, document.getElementsByClassName("task")[0]);
130 }
131 }
132 },
133
134 firstVisibleTask: function () {
135 for (const task of document.getElementsByClassName("task")) {
136 if (task instanceof HTMLElement && task.style.display !== "none") {
137 return task;
138 }
139 }
140 },
141
142 focusTaskNameInput: function (event: Event) {
143 document.getElementById("taskName")!.focus();
144 event.preventDefault();
145 },
146
147 visibleTaskAtOffset(task: Element, offset: number): Element {
148 var cursor: Element | null = task;
149 var valid_cursor = cursor;
150 const increment = offset / Math.abs(offset);
151 while (true) {
152 cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
153 if (!cursor || !(cursor instanceof HTMLElement)) break;
154 if (cursor.style.display !== "none") {
155 offset -= increment;
156 valid_cursor = cursor;
157 }
158 if (Math.abs(offset) < 0.5) break;
159 }
160 return valid_cursor;
161 },
162
163 moveCursor: function (offset: number): boolean {
164 const active = document.activeElement;
165 if (!active) return false;
166 const dest = this.visibleTaskAtOffset(active, offset);
167 if (dest !== active && dest instanceof HTMLElement) {
168 dest.focus();
169 return true;
170 }
171 return false;
172 },
173
174 moveTask: function (offset: number) {
175 const active = document.activeElement;
176 if (!active) return;
177 const dest = this.visibleTaskAtOffset(active, offset);
178 if (dest === active) return; // Already extremal
179 var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
180 if (onePastDest == dest) onePastDest = null; // Will become extremal
181 if (offset > 0) {
182 this.setPriority(active, dest, onePastDest);
183 } else {
184 this.setPriority(active, onePastDest, dest);
185 }
186 },
187
188 // Change task's priority to be between other tasks a and b.
189 setPriority: function (task: Element, a: Element | null, b: Element | null) {
190 const aPriority = a === null ? 0 : Model.getPriority(a);
191 const bPriority = b === null ? Date.now() : Model.getPriority(b);
192 console.assert(aPriority < bPriority, aPriority, "<", bPriority);
193 const span = bPriority - aPriority;
194 const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random();
195 console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority);
196 const newPriorityRounded = Math.round(newPriority);
197 const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority;
198 UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority);
199 },
200
201 setState: function (state: string) {
202 const createTimestamp = document.activeElement?.getAttribute("data-created");
203 this.moveCursor(1) || this.moveCursor(-1);
204 return UI.setState(createTimestamp!, state);
205 },
206 };
207
208 function handleKey(event: any) {
209 if (event.target.tagName === "INPUT") {
210 if (event.key == "Enter") return BrowserUI.addTask(event);
211 } else {
212 if (event.key == "j") return BrowserUI.moveCursor(1);
213 if (event.key == "k") return BrowserUI.moveCursor(-1);
214 if (event.key == "J") return BrowserUI.moveTask(1);
215 if (event.key == "K") return BrowserUI.moveTask(-1);
216 if (event.key == "n") return BrowserUI.focusTaskNameInput(event);
217 if (event.key == "s") return BrowserUI.setState("someday-maybe");
218 if (event.key == "w") return BrowserUI.setState("waiting");
219 if (event.key == "d") return BrowserUI.setState("done");
220 if (event.key == "c") return BrowserUI.setState("cancelled");
221 if (event.key == "X") return BrowserUI.setState("deleted");
222 }
223 }
224
225 function browserInit() {
226 document.body.addEventListener("keydown", handleKey, { capture: false });
227 log.replay();
228 BrowserUI.firstVisibleTask()?.focus();
229 }