Make PostStreamScrubber work for Posts that have top margin (#2369)

Also fixes incorrect page count when scrolling to bottom (https://github.com/flarum/core/issues/1897)
This commit is contained in:
Wadim Kalmykov 2020-10-16 04:35:22 +07:00 committed by GitHub
parent 543b136f7c
commit ac42a5900d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -189,9 +189,9 @@ export default class PostStream extends Component {
// seen if the browser were scrolled right up to the top of the page, // seen if the browser were scrolled right up to the top of the page,
// and the viewport had a height of 0. // and the viewport had a height of 0.
const $items = this.$('.PostStream-item[data-index]'); const $items = this.$('.PostStream-item[data-index]');
let index = $items.first().data('index') || 0;
let visible = 0; let visible = 0;
let period = ''; let period = '';
let indexFromViewPort = null;
// Now loop through each of the items in the discussion. An 'item' is // Now loop through each of the items in the discussion. An 'item' is
// either a single post or a 'gap' of one or more posts that haven't // either a single post or a 'gap' of one or more posts that haven't
@ -217,8 +217,10 @@ export default class PostStream extends Component {
const visibleBottom = Math.min(height, viewportTop + viewportHeight - top); const visibleBottom = Math.min(height, viewportTop + viewportHeight - top);
const visiblePost = visibleBottom - visibleTop; const visiblePost = visibleBottom - visibleTop;
if (top <= viewportTop) { // We take the index of the first item that passed the previous checks.
index = parseFloat($this.data('index')) + visibleTop / height; // It is the item that is first visible in the viewport.
if (indexFromViewPort === null) {
indexFromViewPort = parseFloat($this.data('index')) + visibleTop / height;
} }
if (visiblePost > 0) { if (visiblePost > 0) {
@ -231,7 +233,10 @@ export default class PostStream extends Component {
if (time) period = time; if (time) period = time;
}); });
this.stream.index = index + 1; // If indexFromViewPort is null, it means no posts are visible in the
// viewport. This can happen, when drafting a long reply post. In that case
// set the index to the last post.
this.stream.index = indexFromViewPort !== null ? indexFromViewPort + 1 : this.stream.count();
this.stream.visible = visible; this.stream.visible = visible;
if (period) this.stream.description = dayjs(period).format('MMMM YYYY'); if (period) this.stream.description = dayjs(period).format('MMMM YYYY');
} }