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