]> git.scottworley.com Git - vopamoi/blame - vopamoi.ts
Persistence
[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
SW
16const Model = {
17 createTask: function (description: string) {
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
20 task.setAttribute("tabindex", "0");
21 return task;
22 },
974848d3 23
13c97b99 24 addTask: function (description: string) {
f69ff526 25 document.body.appendChild(this.createTask(description)).focus();
13c97b99 26 },
974848d3 27
13c97b99
SW
28 moveCursor: function (offset: number) {
29 var active = document.activeElement;
30 if (offset === 1 && active) {
31 active = active.nextElementSibling;
32 }
33 if (offset === -1 && active) {
34 active = active.previousElementSibling;
35 }
36 if (active && active instanceof HTMLElement) active.focus();
37 },
38};
f1afad9b 39
60a63831
SW
40const Log = (function () {
41 var next_log_index = 0;
42 return {
43 addTask: function (description: string) {
44 this.recordAndApplyLogEntry(`${Date.now()} Create ${description}`);
45 },
121d9948 46
60a63831
SW
47 applyLogEntry: function (entry: string) {
48 const [timestamp, command, data] = splitN(entry, " ", 2);
49 if (command == "Create") {
50 Model.addTask(data);
51 }
52 },
53
54 recordLogEntry: function (entry: string) {
55 window.localStorage.setItem(`${next_log_index++}`, entry);
56 },
57
58 recordAndApplyLogEntry: function (entry: string) {
59 this.recordLogEntry(entry);
60 this.applyLogEntry(entry);
61 },
62
63 replay: function () {
64 while (true) {
65 const entry = window.localStorage.getItem(`${next_log_index}`);
66 if (entry === null) {
67 break;
68 }
69 this.applyLogEntry(entry);
70 next_log_index++;
71 }
72 },
73 };
74})();
262705dd 75
f1afad9b
SW
76function handleKey(event: any) {
77 if (event.target.tagName !== "INPUT") {
13c97b99
SW
78 if (event.key == "j") Model.moveCursor(1);
79 if (event.key == "k") Model.moveCursor(-1);
75a42da4
SW
80 if (event.key == "c") {
81 document.getElementById("taskName")!.focus();
82 event.preventDefault();
83 }
f1afad9b
SW
84 }
85}
86
974848d3 87function browserCreateTask(form: any) {
f1b121ab 88 if (form.taskName.value) {
262705dd 89 Log.addTask(form.taskName.value);
f1b121ab 90 }
974848d3
SW
91 form.taskName.value = "";
92 return false;
93}
f1afad9b
SW
94
95function browserInit() {
96 document.body.addEventListener("keydown", handleKey, { capture: false });
60a63831 97 Log.replay();
f1afad9b 98}