Files
rpg-companion-sillytavern/src/core/settingsPanel.js
T
ARIA 34553c18f7 Fix: comprehensive bug review — restore refactor regressions & fix critical/major/minor bugs
Critical:
- responseExtractor.js: fix import depth (7x ../ -> 6x) that broke the module
  graph for encounters, separate/external generation, and auto-avatars
- settingsListeners.js: restore ~25 undefined functions, ~40 lost event
  bindings, and the entire 'Initialize UI state' block dropped by the c14c141
  refactor; add initializeSettingsUIState()
- Restore all External API mode handlers (base-url, api-key, model,
  max-tokens, temperature, key-visibility, test-connection) + value init
- validator.js: convert CJS->ESM (crashed under type:module), fix
  glob.sync->globSync (v13 API), fix '!key in obj' precedence bugs

Major:
- sillytavern.js: add missing updateMessageBlock import
- infoBox.js: add missing saveSettings import
- userStats.js: fix double-escaped quantity regex

Minor:
- config.js: settingsVersion 6->7, restore showLockIcons, fix apiKey comment
- events.js: off() tracking + unregister/re-register on disable/enable
- escapeHtml: escape quotes (XSS) in all 3 copies
- template.html: data-prompt dialogue-coloring -> dialogueColoring
- trackerEditor.js: stale externalApiOnly -> sendAllEnabledOnRefresh
- suppression.js: fix '[object Object]' on empty instruct value
- persistence.js: await migrateToV3JSON (loadSettings now async)
- weatherEffects.js: JSON.parse -> repairJSON
- mobile.js: fix corrupted comment line
- Add missing debugMode setting; settings.html v3.7.2 -> v3.7.4

Dead code removed:
- Unused imports/vars (promptBuilder, jsonPromptHelpers, encounterPrompts,
  injector), withChangeDetection, trackJQueryHandler/cleanupJQueryEvents,
  renderThoughtsSidebarOnly, updateCheckpointButtonInMenu, addDiceQuickReply,
  setupRefreshButtonDrag/setupDebugButtonDrag (~430 lines)

Verified: 78/78 tests pass, style.css in sync, validator runs clean.
2026-08-16 21:42:14 +02:00

82 lines
4.0 KiB
JavaScript

/**
* 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
* @param {Function} registerExtensionEvents - (Re)register all SillyTavern event handlers
* @param {Function} unregisterAllEvents - Unregister all tracked SillyTavern event handlers
* @param {Function} initHistoryInjection - (Re)register history injection listeners
*/
export async function addExtensionSettings($, renderExtensionTemplateAsync, clearExtensionPrompts, updateChatThoughts, cleanupCheckpointUI, clearThoughtBasedExpressionsCache, toggleDynamicWeather, initUI, loadChatData, scheduleChatStateRehydration, initThoughtBasedExpressions, injectCheckpointButton, updateAllCheckpointIndicators, removeAlternatePresentCharactersPanel, registerExtensionEvents, unregisterAllEvents, initHistoryInjection) {
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);
// Unregister all tracked event handlers to avoid leaks while disabled
unregisterAllEvents();
$('#rpg-companion-panel').remove();
$('#rpg-mobile-toggle').remove();
$('#rpg-collapse-toggle').remove();
$('#rpg-plot-buttons').remove();
removeAlternatePresentCharactersPanel();
} else if (extensionSettings.enabled && !wasEnabled) {
// Re-register event handlers that were unregistered on disable
registerExtensionEvents();
initHistoryInjection();
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'));
});
}
}