From 1b78427fb97f22c55b91c69914a9179994c8d70c Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Tue, 18 Oct 2016 15:47:28 +0200 Subject: [PATCH] Keep scroll position if we scroll away from the bottom of the term aka, scroll locking. Allows you to scroll back through logs without new log data dragging the scroll back down to the next entries as they come in. --- client/app/scripts/components/terminal.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/client/app/scripts/components/terminal.js b/client/app/scripts/components/terminal.js index d1989f28d..92d9172ef 100644 --- a/client/app/scripts/components/terminal.js +++ b/client/app/scripts/components/terminal.js @@ -142,7 +142,12 @@ class Terminal extends React.Component { socket.onmessage = (event) => { log('pipe data', event.data.size); const input = ab2str(event.data); + const scrolledToBottom = term.ydisp === term.ybase; + const savedScrollPosition = term.ydisp; term.write(input); + if (!scrolledToBottom) { + this.scrollTo(savedScrollPosition); + } }; this.socket = socket; @@ -154,6 +159,18 @@ class Terminal extends React.Component { } } + scrollToBottom() { + this.scrollTo(this.term.ybase); + } + + scrollTo(y) { + if (!this.term) { + return; + } + this.term.ydisp = y; + this.term.refresh(0, this.term.rows - 1); + } + componentDidMount() { this._isMounted = true; if (this.props.connect) { @@ -173,6 +190,7 @@ class Terminal extends React.Component { const innerNode = ReactDOM.findDOMNode(component.innerFlex); this.term.open(innerNode); this.term.on('data', (data) => { + this.scrollToBottom(); if (this.socket) { this.socket.send(data); }