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