Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Extensions/keyboardShortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"ctrl+tab": { callback: () => rotateSidebar(1) },
"ctrl+shift+tab": { callback: () => rotateSidebar(-1) },

// Rotate through sidebar items using Shift+J/Shift+K (Vim-style).
"shift+j": { callback: () => rotateSidebar(1) },
"shift+k": { callback: () => rotateSidebar(-1) },

Comment on lines +35 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Shift+J/K bindings look good; safeguard rotateSidebar index to avoid NaN crash

If findActiveIndex can’t find a match while activeLink or activePage exists, it returns undefined, making findActiveIndex(...) + direction become NaN and allItems[index] will throw. Add a fallback return at the end of findActiveIndex or coerce in rotateSidebar.

Add a fallback in findActiveIndex (outside this hunk):

function findActiveIndex(allItems) {
  // ...existing logic...
  return -1; // ensure a number is always returned
}

Optionally harden in rotateSidebar:

const activeIdx = findActiveIndex(allItems);
let index = (typeof activeIdx === "number" ? activeIdx : -1) + direction;
🤖 Prompt for AI Agents
In Extensions/keyboardShortcut.js around lines 35 to 38, the Shift+J/K handlers
call rotateSidebar which relies on findActiveIndex returning a number; if
findActiveIndex returns undefined the index math becomes NaN and accessing
allItems[index] will throw. Modify findActiveIndex (elsewhere in the file) to
always return a number (e.g., return -1 at the end) and additionally harden
rotateSidebar by coercing the result to a numeric fallback before adding
direction (e.g., treat non-number as -1) so index math cannot produce NaN.

// Focus on the app content before scrolling using Shift+PageUp and Shift+PageDown
"shift+pageup": { callback: () => focusOnApp() },
"shift+pagedown": { callback: () => focusOnApp() },
Expand All @@ -40,6 +44,10 @@
j: { callback: () => createScrollCallback(SCROLL_STEP) },
k: { callback: () => createScrollCallback(-SCROLL_STEP) },

// Scroll half-page using 'u' and 'd' keys
d: { callback: () => scrollHalfPage(1) },
u: { callback: () => scrollHalfPage(-1) },

// Scroll to the top ('gg') or bottom ('Shift+g') of the page
"g g": { callback: () => scrollToPosition(0) },
"shift+g": { callback: () => scrollToPosition(1) },
Expand Down Expand Up @@ -117,9 +125,8 @@
const scrollInterval = setInterval(() => {
app.scrollTop += step;
}, 10);
document.addEventListener("keyup", () => {
clearInterval(scrollInterval);
});
const clear = () => clearInterval(scrollInterval);
document.addEventListener("keyup", clear, { once: true });
}
}

Expand All @@ -128,6 +135,15 @@
app.scroll(0, position === 0 ? 0 : app.scrollHeight);
}

function scrollHalfPage(direction /* 1 | -1 */) {
const app = focusOnApp();
if (!app) return;
const delta = Math.floor(app.clientHeight / 2) * direction;
const maxTop = Math.max(0, app.scrollHeight - app.clientHeight);
const nextTop = Math.max(0, Math.min(app.scrollTop + delta, maxTop));
app.scroll(0, nextTop);
}

/**
* @returns {number | undefined}
* @param {NodeListOf<Element>} allItems
Expand Down
Loading