Add /chat-render and /chat-reload commands

0a03793d7b59d5e9e306422c4a7bf110483a6efc

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

2 files changed, +28 -2Showing whitespace changes
public/script.js+6 -2
@@ -167,6 +167,7 @@ import {
167167 flashHighlight,
168168 isTrueBoolean,
169169 toggleDrawer,
170+ isElementInViewport,
170171} from './scripts/utils.js';
171172import { debounce_timeout } from './scripts/constants.js';
172173
@@ -1827,10 +1828,10 @@ export async function replaceCurrentChat() {
18271828 }
18281829}
18291830
18301831export function showMoreMessages(messagesToLoad = null) {
18311832 const firstDisplayedMesId = $('#chat').children('.mes').first().attr('mesid');
18321833 let messageId = Number(firstDisplayedMesId);
18331834 let count = messagesToLoad || power_user.chat_truncation || Number.MAX_SAFE_INTEGER;
18341835
18351836 // If there are no messages displayed, or the message somehow has no mesid, we default to one higher than last message id,
18361837 // so the first "new" message being shown will be the last available message
@@ -1840,6 +1841,7 @@ export function showMoreMessages() {
18401841
18411842 console.debug('Inserting messages before', messageId, 'count', count, 'chat length', chat.length);
18421843 const prevHeight = $('#chat').prop('scrollHeight');
1844+ const isButtonInView = isElementInViewport($('#show_more_messages')[0]);
18431845
18441846 while (messageId > 0 && count > 0) {
18451847 let newMessageId = messageId - 1;
@@ -1852,9 +1854,11 @@ export function showMoreMessages() {
18521854 $('#show_more_messages').remove();
18531855 }
18541856
1857+ if (isButtonInView) {
18551858 const newHeight = $('#chat').prop('scrollHeight');
18561859 $('#chat').scrollTop(newHeight - prevHeight);
18571860 }
1861+}
18581862
18591863export async function printMessages() {
18601864 let startIndex = 0;
public/scripts/slash-commands.js+22 -0
@@ -39,6 +39,7 @@ import {
3939 setCharacterName,
4040 setExtensionPrompt,
4141 setUserName,
42+ showMoreMessages,
4243 stopGeneration,
4344 substituteParams,
4445 system_avatar,
@@ -1964,6 +1965,27 @@ export function initDefaultSlashCommands() {
19641965 returns: ARGUMENT_TYPE.BOOLEAN,
19651966 helpString: 'Returns true if the current device is a mobile device, false otherwise. Equivalent to <code>{{isMobile}}</code> macro.',
19661967 }));
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
19681990 registerVariableCommands();
19691991}