]>
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 | ||
c70b3eed SW |
32 | // Returns a promise for a hue based on a hash of the string |
33 | function hashHue(str: string) { | |
34 | // Using crypto for this is overkill | |
35 | return crypto.subtle.digest("SHA-256", new TextEncoder().encode(str)).then((buf) => (new Uint16Array(buf)[0] * 360) / 2 ** 16); | |
36 | } | |
37 | ||
13c97b99 | 38 | const Model = { |
6d01c406 | 39 | addTask: function (timestamp: string, description: string): Element { |
13c97b99 | 40 | const task = document.createElement("div"); |
26737687 SW |
41 | const desc = document.createElement("span"); |
42 | desc.textContent = description; | |
43 | desc.classList.add("desc"); | |
44 | task.appendChild(desc); | |
7ccc80f6 | 45 | task.classList.add("task"); |
13c97b99 | 46 | task.setAttribute("tabindex", "0"); |
4101e1b1 | 47 | task.setAttribute("data-created", timestamp); |
682139fc | 48 | task.setAttribute("data-state", "todo"); |
88bd89ef SW |
49 | const tasks = document.getElementById("tasks")!; |
50 | tasks.insertBefore(task, tasks.firstElementChild); | |
6d01c406 | 51 | return task; |
13c97b99 | 52 | }, |
974848d3 | 53 | |
7b5b90b9 SW |
54 | addTag: function (createTimestamp: string, tagName: string): Element | null { |
55 | const task = this.getTask(createTimestamp); | |
56 | if (!task) return null; | |
3916a89c SW |
57 | const existingTag = this.hasTag(task, tagName); |
58 | if (existingTag) return existingTag; | |
7b5b90b9 SW |
59 | const tag = document.createElement("span"); |
60 | tag.appendChild(document.createTextNode(tagName)); | |
61 | tag.classList.add("tag"); | |
62 | tag.setAttribute("tabindex", "0"); | |
c70b3eed | 63 | hashHue(tagName).then((hue) => (tag.style.backgroundColor = `hsl(${hue},90%,45%)`)); |
360beccb SW |
64 | for (const child of task.getElementsByClassName("tag")) { |
65 | if (tagName > child.textContent!) { | |
66 | task.insertBefore(tag, child); | |
67 | return tag; | |
68 | } | |
69 | } | |
8e0dd97e | 70 | task.insertBefore(tag, task.getElementsByClassName("desc")[0]!); |
7b5b90b9 SW |
71 | return tag; |
72 | }, | |
73 | ||
7b574407 SW |
74 | edit: function (createTimestamp: string, newDescription: string): Element | null { |
75 | const target = this.getTask(createTimestamp); | |
76 | if (!target) return null; | |
77 | if (target.hasAttribute("data-description")) { | |
78 | // Oh no: An edit has arrived from a replica while a local edit is in progress. | |
d1d7beaf | 79 | const input = target.getElementsByTagName("input")[0]!; |
7b574407 SW |
80 | if ( |
81 | input.value === target.getAttribute("data-description") && | |
3a731557 | 82 | input.selectionStart === input.value.length && |
7b574407 SW |
83 | input.selectionEnd === input.value.length |
84 | ) { | |
85 | // No local changes have actually been made yet. Change the contents of the edit box! | |
86 | input.value = newDescription; | |
7b574407 SW |
87 | } else { |
88 | // No great options. | |
89 | // Prefer not to interrupt the local user's edit. | |
90 | // The remote edit is mostly lost; this mostly becomes last-write-wins. | |
91 | target.setAttribute("data-description", newDescription); | |
92 | } | |
93 | } else { | |
26737687 | 94 | target.getElementsByClassName("desc")[0].textContent = newDescription; |
7b574407 SW |
95 | } |
96 | return target; | |
97 | }, | |
98 | ||
3916a89c | 99 | hasTag: function (task: Element, tag: string): Element | null { |
54c19180 SW |
100 | for (const child of task.getElementsByClassName("tag")) { |
101 | if (child.textContent === tag) { | |
3916a89c | 102 | return child; |
e1eb33ad SW |
103 | } |
104 | } | |
3916a89c | 105 | return null; |
e1eb33ad SW |
106 | }, |
107 | ||
68a72fde SW |
108 | getPriority: function (task: Element): number { |
109 | if (task.hasAttribute("data-priority")) { | |
110 | return parseFloat(task.getAttribute("data-priority")!); | |
111 | } | |
112 | return parseFloat(task.getAttribute("data-created")!); | |
113 | }, | |
114 | ||
799f4e89 SW |
115 | getTask: function (createTimestamp: string) { |
116 | for (const task of document.getElementsByClassName("task")) { | |
117 | if (task.getAttribute("data-created") === createTimestamp) { | |
118 | return task; | |
119 | } | |
120 | } | |
121 | }, | |
122 | ||
1804fd5a SW |
123 | insertInPriorityOrder: function (task: Element, dest: Element) { |
124 | const priority = this.getPriority(task); | |
125 | for (const t of dest.children) { | |
126 | if (t !== task && this.getPriority(t) < priority) { | |
127 | dest.insertBefore(task, t); | |
128 | return; | |
129 | } | |
130 | } | |
131 | dest.appendChild(task); | |
132 | }, | |
133 | ||
0726872b SW |
134 | removeTag: function (createTimestamp: string, tagName: string) { |
135 | const task = this.getTask(createTimestamp); | |
136 | if (!task) return null; | |
137 | const tag = this.hasTag(task, tagName); | |
138 | if (!tag) return; | |
139 | task.removeChild(tag); | |
b5f15e0e | 140 | if (task instanceof HTMLElement) task.focus(); |
0726872b SW |
141 | }, |
142 | ||
43f3cc0c | 143 | setPriority: function (createTimestamp: string, priority: number): Element | null { |
68a72fde | 144 | const target = this.getTask(createTimestamp); |
43f3cc0c | 145 | if (!target) return null; |
68a72fde | 146 | target.setAttribute("data-priority", `${priority}`); |
1804fd5a | 147 | this.insertInPriorityOrder(target, target.parentElement!); |
43f3cc0c | 148 | return target; |
68a72fde SW |
149 | }, |
150 | ||
01f41859 SW |
151 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { |
152 | const task = this.getTask(createTimestamp); | |
b5ac5cc3 SW |
153 | if (!task) return; |
154 | task.setAttribute("data-state", state); | |
155 | var date = task.getElementsByClassName("statedate")[0]; | |
156 | if (state === "todo") { | |
157 | task.removeChild(date); | |
158 | return; | |
01f41859 | 159 | } |
b5ac5cc3 SW |
160 | if (!date) { |
161 | date = document.createElement("span"); | |
162 | date.classList.add("statedate"); | |
163 | task.insertBefore(date, task.firstChild); | |
164 | } | |
165 | const d = new Date(parseInt(stateTimestamp)); | |
166 | date.textContent = `${d.getFullYear()}-${`${d.getMonth() + 1}`.padStart(2, "0")}-${`${d.getDate()}`.padStart(2, "0")}`; | |
799f4e89 | 167 | }, |
13c97b99 | 168 | }; |
f1afad9b | 169 | |
d03daa19 | 170 | function Log(prefix: string = "vp-") { |
60a63831 SW |
171 | var next_log_index = 0; |
172 | return { | |
e88c099c | 173 | apply: function (entry: string) { |
60a63831 SW |
174 | const [timestamp, command, data] = splitN(entry, " ", 2); |
175 | if (command == "Create") { | |
6d01c406 | 176 | return Model.addTask(timestamp, data); |
60a63831 | 177 | } |
7b574407 SW |
178 | if (command == "Edit") { |
179 | const [createTimestamp, description] = splitN(data, " ", 1); | |
180 | return Model.edit(createTimestamp, description); | |
181 | } | |
68a72fde SW |
182 | if (command == "Priority") { |
183 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 184 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 185 | } |
6a5644f3 SW |
186 | if (command == "State") { |
187 | const [createTimestamp, state] = splitN(data, " ", 1); | |
188 | return Model.setState(timestamp, createTimestamp, state); | |
189 | } | |
7b5b90b9 SW |
190 | if (command == "Tag") { |
191 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
192 | return Model.addTag(createTimestamp, tag); | |
193 | } | |
0726872b SW |
194 | if (command == "Untag") { |
195 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
196 | return Model.removeTag(createTimestamp, tag); | |
197 | } | |
60a63831 SW |
198 | }, |
199 | ||
e88c099c | 200 | record: function (entry: string) { |
d03daa19 | 201 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
202 | }, |
203 | ||
e88c099c SW |
204 | recordAndApply: function (entry: string) { |
205 | this.record(entry); | |
6d01c406 | 206 | return this.apply(entry); |
60a63831 SW |
207 | }, |
208 | ||
209 | replay: function () { | |
210 | while (true) { | |
d03daa19 | 211 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
212 | if (entry === null) { |
213 | break; | |
214 | } | |
e88c099c | 215 | this.apply(entry); |
60a63831 SW |
216 | next_log_index++; |
217 | } | |
218 | }, | |
219 | }; | |
d03daa19 SW |
220 | } |
221 | const log = Log(); | |
262705dd | 222 | |
b56a37d3 | 223 | function UI() { |
0d1c27a8 SW |
224 | const undoLog: string[][] = []; |
225 | const redoLog: string[][] = []; | |
76825ecd | 226 | function perform(forward: string, reverse: string) { |
0d1c27a8 | 227 | undoLog.push([reverse, forward]); |
76825ecd SW |
228 | return log.recordAndApply(`${clock.now()} ${forward}`); |
229 | } | |
b56a37d3 SW |
230 | return { |
231 | addTask: function (description: string): Element { | |
232 | const now = clock.now(); | |
0d1c27a8 | 233 | undoLog.push([`State ${now} deleted`, `State ${now} todo`]); |
b56a37d3 SW |
234 | return <Element>log.recordAndApply(`${now} Create ${description}`); |
235 | }, | |
7b5b90b9 | 236 | addTag: function (createTimestamp: string, tag: string) { |
76825ecd | 237 | return perform(`Tag ${createTimestamp} ${tag}`, `Untag ${createTimestamp} ${tag}`); |
7b5b90b9 | 238 | }, |
b56a37d3 | 239 | edit: function (createTimestamp: string, newDescription: string, oldDescription: string) { |
76825ecd | 240 | return perform(`Edit ${createTimestamp} ${newDescription}`, `Edit ${createTimestamp} ${oldDescription}`); |
b56a37d3 | 241 | }, |
b5f15e0e | 242 | removeTag: function (createTimestamp: string, tag: string) { |
76825ecd | 243 | return perform(`Untag ${createTimestamp} ${tag}`, `Tag ${createTimestamp} ${tag}`); |
b5f15e0e | 244 | }, |
b56a37d3 | 245 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { |
76825ecd | 246 | return perform(`Priority ${createTimestamp} ${newPriority}`, `Priority ${createTimestamp} ${oldPriority}`); |
b56a37d3 SW |
247 | }, |
248 | setState: function (createTimestamp: string, newState: string, oldState: string) { | |
76825ecd | 249 | return perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`); |
b56a37d3 SW |
250 | }, |
251 | undo: function () { | |
0d1c27a8 SW |
252 | const entry = undoLog.pop(); |
253 | if (entry) { | |
254 | redoLog.push(entry); | |
255 | return log.recordAndApply(`${clock.now()} ${entry[0]}`); | |
256 | } | |
257 | }, | |
258 | redo: function () { | |
259 | const entry = redoLog.pop(); | |
260 | if (entry) { | |
261 | undoLog.push(entry); | |
262 | return log.recordAndApply(`${clock.now()} ${entry[1]}`); | |
b56a37d3 SW |
263 | } |
264 | }, | |
265 | }; | |
266 | } | |
267 | const ui = UI(); | |
e88c099c | 268 | |
ad72cd51 SW |
269 | enum CommitOrAbort { |
270 | Commit, | |
271 | Abort, | |
272 | } | |
273 | ||
ada060d7 | 274 | function BrowserUI() { |
c2226333 SW |
275 | const viewColors: { [key: string]: string } = { |
276 | all: "Gold", | |
277 | cancelled: "Red", | |
278 | deleted: "Black", | |
279 | done: "LawnGreen", | |
280 | "someday-maybe": "DeepSkyBlue", | |
281 | todo: "White", | |
282 | waiting: "MediumOrchid", | |
283 | }; | |
c948f3b4 | 284 | var currentTagView: string | null = null; |
868667c1 | 285 | var currentViewState = "todo"; |
a59fbe41 | 286 | var taskFocusedBeforeJumpingToInput: HTMLElement | null = null; |
09cd65ad | 287 | var lastTagNameEntered = ""; |
ada060d7 SW |
288 | return { |
289 | addTask: function (event: KeyboardEvent) { | |
290 | const input = <HTMLInputElement>document.getElementById("taskName"); | |
fb19ac80 SW |
291 | if (input.value.match(/^ *$/)) return; |
292 | const task = ui.addTask(input.value); | |
cddbdce1 | 293 | if (currentViewState === "todo" || currentViewState === "all") { |
fb19ac80 SW |
294 | task instanceof HTMLElement && task.focus(); |
295 | } else if (this.returnFocusAfterInput()) { | |
296 | } else { | |
297 | this.firstVisibleTask()?.focus(); | |
298 | } | |
299 | input.value = ""; | |
c948f3b4 SW |
300 | if (currentTagView) { |
301 | ui.addTag(task.getAttribute("data-created")!, currentTagView); | |
302 | } | |
fb19ac80 | 303 | if (event.getModifierState("Control")) { |
88bd89ef | 304 | this.makeBottomPriority(task); |
bc7996fe | 305 | } |
ada060d7 | 306 | }, |
09657615 | 307 | |
ada060d7 | 308 | beginEdit: function (event: Event) { |
32808c9a | 309 | const task = this.currentTask(); |
ada060d7 SW |
310 | if (!task) return; |
311 | const input = document.createElement("input"); | |
26737687 SW |
312 | const desc = task.getElementsByClassName("desc")[0]; |
313 | const oldDescription = desc.textContent!; | |
ada060d7 SW |
314 | task.setAttribute("data-description", oldDescription); |
315 | input.value = oldDescription; | |
316 | input.addEventListener("blur", this.completeEdit, { once: true }); | |
26737687 | 317 | desc.textContent = ""; |
7b5b90b9 SW |
318 | task.insertBefore(input, task.firstChild); |
319 | input.focus(); | |
320 | event.preventDefault(); | |
321 | }, | |
322 | ||
323 | beginTagEdit: function (event: Event) { | |
32808c9a | 324 | const task = this.currentTask(); |
7b5b90b9 SW |
325 | if (!task) return; |
326 | const input = document.createElement("input"); | |
327 | input.classList.add("tag"); | |
328 | input.addEventListener("blur", this.completeTagEdit, { once: true }); | |
09cd65ad | 329 | input.value = lastTagNameEntered; |
ada060d7 SW |
330 | task.appendChild(input); |
331 | input.focus(); | |
09cd65ad | 332 | input.select(); |
ada060d7 SW |
333 | event.preventDefault(); |
334 | }, | |
7b574407 | 335 | |
ad72cd51 | 336 | completeEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { |
ada060d7 SW |
337 | const input = event.target as HTMLInputElement; |
338 | const task = input.parentElement!; | |
26737687 | 339 | const desc = task.getElementsByClassName("desc")[0]; |
ada060d7 SW |
340 | const oldDescription = task.getAttribute("data-description")!; |
341 | const newDescription = input.value; | |
342 | input.removeEventListener("blur", this.completeEdit); | |
132921e6 | 343 | task.removeChild(input); |
ada060d7 SW |
344 | task.removeAttribute("data-description"); |
345 | task.focus(); | |
fb19ac80 | 346 | if (resolution === CommitOrAbort.Abort || newDescription.match(/^ *$/) || newDescription === oldDescription) { |
26737687 | 347 | desc.textContent = oldDescription; |
ada060d7 | 348 | } else { |
b56a37d3 | 349 | ui.edit(task.getAttribute("data-created")!, newDescription, oldDescription); |
ada060d7 SW |
350 | } |
351 | }, | |
7b574407 | 352 | |
7b5b90b9 SW |
353 | completeTagEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { |
354 | const input = event.target as HTMLInputElement; | |
355 | const task = input.parentElement!; | |
356 | const newTagName = input.value; | |
357 | input.removeEventListener("blur", this.completeTagEdit); | |
358 | task.removeChild(input); | |
359 | task.focus(); | |
fb19ac80 | 360 | if (resolution === CommitOrAbort.Commit && !newTagName.match(/^ *$/) && !Model.hasTag(task, newTagName)) { |
e1eb33ad | 361 | ui.addTag(task.getAttribute("data-created")!, newTagName); |
09cd65ad | 362 | lastTagNameEntered = newTagName; |
e1eb33ad | 363 | } |
7b5b90b9 SW |
364 | }, |
365 | ||
5800003c SW |
366 | currentTag: function (): Element | null { |
367 | var target = document.activeElement; | |
368 | if (!target) return null; | |
369 | if (target.classList.contains("task")) { | |
370 | const tags = target.getElementsByClassName("tag"); | |
371 | target = tags[tags.length - 1]; | |
372 | } | |
373 | if (!target || !target.classList.contains("tag")) return null; | |
374 | return target; | |
375 | }, | |
376 | ||
32808c9a SW |
377 | currentTask: function (): HTMLElement | null { |
378 | var target = document.activeElement; | |
379 | if (!target) return null; | |
380 | if (target.classList.contains("tag")) target = target.parentElement!; | |
381 | return target as HTMLElement; | |
382 | }, | |
383 | ||
ada060d7 SW |
384 | firstVisibleTask: function () { |
385 | for (const task of document.getElementsByClassName("task")) { | |
cddbdce1 | 386 | const state = task.getAttribute("data-state"); |
312acaa8 SW |
387 | if ( |
388 | task instanceof HTMLElement && | |
389 | (state === currentViewState || (currentViewState === "all" && state !== "deleted")) && | |
390 | !task.classList.contains("hide") | |
391 | ) { | |
ada060d7 SW |
392 | return task; |
393 | } | |
65a7510d | 394 | } |
ada060d7 | 395 | }, |
caa93fd1 | 396 | |
ada060d7 | 397 | focusTaskNameInput: function (event: Event) { |
32808c9a | 398 | taskFocusedBeforeJumpingToInput = this.currentTask(); |
ada060d7 | 399 | document.getElementById("taskName")!.focus(); |
a1aa43d8 | 400 | window.scroll(0, 0); |
ada060d7 SW |
401 | event.preventDefault(); |
402 | }, | |
09657615 | 403 | |
ada060d7 SW |
404 | visibleTaskAtOffset(task: Element, offset: number): Element { |
405 | var cursor: Element | null = task; | |
406 | var valid_cursor = cursor; | |
407 | const increment = offset / Math.abs(offset); | |
408 | while (true) { | |
409 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
410 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
cddbdce1 | 411 | const state = cursor.getAttribute("data-state")!; |
312acaa8 SW |
412 | if ( |
413 | (state === currentViewState || (currentViewState === "all" && state !== "deleted")) && | |
414 | !cursor.classList.contains("hide") | |
415 | ) { | |
ada060d7 SW |
416 | offset -= increment; |
417 | valid_cursor = cursor; | |
418 | } | |
419 | if (Math.abs(offset) < 0.5) break; | |
5fa4704c | 420 | } |
ada060d7 SW |
421 | return valid_cursor; |
422 | }, | |
23be73e3 | 423 | |
40025d12 SW |
424 | jumpCursor: function (position: number) { |
425 | const first = this.firstVisibleTask(); | |
426 | if (!first) return; | |
427 | const dest = this.visibleTaskAtOffset(first, position - 1); | |
428 | if (dest instanceof HTMLElement) dest.focus(); | |
429 | }, | |
430 | ||
88bd89ef | 431 | makeBottomPriority: function (task: Element | null = null) { |
32808c9a | 432 | if (!task) task = this.currentTask(); |
88bd89ef SW |
433 | if (!task) return; |
434 | this.setPriority(task, document.getElementById("tasks")!.lastElementChild, null); | |
435 | }, | |
436 | ||
55a4baa8 | 437 | makeTopPriority: function (task: Element | null = null) { |
32808c9a | 438 | if (!task) task = this.currentTask(); |
55a4baa8 | 439 | if (!task) return; |
88bd89ef SW |
440 | ui.setPriority(task.getAttribute("data-created")!, clock.now(), Model.getPriority(task)); |
441 | task instanceof HTMLElement && task.focus(); | |
792b8a18 SW |
442 | }, |
443 | ||
ada060d7 | 444 | moveCursor: function (offset: number): boolean { |
32808c9a | 445 | const active = this.currentTask(); |
ada060d7 SW |
446 | if (!active) return false; |
447 | const dest = this.visibleTaskAtOffset(active, offset); | |
448 | if (dest !== active && dest instanceof HTMLElement) { | |
449 | dest.focus(); | |
450 | return true; | |
451 | } | |
452 | return false; | |
453 | }, | |
01f41859 | 454 | |
ada060d7 | 455 | moveTask: function (offset: number) { |
32808c9a | 456 | const active = this.currentTask(); |
ada060d7 SW |
457 | if (!active) return; |
458 | const dest = this.visibleTaskAtOffset(active, offset); | |
459 | if (dest === active) return; // Already extremal | |
460 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
461 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
462 | if (offset > 0) { | |
463 | this.setPriority(active, dest, onePastDest); | |
464 | } else { | |
465 | this.setPriority(active, onePastDest, dest); | |
466 | } | |
467 | }, | |
68a72fde | 468 | |
b5f15e0e | 469 | removeTag: function () { |
5800003c | 470 | const target = this.currentTag(); |
4ccaa1d6 | 471 | if (!target) return; |
4ccaa1d6 | 472 | ui.removeTag(target.parentElement!.getAttribute("data-created")!, target.textContent!); |
b5f15e0e SW |
473 | }, |
474 | ||
312acaa8 | 475 | resetTagView: function () { |
c948f3b4 | 476 | currentTagView = null; |
3a164930 SW |
477 | const taskList = document.getElementById("tasks")!; |
478 | for (const task of Array.from(document.getElementsByClassName("task"))) { | |
312acaa8 | 479 | task.classList.remove("hide"); |
3a164930 SW |
480 | if (task.parentElement !== taskList) { |
481 | Model.insertInPriorityOrder(task, taskList); | |
482 | } | |
312acaa8 SW |
483 | } |
484 | }, | |
485 | ||
58b569ce SW |
486 | resetView: function () { |
487 | this.setView("todo"); | |
312acaa8 | 488 | this.resetTagView(); |
58b569ce SW |
489 | }, |
490 | ||
a59fbe41 SW |
491 | returnFocusAfterInput: function (): boolean { |
492 | if (taskFocusedBeforeJumpingToInput) { | |
493 | taskFocusedBeforeJumpingToInput.focus(); | |
494 | return true; | |
495 | } | |
496 | return false; | |
497 | }, | |
498 | ||
ada060d7 SW |
499 | // Change task's priority to be between other tasks a and b. |
500 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
88bd89ef SW |
501 | const aPriority = a === null ? clock.now() : Model.getPriority(a); |
502 | const bPriority = b === null ? 0 : Model.getPriority(b); | |
503 | console.assert(aPriority > bPriority, aPriority, ">", bPriority); | |
504 | const span = aPriority - bPriority; | |
505 | const newPriority = bPriority + 0.1 * span + 0.8 * span * Math.random(); | |
506 | console.assert(aPriority > newPriority && newPriority > bPriority, aPriority, ">", newPriority, ">", bPriority); | |
ada060d7 | 507 | const newPriorityRounded = Math.round(newPriority); |
88bd89ef | 508 | const okToRound = aPriority > newPriorityRounded && newPriorityRounded > bPriority; |
b56a37d3 | 509 | ui.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task)); |
ada060d7 SW |
510 | task instanceof HTMLElement && task.focus(); |
511 | }, | |
68a72fde | 512 | |
ada060d7 | 513 | setState: function (newState: string) { |
32808c9a | 514 | const task = this.currentTask(); |
ada060d7 SW |
515 | if (!task) return; |
516 | const oldState = task.getAttribute("data-state")!; | |
517 | if (newState === oldState) return; | |
518 | const createTimestamp = task.getAttribute("data-created")!; | |
cddbdce1 SW |
519 | if (currentViewState !== "all" || newState == "deleted") { |
520 | this.moveCursor(1) || this.moveCursor(-1); | |
521 | } | |
b56a37d3 | 522 | return ui.setState(createTimestamp, newState, oldState); |
ada060d7 | 523 | }, |
43f3cc0c | 524 | |
68d69314 SW |
525 | setTagView: function (tag: string | null = null) { |
526 | if (tag === null) { | |
527 | const target = this.currentTag(); | |
528 | if (!target) return; | |
529 | tag = target.textContent!; | |
530 | } | |
3a164930 SW |
531 | |
532 | if (currentTagView !== null) { | |
533 | this.resetTagView(); | |
534 | } | |
535 | ||
536 | const tasksWithTag = new Map(); | |
312acaa8 | 537 | for (const task of document.getElementsByClassName("task")) { |
3a164930 SW |
538 | if (Model.hasTag(task, tag)) { |
539 | tasksWithTag.set(task.getElementsByClassName("desc")[0].textContent, [Model.getPriority(task), task]); | |
540 | } | |
541 | } | |
542 | ||
543 | function highestPrioritySuperTask(t: Element) { | |
544 | var maxPriority = -1; | |
545 | var superTask = null; | |
546 | for (const child of t.getElementsByClassName("tag")) { | |
547 | const e = tasksWithTag.get(child.textContent); | |
548 | if (e !== undefined && e[0] > maxPriority) { | |
549 | maxPriority = e[0]; | |
550 | superTask = e[1]; | |
551 | } | |
552 | } | |
553 | return superTask; | |
554 | } | |
555 | ||
556 | for (const task of Array.from(document.getElementsByClassName("task"))) { | |
312acaa8 SW |
557 | if (Model.hasTag(task, tag)) { |
558 | task.classList.remove("hide"); | |
559 | } else { | |
3a164930 SW |
560 | const superTask = highestPrioritySuperTask(task); |
561 | if (superTask !== null) { | |
562 | Model.insertInPriorityOrder(task, superTask); | |
563 | } else { | |
564 | task.classList.add("hide"); | |
565 | } | |
312acaa8 SW |
566 | } |
567 | } | |
3a164930 | 568 | |
c948f3b4 | 569 | currentTagView = tag; |
312acaa8 SW |
570 | }, |
571 | ||
c2226333 | 572 | setView: function (state: string) { |
868667c1 | 573 | const sheet = (document.getElementById("viewStyle") as HTMLStyleElement).sheet!; |
cddbdce1 SW |
574 | if (state === "all") { |
575 | sheet.insertRule(`.task[data-state=deleted] { display: none }`); | |
576 | } else { | |
577 | sheet.insertRule(`.task:not([data-state=${state}]) { display: none }`); | |
578 | } | |
c2226333 | 579 | sheet.insertRule(`:root { --view-state-indicator-color: ${viewColors[state]}; }`); |
4c532769 SW |
580 | sheet.removeRule(2); |
581 | sheet.removeRule(2); | |
868667c1 | 582 | currentViewState = state; |
32808c9a | 583 | if (this.currentTask()?.getAttribute("data-state") !== state) { |
868667c1 SW |
584 | this.firstVisibleTask()?.focus(); |
585 | } | |
586 | }, | |
587 | ||
84849dfa | 588 | setUntaggedView: function () { |
3a164930 SW |
589 | if (currentTagView !== null) { |
590 | this.resetTagView(); | |
591 | } | |
84849dfa SW |
592 | for (const task of document.getElementsByClassName("task")) { |
593 | if (task.getElementsByClassName("tag").length === 0) { | |
594 | task.classList.remove("hide"); | |
595 | } else { | |
596 | task.classList.add("hide"); | |
597 | } | |
598 | } | |
599 | }, | |
600 | ||
ada060d7 | 601 | undo: function () { |
b56a37d3 | 602 | const ret = ui.undo(); |
ada060d7 SW |
603 | if (ret && ret instanceof HTMLElement) ret.focus(); |
604 | }, | |
0d1c27a8 SW |
605 | redo: function () { |
606 | const ret = ui.redo(); | |
607 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
608 | }, | |
ada060d7 SW |
609 | }; |
610 | } | |
611 | const browserUI = BrowserUI(); | |
06ee32a1 | 612 | |
90381b6d | 613 | const scrollIncrement = 60; |
e94e9f27 | 614 | enum InputState { |
02c8a409 | 615 | Root, |
36ddfad1 | 616 | S, |
02c8a409 | 617 | V, |
36ddfad1 | 618 | VS, |
e94e9f27 | 619 | } |
02c8a409 | 620 | var inputState = InputState.Root; |
36fa06f4 | 621 | var inputCount: number | null = null; |
e94e9f27 | 622 | |
f1afad9b | 623 | function handleKey(event: any) { |
f1d8d0ed | 624 | if (["Alt", "Control", "Meta", "Shift"].includes(event.key)) return; |
a26b1f4b | 625 | if (event.target.tagName === "INPUT") { |
7b574407 | 626 | if (event.target.id === "taskName") { |
ada060d7 | 627 | if (event.key == "Enter") return browserUI.addTask(event); |
a59fbe41 | 628 | if (event.key == "Escape") return browserUI.returnFocusAfterInput(); |
7b5b90b9 SW |
629 | } else if (event.target.classList.contains("tag")) { |
630 | if (event.key == "Enter") return browserUI.completeTagEdit(event); | |
631 | if (event.key == "Escape") return browserUI.completeTagEdit(event, CommitOrAbort.Abort); | |
7b574407 | 632 | } else { |
ada060d7 | 633 | if (event.key == "Enter") return browserUI.completeEdit(event); |
ad72cd51 | 634 | if (event.key == "Escape") return browserUI.completeEdit(event, CommitOrAbort.Abort); |
7b574407 | 635 | } |
a26b1f4b | 636 | } else { |
02c8a409 | 637 | if (inputState === InputState.Root) { |
36fa06f4 SW |
638 | if ("0" <= event.key && event.key <= "9") { |
639 | return (inputCount = (inputCount ?? 0) * 10 + parseInt(event.key)); | |
640 | } | |
641 | try { | |
90381b6d SW |
642 | if (event.ctrlKey) { |
643 | if (event.key == "e") return window.scrollBy(0, (inputCount ?? 1) * scrollIncrement); | |
644 | if (event.key == "y") return window.scrollBy(0, (inputCount ?? 1) * -scrollIncrement); | |
645 | } else { | |
646 | if (event.key == "j") return browserUI.moveCursor(inputCount ?? 1); | |
647 | if (event.key == "k") return browserUI.moveCursor(-(inputCount ?? 1)); | |
648 | if (event.key == "J") return browserUI.moveTask(inputCount ?? 1); | |
649 | if (event.key == "K") return browserUI.moveTask(-(inputCount ?? 1)); | |
650 | if (event.key == "G") return browserUI.jumpCursor(inputCount ?? MAX_SAFE_INTEGER); | |
651 | if (event.key == "T") return browserUI.makeTopPriority(); | |
652 | if (event.key == "n") return browserUI.focusTaskNameInput(event); | |
653 | if (event.key == "c") return browserUI.setState("cancelled"); | |
654 | if (event.key == "d") return browserUI.setState("done"); | |
655 | if (event.key == "q") return browserUI.setState("todo"); | |
656 | if (event.key == "s") return (inputState = InputState.S); | |
657 | if (event.key == "w") return browserUI.setState("waiting"); | |
658 | if (event.key == "X") return browserUI.setState("deleted"); | |
659 | if (event.key == "x") return browserUI.removeTag(); | |
660 | if (event.key == "u") return browserUI.undo(); | |
661 | if (event.key == "r") return browserUI.redo(); | |
662 | if (event.key == "e") return browserUI.beginEdit(event); | |
663 | if (event.key == "t") return browserUI.beginTagEdit(event); | |
664 | if (event.key == "v") return (inputState = InputState.V); | |
665 | } | |
36fa06f4 SW |
666 | } finally { |
667 | inputCount = null; | |
668 | } | |
36ddfad1 SW |
669 | } else if (inputState === InputState.S) { |
670 | inputState = InputState.Root; | |
671 | if (event.key == "m") return browserUI.setState("someday-maybe"); | |
02c8a409 SW |
672 | } else if (inputState === InputState.V) { |
673 | inputState = InputState.Root; | |
c2226333 SW |
674 | if (event.key == "a") return browserUI.setView("all"); |
675 | if (event.key == "c") return browserUI.setView("cancelled"); | |
676 | if (event.key == "d") return browserUI.setView("done"); | |
84849dfa | 677 | if (event.key == "i") return browserUI.setUntaggedView(); |
68d69314 | 678 | if (event.key == "p") return browserUI.setTagView("Project"); |
c2226333 | 679 | if (event.key == "q") return browserUI.setView("todo"); |
36ddfad1 | 680 | if (event.key == "s") return (inputState = InputState.VS); |
312acaa8 SW |
681 | if (event.key == "T") return browserUI.resetTagView(); |
682 | if (event.key == "t") return browserUI.setTagView(); | |
84849dfa | 683 | if (event.key == "u") return browserUI.setUntaggedView(); |
58b569ce | 684 | if (event.key == "v") return browserUI.resetView(); |
c2226333 SW |
685 | if (event.key == "w") return browserUI.setView("waiting"); |
686 | if (event.key == "x") return browserUI.setView("deleted"); | |
36ddfad1 SW |
687 | } else if (inputState === InputState.VS) { |
688 | inputState = InputState.Root; | |
c2226333 | 689 | if (event.key == "m") return browserUI.setView("someday-maybe"); |
e94e9f27 | 690 | } |
f1afad9b SW |
691 | } |
692 | } | |
693 | ||
f1afad9b | 694 | function browserInit() { |
d03daa19 | 695 | log.replay(); |
ada060d7 | 696 | browserUI.firstVisibleTask()?.focus(); |
bd267c29 | 697 | document.body.addEventListener("keydown", handleKey, { capture: false }); |
f1afad9b | 698 | } |