UI: fix scroll down after sending a multiline message

This commit is contained in:
oobabooga 2024-02-17 19:24:04 -08:00
parent d6bd71db7f
commit 904867a139

View File

@ -377,36 +377,41 @@ function toggleBigPicture() {
} }
//------------------------------------------------ //------------------------------------------------
// Define global CSS properties for resizing and // Handle the chat input box growth
// positioning certain elements
//------------------------------------------------ //------------------------------------------------
let currentChatInputHeight = 0; let currentChatInputHeight = 0;
// Update chat layout based on chat and input dimensions
function updateCssProperties() { function updateCssProperties() {
// Set the height of the chat area
const chatContainer = document.getElementById("chat").parentNode.parentNode.parentNode; const chatContainer = document.getElementById("chat").parentNode.parentNode.parentNode;
const chatInputHeight = document.querySelector("#chat-input textarea").clientHeight; const chatInputHeight = document.querySelector("#chat-input textarea").clientHeight;
// Check if the chat container is visible
if (chatContainer.clientHeight > 0) { if (chatContainer.clientHeight > 0) {
// Calculate new chat height and adjust CSS properties
const newChatHeight = `${chatContainer.clientHeight - chatInputHeight + 40}px`; const newChatHeight = `${chatContainer.clientHeight - chatInputHeight + 40}px`;
document.documentElement.style.setProperty("--chat-height", newChatHeight); document.documentElement.style.setProperty("--chat-height", newChatHeight);
document.documentElement.style.setProperty("--input-delta", `${chatInputHeight - 40}px`); document.documentElement.style.setProperty("--input-delta", `${chatInputHeight - 40}px`);
// Set the position offset of the chat input box // Get and set header height
const header = document.querySelector(".header_bar"); const header = document.querySelector(".header_bar");
const headerHeight = `${header.clientHeight}px`; const headerHeight = `${header.clientHeight}px`;
document.documentElement.style.setProperty("--header-height", headerHeight); document.documentElement.style.setProperty("--header-height", headerHeight);
// Offset the scroll position of the chat area // Adjust scrollTop based on input height change
if (chatInputHeight !== currentChatInputHeight) { if (chatInputHeight !== currentChatInputHeight) {
chatContainer.scrollTop += chatInputHeight > currentChatInputHeight ? chatInputHeight : -chatInputHeight; chatContainer.scrollTop += chatInputHeight > currentChatInputHeight ? chatInputHeight : -chatInputHeight + 40;
currentChatInputHeight = chatInputHeight; currentChatInputHeight = chatInputHeight;
} }
} }
} }
// Observe textarea size changes and call update function
new ResizeObserver(updateCssProperties) new ResizeObserver(updateCssProperties)
.observe(document.querySelector("#chat-input textarea")); .observe(document.querySelector("#chat-input textarea"));
// Handle changes in window size
window.addEventListener("resize", updateCssProperties); window.addEventListener("resize", updateCssProperties);
//------------------------------------------------ //------------------------------------------------