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