refactor(shortcut): Use declarative data attributes for keyboard shortcuts

Instead of having JavaScript code guess which elements exist on a page,
elements now declare their keyboard shortcuts via data-global-keyboard-shortcut
attribute. This makes it easier to add new shortcuts and follows Gitea's
existing patterns for data-global-init and data-global-click.
This commit is contained in:
micahkepe
2026-02-14 22:43:52 -08:00
committed by Micah Kepe
parent 7226ecde9a
commit 663612cdab
5 changed files with 91 additions and 138 deletions
+28
View File
@@ -64,6 +64,34 @@ function attachGlobalEvents() {
if (!func) throw new Error(`Global event function "click:${funcName}" not found`);
func(elem, e);
});
// add global "[data-global-keyboard-shortcut]" event handler
// Elements declare their keyboard shortcuts via data-global-keyboard-shortcut attribute.
// When a matching key is pressed, the element is focused (for inputs) or clicked (for buttons/links).
document.addEventListener('keydown', (e: KeyboardEvent) => {
// Don't trigger shortcuts when typing in input fields
const target = e.target as HTMLElement;
if (target.matches('input, textarea, select, [contenteditable="true"]')) {
return;
}
// Don't trigger shortcuts when modifier keys are pressed
if (e.ctrlKey || e.metaKey || e.altKey) {
return;
}
// Find element with matching shortcut (case-insensitive)
const key = e.key.toLowerCase();
const elem = document.querySelector<HTMLElement>(`[data-global-keyboard-shortcut="${key}"]`);
if (!elem) return;
e.preventDefault();
if (elem.matches('input, textarea, select')) {
elem.focus();
} else {
elem.click();
}
});
}
export function initGlobalSelectorObserver(perfTracer: InitPerformanceTracer | null): void {