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