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>
Signed| @@ -1373,8 +1373,11 @@ export function resultCheckStatus() { | ||
| 1373 | 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 | 1374 | * If the character is different from the currently selected one, it will clear the chat and reset any selected character or group. |
| 1375 | 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 | */ |
| 1377 | 1380 | export async function selectCharacterById(id, { switchMenu = true } = {}) { |
| 1378 | 1381 | if (characters[id] === undefined) { |
| 1379 | 1382 | return; |
| 1380 | 1383 | } |
| @@ -1403,9 +1406,9 @@ export async function selectCharacterById(id) { | ||
| 1403 | 1406 | } |
| 1404 | 1407 | } else { |
| 1405 | 1408 | //if clicked on character that was already selected |
| 1406 | 1409 | switchMenu && (selected_button = 'character_edit'); |
| 1407 | 1410 | await unshallowCharacter(this_chid); |
| 1408 | 1411 | select_selected_character(this_chid, { switchMenu }); |
| 1409 | 1412 | } |
| 1410 | 1413 | } |
| 1411 | 1414 | |
| @@ -1790,6 +1793,7 @@ export async function getCharacters() { | ||
| 1790 | 1793 | body: JSON.stringify({}), |
| 1791 | 1794 | }); |
| 1792 | 1795 | if (response.ok === true) { |
| 1796 | + const previousAvatar = this_chid !== undefined ? characters[this_chid]?.avatar : null; | |
| 1793 | 1797 | characters.splice(0, characters.length); |
| 1794 | 1798 | const getData = await response.json(); |
| 1795 | 1799 | for (let i = 0; i < getData.length; i++) { |
| @@ -1803,8 +1807,16 @@ export async function getCharacters() { | ||
| 1803 | 1807 | |
| 1804 | 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 | } |
| 1809 | 1821 | |
| 1810 | 1822 | await getGroups(); |
| @@ -6530,6 +6542,8 @@ export async function renameCharacter(name = null, { silent = false, renameChats | ||
| 6530 | 6542 | |
| 6531 | 6543 | await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar); |
| 6532 | 6544 | |
| 6545 | + // Unload current character | |
| 6546 | + setCharacterId(undefined); | |
| 6533 | 6547 | // Reload characters list |
| 6534 | 6548 | await getCharacters(); |
| 6535 | 6549 | |
| @@ -6538,7 +6552,6 @@ export async function renameCharacter(name = null, { silent = false, renameChats | ||
| 6538 | 6552 | |
| 6539 | 6553 | if (newChId !== -1) { |
| 6540 | 6554 | // Select the character after the renaming |
| 6541 | - setCharacterId(undefined); | |
| 6542 | 6555 | await selectCharacterById(newChId); |
| 6543 | 6556 | |
| 6544 | 6557 | // Async delay to update UI |
| @@ -7885,14 +7898,19 @@ export function select_rm_info(type, charId, previousCharId = null) { | ||
| 7885 | 7898 | } |
| 7886 | 7899 | } |
| 7887 | 7900 | |
| 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 } = {}) { | |
| 7889 | 7908 | //character select |
| 7890 | 7909 | //console.log('select_selected_character() -- starting with input of -- ' + chid + ' (name:' + characters[chid].name + ')'); |
| 7891 | 7910 | select_rm_create({ switchMenu }); |
| 7892 | 7911 | switchMenu && setMenuType('character_edit'); |
| 7893 | 7912 | $('#delete_button').css('display', 'flex'); |
| 7894 | 7913 | $('#export_button').css('display', 'flex'); |
| 7895 | - var display_name = characters[chid].name; | |
| 7896 | 7914 | |
| 7897 | 7915 | //create text poles |
| 7898 | 7916 | $('#rm_button_back').css('display', 'none'); |
| @@ -7907,7 +7925,7 @@ export function select_selected_character(chid) { | ||
| 7907 | 7925 | |
| 7908 | 7926 | // Don't update the navbar name if we're peeking the group member defs |
| 7909 | 7927 | if (!selected_group) { |
| 7910 | 7928 | $('#rm_button_selected_ch').children('h2').text(display_namecharacters[chid].name); |
| 7911 | 7929 | } |
| 7912 | 7930 | |
| 7913 | 7931 | $('#add_avatar_button').val(''); |
| @@ -7938,22 +7956,20 @@ export function select_selected_character(chid) { | ||
| 7938 | 7956 | $('#chat_import_avatar_url').val(characters[chid].avatar); |
| 7939 | 7957 | $('#chat_import_character_name').val(characters[chid].name); |
| 7940 | 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 | - } | |
| 7945 | 7959 | |
| 7946 | 7960 | updateFavButtonState(characters[chid].fav || characters[chid].fav == 'true'); |
| 7947 | 7961 | |
| 7948 | - $('#avatar_load_preview').attr('src', this_avatar); | |
| 7962 | + const avatarUrl = characters[chid].avatar != 'none' ? getThumbnailUrl('avatar', characters[chid].avatar) : default_avatar; | |
| 7949 | 7963 | $('#name_divavatar_load_preview').removeClassattr('displayBlocksrc', avatarUrl); |
| 7950 | - $('#name_div').addClass('displayNone'); | |
| 7951 | - $('#renameCharButton').css('display', ''); | |
| 7952 | 7964 | $('.open_alternate_greetings').data('chid', chid); |
| 7953 | 7965 | $('#set_character_world').data('chid', chid); |
| 7954 | 7966 | setWorldInfoButtonClass(chid); |
| 7955 | 7967 | checkEmbeddedWorld(chid); |
| 7956 | 7968 | |
| 7969 | + $('#name_div').removeClass('displayBlock'); | |
| 7970 | + $('#name_div').addClass('displayNone'); | |
| 7971 | + $('#renameCharButton').css('display', ''); | |
| 7972 | + | |
| 7957 | 7973 | $('#form_create').attr('actiontype', 'editcharacter'); |
| 7958 | 7974 | $('.form_create_bottom_buttons_block .chat_lorebook_button').show(); |
| 7959 | 7975 | |
| @@ -7965,8 +7981,13 @@ export function select_selected_character(chid) { | ||
| 7965 | 7981 | saveSettingsDebounced(); |
| 7966 | 7982 | } |
| 7967 | 7983 | |
| 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'); | |
| 7970 | 7991 | |
| 7971 | 7992 | //console.log('select_rm_Create() -- selected button: '+selected_button); |
| 7972 | 7993 | if (selected_button == 'create') { |
| @@ -7976,7 +7997,7 @@ function select_rm_create() { | ||
| 7976 | 7997 | } |
| 7977 | 7998 | } |
| 7978 | 7999 | |
| 7979 | 8000 | switchMenu && selectRightMenuWithAnimation('rm_ch_create_block'); |
| 7980 | 8001 | |
| 7981 | 8002 | $('#set_chat_scenario').hide(); |
| 7982 | 8003 | $('#delete_button_div').css('display', 'none'); |