Compare commits

...
Author SHA1 Message Date
l0ng-ai 989a552583 fix(app): open middle-clicked links in the browser 2026-11-21 19:33:02 +08:00
3 changed files with 50 additions and 1 deletions

No files matched your search

+30 -1
View File
@@ -5,7 +5,7 @@ const openUrl = vi.fn(async (_url: string) => {});
vi.mock("./ipc", () => ({ isTauri: () => true }));
vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl }));
import { openExternalUrl } from "./openUrl";
import { installMiddleClickLinks, openExternalUrl } from "./openUrl";
describe("openExternalUrl", () => {
beforeEach(() => openUrl.mockClear());
@@ -30,3 +30,32 @@ describe("openExternalUrl", () => {
expect(openUrl).not.toHaveBeenCalled();
});
});
describe("middle click on a link", () => {
beforeEach(() => openUrl.mockClear());
const middleClick = (target: Element) => {
const event = new MouseEvent("auxclick", { button: 1, bubbles: true, cancelable: true });
target.dispatchEvent(event);
return event;
};
it("opens a web link in the browser instead of letting the webview navigate (#4992)", async () => {
const off = installMiddleClickLinks();
document.body.innerHTML = '<p><a href="https://example.com/pr/1"><code>#1</code></a><a href="/local">x</a></p>';
const event = middleClick(document.querySelector("code")!);
expect(event.defaultPrevented).toBe(true);
await vi.waitFor(() => expect(openUrl).toHaveBeenCalledWith("https://example.com/pr/1"));
// A link into the app itself is not handed to the browser, nor followed.
expect(middleClick(document.querySelector('a[href="/local"]')!).defaultPrevented).toBe(true);
// Left clicks and other targets are left alone.
const left = new MouseEvent("auxclick", { button: 2, bubbles: true, cancelable: true });
document.querySelector("code")!.dispatchEvent(left);
expect(left.defaultPrevented).toBe(false);
expect(middleClick(document.querySelector("p")!).defaultPrevented).toBe(false);
off();
expect(middleClick(document.querySelector("code")!).defaultPrevented).toBe(false);
expect(openUrl).toHaveBeenCalledTimes(1);
});
});
+18
View File
@@ -22,3 +22,21 @@ export async function openExternalUrl(url: string): Promise<void> {
const { openUrl } = await import("@tauri-apps/plugin-opener");
await openUrl(url);
}
/**
* A middle click on a link (`auxclick`) would have the webview open it in a new window or
* navigate this window away from the IDE. A web link opens in the default browser instead,
* as a left click on it does; any other link does nothing.
*/
export function installMiddleClickLinks(target: Document = document): () => void {
const onAuxClick = (e: MouseEvent) => {
if (e.button !== 1 || e.defaultPrevented) return;
const link = (e.target as Element | null)?.closest?.("a[href]");
if (!(link instanceof HTMLAnchorElement)) return;
e.preventDefault();
const url = new URL(link.href, location.href);
if (/^https?:$/.test(url.protocol) && url.origin !== location.origin) void openExternalUrl(url.href);
};
target.addEventListener("auxclick", onAuxClick);
return () => target.removeEventListener("auxclick", onAuxClick);
}
+2
View File
@@ -4,6 +4,7 @@ import "./styles/global.css";
import { installRepeatedDoubleClick } from "./platform/doubleClick";
import { installErrorLogging, logError } from "./platform/errorLog";
import { installImeKeyGuard } from "./platform/ime";
import { installMiddleClickLinks } from "./platform/openUrl";
import { call, commands, isTauri } from "./platform/ipc";
import { installPageSwipeTracking } from "./platform/pageSwipe";
import { guessOs } from "./platform/platformInfo";
@@ -49,6 +50,7 @@ export async function installCommonWindowBoot(): Promise<void> {
toggleMaximize: () => call(commands.windowToggleZoom()).catch((e) => console.warn("cannot zoom the window", e)),
});
installPointerFocus();
installMiddleClickLinks();
installImeKeyGuard();
installErrorLogging();
installPageSwipeTracking(window);