Skip to content

Commit

Permalink
Regression: Fix scroll to bottom (#21731)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Apr 23, 2021
1 parent 7f33b8a commit 47a61f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 8 additions & 5 deletions app/ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -794,16 +798,16 @@ Meteor.startup(() => {
const store = NewRoomManager.getStore(rid);

const afterMessageGroup = () => {
if (store.scroll) {
if (store.scroll && !store.atBottom) {
wrapper.scrollTop = store.scroll;
} else {
this.sendToBottom();
}
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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -857,7 +861,6 @@ Meteor.startup(() => {
});

Tracker.afterFlush(() => {
template.sendToBottomIfNecessary();
wrapper.addEventListener('scroll', wheelHandler);
});

Expand Down
16 changes: 15 additions & 1 deletion client/lib/RoomManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit 47a61f2

Please sign in to comment.