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;
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[] {
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));
17 addTask: function (timestamp: string, description: string): Element {
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
20 task.setAttribute("class", "task");
21 task.setAttribute("tabindex", "0");
22 task.setAttribute("data-created", timestamp);
23 document.getElementById("tasks")!.appendChild(task);
28 getPriority: function (task: Element): number {
29 if (task.hasAttribute("data-priority")) {
30 return parseFloat(task.getAttribute("data-priority")!);
32 return parseFloat(task.getAttribute("data-created")!);
35 getState: function (task: Element): string {
36 return task.getAttribute("data-state") ?? "todo";
39 getTask: function (createTimestamp: string) {
40 for (const task of document.getElementsByClassName("task")) {
41 if (task.getAttribute("data-created") === createTimestamp) {
47 setPriority: function (createTimestamp: string, priority: number): Element | null {
48 const target = this.getTask(createTimestamp);
49 if (!target) return null;
50 target.setAttribute("data-priority", `${priority}`);
51 for (const task of document.getElementsByClassName("task")) {
52 if (task !== target && this.getPriority(task) > priority) {
53 task.parentElement!.insertBefore(target, task);
54 target instanceof HTMLElement && target.focus();
58 document.getElementById("tasks")!.appendChild(target);
59 target instanceof HTMLElement && target.focus();
63 setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
64 const task = this.getTask(createTimestamp);
66 task.setAttribute("data-state", state);
67 if (task instanceof HTMLElement) {
68 task.style.display = state == "todo" ? "block" : "none"; // Until view filtering
74 function Log(prefix: string = "vp-") {
75 var next_log_index = 0;
77 apply: function (entry: string) {
78 const [timestamp, command, data] = splitN(entry, " ", 2);
79 if (command == "Create") {
80 return Model.addTask(timestamp, data);
82 if (command == "State") {
83 const [createTimestamp, state] = splitN(data, " ", 1);
84 return Model.setState(timestamp, createTimestamp, state);
86 if (command == "Priority") {
87 const [createTimestamp, newPriority] = splitN(data, " ", 1);
88 return Model.setPriority(createTimestamp, parseFloat(newPriority));
92 record: function (entry: string) {
93 window.localStorage.setItem(`${prefix}${next_log_index++}`, entry);
96 recordAndApply: function (entry: string) {
98 return this.apply(entry);
101 replay: function () {
103 const entry = window.localStorage.getItem(`${prefix}${next_log_index}`);
104 if (entry === null) {
115 const undoLog: string[] = [];
118 addTask: function (description: string): Element {
119 const now = Date.now();
120 undoLog.push(`State ${now} deleted`);
121 return <Element>log.recordAndApply(`${now} Create ${description}`);
123 setPriority: function (createTimestamp: string, newPriority: number, oldPriority: number) {
124 undoLog.push(`Priority ${createTimestamp} ${oldPriority}`);
125 return log.recordAndApply(`${Date.now()} Priority ${createTimestamp} ${newPriority}`);
127 setState: function (createTimestamp: string, newState: string, oldState: string) {
128 undoLog.push(`State ${createTimestamp} ${oldState}`);
129 return log.recordAndApply(`${Date.now()} State ${createTimestamp} ${newState}`);
132 if (undoLog.length > 0) {
133 return log.recordAndApply(`${Date.now()} ${undoLog.pop()}`);
139 addTask: function (event: KeyboardEvent) {
140 const input = <HTMLInputElement>document.getElementById("taskName");
142 const task = UI.addTask(input.value);
144 if (event.getModifierState("Control")) {
145 this.setPriority(task, null, document.getElementsByClassName("task")[0]);
150 firstVisibleTask: function () {
151 for (const task of document.getElementsByClassName("task")) {
152 if (task instanceof HTMLElement && task.style.display !== "none") {
158 focusTaskNameInput: function (event: Event) {
159 document.getElementById("taskName")!.focus();
160 event.preventDefault();
163 visibleTaskAtOffset(task: Element, offset: number): Element {
164 var cursor: Element | null = task;
165 var valid_cursor = cursor;
166 const increment = offset / Math.abs(offset);
168 cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
169 if (!cursor || !(cursor instanceof HTMLElement)) break;
170 if (cursor.style.display !== "none") {
172 valid_cursor = cursor;
174 if (Math.abs(offset) < 0.5) break;
179 moveCursor: function (offset: number): boolean {
180 const active = document.activeElement;
181 if (!active) return false;
182 const dest = this.visibleTaskAtOffset(active, offset);
183 if (dest !== active && dest instanceof HTMLElement) {
190 moveTask: function (offset: number) {
191 const active = document.activeElement;
193 const dest = this.visibleTaskAtOffset(active, offset);
194 if (dest === active) return; // Already extremal
195 var onePastDest: Element | null = this.visibleTaskAtOffset(dest, offset / Math.abs(offset));
196 if (onePastDest == dest) onePastDest = null; // Will become extremal
198 this.setPriority(active, dest, onePastDest);
200 this.setPriority(active, onePastDest, dest);
204 // Change task's priority to be between other tasks a and b.
205 setPriority: function (task: Element, a: Element | null, b: Element | null) {
206 const aPriority = a === null ? 0 : Model.getPriority(a);
207 const bPriority = b === null ? Date.now() : Model.getPriority(b);
208 console.assert(aPriority < bPriority, aPriority, "<", bPriority);
209 const span = bPriority - aPriority;
210 const newPriority = aPriority + 0.1 * span + 0.8 * span * Math.random();
211 console.assert(aPriority < newPriority && newPriority < bPriority, aPriority, "<", newPriority, "<", bPriority);
212 const newPriorityRounded = Math.round(newPriority);
213 const okToRound = aPriority < newPriorityRounded && newPriorityRounded < bPriority;
214 UI.setPriority(task.getAttribute("data-created")!, okToRound ? newPriorityRounded : newPriority, Model.getPriority(task));
217 setState: function (state: string) {
218 const task = document.activeElement;
220 const createTimestamp = task.getAttribute("data-created")!;
221 this.moveCursor(1) || this.moveCursor(-1);
222 return UI.setState(createTimestamp, state, Model.getState(task));
226 const ret = UI.undo();
227 if (ret && ret instanceof HTMLElement) ret.focus();
231 function handleKey(event: any) {
232 if (event.target.tagName === "INPUT") {
233 if (event.key == "Enter") return BrowserUI.addTask(event);
235 if (event.key == "j") return BrowserUI.moveCursor(1);
236 if (event.key == "k") return BrowserUI.moveCursor(-1);
237 if (event.key == "J") return BrowserUI.moveTask(1);
238 if (event.key == "K") return BrowserUI.moveTask(-1);
239 if (event.key == "n") return BrowserUI.focusTaskNameInput(event);
240 if (event.key == "s") return BrowserUI.setState("someday-maybe");
241 if (event.key == "w") return BrowserUI.setState("waiting");
242 if (event.key == "d") return BrowserUI.setState("done");
243 if (event.key == "c") return BrowserUI.setState("cancelled");
244 if (event.key == "X") return BrowserUI.setState("deleted");
245 if (event.key == "u") return BrowserUI.undo();
249 function browserInit() {
250 document.body.addEventListener("keydown", handleKey, { capture: false });
252 BrowserUI.firstVisibleTask()?.focus();