Update cwb_settingsManager.js

This commit is contained in:
2025-10-04 02:34:48 +08:00
committed by GitHub
parent 802a463c59
commit 536ad5b835

View File

@@ -134,9 +134,7 @@ function bindWorldBookSettings() {
if (settings.cwb_worldbook_target === undefined) settings.cwb_worldbook_target = 'primary'; if (settings.cwb_worldbook_target === undefined) settings.cwb_worldbook_target = 'primary';
if (settings.cwb_custom_worldbook === undefined) settings.cwb_custom_worldbook = null; if (settings.cwb_custom_worldbook === undefined) settings.cwb_custom_worldbook = null;
const sourceRadios = $panel.find('input[name="cwb_worldbook_target"]');
const customSelectWrapper = $panel.find('#cwb_worldbook_select_wrapper'); const customSelectWrapper = $panel.find('#cwb_worldbook_select_wrapper');
const refreshButton = $panel.find('#cwb_refresh_worldbooks');
const bookListContainer = $panel.find('#cwb_worldbook_radio_list'); const bookListContainer = $panel.find('#cwb_worldbook_radio_list');
const renderWorldBookList = () => { const renderWorldBookList = () => {
@@ -151,15 +149,6 @@ function bindWorldBookSettings() {
.val(book.file_name) .val(book.file_name)
.prop('checked', settings.cwb_custom_worldbook === book.file_name); .prop('checked', settings.cwb_custom_worldbook === book.file_name);
radio.on('change', () => {
if (radio.prop('checked')) {
settings.cwb_custom_worldbook = book.file_name;
saveSettingsDebounced();
loadSettings();
showToastr('info', `已选择世界书: ${book.name}`);
}
});
const label = $('<label></label>').attr('for', `cwb-wb-radio-${book.file_name}`).text(book.name); const label = $('<label></label>').attr('for', `cwb-wb-radio-${book.file_name}`).text(book.name);
div.append(radio).append(label); div.append(radio).append(label);
@@ -178,20 +167,28 @@ function bindWorldBookSettings() {
} }
}; };
sourceRadios.each(function() { $panel.find('input[name="cwb_worldbook_target"]').each(function() {
$(this).prop('checked', $(this).val() === settings.cwb_worldbook_target); $(this).prop('checked', $(this).val() === settings.cwb_worldbook_target);
}); });
updateCustomSelectVisibility(); updateCustomSelectVisibility();
sourceRadios.on('change', function() { $panel.off('change.cwb_worldbook_target').on('change.cwb_worldbook_target', 'input[name="cwb_worldbook_target"]', function() {
if ($(this).prop('checked')) { if ($(this).prop('checked')) {
settings.cwb_worldbook_target = $(this).val(); settings.cwb_worldbook_target = $(this).val();
updateCustomSelectVisibility(); updateCustomSelectVisibility();
saveSettingsDebounced(); saveSettingsDebounced();
loadSettings(); // Sync to state loadSettings();
} }
}); });
bookListContainer.off('change.cwb_worldbook_selection').on('change.cwb_worldbook_selection', 'input[name="cwb_worldbook_selection"]', function() {
refreshButton.on('click', renderWorldBookList); const radio = $(this);
if (radio.prop('checked')) {
settings.cwb_custom_worldbook = radio.val();
saveSettingsDebounced();
loadSettings();
showToastr('info', `已选择世界书: ${radio.next('label').text()}`);
}
});
$panel.off('click.cwb_refresh_worldbooks').on('click.cwb_refresh_worldbooks', '#cwb_refresh_worldbooks', renderWorldBookList);
} }
export function bindSettingsEvents($settingsPanel) { export function bindSettingsEvents($settingsPanel) {
@@ -209,8 +206,6 @@ export function bindSettingsEvents($settingsPanel) {
}); });
$panel.on('change', '#cwb-api-mode', function() { $panel.on('change', '#cwb-api-mode', function() {
const selectedMode = $(this).val(); const selectedMode = $(this).val();
// 自动保存API模式设置
getSettings().cwb_api_mode = selectedMode; getSettings().cwb_api_mode = selectedMode;
saveSettingsDebounced(); saveSettingsDebounced();
@@ -223,8 +218,6 @@ export function bindSettingsEvents($settingsPanel) {
}); });
$panel.on('change', '#cwb-tavern-profile', function() { $panel.on('change', '#cwb-tavern-profile', function() {
const selectedProfile = $(this).val(); const selectedProfile = $(this).val();
// 自动保存SillyTavern预设选择
getSettings().cwb_tavern_profile = selectedProfile; getSettings().cwb_tavern_profile = selectedProfile;
saveSettingsDebounced(); saveSettingsDebounced();
@@ -235,11 +228,8 @@ export function bindSettingsEvents($settingsPanel) {
updateApiStatusDisplay($panel); updateApiStatusDisplay($panel);
}); });
// 添加API字段的实时保存
$panel.on('input', '#cwb-api-url', function() { $panel.on('input', '#cwb-api-url', function() {
const apiUrl = $(this).val().trim(); const apiUrl = $(this).val().trim();
// 同时更新设置和状态
getSettings().cwb_api_url = apiUrl; getSettings().cwb_api_url = apiUrl;
state.customApiConfig.url = apiUrl; state.customApiConfig.url = apiUrl;
@@ -251,8 +241,6 @@ export function bindSettingsEvents($settingsPanel) {
$panel.on('input', '#cwb-api-key', function() { $panel.on('input', '#cwb-api-key', function() {
const apiKey = $(this).val(); const apiKey = $(this).val();
// 同时更新设置和状态
getSettings().cwb_api_key = apiKey; getSettings().cwb_api_key = apiKey;
state.customApiConfig.apiKey = apiKey; state.customApiConfig.apiKey = apiKey;
@@ -263,8 +251,6 @@ export function bindSettingsEvents($settingsPanel) {
$panel.on('change', '#cwb-api-model', function() { $panel.on('change', '#cwb-api-model', function() {
const model = $(this).val(); const model = $(this).val();
// 同时更新设置和状态
getSettings().cwb_api_model = model; getSettings().cwb_api_model = model;
state.customApiConfig.model = model; state.customApiConfig.model = model;
@@ -503,12 +489,10 @@ export function loadSettings() {
const settings = getSettings(); const settings = getSettings();
// Initialize settings with defaults if not present
if (!settings) { if (!settings) {
extension_settings[extensionName] = { ...cwbCompleteDefaultSettings }; extension_settings[extensionName] = { ...cwbCompleteDefaultSettings };
console.log('[CWB] Initialized default settings'); console.log('[CWB] Initialized default settings');
} else { } else {
// Ensure all default settings exist
Object.keys(cwbCompleteDefaultSettings).forEach(key => { Object.keys(cwbCompleteDefaultSettings).forEach(key => {
if (settings[key] === undefined || settings[key] === null) { if (settings[key] === undefined || settings[key] === null) {
settings[key] = cwbCompleteDefaultSettings[key]; settings[key] = cwbCompleteDefaultSettings[key];
@@ -518,7 +502,6 @@ export function loadSettings() {
const finalSettings = getSettings(); const finalSettings = getSettings();
// Apply localStorage overrides
const overrides = JSON.parse(localStorage.getItem(CWB_BOOLEAN_SETTINGS_OVERRIDE_KEY) || '{}'); const overrides = JSON.parse(localStorage.getItem(CWB_BOOLEAN_SETTINGS_OVERRIDE_KEY) || '{}');
if (overrides.cwb_master_enabled !== undefined) { if (overrides.cwb_master_enabled !== undefined) {
finalSettings.cwb_master_enabled = overrides.cwb_master_enabled; finalSettings.cwb_master_enabled = overrides.cwb_master_enabled;
@@ -532,8 +515,6 @@ export function loadSettings() {
if (overrides.cwb_incremental_update_enabled !== undefined) { if (overrides.cwb_incremental_update_enabled !== undefined) {
finalSettings.cwb_incremental_update_enabled = overrides.cwb_incremental_update_enabled; finalSettings.cwb_incremental_update_enabled = overrides.cwb_incremental_update_enabled;
} }
// Update state object with current settings
state.masterEnabled = finalSettings.cwb_master_enabled; state.masterEnabled = finalSettings.cwb_master_enabled;
state.viewerEnabled = finalSettings.cwb_viewer_enabled; state.viewerEnabled = finalSettings.cwb_viewer_enabled;
state.autoUpdateEnabled = finalSettings.cwb_auto_update_enabled; state.autoUpdateEnabled = finalSettings.cwb_auto_update_enabled;
@@ -556,15 +537,11 @@ export function loadSettings() {
viewerEnabled: state.viewerEnabled, viewerEnabled: state.viewerEnabled,
autoUpdateEnabled: state.autoUpdateEnabled autoUpdateEnabled: state.autoUpdateEnabled
}); });
// Update UI if panel exists
if ($panel) { if ($panel) {
updateUiWithSettings(); updateUiWithSettings();
} }
updateControlsLockState(); updateControlsLockState();
// Update viewer button visibility after state is loaded
setTimeout(() => { setTimeout(() => {
const $viewerButton = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`); const $viewerButton = $(`#${CHAR_CARD_VIEWER_BUTTON_ID}`);
if ($viewerButton.length > 0) { if ($viewerButton.length > 0) {