]> git.scottworley.com Git - vopamoi/blob - vopamoi.ts
56c4813923e46b600d6ab561f6bdb36b16fa0b7d
[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 return createTimestamp && UI.destroyTask(createTimestamp);
100 },
101
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 };
122
123 function handleKey(event: any) {
124 if (event.target.tagName !== "INPUT") {
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);
128 if (event.key == "X") return BrowserUI.destroyTask();
129 }
130 }
131
132 function browserInit() {
133 document.body.addEventListener("keydown", handleKey, { capture: false });
134 Log.replay();
135 }