Skip to content

Commit

Permalink
Fix scroll on long discussions
Browse files Browse the repository at this point in the history
- Anchor scroll when inserting post placeholders
- Indicate that pages are loading at start of `loadPage`, which allows `onscroll` to not request that multiple pages be loaded at the same time

These changes are particularly applicable to firefox, where previously, dozens of posts could be skipped at a time if scroll up was held while at the top of the viewport.
  • Loading branch information
askvortsov1 committed Dec 14, 2020
1 parent 7340918 commit 210a6b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions js/src/forum/components/PostStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ export default class PostStream extends Component {
*/
onscroll(top = window.pageYOffset) {
if (this.stream.paused) return;

this.updateScrubber(top);

if (this.stream.pagesLoading) return;

const marginTop = this.getMarginTop();
const viewportHeight = $(window).height() - marginTop;
const viewportTop = top + marginTop;
Expand All @@ -174,8 +179,6 @@ export default class PostStream extends Component {
// viewport) to 100ms.
clearTimeout(this.calculatePositionTimeout);
this.calculatePositionTimeout = setTimeout(this.calculatePosition.bind(this, top), 100);

this.updateScrubber(top);
}

updateScrubber(top = window.pageYOffset) {
Expand Down
19 changes: 11 additions & 8 deletions js/src/forum/states/PostStreamState.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,26 @@ class PostStreamState {
* @param {Boolean} backwards
*/
loadPage(start, end, backwards = false) {
m.redraw();
this.pagesLoading++;

const redraw = () => {
if (start < this.visibleStart || end > this.visibleEnd) return;

const anchorIndex = backwards ? this.visibleEnd - 1 : this.visibleStart;
anchorScroll(`.PostStream-item[data-index="${anchorIndex}"]`, m.redraw.sync);
};
redraw();

this.loadPageTimeouts[start] = setTimeout(
() => {
this.loadRange(start, end).then(() => {
if (start >= this.visibleStart && end <= this.visibleEnd) {
const anchorIndex = backwards ? this.visibleEnd - 1 : this.visibleStart;
anchorScroll(`.PostStream-item[data-index="${anchorIndex}"]`, () => m.redraw.sync());
}
redraw();
this.pagesLoading--;
});
this.loadPageTimeouts[start] = null;
},
this.pagesLoading ? 1000 : 0
this.pagesLoading - 1 ? 1000 : 0
);

this.pagesLoading++;
}

/**
Expand Down

0 comments on commit 210a6b3

Please sign in to comment.