diff --git a/public/script.js b/public/script.js index 7ab373427..0e731f063 100644 --- a/public/script.js +++ b/public/script.js @@ -6271,6 +6271,23 @@ export function setSendButtonState(value) { is_send_press = value; } +/** + * Renames the currently selected character, updating relevant references and optionally renaming past chats. + * + * If no name is provided, a popup prompts for a new name. If the new name matches the current name, + * the renaming process is aborted. The function sends a request to the server to rename the character + * and handles updates to other related fields such as tags, lore, and author notes. + * + * If the renaming is successful, the character list is reloaded and the renamed character is selected. + * Optionally, past chats can be renamed to reflect the new character name. + * + * @param {string?} [name=null] - The new name for the character. If not provided, a popup will prompt for it. + * @param {object} [options] - Additional options. + * @param {boolean} [options.silent=false] - If true, suppresses popups and warnings. + * @param {boolean?} [options.renameChats=null] - If true, renames past chats to reflect the new character name. + * @returns {Promise} - Returns true if the character was successfully renamed, false otherwise. + */ + export async function renameCharacter(name = null, { silent = false, renameChats = null } = {}) { if (!name && silent) { toastr.warning(t`No character name provided.`, t`Rename Character`); @@ -6354,10 +6371,15 @@ export async function renameCharacter(name = null, { silent = false, renameChats // Also rename as a group member await renameGroupMember(oldAvatar, newAvatar, newValue); - const renamePastChatsConfirm = renameChats !== null ? renameChats - : silent ? false : await callPopup(`

Character renamed!

-

Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?

- Sprites folder (if any) should be renamed manually.`, 'confirm'); + const renamePastChatsConfirm = renameChats !== null + ? renameChats + : silent + ? false + : await Popup.show.confirm( + t`Character renamed!`, + `

${t`Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?`}

+ ${t`Sprites folder (if any) should be renamed manually.`}`, + ) == POPUP_RESULT.AFFIRMATIVE; if (renamePastChatsConfirm) { await renamePastChats(oldAvatar, newAvatar, newValue); @@ -6377,7 +6399,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats } catch (error) { // Reloading to prevent data corruption - if (!silent) await callPopup(t`Something went wrong. The page will be reloaded.`, 'text'); + if (!silent) await Popup.show.text(t`Rename Character`, t`Something went wrong. The page will be reloaded.`); else toastr.error(t`Something went wrong. The page will be reloaded.`, t`Rename Character`); console.log('Renaming character error:', error);