From c14c1417c156b82be849abae9a342644fd631b9b Mon Sep 17 00:00:00 2001 From: ARIA Date: Sun, 12 Jul 2026 12:24:21 +0200 Subject: [PATCH] Fixes #5: Refactor code quality - Part 1 - Split index.js from 1,567 lines to 456 lines (73% reduction) - Extracted settings event listeners to src/systems/ui/settingsListeners.js - Extracted settings panel to src/core/settingsPanel.js - Simplified initUI() and main initialization block - Modularized style.css into 28 logical CSS modules in src/styles/ - Added build script (npm run build:css) to concatenate modules - Modules: panel, components, user-stats, info-box, thoughts, settings, themes, modals, mobile, inventory, quests, equipment, encounters, weather, music-player, FAB widgets, strip widgets, etc. - Updated package.json with build:css script and type: module No functional changes - pure refactoring for maintainability. --- index.js | 1243 ++---------------------- package-lock.json | 183 +++- package.json | 6 +- scripts/build-css.js | 70 ++ src/core/settingsPanel.js | 72 ++ src/styles/_02_panel.css | 265 +++++ src/styles/_03_components.css | 318 ++++++ src/styles/_04_user-stats.css | 635 ++++++++++++ src/styles/_05_info-box.css | 740 ++++++++++++++ src/styles/_06_thoughts.css | 679 +++++++++++++ src/styles/_07_settings.css | 605 ++++++++++++ src/styles/_08_themes.css | 379 ++++++++ src/styles/_09_dice-modal.css | 425 +++++++++ src/styles/_10_tracker-editor.css | 739 ++++++++++++++ src/styles/_11_settings-modal.css | 246 +++++ src/styles/_12_thought-bubbles.css | 498 ++++++++++ src/styles/_13_mobile.css | 1259 ++++++++++++++++++++++++ src/styles/_14_inventory.css | 389 ++++++++ src/styles/_15_inventory-views.css | 173 ++++ src/styles/_16_desktop-tabs.css | 138 +++ src/styles/_17_quests.css | 391 ++++++++ src/styles/_18_equipment.css | 149 +++ src/styles/_19_encounters.css | 1379 +++++++++++++++++++++++++++ src/styles/_20_plot-buttons.css | 132 +++ src/styles/_21_music-player.css | 259 +++++ src/styles/_22_weather.css | 1056 ++++++++++++++++++++ src/styles/_23_holiday-promo.css | 35 + src/styles/_24_feature-toggles.css | 93 ++ src/styles/_25_import-dialog.css | 100 ++ src/styles/_26_fab-widgets.css | 332 +++++++ src/styles/_27_strip-widgets.css | 497 ++++++++++ src/styles/_28_equipment-modal.css | 379 ++++++++ src/styles/_variables.css | 26 + src/systems/ui/settingsListeners.js | 405 ++++++++ style.css | 197 +++- 35 files changed, 13307 insertions(+), 1185 deletions(-) create mode 100644 scripts/build-css.js create mode 100644 src/core/settingsPanel.js create mode 100644 src/styles/_02_panel.css create mode 100644 src/styles/_03_components.css create mode 100644 src/styles/_04_user-stats.css create mode 100644 src/styles/_05_info-box.css create mode 100644 src/styles/_06_thoughts.css create mode 100644 src/styles/_07_settings.css create mode 100644 src/styles/_08_themes.css create mode 100644 src/styles/_09_dice-modal.css create mode 100644 src/styles/_10_tracker-editor.css create mode 100644 src/styles/_11_settings-modal.css create mode 100644 src/styles/_12_thought-bubbles.css create mode 100644 src/styles/_13_mobile.css create mode 100644 src/styles/_14_inventory.css create mode 100644 src/styles/_15_inventory-views.css create mode 100644 src/styles/_16_desktop-tabs.css create mode 100644 src/styles/_17_quests.css create mode 100644 src/styles/_18_equipment.css create mode 100644 src/styles/_19_encounters.css create mode 100644 src/styles/_20_plot-buttons.css create mode 100644 src/styles/_21_music-player.css create mode 100644 src/styles/_22_weather.css create mode 100644 src/styles/_23_holiday-promo.css create mode 100644 src/styles/_24_feature-toggles.css create mode 100644 src/styles/_25_import-dialog.css create mode 100644 src/styles/_26_fab-widgets.css create mode 100644 src/styles/_27_strip-widgets.css create mode 100644 src/styles/_28_equipment-modal.css create mode 100644 src/styles/_variables.css create mode 100644 src/systems/ui/settingsListeners.js diff --git a/index.js b/index.js index dbc01b7..cab6073 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,11 @@ +/** + * RPG Companion - Main Entry Point + * SillyTavern extension for RPG-style character tracking. + * + * This file is a thin orchestrator that delegates to sub-modules. + * See src/ directory for the actual implementation. + */ + import { getContext, renderExtensionTemplateAsync, extension_settings as st_extension_settings } from '../../../extensions.js'; import { eventSource, event_types, substituteParams, chat, saveSettingsDebounced, chat_metadata, saveChatDebounced, user_avatar, getThumbnailUrl, characters, this_chid, extension_prompt_types, extension_prompt_roles, setExtensionPrompt, reloadCurrentChat, Generate, getRequestHeaders } from '../../../../script.js'; import { selected_group, getGroupMembers } from '../../../group-chats.js'; @@ -46,6 +54,7 @@ import { } from './src/core/state.js'; import { loadSettings, saveSettings, saveChatData, loadChatData, updateMessageSwipeData, commitTrackerDataFromPriorMessage } from './src/core/persistence.js'; import { registerAllEvents } from './src/core/events.js'; +import { addExtensionSettings } from './src/core/settingsPanel.js'; // Generation & Parsing modules import { @@ -102,12 +111,8 @@ import { showWelcomeModalIfNeeded, showDeprecationModalIfNeeded } from './src/systems/ui/modals.js'; -import { - initTrackerEditor -} from './src/systems/ui/trackerEditor.js'; -import { - initPromptsEditor -} from './src/systems/ui/promptsEditor.js'; +import { initTrackerEditor } from './src/systems/ui/trackerEditor.js'; +import { initPromptsEditor } from './src/systems/ui/promptsEditor.js'; import { initChapterCheckpointUI, injectCheckpointButton, @@ -179,125 +184,42 @@ import { initHistoryInjection } from './src/systems/integration/sillytavern.js'; -// Old state variable declarations removed - now imported from core modules -// (extensionSettings, lastGeneratedData, committedTrackerData, etc. are now in src/core/state.js) +// Settings UI event listeners (extracted from initUI) +import { bindSettingsListeners, updateWeatherSubOptionsVisibility } from './src/systems/ui/settingsListeners.js'; +// Set up thought-based expressions refresh handler setThoughtBasedExpressionsRefreshHandler(() => { renderAlternatePresentCharacters({ useCommittedFallback: true }); }); -// Utility functions removed - now imported from src/utils/avatars.js -// (getSafeThumbnailUrl) - -// Persistence functions removed - now imported from src/core/persistence.js -// (loadSettings, saveSettings, saveChatData, loadChatData, updateMessageSwipeData) - -// Theme functions removed - now imported from src/systems/ui/theme.js -// (applyTheme, applyCustomTheme, toggleCustomColors, toggleAnimations, -// updateSettingsPopupTheme, applyCustomThemeToSettingsPopup) - -// Layout functions removed - now imported from src/systems/ui/layout.js -// (togglePlotButtons, updateCollapseToggleIcon, setupCollapseToggle, -// updatePanelVisibility, updateSectionVisibility, applyPanelPosition) -// Note: closeMobilePanelWithAnimation is only used internally by mobile.js - -// Mobile UI functions removed - now imported from src/systems/ui/mobile.js -// (setupMobileToggle, constrainFabToViewport, setupMobileTabs, removeMobileTabs, -// setupMobileKeyboardHandling, setupContentEditableScrolling) - /** * Updates UI elements that are dynamically generated and not covered by data-i18n-key. */ function updateDynamicLabels() { - // Update "Refresh RPG Info" button, but only if it's not disabled const refreshBtn = document.getElementById('rpg-manual-update'); if (refreshBtn && !refreshBtn.disabled) { const refreshText = i18n.getTranslation('template.mainPanel.refreshRpgInfo') || 'Refresh RPG Info'; refreshBtn.innerHTML = ` ${refreshText}`; } - - // Update "Last Roll" label updateDiceDisplay(); - - // Update mobile tab labels updateMobileTabLabels(); } -/** - * Adds the extension settings to the Extensions tab. - */ -async function addExtensionSettings() { - // Load the HTML template for the settings - const settingsHtml = await renderExtensionTemplateAsync(extensionName, 'settings'); - $('#extensions_settings2').append(settingsHtml); - - // Set up the enable/disable toggle - $('#rpg-extension-enabled').prop('checked', extensionSettings.enabled).on('change', async function() { - const wasEnabled = extensionSettings.enabled; - extensionSettings.enabled = $(this).prop('checked'); - saveSettings(); - - if (!extensionSettings.enabled && wasEnabled) { - // Disabling extension - remove UI elements - clearExtensionPrompts(); - updateChatThoughts(); // Remove thought bubbles - cleanupCheckpointUI(); // Remove checkpoint buttons and indicators - clearThoughtBasedExpressionsCache(); - - // Disable dynamic weather effects - toggleDynamicWeather(false); - - // Remove panel and toggle buttons - $('#rpg-companion-panel').remove(); - $('#rpg-mobile-toggle').remove(); - $('#rpg-collapse-toggle').remove(); - $('#rpg-plot-buttons').remove(); // Remove plot buttons - removeAlternatePresentCharactersPanel(); - } else if (extensionSettings.enabled && !wasEnabled) { - // Enabling extension - initialize UI - await initUI(); - loadChatData(); // Load chat data for current chat - scheduleChatStateRehydration(); - initThoughtBasedExpressions(); - updateChatThoughts(); // Create thought bubbles if data exists - injectCheckpointButton(); // Re-add checkpoint buttons - updateAllCheckpointIndicators(); // Update button states - } - }); - - // Set up language selector - const langSelect = $('#rpg-companion-language-select'); - if (langSelect.length) { - langSelect.val(i18n.currentLanguage); - langSelect.on('change', async function() { - const selectedLanguage = $(this).val(); - await i18n.setLanguage(selectedLanguage); - // We need to re-apply translations to the settings panel specifically - i18n.applyTranslations(document.getElementById('extensions_settings2')); - }); - } -} - /** * Initializes the UI for the extension. */ async function initUI() { - // Initialize i18n await i18n.init(); - // Only initialize UI if extension is enabled if (!extensionSettings.enabled) { - // console.log('[RPG Companion] Extension disabled - skipping UI initialization'); return; } - // Load the HTML template using SillyTavern's template system + // Load and inject the HTML template const templateHtml = await renderExtensionTemplateAsync(extensionName, 'template'); - - // Append panel to body - positioning handled by CSS $('body').append(templateHtml); - // Add mobile toggle button (FAB - Floating Action Button) + // Add mobile toggle button (FAB) const theme = extensionSettings.theme || 'default'; const mobileToggleHtml = `