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