Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/js/components/commit-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 7 additions & 5 deletions resources/js/hooks/use-keyboard-shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<HTMLButtonElement>(
`button[aria-keyshortcuts="${CSS.escape(key)}"]`,
);
const action = commitActionScope(
document.activeElement,
document,
)?.querySelector<HTMLButtonElement>(
`button[aria-keyshortcuts="${CSS.escape(key)}"]`,
);

return action && !action.closest('[inert]') ? action : null;
}
Expand Down
16 changes: 16 additions & 0 deletions resources/js/lib/commit-shortcuts.ts
Original file line number Diff line number Diff line change
@@ -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]')
);
}
43 changes: 43 additions & 0 deletions tests/Unit/CommitShortcutScope.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});