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.
This commit is contained in:
Simon Howe
2016-10-18 15:47:28 +02:00
parent f7c0b75eff
commit 1b78427fb9

View File

@@ -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);
}