0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-11 00:43:40 +02:00
gitea/web_src/js/features/repo-shortcuts.ts
micahkepe 663612cdab 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.
2026-02-14 22:43:52 -08:00

48 lines
1.4 KiB
TypeScript

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
import {registerGlobalInitFunc} from '../modules/observer.ts';
/**
* Initialize the code search input with shortcut hint visibility management.
* The shortcut hint is hidden when the input has a value or is focused.
* Pressing Escape clears the input and blurs it.
*/
export function initRepoCodeSearchShortcut(el: HTMLInputElement): void {
const shortcutHint = el.parentElement?.querySelector<HTMLElement>('.repo-search-shortcut-hint');
if (!shortcutHint) return;
let isFocused = false;
const updateHintVisibility = () => {
shortcutHint.style.display = (el.value || isFocused) ? 'none' : '';
};
// Check initial value (e.g., from browser autofill or back navigation)
updateHintVisibility();
el.addEventListener('input', updateHintVisibility);
el.addEventListener('change', updateHintVisibility);
el.addEventListener('focus', () => {
isFocused = true;
updateHintVisibility();
});
el.addEventListener('blur', () => {
isFocused = false;
updateHintVisibility();
});
// Handle Escape key to clear and blur the code search input
el.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === 'Escape') {
el.value = '';
updateHintVisibility();
el.blur();
}
});
}
export function initRepoShortcuts(): void {
registerGlobalInitFunc('initRepoCodeSearchShortcut', initRepoCodeSearchShortcut);
}