]> git.scottworley.com Git - vopamoi/blame - vopamoi.ts
Start with focus on top task
[vopamoi] / vopamoi.ts
CommitLineData
121d9948
SW
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;
5const MAX_SAFE_INTEGER = 9007199254740991;
6
7// A sane split that splits N *times*, leaving the last chunk unsplit.
8function 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
13c97b99 16const Model = {
6d01c406 17 addTask: function (timestamp: string, description: string): Element {
13c97b99
SW
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
799f4e89 20 task.setAttribute("class", "task");
13c97b99 21 task.setAttribute("tabindex", "0");
4101e1b1 22 task.setAttribute("data-created", timestamp);
ef7ebad4
SW
23 document.getElementById("tasks")!.appendChild(task);
24 task.focus();
6d01c406 25 return task;
13c97b99 26 },
974848d3 27
412e3663
SW
28 destroyTask: function (createTimestamp: string) {
29 const task = this.getTask(createTimestamp);
30 if (task) {
31 task.parentElement!.removeChild(task);
32 }
13c97b99 33 },
974848d3 34
68a72fde
SW
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
799f4e89
SW
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
68a72fde
SW
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
01f41859
SW
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 }
799f4e89 73 },
13c97b99 74};
f1afad9b 75
d03daa19 76function Log(prefix: string = "vp-") {
60a63831
SW
77 var next_log_index = 0;
78 return {
e88c099c 79 apply: function (entry: string) {
60a63831
SW
80 const [timestamp, command, data] = splitN(entry, " ", 2);
81 if (command == "Create") {
6d01c406 82 return Model.addTask(timestamp, data);
60a63831 83 }
799f4e89 84 if (command == "Destroy") {
6d01c406 85 return Model.destroyTask(data.split(" ", 1)[0]);
799f4e89 86 }
01f41859
SW
87 if (command == "State") {
88 const [createTimestamp, state] = splitN(data, " ", 1);
6d01c406 89 return Model.setState(timestamp, createTimestamp, state);
01f41859 90 }
68a72fde
SW
91 if (command == "Priority") {
92 const [createTimestamp, newPriority] = splitN(data, " ", 1);
6d01c406 93 return Model.setPriority(createTimestamp, parseFloat(newPriority));
68a72fde 94 }
60a63831
SW
95 },
96
e88c099c 97 record: function (entry: string) {
d03daa19 98 window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
60a63831
SW
99 },
100
e88c099c
SW
101 recordAndApply: function (entry: string) {
102 this.record(entry);
6d01c406 103 return this.apply(entry);
60a63831
SW
104 },
105
106 replay: function () {
107 while (true) {
d03daa19 108 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
60a63831
SW
109 if (entry === null) {
110 break;
111 }
e88c099c 112 this.apply(entry);
60a63831
SW
113 next_log_index++;
114 }
115 },
116 };
d03daa19
SW
117}
118const log = Log();
262705dd 119
e88c099c 120const UI = {
6d01c406
SW
121 addTask: function (description: string): Element {
122 return <Element>log.recordAndApply(`${Date.now()} Create ${description}`);
e88c099c 123 },
799f4e89 124 destroyTask: function (createTimestamp: string) {
6d01c406 125 return log.recordAndApply(`${Date.now()} Destroy ${createTimestamp} ${Model.getTask(createTimestamp)?.textContent}`);
799f4e89 126 },
68a72fde 127 setPriority: function (createTimestamp: string, priority: number) {
6d01c406 128 return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${priority}`);
68a72fde 129 },
01f41859 130 setState: function (createTimestamp: string, state: string) {
6d01c406 131 return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`);
01f41859 132 },
e88c099c
SW
133};
134
caa93fd1 135const BrowserUI = {
bc7996fe 136 addTask: function (event: KeyboardEvent) {
a26b1f4b
SW
137 const input = <HTMLInputElement>document.getElementById("taskName");
138 if (input.value) {
6d01c406 139 const task = UI.addTask(input.value);
a26b1f4b 140 input.value = "";
bc7996fe
SW
141 if (event.getModifierState("Control")) {
142 this.setPriority(task, null, document.getElementsByClassName("task")[0]);
143 }
caa93fd1 144 }
caa93fd1 145 },
09657615 146
65a7510d
SW
147 firstVisibleTask: function () {
148 for (const task of document.getElementsByClassName("task")) {
149 if (task instanceof HTMLElement && task.style.display !== "none") {
150 return task;
151 }
152 }
799f4e89 153 },
caa93fd1 154
bc7996fe 155 focusTaskNameInput: function (event: Event) {
09657615
SW
156 document.getElementById("taskName")!.focus();
157 event.preventDefault();
158 },
159
23be73e3
SW
160 visibleTaskAtOffset(task: Element, offset: number): Element {
161 var cursor: Element | null = task;
5fa4704c
SW
162 var valid_cursor = cursor;
163 const increment = offset / Math.abs(offset);
164 while (true) {
165 cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
166 if (!cursor || !(cursor instanceof HTMLElement)) break;
167 if (cursor.style.display !== "none") {
168 offset -= increment;
169 valid_cursor = cursor;
170 }
171 if (Math.abs(offset) < 0.5) break;
09657615 172 }
23be73e3
SW
173 return valid_cursor;
174 },
175
176 moveCursor: function (offset: number): boolean {
177 const active = document.activeElement;
178 if (!active) return false;
179 const dest = this.visibleTaskAtOffset(active, offset);
180 if (dest !== active && dest instanceof HTMLElement) {
181 dest.focus();
09657615
SW
182 return true;
183 }
184 return false;
185 },
01f41859 186
68a72fde
SW
187 moveTask: function (offset: number) {
188 const active = document.activeElement;
189 if (!active) return;
190 const dest = this.visibleTaskAtOffset(active, offset);
191 if (dest === active) return; // Already extremal
192 var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
193 if (onePastDest == dest) onePastDest = null; // Will become extremal
194 if (offset > 0) {
195 this.setPriority(active, dest, onePastDest);
196 } else {
197 this.setPriority(active, onePastDest, dest);
198 }
199 },
200
201 // Change task's priority to be between other tasks a and b.
202 setPriority: function (task: Element, a: Element | null, b: Element | null) {
203 const aPriority = a === null ? 0 : Model.getPriority(a);
204 const bPriority = b === null ? Date.now() : Model.getPriority(b);
205 console.assert(aPriority < bPriority, aPriority, "<", bPriority);
206 const span = bPriority - aPriority;
207 const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random();
208 console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority);
209 const newPriorityRounded = Math.round(newPriority);
210 const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority;
211 UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority);
212 },
213
01f41859
SW
214 setState: function (state: string) {
215 const createTimestamp = document.activeElement?.getAttribute("data-created");
216 this.moveCursor(1) || this.moveCursor(-1);
217 return UI.setState(createTimestamp!, state);
218 },
09657615 219};
06ee32a1 220
f1afad9b 221function handleKey(event: any) {
a26b1f4b 222 if (event.target.tagName === "INPUT") {
bc7996fe 223 if (event.key == "Enter") return BrowserUI.addTask(event);
a26b1f4b 224 } else {
09657615
SW
225 if (event.key == "j") return BrowserUI.moveCursor(1);
226 if (event.key == "k") return BrowserUI.moveCursor(-1);
68a72fde
SW
227 if (event.key == "J") return BrowserUI.moveTask(1);
228 if (event.key == "K") return BrowserUI.moveTask(-1);
01f41859
SW
229 if (event.key == "n") return BrowserUI.focusTaskNameInput(event);
230 if (event.key == "s") return BrowserUI.setState("someday-maybe");
231 if (event.key == "w") return BrowserUI.setState("waiting");
232 if (event.key == "d") return BrowserUI.setState("done");
233 if (event.key == "c") return BrowserUI.setState("cancelled");
799f4e89 234 if (event.key == "X") return BrowserUI.destroyTask();
f1afad9b
SW
235 }
236}
237
f1afad9b
SW
238function browserInit() {
239 document.body.addEventListener("keydown", handleKey, { capture: false });
d03daa19 240 log.replay();
65a7510d 241 BrowserUI.firstVisibleTask()?.focus();
f1afad9b 242}