Add /chat-render and /chat-reload commands
| @@ -167,6 +167,7 @@ import { | ||
| 167 | 167 | flashHighlight, |
| 168 | 168 | isTrueBoolean, |
| 169 | 169 | toggleDrawer, |
| 170 | + isElementInViewport, | |
| 170 | 171 | } from './scripts/utils.js'; |
| 171 | 172 | import { debounce_timeout } from './scripts/constants.js'; |
| 172 | 173 | |
| @@ -1827,10 +1828,10 @@ export async function replaceCurrentChat() { | ||
| 1827 | 1828 | } |
| 1828 | 1829 | } |
| 1829 | 1830 | |
| 1830 | 1831 | export function showMoreMessages(messagesToLoad = null) { |
| 1831 | 1832 | const firstDisplayedMesId = $('#chat').children('.mes').first().attr('mesid'); |
| 1832 | 1833 | let messageId = Number(firstDisplayedMesId); |
| 1833 | 1834 | let count = messagesToLoad || power_user.chat_truncation || Number.MAX_SAFE_INTEGER; |
| 1834 | 1835 | |
| 1835 | 1836 | // If there are no messages displayed, or the message somehow has no mesid, we default to one higher than last message id, |
| 1836 | 1837 | // so the first "new" message being shown will be the last available message |
| @@ -1840,6 +1841,7 @@ export function showMoreMessages() { | ||
| 1840 | 1841 | |
| 1841 | 1842 | console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length); |
| 1842 | 1843 | const prevHeight = $('#chat').prop('scrollHeight'); |
| 1844 | + const isButtonInView = isElementInViewport($('#show_more_messages')[0]); | |
| 1843 | 1845 | |
| 1844 | 1846 | while (messageId > 0 && count > 0) { |
| 1845 | 1847 | let newMessageId = messageId - 1; |
| @@ -1852,9 +1854,11 @@ export function showMoreMessages() { | ||
| 1852 | 1854 | $('#show_more_messages').remove(); |
| 1853 | 1855 | } |
| 1854 | 1856 | |
| 1857 | + if (isButtonInView) { | |
| 1855 | 1858 | const newHeight = $('#chat').prop('scrollHeight'); |
| 1856 | 1859 | $('#chat').scrollTop(newHeight - prevHeight); |
| 1857 | 1860 | } |
| 1861 | +} | |
| 1858 | 1862 | |
| 1859 | 1863 | export async function printMessages() { |
| 1860 | 1864 | let startIndex = 0; |
| @@ -39,6 +39,7 @@ import { | ||
| 39 | 39 | setCharacterName, |
| 40 | 40 | setExtensionPrompt, |
| 41 | 41 | setUserName, |
| 42 | + showMoreMessages, | |
| 42 | 43 | stopGeneration, |
| 43 | 44 | substituteParams, |
| 44 | 45 | system_avatar, |
| @@ -1964,6 +1965,27 @@ export function initDefaultSlashCommands() { | ||
| 1964 | 1965 | returns: ARGUMENT_TYPE.BOOLEAN, |
| 1965 | 1966 | helpString: 'Returns true if the current device is a mobile device, false otherwise. Equivalent to <code>{{isMobile}}</code> macro.', |
| 1966 | 1967 | })); |
| 1968 | + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | |
| 1969 | + name: 'chat-render', | |
| 1970 | + helpString: 'Renders a specified number of messages into the chat window. Displays all messages if no argument is provided.', | |
| 1971 | + callback: (_, number) => { | |
| 1972 | + showMoreMessages(number && !isNaN(Number(number)) ? Number(number) : Number.MAX_SAFE_INTEGER); | |
| 1973 | + return ''; | |
| 1974 | + }, | |
| 1975 | + unnamedArgumentList: [ | |
| 1976 | + new SlashCommandArgument( | |
| 1977 | + 'number of messages', [ARGUMENT_TYPE.NUMBER], false, | |
| 1978 | + ), | |
| 1979 | + ], | |
| 1980 | + })); | |
| 1981 | + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | |
| 1982 | + name: 'chat-reload', | |
| 1983 | + helpString: 'Reloads the current chat.', | |
| 1984 | + callback: async () => { | |
| 1985 | + await reloadCurrentChat(); | |
| 1986 | + return ''; | |
| 1987 | + }, | |
| 1988 | + })); | |
| 1967 | 1989 | |
| 1968 | 1990 | registerVariableCommands(); |
| 1969 | 1991 | } |