From 8f5e289d102c7b28406345ef20792d6fa95794d4 Mon Sep 17 00:00:00 2001 From: "ioannis.petsis" Date: Tue, 1 Dec 2026 14:51:36 +0100 Subject: [PATCH] fix: reply with r on a commit page without focusing the card The keyboard hook resolved actions only inside the focused reply or commit, so a commit page, where nothing has focus on load, ignored r along with q, y, s and o. Mark the page's own commit with data-focused and fall back to it when no card has focus. --- resources/js/components/commit-card.tsx | 1 + resources/js/hooks/use-keyboard-shortcuts.ts | 12 +++--- resources/js/lib/commit-shortcuts.ts | 16 ++++++++ tests/Unit/CommitShortcutScope.test.ts | 43 ++++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 resources/js/lib/commit-shortcuts.ts create mode 100644 tests/Unit/CommitShortcutScope.test.ts diff --git a/resources/js/components/commit-card.tsx b/resources/js/components/commit-card.tsx index 096488bad5..46a0124640 100644 --- a/resources/js/components/commit-card.tsx +++ b/resources/js/components/commit-card.tsx @@ -270,6 +270,7 @@ export function CommitCard({ )} tabIndex={-1} data-commit={commit.id} + data-focused={isFocused ? 'true' : undefined} data-permalink={wasDeleted ? undefined : permalink.url} data-test="commit" onMouseDown={prefetchClickedCommit} diff --git a/resources/js/hooks/use-keyboard-shortcuts.ts b/resources/js/hooks/use-keyboard-shortcuts.ts index de9be7a96e..4d861b2ea9 100644 --- a/resources/js/hooks/use-keyboard-shortcuts.ts +++ b/resources/js/hooks/use-keyboard-shortcuts.ts @@ -5,6 +5,7 @@ import LogController from '@/actions/App/Http/Controllers/LogController'; import NotificationController from '@/actions/App/Http/Controllers/NotificationController'; import StarredCommitController from '@/actions/App/Http/Controllers/StarredCommitController'; import { signIn } from '@/lib/auth'; +import { commitActionScope } from '@/lib/commit-shortcuts'; import { hasModKey } from '@/lib/keyboard'; import { show } from '@/routes/user'; import { edit } from '@/routes/user-profile'; @@ -51,11 +52,12 @@ export function focusCommit(commit: HTMLElement): void { * A deleted commit's actions are inert, which stops pointers and focus but not a scripted click. */ function focusedCommitAction(key: string): HTMLButtonElement | null { - const action = document.activeElement - ?.closest('[data-reply], [data-commit]') - ?.querySelector( - `button[aria-keyshortcuts="${CSS.escape(key)}"]`, - ); + const action = commitActionScope( + document.activeElement, + document, + )?.querySelector( + `button[aria-keyshortcuts="${CSS.escape(key)}"]`, + ); return action && !action.closest('[inert]') ? action : null; } diff --git a/resources/js/lib/commit-shortcuts.ts b/resources/js/lib/commit-shortcuts.ts new file mode 100644 index 0000000000..19ee5e43d6 --- /dev/null +++ b/resources/js/lib/commit-shortcuts.ts @@ -0,0 +1,16 @@ +/** + * The commit card a keyboard action belongs to. + * + * Focus inside a reply or a commit wins. On a commit's own page no card has + * focus until the reader tabs into one, so the page marks its main commit with + * `data-focused` and that card receives the shortcut. + */ +export function commitActionScope( + active: Element | null, + root: ParentNode, +): Element | null { + return ( + active?.closest('[data-reply], [data-commit]') ?? + root.querySelector('[data-commit][data-focused]') + ); +} diff --git a/tests/Unit/CommitShortcutScope.test.ts b/tests/Unit/CommitShortcutScope.test.ts new file mode 100644 index 0000000000..b7680a6b0c --- /dev/null +++ b/tests/Unit/CommitShortcutScope.test.ts @@ -0,0 +1,43 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { commitActionScope } from '../../resources/js/lib/commit-shortcuts.ts'; + +function element(name: string): Element { + return { name } as unknown as Element; +} + +function rootReturning(focused: Element | null): ParentNode { + return { + querySelector: (selector: string) => + selector === '[data-commit][data-focused]' ? focused : null, + } as unknown as ParentNode; +} + +void test('focus inside a reply or commit wins over the page commit', () => { + for (const name of ['reply', 'commit']) { + const focused = element(name); + const active = { closest: () => focused } as unknown as Element; + const root = { + querySelector: () => { + throw new Error('must not fall back to the page commit'); + }, + } as unknown as ParentNode; + + assert.equal(commitActionScope(active, root), focused); + } +}); + +void test('the commit page falls back to its focused commit', () => { + const focused = element('page commit'); + const active = { closest: () => null } as unknown as Element; + + assert.equal(commitActionScope(active, rootReturning(focused)), focused); + assert.equal(commitActionScope(null, rootReturning(focused)), focused); +}); + +void test('nothing focused and no page commit resolves to nothing', () => { + const active = { closest: () => null } as unknown as Element; + + assert.equal(commitActionScope(active, rootReturning(null)), null); + assert.equal(commitActionScope(null, rootReturning(null)), null); +});