]>
Commit | Line | Data |
---|---|---|
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; | |
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 | ||
13c97b99 | 16 | const 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 | |
68a72fde SW |
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 | ||
799f4e89 SW |
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 | ||
68a72fde SW |
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 | ||
01f41859 SW |
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 | } | |
799f4e89 | 66 | }, |
13c97b99 | 67 | }; |
f1afad9b | 68 | |
d03daa19 | 69 | function Log(prefix: string = "vp-") { |
60a63831 SW |
70 | var next_log_index = 0; |
71 | return { | |
e88c099c | 72 | apply: function (entry: string) { |
60a63831 SW |
73 | const [timestamp, command, data] = splitN(entry, " ", 2); |
74 | if (command == "Create") { | |
6d01c406 | 75 | return Model.addTask(timestamp, data); |
60a63831 | 76 | } |
01f41859 SW |
77 | if (command == "State") { |
78 | const [createTimestamp, state] = splitN(data, " ", 1); | |
6d01c406 | 79 | return Model.setState(timestamp, createTimestamp, state); |
01f41859 | 80 | } |
68a72fde SW |
81 | if (command == "Priority") { |
82 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 83 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 84 | } |
60a63831 SW |
85 | }, |
86 | ||
e88c099c | 87 | record: function (entry: string) { |
d03daa19 | 88 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
89 | }, |
90 | ||
e88c099c SW |
91 | recordAndApply: function (entry: string) { |
92 | this.record(entry); | |
6d01c406 | 93 | return this.apply(entry); |
60a63831 SW |
94 | }, |
95 | ||
96 | replay: function () { | |
97 | while (true) { | |
d03daa19 | 98 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
99 | if (entry === null) { |
100 | break; | |
101 | } | |
e88c099c | 102 | this.apply(entry); |
60a63831 SW |
103 | next_log_index++; |
104 | } | |
105 | }, | |
106 | }; | |
d03daa19 SW |
107 | } |
108 | const log = Log(); | |
262705dd | 109 | |
e88c099c | 110 | const UI = { |
6d01c406 SW |
111 | addTask: function (description: string): Element { |
112 | return <Element>log.recordAndApply(`${Date.now()} Create ${description}`); | |
e88c099c | 113 | }, |
68a72fde | 114 | setPriority: function (createTimestamp: string, priority: number) { |
6d01c406 | 115 | return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${priority}`); |
68a72fde | 116 | }, |
01f41859 | 117 | setState: function (createTimestamp: string, state: string) { |
6d01c406 | 118 | return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`); |
01f41859 | 119 | }, |
e88c099c SW |
120 | }; |
121 | ||
caa93fd1 | 122 | const BrowserUI = { |
bc7996fe | 123 | addTask: function (event: KeyboardEvent) { |
a26b1f4b SW |
124 | const input = <HTMLInputElement>document.getElementById("taskName"); |
125 | if (input.value) { | |
6d01c406 | 126 | const task = UI.addTask(input.value); |
a26b1f4b | 127 | input.value = ""; |
bc7996fe SW |
128 | if (event.getModifierState("Control")) { |
129 | this.setPriority(task, null, document.getElementsByClassName("task")[0]); | |
130 | } | |
caa93fd1 | 131 | } |
caa93fd1 | 132 | }, |
09657615 | 133 | |
65a7510d SW |
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 | } | |
799f4e89 | 140 | }, |
caa93fd1 | 141 | |
bc7996fe | 142 | focusTaskNameInput: function (event: Event) { |
09657615 SW |
143 | document.getElementById("taskName")!.focus(); |
144 | event.preventDefault(); | |
145 | }, | |
146 | ||
23be73e3 SW |
147 | visibleTaskAtOffset(task: Element, offset: number): Element { |
148 | var cursor: Element | null = task; | |
5fa4704c SW |
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; | |
09657615 | 159 | } |
23be73e3 SW |
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(); | |
09657615 SW |
169 | return true; |
170 | } | |
171 | return false; | |
172 | }, | |
01f41859 | 173 | |
68a72fde SW |
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 | ||
01f41859 SW |
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 | }, | |
09657615 | 206 | }; |
06ee32a1 | 207 | |
f1afad9b | 208 | function handleKey(event: any) { |
a26b1f4b | 209 | if (event.target.tagName === "INPUT") { |
bc7996fe | 210 | if (event.key == "Enter") return BrowserUI.addTask(event); |
a26b1f4b | 211 | } else { |
09657615 SW |
212 | if (event.key == "j") return BrowserUI.moveCursor(1); |
213 | if (event.key == "k") return BrowserUI.moveCursor(-1); | |
68a72fde SW |
214 | if (event.key == "J") return BrowserUI.moveTask(1); |
215 | if (event.key == "K") return BrowserUI.moveTask(-1); | |
01f41859 SW |
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"); | |
45cbd5e5 | 221 | if (event.key == "X") return BrowserUI.setState("deleted"); |
f1afad9b SW |
222 | } |
223 | } | |
224 | ||
f1afad9b SW |
225 | function browserInit() { |
226 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
d03daa19 | 227 | log.replay(); |
65a7510d | 228 | BrowserUI.firstVisibleTask()?.focus(); |
f1afad9b | 229 | } |