feat(slash-commands): 优化 /goto-floor 命令并加载所有消息 - 在执行 /goto-floor 命令前加载所有消息,确保目标元素存在 - 添加加载消息的步骤,解决因懒加载导致的元素找不到问题

ceeaeea1237edfff3455a329a00171455a82ac31

awaae001 <3271436144@qq.com>

1 files changed, +24 -13Ignore whitespace
public/scripts/slash-commands.js+24 -13
@@ -2134,8 +2134,15 @@ export function initDefaultSlashCommands() {
21342134 name: 'goto-floor',
21352135 aliases: ['floor', 'jump', 'scrollto'],
21362136 callback: async (_, index) => {
2137+ // --- Load all messages first to ensure the target element exists ---
2138+ console.log(`INFO: Loading all messages before attempting to goto-floor ${index}.`);
2139+ await showMoreMessages(Number.MAX_SAFE_INTEGER);
2140+ console.log(`INFO: All messages loaded (or loading initiated).`);
2141+ // --- End of loading step ---
2142+
2143+
21372144 const floorIndex = Number(index);
2138-
2145+
21392146 // Validate input
21402147 if (isNaN(floorIndex) || floorIndex < 0 || (typeof chat !== 'undefined' && floorIndex >= chat.length)) {
21412148 const maxIndex = (typeof chat !== 'undefined' ? chat.length - 1 : 'unknown');
@@ -2143,21 +2150,25 @@ export function initDefaultSlashCommands() {
21432150 console.warn(`WARN: Invalid message index provided for /goto-floor: ${index}. Max index: ${maxIndex}`);
21442151 return '';
21452152 }
2146-
2153+
2154+ // Give the rendering a moment to potentially catch up after showMoreMessages
2155+ // This might be necessary depending on how showMoreMessages works internally
2156+ await new Promise(resolve => setTimeout(resolve, 100)); // Adjust delay if needed
2157+
21472158 const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);
2148-
2159+
21492160 if (messageElement) {
21502161 const headerElement = messageElement.querySelector('.mes_header') ||
21512162 messageElement.querySelector('.mes_meta') ||
21522163 messageElement.querySelector('.mes_name_area') ||
21532164 messageElement.querySelector('.mes_name');
2154-
2165+
21552166 const elementToScroll = headerElement || messageElement; // Prefer header, fallback to whole message
21562167 const blockPosition = headerElement ? 'center' : 'start'; // Center header, else start of message
2157-
2168+
21582169 elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition });
21592170 console.log(`INFO: Scrolled ${headerElement ? 'header of' : ''} message ${floorIndex} into view (block: ${blockPosition}).`);
2160-
2171+
21612172 // --- Highlight with smooth animation ---
21622173 messageElement.classList.add('highlight-scroll');
21632174 setTimeout(() => {
@@ -2169,10 +2180,12 @@ export function initDefaultSlashCommands() {
21692180 messageElement.classList.remove('highlight-scroll', 'highlight-scroll-fadeout');
21702181 }, 500); // Matches the 0.5s transition duration
21712182 }, 1500); // Start fade out after 1.5 seconds
2172-
2183+
21732184 } else {
2174- toastr.warning(`Could not find element for message ${floorIndex} (using [mesid="${floorIndex}"]). It might not be rendered yet. Try scrolling up or use /chat-render all.`);
2185+ // This case is less likely now after showMoreMessages, but still possible
2175- console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /goto-floor.`);
2186+ // if the element hasn't been added to the DOM yet for some reason after loading.
2187+ 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. Try scrolling up or use /chat-render all again if issues persist.`);
2188+ console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /goto-floor, even after attempting to load all messages.`);
21762189 const chatContainer = document.getElementById('chat');
21772190 if (chatContainer) {
21782191 chatContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
@@ -2192,16 +2205,14 @@ export function initDefaultSlashCommands() {
21922205 <div>
21932206 Scrolls the chat view to the specified message index. Uses the <code>[mesid]</code> attribute for locating the message element. Index starts at 0.
21942207 It attempts to center the character's name/header area within the message block. Highlights the message using the theme's 'matchedText' color with a smooth animation.
2208+ Automatically attempts to load all messages before scrolling to improve success rate, addressing issues with lazy loading.
21952209 </div>
21962210 <div>
21972211 <strong>Example:</strong> <pre><code>/goto-floor 10</code></pre> Scrolls to the 11th message (mesid=10).
21982212 </div>
2199- <div>
2200- Note: Due to virtual scrolling, very old messages might need loading first.
2201- </div>
22022213 `,
22032214 }));
2204-
2215+
22052216 // --- Improved CSS for highlight ---
22062217 const styleId = 'goto-floor-highlight-style';
22072218 if (!document.getElementById(styleId)) {