From 0023988ada043e7e2257703144f0e1e7ebef2a0c Mon Sep 17 00:00:00 2001 From: Ennea Date: Wed, 28 Aug 2024 17:22:13 +0200 Subject: [PATCH] chat2web frontend: fortify autoscroll code so that it hopefully works across all devices.. scrolling needs another look or ten in the javascript spec, seriously --- ChatTwo/Http/static/start.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/ChatTwo/Http/static/start.js b/ChatTwo/Http/static/start.js index d3c34c3..bd48767 100644 --- a/ChatTwo/Http/static/start.js +++ b/ChatTwo/Http/static/start.js @@ -48,7 +48,7 @@ // adjust scroll when the window size changes; mostly for mobile (opening/closing the keyboard) window.addEventListener('resize', () => { if (this.scrolledToBottom) { - this.elements.messagesList.lastChild.scrollIntoView(); + this.scrollMessagesToBottom(); } }) @@ -98,11 +98,28 @@ } messagesAreScrolledToBottom() { - this.scrolledToBottom = - this.elements.messagesContainer.scrollTop >= this.elements.messagesContainer.scrollHeight - this.elements.messagesContainer.offsetHeight; + if (this.elements.messagesContainer.scrollTopMax) { + this.scrolledToBottom = this.elements.messagesContainer.scrollTop === this.elements.messagesContainer.scrollTopMax; + } else { + this.scrolledToBottom = + ( + this.elements.messagesContainer.scrollHeight - + this.elements.messagesContainer.clientHeight - + this.elements.messagesContainer.scrollTop + ) < 1; + } + return this.scrolledToBottom; } + scrollMessagesToBottom() { + if (this.elements.messagesContainer.scrollTopMax) { + this.elements.messagesContainer.scrollTop = this.elements.messagesContainer.scrollTopMax; + } else { + this.elements.messagesList.lastChild.scrollIntoView(); + } + } + addMessage(messageData) { const scrolledToBottom = this.messagesAreScrolledToBottom(); this.calculateTimestampWidth(messageData.timestamp); @@ -121,7 +138,7 @@ this.elements.messagesList.appendChild(liMessage); if (scrolledToBottom) { - liMessage.scrollIntoView(); + this.scrollMessagesToBottom(); } }