2015-12-22 18:18:50 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-12-22 18:18:50 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const React = require("react");
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-07-12 15:58:14 +03:00
|
|
|
import Promise from 'bluebird';
|
2017-12-01 13:41:56 +03:00
|
|
|
import { KeyCode } from '../../Keyboard';
|
2019-03-19 15:42:22 +03:00
|
|
|
import Timer from '../../utils/Timer';
|
2019-03-12 18:30:06 +03:00
|
|
|
import AutoHideScrollbar from "./AutoHideScrollbar";
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2019-03-26 14:19:30 +03:00
|
|
|
const DEBUG_SCROLL = true;
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-11-16 17:25:52 +03:00
|
|
|
// The amount of extra scroll distance to allow prior to unfilling.
|
|
|
|
// See _getExcessHeight.
|
2017-03-13 16:48:15 +03:00
|
|
|
const UNPAGINATION_PADDING = 6000;
|
2016-11-22 20:23:06 +03:00
|
|
|
// The number of milliseconds to debounce calls to onUnfillRequest, to prevent
|
|
|
|
// many scroll events causing many unfilling requests.
|
|
|
|
const UNFILL_REQUEST_DEBOUNCE_MS = 200;
|
2016-11-16 17:25:52 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
const PAGE_SIZE = 200;
|
|
|
|
|
|
|
|
let debuglog;
|
2016-01-04 19:28:32 +03:00
|
|
|
if (DEBUG_SCROLL) {
|
|
|
|
// using bind means that we get to keep useful line numbers in the console
|
2019-03-19 15:42:22 +03:00
|
|
|
debuglog = console.log.bind(console, "ScrollPanel debuglog:");
|
2016-01-04 19:28:32 +03:00
|
|
|
} else {
|
2019-03-19 15:42:22 +03:00
|
|
|
debuglog = function() {};
|
2016-01-04 19:28:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 18:18:50 +03:00
|
|
|
/* This component implements an intelligent scrolling list.
|
|
|
|
*
|
|
|
|
* It wraps a list of <li> children; when items are added to the start or end
|
|
|
|
* of the list, the scroll position is updated so that the user still sees the
|
|
|
|
* same position in the list.
|
|
|
|
*
|
|
|
|
* It also provides a hook which allows parents to provide more list elements
|
|
|
|
* when we get close to the start or end of the list.
|
|
|
|
*
|
2017-05-04 12:00:13 +03:00
|
|
|
* Each child element should have a 'data-scroll-tokens'. This string of
|
|
|
|
* comma-separated tokens may contain a single token or many, where many indicates
|
|
|
|
* that the element contains elements that have scroll tokens themselves. The first
|
|
|
|
* token in 'data-scroll-tokens' is used to serialise the scroll state, and returned
|
|
|
|
* as the 'trackedScrollToken' attribute by getScrollState().
|
2016-02-01 19:31:12 +03:00
|
|
|
*
|
2017-05-04 12:00:13 +03:00
|
|
|
* IMPORTANT: INDIVIDUAL TOKENS WITHIN 'data-scroll-tokens' MUST NOT CONTAIN COMMAS.
|
2017-05-02 19:36:59 +03:00
|
|
|
*
|
2016-02-01 19:31:12 +03:00
|
|
|
* Some notes about the implementation:
|
|
|
|
*
|
|
|
|
* The saved 'scrollState' can exist in one of two states:
|
|
|
|
*
|
2016-02-03 11:03:10 +03:00
|
|
|
* - stuckAtBottom: (the default, and restored by resetScrollState): the
|
|
|
|
* viewport is scrolled down as far as it can be. When the children are
|
|
|
|
* updated, the scroll position will be updated to ensure it is still at
|
|
|
|
* the bottom.
|
2016-02-01 19:31:12 +03:00
|
|
|
*
|
|
|
|
* - fixed, in which the viewport is conceptually tied at a specific scroll
|
|
|
|
* offset. We don't save the absolute scroll offset, because that would be
|
|
|
|
* affected by window width, zoom level, amount of scrollback, etc. Instead
|
|
|
|
* we save an identifier for the last fully-visible message, and the number
|
2016-05-17 22:28:11 +03:00
|
|
|
* of pixels the window was scrolled below it - which is hopefully near
|
2016-02-01 19:31:12 +03:00
|
|
|
* enough.
|
|
|
|
*
|
|
|
|
* The 'stickyBottom' property controls the behaviour when we reach the bottom
|
|
|
|
* of the window (either through a user-initiated scroll, or by calling
|
|
|
|
* scrollToBottom). If stickyBottom is enabled, the scrollState will enter
|
2016-02-03 11:03:10 +03:00
|
|
|
* 'stuckAtBottom' state - ensuring that new additions cause the window to
|
|
|
|
* scroll down further. If stickyBottom is disabled, we just save the scroll
|
|
|
|
* offset as normal.
|
2015-12-22 18:18:50 +03:00
|
|
|
*/
|
2019-02-22 20:30:22 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ScrollPanel',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
/* stickyBottom: if set to true, then once the user hits the bottom of
|
|
|
|
* the list, any new children added to the list will cause the list to
|
|
|
|
* scroll down to show the new element, rather than preserving the
|
|
|
|
* existing view.
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
stickyBottom: PropTypes.bool,
|
2017-09-06 19:40:58 +03:00
|
|
|
|
|
|
|
/* startAtBottom: if set to true, the view is assumed to start
|
|
|
|
* scrolled to the bottom.
|
|
|
|
* XXX: It's likley this is unecessary and can be derived from
|
|
|
|
* stickyBottom, but I'm adding an extra parameter to ensure
|
|
|
|
* behaviour stays the same for other uses of ScrollPanel.
|
|
|
|
* If so, let's remove this parameter down the line.
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
startAtBottom: PropTypes.bool,
|
2017-09-06 19:40:58 +03:00
|
|
|
|
|
|
|
/* onFillRequest(backwards): a callback which is called on scroll when
|
|
|
|
* the user nears the start (backwards = true) or end (backwards =
|
|
|
|
* false) of the list.
|
|
|
|
*
|
|
|
|
* This should return a promise; no more calls will be made until the
|
|
|
|
* promise completes.
|
|
|
|
*
|
|
|
|
* The promise should resolve to true if there is more data to be
|
|
|
|
* retrieved in this direction (in which case onFillRequest may be
|
|
|
|
* called again immediately), or false if there is no more data in this
|
|
|
|
* directon (at this time) - which will stop the pagination cycle until
|
|
|
|
* the user scrolls again.
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
onFillRequest: PropTypes.func,
|
2017-09-06 19:40:58 +03:00
|
|
|
|
|
|
|
/* onUnfillRequest(backwards): a callback which is called on scroll when
|
|
|
|
* there are children elements that are far out of view and could be removed
|
|
|
|
* without causing pagination to occur.
|
|
|
|
*
|
|
|
|
* This function should accept a boolean, which is true to indicate the back/top
|
|
|
|
* of the panel and false otherwise, and a scroll token, which refers to the
|
|
|
|
* first element to remove if removing from the front/bottom, and last element
|
|
|
|
* to remove if removing from the back/top.
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
onUnfillRequest: PropTypes.func,
|
2017-09-06 19:40:58 +03:00
|
|
|
|
|
|
|
/* onScroll: a callback which is called whenever any scroll happens.
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
onScroll: PropTypes.func,
|
2017-09-06 19:40:58 +03:00
|
|
|
|
|
|
|
/* className: classnames to add to the top-level div
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
className: PropTypes.string,
|
2017-09-06 19:40:58 +03:00
|
|
|
|
|
|
|
/* style: styles to add to the top-level div
|
|
|
|
*/
|
2017-12-26 04:03:18 +03:00
|
|
|
style: PropTypes.object,
|
2019-03-18 16:05:56 +03:00
|
|
|
/* resizeNotifier: ResizeNotifier to know when middle column has changed size
|
|
|
|
*/
|
|
|
|
resizeNotifier: PropTypes.object,
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
stickyBottom: true,
|
|
|
|
startAtBottom: true,
|
|
|
|
onFillRequest: function(backwards) { return Promise.resolve(false); },
|
|
|
|
onUnfillRequest: function(backwards, scrollToken) {},
|
|
|
|
onScroll: function() {},
|
|
|
|
};
|
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
componentWillMount: function() {
|
2019-03-26 15:34:55 +03:00
|
|
|
this._fillRequestWhileRunning = false;
|
|
|
|
this._isFilling = false;
|
2015-12-24 03:10:21 +03:00
|
|
|
this._pendingFillRequests = {b: null, f: null};
|
2019-03-18 16:05:56 +03:00
|
|
|
|
|
|
|
if (this.props.resizeNotifier) {
|
|
|
|
this.props.resizeNotifier.on("middlePanelResized", this.onResize);
|
|
|
|
}
|
2019-03-12 18:36:12 +03:00
|
|
|
|
2015-12-22 18:18:50 +03:00
|
|
|
this.resetScrollState();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
componentDidMount: function() {
|
2017-09-08 17:41:19 +03:00
|
|
|
this.checkScroll();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-24 03:10:21 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
componentDidUpdate: function() {
|
2015-12-22 18:18:50 +03:00
|
|
|
// after adding event tiles, we may need to tweak the scroll (either to
|
|
|
|
// keep at the bottom of the timeline, or to maintain the view after
|
|
|
|
// adding events to the top).
|
2016-02-22 20:19:04 +03:00
|
|
|
//
|
|
|
|
// This will also re-check the fill state, in case the paginate was inadequate
|
|
|
|
this.checkScroll();
|
2019-03-20 19:02:53 +03:00
|
|
|
this.updatePreventShrinking();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
componentWillUnmount: function() {
|
2016-01-11 14:38:04 +03:00
|
|
|
// set a boolean to say we've been unmounted, which any pending
|
|
|
|
// promises can use to throw away their results.
|
|
|
|
//
|
|
|
|
// (We could use isMounted(), but facebook have deprecated that.)
|
|
|
|
this.unmounted = true;
|
2019-03-18 16:05:56 +03:00
|
|
|
|
|
|
|
if (this.props.resizeNotifier) {
|
|
|
|
this.props.resizeNotifier.removeListener("middlePanelResized", this.onResize);
|
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-01-11 14:38:04 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
onScroll: function(ev) {
|
2019-03-22 17:47:04 +03:00
|
|
|
debuglog("onScroll", this._getScrollNode().scrollTop);
|
2019-03-19 15:42:22 +03:00
|
|
|
this._scrollTimeout.restart();
|
|
|
|
this._saveScrollState();
|
2019-03-20 19:02:53 +03:00
|
|
|
this.updatePreventShrinking();
|
2015-12-22 18:18:50 +03:00
|
|
|
this.props.onScroll(ev);
|
2019-03-26 14:19:30 +03:00
|
|
|
this.checkFillState();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
onResize: function() {
|
2016-03-24 03:13:32 +03:00
|
|
|
this.checkScroll();
|
2019-03-20 19:02:53 +03:00
|
|
|
// update preventShrinkingState if present
|
|
|
|
if (this.preventShrinkingState) {
|
|
|
|
this.preventShrinking();
|
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-03-24 03:13:32 +03:00
|
|
|
|
2016-02-22 20:19:04 +03:00
|
|
|
// after an update to the contents of the panel, check that the scroll is
|
|
|
|
// where it ought to be, and set off pagination requests if necessary.
|
2017-09-06 19:40:58 +03:00
|
|
|
checkScroll: function() {
|
2016-02-22 20:19:04 +03:00
|
|
|
this._restoreSavedScrollState();
|
|
|
|
this.checkFillState();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-02-22 20:19:04 +03:00
|
|
|
|
2016-01-05 13:58:01 +03:00
|
|
|
// return true if the content is fully scrolled down right now; else false.
|
2016-02-03 11:03:10 +03:00
|
|
|
//
|
|
|
|
// note that this is independent of the 'stuckAtBottom' state - it is simply
|
2019-03-20 19:10:38 +03:00
|
|
|
// about whether the content is scrolled down right now, irrespective of
|
2016-02-03 11:03:10 +03:00
|
|
|
// whether it will stay that way when the children update.
|
2017-09-06 19:40:58 +03:00
|
|
|
isAtBottom: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const sn = this._getScrollNode();
|
2019-03-19 15:42:22 +03:00
|
|
|
return sn.scrollTop === sn.scrollHeight - sn.clientHeight;
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-11-16 17:25:52 +03:00
|
|
|
// returns the vertical height in the given direction that can be removed from
|
|
|
|
// the content box (which has a height of scrollHeight, see checkFillState) without
|
|
|
|
// pagination occuring.
|
|
|
|
//
|
|
|
|
// padding* = UNPAGINATION_PADDING
|
|
|
|
//
|
|
|
|
// ### Region determined as excess.
|
|
|
|
//
|
|
|
|
// .---------. - -
|
|
|
|
// |#########| | |
|
|
|
|
// |#########| - | scrollTop |
|
|
|
|
// | | | padding* | |
|
|
|
|
// | | | | |
|
|
|
|
// .-+---------+-. - - | |
|
|
|
|
// : | | : | | |
|
|
|
|
// : | | : | clientHeight | |
|
|
|
|
// : | | : | | |
|
|
|
|
// .-+---------+-. - - |
|
|
|
|
// | | | | | |
|
|
|
|
// | | | | | clientHeight | scrollHeight
|
|
|
|
// | | | | | |
|
|
|
|
// `-+---------+-' - |
|
|
|
|
// : | | : | |
|
|
|
|
// : | | : | clientHeight |
|
|
|
|
// : | | : | |
|
|
|
|
// `-+---------+-' - - |
|
|
|
|
// | | | padding* |
|
|
|
|
// | | | |
|
|
|
|
// |#########| - |
|
|
|
|
// |#########| |
|
|
|
|
// `---------' -
|
2017-09-06 19:40:58 +03:00
|
|
|
_getExcessHeight: function(backwards) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const sn = this._getScrollNode();
|
2019-03-19 15:42:22 +03:00
|
|
|
const contentHeight = this._getMessagesHeight();
|
|
|
|
const listHeight = this._getListHeight();
|
|
|
|
const clippedHeight = contentHeight - listHeight;
|
|
|
|
const unclippedScrollTop = sn.scrollTop + clippedHeight;
|
|
|
|
|
2016-11-16 17:25:52 +03:00
|
|
|
if (backwards) {
|
2019-03-19 15:42:22 +03:00
|
|
|
return unclippedScrollTop - sn.clientHeight - UNPAGINATION_PADDING;
|
2016-11-16 17:25:52 +03:00
|
|
|
} else {
|
2019-03-19 15:42:22 +03:00
|
|
|
return contentHeight - (unclippedScrollTop + 2*sn.clientHeight) - UNPAGINATION_PADDING;
|
2016-11-16 17:25:52 +03:00
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-11-16 17:25:52 +03:00
|
|
|
|
2015-12-22 18:18:50 +03:00
|
|
|
// check the scroll state and send out backfill requests if necessary.
|
2019-03-26 14:19:30 +03:00
|
|
|
checkFillState: async function(depth=0) {
|
2016-01-11 14:38:04 +03:00
|
|
|
if (this.unmounted) {
|
2016-01-08 15:03:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:19:30 +03:00
|
|
|
const isFirstCall = depth === 0;
|
2017-10-11 19:56:17 +03:00
|
|
|
const sn = this._getScrollNode();
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-01-04 19:28:32 +03:00
|
|
|
// if there is less than a screenful of messages above or below the
|
|
|
|
// viewport, try to get some more messages.
|
|
|
|
//
|
|
|
|
// scrollTop is the number of pixels between the top of the content and
|
|
|
|
// the top of the viewport.
|
|
|
|
//
|
|
|
|
// scrollHeight is the total height of the content.
|
|
|
|
//
|
|
|
|
// clientHeight is the height of the viewport (excluding borders,
|
|
|
|
// margins, and scrollbars).
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// .---------. - -
|
|
|
|
// | | | scrollTop |
|
|
|
|
// .-+---------+-. - - |
|
|
|
|
// | | | | | |
|
|
|
|
// | | | | | clientHeight | scrollHeight
|
|
|
|
// | | | | | |
|
|
|
|
// `-+---------+-' - |
|
|
|
|
// | | |
|
|
|
|
// | | |
|
|
|
|
// `---------' -
|
|
|
|
//
|
|
|
|
|
2019-03-26 15:34:55 +03:00
|
|
|
// as filling is async and recursive,
|
|
|
|
// don't allow more than 1 chain of calls concurrently
|
|
|
|
// do make a note when a new request comes in while already running one,
|
|
|
|
// so we can trigger a new chain of calls once done.
|
2019-03-26 14:19:30 +03:00
|
|
|
if (isFirstCall) {
|
|
|
|
if (this._isFilling) {
|
|
|
|
debuglog("_isFilling: not entering while request is ongoing, marking for a subsequent request");
|
|
|
|
this._fillRequestWhileRunning = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debuglog("_isFilling: setting");
|
|
|
|
this._isFilling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-19 18:51:39 +03:00
|
|
|
const contentHeight = this._getMessagesHeight();
|
|
|
|
const contentTop = contentHeight - this._getListHeight();
|
|
|
|
const contentScrollTop = sn.scrollTop + contentTop;
|
2019-03-26 14:19:30 +03:00
|
|
|
const fillPromises = [];
|
|
|
|
|
2019-03-19 18:51:39 +03:00
|
|
|
if (contentScrollTop < sn.clientHeight) {
|
2016-01-04 19:28:32 +03:00
|
|
|
// need to back-fill
|
2019-03-26 14:19:30 +03:00
|
|
|
fillPromises.push(this._maybeFill(depth, true));
|
2015-12-24 03:10:21 +03:00
|
|
|
}
|
2019-03-19 18:51:39 +03:00
|
|
|
if (contentScrollTop > contentHeight - sn.clientHeight * 2) {
|
2016-01-04 19:28:32 +03:00
|
|
|
// need to forward-fill
|
2019-03-26 14:19:30 +03:00
|
|
|
fillPromises.push(this._maybeFill(depth, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fillPromises.length) {
|
|
|
|
try {
|
|
|
|
await Promise.all(fillPromises);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isFirstCall) {
|
|
|
|
debuglog("_isFilling: clearing");
|
|
|
|
this._isFilling = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._fillRequestWhileRunning) {
|
|
|
|
this._fillRequestWhileRunning = false;
|
|
|
|
this.checkFillState();
|
2015-12-22 18:18:50 +03:00
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-11-16 17:25:52 +03:00
|
|
|
// check if unfilling is possible and send an unfill request if necessary
|
2017-09-06 19:40:58 +03:00
|
|
|
_checkUnfillState: function(backwards) {
|
2016-11-16 17:25:52 +03:00
|
|
|
let excessHeight = this._getExcessHeight(backwards);
|
|
|
|
if (excessHeight <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-19 15:42:22 +03:00
|
|
|
|
|
|
|
const origExcessHeight = excessHeight;
|
|
|
|
|
2017-04-04 15:28:26 +03:00
|
|
|
const tiles = this.refs.itemlist.children;
|
2016-11-16 17:25:52 +03:00
|
|
|
|
|
|
|
// The scroll token of the first/last tile to be unpaginated
|
|
|
|
let markerScrollToken = null;
|
|
|
|
|
2017-04-04 15:28:26 +03:00
|
|
|
// Subtract heights of tiles to simulate the tiles being unpaginated until the
|
|
|
|
// excess height is less than the height of the next tile to subtract. This
|
|
|
|
// prevents excessHeight becoming negative, which could lead to future
|
|
|
|
// pagination.
|
|
|
|
//
|
|
|
|
// If backwards is true, we unpaginate (remove) tiles from the back (top).
|
2019-03-19 15:42:22 +03:00
|
|
|
let tile;
|
2017-04-05 19:48:24 +03:00
|
|
|
for (let i = 0; i < tiles.length; i++) {
|
2019-03-19 15:42:22 +03:00
|
|
|
tile = tiles[backwards ? i : tiles.length - 1 - i];
|
2017-04-04 15:28:26 +03:00
|
|
|
// Subtract height of tile as if it were unpaginated
|
2017-04-05 19:48:24 +03:00
|
|
|
excessHeight -= tile.clientHeight;
|
2017-06-07 06:49:41 +03:00
|
|
|
//If removing the tile would lead to future pagination, break before setting scroll token
|
|
|
|
if (tile.clientHeight > excessHeight) {
|
|
|
|
break;
|
|
|
|
}
|
2017-04-04 15:28:26 +03:00
|
|
|
// The tile may not have a scroll token, so guard it
|
2017-05-04 12:00:13 +03:00
|
|
|
if (tile.dataset.scrollTokens) {
|
|
|
|
markerScrollToken = tile.dataset.scrollTokens.split(',')[0];
|
2017-04-05 19:48:24 +03:00
|
|
|
}
|
2016-11-16 17:25:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (markerScrollToken) {
|
2016-11-22 19:47:56 +03:00
|
|
|
// Use a debouncer to prevent multiple unfill calls in quick succession
|
|
|
|
// This is to make the unfilling process less aggressive
|
|
|
|
if (this._unfillDebouncer) {
|
|
|
|
clearTimeout(this._unfillDebouncer);
|
|
|
|
}
|
|
|
|
this._unfillDebouncer = setTimeout(() => {
|
2016-11-22 20:23:06 +03:00
|
|
|
this._unfillDebouncer = null;
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("unfilling now", backwards, origExcessHeight);
|
2016-11-22 19:47:56 +03:00
|
|
|
this.props.onUnfillRequest(backwards, markerScrollToken);
|
2016-11-22 20:23:06 +03:00
|
|
|
}, UNFILL_REQUEST_DEBOUNCE_MS);
|
2016-11-16 17:25:52 +03:00
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-11-16 17:25:52 +03:00
|
|
|
|
2015-12-24 03:10:21 +03:00
|
|
|
// check if there is already a pending fill request. If not, set one off.
|
2019-03-26 14:19:30 +03:00
|
|
|
_maybeFill: function(depth, backwards) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const dir = backwards ? 'b' : 'f';
|
2015-12-24 03:10:21 +03:00
|
|
|
if (this._pendingFillRequests[dir]) {
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("Already a "+dir+" fill in progress - not starting another");
|
2015-12-24 03:10:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("starting "+dir+" fill");
|
2015-12-24 03:10:21 +03:00
|
|
|
|
|
|
|
// onFillRequest can end up calling us recursively (via onScroll
|
2017-07-12 17:15:59 +03:00
|
|
|
// events) so make sure we set this before firing off the call.
|
2015-12-24 03:10:21 +03:00
|
|
|
this._pendingFillRequests[dir] = true;
|
|
|
|
|
2019-03-26 15:34:55 +03:00
|
|
|
// wait 1ms before paginating, because otherwise
|
|
|
|
// this will block the scroll event handler for +700ms
|
|
|
|
// if messages are already cached in memory,
|
|
|
|
// This would cause jumping to happen on Chrome/macOS.
|
2019-03-26 14:19:30 +03:00
|
|
|
return new Promise(resolve => setTimeout(resolve, 1)).then(() => {
|
2017-07-12 17:15:59 +03:00
|
|
|
return this.props.onFillRequest(backwards);
|
|
|
|
}).finally(() => {
|
2015-12-24 03:10:21 +03:00
|
|
|
this._pendingFillRequests[dir] = false;
|
2016-01-04 19:28:32 +03:00
|
|
|
}).then((hasMoreResults) => {
|
2016-12-06 17:59:10 +03:00
|
|
|
if (this.unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-16 17:25:52 +03:00
|
|
|
// Unpaginate once filling is complete
|
|
|
|
this._checkUnfillState(!backwards);
|
|
|
|
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog(""+dir+" fill complete; hasMoreResults:"+hasMoreResults);
|
2016-01-04 19:28:32 +03:00
|
|
|
if (hasMoreResults) {
|
2015-12-24 03:10:21 +03:00
|
|
|
// further pagination requests have been disabled until now, so
|
|
|
|
// it's time to check the fill state again in case the pagination
|
|
|
|
// was insufficient.
|
2019-03-26 14:19:30 +03:00
|
|
|
return this.checkFillState(depth + 1);
|
2015-12-24 03:10:21 +03:00
|
|
|
}
|
2019-03-26 14:19:30 +03:00
|
|
|
});
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-02-03 11:03:10 +03:00
|
|
|
/* get the current scroll state. This returns an object with the following
|
|
|
|
* properties:
|
|
|
|
*
|
|
|
|
* boolean stuckAtBottom: true if we are tracking the bottom of the
|
|
|
|
* scroll. false if we are tracking a particular child.
|
|
|
|
*
|
|
|
|
* string trackedScrollToken: undefined if stuckAtBottom is true; if it is
|
2017-05-04 15:03:04 +03:00
|
|
|
* false, the first token in data-scroll-tokens of the child which we are
|
2017-05-04 12:00:13 +03:00
|
|
|
* tracking.
|
2016-02-03 11:03:10 +03:00
|
|
|
*
|
2019-03-19 15:42:22 +03:00
|
|
|
* number bottomOffset: undefined if stuckAtBottom is true; if it is false,
|
2016-02-03 11:03:10 +03:00
|
|
|
* the number of pixels the bottom of the tracked child is above the
|
|
|
|
* bottom of the scroll panel.
|
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
getScrollState: function() {
|
2015-12-22 18:18:50 +03:00
|
|
|
return this.scrollState;
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
|
|
|
/* reset the saved scroll state.
|
|
|
|
*
|
|
|
|
* This is useful if the list is being replaced, and you don't want to
|
|
|
|
* preserve scroll even if new children happen to have the same scroll
|
|
|
|
* tokens as old ones.
|
2016-02-01 19:31:12 +03:00
|
|
|
*
|
|
|
|
* This will cause the viewport to be scrolled down to the bottom on the
|
|
|
|
* next update of the child list. This is different to scrollToBottom(),
|
|
|
|
* which would save the current bottom-most child as the active one (so is
|
|
|
|
* no use if no children exist yet, or if you are about to replace the
|
|
|
|
* child list.)
|
2015-12-22 18:18:50 +03:00
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
resetScrollState: function() {
|
2019-03-19 15:42:22 +03:00
|
|
|
this.scrollState = {
|
|
|
|
stuckAtBottom: this.props.startAtBottom,
|
|
|
|
};
|
|
|
|
this._bottomGrowth = 0;
|
|
|
|
this._pages = 0;
|
|
|
|
this._scrollTimeout = new Timer(100);
|
|
|
|
this._heightUpdateInProgress = false;
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-04-05 15:14:11 +03:00
|
|
|
/**
|
|
|
|
* jump to the top of the content.
|
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
scrollToTop: function() {
|
2019-03-19 15:42:22 +03:00
|
|
|
this._getScrollNode().scrollTop = 0;
|
2016-04-05 15:14:11 +03:00
|
|
|
this._saveScrollState();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-04-05 15:14:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* jump to the bottom of the content.
|
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
scrollToBottom: function() {
|
2016-02-01 19:31:12 +03:00
|
|
|
// the easiest way to make sure that the scroll state is correctly
|
|
|
|
// saved is to do the scroll, then save the updated state. (Calculating
|
|
|
|
// it ourselves is hard, and we can't rely on an onScroll callback
|
|
|
|
// happening, since there may be no user-visible change here).
|
2019-03-19 15:42:22 +03:00
|
|
|
const sn = this._getScrollNode();
|
|
|
|
sn.scrollTop = sn.scrollHeight;
|
2016-02-01 19:31:12 +03:00
|
|
|
this._saveScrollState();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2016-04-05 15:14:11 +03:00
|
|
|
/**
|
|
|
|
* Page up/down.
|
|
|
|
*
|
2019-03-19 15:42:22 +03:00
|
|
|
* @param {number} mult: -1 to page up, +1 to page down
|
2016-04-05 15:14:11 +03:00
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
scrollRelative: function(mult) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const scrollNode = this._getScrollNode();
|
|
|
|
const delta = mult * scrollNode.clientHeight * 0.5;
|
2019-03-19 15:42:22 +03:00
|
|
|
scrollNode.scrollTop = scrollNode.scrollTop + delta;
|
2016-04-05 15:14:11 +03:00
|
|
|
this._saveScrollState();
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-04-05 15:14:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll up/down in response to a scroll key
|
2019-03-19 15:42:22 +03:00
|
|
|
* @param {object} ev the keyboard event
|
2016-04-05 15:14:11 +03:00
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
handleScrollKey: function(ev) {
|
2016-04-05 15:14:11 +03:00
|
|
|
switch (ev.keyCode) {
|
|
|
|
case KeyCode.PAGE_UP:
|
2017-04-23 02:49:14 +03:00
|
|
|
if (!ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) {
|
|
|
|
this.scrollRelative(-1);
|
|
|
|
}
|
2016-04-05 15:14:11 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCode.PAGE_DOWN:
|
2017-04-23 02:49:14 +03:00
|
|
|
if (!ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) {
|
|
|
|
this.scrollRelative(1);
|
|
|
|
}
|
2016-04-05 15:14:11 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCode.HOME:
|
2017-04-23 02:49:14 +03:00
|
|
|
if (ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) {
|
2016-04-05 15:14:11 +03:00
|
|
|
this.scrollToTop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyCode.END:
|
2017-04-23 02:49:14 +03:00
|
|
|
if (ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) {
|
2016-04-05 15:14:11 +03:00
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-04-05 15:14:11 +03:00
|
|
|
|
2016-03-10 19:44:50 +03:00
|
|
|
/* Scroll the panel to bring the DOM node with the scroll token
|
|
|
|
* `scrollToken` into view.
|
|
|
|
*
|
2019-03-26 17:51:02 +03:00
|
|
|
* offsetBase gives the reference point for the pixelOffset. 0 means the
|
2016-03-10 19:44:50 +03:00
|
|
|
* top of the container, 1 means the bottom, and fractional values mean
|
|
|
|
* somewhere in the middle. If omitted, it defaults to 0.
|
|
|
|
*
|
2019-03-26 17:51:02 +03:00
|
|
|
* pixelOffset gives the number of pixels *above* the offsetBase that the
|
2016-03-10 19:44:50 +03:00
|
|
|
* node (specifically, the bottom of it) will be positioned. If omitted, it
|
|
|
|
* defaults to 0.
|
|
|
|
*/
|
2019-03-26 17:51:02 +03:00
|
|
|
scrollToToken: function(scrollToken, pixelOffset, offsetBase) {
|
|
|
|
pixelOffset = pixelOffset || 0;
|
2016-03-10 19:44:50 +03:00
|
|
|
offsetBase = offsetBase || 0;
|
2016-02-01 19:31:12 +03:00
|
|
|
|
2019-03-26 17:51:02 +03:00
|
|
|
// set the trackedScrollToken so we can get the node through _getTrackedNode
|
2016-02-01 19:31:12 +03:00
|
|
|
this.scrollState = {
|
2016-02-03 11:03:10 +03:00
|
|
|
stuckAtBottom: false,
|
|
|
|
trackedScrollToken: scrollToken,
|
2016-02-01 19:31:12 +03:00
|
|
|
};
|
2019-03-26 17:51:02 +03:00
|
|
|
const trackedNode = this._getTrackedNode();
|
|
|
|
const scrollNode = this._getScrollNode();
|
|
|
|
if (trackedNode) {
|
|
|
|
// set the scrollTop to the position we want.
|
|
|
|
// note though, that this might not succeed if the combination of offsetBase and pixelOffset
|
|
|
|
// would position the trackedNode towards the top of the viewport.
|
|
|
|
// This because when setting the scrollTop only 10 or so events might be loaded,
|
|
|
|
// not giving enough content below the trackedNode to scroll downwards
|
|
|
|
// enough so it ends up in the top of the viewport.
|
|
|
|
scrollNode.scrollTop = (trackedNode.offsetTop - (scrollNode.clientHeight * offsetBase)) + pixelOffset;
|
|
|
|
this._saveScrollState();
|
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-02-01 19:31:12 +03:00
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
_saveScrollState: function() {
|
2016-02-01 19:31:12 +03:00
|
|
|
if (this.props.stickyBottom && this.isAtBottom()) {
|
2016-02-03 11:03:10 +03:00
|
|
|
this.scrollState = { stuckAtBottom: true };
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("saved stuckAtBottom state");
|
2016-02-01 19:31:12 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2019-02-21 21:28:55 +03:00
|
|
|
const scrollNode = this._getScrollNode();
|
2019-03-19 15:42:22 +03:00
|
|
|
const viewportBottom = scrollNode.scrollHeight - (scrollNode.scrollTop + scrollNode.clientHeight);
|
2019-02-21 21:28:55 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const itemlist = this.refs.itemlist;
|
|
|
|
const messages = itemlist.children;
|
2019-02-21 21:28:55 +03:00
|
|
|
let node = null;
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
// TODO: do a binary search here, as items are sorted by offsetTop
|
check top of node instead of bottom, since coming in from last
as we're approaching from the last node, if we're scrolled up,
the first node we encounter would be below the bottom of the viewport
change the logic to stop at the first node that has its top
above the viewport bottom.
When completely scrolled up, this was causing nodes way below
the viewport to be selected as the reference for the pixelOffset,
and when pagination came in, it would immediately try to apply
the big negative pixelOffset, scrolling to a negative scrollTop,
thus going to the top again, and triggering another pagination,
entering in an infinite pagination loop until you scrolled down.
2019-02-26 18:26:24 +03:00
|
|
|
// loop backwards, from bottom-most message (as that is the most common case)
|
2017-10-11 19:56:17 +03:00
|
|
|
for (let i = messages.length-1; i >= 0; --i) {
|
2019-02-21 21:28:55 +03:00
|
|
|
if (!messages[i].dataset.scrollTokens) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
node = messages[i];
|
check top of node instead of bottom, since coming in from last
as we're approaching from the last node, if we're scrolled up,
the first node we encounter would be below the bottom of the viewport
change the logic to stop at the first node that has its top
above the viewport bottom.
When completely scrolled up, this was causing nodes way below
the viewport to be selected as the reference for the pixelOffset,
and when pagination came in, it would immediately try to apply
the big negative pixelOffset, scrolling to a negative scrollTop,
thus going to the top again, and triggering another pagination,
entering in an infinite pagination loop until you scrolled down.
2019-02-26 18:26:24 +03:00
|
|
|
// break at the first message (coming from the bottom)
|
|
|
|
// that has it's offsetTop above the bottom of the viewport.
|
2019-03-19 18:50:55 +03:00
|
|
|
if (this._topFromBottom(node) > viewportBottom) {
|
2017-04-04 13:55:53 +03:00
|
|
|
// Use this node as the scrollToken
|
|
|
|
break;
|
2015-12-22 18:18:50 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 21:28:55 +03:00
|
|
|
|
|
|
|
if (!node) {
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("unable to save scroll state: found no children in the viewport");
|
2019-02-21 21:28:55 +03:00
|
|
|
return;
|
2017-04-04 13:55:53 +03:00
|
|
|
}
|
2019-03-20 19:15:43 +03:00
|
|
|
const scrollToken = node.dataset.scrollTokens.split(',')[0];
|
|
|
|
debuglog("saving anchored scroll state to message", node && node.innerText, scrollToken);
|
2019-03-26 17:51:02 +03:00
|
|
|
const bottomOffset = this._topFromBottom(node);
|
2019-02-21 21:28:55 +03:00
|
|
|
this.scrollState = {
|
|
|
|
stuckAtBottom: false,
|
2019-03-19 15:42:22 +03:00
|
|
|
trackedNode: node,
|
2019-03-20 19:15:43 +03:00
|
|
|
trackedScrollToken: scrollToken,
|
2019-03-26 17:51:02 +03:00
|
|
|
bottomOffset: bottomOffset,
|
|
|
|
pixelOffset: bottomOffset - viewportBottom, //needed for restoring the scroll position when coming back to the room
|
2019-02-21 21:28:55 +03:00
|
|
|
};
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2019-03-19 18:53:23 +03:00
|
|
|
_restoreSavedScrollState: async function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const scrollState = this.scrollState;
|
2016-02-01 19:31:12 +03:00
|
|
|
|
2016-02-03 11:03:10 +03:00
|
|
|
if (scrollState.stuckAtBottom) {
|
2019-03-19 15:42:22 +03:00
|
|
|
const sn = this._getScrollNode();
|
|
|
|
sn.scrollTop = sn.scrollHeight;
|
2016-02-03 11:03:10 +03:00
|
|
|
} else if (scrollState.trackedScrollToken) {
|
2019-03-19 15:42:22 +03:00
|
|
|
const itemlist = this.refs.itemlist;
|
|
|
|
const trackedNode = this._getTrackedNode();
|
|
|
|
if (trackedNode) {
|
|
|
|
const newBottomOffset = this._topFromBottom(trackedNode);
|
|
|
|
const bottomDiff = newBottomOffset - scrollState.bottomOffset;
|
|
|
|
this._bottomGrowth += bottomDiff;
|
|
|
|
scrollState.bottomOffset = newBottomOffset;
|
|
|
|
itemlist.style.height = `${this._getListHeight()}px`;
|
2019-03-26 15:44:39 +03:00
|
|
|
debuglog("balancing height because messages below viewport grew by", bottomDiff);
|
2019-03-19 15:42:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this._heightUpdateInProgress) {
|
2019-03-19 18:53:23 +03:00
|
|
|
this._heightUpdateInProgress = true;
|
|
|
|
try {
|
|
|
|
await this._updateHeight();
|
|
|
|
} finally {
|
|
|
|
this._heightUpdateInProgress = false;
|
2019-03-19 15:42:22 +03:00
|
|
|
}
|
2019-03-20 19:12:49 +03:00
|
|
|
} else {
|
|
|
|
debuglog("not updating height because request already in progress");
|
2015-12-22 18:18:50 +03:00
|
|
|
}
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2019-03-19 15:42:22 +03:00
|
|
|
// need a better name that also indicates this will change scrollTop? Rebalance height? Reveal content?
|
|
|
|
async _updateHeight() {
|
2019-03-19 18:53:23 +03:00
|
|
|
// wait until user has stopped scrolling
|
|
|
|
if (this._scrollTimeout.isRunning()) {
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("updateHeight waiting for scrolling to end ... ");
|
2019-03-19 18:53:23 +03:00
|
|
|
await this._scrollTimeout.finished();
|
2019-03-20 19:12:49 +03:00
|
|
|
} else {
|
|
|
|
debuglog("updateHeight getting straight to business, no scrolling going on.");
|
2019-03-19 15:42:22 +03:00
|
|
|
}
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2019-03-19 18:53:23 +03:00
|
|
|
const sn = this._getScrollNode();
|
|
|
|
const itemlist = this.refs.itemlist;
|
|
|
|
const contentHeight = this._getMessagesHeight();
|
|
|
|
const minHeight = sn.clientHeight;
|
|
|
|
const height = Math.max(minHeight, contentHeight);
|
|
|
|
this._pages = Math.ceil(height / PAGE_SIZE);
|
|
|
|
this._bottomGrowth = 0;
|
|
|
|
const newHeight = this._getListHeight();
|
|
|
|
|
2019-03-20 13:00:27 +03:00
|
|
|
const scrollState = this.scrollState;
|
|
|
|
if (scrollState.stuckAtBottom) {
|
2019-03-19 18:53:23 +03:00
|
|
|
itemlist.style.height = `${newHeight}px`;
|
|
|
|
sn.scrollTop = sn.scrollHeight;
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("updateHeight to", newHeight);
|
2019-03-20 13:00:27 +03:00
|
|
|
} else if (scrollState.trackedScrollToken) {
|
2019-03-19 18:53:23 +03:00
|
|
|
const trackedNode = this._getTrackedNode();
|
2019-03-20 13:00:27 +03:00
|
|
|
// if the timeline has been reloaded
|
|
|
|
// this can be called before scrollToBottom or whatever has been called
|
2019-03-21 17:37:35 +03:00
|
|
|
// so don't do anything if the node has disappeared from
|
2019-03-20 13:00:27 +03:00
|
|
|
// the currently filled piece of the timeline
|
|
|
|
if (trackedNode) {
|
|
|
|
const oldTop = trackedNode.offsetTop;
|
2019-03-22 17:23:21 +03:00
|
|
|
// changing the height might change the scrollTop
|
|
|
|
// if the new height is smaller than the scrollTop.
|
|
|
|
// We calculate the diff that needs to be applied
|
|
|
|
// ourselves, so be sure to measure the
|
|
|
|
// scrollTop before changing the height.
|
|
|
|
const preexistingScrollTop = sn.scrollTop;
|
2019-03-20 13:00:27 +03:00
|
|
|
itemlist.style.height = `${newHeight}px`;
|
|
|
|
const newTop = trackedNode.offsetTop;
|
|
|
|
const topDiff = newTop - oldTop;
|
2019-03-22 17:23:21 +03:00
|
|
|
sn.scrollTop = preexistingScrollTop + topDiff;
|
|
|
|
debuglog("updateHeight to", {newHeight, topDiff, preexistingScrollTop});
|
2019-03-20 13:00:27 +03:00
|
|
|
}
|
2019-03-19 15:42:22 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getTrackedNode() {
|
|
|
|
const scrollState = this.scrollState;
|
|
|
|
const trackedNode = scrollState.trackedNode;
|
|
|
|
|
|
|
|
if (!trackedNode || !trackedNode.parentElement) {
|
|
|
|
let node;
|
|
|
|
const messages = this.refs.itemlist.children;
|
|
|
|
const scrollToken = scrollState.trackedScrollToken;
|
|
|
|
|
|
|
|
for (let i = messages.length-1; i >= 0; --i) {
|
|
|
|
const m = messages[i];
|
|
|
|
// 'data-scroll-tokens' is a DOMString of comma-separated scroll tokens
|
|
|
|
// There might only be one scroll token
|
|
|
|
if (m.dataset.scrollTokens &&
|
|
|
|
m.dataset.scrollTokens.split(',').indexOf(scrollToken) !== -1) {
|
|
|
|
node = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 13:10:04 +03:00
|
|
|
if (node) {
|
|
|
|
debuglog("had to find tracked node again for " + scrollState.trackedScrollToken);
|
|
|
|
}
|
2019-03-19 15:42:22 +03:00
|
|
|
scrollState.trackedNode = node;
|
|
|
|
}
|
2016-03-22 22:33:02 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
if (!scrollState.trackedNode) {
|
2019-03-20 19:12:49 +03:00
|
|
|
debuglog("No node with ; '"+scrollState.trackedScrollToken+"'");
|
2019-03-19 15:42:22 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-03-22 22:33:02 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
return scrollState.trackedNode;
|
|
|
|
},
|
2016-03-23 13:51:07 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
_getListHeight() {
|
|
|
|
return this._bottomGrowth + (this._pages * PAGE_SIZE);
|
|
|
|
},
|
2016-03-22 22:33:02 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
_getMessagesHeight() {
|
|
|
|
const itemlist = this.refs.itemlist;
|
|
|
|
const lastNode = itemlist.lastElementChild;
|
|
|
|
// 18 is itemlist padding
|
|
|
|
return (lastNode.offsetTop + lastNode.clientHeight) - itemlist.firstElementChild.offsetTop + (18 * 2);
|
|
|
|
},
|
2016-03-22 22:33:02 +03:00
|
|
|
|
2019-03-19 15:42:22 +03:00
|
|
|
_topFromBottom(node) {
|
|
|
|
return this.refs.itemlist.clientHeight - node.offsetTop;
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2016-02-01 19:31:12 +03:00
|
|
|
|
2015-12-22 18:18:50 +03:00
|
|
|
/* get the DOM node which has the scrollTop property we care about for our
|
|
|
|
* message panel.
|
|
|
|
*/
|
2017-09-06 19:40:58 +03:00
|
|
|
_getScrollNode: function() {
|
2016-01-11 14:38:04 +03:00
|
|
|
if (this.unmounted) {
|
2016-01-08 15:03:45 +03:00
|
|
|
// this shouldn't happen, but when it does, turn the NPE into
|
|
|
|
// something more meaningful.
|
|
|
|
throw new Error("ScrollPanel._getScrollNode called when unmounted");
|
|
|
|
}
|
|
|
|
|
2019-03-12 18:30:06 +03:00
|
|
|
if (!this._divScroll) {
|
2018-03-21 18:58:14 +03:00
|
|
|
// Likewise, we should have the ref by this point, but if not
|
|
|
|
// turn the NPE into something meaningful.
|
|
|
|
throw new Error("ScrollPanel._getScrollNode called before gemini ref collected");
|
|
|
|
}
|
|
|
|
|
2019-03-12 18:30:06 +03:00
|
|
|
return this._divScroll;
|
2018-03-21 18:58:14 +03:00
|
|
|
},
|
|
|
|
|
2019-03-12 18:30:06 +03:00
|
|
|
_collectScroll: function(divScroll) {
|
|
|
|
this._divScroll = divScroll;
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
2019-01-22 13:37:09 +03:00
|
|
|
|
2019-01-18 20:53:54 +03:00
|
|
|
/**
|
2019-03-20 19:02:53 +03:00
|
|
|
Mark the bottom offset of the last tile so we can balance it out when
|
|
|
|
anything below it changes, by calling updatePreventShrinking, to keep
|
|
|
|
the same minimum bottom offset, effectively preventing the timeline to shrink.
|
|
|
|
*/
|
|
|
|
preventShrinking: function() {
|
2019-01-18 20:53:54 +03:00
|
|
|
const messageList = this.refs.itemlist;
|
2019-03-20 19:02:53 +03:00
|
|
|
const tiles = messageList && messageList.children;
|
|
|
|
if (!messageList) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let lastTileNode;
|
|
|
|
for (let i = tiles.length - 1; i >= 0; i--) {
|
|
|
|
const node = tiles[i];
|
|
|
|
if (node.dataset.scrollTokens) {
|
|
|
|
lastTileNode = node;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-18 20:53:54 +03:00
|
|
|
}
|
2019-03-20 19:02:53 +03:00
|
|
|
if (!lastTileNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.clearPreventShrinking();
|
|
|
|
const offsetFromBottom = messageList.clientHeight - (lastTileNode.offsetTop + lastTileNode.clientHeight);
|
|
|
|
this.preventShrinkingState = {
|
|
|
|
offsetFromBottom: offsetFromBottom,
|
|
|
|
offsetNode: lastTileNode,
|
|
|
|
};
|
|
|
|
debuglog("prevent shrinking, last tile ", offsetFromBottom, "px from bottom");
|
2019-01-18 20:53:54 +03:00
|
|
|
},
|
2019-01-22 13:37:09 +03:00
|
|
|
|
2019-03-20 19:02:53 +03:00
|
|
|
/** Clear shrinking prevention. Used internally, and when the timeline is reloaded. */
|
|
|
|
clearPreventShrinking: function() {
|
2019-01-18 20:53:54 +03:00
|
|
|
const messageList = this.refs.itemlist;
|
2019-03-20 19:02:53 +03:00
|
|
|
const balanceElement = messageList && messageList.parentElement;
|
|
|
|
if (balanceElement) balanceElement.style.paddingBottom = null;
|
|
|
|
this.preventShrinkingState = null;
|
|
|
|
debuglog("prevent shrinking cleared");
|
2019-01-18 20:53:54 +03:00
|
|
|
},
|
2015-12-22 18:18:50 +03:00
|
|
|
|
2019-03-20 19:02:53 +03:00
|
|
|
/**
|
|
|
|
update the container padding to balance
|
|
|
|
the bottom offset of the last tile since
|
|
|
|
preventShrinking was called.
|
|
|
|
Clears the prevent-shrinking state ones the offset
|
|
|
|
from the bottom of the marked tile grows larger than
|
|
|
|
what it was when marking.
|
|
|
|
*/
|
|
|
|
updatePreventShrinking: function() {
|
|
|
|
if (this.preventShrinkingState) {
|
|
|
|
const sn = this._getScrollNode();
|
|
|
|
const scrollState = this.scrollState;
|
|
|
|
const messageList = this.refs.itemlist;
|
|
|
|
const {offsetNode, offsetFromBottom} = this.preventShrinkingState;
|
|
|
|
// element used to set paddingBottom to balance the typing notifs disappearing
|
|
|
|
const balanceElement = messageList.parentElement;
|
|
|
|
// if the offsetNode got unmounted, clear
|
|
|
|
let shouldClear = !offsetNode.parentElement;
|
|
|
|
// also if 200px from bottom
|
|
|
|
if (!shouldClear && !scrollState.stuckAtBottom) {
|
|
|
|
const spaceBelowViewport = sn.scrollHeight - (sn.scrollTop + sn.clientHeight);
|
|
|
|
shouldClear = spaceBelowViewport >= 200;
|
|
|
|
}
|
|
|
|
// try updating if not clearing
|
|
|
|
if (!shouldClear) {
|
|
|
|
const currentOffset = messageList.clientHeight - (offsetNode.offsetTop + offsetNode.clientHeight);
|
|
|
|
const offsetDiff = offsetFromBottom - currentOffset;
|
|
|
|
if (offsetDiff > 0) {
|
|
|
|
balanceElement.style.paddingBottom = `${offsetDiff}px`;
|
|
|
|
debuglog("update prevent shrinking ", offsetDiff, "px from bottom");
|
|
|
|
} else if (offsetDiff < 0) {
|
|
|
|
shouldClear = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldClear) {
|
|
|
|
this.clearPreventShrinking();
|
2019-02-22 15:12:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-06 19:40:58 +03:00
|
|
|
render: function() {
|
2015-12-22 18:18:50 +03:00
|
|
|
// TODO: the classnames on the div and ol could do with being updated to
|
|
|
|
// reflect the fact that we don't necessarily contain a list of messages.
|
|
|
|
// it's not obvious why we have a separate div and ol anyway.
|
2019-03-12 18:30:06 +03:00
|
|
|
return (<AutoHideScrollbar wrappedRef={this._collectScroll}
|
|
|
|
onScroll={this.onScroll}
|
2019-03-19 15:42:22 +03:00
|
|
|
className={`mx_ScrollPanel ${this.props.className}`} style={this.props.style}>
|
2017-09-06 19:40:58 +03:00
|
|
|
<div className="mx_RoomView_messageListWrapper">
|
|
|
|
<ol ref="itemlist" className="mx_RoomView_MessageList" aria-live="polite">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ this.props.children }
|
2017-09-06 19:40:58 +03:00
|
|
|
</ol>
|
|
|
|
</div>
|
2019-03-12 18:30:06 +03:00
|
|
|
</AutoHideScrollbar>
|
|
|
|
);
|
2017-09-06 19:40:58 +03:00
|
|
|
},
|
|
|
|
});
|