fix(web): middle-click opens pull requests in the browser, like cmd+click (#19854)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
cd34df59a1
commit
00cdceb196
8 files changed
+43
-7
No files matched your search
@@ -67,6 +67,14 @@ describe("resolveLinkTarget", () => {
|
||||
canOpenInApp: true,
|
||||
}),
|
||||
).toBe("system");
|
||||
expect(
|
||||
resolveLinkTarget({
|
||||
url: "https://example.com/",
|
||||
event: { metaKey: false, ctrlKey: false, button: 1 },
|
||||
preference: "app",
|
||||
canOpenInApp: true,
|
||||
}),
|
||||
).toBe("system");
|
||||
});
|
||||
|
||||
it("leaves non-web schemes to the shell", () => {
|
||||
|
||||
@@ -15,8 +15,12 @@ import { isPreviewAvailableFor } from "./previewRuntime";
|
||||
|
||||
export interface ResolveLinkTargetInput {
|
||||
readonly url: string;
|
||||
/** Cmd/Ctrl-click always goes to the system browser, whatever the default. */
|
||||
readonly event: { readonly metaKey: boolean; readonly ctrlKey: boolean };
|
||||
/** Cmd/Ctrl-click and middle-click always go to the system browser, whatever the default. */
|
||||
readonly event: {
|
||||
readonly metaKey: boolean;
|
||||
readonly ctrlKey: boolean;
|
||||
readonly button?: number | undefined;
|
||||
};
|
||||
readonly preference: BrowserLinkTarget;
|
||||
/** Whether this client has an in-app browser and a thread to open it beside. */
|
||||
readonly canOpenInApp: boolean;
|
||||
@@ -30,7 +34,7 @@ export interface ResolveLinkTargetInput {
|
||||
* already treat it.
|
||||
*/
|
||||
export function resolveLinkTarget(input: ResolveLinkTargetInput): BrowserLinkTarget {
|
||||
if (input.event.metaKey || input.event.ctrlKey) return "system";
|
||||
if (input.event.metaKey || input.event.ctrlKey || input.event.button === 1) return "system";
|
||||
if (input.preference !== "app") return "system";
|
||||
if (!input.canOpenInApp) return "system";
|
||||
if (!isWebUrl(input.url)) return "system";
|
||||
|
||||
@@ -34,7 +34,11 @@ const NO_MODIFIER = { metaKey: false, ctrlKey: false } as const;
|
||||
export function useOpenLink(threadRef: ScopedThreadRef | null | undefined): (
|
||||
url: string,
|
||||
options?: {
|
||||
readonly event?: { readonly metaKey: boolean; readonly ctrlKey: boolean };
|
||||
readonly event?: {
|
||||
readonly metaKey: boolean;
|
||||
readonly ctrlKey: boolean;
|
||||
readonly button?: number | undefined;
|
||||
};
|
||||
/** Thread to open beside when it is not the hook's own, e.g. a sidebar row's. */
|
||||
readonly threadRef?: ScopedThreadRef | undefined;
|
||||
},
|
||||
|
||||
@@ -395,6 +395,7 @@ export function ThreadDetailsPrRow({
|
||||
part={part}
|
||||
aria-label={openAriaLabel}
|
||||
onClick={onOpen}
|
||||
onAuxClick={onOpen}
|
||||
/>
|
||||
}
|
||||
>
|
||||
|
||||
@@ -113,6 +113,7 @@ export function PullRequestChip(props: {
|
||||
aria-label={`Open ${props.kindLabel} ${props.label}: ${props.metadata.title}`}
|
||||
data-markdown-copy={props.copyMarkdown}
|
||||
onClick={(event) => props.onOpen(event, props.metadata.url)}
|
||||
onAuxClick={(event) => props.onOpen(event, props.metadata.url)}
|
||||
>
|
||||
<StateIcon />
|
||||
<ContextChipLabel>{props.label}</ContextChipLabel>
|
||||
|
||||
@@ -1876,6 +1876,10 @@ export function PullRequestDetailPanel({
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void readLocalApi()?.shell.openExternal(detail.url)}
|
||||
onAuxClick={(event) => {
|
||||
if (event.button === 1)
|
||||
void readLocalApi()?.shell.openExternal(detail.url);
|
||||
}}
|
||||
onContextMenu={(event) => openNumberContextMenu(event, detail)}
|
||||
className={cn(
|
||||
"inline-flex shrink-0 cursor-pointer items-center gap-0.5 font-medium underline-offset-2 hover:underline",
|
||||
@@ -1931,6 +1935,10 @@ export function PullRequestDetailPanel({
|
||||
type="button"
|
||||
tabIndex={condensed ? 0 : -1}
|
||||
onClick={() => void readLocalApi()?.shell.openExternal(detail.url)}
|
||||
onAuxClick={(event) => {
|
||||
if (event.button === 1)
|
||||
void readLocalApi()?.shell.openExternal(detail.url);
|
||||
}}
|
||||
onContextMenu={(event) => openNumberContextMenu(event, detail)}
|
||||
className={cn(
|
||||
"inline-flex shrink-0 cursor-pointer items-center gap-0.5 font-medium underline-offset-2 hover:underline",
|
||||
|
||||
@@ -221,6 +221,12 @@ describe("shouldOpenPullRequestExternally", () => {
|
||||
expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
|
||||
});
|
||||
|
||||
it("uses the browser for middle-click", () => {
|
||||
expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false, button: 1 })).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps an unmodified click in the pull request view", () => {
|
||||
expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
|
||||
});
|
||||
|
||||
@@ -126,10 +126,12 @@ export function findProjectOnChangeRequestHost(
|
||||
*
|
||||
* With "Open pull requests in T3 Code" turned off, every link is left alone.
|
||||
*/
|
||||
/** Cmd/Ctrl-click and middle-click send a pull request to the browser instead of the panel. */
|
||||
export function shouldOpenPullRequestExternally(
|
||||
event: Pick<MouseEvent<HTMLElement>, "metaKey" | "ctrlKey">,
|
||||
event: Pick<MouseEvent<HTMLElement>, "metaKey" | "ctrlKey"> &
|
||||
Partial<Pick<MouseEvent<HTMLElement>, "button">>,
|
||||
): boolean {
|
||||
return event.metaKey || event.ctrlKey;
|
||||
return event.metaKey || event.ctrlKey || event.button === 1;
|
||||
}
|
||||
|
||||
export function useOpenChangeRequestLink(
|
||||
@@ -256,11 +258,13 @@ export function useOpenPrLink(threadRef?: ScopedThreadRef) {
|
||||
const openLink = useOpenLink(threadRef);
|
||||
return useCallback(
|
||||
(event: MouseEvent<HTMLElement>, prUrl: string, targetThreadRef?: ScopedThreadRef) => {
|
||||
// Buttons also pass this to onAuxClick for middle-click; a right-click is not an open.
|
||||
if (event.button > 1) return false;
|
||||
event.stopPropagation();
|
||||
const openInBrowser = shouldOpenPullRequestExternally(event);
|
||||
const isAnchor =
|
||||
event.currentTarget instanceof HTMLAnchorElement && event.currentTarget.href.length > 0;
|
||||
// A real link already knows how to cmd/ctrl+click. Leave its default
|
||||
// A real link already knows how to cmd/ctrl/middle-click. Leave its default
|
||||
// action alone so the browser (or Electron's window-open handler) opens
|
||||
// the host. Buttons have no href, so they still go through openExternal.
|
||||
if (openInBrowser && isAnchor) return false;
|
||||
|
||||
Reference in new issue
Block a user