Skip to content

Commit 31dd042

Browse files
committed
Fix(tabs): middle-click close no longer pastes the primary selection (#498)
On Linux the middle button is two gestures at once: close this tab, and paste the PRIMARY selection. The tab already cancelled its own mousedown and auxclick, which only helps when Chromium targets the paste at the element under the pointer. Under Wayland it can deliver the paste to the FOCUSED editor instead, and our mousedown preventDefault deliberately keeps focus there, so whatever was last selected landed at the caret of the note that took over after the close. The close now cancels the middle-button mouseup as well, and arms a guard that catches the paste event itself wherever it is delivered: the one chokepoint every delivery path shares. One paste, inside a 150ms window, then it disarms, so a paste the user actually asks for cannot be eaten; re-arming extends the window rather than stacking guards. Verified at the event level in the built app (the paste riding the click is cancelled, later pastes untouched) and with unit tests on the guard; there is no Wayland machine here, so the reporter is asked to confirm on 2.20.0.
1 parent 4d49b6f commit 31dd042

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎packages/app-core/src/components/EditorPane.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ import {
224224
} from '../lib/pane-mode'
225225
import { resolveCommentAnchor, selectionToCommentAnchor } from '../lib/comments'
226226
import { ZEN_OPEN_EDITOR_CONTEXT_MENU_EVENT } from '../lib/keyboard-context-menu'
227+
import { armMiddleClickPasteGuard } from '../lib/middle-click-paste-guard'
227228
import {
228229
assetPathFromTab,
229230
assetTitleFromPath,
@@ -2874,9 +2875,20 @@ export function EditorPane({ pane }: { pane: PaneLeaf }): JSX.Element {
28742875
e.preventDefault()
28752876
}
28762877
}}
2878+
onMouseUp={(e) => {
2879+
// Some Chromium paths run the Linux primary-selection paste as the
2880+
// mouseup default action, before auxclick can refuse it. (#498)
2881+
if (e.button === 1) {
2882+
e.preventDefault()
2883+
}
2884+
}}
28772885
onAuxClick={(e) => {
28782886
if (e.button === 1) {
28792887
e.preventDefault()
2888+
// Under Wayland the paste can be delivered to the FOCUSED editor
2889+
// rather than to this tab, so cancelling events here is not
2890+
// enough on its own; the guard catches the paste itself. (#498)
2891+
armMiddleClickPasteGuard()
28802892
void closeTabInPane(paneId, tab.path)
28812893
}
28822894
}}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @vitest-environment jsdom
2+
3+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
4+
import { armMiddleClickPasteGuard } from './middle-click-paste-guard'
5+
6+
// jsdom has no ClipboardEvent constructor, and the guard only ever reads the
7+
// event's name, so a plain Event is the honest double.
8+
function firePaste(): Event {
9+
const event = new Event('paste', { bubbles: true, cancelable: true })
10+
document.body.dispatchEvent(event)
11+
return event
12+
}
13+
14+
describe('armMiddleClickPasteGuard', () => {
15+
beforeEach(() => {
16+
vi.useFakeTimers()
17+
})
18+
afterEach(() => {
19+
// Drain any armed guard so one test's window cannot leak into the next.
20+
vi.runAllTimers()
21+
vi.useRealTimers()
22+
})
23+
24+
it('cancels a paste that arrives inside the window', () => {
25+
armMiddleClickPasteGuard()
26+
expect(firePaste().defaultPrevented).toBe(true)
27+
})
28+
29+
it('disarms after one paste, so the next real paste goes through', () => {
30+
armMiddleClickPasteGuard()
31+
firePaste()
32+
expect(firePaste().defaultPrevented).toBe(false)
33+
})
34+
35+
it('disarms on the deadline when no paste ever arrives', () => {
36+
armMiddleClickPasteGuard(150)
37+
vi.advanceTimersByTime(151)
38+
expect(firePaste().defaultPrevented).toBe(false)
39+
})
40+
41+
it('re-arming extends the window instead of stacking guards', () => {
42+
armMiddleClickPasteGuard(150)
43+
armMiddleClickPasteGuard(150)
44+
firePaste()
45+
// A second guard would have eaten this one too.
46+
expect(firePaste().defaultPrevented).toBe(false)
47+
})
48+
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Suppress the Linux primary-selection paste that rides on a middle click.
2+
//
3+
// On Linux, middle click is two things at once: our "close this tab" gesture
4+
// and the desktop's "paste the PRIMARY selection" gesture. The textbook
5+
// defense (preventDefault on the tab's mousedown and auxclick) is already in
6+
// place and is not enough everywhere: under Wayland, Chromium can deliver the
7+
// paste to the FOCUSED editable rather than to the element under the pointer,
8+
// so cancelling events on the tab never touches it and the selection lands in
9+
// the editor that takes over after the close (#498).
10+
//
11+
// The one chokepoint every delivery path shares is the `paste` clipboard event
12+
// itself: Blink dispatches it, cancelable, to the target before inserting
13+
// anything. So the close gesture arms a capture-phase listener at the window
14+
// for a few tens of milliseconds, and whatever paste arrives inside that
15+
// window is cancelled before CodeMirror can see it. A real paste the user asks
16+
// for cannot land inside the window: hands are on the mouse mid-click, and the
17+
// guard disarms itself immediately after one event or the deadline, whichever
18+
// comes first.
19+
20+
/** Long enough for the click's paste to arrive, far too short to eat a real one. */
21+
const GUARD_WINDOW_MS = 150
22+
23+
let disarm: (() => void) | null = null
24+
25+
/**
26+
* Arm the guard for one paste or `windowMs`, whichever comes first.
27+
*
28+
* Call it from the middle-click handler BEFORE closing the tab, so the guard
29+
* exists no matter how quickly the paste follows. Re-arming while armed just
30+
* extends the deadline; two guards would eat two pastes.
31+
*/
32+
export function armMiddleClickPasteGuard(windowMs: number = GUARD_WINDOW_MS): void {
33+
disarm?.()
34+
35+
const onPaste = (event: Event): void => {
36+
event.preventDefault()
37+
event.stopPropagation()
38+
disarm?.()
39+
}
40+
const timer = window.setTimeout(() => disarm?.(), windowMs)
41+
42+
disarm = () => {
43+
window.clearTimeout(timer)
44+
window.removeEventListener('paste', onPaste, true)
45+
disarm = null
46+
}
47+
window.addEventListener('paste', onPaste, true)
48+
}

0 commit comments

Comments
 (0)