Skip to content

Commit 9225c03

Browse files
committed
refactor: remove intermediate function and extract scroll wrappers for clarity
1 parent b7dfb92 commit 9225c03

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/webview/chat.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,40 +129,38 @@ const LINE_HEIGHT = 36;
129129

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

139-
function withScroll(wrapped, stickyOnly = true) {
140-
if (stickyOnly) {
141-
return function () {
142-
const { scrollHeight: scrollHeightBefore } = messageContainer;
143-
wrapped.apply(this, arguments);
144-
const { scrollHeight: scrollHeightAfter } = messageContainer;
145-
stickyScrollToBottom(scrollHeightAfter - scrollHeightBefore);
146-
};
147-
} else {
148-
return function () {
149-
wrapped.apply(this, arguments);
150-
const { clientHeight, scrollHeight } = messageContainer;
139+
// After the function completes, scroll the message container if it was already scrolled to the bottom
140+
function withStickyScroll(wrapped) {
141+
return function () {
142+
const { scrollHeight: scrollHeightBefore } = messageContainer;
143+
wrapped.apply(this, arguments);
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) {
151150
messageContainer.scrollTop = scrollHeight - clientHeight;
152-
};
153-
}
151+
}
152+
};
154153
}
155154

156-
// If we're already at the bottom, scroll the bottom into view
157-
function stickyScrollToBottom(diff = LINE_HEIGHT) {
158-
const { scrollTop, clientHeight, scrollHeight } = messageContainer;
159-
const scrollDiff = Math.abs(scrollHeight - clientHeight - scrollTop);
160-
const isScrolledToBottom = scrollDiff <= diff + 1;
161-
162-
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;
163160
messageContainer.scrollTop = scrollHeight - clientHeight;
164-
}
161+
};
165162
}
163+
// If we're already at the bottom, scroll the bottom into view
166164

167165
const setupCopyButton = (block) => {
168166
if (navigator.clipboard) {

0 commit comments

Comments
 (0)