From f6d2f966bfb57492632c65c23e318dd792bbdced Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 10 Jul 2017 15:53:23 -0600 Subject: [PATCH] Fix middle-mouse-button paste on Linux Chrome now synthesizes a textInput event on mouseup for middle mouse button clicks, which rendered our custom JS for handling that case redundant. --- spec/text-editor-component-spec.js | 37 +++++++----------------------- src/text-editor-component.js | 25 ++++++++++---------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/spec/text-editor-component-spec.js b/spec/text-editor-component-spec.js index 555ee3015..2028ae8bd 100644 --- a/spec/text-editor-component-spec.js +++ b/spec/text-editor-component-spec.js @@ -2412,10 +2412,13 @@ describe('TextEditorComponent', () => { ctrlKey: true }) ) - expect(editor.getCursorScreenPositions()).toEqual([[1, 4]]) + expect(editor.getSelectedScreenRanges()).toEqual([ + [[1, 16], [1, 16]] + ]) // ctrl-click adds cursors on platforms *other* than macOS component.props.platform = 'win32' + editor.setCursorScreenPosition([1, 4]) component.didMouseDownOnContent( Object.assign(clientPositionForCharacter(component, 1, 16), { detail: 1, @@ -2675,42 +2678,18 @@ describe('TextEditorComponent', () => { expect(component.getScrollLeft()).toBe(maxScrollLeft) }) - it('pastes the previously selected text when clicking the middle mouse button on Linux', async () => { - spyOn(electron.ipcRenderer, 'send').andCallFake(function (eventName, selectedText) { - if (eventName === 'write-text-to-selection-clipboard') { - clipboard.writeText(selectedText, 'selection') - } - }) - + it('positions the cursor on clicking the middle mouse button on Linux', async () => { + // The browser synthesizes the paste as a textInput event on mouseup + // so it is not possible to test it here. const {component, editor} = buildComponent({platform: 'linux'}) - // Middle mouse pasting. editor.setSelectedBufferRange([[1, 6], [1, 10]]) - await conditionPromise(() => TextEditor.clipboard.read() === 'sort') component.didMouseDownOnContent({ button: 1, clientX: clientLeftForCharacter(component, 10, 0), clientY: clientTopForLine(component, 10) }) - expect(TextEditor.clipboard.read()).toBe('sort') - expect(editor.lineTextForBufferRow(10)).toBe('sort') - editor.undo() - - // Ensure left clicks don't interfere. - editor.setSelectedBufferRange([[1, 2], [1, 5]]) - await conditionPromise(() => TextEditor.clipboard.read() === 'var') - component.didMouseDownOnContent({ - button: 0, - detail: 1, - clientX: clientLeftForCharacter(component, 10, 0), - clientY: clientTopForLine(component, 10) - }) - component.didMouseDownOnContent({ - button: 1, - clientX: clientLeftForCharacter(component, 10, 0), - clientY: clientTopForLine(component, 10) - }) - expect(editor.lineTextForBufferRow(10)).toBe('var') + expect(editor.getSelectedBufferRange()).toEqual([[10, 0], [10, 0]]) }) }) diff --git a/src/text-editor-component.js b/src/text-editor-component.js index dfcdbc150..cf9e5c53f 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -1647,9 +1647,21 @@ class TextEditorComponent { const {target, button, detail, ctrlKey, shiftKey, metaKey} = event const platform = this.getPlatform() + // On Linux, position the cursor on middle mouse button click. A + // textInput event with the contents of the selection clipboard will be + // dispatched by the browser automatically on mouseup. + if (platform === 'linux' && button === 1) { + const screenPosition = this.screenPositionForMouseEvent(event) + model.setCursorScreenPosition(screenPosition, {autoscroll: false}) + return + } + // Only handle mousedown events for left mouse button (or the middle mouse // button on Linux where it pastes the selection clipboard). - if (!(button === 0 || (platform === 'linux' && button === 1))) return + if (button !== 0) return + + // Ctrl-click brings up the context menu on macOS + if (platform === 'darwin' && ctrlKey) return const screenPosition = this.screenPositionForMouseEvent(event) @@ -1659,17 +1671,6 @@ class TextEditorComponent { return } - // Handle middle mouse button only on Linux (paste clipboard) - if (platform === 'linux' && button === 1) { - const selection = clipboard.readText('selection') - model.setCursorScreenPosition(screenPosition, {autoscroll: false}) - model.insertText(selection) - return - } - - // Ctrl-click brings up the context menu on macOS - if (platform === 'darwin' && ctrlKey) return - const addOrRemoveSelection = metaKey || (ctrlKey && platform !== 'darwin') switch (detail) {