]> git.scottworley.com Git - vopamoi/commitdiff
Fix moving focus over completed tasks
authorScott Worley <scottworley@scottworley.com>
Wed, 26 Jan 2022 18:44:48 +0000 (10:44 -0800)
committerScott Worley <scottworley@scottworley.com>
Thu, 27 Jan 2022 20:21:55 +0000 (12:21 -0800)
It sure would be nice if we could just trigger whatever the browser does
for tab and shift-tab key presses, instead of doing all this.

vopamoi.ts

index 59c287504c5ab604411bd1a757b98d37db38cea7..b90b8956a90e80c2d01bf21635fa138ab9133dc0 100644 (file)
@@ -126,15 +126,22 @@ const BrowserUI = {
   },
 
   moveCursor: function (offset: number): boolean {
-    var active = document.activeElement;
-    if (offset === 1 && active) {
-      active = active.nextElementSibling;
-    }
-    if (offset === -1 && active) {
-      active = active.previousElementSibling;
+    var initial_cursor = document.activeElement;
+    if (!initial_cursor) return false;
+    var cursor: Element | null = initial_cursor;
+    var valid_cursor = cursor;
+    const increment = offset / Math.abs(offset);
+    while (true) {
+      cursor = increment > 0 ? cursor.nextElementSibling : cursor.previousElementSibling;
+      if (!cursor || !(cursor instanceof HTMLElement)) break;
+      if (cursor.style.display !== "none") {
+        offset -= increment;
+        valid_cursor = cursor;
+      }
+      if (Math.abs(offset) < 0.5) break;
     }
-    if (active && active instanceof HTMLElement) {
-      active.focus();
+    if (valid_cursor !== initial_cursor && valid_cursor instanceof HTMLElement) {
+      valid_cursor.focus();
       return true;
     }
     return false;