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