From 82619a989b8235f3044ea416bc17137704228b93 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 10 Jul 2017 14:27:55 -0600 Subject: [PATCH 1/4] :art: --- src/text-editor-component.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/text-editor-component.js b/src/text-editor-component.js index b44fac331..fef64d654 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -1645,10 +1645,11 @@ class TextEditorComponent { didMouseDownOnContent (event) { const {model} = this.props const {target, button, detail, ctrlKey, shiftKey, metaKey} = event + const platform = this.getPlatform() // Only handle mousedown events for left mouse button (or the middle mouse // button on Linux where it pastes the selection clipboard). - if (!(button === 0 || (this.getPlatform() === 'linux' && button === 1))) return + if (!(button === 0 || (platform === 'linux' && button === 1))) return const screenPosition = this.screenPositionForMouseEvent(event) @@ -1659,14 +1660,14 @@ class TextEditorComponent { } // Handle middle mouse button only on Linux (paste clipboard) - if (this.getPlatform() === 'linux' && button === 1) { + if (platform === 'linux' && button === 1) { const selection = clipboard.readText('selection') model.setCursorScreenPosition(screenPosition, {autoscroll: false}) model.insertText(selection) return } - const addOrRemoveSelection = metaKey || (ctrlKey && this.getPlatform() !== 'darwin') + const addOrRemoveSelection = metaKey || (ctrlKey && platform !== 'darwin') switch (detail) { case 1: From cd27b49dc471db63c82850d119e7c98293f251a2 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 10 Jul 2017 14:28:21 -0600 Subject: [PATCH 2/4] Don't handle ctrl-click events on macOS It brings up the context menu, so we shouldn't change the cursor position --- src/text-editor-component.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/text-editor-component.js b/src/text-editor-component.js index fef64d654..dfcdbc150 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -1667,6 +1667,9 @@ class TextEditorComponent { return } + // Ctrl-click brings up the context menu on macOS + if (platform === 'darwin' && ctrlKey) return + const addOrRemoveSelection = metaKey || (ctrlKey && platform !== 'darwin') switch (detail) { From f6d2f966bfb57492632c65c23e318dd792bbdced Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 10 Jul 2017 15:53:23 -0600 Subject: [PATCH 3/4] 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) { From 07c7e53b1eb620064e9d81004438f3028d22151a Mon Sep 17 00:00:00 2001 From: ungb Date: Mon, 10 Jul 2017 16:26:47 -0700 Subject: [PATCH 4/4] fix lint errors --- src/text-editor-component.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/text-editor-component.js b/src/text-editor-component.js index cf9e5c53f..037e45462 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -5,7 +5,6 @@ const {Point, Range} = require('text-buffer') const LineTopIndex = require('line-top-index') const TextEditor = require('./text-editor') const {isPairedCharacter} = require('./text-utils') -const clipboard = require('./safe-clipboard') const electron = require('electron') const $ = etch.dom