]> git.scottworley.com Git - vopamoi/blame - vopamoi.ts
Leave something focused after destroying a task
[vopamoi] / vopamoi.ts
CommitLineData
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;
5const MAX_SAFE_INTEGER = 9007199254740991;
6
7// A sane split that splits N *times*, leaving the last chunk unsplit.
8function 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 16const Model = {
4101e1b1 17 createTask: function (timestamp: string, description: string) {
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);
13c97b99
SW
23 return task;
24 },
974848d3 25
4101e1b1
SW
26 addTask: function (timestamp: string, description: string) {
27 document.body.appendChild(this.createTask(timestamp, description)).focus();
13c97b99 28 },
974848d3 29
799f4e89
SW
30 getTask: function (createTimestamp: string) {
31 for (const task of document.getElementsByClassName("task")) {
32 if (task.getAttribute("data-created") === createTimestamp) {
33 return task;
34 }
35 }
36 },
37
38 destroyTask: function (createTimestamp: string) {
39 const task = this.getTask(createTimestamp);
40 task!.parentElement!.removeChild(task!);
41 },
13c97b99 42};
f1afad9b 43
60a63831
SW
44const Log = (function () {
45 var next_log_index = 0;
46 return {
e88c099c 47 apply: function (entry: string) {
60a63831
SW
48 const [timestamp, command, data] = splitN(entry, " ", 2);
49 if (command == "Create") {
4101e1b1 50 Model.addTask(timestamp, data);
60a63831 51 }
799f4e89
SW
52 if (command == "Destroy") {
53 Model.destroyTask(data.split(" ", 1)[0]);
54 }
60a63831
SW
55 },
56
e88c099c 57 record: function (entry: string) {
60a63831
SW
58 window.localStorage.setItem(`${next_log_index++}`, entry);
59 },
60
e88c099c
SW
61 recordAndApply: function (entry: string) {
62 this.record(entry);
63 this.apply(entry);
60a63831
SW
64 },
65
66 replay: function () {
67 while (true) {
68 const entry = window.localStorage.getItem(`${next_log_index}`);
69 if (entry === null) {
70 break;
71 }
e88c099c 72 this.apply(entry);
60a63831
SW
73 next_log_index++;
74 }
75 },
76 };
77})();
262705dd 78
e88c099c
SW
79const UI = {
80 addTask: function (description: string) {
81 Log.recordAndApply(`${Date.now()} Create ${description}`);
82 },
799f4e89
SW
83 destroyTask: function (createTimestamp: string) {
84 Log.recordAndApply(`${Date.now()} Destroy ${createTimestamp} ${Model.getTask(createTimestamp)?.textContent}`);
85 },
e88c099c
SW
86};
87
caa93fd1
SW
88const BrowserUI = {
89 addTask: function (form: any) {
90 if (form.taskName.value) {
91 UI.addTask(form.taskName.value);
92 form.taskName.value = "";
93 }
94 return false;
95 },
09657615 96
799f4e89
SW
97 destroyTask: function () {
98 const createTimestamp = document.activeElement?.getAttribute("data-created");
61535898
SW
99 this.moveCursor(1) || this.moveCursor(-1);
100 return UI.destroyTask(createTimestamp!);
799f4e89 101 },
caa93fd1 102
09657615
SW
103 focusTaskNameInput: function (event: any) {
104 document.getElementById("taskName")!.focus();
105 event.preventDefault();
106 },
107
108 moveCursor: function (offset: number): boolean {
109 var active = document.activeElement;
110 if (offset === 1 && active) {
111 active = active.nextElementSibling;
112 }
113 if (offset === -1 && active) {
114 active = active.previousElementSibling;
115 }
116 if (active && active instanceof HTMLElement) {
117 active.focus();
118 return true;
119 }
120 return false;
121 },
122};
06ee32a1 123
f1afad9b
SW
124function handleKey(event: any) {
125 if (event.target.tagName !== "INPUT") {
09657615
SW
126 if (event.key == "j") return BrowserUI.moveCursor(1);
127 if (event.key == "k") return BrowserUI.moveCursor(-1);
128 if (event.key == "c") return BrowserUI.focusTaskNameInput(event);
799f4e89 129 if (event.key == "X") return BrowserUI.destroyTask();
f1afad9b
SW
130 }
131}
132
f1afad9b
SW
133function browserInit() {
134 document.body.addEventListener("keydown", handleKey, { capture: false });
60a63831 135 Log.replay();
f1afad9b 136}