]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
a88408f639f3c8663ff16d92f38c4476741d93a3
[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): Element | null {
44 const target = this.getTask(createTimestamp);
45 if (!target) return null;
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 target;
52 }
53 }
54 document.getElementById("tasks")!.appendChild(target);
55 target instanceof HTMLElement && target.focus();
56 return target;
57 },
58
59 setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
60 const task = this.getTask(createTimestamp);
61 if (task) {
62 task.setAttribute(`data-${state}`, stateTimestamp);
63 if (task instanceof HTMLElement) {
64 task.style.display = "none"; // Until view filtering
65 }
66 }
67 },
68 };
69
70 function Log(prefix: string = "vp-") {
71 var next_log_index = 0;
72 return {
73 apply: function (entry: string) {
74 const [timestamp, command, data] = splitN(entry, " ", 2);
75 if (command == "Create") {
76 return Model.addTask(timestamp, data);
77 }
78 if (command == "State") {
79 const [createTimestamp, state] = splitN(data, " ", 1);
80 return Model.setState(timestamp, createTimestamp, state);
81 }
82 if (command == "Priority") {
83 const [createTimestamp, newPriority] = splitN(data, " ", 1);
84 return Model.setPriority(createTimestamp, parseFloat(newPriority));
85 }
86 },
87
88 record: function (entry: string) {
89 window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
90 },
91
92 recordAndApply: function (entry: string) {
93 this.record(entry);
94 return this.apply(entry);
95 },
96
97 replay: function () {
98 while (true) {
99 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
100 if (entry === null) {
101 break;
102 }
103 this.apply(entry);
104 next_log_index++;
105 }
106 },
107 };
108 }
109 const log = Log();
110
111 const undoLog: string[] = [];
112
113 const UI = {
114 addTask: function (description: string): Element {
115 const now = Date.now();
116 undoLog.push(`State ${now} deleted`);
117 return <Element>log.recordAndApply(`${now} Create ${description}`);
118 },
119 setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
120 undoLog.push(`Priority ${createTimestamp} ${oldPriority}`);
121 return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${newPriority}`);
122 },
123 setState: function (createTimestamp: string, state: string) {
124 return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`);
125 },
126 undo: function () {
127 if (undoLog.length > 0) {
128 return log.recordAndApply(`${Date.now()} ${undoLog.pop()}`);
129 }
130 },
131 };
132
133 const BrowserUI = {
134 addTask: function (event: KeyboardEvent) {
135 const input = <HTMLInputElement>document.getElementById("taskName");
136 if (input.value) {
137 const task = UI.addTask(input.value);
138 input.value = "";
139 if (event.getModifierState("Control")) {
140 this.setPriority(task, null, document.getElementsByClassName("task")[0]);
141 }
142 }
143 },
144
145 firstVisibleTask: function () {
146 for (const task of document.getElementsByClassName("task")) {
147 if (task instanceof HTMLElement && task.style.display !== "none") {
148 return task;
149 }
150 }
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, Model.getPriority(task));
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 undo: function () {
219 const ret = UI.undo();
220 if (ret && ret instanceof HTMLElement) ret.focus();
221 },
222 };
223
224 function handleKey(event: any) {
225 if (event.target.tagName === "INPUT") {
226 if (event.key == "Enter") return BrowserUI.addTask(event);
227 } else {
228 if (event.key == "j") return BrowserUI.moveCursor(1);
229 if (event.key == "k") return BrowserUI.moveCursor(-1);
230 if (event.key == "J") return BrowserUI.moveTask(1);
231 if (event.key == "K") return BrowserUI.moveTask(-1);
232 if (event.key == "n") return BrowserUI.focusTaskNameInput(event);
233 if (event.key == "s") return BrowserUI.setState("someday-maybe");
234 if (event.key == "w") return BrowserUI.setState("waiting");
235 if (event.key == "d") return BrowserUI.setState("done");
236 if (event.key == "c") return BrowserUI.setState("cancelled");
237 if (event.key == "X") return BrowserUI.setState("deleted");
238 if (event.key == "u") return BrowserUI.undo();
239 }
240 }
241
242 function browserInit() {
243 document.body.addEventListener("keydown", handleKey, { capture: false });
244 log.replay();
245 BrowserUI.firstVisibleTask()?.focus();
246 }