feat(slash-commands): 优化 /goto-floor 命令并加载所有消息

- 在执行 /goto-floor 命令前加载所有消息,确保目标元素存在
- 添加加载消息的步骤,解决因懒加载导致的元素找不到问题
This commit is contained in:
awaae001
2025-04-23 00:45:05 +08:00
parent 6d0318eb36
commit ceeaeea123

View File

@ -2134,6 +2134,13 @@ export function initDefaultSlashCommands() {
name: 'goto-floor',
aliases: ['floor', 'jump', 'scrollto'],
callback: async (_, index) => {
// --- Load all messages first to ensure the target element exists ---
console.log(`INFO: Loading all messages before attempting to goto-floor ${index}.`);
await showMoreMessages(Number.MAX_SAFE_INTEGER);
console.log(`INFO: All messages loaded (or loading initiated).`);
// --- End of loading step ---
const floorIndex = Number(index);
// Validate input
@ -2144,6 +2151,10 @@ export function initDefaultSlashCommands() {
return '';
}
// Give the rendering a moment to potentially catch up after showMoreMessages
// This might be necessary depending on how showMoreMessages works internally
await new Promise(resolve => setTimeout(resolve, 100)); // Adjust delay if needed
const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);
if (messageElement) {
@ -2171,8 +2182,10 @@ export function initDefaultSlashCommands() {
}, 1500); // Start fade out after 1.5 seconds
} else {
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.`);
console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /goto-floor.`);
// This case is less likely now after showMoreMessages, but still possible
// if the element hasn't been added to the DOM yet for some reason after loading.
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.`);
console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /goto-floor, even after attempting to load all messages.`);
const chatContainer = document.getElementById('chat');
if (chatContainer) {
chatContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
@ -2192,13 +2205,11 @@ export function initDefaultSlashCommands() {
<div>
Scrolls the chat view to the specified message index. Uses the <code>[mesid]</code> attribute for locating the message element. Index starts at 0.
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.
Automatically attempts to load all messages before scrolling to improve success rate, addressing issues with lazy loading.
</div>
<div>
<strong>Example:</strong> <pre><code>/goto-floor 10</code></pre> Scrolls to the 11th message (mesid=10).
</div>
<div>
Note: Due to virtual scrolling, very old messages might need loading first.
</div>
`,
}));