refactor(slash-commands): 优化消息定位和滚动效果

b39b7998ce5cfed8e5945d0bd29e3851eb9a205a

awaae001 <3271436144@qq.com>

1 files changed, +12 -7Ignore whitespace
public/scripts/slash-commands.js+12 -7
@@ -2161,14 +2161,22 @@ export function initDefaultSlashCommands() {
2161 // --- End of loading step ---2161 // --- End of loading step ---
21622162
2163 const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);2163 const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);
2164 const chatContainer = document.getElementById('chat');
21642165
2165 if (messageElement) {2166 if (messageElement && chatContainer) {
2166 // --- Corrected: Use the actual class from the template ---
2167 const headerElement = messageElement.querySelector('.ch_name');2167 const headerElement = messageElement.querySelector('.ch_name');
2168 const elementToScroll = headerElement || messageElement; // Fallback to the entire message div2168 const elementToScroll = headerElement || messageElement; // Fallback to the entire message div
2169 const blockPosition = headerElement ? 'center' : 'start';
21702169
2171 elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition });2170 // Calculate position relative to chat container
2171 const elementRect = elementToScroll.getBoundingClientRect();
2172 const containerRect = chatContainer.getBoundingClientRect();
2173 const scrollPosition = elementRect.top - containerRect.top + chatContainer.scrollTop;
2174
2175 const viewportOffset = chatContainer.clientHeight * 0.1; // 25% from top
2176 chatContainer.scrollTo({
2177 top: scrollPosition - viewportOffset,
2178 behavior: 'smooth',
2179 });
21722180
2173 // Highlight the message element2181 // Highlight the message element
2174 if (messageElement instanceof HTMLElement) {2182 if (messageElement instanceof HTMLElement) {
@@ -2183,12 +2191,9 @@ export function initDefaultSlashCommands() {
2183 messageElement.style.backgroundColor = ''; // Remove highlight2191 messageElement.style.backgroundColor = ''; // Remove highlight
2184 }, 1500); // Match flash duration2192 }, 1500); // Match flash duration
2185 }2193 }
2186
2187 } else {2194 } else {
2188 console.warn('Message element is not an HTMLElement, cannot flash highlight.');2195 console.warn('Message element is not an HTMLElement, cannot flash highlight.');
2189 }2196 }
2190
2191
2192 } else {2197 } else {
2193 // Only warn if element is not found *after* attempting to load all messages2198 // Only warn if element is not found *after* attempting to load all messages
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.`);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.`);