]>
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 | if (task instanceof HTMLElement) { |
5350da9f | 104 | task.style.display = state == "todo" ? "block" : "none"; // Until view filtering |
01f41859 SW |
105 | } |
106 | } | |
799f4e89 | 107 | }, |
13c97b99 | 108 | }; |
f1afad9b | 109 | |
d03daa19 | 110 | function Log(prefix: string = "vp-") { |
60a63831 SW |
111 | var next_log_index = 0; |
112 | return { | |
e88c099c | 113 | apply: function (entry: string) { |
60a63831 SW |
114 | const [timestamp, command, data] = splitN(entry, " ", 2); |
115 | if (command == "Create") { | |
6d01c406 | 116 | return Model.addTask(timestamp, data); |
60a63831 | 117 | } |
7b574407 SW |
118 | if (command == "Edit") { |
119 | const [createTimestamp, description] = splitN(data, " ", 1); | |
120 | return Model.edit(createTimestamp, description); | |
121 | } | |
01f41859 SW |
122 | if (command == "State") { |
123 | const [createTimestamp, state] = splitN(data, " ", 1); | |
6d01c406 | 124 | return Model.setState(timestamp, createTimestamp, state); |
01f41859 | 125 | } |
68a72fde SW |
126 | if (command == "Priority") { |
127 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 128 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 129 | } |
60a63831 SW |
130 | }, |
131 | ||
e88c099c | 132 | record: function (entry: string) { |
d03daa19 | 133 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
134 | }, |
135 | ||
e88c099c SW |
136 | recordAndApply: function (entry: string) { |
137 | this.record(entry); | |
6d01c406 | 138 | return this.apply(entry); |
60a63831 SW |
139 | }, |
140 | ||
141 | replay: function () { | |
142 | while (true) { | |
d03daa19 | 143 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
144 | if (entry === null) { |
145 | break; | |
146 | } | |
e88c099c | 147 | this.apply(entry); |
60a63831 SW |
148 | next_log_index++; |
149 | } | |
150 | }, | |
151 | }; | |
d03daa19 SW |
152 | } |
153 | const log = Log(); | |
262705dd | 154 | |
43f3cc0c SW |
155 | const undoLog: string[] = []; |
156 | ||
e88c099c | 157 | const UI = { |
6d01c406 | 158 | addTask: function (description: string): Element { |
27c67784 | 159 | const now = clock.now(); |
43f3cc0c SW |
160 | undoLog.push(`State ${now} deleted`); |
161 | return <Element>log.recordAndApply(`${now} Create ${description}`); | |
e88c099c | 162 | }, |
7b574407 SW |
163 | edit: function (createTimestamp: string, newDescription: string, oldDescription: string) { |
164 | undoLog.push(`Edit ${createTimestamp} ${oldDescription}`); | |
27c67784 | 165 | return log.recordAndApply(`${clock.now()} Edit ${createTimestamp} ${newDescription}`); |
7b574407 | 166 | }, |
43f3cc0c SW |
167 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { |
168 | undoLog.push(`Priority ${createTimestamp} ${oldPriority}`); | |
27c67784 | 169 | return log.recordAndApply(`${clock.now()} Priority ${createTimestamp} ${newPriority}`); |
68a72fde | 170 | }, |
5350da9f SW |
171 | setState: function (createTimestamp: string, newState: string, oldState: string) { |
172 | undoLog.push(`State ${createTimestamp} ${oldState}`); | |
27c67784 | 173 | return log.recordAndApply(`${clock.now()} State ${createTimestamp} ${newState}`); |
01f41859 | 174 | }, |
43f3cc0c SW |
175 | undo: function () { |
176 | if (undoLog.length > 0) { | |
27c67784 | 177 | return log.recordAndApply(`${clock.now()} ${undoLog.pop()}`); |
43f3cc0c SW |
178 | } |
179 | }, | |
e88c099c SW |
180 | }; |
181 | ||
caa93fd1 | 182 | const BrowserUI = { |
bc7996fe | 183 | addTask: function (event: KeyboardEvent) { |
a26b1f4b SW |
184 | const input = <HTMLInputElement>document.getElementById("taskName"); |
185 | if (input.value) { | |
6d01c406 | 186 | const task = UI.addTask(input.value); |
9f6be65e | 187 | if (task && task instanceof HTMLElement) task.focus(); |
a26b1f4b | 188 | input.value = ""; |
bc7996fe SW |
189 | if (event.getModifierState("Control")) { |
190 | this.setPriority(task, null, document.getElementsByClassName("task")[0]); | |
191 | } | |
caa93fd1 | 192 | } |
caa93fd1 | 193 | }, |
09657615 | 194 | |
7b574407 SW |
195 | beginEdit: function (event: Event) { |
196 | const task = document.activeElement; | |
197 | if (!task) return; | |
198 | const input = document.createElement("input"); | |
199 | const oldDescription = task.textContent!; | |
200 | task.setAttribute("data-description", oldDescription); | |
201 | input.value = oldDescription; | |
202 | input.addEventListener("blur", BrowserUI.completeEdit, { once: true }); | |
203 | task.textContent = ""; | |
204 | task.appendChild(input); | |
205 | input.focus(); | |
206 | input.select(); | |
207 | event.preventDefault(); | |
208 | }, | |
209 | ||
210 | completeEdit: function (event: Event) { | |
211 | const input = event.target as HTMLInputElement; | |
212 | const task = input.parentElement!; | |
213 | const oldDescription = task.getAttribute("data-description")!; | |
214 | const newDescription = input.value; | |
8ca3cda9 | 215 | input.removeEventListener("blur", BrowserUI.completeEdit); |
7b574407 SW |
216 | task.removeChild(task.children[0]); |
217 | task.removeAttribute("data-description"); | |
218 | task.focus(); | |
219 | if (newDescription === oldDescription) { | |
220 | task.textContent = oldDescription; | |
221 | } else { | |
222 | UI.edit(task.getAttribute("data-created")!, newDescription, oldDescription); | |
223 | } | |
224 | }, | |
225 | ||
65a7510d SW |
226 | firstVisibleTask: function () { |
227 | for (const task of document.getElementsByClassName("task")) { | |
228 | if (task instanceof HTMLElement && task.style.display !== "none") { | |
229 | return task; | |
230 | } | |
231 | } | |
799f4e89 | 232 | }, |
caa93fd1 | 233 | |
bc7996fe | 234 | focusTaskNameInput: function (event: Event) { |
09657615 SW |
235 | document.getElementById("taskName")!.focus(); |
236 | event.preventDefault(); | |
237 | }, | |
238 | ||
23be73e3 SW |
239 | visibleTaskAtOffset(task: Element, offset: number): Element { |
240 | var cursor: Element | null = task; | |
5fa4704c SW |
241 | var valid_cursor = cursor; |
242 | const increment = offset / Math.abs(offset); | |
243 | while (true) { | |
244 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
245 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
246 | if (cursor.style.display !== "none") { | |
247 | offset -= increment; | |
248 | valid_cursor = cursor; | |
249 | } | |
250 | if (Math.abs(offset) < 0.5) break; | |
09657615 | 251 | } |
23be73e3 SW |
252 | return valid_cursor; |
253 | }, | |
254 | ||
255 | moveCursor: function (offset: number): boolean { | |
256 | const active = document.activeElement; | |
257 | if (!active) return false; | |
258 | const dest = this.visibleTaskAtOffset(active, offset); | |
259 | if (dest !== active && dest instanceof HTMLElement) { | |
260 | dest.focus(); | |
09657615 SW |
261 | return true; |
262 | } | |
263 | return false; | |
264 | }, | |
01f41859 | 265 | |
68a72fde SW |
266 | moveTask: function (offset: number) { |
267 | const active = document.activeElement; | |
268 | if (!active) return; | |
269 | const dest = this.visibleTaskAtOffset(active, offset); | |
270 | if (dest === active) return; // Already extremal | |
271 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
272 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
273 | if (offset > 0) { | |
274 | this.setPriority(active, dest, onePastDest); | |
275 | } else { | |
276 | this.setPriority(active, onePastDest, dest); | |
277 | } | |
278 | }, | |
279 | ||
280 | // Change task's priority to be between other tasks a and b. | |
281 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
282 | const aPriority = a === null ? 0 : Model.getPriority(a); | |
27c67784 | 283 | const bPriority = b === null ? clock.now() : Model.getPriority(b); |
68a72fde SW |
284 | console.assert(aPriority < bPriority, aPriority, "<", bPriority); |
285 | const span = bPriority - aPriority; | |
286 | const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random(); | |
287 | console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority); | |
288 | const newPriorityRounded = Math.round(newPriority); | |
289 | const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority; | |
43f3cc0c | 290 | UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task)); |
9f6be65e | 291 | task instanceof HTMLElement && task.focus(); |
68a72fde SW |
292 | }, |
293 | ||
da9a2716 | 294 | setState: function (newState: string) { |
5350da9f SW |
295 | const task = document.activeElement; |
296 | if (!task) return; | |
682139fc | 297 | const oldState = task.getAttribute("data-state"); |
da9a2716 | 298 | if (newState === oldState) return; |
5350da9f | 299 | const createTimestamp = task.getAttribute("data-created")!; |
01f41859 | 300 | this.moveCursor(1) || this.moveCursor(-1); |
da9a2716 | 301 | return UI.setState(createTimestamp, newState, oldState); |
01f41859 | 302 | }, |
43f3cc0c SW |
303 | |
304 | undo: function () { | |
305 | const ret = UI.undo(); | |
306 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
307 | }, | |
09657615 | 308 | }; |
06ee32a1 | 309 | |
e94e9f27 SW |
310 | enum InputState { |
311 | Command, | |
854992ec | 312 | View, |
e94e9f27 SW |
313 | } |
314 | var inputState = InputState.Command; | |
315 | ||
f1afad9b | 316 | function handleKey(event: any) { |
a26b1f4b | 317 | if (event.target.tagName === "INPUT") { |
7b574407 SW |
318 | if (event.target.id === "taskName") { |
319 | if (event.key == "Enter") return BrowserUI.addTask(event); | |
320 | } else { | |
321 | if (event.key == "Enter") return BrowserUI.completeEdit(event); | |
322 | } | |
a26b1f4b | 323 | } else { |
e94e9f27 SW |
324 | if (inputState === InputState.Command) { |
325 | if (event.key == "j") return BrowserUI.moveCursor(1); | |
326 | if (event.key == "k") return BrowserUI.moveCursor(-1); | |
327 | if (event.key == "J") return BrowserUI.moveTask(1); | |
328 | if (event.key == "K") return BrowserUI.moveTask(-1); | |
329 | if (event.key == "n") return BrowserUI.focusTaskNameInput(event); | |
330 | if (event.key == "s") return BrowserUI.setState("someday-maybe"); | |
331 | if (event.key == "w") return BrowserUI.setState("waiting"); | |
332 | if (event.key == "d") return BrowserUI.setState("done"); | |
333 | if (event.key == "c") return BrowserUI.setState("cancelled"); | |
334 | if (event.key == "t") return BrowserUI.setState("todo"); | |
335 | if (event.key == "X") return BrowserUI.setState("deleted"); | |
336 | if (event.key == "u") return BrowserUI.undo(); | |
337 | if (event.key == "e") return BrowserUI.beginEdit(event); | |
854992ec SW |
338 | if (event.key == "v") return (inputState = InputState.View); |
339 | } else if (inputState === InputState.View) { | |
340 | return (inputState = InputState.Command); | |
e94e9f27 | 341 | } |
f1afad9b SW |
342 | } |
343 | } | |
344 | ||
f1afad9b SW |
345 | function browserInit() { |
346 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
d03daa19 | 347 | log.replay(); |
65a7510d | 348 | BrowserUI.firstVisibleTask()?.focus(); |
f1afad9b | 349 | } |