refactor(slash-commands): 重命名 goto-floor 命令为 chat-jump - 将命令名称从 'goto-floor' 修改为 'chat-jump',以更好地反映其功能
| @@ -2132,25 +2132,25 @@ export function initDefaultSlashCommands() { | ||
| 2132 | 2132 | })); |
| 2133 | 2133 | |
| 2134 | 2134 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 2135 | 2135 | name: 'gotochat-floorjump', |
| 2136 | 2136 | aliases: ['floor', 'jump', 'scrollto'], |
| 2137 | 2137 | callback: async (_, index) => { |
| 2138 | 2138 | const floorIndex = Number(index); |
| 2139 | - | |
| 2139 | + | |
| 2140 | 2140 | const chatLength = typeof chat !== 'undefined' ? chat.length : -1; // Use -1 if chat is undefined to avoid errors |
| 2141 | 2141 | if (isNaN(floorIndex) || floorIndex < 0 || (chatLength !== -1 && floorIndex >= chatLength)) { |
| 2142 | 2142 | const maxIndex = (chatLength !== -1 ? chatLength - 1 : 'unknown'); |
| 2143 | 2143 | toastr.warning(`Invalid message index: ${index}. Please enter a number between 0 and ${maxIndex}.`); |
| 2144 | 2144 | console.warn(`WARN: Invalid message index provided for /gotochat-floorjump: ${index}. Max index: ${maxIndex}`); |
| 2145 | 2145 | return ''; |
| 2146 | 2146 | } |
| 2147 | - | |
| 2147 | + | |
| 2148 | 2148 | // --- Load all messages first to ensure the target element exists --- |
| 2149 | 2149 | console.log(`INFO: Attempting to load all messages before attempting to gotochat-floorjump ${index}.`); |
| 2150 | 2150 | try { |
| 2151 | 2151 | // Assuming showMoreMessages is available globally or within scope |
| 2152 | 2152 | await showMoreMessages(Number.MAX_SAFE_INTEGER); |
| 2153 | 2153 | console.log(`'INFO: All messages loaded (or loading initiated).`'); |
| 2154 | 2154 | // Give the rendering a moment to potentially catch up after showMoreMessages |
| 2155 | 2155 | await new Promise(resolve => setTimeout(resolve, 100)); // Adjust delay if needed |
| 2156 | 2156 | } catch (error) { |
| @@ -2159,40 +2159,40 @@ export function initDefaultSlashCommands() { | ||
| 2159 | 2159 | return ''; // Exit if loading fails |
| 2160 | 2160 | } |
| 2161 | 2161 | // --- End of loading step --- |
| 2162 | - | |
| 2162 | + | |
| 2163 | 2163 | const messageElement = document.querySelector(`[mesid="${floorIndex}"]`); |
| 2164 | - | |
| 2164 | + | |
| 2165 | 2165 | if (messageElement) { |
| 2166 | 2166 | // --- Corrected: Use the actual class from the template --- |
| 2167 | 2167 | const headerElement = messageElement.querySelector('.ch_name'); |
| 2168 | 2168 | const elementToScroll = headerElement || messageElement; // Fallback to the entire message div |
| 2169 | 2169 | const blockPosition = headerElement ? 'center' : 'start'; |
| 2170 | - | |
| 2170 | + | |
| 2171 | 2171 | elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition }); |
| 2172 | - | |
| 2172 | + | |
| 2173 | 2173 | // Highlight the message element |
| 2174 | 2174 | if (messageElement instanceof HTMLElement) { |
| 2175 | 2175 | if (typeof $ !== 'undefined') { // Check if jQuery is available |
| 2176 | 2176 | flashHighlight($(messageElement), 1500); |
| 2177 | 2177 | } else { |
| 2178 | 2178 | console.warn('jQuery not available, cannot use flashHighlight.'); |
| 2179 | 2179 | // Optional: Add a temporary CSS class highlight if jQuery/flashHighlight is missing |
| 2180 | 2180 | messageElement.style.transition = 'background-color 0.5s ease'; |
| 2181 | 2181 | messageElement.style.backgroundColor = 'yellow'; // Or some highlight color |
| 2182 | 2182 | setTimeout(() => { |
| 2183 | 2183 | messageElement.style.backgroundColor = ''; // Remove highlight |
| 2184 | 2184 | }, 1500); // Match flash duration |
| 2185 | 2185 | } |
| 2186 | - | |
| 2186 | + | |
| 2187 | 2187 | } else { |
| 2188 | 2188 | console.warn('Message element is not an HTMLElement, cannot flash highlight.'); |
| 2189 | 2189 | } |
| 2190 | - | |
| 2190 | + | |
| 2191 | - | |
| 2191 | + | |
| 2192 | 2192 | } else { |
| 2193 | 2193 | // Only warn if element is not found *after* attempting to load all messages |
| 2194 | 2194 | 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.`); |
| 2195 | 2195 | console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /gotochat-floorjump, even after attempting to load all messages.`); |
| 2196 | 2196 | // Do NOT scroll the chat container in this case |
| 2197 | 2197 | } |
| 2198 | 2198 | return ''; // Return empty string as expected by some slash command parsers |
| @@ -2213,12 +2213,12 @@ export function initDefaultSlashCommands() { | ||
| 2213 | 2213 | A warning is displayed if the message element cannot be located even after attempting to load all messages. |
| 2214 | 2214 | </div> |
| 2215 | 2215 | <div> |
| 2216 | 2216 | <strong>Example:</strong> <pre><code>/gotochat-floorjump 10</code></pre> Scrolls to the 11th message (mesid=10). |
| 2217 | 2217 | </div> |
| 2218 | 2218 | `, |
| 2219 | 2219 | })); |
| 2220 | - | |
| 2220 | + | |
| 2221 | 2221 | const styleId = 'gotochat-floorjump-highlight-style'; |
| 2222 | 2222 | if (document.getElementById(styleId)) { |
| 2223 | 2223 | document.getElementById(styleId).remove(); |
| 2224 | 2224 | } |