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