From b39b7998ce5cfed8e5945d0bd29e3851eb9a205a Mon Sep 17 00:00:00 2001 From: awaae001 <3271436144@qq.com> Date: Wed, 23 Apr 2025 01:34:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(slash-commands):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=AE=9A=E4=BD=8D=E5=92=8C=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/scripts/slash-commands.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 1ba7a3a5f..6ac0e445c 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2161,14 +2161,22 @@ export function initDefaultSlashCommands() { // --- End of loading step --- const messageElement = document.querySelector(`[mesid="${floorIndex}"]`); + const chatContainer = document.getElementById('chat'); - if (messageElement) { - // --- Corrected: Use the actual class from the template --- + if (messageElement && chatContainer) { const headerElement = messageElement.querySelector('.ch_name'); const elementToScroll = headerElement || messageElement; // Fallback to the entire message div - const blockPosition = headerElement ? 'center' : 'start'; - elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition }); + // Calculate position relative to chat container + const elementRect = elementToScroll.getBoundingClientRect(); + const containerRect = chatContainer.getBoundingClientRect(); + const scrollPosition = elementRect.top - containerRect.top + chatContainer.scrollTop; + + const viewportOffset = chatContainer.clientHeight * 0.1; // 25% from top + chatContainer.scrollTo({ + top: scrollPosition - viewportOffset, + behavior: 'smooth', + }); // Highlight the message element if (messageElement instanceof HTMLElement) { @@ -2183,12 +2191,9 @@ export function initDefaultSlashCommands() { messageElement.style.backgroundColor = ''; // Remove highlight }, 1500); // Match flash duration } - } else { console.warn('Message element is not an HTMLElement, cannot flash highlight.'); } - - } else { // Only warn if element is not found *after* attempting to load all messages 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.`);