]>
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 | ||
27c67784 SW |
16 | // A clock that never goes backwards; monotonic. |
17 | function Clock() { | |
18 | var previousNow = Date.now(); | |
19 | return { | |
20 | now: function (): number { | |
21 | const now = Date.now(); | |
22 | if (now > previousNow) { | |
23 | previousNow = now; | |
24 | return now; | |
25 | } | |
26 | return ++previousNow; | |
27 | }, | |
28 | }; | |
29 | } | |
30 | const clock = Clock(); | |
31 | ||
c70b3eed SW |
32 | // Returns a promise for a hue based on a hash of the string |
33 | function hashHue(str: string) { | |
34 | // Using crypto for this is overkill | |
35 | return crypto.subtle.digest("SHA-256", new TextEncoder().encode(str)).then((buf) => (new Uint16Array(buf)[0] * 360) / 2 ** 16); | |
36 | } | |
37 | ||
13c97b99 | 38 | const Model = { |
6d01c406 | 39 | addTask: function (timestamp: string, description: string): Element { |
13c97b99 | 40 | const task = document.createElement("div"); |
26737687 SW |
41 | const desc = document.createElement("span"); |
42 | desc.textContent = description; | |
43 | desc.classList.add("desc"); | |
44 | task.appendChild(desc); | |
7ccc80f6 | 45 | task.classList.add("task"); |
13c97b99 | 46 | task.setAttribute("tabindex", "0"); |
4101e1b1 | 47 | task.setAttribute("data-created", timestamp); |
682139fc | 48 | task.setAttribute("data-state", "todo"); |
ef7ebad4 | 49 | document.getElementById("tasks")!.appendChild(task); |
6d01c406 | 50 | return task; |
13c97b99 | 51 | }, |
974848d3 | 52 | |
7b5b90b9 SW |
53 | addTag: function (createTimestamp: string, tagName: string): Element | null { |
54 | const task = this.getTask(createTimestamp); | |
55 | if (!task) return null; | |
3916a89c SW |
56 | const existingTag = this.hasTag(task, tagName); |
57 | if (existingTag) return existingTag; | |
7b5b90b9 SW |
58 | const tag = document.createElement("span"); |
59 | tag.appendChild(document.createTextNode(tagName)); | |
60 | tag.classList.add("tag"); | |
61 | tag.setAttribute("tabindex", "0"); | |
c70b3eed | 62 | hashHue(tagName).then((hue) => (tag.style.backgroundColor = `hsl(${hue},90%,45%)`)); |
7b5b90b9 SW |
63 | task.appendChild(tag); |
64 | return tag; | |
65 | }, | |
66 | ||
7b574407 SW |
67 | edit: function (createTimestamp: string, newDescription: string): Element | null { |
68 | const target = this.getTask(createTimestamp); | |
69 | if (!target) return null; | |
70 | if (target.hasAttribute("data-description")) { | |
71 | // Oh no: An edit has arrived from a replica while a local edit is in progress. | |
132921e6 | 72 | const input = target.firstChild as HTMLInputElement; |
7b574407 SW |
73 | if ( |
74 | input.value === target.getAttribute("data-description") && | |
3a731557 | 75 | input.selectionStart === input.value.length && |
7b574407 SW |
76 | input.selectionEnd === input.value.length |
77 | ) { | |
78 | // No local changes have actually been made yet. Change the contents of the edit box! | |
79 | input.value = newDescription; | |
7b574407 SW |
80 | } else { |
81 | // No great options. | |
82 | // Prefer not to interrupt the local user's edit. | |
83 | // The remote edit is mostly lost; this mostly becomes last-write-wins. | |
84 | target.setAttribute("data-description", newDescription); | |
85 | } | |
86 | } else { | |
26737687 | 87 | target.getElementsByClassName("desc")[0].textContent = newDescription; |
7b574407 SW |
88 | } |
89 | return target; | |
90 | }, | |
91 | ||
3916a89c | 92 | hasTag: function (task: Element, tag: string): Element | null { |
54c19180 SW |
93 | for (const child of task.getElementsByClassName("tag")) { |
94 | if (child.textContent === tag) { | |
3916a89c | 95 | return child; |
e1eb33ad SW |
96 | } |
97 | } | |
3916a89c | 98 | return null; |
e1eb33ad SW |
99 | }, |
100 | ||
68a72fde SW |
101 | getPriority: function (task: Element): number { |
102 | if (task.hasAttribute("data-priority")) { | |
103 | return parseFloat(task.getAttribute("data-priority")!); | |
104 | } | |
105 | return parseFloat(task.getAttribute("data-created")!); | |
106 | }, | |
107 | ||
799f4e89 SW |
108 | getTask: function (createTimestamp: string) { |
109 | for (const task of document.getElementsByClassName("task")) { | |
110 | if (task.getAttribute("data-created") === createTimestamp) { | |
111 | return task; | |
112 | } | |
113 | } | |
114 | }, | |
115 | ||
43f3cc0c | 116 | setPriority: function (createTimestamp: string, priority: number): Element | null { |
68a72fde | 117 | const target = this.getTask(createTimestamp); |
43f3cc0c | 118 | if (!target) return null; |
68a72fde SW |
119 | target.setAttribute("data-priority", `${priority}`); |
120 | for (const task of document.getElementsByClassName("task")) { | |
121 | if (task !== target && this.getPriority(task) > priority) { | |
122 | task.parentElement!.insertBefore(target, task); | |
43f3cc0c | 123 | return target; |
68a72fde SW |
124 | } |
125 | } | |
126 | document.getElementById("tasks")!.appendChild(target); | |
43f3cc0c | 127 | return target; |
68a72fde SW |
128 | }, |
129 | ||
01f41859 SW |
130 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { |
131 | const task = this.getTask(createTimestamp); | |
132 | if (task) { | |
5350da9f | 133 | task.setAttribute("data-state", state); |
01f41859 | 134 | } |
799f4e89 | 135 | }, |
13c97b99 | 136 | }; |
f1afad9b | 137 | |
d03daa19 | 138 | function Log(prefix: string = "vp-") { |
60a63831 SW |
139 | var next_log_index = 0; |
140 | return { | |
e88c099c | 141 | apply: function (entry: string) { |
60a63831 SW |
142 | const [timestamp, command, data] = splitN(entry, " ", 2); |
143 | if (command == "Create") { | |
6d01c406 | 144 | return Model.addTask(timestamp, data); |
60a63831 | 145 | } |
7b574407 SW |
146 | if (command == "Edit") { |
147 | const [createTimestamp, description] = splitN(data, " ", 1); | |
148 | return Model.edit(createTimestamp, description); | |
149 | } | |
68a72fde SW |
150 | if (command == "Priority") { |
151 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 152 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 153 | } |
6a5644f3 SW |
154 | if (command == "State") { |
155 | const [createTimestamp, state] = splitN(data, " ", 1); | |
156 | return Model.setState(timestamp, createTimestamp, state); | |
157 | } | |
7b5b90b9 SW |
158 | if (command == "Tag") { |
159 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
160 | return Model.addTag(createTimestamp, tag); | |
161 | } | |
60a63831 SW |
162 | }, |
163 | ||
e88c099c | 164 | record: function (entry: string) { |
d03daa19 | 165 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
166 | }, |
167 | ||
e88c099c SW |
168 | recordAndApply: function (entry: string) { |
169 | this.record(entry); | |
6d01c406 | 170 | return this.apply(entry); |
60a63831 SW |
171 | }, |
172 | ||
173 | replay: function () { | |
174 | while (true) { | |
d03daa19 | 175 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
176 | if (entry === null) { |
177 | break; | |
178 | } | |
e88c099c | 179 | this.apply(entry); |
60a63831 SW |
180 | next_log_index++; |
181 | } | |
182 | }, | |
183 | }; | |
d03daa19 SW |
184 | } |
185 | const log = Log(); | |
262705dd | 186 | |
b56a37d3 SW |
187 | function UI() { |
188 | const undoLog: string[] = []; | |
189 | return { | |
190 | addTask: function (description: string): Element { | |
191 | const now = clock.now(); | |
192 | undoLog.push(`State ${now} deleted`); | |
193 | return <Element>log.recordAndApply(`${now} Create ${description}`); | |
194 | }, | |
7b5b90b9 SW |
195 | addTag: function (createTimestamp: string, tag: string) { |
196 | // TODO: undo | |
197 | return log.recordAndApply(`${clock.now()} Tag ${createTimestamp} ${tag}`); | |
198 | }, | |
b56a37d3 SW |
199 | edit: function (createTimestamp: string, newDescription: string, oldDescription: string) { |
200 | undoLog.push(`Edit ${createTimestamp} ${oldDescription}`); | |
201 | return log.recordAndApply(`${clock.now()} Edit ${createTimestamp} ${newDescription}`); | |
202 | }, | |
203 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { | |
204 | undoLog.push(`Priority ${createTimestamp} ${oldPriority}`); | |
205 | return log.recordAndApply(`${clock.now()} Priority ${createTimestamp} ${newPriority}`); | |
206 | }, | |
207 | setState: function (createTimestamp: string, newState: string, oldState: string) { | |
208 | undoLog.push(`State ${createTimestamp} ${oldState}`); | |
209 | return log.recordAndApply(`${clock.now()} State ${createTimestamp} ${newState}`); | |
210 | }, | |
211 | undo: function () { | |
212 | if (undoLog.length > 0) { | |
213 | return log.recordAndApply(`${clock.now()} ${undoLog.pop()}`); | |
214 | } | |
215 | }, | |
216 | }; | |
217 | } | |
218 | const ui = UI(); | |
e88c099c | 219 | |
ad72cd51 SW |
220 | enum CommitOrAbort { |
221 | Commit, | |
222 | Abort, | |
223 | } | |
224 | ||
ada060d7 | 225 | function BrowserUI() { |
868667c1 | 226 | var currentViewState = "todo"; |
a59fbe41 | 227 | var taskFocusedBeforeJumpingToInput: HTMLElement | null = null; |
09cd65ad | 228 | var lastTagNameEntered = ""; |
ada060d7 SW |
229 | return { |
230 | addTask: function (event: KeyboardEvent) { | |
231 | const input = <HTMLInputElement>document.getElementById("taskName"); | |
232 | if (input.value) { | |
b56a37d3 | 233 | const task = ui.addTask(input.value); |
a59fbe41 SW |
234 | if (currentViewState === "todo") { |
235 | task instanceof HTMLElement && task.focus(); | |
236 | } else if (this.returnFocusAfterInput()) { | |
237 | } else { | |
238 | this.firstVisibleTask()?.focus(); | |
239 | } | |
ada060d7 SW |
240 | input.value = ""; |
241 | if (event.getModifierState("Control")) { | |
242 | this.setPriority(task, null, document.getElementsByClassName("task")[0]); | |
243 | } | |
bc7996fe | 244 | } |
ada060d7 | 245 | }, |
09657615 | 246 | |
ada060d7 SW |
247 | beginEdit: function (event: Event) { |
248 | const task = document.activeElement; | |
249 | if (!task) return; | |
250 | const input = document.createElement("input"); | |
26737687 SW |
251 | const desc = task.getElementsByClassName("desc")[0]; |
252 | const oldDescription = desc.textContent!; | |
ada060d7 SW |
253 | task.setAttribute("data-description", oldDescription); |
254 | input.value = oldDescription; | |
255 | input.addEventListener("blur", this.completeEdit, { once: true }); | |
26737687 | 256 | desc.textContent = ""; |
7b5b90b9 SW |
257 | task.insertBefore(input, task.firstChild); |
258 | input.focus(); | |
259 | event.preventDefault(); | |
260 | }, | |
261 | ||
262 | beginTagEdit: function (event: Event) { | |
263 | const task = document.activeElement; | |
264 | if (!task) return; | |
265 | const input = document.createElement("input"); | |
266 | input.classList.add("tag"); | |
267 | input.addEventListener("blur", this.completeTagEdit, { once: true }); | |
09cd65ad | 268 | input.value = lastTagNameEntered; |
ada060d7 SW |
269 | task.appendChild(input); |
270 | input.focus(); | |
09cd65ad | 271 | input.select(); |
ada060d7 SW |
272 | event.preventDefault(); |
273 | }, | |
7b574407 | 274 | |
ad72cd51 | 275 | completeEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { |
ada060d7 SW |
276 | const input = event.target as HTMLInputElement; |
277 | const task = input.parentElement!; | |
26737687 | 278 | const desc = task.getElementsByClassName("desc")[0]; |
ada060d7 SW |
279 | const oldDescription = task.getAttribute("data-description")!; |
280 | const newDescription = input.value; | |
281 | input.removeEventListener("blur", this.completeEdit); | |
132921e6 | 282 | task.removeChild(input); |
ada060d7 SW |
283 | task.removeAttribute("data-description"); |
284 | task.focus(); | |
ad72cd51 | 285 | if (newDescription === oldDescription || resolution === CommitOrAbort.Abort) { |
26737687 | 286 | desc.textContent = oldDescription; |
ada060d7 | 287 | } else { |
b56a37d3 | 288 | ui.edit(task.getAttribute("data-created")!, newDescription, oldDescription); |
ada060d7 SW |
289 | } |
290 | }, | |
7b574407 | 291 | |
7b5b90b9 SW |
292 | completeTagEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { |
293 | const input = event.target as HTMLInputElement; | |
294 | const task = input.parentElement!; | |
295 | const newTagName = input.value; | |
296 | input.removeEventListener("blur", this.completeTagEdit); | |
297 | task.removeChild(input); | |
298 | task.focus(); | |
187164d5 | 299 | if (resolution === CommitOrAbort.Commit && newTagName && !Model.hasTag(task, newTagName)) { |
e1eb33ad | 300 | ui.addTag(task.getAttribute("data-created")!, newTagName); |
09cd65ad | 301 | lastTagNameEntered = newTagName; |
e1eb33ad | 302 | } |
7b5b90b9 SW |
303 | }, |
304 | ||
ada060d7 SW |
305 | firstVisibleTask: function () { |
306 | for (const task of document.getElementsByClassName("task")) { | |
868667c1 | 307 | if (task instanceof HTMLElement && task.getAttribute("data-state") === currentViewState) { |
ada060d7 SW |
308 | return task; |
309 | } | |
65a7510d | 310 | } |
ada060d7 | 311 | }, |
caa93fd1 | 312 | |
ada060d7 | 313 | focusTaskNameInput: function (event: Event) { |
a59fbe41 SW |
314 | if (document.activeElement instanceof HTMLElement) { |
315 | taskFocusedBeforeJumpingToInput = document.activeElement; | |
316 | } | |
ada060d7 SW |
317 | document.getElementById("taskName")!.focus(); |
318 | event.preventDefault(); | |
319 | }, | |
09657615 | 320 | |
ada060d7 SW |
321 | visibleTaskAtOffset(task: Element, offset: number): Element { |
322 | var cursor: Element | null = task; | |
323 | var valid_cursor = cursor; | |
324 | const increment = offset / Math.abs(offset); | |
325 | while (true) { | |
326 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
327 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
868667c1 | 328 | if (cursor.getAttribute("data-state")! === currentViewState) { |
ada060d7 SW |
329 | offset -= increment; |
330 | valid_cursor = cursor; | |
331 | } | |
332 | if (Math.abs(offset) < 0.5) break; | |
5fa4704c | 333 | } |
ada060d7 SW |
334 | return valid_cursor; |
335 | }, | |
23be73e3 | 336 | |
ada060d7 SW |
337 | moveCursor: function (offset: number): boolean { |
338 | const active = document.activeElement; | |
339 | if (!active) return false; | |
340 | const dest = this.visibleTaskAtOffset(active, offset); | |
341 | if (dest !== active && dest instanceof HTMLElement) { | |
342 | dest.focus(); | |
343 | return true; | |
344 | } | |
345 | return false; | |
346 | }, | |
01f41859 | 347 | |
ada060d7 SW |
348 | moveTask: function (offset: number) { |
349 | const active = document.activeElement; | |
350 | if (!active) return; | |
351 | const dest = this.visibleTaskAtOffset(active, offset); | |
352 | if (dest === active) return; // Already extremal | |
353 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
354 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
355 | if (offset > 0) { | |
356 | this.setPriority(active, dest, onePastDest); | |
357 | } else { | |
358 | this.setPriority(active, onePastDest, dest); | |
359 | } | |
360 | }, | |
68a72fde | 361 | |
a59fbe41 SW |
362 | returnFocusAfterInput: function (): boolean { |
363 | if (taskFocusedBeforeJumpingToInput) { | |
364 | taskFocusedBeforeJumpingToInput.focus(); | |
365 | return true; | |
366 | } | |
367 | return false; | |
368 | }, | |
369 | ||
ada060d7 SW |
370 | // Change task's priority to be between other tasks a and b. |
371 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
372 | const aPriority = a === null ? 0 : Model.getPriority(a); | |
373 | const bPriority = b === null ? clock.now() : Model.getPriority(b); | |
374 | console.assert(aPriority < bPriority, aPriority, "<", bPriority); | |
375 | const span = bPriority - aPriority; | |
376 | const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random(); | |
377 | console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority); | |
378 | const newPriorityRounded = Math.round(newPriority); | |
379 | const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority; | |
b56a37d3 | 380 | ui.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task)); |
ada060d7 SW |
381 | task instanceof HTMLElement && task.focus(); |
382 | }, | |
68a72fde | 383 | |
ada060d7 SW |
384 | setState: function (newState: string) { |
385 | const task = document.activeElement; | |
386 | if (!task) return; | |
387 | const oldState = task.getAttribute("data-state")!; | |
388 | if (newState === oldState) return; | |
389 | const createTimestamp = task.getAttribute("data-created")!; | |
390 | this.moveCursor(1) || this.moveCursor(-1); | |
b56a37d3 | 391 | return ui.setState(createTimestamp, newState, oldState); |
ada060d7 | 392 | }, |
43f3cc0c | 393 | |
868667c1 SW |
394 | setView: function (state: string) { |
395 | const sheet = (document.getElementById("viewStyle") as HTMLStyleElement).sheet!; | |
396 | sheet.insertRule(`.task:not([data-state=${state}]) { display: none }`); | |
397 | sheet.removeRule(1); | |
398 | currentViewState = state; | |
399 | if (document.activeElement?.getAttribute("data-state") !== state) { | |
400 | this.firstVisibleTask()?.focus(); | |
401 | } | |
402 | }, | |
403 | ||
ada060d7 | 404 | undo: function () { |
b56a37d3 | 405 | const ret = ui.undo(); |
ada060d7 SW |
406 | if (ret && ret instanceof HTMLElement) ret.focus(); |
407 | }, | |
408 | }; | |
409 | } | |
410 | const browserUI = BrowserUI(); | |
06ee32a1 | 411 | |
e94e9f27 SW |
412 | enum InputState { |
413 | Command, | |
854992ec | 414 | View, |
e94e9f27 SW |
415 | } |
416 | var inputState = InputState.Command; | |
417 | ||
f1afad9b | 418 | function handleKey(event: any) { |
a26b1f4b | 419 | if (event.target.tagName === "INPUT") { |
7b574407 | 420 | if (event.target.id === "taskName") { |
ada060d7 | 421 | if (event.key == "Enter") return browserUI.addTask(event); |
a59fbe41 | 422 | if (event.key == "Escape") return browserUI.returnFocusAfterInput(); |
7b5b90b9 SW |
423 | } else if (event.target.classList.contains("tag")) { |
424 | if (event.key == "Enter") return browserUI.completeTagEdit(event); | |
425 | if (event.key == "Escape") return browserUI.completeTagEdit(event, CommitOrAbort.Abort); | |
7b574407 | 426 | } else { |
ada060d7 | 427 | if (event.key == "Enter") return browserUI.completeEdit(event); |
ad72cd51 | 428 | if (event.key == "Escape") return browserUI.completeEdit(event, CommitOrAbort.Abort); |
7b574407 | 429 | } |
a26b1f4b | 430 | } else { |
e94e9f27 | 431 | if (inputState === InputState.Command) { |
ada060d7 SW |
432 | if (event.key == "j") return browserUI.moveCursor(1); |
433 | if (event.key == "k") return browserUI.moveCursor(-1); | |
434 | if (event.key == "J") return browserUI.moveTask(1); | |
435 | if (event.key == "K") return browserUI.moveTask(-1); | |
436 | if (event.key == "n") return browserUI.focusTaskNameInput(event); | |
ada060d7 | 437 | if (event.key == "c") return browserUI.setState("cancelled"); |
868667c1 | 438 | if (event.key == "d") return browserUI.setState("done"); |
1f300e10 | 439 | if (event.key == "q") return browserUI.setState("todo"); |
868667c1 | 440 | if (event.key == "s") return browserUI.setState("someday-maybe"); |
868667c1 | 441 | if (event.key == "w") return browserUI.setState("waiting"); |
ada060d7 SW |
442 | if (event.key == "X") return browserUI.setState("deleted"); |
443 | if (event.key == "u") return browserUI.undo(); | |
444 | if (event.key == "e") return browserUI.beginEdit(event); | |
7b5b90b9 | 445 | if (event.key == "t") return browserUI.beginTagEdit(event); |
854992ec SW |
446 | if (event.key == "v") return (inputState = InputState.View); |
447 | } else if (inputState === InputState.View) { | |
868667c1 SW |
448 | inputState = InputState.Command; |
449 | if (event.key == "c") return browserUI.setView("cancelled"); | |
450 | if (event.key == "d") return browserUI.setView("done"); | |
1f300e10 | 451 | if (event.key == "q") return browserUI.setView("todo"); |
868667c1 | 452 | if (event.key == "s") return browserUI.setView("someday-maybe"); |
868667c1 SW |
453 | if (event.key == "w") return browserUI.setView("waiting"); |
454 | if (event.key == "x") return browserUI.setView("deleted"); | |
e94e9f27 | 455 | } |
f1afad9b SW |
456 | } |
457 | } | |
458 | ||
f1afad9b SW |
459 | function browserInit() { |
460 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
d03daa19 | 461 | log.replay(); |
ada060d7 | 462 | browserUI.firstVisibleTask()?.focus(); |
f1afad9b | 463 | } |