Merge pull request #2587 from SillyTavern/fix-lazy-loading-chat-when-empty Fix "show more messages" on empty chat
Signed| @@ -227,7 +227,7 @@ import { BulkEditOverlay, CharacterContextMenu } from './scripts/BulkEditOverlay | ||
| 227 | 227 | import { loadFeatherlessModels, loadMancerModels, loadOllamaModels, loadTogetherAIModels, loadInfermaticAIModels, loadOpenRouterModels, loadVllmModels, loadAphroditeModels, loadDreamGenModels } from './scripts/textgen-models.js'; |
| 228 | 228 | import { appendFileContent, hasPendingFileAttachment, populateFileAttachment, decodeStyleTags, encodeStyleTags, isExternalMediaAllowed, getCurrentEntityId } from './scripts/chats.js'; |
| 229 | 229 | import { initPresetManager } from './scripts/preset-manager.js'; |
| 230 | 230 | import { MacrosParser, evaluateMacros, getLastMessageId } from './scripts/macros.js'; |
| 231 | 231 | import { currentUser, setUserControls } from './scripts/user.js'; |
| 232 | 232 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup, fixToastrForDialogs } from './scripts/popup.js'; |
| 233 | 233 | import { renderTemplate, renderTemplateAsync } from './scripts/templates.js'; |
| @@ -1721,16 +1721,24 @@ export async function replaceCurrentChat() { | ||
| 1721 | 1721 | } |
| 1722 | 1722 | |
| 1723 | 1723 | export function showMoreMessages() { |
| 1724 | 1724 | letconst messageIdfirstDisplayedMesId = Number($('#chat').children('.mes').first().attr('mesid')); |
| 1725 | + let messageId = Number(firstDisplayedMesId); | |
| 1725 | 1726 | let count = power_user.chat_truncation || Number.MAX_SAFE_INTEGER; |
| 1726 | 1727 | |
| 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 | 1734 | console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length); |
| 1728 | 1735 | const prevHeight = $('#chat').prop('scrollHeight'); |
| 1729 | 1736 | |
| 1730 | 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 | 1740 | count--; |
| 1732 | 1741 | messageId--; |
| 1733 | - addOneMessage(chat[messageId], { insertBefore: messageId + 1, scroll: false, forceId: messageId }); | |
| 1734 | 1742 | } |
| 1735 | 1743 | |
| 1736 | 1744 | if (messageId == 0) { |