]>
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 | } | |
7b5b90b9 SW |
70 | task.appendChild(tag); |
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. | |
132921e6 | 79 | const input = target.firstChild as HTMLInputElement; |
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 | ||
0726872b SW |
123 | removeTag: function (createTimestamp: string, tagName: string) { |
124 | const task = this.getTask(createTimestamp); | |
125 | if (!task) return null; | |
126 | const tag = this.hasTag(task, tagName); | |
127 | if (!tag) return; | |
128 | task.removeChild(tag); | |
b5f15e0e | 129 | if (task instanceof HTMLElement) task.focus(); |
0726872b SW |
130 | }, |
131 | ||
43f3cc0c | 132 | setPriority: function (createTimestamp: string, priority: number): Element | null { |
68a72fde | 133 | const target = this.getTask(createTimestamp); |
43f3cc0c | 134 | if (!target) return null; |
68a72fde SW |
135 | target.setAttribute("data-priority", `${priority}`); |
136 | for (const task of document.getElementsByClassName("task")) { | |
88bd89ef | 137 | if (task !== target && this.getPriority(task) < priority) { |
68a72fde | 138 | task.parentElement!.insertBefore(target, task); |
43f3cc0c | 139 | return target; |
68a72fde SW |
140 | } |
141 | } | |
142 | document.getElementById("tasks")!.appendChild(target); | |
43f3cc0c | 143 | return target; |
68a72fde SW |
144 | }, |
145 | ||
01f41859 SW |
146 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { |
147 | const task = this.getTask(createTimestamp); | |
b5ac5cc3 SW |
148 | if (!task) return; |
149 | task.setAttribute("data-state", state); | |
150 | var date = task.getElementsByClassName("statedate")[0]; | |
151 | if (state === "todo") { | |
152 | task.removeChild(date); | |
153 | return; | |
01f41859 | 154 | } |
b5ac5cc3 SW |
155 | if (!date) { |
156 | date = document.createElement("span"); | |
157 | date.classList.add("statedate"); | |
158 | task.insertBefore(date, task.firstChild); | |
159 | } | |
160 | const d = new Date(parseInt(stateTimestamp)); | |
161 | date.textContent = `${d.getFullYear()}-${`${d.getMonth() + 1}`.padStart(2, "0")}-${`${d.getDate()}`.padStart(2, "0")}`; | |
799f4e89 | 162 | }, |
13c97b99 | 163 | }; |
f1afad9b | 164 | |
d03daa19 | 165 | function Log(prefix: string = "vp-") { |
60a63831 SW |
166 | var next_log_index = 0; |
167 | return { | |
e88c099c | 168 | apply: function (entry: string) { |
60a63831 SW |
169 | const [timestamp, command, data] = splitN(entry, " ", 2); |
170 | if (command == "Create") { | |
6d01c406 | 171 | return Model.addTask(timestamp, data); |
60a63831 | 172 | } |
7b574407 SW |
173 | if (command == "Edit") { |
174 | const [createTimestamp, description] = splitN(data, " ", 1); | |
175 | return Model.edit(createTimestamp, description); | |
176 | } | |
68a72fde SW |
177 | if (command == "Priority") { |
178 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 179 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 180 | } |
6a5644f3 SW |
181 | if (command == "State") { |
182 | const [createTimestamp, state] = splitN(data, " ", 1); | |
183 | return Model.setState(timestamp, createTimestamp, state); | |
184 | } | |
7b5b90b9 SW |
185 | if (command == "Tag") { |
186 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
187 | return Model.addTag(createTimestamp, tag); | |
188 | } | |
0726872b SW |
189 | if (command == "Untag") { |
190 | const [createTimestamp, tag] = splitN(data, " ", 1); | |
191 | return Model.removeTag(createTimestamp, tag); | |
192 | } | |
60a63831 SW |
193 | }, |
194 | ||
e88c099c | 195 | record: function (entry: string) { |
d03daa19 | 196 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
197 | }, |
198 | ||
e88c099c SW |
199 | recordAndApply: function (entry: string) { |
200 | this.record(entry); | |
6d01c406 | 201 | return this.apply(entry); |
60a63831 SW |
202 | }, |
203 | ||
204 | replay: function () { | |
205 | while (true) { | |
d03daa19 | 206 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
207 | if (entry === null) { |
208 | break; | |
209 | } | |
e88c099c | 210 | this.apply(entry); |
60a63831 SW |
211 | next_log_index++; |
212 | } | |
213 | }, | |
214 | }; | |
d03daa19 SW |
215 | } |
216 | const log = Log(); | |
262705dd | 217 | |
b56a37d3 | 218 | function UI() { |
0d1c27a8 SW |
219 | const undoLog: string[][] = []; |
220 | const redoLog: string[][] = []; | |
76825ecd | 221 | function perform(forward: string, reverse: string) { |
0d1c27a8 | 222 | undoLog.push([reverse, forward]); |
76825ecd SW |
223 | return log.recordAndApply(`${clock.now()} ${forward}`); |
224 | } | |
b56a37d3 SW |
225 | return { |
226 | addTask: function (description: string): Element { | |
227 | const now = clock.now(); | |
0d1c27a8 | 228 | undoLog.push([`State ${now} deleted`, `State ${now} todo`]); |
b56a37d3 SW |
229 | return <Element>log.recordAndApply(`${now} Create ${description}`); |
230 | }, | |
7b5b90b9 | 231 | addTag: function (createTimestamp: string, tag: string) { |
76825ecd | 232 | return perform(`Tag ${createTimestamp} ${tag}`, `Untag ${createTimestamp} ${tag}`); |
7b5b90b9 | 233 | }, |
b56a37d3 | 234 | edit: function (createTimestamp: string, newDescription: string, oldDescription: string) { |
76825ecd | 235 | return perform(`Edit ${createTimestamp} ${newDescription}`, `Edit ${createTimestamp} ${oldDescription}`); |
b56a37d3 | 236 | }, |
b5f15e0e | 237 | removeTag: function (createTimestamp: string, tag: string) { |
76825ecd | 238 | return perform(`Untag ${createTimestamp} ${tag}`, `Tag ${createTimestamp} ${tag}`); |
b5f15e0e | 239 | }, |
b56a37d3 | 240 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { |
76825ecd | 241 | return perform(`Priority ${createTimestamp} ${newPriority}`, `Priority ${createTimestamp} ${oldPriority}`); |
b56a37d3 SW |
242 | }, |
243 | setState: function (createTimestamp: string, newState: string, oldState: string) { | |
76825ecd | 244 | return perform(`State ${createTimestamp} ${newState}`, `State ${createTimestamp} ${oldState}`); |
b56a37d3 SW |
245 | }, |
246 | undo: function () { | |
0d1c27a8 SW |
247 | const entry = undoLog.pop(); |
248 | if (entry) { | |
249 | redoLog.push(entry); | |
250 | return log.recordAndApply(`${clock.now()} ${entry[0]}`); | |
251 | } | |
252 | }, | |
253 | redo: function () { | |
254 | const entry = redoLog.pop(); | |
255 | if (entry) { | |
256 | undoLog.push(entry); | |
257 | return log.recordAndApply(`${clock.now()} ${entry[1]}`); | |
b56a37d3 SW |
258 | } |
259 | }, | |
260 | }; | |
261 | } | |
262 | const ui = UI(); | |
e88c099c | 263 | |
ad72cd51 SW |
264 | enum CommitOrAbort { |
265 | Commit, | |
266 | Abort, | |
267 | } | |
268 | ||
ada060d7 | 269 | function BrowserUI() { |
c2226333 SW |
270 | const viewColors: { [key: string]: string } = { |
271 | all: "Gold", | |
272 | cancelled: "Red", | |
273 | deleted: "Black", | |
274 | done: "LawnGreen", | |
275 | "someday-maybe": "DeepSkyBlue", | |
276 | todo: "White", | |
277 | waiting: "MediumOrchid", | |
278 | }; | |
868667c1 | 279 | var currentViewState = "todo"; |
a59fbe41 | 280 | var taskFocusedBeforeJumpingToInput: HTMLElement | null = null; |
09cd65ad | 281 | var lastTagNameEntered = ""; |
ada060d7 SW |
282 | return { |
283 | addTask: function (event: KeyboardEvent) { | |
284 | const input = <HTMLInputElement>document.getElementById("taskName"); | |
fb19ac80 SW |
285 | if (input.value.match(/^ *$/)) return; |
286 | const task = ui.addTask(input.value); | |
cddbdce1 | 287 | if (currentViewState === "todo" || currentViewState === "all") { |
fb19ac80 SW |
288 | task instanceof HTMLElement && task.focus(); |
289 | } else if (this.returnFocusAfterInput()) { | |
290 | } else { | |
291 | this.firstVisibleTask()?.focus(); | |
292 | } | |
293 | input.value = ""; | |
294 | if (event.getModifierState("Control")) { | |
88bd89ef | 295 | this.makeBottomPriority(task); |
bc7996fe | 296 | } |
ada060d7 | 297 | }, |
09657615 | 298 | |
ada060d7 | 299 | beginEdit: function (event: Event) { |
32808c9a | 300 | const task = this.currentTask(); |
ada060d7 SW |
301 | if (!task) return; |
302 | const input = document.createElement("input"); | |
26737687 SW |
303 | const desc = task.getElementsByClassName("desc")[0]; |
304 | const oldDescription = desc.textContent!; | |
ada060d7 SW |
305 | task.setAttribute("data-description", oldDescription); |
306 | input.value = oldDescription; | |
307 | input.addEventListener("blur", this.completeEdit, { once: true }); | |
26737687 | 308 | desc.textContent = ""; |
7b5b90b9 SW |
309 | task.insertBefore(input, task.firstChild); |
310 | input.focus(); | |
311 | event.preventDefault(); | |
312 | }, | |
313 | ||
314 | beginTagEdit: function (event: Event) { | |
32808c9a | 315 | const task = this.currentTask(); |
7b5b90b9 SW |
316 | if (!task) return; |
317 | const input = document.createElement("input"); | |
318 | input.classList.add("tag"); | |
319 | input.addEventListener("blur", this.completeTagEdit, { once: true }); | |
09cd65ad | 320 | input.value = lastTagNameEntered; |
ada060d7 SW |
321 | task.appendChild(input); |
322 | input.focus(); | |
09cd65ad | 323 | input.select(); |
ada060d7 SW |
324 | event.preventDefault(); |
325 | }, | |
7b574407 | 326 | |
ad72cd51 | 327 | completeEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { |
ada060d7 SW |
328 | const input = event.target as HTMLInputElement; |
329 | const task = input.parentElement!; | |
26737687 | 330 | const desc = task.getElementsByClassName("desc")[0]; |
ada060d7 SW |
331 | const oldDescription = task.getAttribute("data-description")!; |
332 | const newDescription = input.value; | |
333 | input.removeEventListener("blur", this.completeEdit); | |
132921e6 | 334 | task.removeChild(input); |
ada060d7 SW |
335 | task.removeAttribute("data-description"); |
336 | task.focus(); | |
fb19ac80 | 337 | if (resolution === CommitOrAbort.Abort || newDescription.match(/^ *$/) || newDescription === oldDescription) { |
26737687 | 338 | desc.textContent = oldDescription; |
ada060d7 | 339 | } else { |
b56a37d3 | 340 | ui.edit(task.getAttribute("data-created")!, newDescription, oldDescription); |
ada060d7 SW |
341 | } |
342 | }, | |
7b574407 | 343 | |
7b5b90b9 SW |
344 | completeTagEdit: function (event: Event, resolution: CommitOrAbort = CommitOrAbort.Commit) { |
345 | const input = event.target as HTMLInputElement; | |
346 | const task = input.parentElement!; | |
347 | const newTagName = input.value; | |
348 | input.removeEventListener("blur", this.completeTagEdit); | |
349 | task.removeChild(input); | |
350 | task.focus(); | |
fb19ac80 | 351 | if (resolution === CommitOrAbort.Commit && !newTagName.match(/^ *$/) && !Model.hasTag(task, newTagName)) { |
e1eb33ad | 352 | ui.addTag(task.getAttribute("data-created")!, newTagName); |
09cd65ad | 353 | lastTagNameEntered = newTagName; |
e1eb33ad | 354 | } |
7b5b90b9 SW |
355 | }, |
356 | ||
5800003c SW |
357 | currentTag: function (): Element | null { |
358 | var target = document.activeElement; | |
359 | if (!target) return null; | |
360 | if (target.classList.contains("task")) { | |
361 | const tags = target.getElementsByClassName("tag"); | |
362 | target = tags[tags.length - 1]; | |
363 | } | |
364 | if (!target || !target.classList.contains("tag")) return null; | |
365 | return target; | |
366 | }, | |
367 | ||
32808c9a SW |
368 | currentTask: function (): HTMLElement | null { |
369 | var target = document.activeElement; | |
370 | if (!target) return null; | |
371 | if (target.classList.contains("tag")) target = target.parentElement!; | |
372 | return target as HTMLElement; | |
373 | }, | |
374 | ||
ada060d7 SW |
375 | firstVisibleTask: function () { |
376 | for (const task of document.getElementsByClassName("task")) { | |
cddbdce1 | 377 | const state = task.getAttribute("data-state"); |
312acaa8 SW |
378 | if ( |
379 | task instanceof HTMLElement && | |
380 | (state === currentViewState || (currentViewState === "all" && state !== "deleted")) && | |
381 | !task.classList.contains("hide") | |
382 | ) { | |
ada060d7 SW |
383 | return task; |
384 | } | |
65a7510d | 385 | } |
ada060d7 | 386 | }, |
caa93fd1 | 387 | |
ada060d7 | 388 | focusTaskNameInput: function (event: Event) { |
32808c9a | 389 | taskFocusedBeforeJumpingToInput = this.currentTask(); |
ada060d7 | 390 | document.getElementById("taskName")!.focus(); |
a1aa43d8 | 391 | window.scroll(0, 0); |
ada060d7 SW |
392 | event.preventDefault(); |
393 | }, | |
09657615 | 394 | |
ada060d7 SW |
395 | visibleTaskAtOffset(task: Element, offset: number): Element { |
396 | var cursor: Element | null = task; | |
397 | var valid_cursor = cursor; | |
398 | const increment = offset / Math.abs(offset); | |
399 | while (true) { | |
400 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
401 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
cddbdce1 | 402 | const state = cursor.getAttribute("data-state")!; |
312acaa8 SW |
403 | if ( |
404 | (state === currentViewState || (currentViewState === "all" && state !== "deleted")) && | |
405 | !cursor.classList.contains("hide") | |
406 | ) { | |
ada060d7 SW |
407 | offset -= increment; |
408 | valid_cursor = cursor; | |
409 | } | |
410 | if (Math.abs(offset) < 0.5) break; | |
5fa4704c | 411 | } |
ada060d7 SW |
412 | return valid_cursor; |
413 | }, | |
23be73e3 | 414 | |
40025d12 SW |
415 | jumpCursor: function (position: number) { |
416 | const first = this.firstVisibleTask(); | |
417 | if (!first) return; | |
418 | const dest = this.visibleTaskAtOffset(first, position - 1); | |
419 | if (dest instanceof HTMLElement) dest.focus(); | |
420 | }, | |
421 | ||
88bd89ef | 422 | makeBottomPriority: function (task: Element | null = null) { |
32808c9a | 423 | if (!task) task = this.currentTask(); |
88bd89ef SW |
424 | if (!task) return; |
425 | this.setPriority(task, document.getElementById("tasks")!.lastElementChild, null); | |
426 | }, | |
427 | ||
55a4baa8 | 428 | makeTopPriority: function (task: Element | null = null) { |
32808c9a | 429 | if (!task) task = this.currentTask(); |
55a4baa8 | 430 | if (!task) return; |
88bd89ef SW |
431 | ui.setPriority(task.getAttribute("data-created")!, clock.now(), Model.getPriority(task)); |
432 | task instanceof HTMLElement && task.focus(); | |
792b8a18 SW |
433 | }, |
434 | ||
ada060d7 | 435 | moveCursor: function (offset: number): boolean { |
32808c9a | 436 | const active = this.currentTask(); |
ada060d7 SW |
437 | if (!active) return false; |
438 | const dest = this.visibleTaskAtOffset(active, offset); | |
439 | if (dest !== active && dest instanceof HTMLElement) { | |
440 | dest.focus(); | |
441 | return true; | |
442 | } | |
443 | return false; | |
444 | }, | |
01f41859 | 445 | |
ada060d7 | 446 | moveTask: function (offset: number) { |
32808c9a | 447 | const active = this.currentTask(); |
ada060d7 SW |
448 | if (!active) return; |
449 | const dest = this.visibleTaskAtOffset(active, offset); | |
450 | if (dest === active) return; // Already extremal | |
451 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
452 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
453 | if (offset > 0) { | |
454 | this.setPriority(active, dest, onePastDest); | |
455 | } else { | |
456 | this.setPriority(active, onePastDest, dest); | |
457 | } | |
458 | }, | |
68a72fde | 459 | |
b5f15e0e | 460 | removeTag: function () { |
5800003c | 461 | const target = this.currentTag(); |
4ccaa1d6 | 462 | if (!target) return; |
4ccaa1d6 | 463 | ui.removeTag(target.parentElement!.getAttribute("data-created")!, target.textContent!); |
b5f15e0e SW |
464 | }, |
465 | ||
312acaa8 SW |
466 | resetTagView: function () { |
467 | for (const task of document.getElementsByClassName("task")) { | |
468 | task.classList.remove("hide"); | |
469 | } | |
470 | }, | |
471 | ||
58b569ce SW |
472 | resetView: function () { |
473 | this.setView("todo"); | |
312acaa8 | 474 | this.resetTagView(); |
58b569ce SW |
475 | }, |
476 | ||
a59fbe41 SW |
477 | returnFocusAfterInput: function (): boolean { |
478 | if (taskFocusedBeforeJumpingToInput) { | |
479 | taskFocusedBeforeJumpingToInput.focus(); | |
480 | return true; | |
481 | } | |
482 | return false; | |
483 | }, | |
484 | ||
ada060d7 SW |
485 | // Change task's priority to be between other tasks a and b. |
486 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
88bd89ef SW |
487 | const aPriority = a === null ? clock.now() : Model.getPriority(a); |
488 | const bPriority = b === null ? 0 : Model.getPriority(b); | |
489 | console.assert(aPriority > bPriority, aPriority, ">", bPriority); | |
490 | const span = aPriority - bPriority; | |
491 | const newPriority = bPriority + 0.1 * span + 0.8 * span * Math.random(); | |
492 | console.assert(aPriority > newPriority && newPriority > bPriority, aPriority, ">", newPriority, ">", bPriority); | |
ada060d7 | 493 | const newPriorityRounded = Math.round(newPriority); |
88bd89ef | 494 | const okToRound = aPriority > newPriorityRounded && newPriorityRounded > bPriority; |
b56a37d3 | 495 | ui.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task)); |
ada060d7 SW |
496 | task instanceof HTMLElement && task.focus(); |
497 | }, | |
68a72fde | 498 | |
ada060d7 | 499 | setState: function (newState: string) { |
32808c9a | 500 | const task = this.currentTask(); |
ada060d7 SW |
501 | if (!task) return; |
502 | const oldState = task.getAttribute("data-state")!; | |
503 | if (newState === oldState) return; | |
504 | const createTimestamp = task.getAttribute("data-created")!; | |
cddbdce1 SW |
505 | if (currentViewState !== "all" || newState == "deleted") { |
506 | this.moveCursor(1) || this.moveCursor(-1); | |
507 | } | |
b56a37d3 | 508 | return ui.setState(createTimestamp, newState, oldState); |
ada060d7 | 509 | }, |
43f3cc0c | 510 | |
312acaa8 SW |
511 | setTagView: function () { |
512 | const target = this.currentTag(); | |
513 | if (!target) return; | |
514 | const tag = target.textContent!; | |
515 | for (const task of document.getElementsByClassName("task")) { | |
516 | if (Model.hasTag(task, tag)) { | |
517 | task.classList.remove("hide"); | |
518 | } else { | |
519 | task.classList.add("hide"); | |
520 | } | |
521 | } | |
522 | }, | |
523 | ||
c2226333 | 524 | setView: function (state: string) { |
868667c1 | 525 | const sheet = (document.getElementById("viewStyle") as HTMLStyleElement).sheet!; |
cddbdce1 SW |
526 | if (state === "all") { |
527 | sheet.insertRule(`.task[data-state=deleted] { display: none }`); | |
528 | } else { | |
529 | sheet.insertRule(`.task:not([data-state=${state}]) { display: none }`); | |
530 | } | |
c2226333 | 531 | sheet.insertRule(`:root { --view-state-indicator-color: ${viewColors[state]}; }`); |
4c532769 SW |
532 | sheet.removeRule(2); |
533 | sheet.removeRule(2); | |
868667c1 | 534 | currentViewState = state; |
32808c9a | 535 | if (this.currentTask()?.getAttribute("data-state") !== state) { |
868667c1 SW |
536 | this.firstVisibleTask()?.focus(); |
537 | } | |
538 | }, | |
539 | ||
84849dfa SW |
540 | setUntaggedView: function () { |
541 | for (const task of document.getElementsByClassName("task")) { | |
542 | if (task.getElementsByClassName("tag").length === 0) { | |
543 | task.classList.remove("hide"); | |
544 | } else { | |
545 | task.classList.add("hide"); | |
546 | } | |
547 | } | |
548 | }, | |
549 | ||
ada060d7 | 550 | undo: function () { |
b56a37d3 | 551 | const ret = ui.undo(); |
ada060d7 SW |
552 | if (ret && ret instanceof HTMLElement) ret.focus(); |
553 | }, | |
0d1c27a8 SW |
554 | redo: function () { |
555 | const ret = ui.redo(); | |
556 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
557 | }, | |
ada060d7 SW |
558 | }; |
559 | } | |
560 | const browserUI = BrowserUI(); | |
06ee32a1 | 561 | |
90381b6d | 562 | const scrollIncrement = 60; |
e94e9f27 | 563 | enum InputState { |
02c8a409 | 564 | Root, |
36ddfad1 | 565 | S, |
02c8a409 | 566 | V, |
36ddfad1 | 567 | VS, |
e94e9f27 | 568 | } |
02c8a409 | 569 | var inputState = InputState.Root; |
36fa06f4 | 570 | var inputCount: number | null = null; |
e94e9f27 | 571 | |
f1afad9b | 572 | function handleKey(event: any) { |
f1d8d0ed | 573 | if (["Alt", "Control", "Meta", "Shift"].includes(event.key)) return; |
a26b1f4b | 574 | if (event.target.tagName === "INPUT") { |
7b574407 | 575 | if (event.target.id === "taskName") { |
ada060d7 | 576 | if (event.key == "Enter") return browserUI.addTask(event); |
a59fbe41 | 577 | if (event.key == "Escape") return browserUI.returnFocusAfterInput(); |
7b5b90b9 SW |
578 | } else if (event.target.classList.contains("tag")) { |
579 | if (event.key == "Enter") return browserUI.completeTagEdit(event); | |
580 | if (event.key == "Escape") return browserUI.completeTagEdit(event, CommitOrAbort.Abort); | |
7b574407 | 581 | } else { |
ada060d7 | 582 | if (event.key == "Enter") return browserUI.completeEdit(event); |
ad72cd51 | 583 | if (event.key == "Escape") return browserUI.completeEdit(event, CommitOrAbort.Abort); |
7b574407 | 584 | } |
a26b1f4b | 585 | } else { |
02c8a409 | 586 | if (inputState === InputState.Root) { |
36fa06f4 SW |
587 | if ("0" <= event.key && event.key <= "9") { |
588 | return (inputCount = (inputCount ?? 0) * 10 + parseInt(event.key)); | |
589 | } | |
590 | try { | |
90381b6d SW |
591 | if (event.ctrlKey) { |
592 | if (event.key == "e") return window.scrollBy(0, (inputCount ?? 1) * scrollIncrement); | |
593 | if (event.key == "y") return window.scrollBy(0, (inputCount ?? 1) * -scrollIncrement); | |
594 | } else { | |
595 | if (event.key == "j") return browserUI.moveCursor(inputCount ?? 1); | |
596 | if (event.key == "k") return browserUI.moveCursor(-(inputCount ?? 1)); | |
597 | if (event.key == "J") return browserUI.moveTask(inputCount ?? 1); | |
598 | if (event.key == "K") return browserUI.moveTask(-(inputCount ?? 1)); | |
599 | if (event.key == "G") return browserUI.jumpCursor(inputCount ?? MAX_SAFE_INTEGER); | |
600 | if (event.key == "T") return browserUI.makeTopPriority(); | |
601 | if (event.key == "n") return browserUI.focusTaskNameInput(event); | |
602 | if (event.key == "c") return browserUI.setState("cancelled"); | |
603 | if (event.key == "d") return browserUI.setState("done"); | |
604 | if (event.key == "q") return browserUI.setState("todo"); | |
605 | if (event.key == "s") return (inputState = InputState.S); | |
606 | if (event.key == "w") return browserUI.setState("waiting"); | |
607 | if (event.key == "X") return browserUI.setState("deleted"); | |
608 | if (event.key == "x") return browserUI.removeTag(); | |
609 | if (event.key == "u") return browserUI.undo(); | |
610 | if (event.key == "r") return browserUI.redo(); | |
611 | if (event.key == "e") return browserUI.beginEdit(event); | |
612 | if (event.key == "t") return browserUI.beginTagEdit(event); | |
613 | if (event.key == "v") return (inputState = InputState.V); | |
614 | } | |
36fa06f4 SW |
615 | } finally { |
616 | inputCount = null; | |
617 | } | |
36ddfad1 SW |
618 | } else if (inputState === InputState.S) { |
619 | inputState = InputState.Root; | |
620 | if (event.key == "m") return browserUI.setState("someday-maybe"); | |
02c8a409 SW |
621 | } else if (inputState === InputState.V) { |
622 | inputState = InputState.Root; | |
c2226333 SW |
623 | if (event.key == "a") return browserUI.setView("all"); |
624 | if (event.key == "c") return browserUI.setView("cancelled"); | |
625 | if (event.key == "d") return browserUI.setView("done"); | |
84849dfa | 626 | if (event.key == "i") return browserUI.setUntaggedView(); |
c2226333 | 627 | if (event.key == "q") return browserUI.setView("todo"); |
36ddfad1 | 628 | if (event.key == "s") return (inputState = InputState.VS); |
312acaa8 SW |
629 | if (event.key == "T") return browserUI.resetTagView(); |
630 | if (event.key == "t") return browserUI.setTagView(); | |
84849dfa | 631 | if (event.key == "u") return browserUI.setUntaggedView(); |
58b569ce | 632 | if (event.key == "v") return browserUI.resetView(); |
c2226333 SW |
633 | if (event.key == "w") return browserUI.setView("waiting"); |
634 | if (event.key == "x") return browserUI.setView("deleted"); | |
36ddfad1 SW |
635 | } else if (inputState === InputState.VS) { |
636 | inputState = InputState.Root; | |
c2226333 | 637 | if (event.key == "m") return browserUI.setView("someday-maybe"); |
e94e9f27 | 638 | } |
f1afad9b SW |
639 | } |
640 | } | |
641 | ||
f1afad9b | 642 | function browserInit() { |
d03daa19 | 643 | log.replay(); |
ada060d7 | 644 | browserUI.firstVisibleTask()?.focus(); |
bd267c29 | 645 | document.body.addEventListener("keydown", handleKey, { capture: false }); |
f1afad9b | 646 | } |