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() {
1373 * If the character ID doesn't exist, if the chat is being saved, or if a group is being generated, this function does nothing.1373 * If the character ID doesn't exist, if the chat is being saved, or if a group is being generated, this function does nothing.
1374 * If the character is different from the currently selected one, it will clear the chat and reset any selected character or group.1374 * If the character is different from the currently selected one, it will clear the chat and reset any selected character or group.
1375 * @param {number} id The ID of the character to switch to.1375 * @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.
1376 */1379 */
1377export async function selectCharacterById(id) {1380export async function selectCharacterById(id, { switchMenu = true } = {}) {
1378 if (characters[id] === undefined) {1381 if (characters[id] === undefined) {
1379 return;1382 return;
1380 }1383 }
@@ -1403,9 +1406,9 @@ export async function selectCharacterById(id) {
1403 }1406 }
1404 } else {1407 } else {
1405 //if clicked on character that was already selected1408 //if clicked on character that was already selected
1406 selected_button = 'character_edit';1409 switchMenu && (selected_button = 'character_edit');
1407 await unshallowCharacter(this_chid);1410 await unshallowCharacter(this_chid);
1408 select_selected_character(this_chid);1411 select_selected_character(this_chid, { switchMenu });
1409 }1412 }
1410}1413}
14111414
@@ -1790,6 +1793,7 @@ export async function getCharacters() {
1790 body: JSON.stringify({}),1793 body: JSON.stringify({}),
1791 });1794 });
1792 if (response.ok === true) {1795 if (response.ok === true) {
1796 const previousAvatar = this_chid !== undefined ? characters[this_chid]?.avatar : null;
1793 characters.splice(0, characters.length);1797 characters.splice(0, characters.length);
1794 const getData = await response.json();1798 const getData = await response.json();
1795 for (let i = 0; i < getData.length; i++) {1799 for (let i = 0; i < getData.length; i++) {
@@ -1803,8 +1807,16 @@ export async function getCharacters() {
18031807
1804 characters[i]['chat'] = String(characters[i]['chat']);1808 characters[i]['chat'] = String(characters[i]['chat']);
1805 }1809 }
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 }
1808 }1820 }
18091821
1810 await getGroups();1822 await getGroups();
@@ -6530,6 +6542,8 @@ export async function renameCharacter(name = null, { silent = false, renameChats
65306542
6531 await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar);6543 await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar);
65326544
6545 // Unload current character
6546 setCharacterId(undefined);
6533 // Reload characters list6547 // Reload characters list
6534 await getCharacters();6548 await getCharacters();
65356549
@@ -6538,7 +6552,6 @@ export async function renameCharacter(name = null, { silent = false, renameChats
65386552
6539 if (newChId !== -1) {6553 if (newChId !== -1) {
6540 // Select the character after the renaming6554 // Select the character after the renaming
6541 setCharacterId(undefined);
6542 await selectCharacterById(newChId);6555 await selectCharacterById(newChId);
65436556
6544 // Async delay to update UI6557 // Async delay to update UI
@@ -7885,14 +7898,19 @@ export function select_rm_info(type, charId, previousCharId = null) {
7885 }7898 }
7886}7899}
78877900
7888export 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 */
7907export function select_selected_character(chid, { switchMenu = true } = {}) {
7889 //character select7908 //character select
7890 //console.log('select_selected_character() -- starting with input of -- ' + chid + ' (name:' + characters[chid].name + ')');7909 //console.log('select_selected_character() -- starting with input of -- ' + chid + ' (name:' + characters[chid].name + ')');
7891 select_rm_create();7910 select_rm_create({ switchMenu });
7892 setMenuType('character_edit');7911 switchMenu && setMenuType('character_edit');
7893 $('#delete_button').css('display', 'flex');7912 $('#delete_button').css('display', 'flex');
7894 $('#export_button').css('display', 'flex');7913 $('#export_button').css('display', 'flex');
7895 var display_name = characters[chid].name;
78967914
7897 //create text poles7915 //create text poles
7898 $('#rm_button_back').css('display', 'none');7916 $('#rm_button_back').css('display', 'none');
@@ -7907,7 +7925,7 @@ export function select_selected_character(chid) {
79077925
7908 // Don't update the navbar name if we're peeking the group member defs7926 // Don't update the navbar name if we're peeking the group member defs
7909 if (!selected_group) {7927 if (!selected_group) {
7910 $('#rm_button_selected_ch').children('h2').text(display_name);7928 $('#rm_button_selected_ch').children('h2').text(characters[chid].name);
7911 }7929 }
79127930
7913 $('#add_avatar_button').val('');7931 $('#add_avatar_button').val('');
@@ -7938,22 +7956,20 @@ export function select_selected_character(chid) {
7938 $('#chat_import_avatar_url').val(characters[chid].avatar);7956 $('#chat_import_avatar_url').val(characters[chid].avatar);
7939 $('#chat_import_character_name').val(characters[chid].name);7957 $('#chat_import_character_name').val(characters[chid].name);
7940 $('#character_json_data').val(characters[chid].json_data);7958 $('#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
7946 updateFavButtonState(characters[chid].fav || characters[chid].fav == 'true');7960 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;
7949 $('#name_div').removeClass('displayBlock');7963 $('#avatar_load_preview').attr('src', avatarUrl);
7950 $('#name_div').addClass('displayNone');
7951 $('#renameCharButton').css('display', '');
7952 $('.open_alternate_greetings').data('chid', chid);7964 $('.open_alternate_greetings').data('chid', chid);
7953 $('#set_character_world').data('chid', chid);7965 $('#set_character_world').data('chid', chid);
7954 setWorldInfoButtonClass(chid);7966 setWorldInfoButtonClass(chid);
7955 checkEmbeddedWorld(chid);7967 checkEmbeddedWorld(chid);
79567968
7969 $('#name_div').removeClass('displayBlock');
7970 $('#name_div').addClass('displayNone');
7971 $('#renameCharButton').css('display', '');
7972
7957 $('#form_create').attr('actiontype', 'editcharacter');7973 $('#form_create').attr('actiontype', 'editcharacter');
7958 $('.form_create_bottom_buttons_block .chat_lorebook_button').show();7974 $('.form_create_bottom_buttons_block .chat_lorebook_button').show();
79597975
@@ -7965,8 +7981,13 @@ export function select_selected_character(chid) {
7965 saveSettingsDebounced();7981 saveSettingsDebounced();
7966}7982}
79677983
7968function 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 */
7989function select_rm_create({ switchMenu = true } = {}) {
7990 switchMenu && setMenuType('create');
79707991
7971 //console.log('select_rm_Create() -- selected button: '+selected_button);7992 //console.log('select_rm_Create() -- selected button: '+selected_button);
7972 if (selected_button == 'create') {7993 if (selected_button == 'create') {
@@ -7976,7 +7997,7 @@ function select_rm_create() {
7976 }7997 }
7977 }7998 }
79787999
7979 selectRightMenuWithAnimation('rm_ch_create_block');8000 switchMenu && selectRightMenuWithAnimation('rm_ch_create_block');
79808001
7981 $('#set_chat_scenario').hide();8002 $('#set_chat_scenario').hide();
7982 $('#delete_button_div').css('display', 'none');8003 $('#delete_button_div').css('display', 'none');