refactor(slash-commands): 重命名 goto-floor 命令为 chat-jump

- 将命令名称从 'goto-floor' 修改为 'chat-jump',以更好地反映其功能
This commit is contained in:
awaae001
2025-04-23 01:12:57 +08:00
parent ee11f021eb
commit 59ebf2e5b8

View File

@ -2132,25 +2132,25 @@ export function initDefaultSlashCommands() {
})); }));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'goto-floor', name: 'chat-jump',
aliases: ['floor', 'jump', 'scrollto'], aliases: ['floor', 'jump', 'scrollto'],
callback: async (_, index) => { callback: async (_, index) => {
const floorIndex = Number(index); const floorIndex = Number(index);
const chatLength = typeof chat !== 'undefined' ? chat.length : -1; // Use -1 if chat is undefined to avoid errors const chatLength = typeof chat !== 'undefined' ? chat.length : -1; // Use -1 if chat is undefined to avoid errors
if (isNaN(floorIndex) || floorIndex < 0 || (chatLength !== -1 && floorIndex >= chatLength)) { if (isNaN(floorIndex) || floorIndex < 0 || (chatLength !== -1 && floorIndex >= chatLength)) {
const maxIndex = (chatLength !== -1 ? chatLength - 1 : 'unknown'); const maxIndex = (chatLength !== -1 ? chatLength - 1 : 'unknown');
toastr.warning(`Invalid message index: ${index}. Please enter a number between 0 and ${maxIndex}.`); toastr.warning(`Invalid message index: ${index}. Please enter a number between 0 and ${maxIndex}.`);
console.warn(`WARN: Invalid message index provided for /goto-floor: ${index}. Max index: ${maxIndex}`); console.warn(`WARN: Invalid message index provided for /chat-jump: ${index}. Max index: ${maxIndex}`);
return ''; return '';
} }
// --- Load all messages first to ensure the target element exists --- // --- Load all messages first to ensure the target element exists ---
console.log(`INFO: Attempting to load all messages before attempting to goto-floor ${index}.`); console.log(`INFO: Attempting to load all messages before attempting to chat-jump ${index}.`);
try { try {
// Assuming showMoreMessages is available globally or within scope // Assuming showMoreMessages is available globally or within scope
await showMoreMessages(Number.MAX_SAFE_INTEGER); await showMoreMessages(Number.MAX_SAFE_INTEGER);
console.log(`INFO: All messages loaded (or loading initiated).`); console.log('INFO: All messages loaded (or loading initiated).');
// Give the rendering a moment to potentially catch up after showMoreMessages // Give the rendering a moment to potentially catch up after showMoreMessages
await new Promise(resolve => setTimeout(resolve, 100)); // Adjust delay if needed await new Promise(resolve => setTimeout(resolve, 100)); // Adjust delay if needed
} catch (error) { } catch (error) {
@ -2159,40 +2159,40 @@ export function initDefaultSlashCommands() {
return ''; // Exit if loading fails return ''; // Exit if loading fails
} }
// --- End of loading step --- // --- End of loading step ---
const messageElement = document.querySelector(`[mesid="${floorIndex}"]`); const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);
if (messageElement) { if (messageElement) {
// --- Corrected: Use the actual class from the template --- // --- Corrected: Use the actual class from the template ---
const headerElement = messageElement.querySelector('.ch_name'); const headerElement = messageElement.querySelector('.ch_name');
const elementToScroll = headerElement || messageElement; // Fallback to the entire message div const elementToScroll = headerElement || messageElement; // Fallback to the entire message div
const blockPosition = headerElement ? 'center' : 'start'; const blockPosition = headerElement ? 'center' : 'start';
elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition }); elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition });
// Highlight the message element // Highlight the message element
if (messageElement instanceof HTMLElement) { if (messageElement instanceof HTMLElement) {
if (typeof $ !== 'undefined') { // Check if jQuery is available if (typeof $ !== 'undefined') { // Check if jQuery is available
flashHighlight($(messageElement), 1500); flashHighlight($(messageElement), 1500);
} else { } else {
console.warn('jQuery not available, cannot use flashHighlight.'); console.warn('jQuery not available, cannot use flashHighlight.');
// Optional: Add a temporary CSS class highlight if jQuery/flashHighlight is missing // Optional: Add a temporary CSS class highlight if jQuery/flashHighlight is missing
messageElement.style.transition = 'background-color 0.5s ease'; messageElement.style.transition = 'background-color 0.5s ease';
messageElement.style.backgroundColor = 'yellow'; // Or some highlight color messageElement.style.backgroundColor = 'yellow'; // Or some highlight color
setTimeout(() => { setTimeout(() => {
messageElement.style.backgroundColor = ''; // Remove highlight messageElement.style.backgroundColor = ''; // Remove highlight
}, 1500); // Match flash duration }, 1500); // Match flash duration
} }
} else { } else {
console.warn('Message element is not an HTMLElement, cannot flash highlight.'); console.warn('Message element is not an HTMLElement, cannot flash highlight.');
} }
} else { } else {
// Only warn if element is not found *after* attempting to load all messages // 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.`); 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.`);
console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /goto-floor, even after attempting to load all messages.`); console.warn(`WARN: Element not found for message index ${floorIndex} using querySelector [mesid="${floorIndex}"] in /chat-jump, even after attempting to load all messages.`);
// Do NOT scroll the chat container in this case // Do NOT scroll the chat container in this case
} }
return ''; // Return empty string as expected by some slash command parsers return ''; // Return empty string as expected by some slash command parsers
@ -2213,12 +2213,12 @@ export function initDefaultSlashCommands() {
A warning is displayed if the message element cannot be located even after attempting to load all messages. A warning is displayed if the message element cannot be located even after attempting to load all messages.
</div> </div>
<div> <div>
<strong>Example:</strong> <pre><code>/goto-floor 10</code></pre> Scrolls to the 11th message (mesid=10). <strong>Example:</strong> <pre><code>/chat-jump 10</code></pre> Scrolls to the 11th message (mesid=10).
</div> </div>
`, `,
})); }));
const styleId = 'goto-floor-highlight-style'; const styleId = 'chat-jump-highlight-style';
if (document.getElementById(styleId)) { if (document.getElementById(styleId)) {
document.getElementById(styleId).remove(); document.getElementById(styleId).remove();
} }