Skip to content

Comments

Add keyboard shortcuts for repository file and code search#36416

Open
micahkepe wants to merge 11 commits intogo-gitea:mainfrom
micahkepe:feature/repo-keyboard-shortcuts
Open

Add keyboard shortcuts for repository file and code search#36416
micahkepe wants to merge 11 commits intogo-gitea:mainfrom
micahkepe:feature/repo-keyboard-shortcuts

Conversation

@micahkepe
Copy link

@micahkepe micahkepe commented Jan 21, 2026

Summary

Resolves #36417: 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 clear and unfocus 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.

Screenshots

Screenshot 2026-01-20 at 4 33 27 PM Screenshot 2026-01-20 at 4 33 33 PM

Test Plan

  • Unit tests added in web_src/js/features/repo-shortcuts.test.ts (5 tests)
  • E2E tests added in tests/e2e/repo-shortcuts.test.e2e.ts (6 tests)
  • Manual testing:
    • Navigate to any repository with files
    • Press T → file search input should focus
    • Press S → code search input should focus
    • Press Escape → input should clear and unfocus
    • Verify kbd hints show/hide appropriately

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 21, 2026
@github-actions github-actions bot added modifies/templates This PR modifies the template files modifies/frontend labels Jan 21, 2026
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jan 21, 2026

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.

@micahkepe
Copy link
Author

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

I've refactored to use a declarative approach.

Elements now declare their keyboard shortcuts via data-global-keyboard-shortcut attribute, and a central handler in observer.ts picks them up, similar to the existing data-global-init and data-global-click patterns.

Example:

<input data-global-keyboard-shortcut="s" ...>

Adding new shortcuts now only requires adding the attribute to the element without any JS changes.

@micahkepe micahkepe force-pushed the feature/repo-keyboard-shortcuts branch 2 times, most recently from c3ee89a to 6b95dc0 Compare January 21, 2026 17:45
@silverwind
Copy link
Member

silverwind commented Jan 22, 2026

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.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jan 22, 2026
@silverwind silverwind requested a review from Copilot January 24, 2026 04:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 keydown handler for elements declaring data-global-keyboard-shortcut.
  • Adds code-search hint visibility + Escape handling via a new global init function.
  • Updates repo file search UI to show a T hint 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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@micahkepe micahkepe force-pushed the feature/repo-keyboard-shortcuts branch from bb2d4a8 to 7f4493d Compare January 26, 2026 17:16
@lunny lunny added this to the 1.26.0 milestone Jan 26, 2026
@silverwind
Copy link
Member

silverwind commented Feb 13, 2026

@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)

@micahkepe
Copy link
Author

micahkepe commented Feb 13, 2026

@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-shortcuts

All 6 repo-shortcuts tests pass on webkit and Mobile Safari. On chromium/Mobile Chrome, the beforeAll login hook times out, but this appears to be a pre-existing issue that also affects the existing example.test.e2e.ts tests (the homepage test fails identically). Not related to this PR.

Details are in tests/e2e/README.md. The Go harness (e2e_test.go) starts a Gitea server with fixtures, then runs npx playwright test against it.

@silverwind
Copy link
Member

Ok thanks, I will try that.

@micahkepe
Copy link
Author

@silverwind Thanks for the review! All addressed in the latest push:

  • Removed license headers from e2e test
  • Removed all waitForLoadState calls
  • Replaced CSS selectors with getByPlaceholder('Go to file') / getByPlaceholder('Search code…')
  • Removed z-index from .repo-search-shortcut-hint

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Feb 14, 2026

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 initRepoCodeSearchShortcut, don't use <kbd v-show="!searchQuery && !isInputFocused")

The design could be like this:

<div class="ui small action input">
   <div class="....">
      <input>
      <kbd data-global-keyboard-shortcut="s">
   </div>
   {{template "shared/search/button"}}
</div>

<div class="ui small action input">
   <input>
   <kbd data-global-keyboard-shortcut="t">
</div>

input focus/blur event: (() => {
    toggle kbd
})
document key event: (() => {
    shortcut: focus input
    escape: blur
})

This "kbd" mechanism can be added to a devtest page to make it easier to fine tune and test.

@micahkepe
Copy link
Author

micahkepe commented Feb 15, 2026

@wxiaoguang Thank you for the feedback! Addressed in the latest push:

  • Moved data-global-keyboard-shortcut from <input> to <kbd> - the <kbd>
    element now declares the shortcut
  • Removed initRepoCodeSearchShortcut: no more element-specific init functions
  • Removed Vue v-show / isInputFocused: the generic handler in observer.ts
    manages kbd visibility via focusin/focusout event delegation
  • Generic handler now covers everything: shortcut key → focus sibling
    input, Escape → clear + blur, focus/blur → toggle kbd hint
  • Added devtest page at /devtest/keyboard-shortcut for testing the mechanism

Any input can now get keyboard shortcut support by placing <kbd data-global-keyboard-shortcut="key"> next to it, no JS changes needed.

micahkepe and others added 8 commits February 14, 2026 22:43
…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.
@micahkepe micahkepe force-pushed the feature/repo-keyboard-shortcuts branch from 28b975f to 0cabec3 Compare February 15, 2026 06:43
@silverwind
Copy link
Member

silverwind commented Feb 15, 2026

FYI I'm reworking the e2e tests in #36634. Shouldn't really affect you much here.

@silverwind
Copy link
Member

silverwind commented Feb 22, 2026

e2e test rewrite is merged, please update filenames to tests/e2e/repo/shortcuts.test.ts (removing the .e2e part and adding the repo folder). They will now also run on CI.

@wxiaoguang
Copy link
Contributor

I prefer to remove the e2e test.

There is already a unit test web_src/js/features/repo-shortcuts.test.ts which has good coverage.

@silverwind
Copy link
Member

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.

@micahkepe
Copy link
Author

I have removed the E2E test, let me know if there is anything else!

Copy link
Member

@silverwind silverwind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, works.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/frontend modifies/templates This PR modifies the template files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Add keyboard shortcuts for repository file and code search

5 participants