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;
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[] {
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));
17 createTask: function (description: string) {
18 const task = document.createElement("div");
19 task.appendChild(document.createTextNode(description));
20 task.setAttribute("tabindex", "0");
24 addTask: function (description: string) {
25 document.body.appendChild(this.createTask(description)).focus();
28 moveCursor: function (offset: number) {
29 var active = document.activeElement;
30 if (offset === 1 && active) {
31 active = active.nextElementSibling;
33 if (offset === -1 && active) {
34 active = active.previousElementSibling;
36 if (active && active instanceof HTMLElement) active.focus();
41 addTask: function (description: string) {
42 this.applyLogEntry(`${Date.now()} Create ${description}`);
45 applyLogEntry: function (entry: string) {
46 const [timestamp, command, data] = splitN(entry, " ", 2);
47 if (command == "Create") {
53 function handleKey(event: any) {
54 if (event.target.tagName !== "INPUT") {
55 if (event.key == "j") Model.moveCursor(1);
56 if (event.key == "k") Model.moveCursor(-1);
57 if (event.key == "c") {
58 document.getElementById("taskName")!.focus();
59 event.preventDefault();
64 function browserCreateTask(form: any) {
65 if (form.taskName.value) {
66 Log.addTask(form.taskName.value);
68 form.taskName.value = "";
72 function browserInit() {
73 document.body.addEventListener("keydown", handleKey, { capture: false });