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.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Settings Panel Module
|
||||
* Manages the extension settings tab in SillyTavern's Extensions panel.
|
||||
* Extracted from index.js to reduce main entry point size.
|
||||
*/
|
||||
|
||||
import { extensionSettings } from './state.js';
|
||||
import { saveSettings } from './persistence.js';
|
||||
import { extensionName } from './config.js';
|
||||
import { i18n } from './i18n.js';
|
||||
|
||||
/**
|
||||
* Adds the extension settings to the Extensions tab.
|
||||
* @param {Function} $ - jQuery function
|
||||
* @param {Function} renderExtensionTemplateAsync - SillyTavern template renderer
|
||||
* @param {Function} clearExtensionPrompts - Clear extension prompts from SillyTavern
|
||||
* @param {Function} updateChatThoughts - Update thought bubbles in chat
|
||||
* @param {Function} cleanupCheckpointUI - Remove checkpoint UI elements
|
||||
* @param {Function} clearThoughtBasedExpressionsCache - Clear expression cache
|
||||
* @param {Function} toggleDynamicWeather - Toggle weather effects
|
||||
* @param {Function} initUI - Initialize the main UI panel
|
||||
* @param {Function} loadChatData - Load chat-specific data
|
||||
* @param {Function} scheduleChatStateRehydration - Schedule chat state rehydration
|
||||
* @param {Function} initThoughtBasedExpressions - Initialize thought-based expressions
|
||||
* @param {Function} injectCheckpointButton - Add checkpoint buttons
|
||||
* @param {Function} updateAllCheckpointIndicators - Update checkpoint button states
|
||||
* @param {Function} removeAlternatePresentCharactersPanel - Remove alt characters panel
|
||||
*/
|
||||
export async function addExtensionSettings($, renderExtensionTemplateAsync, clearExtensionPrompts, updateChatThoughts, cleanupCheckpointUI, clearThoughtBasedExpressionsCache, toggleDynamicWeather, initUI, loadChatData, scheduleChatStateRehydration, initThoughtBasedExpressions, injectCheckpointButton, updateAllCheckpointIndicators, removeAlternatePresentCharactersPanel) {
|
||||
const settingsHtml = await renderExtensionTemplateAsync(extensionName, 'settings');
|
||||
$('#extensions_settings2').append(settingsHtml);
|
||||
|
||||
// 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) {
|
||||
clearExtensionPrompts();
|
||||
updateChatThoughts();
|
||||
cleanupCheckpointUI();
|
||||
clearThoughtBasedExpressionsCache();
|
||||
toggleDynamicWeather(false);
|
||||
|
||||
$('#rpg-companion-panel').remove();
|
||||
$('#rpg-mobile-toggle').remove();
|
||||
$('#rpg-collapse-toggle').remove();
|
||||
$('#rpg-plot-buttons').remove();
|
||||
removeAlternatePresentCharactersPanel();
|
||||
} else if (extensionSettings.enabled && !wasEnabled) {
|
||||
await initUI();
|
||||
loadChatData();
|
||||
scheduleChatStateRehydration();
|
||||
initThoughtBasedExpressions();
|
||||
updateChatThoughts();
|
||||
injectCheckpointButton();
|
||||
updateAllCheckpointIndicators();
|
||||
}
|
||||
});
|
||||
|
||||
// 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);
|
||||
i18n.applyTranslations(document.getElementById('extensions_settings2'));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user