From 1e10a0a746c58dc72d56cdcf8d5782a361289727 Mon Sep 17 00:00:00 2001 From: Sadick Date: Fri, 15 Jan 2021 09:50:45 +0300 Subject: [PATCH] Add setting to disable middle mouse paste (#21864) * Add editor setting * Only paste on middle click if selectionClipboard is true * Add specs --- spec/text-editor-component-spec.js | 14 ++++++++++++++ src/config-schema.js | 8 ++++++++ src/text-editor-component.js | 8 ++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spec/text-editor-component-spec.js b/spec/text-editor-component-spec.js index d6769dd5998..5cd9288d57c 100644 --- a/spec/text-editor-component-spec.js +++ b/spec/text-editor-component-spec.js @@ -4051,6 +4051,7 @@ describe('TextEditorComponent', () => { describe('on the lines', () => { describe('when there is only one cursor', () => { it('positions the cursor on single-click or when middle-clicking', async () => { + atom.config.set('editor.selectionClipboard', true); for (const button of [0, 1]) { const { component, editor } = buildComponent(); const { lineHeight } = component.measurements; @@ -4798,6 +4799,7 @@ describe('TextEditorComponent', () => { const { component, editor } = buildComponent({ platform: 'linux' }); // Middle mouse pasting. + atom.config.set('editor.selectionClipboard', true); editor.setSelectedBufferRange([[1, 6], [1, 10]]); await conditionPromise(() => TextEditor.clipboard.read() === 'sort'); component.didMouseDownOnContent({ @@ -4809,7 +4811,19 @@ describe('TextEditorComponent', () => { expect(editor.lineTextForBufferRow(10)).toBe('sort'); editor.undo(); + // Doesn't paste when middle mouse button is clicked + atom.config.set('editor.selectionClipboard', false); + editor.setSelectedBufferRange([[1, 6], [1, 10]]); + component.didMouseDownOnContent({ + button: 1, + clientX: clientLeftForCharacter(component, 10, 0), + clientY: clientTopForLine(component, 10) + }); + expect(TextEditor.clipboard.read()).toBe('sort'); + expect(editor.lineTextForBufferRow(10)).toBe(''); + // Ensure left clicks don't interfere. + atom.config.set('editor.selectionClipboard', true); editor.setSelectedBufferRange([[1, 2], [1, 5]]); await conditionPromise(() => TextEditor.clipboard.read() === 'var'); component.didMouseDownOnContent({ diff --git a/src/config-schema.js b/src/config-schema.js index 07ddf4c138f..f1a8ff3b73b 100644 --- a/src/config-schema.js +++ b/src/config-schema.js @@ -660,4 +660,12 @@ if (process.platform === 'darwin') { }; } +if (process.platform === 'linux') { + configSchema.editor.properties.selectionClipboard = { + type: 'boolean', + default: true, + description: 'Enable pasting on middle mouse button click' + }; +} + module.exports = configSchema; diff --git a/src/text-editor-component.js b/src/text-editor-component.js index 1281fa5a5c0..0ff3036f40d 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -1969,8 +1969,12 @@ module.exports = class TextEditorComponent { // On Linux, pasting happens on middle click. A textInput event with the // contents of the selection clipboard will be dispatched by the browser - // automatically on mouseup. - if (platform === 'linux' && this.isInputEnabled()) + // automatically on mouseup if editor.selectionClipboard is set to true. + if ( + platform === 'linux' && + this.isInputEnabled() && + atom.config.get('editor.selectionClipboard') + ) model.insertText(clipboard.readText('selection')); return; }