Skip to content

Commit

Permalink
Add setting to disable middle mouse paste (atom#21864)
Browse files Browse the repository at this point in the history
* Add editor setting

* Only paste on middle click if selectionClipboard is true

* Add specs
  • Loading branch information
sadick254 authored Jan 15, 2021
1 parent e231837 commit 1e10a0a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 14 additions & 0 deletions spec/text-editor-component-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down
8 changes: 8 additions & 0 deletions src/config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 6 additions & 2 deletions src/text-editor-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 1e10a0a

Please sign in to comment.