]>
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 | 23 | document.getElementById("tasks")!.appendChild(task); |
6d01c406 | 24 | return task; |
13c97b99 | 25 | }, |
974848d3 | 26 | |
7b574407 SW |
27 | edit: function (createTimestamp: string, newDescription: string): Element | null { |
28 | const target = this.getTask(createTimestamp); | |
29 | if (!target) return null; | |
30 | if (target.hasAttribute("data-description")) { | |
31 | // Oh no: An edit has arrived from a replica while a local edit is in progress. | |
32 | const input = target.children[0] as HTMLInputElement; | |
33 | if ( | |
34 | input.value === target.getAttribute("data-description") && | |
35 | input.selectionStart === 0 && | |
36 | input.selectionEnd === input.value.length | |
37 | ) { | |
38 | // No local changes have actually been made yet. Change the contents of the edit box! | |
39 | input.value = newDescription; | |
40 | input.select(); | |
41 | } else { | |
42 | // No great options. | |
43 | // Prefer not to interrupt the local user's edit. | |
44 | // The remote edit is mostly lost; this mostly becomes last-write-wins. | |
45 | target.setAttribute("data-description", newDescription); | |
46 | } | |
47 | } else { | |
48 | target.textContent = newDescription; | |
49 | } | |
50 | return target; | |
51 | }, | |
52 | ||
68a72fde SW |
53 | getPriority: function (task: Element): number { |
54 | if (task.hasAttribute("data-priority")) { | |
55 | return parseFloat(task.getAttribute("data-priority")!); | |
56 | } | |
57 | return parseFloat(task.getAttribute("data-created")!); | |
58 | }, | |
59 | ||
5350da9f SW |
60 | getState: function (task: Element): string { |
61 | return task.getAttribute("data-state") ?? "todo"; | |
62 | }, | |
63 | ||
799f4e89 SW |
64 | getTask: function (createTimestamp: string) { |
65 | for (const task of document.getElementsByClassName("task")) { | |
66 | if (task.getAttribute("data-created") === createTimestamp) { | |
67 | return task; | |
68 | } | |
69 | } | |
70 | }, | |
71 | ||
43f3cc0c | 72 | setPriority: function (createTimestamp: string, priority: number): Element | null { |
68a72fde | 73 | const target = this.getTask(createTimestamp); |
43f3cc0c | 74 | if (!target) return null; |
68a72fde SW |
75 | target.setAttribute("data-priority", `${priority}`); |
76 | for (const task of document.getElementsByClassName("task")) { | |
77 | if (task !== target && this.getPriority(task) > priority) { | |
78 | task.parentElement!.insertBefore(target, task); | |
43f3cc0c | 79 | return target; |
68a72fde SW |
80 | } |
81 | } | |
82 | document.getElementById("tasks")!.appendChild(target); | |
43f3cc0c | 83 | return target; |
68a72fde SW |
84 | }, |
85 | ||
01f41859 SW |
86 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { |
87 | const task = this.getTask(createTimestamp); | |
88 | if (task) { | |
5350da9f | 89 | task.setAttribute("data-state", state); |
01f41859 | 90 | if (task instanceof HTMLElement) { |
5350da9f | 91 | task.style.display = state == "todo" ? "block" : "none"; // Until view filtering |
01f41859 SW |
92 | } |
93 | } | |
799f4e89 | 94 | }, |
13c97b99 | 95 | }; |
f1afad9b | 96 | |
d03daa19 | 97 | function Log(prefix: string = "vp-") { |
60a63831 SW |
98 | var next_log_index = 0; |
99 | return { | |
e88c099c | 100 | apply: function (entry: string) { |
60a63831 SW |
101 | const [timestamp, command, data] = splitN(entry, " ", 2); |
102 | if (command == "Create") { | |
6d01c406 | 103 | return Model.addTask(timestamp, data); |
60a63831 | 104 | } |
7b574407 SW |
105 | if (command == "Edit") { |
106 | const [createTimestamp, description] = splitN(data, " ", 1); | |
107 | return Model.edit(createTimestamp, description); | |
108 | } | |
01f41859 SW |
109 | if (command == "State") { |
110 | const [createTimestamp, state] = splitN(data, " ", 1); | |
6d01c406 | 111 | return Model.setState(timestamp, createTimestamp, state); |
01f41859 | 112 | } |
68a72fde SW |
113 | if (command == "Priority") { |
114 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 115 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 116 | } |
60a63831 SW |
117 | }, |
118 | ||
e88c099c | 119 | record: function (entry: string) { |
d03daa19 | 120 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
121 | }, |
122 | ||
e88c099c SW |
123 | recordAndApply: function (entry: string) { |
124 | this.record(entry); | |
6d01c406 | 125 | return this.apply(entry); |
60a63831 SW |
126 | }, |
127 | ||
128 | replay: function () { | |
129 | while (true) { | |
d03daa19 | 130 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
131 | if (entry === null) { |
132 | break; | |
133 | } | |
e88c099c | 134 | this.apply(entry); |
60a63831 SW |
135 | next_log_index++; |
136 | } | |
137 | }, | |
138 | }; | |
d03daa19 SW |
139 | } |
140 | const log = Log(); | |
262705dd | 141 | |
43f3cc0c SW |
142 | const undoLog: string[] = []; |
143 | ||
e88c099c | 144 | const UI = { |
6d01c406 | 145 | addTask: function (description: string): Element { |
43f3cc0c SW |
146 | const now = Date.now(); |
147 | undoLog.push(`State ${now} deleted`); | |
148 | return <Element>log.recordAndApply(`${now} Create ${description}`); | |
e88c099c | 149 | }, |
7b574407 SW |
150 | edit: function (createTimestamp: string, newDescription: string, oldDescription: string) { |
151 | undoLog.push(`Edit ${createTimestamp} ${oldDescription}`); | |
152 | return log.recordAndApply(`${Date.now()} Edit ${createTimestamp} ${newDescription}`); | |
153 | }, | |
43f3cc0c SW |
154 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { |
155 | undoLog.push(`Priority ${createTimestamp} ${oldPriority}`); | |
156 | return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${newPriority}`); | |
68a72fde | 157 | }, |
5350da9f SW |
158 | setState: function (createTimestamp: string, newState: string, oldState: string) { |
159 | undoLog.push(`State ${createTimestamp} ${oldState}`); | |
160 | return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${newState}`); | |
01f41859 | 161 | }, |
43f3cc0c SW |
162 | undo: function () { |
163 | if (undoLog.length > 0) { | |
164 | return log.recordAndApply(`${Date.now()} ${undoLog.pop()}`); | |
165 | } | |
166 | }, | |
e88c099c SW |
167 | }; |
168 | ||
caa93fd1 | 169 | const BrowserUI = { |
bc7996fe | 170 | addTask: function (event: KeyboardEvent) { |
a26b1f4b SW |
171 | const input = <HTMLInputElement>document.getElementById("taskName"); |
172 | if (input.value) { | |
6d01c406 | 173 | const task = UI.addTask(input.value); |
9f6be65e | 174 | if (task && task instanceof HTMLElement) task.focus(); |
a26b1f4b | 175 | input.value = ""; |
bc7996fe SW |
176 | if (event.getModifierState("Control")) { |
177 | this.setPriority(task, null, document.getElementsByClassName("task")[0]); | |
178 | } | |
caa93fd1 | 179 | } |
caa93fd1 | 180 | }, |
09657615 | 181 | |
7b574407 SW |
182 | beginEdit: function (event: Event) { |
183 | const task = document.activeElement; | |
184 | if (!task) return; | |
185 | const input = document.createElement("input"); | |
186 | const oldDescription = task.textContent!; | |
187 | task.setAttribute("data-description", oldDescription); | |
188 | input.value = oldDescription; | |
189 | input.addEventListener("blur", BrowserUI.completeEdit, { once: true }); | |
190 | task.textContent = ""; | |
191 | task.appendChild(input); | |
192 | input.focus(); | |
193 | input.select(); | |
194 | event.preventDefault(); | |
195 | }, | |
196 | ||
197 | completeEdit: function (event: Event) { | |
198 | const input = event.target as HTMLInputElement; | |
199 | const task = input.parentElement!; | |
200 | const oldDescription = task.getAttribute("data-description")!; | |
201 | const newDescription = input.value; | |
202 | task.removeChild(task.children[0]); | |
203 | task.removeAttribute("data-description"); | |
204 | task.focus(); | |
205 | if (newDescription === oldDescription) { | |
206 | task.textContent = oldDescription; | |
207 | } else { | |
208 | UI.edit(task.getAttribute("data-created")!, newDescription, oldDescription); | |
209 | } | |
210 | }, | |
211 | ||
65a7510d SW |
212 | firstVisibleTask: function () { |
213 | for (const task of document.getElementsByClassName("task")) { | |
214 | if (task instanceof HTMLElement && task.style.display !== "none") { | |
215 | return task; | |
216 | } | |
217 | } | |
799f4e89 | 218 | }, |
caa93fd1 | 219 | |
bc7996fe | 220 | focusTaskNameInput: function (event: Event) { |
09657615 SW |
221 | document.getElementById("taskName")!.focus(); |
222 | event.preventDefault(); | |
223 | }, | |
224 | ||
23be73e3 SW |
225 | visibleTaskAtOffset(task: Element, offset: number): Element { |
226 | var cursor: Element | null = task; | |
5fa4704c SW |
227 | var valid_cursor = cursor; |
228 | const increment = offset / Math.abs(offset); | |
229 | while (true) { | |
230 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
231 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
232 | if (cursor.style.display !== "none") { | |
233 | offset -= increment; | |
234 | valid_cursor = cursor; | |
235 | } | |
236 | if (Math.abs(offset) < 0.5) break; | |
09657615 | 237 | } |
23be73e3 SW |
238 | return valid_cursor; |
239 | }, | |
240 | ||
241 | moveCursor: function (offset: number): boolean { | |
242 | const active = document.activeElement; | |
243 | if (!active) return false; | |
244 | const dest = this.visibleTaskAtOffset(active, offset); | |
245 | if (dest !== active && dest instanceof HTMLElement) { | |
246 | dest.focus(); | |
09657615 SW |
247 | return true; |
248 | } | |
249 | return false; | |
250 | }, | |
01f41859 | 251 | |
68a72fde SW |
252 | moveTask: function (offset: number) { |
253 | const active = document.activeElement; | |
254 | if (!active) return; | |
255 | const dest = this.visibleTaskAtOffset(active, offset); | |
256 | if (dest === active) return; // Already extremal | |
257 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
258 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
259 | if (offset > 0) { | |
260 | this.setPriority(active, dest, onePastDest); | |
261 | } else { | |
262 | this.setPriority(active, onePastDest, dest); | |
263 | } | |
264 | }, | |
265 | ||
266 | // Change task's priority to be between other tasks a and b. | |
267 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
268 | const aPriority = a === null ? 0 : Model.getPriority(a); | |
269 | const bPriority = b === null ? Date.now() : Model.getPriority(b); | |
270 | console.assert(aPriority < bPriority, aPriority, "<", bPriority); | |
271 | const span = bPriority - aPriority; | |
272 | const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random(); | |
273 | console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority); | |
274 | const newPriorityRounded = Math.round(newPriority); | |
275 | const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority; | |
43f3cc0c | 276 | UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task)); |
9f6be65e | 277 | task instanceof HTMLElement && task.focus(); |
68a72fde SW |
278 | }, |
279 | ||
da9a2716 | 280 | setState: function (newState: string) { |
5350da9f SW |
281 | const task = document.activeElement; |
282 | if (!task) return; | |
da9a2716 SW |
283 | const oldState = Model.getState(task); |
284 | if (newState === oldState) return; | |
5350da9f | 285 | const createTimestamp = task.getAttribute("data-created")!; |
01f41859 | 286 | this.moveCursor(1) || this.moveCursor(-1); |
da9a2716 | 287 | return UI.setState(createTimestamp, newState, oldState); |
01f41859 | 288 | }, |
43f3cc0c SW |
289 | |
290 | undo: function () { | |
291 | const ret = UI.undo(); | |
292 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
293 | }, | |
09657615 | 294 | }; |
06ee32a1 | 295 | |
f1afad9b | 296 | function handleKey(event: any) { |
a26b1f4b | 297 | if (event.target.tagName === "INPUT") { |
7b574407 SW |
298 | if (event.target.id === "taskName") { |
299 | if (event.key == "Enter") return BrowserUI.addTask(event); | |
300 | } else { | |
301 | if (event.key == "Enter") return BrowserUI.completeEdit(event); | |
302 | } | |
a26b1f4b | 303 | } else { |
09657615 SW |
304 | if (event.key == "j") return BrowserUI.moveCursor(1); |
305 | if (event.key == "k") return BrowserUI.moveCursor(-1); | |
68a72fde SW |
306 | if (event.key == "J") return BrowserUI.moveTask(1); |
307 | if (event.key == "K") return BrowserUI.moveTask(-1); | |
01f41859 SW |
308 | if (event.key == "n") return BrowserUI.focusTaskNameInput(event); |
309 | if (event.key == "s") return BrowserUI.setState("someday-maybe"); | |
310 | if (event.key == "w") return BrowserUI.setState("waiting"); | |
311 | if (event.key == "d") return BrowserUI.setState("done"); | |
312 | if (event.key == "c") return BrowserUI.setState("cancelled"); | |
da9a2716 | 313 | if (event.key == "t") return BrowserUI.setState("todo"); |
45cbd5e5 | 314 | if (event.key == "X") return BrowserUI.setState("deleted"); |
43f3cc0c | 315 | if (event.key == "u") return BrowserUI.undo(); |
7b574407 | 316 | if (event.key == "e") return BrowserUI.beginEdit(event); |
f1afad9b SW |
317 | } |
318 | } | |
319 | ||
f1afad9b SW |
320 | function browserInit() { |
321 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
d03daa19 | 322 | log.replay(); |
65a7510d | 323 | BrowserUI.firstVisibleTask()?.focus(); |
f1afad9b | 324 | } |