Handle middle click to paste selection

This appears to work in Chrome but not Firefox
This commit is contained in:
Daniel Imms
2017-06-20 10:18:29 -07:00
parent 8811d96a8b
commit dc165175cf
4 changed files with 29 additions and 16 deletions
+10 -12
View File
@@ -2,10 +2,11 @@
* @license MIT * @license MIT
*/ */
import * as Mouse from './utils/Mouse';
import * as Browser from './utils/Browser';
import { CharMeasure } from './utils/CharMeasure'; import { CharMeasure } from './utils/CharMeasure';
import { CircularList } from './utils/CircularList'; import { CircularList } from './utils/CircularList';
import { EventEmitter } from './EventEmitter'; import { EventEmitter } from './EventEmitter';
import * as Mouse from './utils/Mouse';
import { ITerminal } from './Interfaces'; import { ITerminal } from './Interfaces';
import { SelectionModel } from './SelectionModel'; import { SelectionModel } from './SelectionModel';
@@ -299,16 +300,13 @@ export class SelectionManager extends EventEmitter {
this._refreshAnimationFrame = window.requestAnimationFrame(() => this._refresh()); this._refreshAnimationFrame = window.requestAnimationFrame(() => this._refresh());
} }
// If the refresh comes from a mouse event then emit a newselection event // If the platform is Linux and the refresh call comes from a mouse event,
if (!fromMouseEvent) { // we need to update the selection for middle click to paste selection.
return; if (Browser.isLinux && fromMouseEvent) {
} const selectionText = this.selectionText;
// TODO: Only do this when the selection has actually changed if (selectionText.length) {
// TODO: Ensure we're not doing more work than absolutely necessary, particularly for large selections this.emit('newselection', this.selectionText);
// TODO: Only do this on Linux }
const selectionText = this.selectionText;
if (selectionText.length) {
this.emit('newselection', this.selectionText);
} }
} }
@@ -560,7 +558,7 @@ export class SelectionManager extends EventEmitter {
if (!previousSelectionEnd || if (!previousSelectionEnd ||
previousSelectionEnd[0] !== this._model.selectionEnd[0] || previousSelectionEnd[0] !== this._model.selectionEnd[0] ||
previousSelectionEnd[1] !== this._model.selectionEnd[1]) { previousSelectionEnd[1] !== this._model.selectionEnd[1]) {
this.refresh(); this.refresh(true);
} }
} }
+1 -1
View File
@@ -80,7 +80,7 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal) {
* @param term The terminal on which to apply the handled paste event * @param term The terminal on which to apply the handled paste event
* @param selectionManager The terminal's selection manager. * @param selectionManager The terminal's selection manager.
*/ */
export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager) { export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager) {
// Bring textarea at the cursor position // Bring textarea at the cursor position
textarea.style.position = 'fixed'; textarea.style.position = 'fixed';
textarea.style.width = '20px'; textarea.style.width = '20px';
+1
View File
@@ -20,3 +20,4 @@ export const isMac = contains(['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'], pla
export const isIpad = platform === 'iPad'; export const isIpad = platform === 'iPad';
export const isIphone = platform === 'iPhone'; export const isIphone = platform === 'iPhone';
export const isMSWindows = contains(['Windows', 'Win16', 'Win32', 'WinCE'], platform); export const isMSWindows = contains(['Windows', 'Win16', 'Win32', 'WinCE'], platform);
export const isLinux = platform.indexOf('Linux') >= 0;
+17 -3
View File
@@ -13,7 +13,7 @@
import { CompositionHelper } from './CompositionHelper'; import { CompositionHelper } from './CompositionHelper';
import { EventEmitter } from './EventEmitter'; import { EventEmitter } from './EventEmitter';
import { Viewport } from './Viewport'; import { Viewport } from './Viewport';
import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard'; import { moveTextAreaUnderMouseCursor, pasteHandler, copyHandler } from './handlers/Clipboard';
import { CircularList } from './utils/CircularList'; import { CircularList } from './utils/CircularList';
import { C0 } from './EscapeSequences'; import { C0 } from './EscapeSequences';
import { InputHandler } from './InputHandler'; import { InputHandler } from './InputHandler';
@@ -537,15 +537,29 @@ Terminal.prototype.initGlobal = function() {
on(this.textarea, 'paste', pasteHandlerWrapper); on(this.textarea, 'paste', pasteHandlerWrapper);
on(this.element, 'paste', pasteHandlerWrapper); on(this.element, 'paste', pasteHandlerWrapper);
// Handle right click context menus
if (term.browser.isFirefox) { if (term.browser.isFirefox) {
on(this.element, 'mousedown', event => { on(this.element, 'mousedown', event => {
if (ev.button == 2) { if (ev.button == 2) {
rightClickHandler(event, this.textarea, this.selectionManager); moveTextAreaUnderMouseCursor(event, this.textarea, this.selectionManager);
} }
}); });
} else { } else {
on(this.element, 'contextmenu', event => { on(this.element, 'contextmenu', event => {
rightClickHandler(event, this.textarea, this.selectionManager); moveTextAreaUnderMouseCursor(event, this.textarea, this.selectionManager);
});
}
// Move the textarea under the cursor when middle clicking on Linux to ensure
// middle click to paste selection works. This only appears to work in Chrome
// at the time is writing.
if (term.browser.isLinux) {
// Use auxclick event over mousedown the latter doesn't seem to work. Note
// that the regular click event doesn't fire for the middle mouse button.
on(this.element, 'click', event => {
if (event.button === 1) {
moveTextAreaUnderMouseCursor(event, this.textarea, this.selectionManager);
}
}); });
} }
}; };