Add keyboard shortcuts for repository file and code search#36416
Add keyboard shortcuts for repository file and code search#36416micahkepe wants to merge 11 commits intogo-gitea:mainfrom
Conversation
|
It needs to make the elements declare what keyboard shortcut they support, but not let the code to guess which element is on current page. Otherwise it will be very difficult to support more, ref : #5796 Update: some maintainers have discussed in #5796 a lot, they might have some ideas and can provide more inputs. |
I've refactored to use a declarative approach. Elements now declare their keyboard shortcuts via Example: <input data-global-keyboard-shortcut="s" ...>Adding new shortcuts now only requires adding the attribute to the element without any JS changes. |
c3ee89a to
6b95dc0
Compare
|
Thanks. Note that E2E tests currently don't execute as part of the CI. We should fix that eventually but until then it's best if another contributor can run them locally to confirm they work. |
There was a problem hiding this comment.
Pull request overview
Adds GitHub-like keyboard shortcuts for repository navigation/search (focus “Go to file” with T, focus “Search code” with S, and clear/unfocus via Escape), plus UI hints to advertise the shortcuts.
Changes:
- Introduces a global
keydownhandler for elements declaringdata-global-keyboard-shortcut. - Adds code-search hint visibility +
Escapehandling via a new global init function. - Updates repo file search UI to show a
Thint and blur/clear behavior, with accompanying unit + E2E tests.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| web_src/js/modules/observer.ts | Adds global keyboard shortcut dispatch based on data-global-keyboard-shortcut. |
| web_src/js/index-domready.ts | Registers repo shortcut init function during app init. |
| web_src/js/features/repo-shortcuts.ts | Adds code-search hint/escape behavior via data-global-init. |
| web_src/js/features/repo-shortcuts.test.ts | Unit tests for code-search hint visibility and Escape behavior. |
| web_src/js/components/RepoFileSearch.vue | Adds T shortcut attribute, hint rendering, and blur-on-escape behavior. |
| web_src/css/repo.css | Adds styling for shortcut hints and wrapper positioning/overrides. |
| tests/e2e/repo-shortcuts.test.e2e.ts | Playwright coverage for focusing inputs and hint show/hide behavior. |
| templates/repo/home_sidebar_top.tmpl | Adds code search shortcut attribute, hint element, and wrapper class. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fb1bab4 to
7f14c0a
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bb2d4a8 to
7f4493d
Compare
|
@micahkepe how are you running those e2e tests? Which commands? (It might sound like a strange question but I have ignored the "demo" e2e tests that exist in the repo so far. I plan to fix them up soon, but any pointers woul be much appreciated) |
I ran them with: make playwright
make test-e2e-sqlite#repo-shortcutsAll 6 repo-shortcuts tests pass on webkit and Mobile Safari. On chromium/Mobile Chrome, the Details are in |
|
Ok thanks, I will try that. |
|
@silverwind Thanks for the review! All addressed in the latest push:
|
|
Some design problems: if you are introducing the global event handlers for a general approach, then the implementation should be fully decoupled from the elements (e.g.: don't use The design could be like this: This "kbd" mechanism can be added to a |
|
@wxiaoguang Thank you for the feedback! Addressed in the latest push:
Any input can now get keyboard shortcut support by placing |
…earch Add GitHub-like keyboard shortcuts for repository navigation: - Press 'T' to focus the "Go to file" search input - Press 'S' to focus the "Search code" input - Press 'Escape' to blur focused search inputs Both search inputs display a keyboard hint (kbd element) showing the available shortcut. The hint hides when the input is focused or has a value. Includes unit tests and e2e tests for the new functionality.
…tcuts 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.
- Use `target.isContentEditable` instead of `[contenteditable="true"]` selector to properly detect all contenteditable elements - Use `CSS.escape()` for keyboard shortcut key to handle special characters - Rename scoped CSS class to `.repo-file-search-shortcut-hint` to avoid conflict with global `.repo-search-shortcut-hint` styles
Co-authored-by: silverwind <me@silverwind.io> Signed-off-by: Micah Kepe <micahkepe@gmail.com>
…oard shortcuts - Remove license headers from e2e test (not used in TypeScript files) - Remove waitForLoadState calls (Playwright waits implicitly) - Use getByPlaceholder selectors instead of CSS class selectors - Remove unnecessary z-index from shortcut hint styling
…c kbd handler Move data-global-keyboard-shortcut from <input> to <kbd> elements so the shortcut hint itself declares the shortcut key. The generic handler in observer.ts now manages everything: focusing the sibling input on shortcut key press, toggling kbd visibility on focus/blur, and clearing+blurring on Escape. This removes the element-specific initRepoCodeSearchShortcut function and Vue v-show logic in favor of a fully decoupled approach. Any input can now gain keyboard shortcut support by placing a sibling <kbd data-global-keyboard-shortcut="key"> element. Adds a devtest page at /devtest/keyboard-shortcut for manual testing.
28b975f to
0cabec3
Compare
|
FYI I'm reworking the e2e tests in #36634. Shouldn't really affect you much here. |
|
e2e test rewrite is merged, please update filenames to |
|
I prefer to remove the e2e test. There is already a unit test |
|
Unit test is more lightweight than e2e, yes. Also I've learned that e2e are very prone to being flaky, so we need to be very careful which ones we merge. |
|
I have removed the E2E test, let me know if there is anything else! |
Summary
Resolves #36417: Add GitHub-like keyboard shortcuts for repository navigation:
Tto focus the "Go to file" search inputSto focus the "Search code" inputEscapeto clear and unfocus search inputsBoth search inputs display a keyboard hint (
kbdelement) showing the available shortcut. The hint hides when the input is focused or has a value.Screenshots
Test Plan
web_src/js/features/repo-shortcuts.test.ts(5 tests)tests/e2e/repo-shortcuts.test.e2e.ts(6 tests)T→ file search input should focusS→ code search input should focusEscape→ input should clear and unfocus