]> git.scottworley.com Git - vopamoi/blobdiff - vopamoi.ts
Put tasks in a div
[vopamoi] / vopamoi.ts
index b90b8956a90e80c2d01bf21635fa138ab9133dc0..725f3a0209c2b41a51d8b18afe2834fa545b6a72 100644 (file)
@@ -14,6 +14,10 @@ function splitN(str: string, delimiter: string, limit: number = MAX_SAFE_INTEGER
 }
 
 const Model = {
+  addTask: function (timestamp: string, description: string) {
+    document.getElementById("tasks")!.appendChild(this.createTask(timestamp, description)).focus();
+  },
+
   createTask: function (timestamp: string, description: string) {
     const task = document.createElement("div");
     task.appendChild(document.createTextNode(description));
@@ -23,8 +27,11 @@ const Model = {
     return task;
   },
 
-  addTask: function (timestamp: string, description: string) {
-    document.body.appendChild(this.createTask(timestamp, description)).focus();
+  destroyTask: function (createTimestamp: string) {
+    const task = this.getTask(createTimestamp);
+    if (task) {
+      task.parentElement!.removeChild(task);
+    }
   },
 
   getTask: function (createTimestamp: string) {
@@ -35,13 +42,6 @@ const Model = {
     }
   },
 
-  destroyTask: function (createTimestamp: string) {
-    const task = this.getTask(createTimestamp);
-    if (task) {
-      task.parentElement!.removeChild(task);
-    }
-  },
-
   setState: function (stateTimestamp: string, createTimestamp: string, state: string) {
     const task = this.getTask(createTimestamp);
     if (task) {
@@ -125,10 +125,8 @@ const BrowserUI = {
     event.preventDefault();
   },
 
-  moveCursor: function (offset: number): boolean {
-    var initial_cursor = document.activeElement;
-    if (!initial_cursor) return false;
-    var cursor: Element | null = initial_cursor;
+  visibleTaskAtOffset(task: Element, offset: number): Element {
+    var cursor: Element | null = task;
     var valid_cursor = cursor;
     const increment = offset / Math.abs(offset);
     while (true) {
@@ -140,8 +138,15 @@ const BrowserUI = {
       }
       if (Math.abs(offset) < 0.5) break;
     }
-    if (valid_cursor !== initial_cursor && valid_cursor instanceof HTMLElement) {
-      valid_cursor.focus();
+    return valid_cursor;
+  },
+
+  moveCursor: function (offset: number): boolean {
+    const active = document.activeElement;
+    if (!active) return false;
+    const dest = this.visibleTaskAtOffset(active, offset);
+    if (dest !== active && dest instanceof HTMLElement) {
+      dest.focus();
       return true;
     }
     return false;