]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
151c5e06103d8e8f68791350c0361c56fe5f9fdf
[vopamoi] / vopamoi.ts
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
16 const Model = {
17 createTask: function (timestamp: string, description: string) {
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 return task;
24 },
25
26 addTask: function (timestamp: string, description: string) {
27 document.body.appendChild(this.createTask(timestamp, description)).focus();
28 },
29
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 },
42 };
43
44 const Log = (function () {
45 var next_log_index = 0;
46 return {
47 apply: function (entry: string) {
48 const [timestamp, command, data] = splitN(entry, " ", 2);
49 if (command == "Create") {
50 Model.addTask(timestamp, data);
51 }
52 if (command == "Destroy") {
53 Model.destroyTask(data.split(" ", 1)[0]);
54 }
55 },
56
57 record: function (entry: string) {
58 window.localStorage.setItem(`${next_log_index++}`, entry);
59 },
60
61 recordAndApply: function (entry: string) {
62 this.record(entry);
63 this.apply(entry);
64 },
65
66 replay: function () {
67 while (true) {
68 const entry = window.localStorage.getItem(`${next_log_index}`);
69 if (entry === null) {
70 break;
71 }
72 this.apply(entry);
73 next_log_index++;
74 }
75 },
76 };
77 })();
78
79 const UI = {
80 addTask: function (description: string) {
81 Log.recordAndApply(`${Date.now()} Create ${description}`);
82 },
83 destroyTask: function (createTimestamp: string) {
84 Log.recordAndApply(`${Date.now()} Destroy ${createTimestamp} ${Model.getTask(createTimestamp)?.textContent}`);
85 },
86 };
87
88 const 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 },
96
97 destroyTask: function () {
98 const createTimestamp = document.activeElement?.getAttribute("data-created");
99 this.moveCursor(1) || this.moveCursor(-1);
100 return UI.destroyTask(createTimestamp!);
101 },
102
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 };
123
124 function handleKey(event: any) {
125 if (event.target.tagName !== "INPUT") {
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);
129 if (event.key == "X") return BrowserUI.destroyTask();
130 }
131 }
132
133 function browserInit() {
134 document.body.addEventListener("keydown", handleKey, { capture: false });
135 Log.replay();
136 }