Code clean-up

870abe077675f30e3546a9c4eaa6a4b68b1c234e

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

1 files changed, +24 -61Showing whitespace changes
public/scripts/slash-commands.js+24 -61
@@ -22,6 +22,7 @@ import {
2222 extractMessageBias,
2323 generateQuietPrompt,
2424 generateRaw,
25+ getFirstDisplayedMessageId,
2526 getThumbnailUrl,
2627 is_send_press,
2728 main_api,
@@ -2133,74 +2134,44 @@ export function initDefaultSlashCommands() {
21332134
21342135 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
21352136 name: 'chat-jump',
21362137 aliases: ['floor', 'jumpchat-scrollto', 'scrolltofloor-teleport'],
21372138 callback: async (_, index) => {
21382139 const floorIndexmessageIndex = Number(index);
21392140
2140- const chatLength = typeof chat !== 'undefined' ? chat.length : -1; // Use -1 if chat is undefined to avoid errors
2141+ if (isNaN(messageIndex) || messageIndex < 0 || messageIndex >= chat.length) {
2141- if (isNaN(floorIndex) || floorIndex < 0 || (chatLength !== -1 && floorIndex >= chatLength)) {
2142+ toastr.warning(t`Invalid message index: ${index}. Please enter a number between 0 and ${chat.length}.`);
2142- const maxIndex = (chatLength !== -1 ? chatLength - 1 : 'unknown');
2143+ console.warn(`WARN: Invalid message index provided for /chat-jump: ${index}. Max index: ${chat.length}`);
2143- toastr.warning(`Invalid message index: ${index}. Please enter a number between 0 and ${maxIndex}.`);
2144- console.warn(`WARN: Invalid message index provided for /chat-jump: ${index}. Max index: ${maxIndex}`);
21452144 return '';
21462145 }
21472146
2148- // --- Load all messages first to ensure the target element exists ---
2147+ // Load more messages if needed
2149- console.log(`INFO: Attempting to load all messages before attempting to chat-jump ${index}.`);
2148+ const firstDisplayedMessageId = getFirstDisplayedMessageId();
2150- try {
2149+ if (isFinite(firstDisplayedMessageId) && messageIndex < firstDisplayedMessageId) {
2151- // Assuming showMoreMessages is available globally or within scope
2150+ const needToLoadCount = firstDisplayedMessageId - messageIndex;
21522151 await showMoreMessages(Number.MAX_SAFE_INTEGERneedToLoadCount);
2153- console.log('INFO: All messages loaded (or loading initiated).');
2152+ await delay(1);
2154- // Give the rendering a moment to potentially catch up after showMoreMessages
2155- await new Promise(resolve => setTimeout(resolve, 100)); // Adjust delay if needed
2156- } catch (error) {
2157- console.error('Error loading messages:', error);
2158- toastr.error('An error occurred while trying to load messages.');
2159- return ''; // Exit if loading fails
21602153 }
2161- // --- End of loading step ---
21622154
2163- const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);
21642155 const chatContainer = document.getElementById('chat');
2156+ const messageElement = document.querySelector(`#chat .mes[mesid="${messageIndex}"]`);
21652157
21662158 if (messageElement instanceof HTMLElement && chatContainer instanceof HTMLElement) {
21672159 const headerElementelementRect = messageElement.querySelectorgetBoundingClientRect('.ch_name');
2168- const elementToScroll = headerElement || messageElement; // Fallback to the entire message div
2169-
2170- // Calculate position relative to chat container
2171- const elementRect = elementToScroll.getBoundingClientRect();
21722160 const containerRect = chatContainer.getBoundingClientRect();
2173- const scrollPosition = elementRect.top - containerRect.top + chatContainer.scrollTop;
21742161
2175- const viewportOffset = chatContainer.clientHeight * 0.1; // 25% from top
2162+ const scrollPosition = elementRect.top - containerRect.top + chatContainer.scrollTop;
21762163 chatContainer.scrollTo({
21772164 top: scrollPosition - viewportOffset,
21782165 behavior: 'smooth',
21792166 });
21802167
2181- // Highlight the message element
2168+ flashHighlight($(messageElement), 2000);
2182- if (messageElement instanceof HTMLElement) {
2183- if (typeof $ !== 'undefined') { // Check if jQuery is available
2184- flashHighlight($(messageElement), 1500);
2185- } else {
2186- console.warn('jQuery not available, cannot use flashHighlight.');
2187- // Optional: Add a temporary CSS class highlight if jQuery/flashHighlight is missing
2188- messageElement.style.transition = 'background-color 0.5s ease';
2189- messageElement.style.backgroundColor = 'yellow'; // Or some highlight color
2190- setTimeout(() => {
2191- messageElement.style.backgroundColor = ''; // Remove highlight
2192- }, 1500); // Match flash duration
2193- }
2194- } else {
2195- console.warn('Message element is not an HTMLElement, cannot flash highlight.');
2196- }
21972169 } else {
2198- // Only warn if element is not found *after* attempting to load all messages
2170+ toastr.warning(t`Could not find element for message ${messageIndex}. It might not be rendered yet or the index is invalid.`);
2199- toastr.warning(`Could not find element for message ${floorIndex} (using [mesid="${floorIndex}"]) even after attempting to load all messages. It might not be rendered yet or the index is invalid.`);
2171+ console.warn(`WARN: Element not found for message index ${messageIndex} in /chat-jump.`);
2200- console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /chat-jump, even after attempting to load all messages.`);
2201- // Do NOT scroll the chat container in this case
22022172 }
2203- return ''; // Return empty string as expected by some slash command parsers
2173+
2174+ return '';
22042175 },
22052176 unnamedArgumentList: [
22062177 SlashCommandArgument.fromProps({
@@ -2212,22 +2183,14 @@ export function initDefaultSlashCommands() {
22122183 ],
22132184 helpString: `
22142185 <div>
22152186 Scrolls the chat view to the specified message index. Uses the <code>[mesid]</code> attribute for locating the message element. Index starts at 0.
2216- It attempts to center the character's name/header area within the message block by targeting the <code>.ch_name</code> element. Highlights the message using a flash animation.
2217- Automatically attempts to load all messages before scrolling to improve success rate, addressing issues with lazy loading.
2218- A warning is displayed if the message element cannot be located even after attempting to load all messages.
22192187 </div>
22202188 <div>
22212189 <strong>Example:</strong> <pre><code>/chat-jump 10</code></pre> Scrolls to the 11th message (mesidid=10).
22222190 </div>
22232191 `,
22242192 }));
22252193
2226- const styleId = 'chat-jump-highlight-style';
2227- if (document.getElementById(styleId)) {
2228- document.getElementById(styleId).remove();
2229- }
2230-
22312194 registerVariableCommands();
22322195}
22332196