From 8811d96a8b8d79db4b31bcf2b2d5c428f47d3d50 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 19 Jun 2017 15:45:18 -0700 Subject: [PATCH] Fix selection going to clipboard Part of #699 --- src/SelectionManager.ts | 17 +++++++++++++++-- src/xterm.js | 9 ++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index a769afd1..63720b86 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -293,10 +293,23 @@ export class SelectionManager extends EventEmitter { /** * Queues a refresh, redrawing the selection on the next opportunity. */ - public refresh(): void { + public refresh(fromMouseEvent?: boolean): void { + // Queue the refresh for the renderer if (!this._refreshAnimationFrame) { this._refreshAnimationFrame = window.requestAnimationFrame(() => this._refresh()); } + + // If the refresh comes from a mouse event then emit a newselection event + if (!fromMouseEvent) { + return; + } + // TODO: Only do this when the selection has actually changed + // TODO: Ensure we're not doing more work than absolutely necessary, particularly for large selections + // TODO: Only do this on Linux + const selectionText = this.selectionText; + if (selectionText.length) { + this.emit('newselection', this.selectionText); + } } /** @@ -392,7 +405,7 @@ export class SelectionManager extends EventEmitter { } this._addMouseDownListeners(); - this.refresh(); + this.refresh(true); } /** diff --git a/src/xterm.js b/src/xterm.js index 0abcbeae..fcd42376 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -703,7 +703,14 @@ Terminal.prototype.open = function(parent, focus) { this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure); this.renderer = new Renderer(this); this.selectionManager = new SelectionManager(this, this.lines, this.rowContainer, this.charMeasure); - this.selectionManager.on('refresh', data => this.renderer.refreshSelection(data.start, data.end)); + this.selectionManager.on('refresh', data => { + this.renderer.refreshSelection(data.start, data.end); + }); + this.selectionManager.on('newselection', text => { + this.textarea.value = text; + this.textarea.focus(); + this.textarea.select(); + }); this.on('scroll', () => this.selectionManager.refresh()); this.viewportElement.addEventListener('scroll', () => this.selectionManager.refresh());