Skip to content

Commit f2582db

Browse files
committed
feat: improve chat scrolling to show max 3 lines of last user message
1 parent 10495e7 commit f2582db

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

_build/js/src/ui/localChat/messageHandlers.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { confirmDialog } from '../cofirmDialog';
1010
import { icon } from '../dom/icon';
1111
import { copy, edit, plus, textSelect, triangleError } from '../icons';
1212
import { createElement, nlToBr } from '../utils';
13+
import { scrollToBottom } from './modalActions';
1314

1415
import type { LocalChatConfig } from './types';
1516
import type {
@@ -59,7 +60,6 @@ export const addUserMessage = (msg: UserMessage) => {
5960
'div',
6061
`message-wrapper user ${msg.init ? '' : 'new'}`,
6162
);
62-
messageWrapper.style.setProperty('--user-msg-height', '-10px');
6363

6464
const messageElement = createElement('div', 'message user');
6565

@@ -160,6 +160,10 @@ export const addUserMessage = (msg: UserMessage) => {
160160
messageWrapper.appendChild(messageElement);
161161
globalState.modal.chatMessages.appendChild(messageWrapper);
162162

163+
const userMsgHeight = messageWrapper.firstElementChild?.clientHeight ?? 0;
164+
const msgHeight = userMsgHeight < 100 ? -10 : -1 * (userMsgHeight - 62);
165+
messageWrapper.style.setProperty('--user-msg-height', `${msgHeight}px`);
166+
163167
return messageWrapper;
164168
};
165169

@@ -187,11 +191,15 @@ export const addAssistantMessage = (msg: AssistantMessage, config: LocalChatConf
187191
'div',
188192
`message-wrapper ai ${msg.init ? '' : 'new'}`,
189193
);
194+
190195
if (globalState.modal.chatMessages.lastElementChild?.firstElementChild) {
191-
messageWrapper.style.setProperty(
192-
'--user-msg-height',
193-
`${globalState.modal.chatMessages.lastElementChild.firstElementChild.clientHeight}px`,
194-
);
196+
const msgHeight =
197+
globalState.modal.chatMessages.lastElementChild.firstElementChild.clientHeight < 100
198+
? globalState.modal.chatMessages.lastElementChild.firstElementChild.clientHeight
199+
: globalState.modal.chatMessages.lastElementChild.firstElementChild.clientHeight -
200+
(globalState.modal.chatMessages.lastElementChild.firstElementChild.clientHeight - 62) +
201+
10;
202+
messageWrapper.style.setProperty('--user-msg-height', `${msgHeight}px`);
195203
}
196204

197205
const messageElement = createElement('div', 'message ai');
@@ -363,25 +371,15 @@ export const renderMessage = (msg: Message, config: LocalChatConfig) => {
363371

364372
if (msg.__type === 'UserMessage') {
365373
const el = addUserMessage(msg);
366-
el.firstElementChild?.scrollIntoView({
367-
behavior: msg.init === true ? 'instant' : 'smooth',
368-
block: 'start',
369-
});
374+
if (!msg.init) {
375+
scrollToBottom('smooth');
376+
}
370377

371378
return el;
372379
}
373380

374381
if (msg.__type === 'AssistantMessage') {
375-
const el = addAssistantMessage(msg, config);
376-
377-
if (msg.init === true) {
378-
el.firstElementChild?.scrollIntoView({
379-
behavior: 'instant',
380-
block: 'start',
381-
});
382-
}
383-
384-
return el;
382+
return addAssistantMessage(msg, config);
385383
}
386384

387385
return;

_build/js/src/ui/localChat/modalActions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const switchType = (type: ModalType, config: LocalChatConfig) => {
272272
});
273273
}
274274

275-
scrollToBottom();
275+
scrollToBottom('instant');
276276
};
277277

278278
export const clearChat = () => {
@@ -312,6 +312,9 @@ export const handleImageUpload = async (fileOrUrl: File | string, isRemoteUrl: b
312312
globalState.modal.attachments.addImageAttachment(dataURL);
313313
};
314314

315-
export const scrollToBottom = () => {
316-
globalState.modal.chatContainer.scrollTop = globalState.modal.chatContainer.scrollHeight;
315+
export const scrollToBottom = (behavior: 'smooth' | 'instant' = 'smooth') => {
316+
globalState.modal.chatContainer.scrollTo({
317+
top: globalState.modal.chatContainer.scrollHeight,
318+
behavior: behavior,
319+
});
317320
};

_build/js/src/ui/localChat/modalBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { UpdatableHTMLElement } from '../../chatHistory';
1414

1515
export const buildModal = (config: LocalChatConfig) => {
1616
const { shadow, shadowRoot } = createModAIShadow<Modal>(true, () => {
17-
scrollToBottom();
17+
scrollToBottom('instant');
1818
shadow.messageInput.focus();
1919
});
2020

assets/components/modai/js/modai.js

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/components/modai/src/API/Prompt/Chat.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use modAI\Services\AIServiceFactory;
1111
use modAI\Services\Config\CompletionsConfig;
1212
use modAI\Settings;
13-
use modAI\Tools\GetWeather;
1413
use Psr\Http\Message\ServerRequestInterface;
1514

1615
class Chat extends API

0 commit comments

Comments
 (0)