Skip to content

Commit 1628e5c

Browse files
authored
Merge pull request #189 from sourcery-ai/ben/sou-1540-sending-a-new-chat-request-should-scroll-to-the-bottom-of
feat: scroll to bottom when new messages are sent
2 parents 60c4450 + 9225c03 commit 1628e5c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/webview/chat.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const LINE_HEIGHT = 36;
7171
window.addEventListener("message", (event) => {
7272
const message = event.data;
7373
if (message.command === "add_result") {
74-
withStickyScroll(addMessageToUI)(message.result);
74+
addMessageToUI(message.result);
7575
} else if (message.command === "clear_chat") {
7676
clearAllMessages();
7777
} else if (message.command === "focus") {
@@ -129,32 +129,38 @@ const LINE_HEIGHT = 36;
129129

130130
function addMessageToUI(result) {
131131
if (result.role === "assistant") {
132-
addAssistantMessageToUI(result);
132+
withStickyScroll(addAssistantMessageToUI)(result);
133133
} else {
134-
addUserMessageToUI(result.textContent);
135-
addAssistantThinkingMessageToUI();
134+
withScroll(addUserMessageToUI)(result.textContent);
135+
withScroll(addAssistantThinkingMessageToUI)();
136136
}
137137
}
138138

139+
// After the function completes, scroll the message container if it was already scrolled to the bottom
139140
function withStickyScroll(wrapped) {
140141
return function () {
141142
const { scrollHeight: scrollHeightBefore } = messageContainer;
142143
wrapped.apply(this, arguments);
143-
const { scrollHeight: scrollHeightAfter } = messageContainer;
144-
stickyScrollToBottom(scrollHeightAfter - scrollHeightBefore);
144+
const { scrollHeight, scrollTop, clientHeight } = messageContainer;
145+
const scrollDiff = Math.abs(scrollHeight - clientHeight - scrollTop);
146+
const isScrolledToBottom =
147+
scrollDiff <= scrollHeight - scrollHeightBefore + 1;
148+
149+
if (isScrolledToBottom) {
150+
messageContainer.scrollTop = scrollHeight - clientHeight;
151+
}
145152
};
146153
}
147154

148-
// If we're already at the bottom, scroll the bottom into view
149-
function stickyScrollToBottom(diff = LINE_HEIGHT) {
150-
const { scrollTop, clientHeight, scrollHeight } = messageContainer;
151-
const scrollDiff = Math.abs(scrollHeight - clientHeight - scrollTop);
152-
const isScrolledToBottom = scrollDiff <= diff + 1;
153-
154-
if (isScrolledToBottom) {
155+
// After the function completes, scroll the message container to the bottom.
156+
function withScroll(wrapped) {
157+
return function () {
158+
wrapped.apply(this, arguments);
159+
const { clientHeight, scrollHeight } = messageContainer;
155160
messageContainer.scrollTop = scrollHeight - clientHeight;
156-
}
161+
};
157162
}
163+
// If we're already at the bottom, scroll the bottom into view
158164

159165
const setupCopyButton = (block) => {
160166
if (navigator.clipboard) {

0 commit comments

Comments
 (0)