From 47a61f2523f9bfba010a38cb2dc25daf04187a3c Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 22 Apr 2021 22:44:24 -0300 Subject: [PATCH] Regression: Fix scroll to bottom (#21731) --- app/ui/client/views/app/room.js | 13 ++++++++----- client/lib/RoomManager.ts | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/ui/client/views/app/room.js b/app/ui/client/views/app/room.js index a326e2391af0..21e809a42935 100644 --- a/app/ui/client/views/app/room.js +++ b/app/ui/client/views/app/room.js @@ -782,6 +782,10 @@ Meteor.startup(() => { callbacks.remove('streamNewMessage', this.data._id); }); + const isAtBottom = function(element, scrollThreshold = 0) { + return element.scrollTop + scrollThreshold >= element.scrollHeight - element.clientHeight; + }; + Template.roomOld.onRendered(function() { const { _id: rid } = this.data; @@ -794,7 +798,7 @@ Meteor.startup(() => { const store = NewRoomManager.getStore(rid); const afterMessageGroup = () => { - if (store.scroll) { + if (store.scroll && !store.atBottom) { wrapper.scrollTop = store.scroll; } else { this.sendToBottom(); @@ -802,8 +806,8 @@ Meteor.startup(() => { wrapper.removeEventListener('MessageGroup', afterMessageGroup); wrapper.addEventListener('scroll', _.throttle(() => { - store.update({ scroll: wrapper.scrollTop }); - }, 100)); + store.update({ scroll: wrapper.scrollTop, atBottom: isAtBottom(wrapper, 50) }); + }, 30)); }; wrapper.addEventListener('MessageGroup', afterMessageGroup); @@ -820,7 +824,7 @@ Meteor.startup(() => { const messageBox = $('.messages-box'); template.isAtBottom = function(scrollThreshold = 0) { - if (wrapper.scrollTop + scrollThreshold >= wrapper.scrollHeight - wrapper.clientHeight) { + if (isAtBottom(wrapper, scrollThreshold)) { newMessage.className = 'new-message background-primary-action-color color-content-background-color not'; return true; } @@ -857,7 +861,6 @@ Meteor.startup(() => { }); Tracker.afterFlush(() => { - template.sendToBottomIfNecessary(); wrapper.addEventListener('scroll', wheelHandler); }); diff --git a/client/lib/RoomManager.ts b/client/lib/RoomManager.ts index 4ab17b214f0b..036b0a1e43a0 100644 --- a/client/lib/RoomManager.ts +++ b/client/lib/RoomManager.ts @@ -17,19 +17,33 @@ export class RoomStore extends Emitter<{ scroll?: number; + atBottom = true; + constructor(readonly rid: string) { super(); debug && this.on('changed', () => console.log(`RoomStore ${this.rid} changed`, this)); } - update({ scroll, lastTime }: { scroll?: number; lastTime?: Date }): void { + update({ + scroll, + lastTime, + atBottom, + }: { + scroll?: number; + lastTime?: Date; + atBottom?: boolean; + }): void { if (scroll !== undefined) { this.scroll = scroll; } if (lastTime !== undefined) { this.lastTime = lastTime; } + + if (atBottom !== undefined) { + this.atBottom = atBottom; + } if (scroll || lastTime) { this.emit('changed'); }