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>

8ec83fd5d9cc33fa2656fa56992f7a131698fbc5

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +43 -22Showing whitespace changes
public/script.js+43 -22
@@ -1373,8 +1373,11 @@ export function resultCheckStatus() {
13731373 * If the character ID doesn't exist, if the chat is being saved, or if a group is being generated, this function does nothing.
13741374 * If the character is different from the currently selected one, it will clear the chat and reset any selected character or group.
13751375 * @param {number} id The ID of the character to switch to.
1376+ * @param {object} [options] Options for the switch.
1377+ * @param {boolean} [options.switchMenu=true] Whether to switch the right menu to the character edit menu if the character is already selected.
1378+ * @returns {Promise<void>} A promise that resolves when the character is switched.
13761379 */
13771380export async function selectCharacterById(id, { switchMenu = true } = {}) {
13781381 if (characters[id] === undefined) {
13791382 return;
13801383 }
@@ -1403,9 +1406,9 @@ export async function selectCharacterById(id) {
14031406 }
14041407 } else {
14051408 //if clicked on character that was already selected
14061409 switchMenu && (selected_button = 'character_edit');
14071410 await unshallowCharacter(this_chid);
14081411 select_selected_character(this_chid, { switchMenu });
14091412 }
14101413}
14111414
@@ -1790,6 +1793,7 @@ export async function getCharacters() {
17901793 body: JSON.stringify({}),
17911794 });
17921795 if (response.ok === true) {
1796+ const previousAvatar = this_chid !== undefined ? characters[this_chid]?.avatar : null;
17931797 characters.splice(0, characters.length);
17941798 const getData = await response.json();
17951799 for (let i = 0; i < getData.length; i++) {
@@ -1803,8 +1807,16 @@ export async function getCharacters() {
18031807
18041808 characters[i]['chat'] = String(characters[i]['chat']);
18051809 }
1806- if (this_chid !== undefined) {
1810+
1807- $('#avatar_url_pole').val(characters[this_chid].avatar);
1811+ if (previousAvatar) {
1812+ const newCharacterId = characters.findIndex(x => x.avatar === previousAvatar);
1813+ if (newCharacterId >= 0) {
1814+ setCharacterId(newCharacterId);
1815+ await selectCharacterById(newCharacterId, { switchMenu: false });
1816+ } else {
1817+ 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.`);
1818+ return location.reload();
1819+ }
18081820 }
18091821
18101822 await getGroups();
@@ -6530,6 +6542,8 @@ export async function renameCharacter(name = null, { silent = false, renameChats
65306542
65316543 await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar);
65326544
6545+ // Unload current character
6546+ setCharacterId(undefined);
65336547 // Reload characters list
65346548 await getCharacters();
65356549
@@ -6538,7 +6552,6 @@ export async function renameCharacter(name = null, { silent = false, renameChats
65386552
65396553 if (newChId !== -1) {
65406554 // Select the character after the renaming
6541- setCharacterId(undefined);
65426555 await selectCharacterById(newChId);
65436556
65446557 // Async delay to update UI
@@ -7885,14 +7898,19 @@ export function select_rm_info(type, charId, previousCharId = null) {
78857898 }
78867899}
78877900
7888-export function select_selected_character(chid) {
7901+/**
7902+ * Selects the right menu for displaying the character editor.
7903+ * @param {number|string} chid Character array index
7904+ * @param {object} [param1] Options for the switch
7905+ * @param {boolean} [param1.switchMenu=true] Whether to switch the menu
7906+ */
7907+export function select_selected_character(chid, { switchMenu = true } = {}) {
78897908 //character select
78907909 //console.log('select_selected_character() -- starting with input of -- ' + chid + ' (name:' + characters[chid].name + ')');
78917910 select_rm_create({ switchMenu });
78927911 switchMenu && setMenuType('character_edit');
78937912 $('#delete_button').css('display', 'flex');
78947913 $('#export_button').css('display', 'flex');
7895- var display_name = characters[chid].name;
78967914
78977915 //create text poles
78987916 $('#rm_button_back').css('display', 'none');
@@ -7907,7 +7925,7 @@ export function select_selected_character(chid) {
79077925
79087926 // Don't update the navbar name if we're peeking the group member defs
79097927 if (!selected_group) {
79107928 $('#rm_button_selected_ch').children('h2').text(display_namecharacters[chid].name);
79117929 }
79127930
79137931 $('#add_avatar_button').val('');
@@ -7938,22 +7956,20 @@ export function select_selected_character(chid) {
79387956 $('#chat_import_avatar_url').val(characters[chid].avatar);
79397957 $('#chat_import_character_name').val(characters[chid].name);
79407958 $('#character_json_data').val(characters[chid].json_data);
7941- let this_avatar = default_avatar;
7942- if (characters[chid].avatar != 'none') {
7943- this_avatar = getThumbnailUrl('avatar', characters[chid].avatar);
7944- }
79457959
79467960 updateFavButtonState(characters[chid].fav || characters[chid].fav == 'true');
79477961
7948- $('#avatar_load_preview').attr('src', this_avatar);
7962+ const avatarUrl = characters[chid].avatar != 'none' ? getThumbnailUrl('avatar', characters[chid].avatar) : default_avatar;
79497963 $('#name_divavatar_load_preview').removeClassattr('displayBlocksrc', avatarUrl);
7950- $('#name_div').addClass('displayNone');
7951- $('#renameCharButton').css('display', '');
79527964 $('.open_alternate_greetings').data('chid', chid);
79537965 $('#set_character_world').data('chid', chid);
79547966 setWorldInfoButtonClass(chid);
79557967 checkEmbeddedWorld(chid);
79567968
7969+ $('#name_div').removeClass('displayBlock');
7970+ $('#name_div').addClass('displayNone');
7971+ $('#renameCharButton').css('display', '');
7972+
79577973 $('#form_create').attr('actiontype', 'editcharacter');
79587974 $('.form_create_bottom_buttons_block .chat_lorebook_button').show();
79597975
@@ -7965,8 +7981,13 @@ export function select_selected_character(chid) {
79657981 saveSettingsDebounced();
79667982}
79677983
7968-function select_rm_create() {
7984+/**
7969- setMenuType('create');
7985+ * Selects the right menu for creating a new character.
7986+ * @param {object} [options] Options for the switch
7987+ * @param {boolean} [options.switchMenu=true] Whether to switch the menu
7988+ */
7989+function select_rm_create({ switchMenu = true } = {}) {
7990+ switchMenu && setMenuType('create');
79707991
79717992 //console.log('select_rm_Create() -- selected button: '+selected_button);
79727993 if (selected_button == 'create') {
@@ -7976,7 +7997,7 @@ function select_rm_create() {
79767997 }
79777998 }
79787999
79798000 switchMenu && selectRightMenuWithAnimation('rm_ch_create_block');
79808001
79818002 $('#set_chat_scenario').hide();
79828003 $('#delete_button_div').css('display', 'none');