Merge pull request #2587 from SillyTavern/fix-lazy-loading-chat-when-empty Fix "show more messages" on empty chat

2a08916efe0c141a56a8693907d91488464136eb

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +11 -3Ignore whitespace
public/script.js+11 -3
@@ -227,7 +227,7 @@ import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay
227import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels } from './scripts/textgen-models.js';227import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels } from './scripts/textgen-models.js';
228import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId } from './scripts/chats.js';228import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId } from './scripts/chats.js';
229import { initPresetManager } from './scripts/preset-manager.js';229import { initPresetManager } from './scripts/preset-manager.js';
230import { MacrosParser, evaluateMacros } from './scripts/macros.js';230import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js';
231import { currentUser, setUserControls } from './scripts/user.js';231import { currentUser, setUserControls } from './scripts/user.js';
232import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js';232import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js';
233import { renderTemplate, renderTemplateAsync } from './scripts/templates.js';233import { renderTemplate, renderTemplateAsync } from './scripts/templates.js';
@@ -1721,16 +1721,24 @@ export async function replaceCurrentChat() {
1721}1721}
17221722
1723export function showMoreMessages() {1723export function showMoreMessages() {
1724 let messageId = Number($('#chat').children('.mes').first().attr('mesid'));1724 const firstDisplayedMesId = $('#chat').children('.mes').first().attr('mesid');
1725 let messageId = Number(firstDisplayedMesId);
1725 let count = power_user.chat_truncation || Number.MAX_SAFE_INTEGER;1726 let count = power_user.chat_truncation || Number.MAX_SAFE_INTEGER;
17261727
1728 // If there are no messages displayed, or the message somehow has no mesid, we default to one higher than last message id,
1729 // so the first "new" message being shown will be the last available message
1730 if (isNaN(messageId)) {
1731 messageId = getLastMessageId() + 1;
1732 }
1733
1727 console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length);1734 console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length);
1728 const prevHeight = $('#chat').prop('scrollHeight');1735 const prevHeight = $('#chat').prop('scrollHeight');
17291736
1730 while (messageId > 0 && count > 0) {1737 while (messageId > 0 && count > 0) {
1738 let newMessageId = messageId - 1;
1739 addOneMessage(chat[newMessageId], { insertBefore: messageId >= chat.length ? null : messageId, scroll: false, forceId: newMessageId });
1731 count--;1740 count--;
1732 messageId--;1741 messageId--;
1733 addOneMessage(chat[messageId], { insertBefore: messageId + 1, scroll: false, forceId: messageId });
1734 }1742 }
17351743
1736 if (messageId == 0) {1744 if (messageId == 0) {