Skip to content

feat(preview): open a middle-clicked link in a background tab (#1087) - #1091

Merged
PathGao merged 1 commit into
masterfrom
feat/1087-middle-click-link
Nov 19, 2026
Merged

PathGao merged 1 commit into
masterfrom
feat/1087-middle-click-link

Conversation

@PathGao

@PathGao PathGao commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Middle-clicking a link to another Markdown file did nothing on Windows. On macOS, in a build without the guard below, WebKit navigated the whole window to the link's href and left a bare "404 Not Found" page until restart. Now a middle-click opens the file in a new tab behind the current one, as browsers do. Plain click, Ctrl/Cmd-click, the "Open Links in a New Tab" setting and the context menu keep their current behavior.

Mechanism

  • The preview handles middle-click on mousedown with button 1, the same way a tab's middle-click close does. A Markdown target goes to openInBackgroundTab. Other links (external URLs, local non-Markdown files) open as a plain click would.
  • openInBackgroundTab adds the tab with addTab(..., activate = false) and marks it unread, so it is read the first time it is shown, through the existing lazy-restore path. Nothing is loaded into the tab being read, and the file joins recent files only when that read succeeds, so a dead link leaves no entry.
  • A #heading in the link is stored on the tab as pendingAnchor. readActiveIfUnread hands it back once after that first read, and the viewer scrolls to it with the same scrollToAnchorWhenReady call the Ctrl-click path uses.
  • If the file is already open in this window, no tab is added and none is activated. That tab flashes for 700ms and is scrolled into view in the tab bar. Under prefers-reduced-motion the highlight is held still instead of animated.
  • Cancelling mousedown does not stop WebKit's own middle-click navigation, which runs on auxclick. blockMiddleClickNavigation cancels auxclick and click with button 1 on any a[href], on window in the capture phase.

Tests

tabs.spec.ts checks that a background open keeps the active tab and reads on first show, that an already-open file flashes without changing the active tab or tab count, that the heading is handed over once, after the read, never to the tab being read, and that recent files are written by the read rather than the click. preview.spec.ts checks the navigation guard, and preview.test.ts the middle-click routing.

Verification

  • npm run check, npm run lint, npm test (962 passed), npx vitest run (776 passed), vite build and check-startup-chunk. No Rust changes.
  • In a macOS test build: middle-click opened the target in a background tab, switching to it landed on the linked heading, middle-clicking an open file flashed its tab, and Cmd-click still opened and switched.
  • Not run on Windows or Linux.

Closes #1087

Middle-clicking a link to another Markdown file opens it in a new tab
behind the current one, scrolled to the link's heading when first shown.
If the file is already open in this window, its tab flashes instead.
Other links open as a plain click would. A window-level guard stops
WebKit from navigating the whole webview on a middle-clicked link.
@PathGao
PathGao merged commit 61aee3c into master Nov 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Middle-click on a link should open it in a new tab

1 participant