Skip to content

Commit b3c4817

Browse files
committed
Private Messages: improve "scrollToBottom" logic
1 parent 7c9c45a commit b3c4817

File tree

1 file changed

+44
-5
lines changed
  • website/client/src/pages/private-messages

1 file changed

+44
-5
lines changed

website/client/src/pages/private-messages/index.vue

+44-5
Original file line numberDiff line numberDiff line change
@@ -1027,19 +1027,58 @@ export default defineComponent({
10271027
this.scrollToBottom();
10281028
}, 150);
10291029
},
1030-
scrollToBottom () {
1030+
/**
1031+
* This method does a couple of things:
1032+
* - first round:
1033+
* - tries to scroll down
1034+
* - in the next tick it triggers it again
1035+
* (during testing it seemed that the first trigger still had some space left to scroll)
1036+
* - 2nd round:
1037+
* - tries to scroll down
1038+
* - in the next tick it checks if the scrollTop is to most it can scroll down,
1039+
* if it is, it stops from doing that again
1040+
* if not, it goes into the next round
1041+
* - if we reach round 6 it stops completely,
1042+
* no need to have a endless loop of just scrolling down
1043+
*/
1044+
scrollToBottom (callCount = 0) {
1045+
if (callCount > 5) {
1046+
return;
1047+
}
1048+
10311049
if (!this.$refs.chatscroll) {
1050+
// if the message list component not loaded yet, but scrollToBottom was called
1051+
// just try again at a later time
1052+
setTimeout(() => {
1053+
this.scrollToBottom(callCount + 1);
1054+
}, 125);
10321055
return;
10331056
}
1034-
const chatscrollBeforeTick = this.$refs.chatscroll.$el;
1035-
chatscrollBeforeTick.scrollTop = chatscrollBeforeTick.scrollHeight;
1057+
1058+
const chatscrollEl = this.$refs.chatscroll.$el;
1059+
// chatscrollBeforeTick.scrollTop = chatscrollBeforeTick.scrollHeight;
1060+
chatscrollEl.scrollTo(0, chatscrollEl.scrollHeight);
10361061
10371062
Vue.nextTick(() => {
10381063
if (!this.$refs.chatscroll) {
10391064
return;
10401065
}
1041-
const chatscroll = this.$refs.chatscroll.$el;
1042-
chatscroll.scrollTop = chatscroll.scrollHeight;
1066+
1067+
let shouldRetrigger = true;
1068+
1069+
if (callCount > 1) {
1070+
const maxPossibleScrollPos = chatscrollEl.scrollHeight - chatscrollEl.clientHeight;
1071+
1072+
if (chatscrollEl.scrollTop === maxPossibleScrollPos) {
1073+
shouldRetrigger = false;
1074+
}
1075+
}
1076+
1077+
if (shouldRetrigger) {
1078+
setTimeout(() => {
1079+
this.scrollToBottom(callCount + 1);
1080+
}, 125);
1081+
}
10431082
});
10441083
},
10451084
infiniteScrollTrigger () {

0 commit comments

Comments
 (0)