Skip to content

Commit

Permalink
fix: webui message body do not autoscroll to bottom sometimes (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed May 19, 2024
1 parent 9b7d93e commit 482822d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
28 changes: 12 additions & 16 deletions assets/arena.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
position: relative;
display: flex;
flex-direction: column;
width: 100%;
width: calc(100% - 1rem);
margin-top: -2px;
padding-left: 0.625rem;
flex-grow: 1;
Expand Down Expand Up @@ -674,22 +674,25 @@

handleScrollChatBody(event, index) {
const chat = this.chats[index];
const { scrollTop, clientHeight, scrollHeight } = event.target;
if ((event.target._prevScrollTop || 0) > scrollTop + 1) {
const $chatBody = event.target;
const { scrollTop, clientHeight, scrollHeight, _prevScrollTop = 0 } = $chatBody;
if (scrollTop + clientHeight > scrollHeight - 5) {
chat.isShowScrollToBottomBtn = false;
chat.shouldScrollChatBodyToBottom = true;
}
if (scrollHeight > clientHeight && _prevScrollTop > 1 && _prevScrollTop > scrollTop + 1) {
chat.shouldScrollChatBodyToBottom = false;
chat.isShowScrollToBottomBtn = true;
} else if (chat.isShowScrollToBottomBtn && scrollTop + clientHeight > scrollHeight - 5) {
chat.isShowScrollToBottomBtn = false;
}
$chatBody._prevScrollTop = scrollTop;
},

handleScrollToBottom(index) {
const chat = this.chats[index];
const $chatBody = document.querySelector('#chat-body-' + index);
$chatBody.scrollTop = $chatBody.scrollHeight;
$chatBody._prevScrollTop = $chatBody.scrollTop;
chat.isShowScrollToBottomBtn = false;
chat.autoScrollChatBodyToBottom = true;
chat.shouldScrollChatBodyToBottom = true;
},

handleNewlineInput(event) {
Expand Down Expand Up @@ -743,11 +746,7 @@
if (chat.shouldScrollChatBodyToBottom) {
const $chatBody = document.querySelector('#chat-body-' + index);
if ($chatBody) {
const { scrollTop, scrollHeight, clientHeight } = $chatBody;
if (scrollTop + clientHeight < scrollHeight - 5) {
$chatBody.scrollTop = scrollHeight;
$chatBody._prevScrollTop = $chatBody.scrollTop;
}
$chatBody.scrollTop = $chatBody.scrollHeight;
}
}
},
Expand All @@ -774,10 +773,7 @@
async ask(index) {
const chat = this.chats[index];
chat.askAbortController = new AbortController();
this.$nextTick(() => {
chat.shouldScrollChatBodyToBottom = true;
this.autoScrollChatBodyToBottom(index);
});
chat.shouldScrollChatBodyToBottom = true;
const lastMessage = chat.messages[chat.messages.length - 1];
const body = this.buildBody(index);
let succeed = false;
Expand Down
33 changes: 15 additions & 18 deletions assets/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
position: relative;
display: flex;
flex-direction: column;
width: 100%;
width: calc(100% - 1rem);
margin-top: -2px;
padding-left: 0.625rem;
flex-grow: 1;
Expand Down Expand Up @@ -825,21 +825,24 @@
},

handleScrollChatBody(event) {
const { scrollTop, clientHeight, scrollHeight } = event.target;
if ((event.target._prevScrollTop || 0) > scrollTop + 1) {
const $chatBody = event.target;
const { scrollTop, clientHeight, scrollHeight, _prevScrollTop = 0 } = $chatBody;
if (scrollTop + clientHeight > scrollHeight - 5) {
this.isShowScrollToBottomBtn = false;
this.shouldScrollChatBodyToBottom = true;
}
if (scrollHeight > clientHeight && _prevScrollTop > 1 && _prevScrollTop > scrollTop + 1) {
this.shouldScrollChatBodyToBottom = false;
this.isShowScrollToBottomBtn = true;
} else if (this.isShowScrollToBottomBtn && scrollTop + clientHeight > scrollHeight - 5) {
this.isShowScrollToBottomBtn = false;
}
$chatBody._prevScrollTop = scrollTop;
},

handleScrollToBottom() {
const $chatBody = this.$refs["chat-body"];
$chatBody.scrollTop = $chatBody.scrollHeight;
$chatBody._prevScrollTop = $chatBody.scrollTop;
this.isShowScrollToBottomBtn = false;
this.autoScrollChatBodyToBottom = true;
this.shouldScrollChatBodyToBottom = true;
},

handleShowSidebarBtnClick() {
Expand Down Expand Up @@ -893,14 +896,11 @@

autoScrollChatBodyToBottom() {
if (this.shouldScrollChatBodyToBottom) {
const $chatBody = this.$refs["chat-body"];
if ($chatBody) {
const { scrollTop, scrollHeight, clientHeight } = $chatBody;
if (scrollTop + clientHeight < scrollHeight - 5) {
$chatBody.scrollTop = scrollHeight;
$chatBody._prevScrollTop = $chatBody.scrollTop;
}
let $chatBody = this.$refs["chat-body"];
if (!$chatBody) {
$chatBody = document.querySelector('[x-ref="chat-body"]')
}
$chatBody.scrollTop = $chatBody.scrollHeight;
}
},

Expand All @@ -925,10 +925,7 @@

async ask() {
this.askAbortController = new AbortController();
this.$nextTick(() => {
this.shouldScrollChatBodyToBottom = true;
this.autoScrollChatBodyToBottom();
});
this.shouldScrollChatBodyToBottom = true;
const lastMessage = this.messages[this.messages.length - 1];
const body = this.buildBody();
let succeed = false;
Expand Down

0 comments on commit 482822d

Please sign in to comment.