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

ceeaeea1237edfff3455a329a00171455a82ac31

awaae001 <3271436144@qq.com>

1 files changed, +16 -5Showing whitespace changes
public/scripts/slash-commands.js+16 -5
@@ -2134,6 +2134,13 @@ 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);
21382145
21392146 // Validate input
@@ -2144,6 +2151,10 @@ export function initDefaultSlashCommands() {
21442151 return '';
21452152 }
21462153
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}"]`);
21482159
21492160 if (messageElement) {
@@ -2171,8 +2182,10 @@ export function initDefaultSlashCommands() {
21712182 }, 1500); // Start fade out after 1.5 seconds
21722183
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,13 +2205,11 @@ 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 }));
22042215