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 -3Showing whitespace changes
public/script.js+11 -3
@@ -227,7 +227,7 @@ import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay
227227import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels } from './scripts/textgen-models.js';
228228import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId } from './scripts/chats.js';
229229import { initPresetManager } from './scripts/preset-manager.js';
230230import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js';
231231import { currentUser, setUserControls } from './scripts/user.js';
232232import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js';
233233import { renderTemplate, renderTemplateAsync } from './scripts/templates.js';
@@ -1721,16 +1721,24 @@ export async function replaceCurrentChat() {
17211721}
17221722
17231723export function showMoreMessages() {
17241724 letconst messageIdfirstDisplayedMesId = Number($('#chat').children('.mes').first().attr('mesid'));
1725+ let messageId = Number(firstDisplayedMesId);
17251726 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+
17271734 console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length);
17281735 const prevHeight = $('#chat').prop('scrollHeight');
17291736
17301737 while (messageId > 0 && count > 0) {
1738+ let newMessageId = messageId - 1;
1739+ addOneMessage(chat[newMessageId], { insertBefore: messageId >= chat.length ? null : messageId, scroll: false, forceId: newMessageId });
17311740 count--;
17321741 messageId--;
1733- addOneMessage(chat[messageId], { insertBefore: messageId + 1, scroll: false, forceId: messageId });
17341742 }
17351743
17361744 if (messageId == 0) {