]>
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 | ||
13c97b99 | 16 | const Model = { |
6d01c406 | 17 | addTask: function (timestamp: string, description: string): Element { |
13c97b99 SW |
18 | const task = document.createElement("div"); |
19 | task.appendChild(document.createTextNode(description)); | |
799f4e89 | 20 | task.setAttribute("class", "task"); |
13c97b99 | 21 | task.setAttribute("tabindex", "0"); |
4101e1b1 | 22 | task.setAttribute("data-created", timestamp); |
ef7ebad4 SW |
23 | document.getElementById("tasks")!.appendChild(task); |
24 | task.focus(); | |
6d01c406 | 25 | return task; |
13c97b99 | 26 | }, |
974848d3 | 27 | |
68a72fde SW |
28 | getPriority: function (task: Element): number { |
29 | if (task.hasAttribute("data-priority")) { | |
30 | return parseFloat(task.getAttribute("data-priority")!); | |
31 | } | |
32 | return parseFloat(task.getAttribute("data-created")!); | |
33 | }, | |
34 | ||
799f4e89 SW |
35 | getTask: function (createTimestamp: string) { |
36 | for (const task of document.getElementsByClassName("task")) { | |
37 | if (task.getAttribute("data-created") === createTimestamp) { | |
38 | return task; | |
39 | } | |
40 | } | |
41 | }, | |
42 | ||
43f3cc0c | 43 | setPriority: function (createTimestamp: string, priority: number): Element | null { |
68a72fde | 44 | const target = this.getTask(createTimestamp); |
43f3cc0c | 45 | if (!target) return null; |
68a72fde SW |
46 | target.setAttribute("data-priority", `${priority}`); |
47 | for (const task of document.getElementsByClassName("task")) { | |
48 | if (task !== target && this.getPriority(task) > priority) { | |
49 | task.parentElement!.insertBefore(target, task); | |
50 | target instanceof HTMLElement && target.focus(); | |
43f3cc0c | 51 | return target; |
68a72fde SW |
52 | } |
53 | } | |
54 | document.getElementById("tasks")!.appendChild(target); | |
55 | target instanceof HTMLElement && target.focus(); | |
43f3cc0c | 56 | return target; |
68a72fde SW |
57 | }, |
58 | ||
01f41859 SW |
59 | setState: function (stateTimestamp: string, createTimestamp: string, state: string) { |
60 | const task = this.getTask(createTimestamp); | |
61 | if (task) { | |
62 | task.setAttribute(`data-${state}`, stateTimestamp); | |
63 | if (task instanceof HTMLElement) { | |
64 | task.style.display = "none"; // Until view filtering | |
65 | } | |
66 | } | |
799f4e89 | 67 | }, |
13c97b99 | 68 | }; |
f1afad9b | 69 | |
d03daa19 | 70 | function Log(prefix: string = "vp-") { |
60a63831 SW |
71 | var next_log_index = 0; |
72 | return { | |
e88c099c | 73 | apply: function (entry: string) { |
60a63831 SW |
74 | const [timestamp, command, data] = splitN(entry, " ", 2); |
75 | if (command == "Create") { | |
6d01c406 | 76 | return Model.addTask(timestamp, data); |
60a63831 | 77 | } |
01f41859 SW |
78 | if (command == "State") { |
79 | const [createTimestamp, state] = splitN(data, " ", 1); | |
6d01c406 | 80 | return Model.setState(timestamp, createTimestamp, state); |
01f41859 | 81 | } |
68a72fde SW |
82 | if (command == "Priority") { |
83 | const [createTimestamp, newPriority] = splitN(data, " ", 1); | |
6d01c406 | 84 | return Model.setPriority(createTimestamp, parseFloat(newPriority)); |
68a72fde | 85 | } |
60a63831 SW |
86 | }, |
87 | ||
e88c099c | 88 | record: function (entry: string) { |
d03daa19 | 89 | window.localStorage.setItem(`${prefix}${next_log_index++}`, entry); |
60a63831 SW |
90 | }, |
91 | ||
e88c099c SW |
92 | recordAndApply: function (entry: string) { |
93 | this.record(entry); | |
6d01c406 | 94 | return this.apply(entry); |
60a63831 SW |
95 | }, |
96 | ||
97 | replay: function () { | |
98 | while (true) { | |
d03daa19 | 99 | const entry = window.localStorage.getItem(`${prefix}${next_log_index}`); |
60a63831 SW |
100 | if (entry === null) { |
101 | break; | |
102 | } | |
e88c099c | 103 | this.apply(entry); |
60a63831 SW |
104 | next_log_index++; |
105 | } | |
106 | }, | |
107 | }; | |
d03daa19 SW |
108 | } |
109 | const log = Log(); | |
262705dd | 110 | |
43f3cc0c SW |
111 | const undoLog: string[] = []; |
112 | ||
e88c099c | 113 | const UI = { |
6d01c406 | 114 | addTask: function (description: string): Element { |
43f3cc0c SW |
115 | const now = Date.now(); |
116 | undoLog.push(`State ${now} deleted`); | |
117 | return <Element>log.recordAndApply(`${now} Create ${description}`); | |
e88c099c | 118 | }, |
43f3cc0c SW |
119 | setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) { |
120 | undoLog.push(`Priority ${createTimestamp} ${oldPriority}`); | |
121 | return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${newPriority}`); | |
68a72fde | 122 | }, |
01f41859 | 123 | setState: function (createTimestamp: string, state: string) { |
6d01c406 | 124 | return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${state}`); |
01f41859 | 125 | }, |
43f3cc0c SW |
126 | undo: function () { |
127 | if (undoLog.length > 0) { | |
128 | return log.recordAndApply(`${Date.now()} ${undoLog.pop()}`); | |
129 | } | |
130 | }, | |
e88c099c SW |
131 | }; |
132 | ||
caa93fd1 | 133 | const BrowserUI = { |
bc7996fe | 134 | addTask: function (event: KeyboardEvent) { |
a26b1f4b SW |
135 | const input = <HTMLInputElement>document.getElementById("taskName"); |
136 | if (input.value) { | |
6d01c406 | 137 | const task = UI.addTask(input.value); |
a26b1f4b | 138 | input.value = ""; |
bc7996fe SW |
139 | if (event.getModifierState("Control")) { |
140 | this.setPriority(task, null, document.getElementsByClassName("task")[0]); | |
141 | } | |
caa93fd1 | 142 | } |
caa93fd1 | 143 | }, |
09657615 | 144 | |
65a7510d SW |
145 | firstVisibleTask: function () { |
146 | for (const task of document.getElementsByClassName("task")) { | |
147 | if (task instanceof HTMLElement && task.style.display !== "none") { | |
148 | return task; | |
149 | } | |
150 | } | |
799f4e89 | 151 | }, |
caa93fd1 | 152 | |
bc7996fe | 153 | focusTaskNameInput: function (event: Event) { |
09657615 SW |
154 | document.getElementById("taskName")!.focus(); |
155 | event.preventDefault(); | |
156 | }, | |
157 | ||
23be73e3 SW |
158 | visibleTaskAtOffset(task: Element, offset: number): Element { |
159 | var cursor: Element | null = task; | |
5fa4704c SW |
160 | var valid_cursor = cursor; |
161 | const increment = offset / Math.abs(offset); | |
162 | while (true) { | |
163 | cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling; | |
164 | if (!cursor || !(cursor instanceof HTMLElement)) break; | |
165 | if (cursor.style.display !== "none") { | |
166 | offset -= increment; | |
167 | valid_cursor = cursor; | |
168 | } | |
169 | if (Math.abs(offset) < 0.5) break; | |
09657615 | 170 | } |
23be73e3 SW |
171 | return valid_cursor; |
172 | }, | |
173 | ||
174 | moveCursor: function (offset: number): boolean { | |
175 | const active = document.activeElement; | |
176 | if (!active) return false; | |
177 | const dest = this.visibleTaskAtOffset(active, offset); | |
178 | if (dest !== active && dest instanceof HTMLElement) { | |
179 | dest.focus(); | |
09657615 SW |
180 | return true; |
181 | } | |
182 | return false; | |
183 | }, | |
01f41859 | 184 | |
68a72fde SW |
185 | moveTask: function (offset: number) { |
186 | const active = document.activeElement; | |
187 | if (!active) return; | |
188 | const dest = this.visibleTaskAtOffset(active, offset); | |
189 | if (dest === active) return; // Already extremal | |
190 | var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset)); | |
191 | if (onePastDest == dest) onePastDest = null; // Will become extremal | |
192 | if (offset > 0) { | |
193 | this.setPriority(active, dest, onePastDest); | |
194 | } else { | |
195 | this.setPriority(active, onePastDest, dest); | |
196 | } | |
197 | }, | |
198 | ||
199 | // Change task's priority to be between other tasks a and b. | |
200 | setPriority: function (task: Element, a: Element | null, b: Element | null) { | |
201 | const aPriority = a === null ? 0 : Model.getPriority(a); | |
202 | const bPriority = b === null ? Date.now() : Model.getPriority(b); | |
203 | console.assert(aPriority < bPriority, aPriority, "<", bPriority); | |
204 | const span = bPriority - aPriority; | |
205 | const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random(); | |
206 | console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority); | |
207 | const newPriorityRounded = Math.round(newPriority); | |
208 | const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority; | |
43f3cc0c | 209 | UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task)); |
68a72fde SW |
210 | }, |
211 | ||
01f41859 SW |
212 | setState: function (state: string) { |
213 | const createTimestamp = document.activeElement?.getAttribute("data-created"); | |
214 | this.moveCursor(1) || this.moveCursor(-1); | |
215 | return UI.setState(createTimestamp!, state); | |
216 | }, | |
43f3cc0c SW |
217 | |
218 | undo: function () { | |
219 | const ret = UI.undo(); | |
220 | if (ret && ret instanceof HTMLElement) ret.focus(); | |
221 | }, | |
09657615 | 222 | }; |
06ee32a1 | 223 | |
f1afad9b | 224 | function handleKey(event: any) { |
a26b1f4b | 225 | if (event.target.tagName === "INPUT") { |
bc7996fe | 226 | if (event.key == "Enter") return BrowserUI.addTask(event); |
a26b1f4b | 227 | } else { |
09657615 SW |
228 | if (event.key == "j") return BrowserUI.moveCursor(1); |
229 | if (event.key == "k") return BrowserUI.moveCursor(-1); | |
68a72fde SW |
230 | if (event.key == "J") return BrowserUI.moveTask(1); |
231 | if (event.key == "K") return BrowserUI.moveTask(-1); | |
01f41859 SW |
232 | if (event.key == "n") return BrowserUI.focusTaskNameInput(event); |
233 | if (event.key == "s") return BrowserUI.setState("someday-maybe"); | |
234 | if (event.key == "w") return BrowserUI.setState("waiting"); | |
235 | if (event.key == "d") return BrowserUI.setState("done"); | |
236 | if (event.key == "c") return BrowserUI.setState("cancelled"); | |
45cbd5e5 | 237 | if (event.key == "X") return BrowserUI.setState("deleted"); |
43f3cc0c | 238 | if (event.key == "u") return BrowserUI.undo(); |
f1afad9b SW |
239 | } |
240 | } | |
241 | ||
f1afad9b SW |
242 | function browserInit() { |
243 | document.body.addEventListener("keydown", handleKey, { capture: false }); | |
d03daa19 | 244 | log.replay(); |
65a7510d | 245 | BrowserUI.firstVisibleTask()?.focus(); |
f1afad9b | 246 | } |