From 3813d27d5f560c6319d2da5abfe0f40c38fc8d95 Mon Sep 17 00:00:00 2001 From: ppot Date: Thu, 18 Aug 2016 23:09:40 -0400 Subject: [PATCH] Fix ScrollPort SelectAll --- lib/components/term.js | 4 ++++ lib/containers/hyperterm.js | 1 + lib/hterm.js | 9 ++++++++- lib/utils/selection.js | 38 +++++++++++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/components/term.js b/lib/components/term.js index 73474455..9a69151d 100644 --- a/lib/components/term.js +++ b/lib/components/term.js @@ -145,6 +145,10 @@ export default class Term extends Component { this.term.onVTKeystroke('\x05'); } + selectAll () { + this.term.selectAll(); + } + getTermDocument () { return this.term.document_; } diff --git a/lib/containers/hyperterm.js b/lib/containers/hyperterm.js index bd5f298a..98a6b720 100644 --- a/lib/containers/hyperterm.js +++ b/lib/containers/hyperterm.js @@ -62,6 +62,7 @@ class HyperTerm extends Component { keys.bind('command+backspace', bound('deleteLine')); keys.bind('command+left', bound('moveToStart')); keys.bind('command+right', bound('moveToEnd')); + keys.bind('command+a', bound('selectAll')); this.keys = keys; } diff --git a/lib/hterm.js b/lib/hterm.js index 531655ea..ee61af3a 100644 --- a/lib/hterm.js +++ b/lib/hterm.js @@ -3,11 +3,18 @@ const selection = require('./utils/selection'); hterm.defaultStorage = new lib.Storage.Memory(); +// Provide selectAll to terminal viewport +hterm.Terminal.prototype.selectAll = function () { + // We need to clear dom range to reset anchorNode + selection.clear(this); + selection.all(this); +}; + // override double click behavior to copy const oldMouse = hterm.Terminal.prototype.onMouse_; hterm.Terminal.prototype.onMouse_ = function (e) { if ('dblclick' === e.type) { - selection.expand(this); + selection.extend(this); console.log('[hyperterm+hterm] ignore double click'); return; } diff --git a/lib/utils/selection.js b/lib/utils/selection.js index 001b33f8..b6bfaad3 100644 --- a/lib/utils/selection.js +++ b/lib/utils/selection.js @@ -4,7 +4,41 @@ exports.clear = function (terminal) { terminal.document_.getSelection().removeAllRanges(); }; -// Use selection expand upon dblclick -exports.expand = function (terminal) { +// Use selection extend upon dblclick +exports.extend = function (terminal) { terminal.screen_.expandSelection(terminal.document_.getSelection()); }; + +// Fix a bug in ScrollPort selectAll behavior +// Select all rows in the viewport +exports.all = function (terminal) { + let scrollPort = terminal.scrollPort_; + let firstRow, lastRowIndex, lastRow; + + if (scrollPort.topFold_.nextSibling.rowIndex !== 0) { + while (scrollPort.topFold_.previousSibling) { + scrollPort.rowNodes_.removeChild(scrollPort.topFold_.previousSibling); + } + + firstRow = scrollPort.fetchRowNode_(0); + scrollPort.rowNodes_.insertBefore(firstRow, scrollPort.topFold_); + scrollPort.syncRowNodesDimensions_(); + } else { + firstRow = scrollPort.topFold_.nextSibling; + } + + lastRowIndex = scrollPort.rowProvider_.getRowCount() - 1; + + if (scrollPort.bottomFold_.previousSibling.rowIndex !== lastRowIndex) { + while (scrollPort.bottomFold_.nextSibling) { + scrollPort.rowNodes_.removeChild(scrollPort.bottomFold_.nextSibling); + } + + lastRow = scrollPort.fetchRowNode_(lastRowIndex); + scrollPort.rowNodes_.appendChild(lastRow); + } else { + lastRow = scrollPort.bottomFold_.previousSibling.rowIndex; + } + + scrollPort.selection.sync(); +};