Fix corruption due to this_chid shift (#3669)

* continue works same as swipe continued message isn't depth counted

* correct early-out check

* update regex depth setting tooltips for accuracy

* update max tooltip

* remove redundant check

* Fix corruption due to this_chid shift
Fixes #3667

* Unshallow current character on reload

* Allow -1 as a min depth value

* Use selectCharacterById, fix rename logic

* Remove pointless local variables

* Add 'switchMenu' param to selectCharacterById

---------

Co-authored-by: Reithan <bo122081@hotmail.com>
This commit is contained in:
Cohee
2025-03-16 19:14:27 +02:00
committed by GitHub
parent d42a81f97c
commit 8ec83fd5d9

View File

@ -1373,8 +1373,11 @@ export function resultCheckStatus() {
* If the character ID doesn't exist, if the chat is being saved, or if a group is being generated, this function does nothing. * If the character ID doesn't exist, if the chat is being saved, or if a group is being generated, this function does nothing.
* If the character is different from the currently selected one, it will clear the chat and reset any selected character or group. * If the character is different from the currently selected one, it will clear the chat and reset any selected character or group.
* @param {number} id The ID of the character to switch to. * @param {number} id The ID of the character to switch to.
* @param {object} [options] Options for the switch.
* @param {boolean} [options.switchMenu=true] Whether to switch the right menu to the character edit menu if the character is already selected.
* @returns {Promise<void>} A promise that resolves when the character is switched.
*/ */
export async function selectCharacterById(id) { export async function selectCharacterById(id, { switchMenu = true } = {}) {
if (characters[id] === undefined) { if (characters[id] === undefined) {
return; return;
} }
@ -1403,9 +1406,9 @@ export async function selectCharacterById(id) {
} }
} else { } else {
//if clicked on character that was already selected //if clicked on character that was already selected
selected_button = 'character_edit'; switchMenu && (selected_button = 'character_edit');
await unshallowCharacter(this_chid); await unshallowCharacter(this_chid);
select_selected_character(this_chid); select_selected_character(this_chid, { switchMenu });
} }
} }
@ -1790,6 +1793,7 @@ export async function getCharacters() {
body: JSON.stringify({}), body: JSON.stringify({}),
}); });
if (response.ok === true) { if (response.ok === true) {
const previousAvatar = this_chid !== undefined ? characters[this_chid]?.avatar : null;
characters.splice(0, characters.length); characters.splice(0, characters.length);
const getData = await response.json(); const getData = await response.json();
for (let i = 0; i < getData.length; i++) { for (let i = 0; i < getData.length; i++) {
@ -1803,8 +1807,16 @@ export async function getCharacters() {
characters[i]['chat'] = String(characters[i]['chat']); characters[i]['chat'] = String(characters[i]['chat']);
} }
if (this_chid !== undefined) {
$('#avatar_url_pole').val(characters[this_chid].avatar); if (previousAvatar) {
const newCharacterId = characters.findIndex(x => x.avatar === previousAvatar);
if (newCharacterId >= 0) {
setCharacterId(newCharacterId);
await selectCharacterById(newCharacterId, { switchMenu: false });
} else {
await Popup.show.text(t`ERROR: The active character is no longer available.`, t`The page will be refreshed to prevent data loss. Press "OK" to continue.`);
return location.reload();
}
} }
await getGroups(); await getGroups();
@ -6530,6 +6542,8 @@ export async function renameCharacter(name = null, { silent = false, renameChats
await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar); await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar);
// Unload current character
setCharacterId(undefined);
// Reload characters list // Reload characters list
await getCharacters(); await getCharacters();
@ -6538,7 +6552,6 @@ export async function renameCharacter(name = null, { silent = false, renameChats
if (newChId !== -1) { if (newChId !== -1) {
// Select the character after the renaming // Select the character after the renaming
setCharacterId(undefined);
await selectCharacterById(newChId); await selectCharacterById(newChId);
// Async delay to update UI // Async delay to update UI
@ -7885,14 +7898,19 @@ export function select_rm_info(type, charId, previousCharId = null) {
} }
} }
export function select_selected_character(chid) { /**
* Selects the right menu for displaying the character editor.
* @param {number|string} chid Character array index
* @param {object} [param1] Options for the switch
* @param {boolean} [param1.switchMenu=true] Whether to switch the menu
*/
export function select_selected_character(chid, { switchMenu = true } = {}) {
//character select //character select
//console.log('select_selected_character() -- starting with input of -- ' + chid + ' (name:' + characters[chid].name + ')'); //console.log('select_selected_character() -- starting with input of -- ' + chid + ' (name:' + characters[chid].name + ')');
select_rm_create(); select_rm_create({ switchMenu });
setMenuType('character_edit'); switchMenu && setMenuType('character_edit');
$('#delete_button').css('display', 'flex'); $('#delete_button').css('display', 'flex');
$('#export_button').css('display', 'flex'); $('#export_button').css('display', 'flex');
var display_name = characters[chid].name;
//create text poles //create text poles
$('#rm_button_back').css('display', 'none'); $('#rm_button_back').css('display', 'none');
@ -7907,7 +7925,7 @@ export function select_selected_character(chid) {
// Don't update the navbar name if we're peeking the group member defs // Don't update the navbar name if we're peeking the group member defs
if (!selected_group) { if (!selected_group) {
$('#rm_button_selected_ch').children('h2').text(display_name); $('#rm_button_selected_ch').children('h2').text(characters[chid].name);
} }
$('#add_avatar_button').val(''); $('#add_avatar_button').val('');
@ -7938,22 +7956,20 @@ export function select_selected_character(chid) {
$('#chat_import_avatar_url').val(characters[chid].avatar); $('#chat_import_avatar_url').val(characters[chid].avatar);
$('#chat_import_character_name').val(characters[chid].name); $('#chat_import_character_name').val(characters[chid].name);
$('#character_json_data').val(characters[chid].json_data); $('#character_json_data').val(characters[chid].json_data);
let this_avatar = default_avatar;
if (characters[chid].avatar != 'none') {
this_avatar = getThumbnailUrl('avatar', characters[chid].avatar);
}
updateFavButtonState(characters[chid].fav || characters[chid].fav == 'true'); updateFavButtonState(characters[chid].fav || characters[chid].fav == 'true');
$('#avatar_load_preview').attr('src', this_avatar); const avatarUrl = characters[chid].avatar != 'none' ? getThumbnailUrl('avatar', characters[chid].avatar) : default_avatar;
$('#name_div').removeClass('displayBlock'); $('#avatar_load_preview').attr('src', avatarUrl);
$('#name_div').addClass('displayNone');
$('#renameCharButton').css('display', '');
$('.open_alternate_greetings').data('chid', chid); $('.open_alternate_greetings').data('chid', chid);
$('#set_character_world').data('chid', chid); $('#set_character_world').data('chid', chid);
setWorldInfoButtonClass(chid); setWorldInfoButtonClass(chid);
checkEmbeddedWorld(chid); checkEmbeddedWorld(chid);
$('#name_div').removeClass('displayBlock');
$('#name_div').addClass('displayNone');
$('#renameCharButton').css('display', '');
$('#form_create').attr('actiontype', 'editcharacter'); $('#form_create').attr('actiontype', 'editcharacter');
$('.form_create_bottom_buttons_block .chat_lorebook_button').show(); $('.form_create_bottom_buttons_block .chat_lorebook_button').show();
@ -7965,8 +7981,13 @@ export function select_selected_character(chid) {
saveSettingsDebounced(); saveSettingsDebounced();
} }
function select_rm_create() { /**
setMenuType('create'); * Selects the right menu for creating a new character.
* @param {object} [options] Options for the switch
* @param {boolean} [options.switchMenu=true] Whether to switch the menu
*/
function select_rm_create({ switchMenu = true } = {}) {
switchMenu && setMenuType('create');
//console.log('select_rm_Create() -- selected button: '+selected_button); //console.log('select_rm_Create() -- selected button: '+selected_button);
if (selected_button == 'create') { if (selected_button == 'create') {
@ -7976,7 +7997,7 @@ function select_rm_create() {
} }
} }
selectRightMenuWithAnimation('rm_ch_create_block'); switchMenu && selectRightMenuWithAnimation('rm_ch_create_block');
$('#set_chat_scenario').hide(); $('#set_chat_scenario').hide();
$('#delete_button_div').css('display', 'none'); $('#delete_button_div').css('display', 'none');