]> git.scottworley.com Git - vopamoi/blame - vopamoi.ts
Move focusTaskNameInput and moveCursor into BrowserUI
[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");
99 return createTimestamp && UI.destroyTask(createTimestamp);
100 },
caa93fd1 101
09657615
SW
102 focusTaskNameInput: function (event: any) {
103 document.getElementById("taskName")!.focus();
104 event.preventDefault();
105 },
106
107 moveCursor: function (offset: number): boolean {
108 var active = document.activeElement;
109 if (offset === 1 && active) {
110 active = active.nextElementSibling;
111 }
112 if (offset === -1 && active) {
113 active = active.previousElementSibling;
114 }
115 if (active && active instanceof HTMLElement) {
116 active.focus();
117 return true;
118 }
119 return false;
120 },
121};
06ee32a1 122
f1afad9b
SW
123function handleKey(event: any) {
124 if (event.target.tagName !== "INPUT") {
09657615
SW
125 if (event.key == "j") return BrowserUI.moveCursor(1);
126 if (event.key == "k") return BrowserUI.moveCursor(-1);
127 if (event.key == "c") return BrowserUI.focusTaskNameInput(event);
799f4e89 128 if (event.key == "X") return BrowserUI.destroyTask();
f1afad9b
SW
129 }
130}
131
f1afad9b
SW
132function browserInit() {
133 document.body.addEventListener("keydown", handleKey, { capture: false });
60a63831 134 Log.replay();
f1afad9b 135}