From 73be802a0c175ecaa3052a91b31c00f58bf800be Mon Sep 17 00:00:00 2001 From: Carter Watson <54688033+cartwatson@users.noreply.github.com> Date: Wed, 7 May 2025 14:15:00 -0700 Subject: [PATCH] feat: navbar shortcuts using ctrl+shift+num1-0 --- internal/glance/static/js/main.js | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/glance/static/js/main.js b/internal/glance/static/js/main.js index dca713bb..657d1427 100644 --- a/internal/glance/static/js/main.js +++ b/internal/glance/static/js/main.js @@ -205,6 +205,38 @@ function setupSearchBoxes() { } } +function setupNavbarShortcuts() { + const navItems = Array.from( + document.getElementsByClassName("nav-item") + ).filter(item => { + return !item.parentElement.className.includes('mobile'); + }); + + if (navItems.length < 2) { + return; + } + + const keyCodeMap = { + 'Digit1': 1, 'Digit2': 2, 'Digit3': 3, 'Digit4': 4, 'Digit5': 5, + 'Digit6': 6, 'Digit7': 7, 'Digit8': 8, 'Digit9': 9, 'Digit0': 10 + }; + + document.addEventListener("keydown", (event) => { + if (event.ctrlKey && event.shiftKey) { + if (event.code in keyCodeMap) { + let keyNum = keyCodeMap[event.code]; + if (!isNaN(keyNum) && keyNum >= 1 && keyNum <= navItems.length) { + const index = keyNum - 1; + const targetHref = navItems[index].getAttribute('href'); + if (targetHref !== window.location.pathname) { + window.location.href = targetHref; + } + } + } + } + }); +} + function setupDynamicRelativeTime() { const elements = document.querySelectorAll("[data-dynamic-relative-time]"); const updateInterval = 60 * 1000; @@ -667,6 +699,7 @@ async function setupPage() { await setupCalendars(); setupCarousels(); setupSearchBoxes(); + setupNavbarShortcuts(); setupCollapsibleLists(); setupCollapsibleGrids(); setupGroups();