-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Add keyboard shortcuts for repository file and code search #36416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wxiaoguang
merged 16 commits into
go-gitea:main
from
micahkepe:feature/repo-keyboard-shortcuts
Feb 23, 2026
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7226ecd
feat(shortcut): Add keyboard shortcuts for repository file and code s…
micahkepe 663612c
refactor(shortcut): Use declarative data attributes for keyboard shor…
micahkepe a548013
fix(shortcut): Address PR review feedback
micahkepe e6ad294
feat(aria): add aria-keyshortcuts for file and repo search shortcuts
micahkepe 28cfd9d
Update web_src/css/repo.css
micahkepe 670be30
fix(styling): use `var(--border-radius)` over hardcoded styling
micahkepe ec3420a
fix(repo-shortcuts): Address maintainer review feedback for repo keyb…
micahkepe 0cabec3
refactor(shortcuts): decouple keyboard shortcut mechanism into generi…
micahkepe fa82503
Merge branch 'main' into feature/repo-keyboard-shortcuts
silverwind d14da9f
chore(e2e): remove `repo-keyboard-shortcuts` E2E file (unit test only)
micahkepe 8ea2482
Merge branch 'main' into feature/repo-keyboard-shortcuts
silverwind b6364c6
Merge branch 'main' into feature/repo-keyboard-shortcuts
wxiaoguang 29e32af
refactor
wxiaoguang 6725c35
remove unneeded imports
silverwind 2c43d36
enable vitest/no-importing-vitest-globals and fix issues
silverwind 8ae9de0
remove incorrect test (AI Hallucination)
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| {{template "devtest/devtest-header"}} | ||
| <div class="page-content devtest ui container"> | ||
| <h1>Keyboard Shortcut</h1> | ||
| <p> | ||
| A <code><kbd data-global-keyboard-shortcut="key"></code> element next to an <code><input></code> declares a keyboard shortcut. | ||
| Pressing the key focuses the input. Pressing <kbd>Escape</kbd> clears and blurs it. | ||
| The hint is hidden automatically when the input is focused or has a value. | ||
| </p> | ||
|
|
||
| <h2>Input with "s" shortcut</h2> | ||
| <div style="position: relative; display: inline-flex; align-items: center;"> | ||
| <input class="ui input" placeholder="Press S to focus" style="padding-right: 36px;"> | ||
| <kbd data-global-keyboard-shortcut="s" class="devtest-shortcut-hint">S</kbd> | ||
| </div> | ||
|
|
||
| <h2>Input with "f" shortcut</h2> | ||
| <div style="position: relative; display: inline-flex; align-items: center;"> | ||
| <input class="ui input" placeholder="Press F to focus" style="padding-right: 36px;"> | ||
| <kbd data-global-keyboard-shortcut="f" class="devtest-shortcut-hint">F</kbd> | ||
| </div> | ||
| </div> | ||
|
|
||
| <style> | ||
| .devtest-shortcut-hint { | ||
| position: absolute; | ||
| right: 8px; | ||
| top: 50%; | ||
| transform: translateY(-50%); | ||
| display: inline-block; | ||
| padding: 2px 6px; | ||
| font-size: 11px; | ||
| line-height: 14px; | ||
| color: var(--color-text-light-2); | ||
| background-color: var(--color-box-body); | ||
| border: 1px solid var(--color-secondary); | ||
| border-radius: var(--border-radius); | ||
| box-shadow: inset 0 -1px 0 var(--color-secondary); | ||
| pointer-events: none; | ||
| } | ||
| </style> | ||
| {{template "devtest/devtest-footer"}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import {describe, test, expect, beforeEach, afterEach} from 'vitest'; | ||
|
|
||
| // The keyboard shortcut mechanism is driven by global event delegation in observer.ts. | ||
| // These tests set up the same event listeners to verify the behavior in isolation. | ||
|
|
||
| function setupKeyboardShortcutListeners() { | ||
| document.addEventListener('keydown', (e: KeyboardEvent) => { | ||
| const target = e.target as HTMLElement; | ||
|
|
||
| if (e.key === 'Escape' && target.matches('input, textarea, select')) { | ||
| const kbd = target.parentElement?.querySelector<HTMLElement>('kbd[data-global-keyboard-shortcut]'); | ||
| if (kbd) { | ||
| (target as HTMLInputElement).value = ''; | ||
| (target as HTMLInputElement).blur(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (target.matches('input, textarea, select') || target.isContentEditable) return; | ||
| if (e.ctrlKey || e.metaKey || e.altKey) return; | ||
|
|
||
| const key = e.key.toLowerCase(); | ||
| const escapedKey = CSS.escape(key); | ||
| const kbd = document.querySelector<HTMLElement>(`kbd[data-global-keyboard-shortcut="${escapedKey}"]`); | ||
| if (!kbd) return; | ||
|
|
||
| e.preventDefault(); | ||
| const input = kbd.parentElement?.querySelector<HTMLInputElement>('input, textarea, select'); | ||
| if (input) input.focus(); | ||
| }); | ||
|
|
||
| document.addEventListener('focusin', (e) => { | ||
| const target = e.target as HTMLElement; | ||
| if (!target.matches('input, textarea, select')) return; | ||
| const kbd = target.parentElement?.querySelector<HTMLElement>('kbd[data-global-keyboard-shortcut]'); | ||
| if (kbd) kbd.style.display = 'none'; | ||
| }); | ||
|
|
||
| document.addEventListener('focusout', (e) => { | ||
| const target = e.target as HTMLElement; | ||
| if (!target.matches('input, textarea, select')) return; | ||
| const kbd = target.parentElement?.querySelector<HTMLElement>('kbd[data-global-keyboard-shortcut]'); | ||
| if (kbd) kbd.style.display = (target as HTMLInputElement).value ? 'none' : ''; | ||
| }); | ||
| } | ||
|
|
||
| describe('Keyboard Shortcut Mechanism', () => { | ||
| let input: HTMLInputElement; | ||
| let kbd: HTMLElement; | ||
|
|
||
| beforeEach(() => { | ||
| document.body.innerHTML = ` | ||
| <div> | ||
| <input placeholder="Search" type="text"> | ||
| <kbd data-global-keyboard-shortcut="s">S</kbd> | ||
| </div> | ||
| `; | ||
| input = document.querySelector('input')!; | ||
| kbd = document.querySelector('kbd')!; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| // Register listeners once for all tests (they persist across tests on document) | ||
| setupKeyboardShortcutListeners(); | ||
|
|
||
| test('Shortcut key focuses the associated input', () => { | ||
| expect(document.activeElement).not.toBe(input); | ||
|
|
||
| document.body.dispatchEvent(new KeyboardEvent('keydown', {key: 's', bubbles: true})); | ||
|
|
||
| expect(document.activeElement).toBe(input); | ||
| }); | ||
|
|
||
| test('Kbd hint hides when input is focused', () => { | ||
| expect(kbd.style.display).toBe(''); | ||
|
|
||
| input.focus(); | ||
|
|
||
| expect(kbd.style.display).toBe('none'); | ||
| }); | ||
|
|
||
| test('Kbd hint shows when input is blurred with empty value', () => { | ||
| input.focus(); | ||
| expect(kbd.style.display).toBe('none'); | ||
|
|
||
| input.blur(); | ||
|
|
||
| expect(kbd.style.display).toBe(''); | ||
| }); | ||
|
|
||
| test('Kbd hint stays hidden when input is blurred with a value', () => { | ||
| input.focus(); | ||
| input.value = 'test'; | ||
|
|
||
| input.blur(); | ||
|
|
||
| expect(kbd.style.display).toBe('none'); | ||
| }); | ||
|
|
||
| test('Escape key clears and blurs the input', () => { | ||
| input.focus(); | ||
| input.value = 'test'; | ||
|
|
||
| const event = new KeyboardEvent('keydown', {key: 'Escape', bubbles: true}); | ||
| input.dispatchEvent(event); | ||
|
|
||
| expect(input.value).toBe(''); | ||
| expect(document.activeElement).not.toBe(input); | ||
| }); | ||
|
|
||
| test('Escape key shows kbd hint after clearing', () => { | ||
| input.focus(); | ||
| input.value = 'test'; | ||
| expect(kbd.style.display).toBe('none'); | ||
|
|
||
| const event = new KeyboardEvent('keydown', {key: 'Escape', bubbles: true}); | ||
| input.dispatchEvent(event); | ||
|
|
||
| expect(kbd.style.display).toBe(''); | ||
| }); | ||
|
|
||
| test('Shortcut does not trigger with modifier keys', () => { | ||
| document.body.dispatchEvent(new KeyboardEvent('keydown', {key: 's', ctrlKey: true, bubbles: true})); | ||
| expect(document.activeElement).not.toBe(input); | ||
|
|
||
| document.body.dispatchEvent(new KeyboardEvent('keydown', {key: 's', metaKey: true, bubbles: true})); | ||
| expect(document.activeElement).not.toBe(input); | ||
|
|
||
| document.body.dispatchEvent(new KeyboardEvent('keydown', {key: 's', altKey: true, bubbles: true})); | ||
| expect(document.activeElement).not.toBe(input); | ||
| }); | ||
|
|
||
| test('Shortcut does not trigger when typing in another input', () => { | ||
| // Add a second input without a shortcut | ||
| const otherInput = document.createElement('input'); | ||
| document.body.append(otherInput); | ||
| otherInput.focus(); | ||
|
|
||
| const event = new KeyboardEvent('keydown', {key: 's', bubbles: true}); | ||
| otherInput.dispatchEvent(event); | ||
|
|
||
| expect(document.activeElement).toBe(otherInput); | ||
| expect(document.activeElement).not.toBe(input); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.