Add /chat-render and /chat-reload commands

0a03793d7b59d5e9e306422c4a7bf110483a6efc

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

2 files changed, +30 -4Ignore whitespace
public/script.js+8 -4
@@ -167,6 +167,7 @@ import {
167 flashHighlight,167 flashHighlight,
168 isTrueBoolean,168 isTrueBoolean,
169 toggleDrawer,169 toggleDrawer,
170 isElementInViewport,
170} from './scripts/utils.js';171} from './scripts/utils.js';
171import { debounce_timeout } from './scripts/constants.js';172import { debounce_timeout } from './scripts/constants.js';
172173
@@ -1827,10 +1828,10 @@ export async function replaceCurrentChat() {
1827 }1828 }
1828}1829}
18291830
1830export function showMoreMessages() {1831export function showMoreMessages(messagesToLoad = null) {
1831 const firstDisplayedMesId = $('#chat').children('.mes').first().attr('mesid');1832 const firstDisplayedMesId = $('#chat').children('.mes').first().attr('mesid');
1832 let messageId = Number(firstDisplayedMesId);1833 let messageId = Number(firstDisplayedMesId);
1833 let count = power_user.chat_truncation || Number.MAX_SAFE_INTEGER;1834 let count = messagesToLoad || power_user.chat_truncation || Number.MAX_SAFE_INTEGER;
18341835
1835 // If there are no messages displayed, or the message somehow has no mesid, we default to one higher than last message id,1836 // If there are no messages displayed, or the message somehow has no mesid, we default to one higher than last message id,
1836 // so the first "new" message being shown will be the last available message1837 // so the first "new" message being shown will be the last available message
@@ -1840,6 +1841,7 @@ export function showMoreMessages() {
18401841
1841 console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length);1842 console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length);
1842 const prevHeight = $('#chat').prop('scrollHeight');1843 const prevHeight = $('#chat').prop('scrollHeight');
1844 const isButtonInView = isElementInViewport($('#show_more_messages')[0]);
18431845
1844 while (messageId > 0 && count > 0) {1846 while (messageId > 0 && count > 0) {
1845 let newMessageId = messageId - 1;1847 let newMessageId = messageId - 1;
@@ -1852,8 +1854,10 @@ export function showMoreMessages() {
1852 $('#show_more_messages').remove();1854 $('#show_more_messages').remove();
1853 }1855 }
18541856
1855 const newHeight = $('#chat').prop('scrollHeight');1857 if (isButtonInView) {
1856 $('#chat').scrollTop(newHeight - prevHeight);1858 const newHeight = $('#chat').prop('scrollHeight');
1859 $('#chat').scrollTop(newHeight - prevHeight);
1860 }
1857}1861}
18581862
1859export async function printMessages() {1863export async function printMessages() {
public/scripts/slash-commands.js+22 -0
@@ -39,6 +39,7 @@ import {
39 setCharacterName,39 setCharacterName,
40 setExtensionPrompt,40 setExtensionPrompt,
41 setUserName,41 setUserName,
42 showMoreMessages,
42 stopGeneration,43 stopGeneration,
43 substituteParams,44 substituteParams,
44 system_avatar,45 system_avatar,
@@ -1964,6 +1965,27 @@ export function initDefaultSlashCommands() {
1964 returns: ARGUMENT_TYPE.BOOLEAN,1965 returns: ARGUMENT_TYPE.BOOLEAN,
1965 helpString: 'Returns true if the current device is a mobile device, false otherwise. Equivalent to <code>{{isMobile}}</code> macro.',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 }));
19671989
1968 registerVariableCommands();1990 registerVariableCommands();
1969}1991}