diff --git a/public/scripts/popup.js b/public/scripts/popup.js index 92152b4db..ddd7dd5d0 100644 --- a/public/scripts/popup.js +++ b/public/scripts/popup.js @@ -74,6 +74,22 @@ const showPopupHelper = { const value = await popup.show(); return value ? String(value) : null; }, + + /** + * Asynchronously displays a confirmation popup with the given header and text, returning the clicked result button value. + * + * @param {string} header - The header text for the popup. + * @param {string} text - The main text for the popup. + * @param {PopupOptions} [popupOptions={}] - Options for the popup. + * @return {Promise} A Promise that resolves with the result of the user's interaction. + */ + confirm: async (header, text, popupOptions = {}) => { + const content = PopupUtils.BuildTextWithHeader(header, text); + const popup = new Popup(content, POPUP_TYPE.CONFIRM, null, popupOptions); + const result = await popup.show(); + if (typeof result === 'string' || typeof result === 'boolean') throw new Error(`Invalid popup result. CONFIRM popups only support numbers, or null. Result: ${result}`); + return result; + } }; export class Popup { diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 1818d7563..23db6945e 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -16,6 +16,7 @@ import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandE import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; import { SlashCommandExecutor } from './slash-commands/SlashCommandExecutor.js'; import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; +import { Popup } from './popup.js'; export { world_info, @@ -1684,8 +1685,7 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl // Regardless of whether success is displayed or not. Make sure the delete button is available. // Do not put this code behind. $('#world_popup_delete').off('click').on('click', async () => { - const confirmation = await callPopup(`

Delete the World/Lorebook: "${name}"?

This action is irreversible!`, 'confirm'); - + const confirmation = await Popup.show.confirm(`Delete the World/Lorebook: "${name}"?`, `This action is irreversible!`); if (!confirmation) { return; } @@ -1862,7 +1862,7 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl $('#world_duplicate').off('click').on('click', async () => { const tempName = getFreeWorldName(); - const finalName = await callPopup('

Create a new World Info?

Enter a name for the new file:', 'input', tempName); + const finalName = await Popup.show.input('Create a new World Info?', 'Enter a name for the new file:', tempName); if (finalName) { await saveWorldInfo(finalName, data, true); @@ -3190,7 +3190,7 @@ async function saveWorldInfo(name, data, immediately) { async function renameWorldInfo(name, data) { const oldName = name; - const newName = await callPopup('

Rename World Info

Enter a new name:', 'input', oldName); + const newName = await Popup.show.input('Rename World Info', 'Enter a new name:', oldName); if (oldName === newName || !newName) { console.debug('World info rename cancelled'); @@ -4138,11 +4138,9 @@ export async function importEmbeddedWorldInfo(skipPopup = false) { } const bookName = characters[chid]?.data?.character_book?.name || `${characters[chid]?.name}'s Lorebook`; - const confirmationText = (`

Are you sure you want to import "${bookName}"?

`) + (world_names.includes(bookName) ? 'It will overwrite the World/Lorebook with the same name.' : ''); if (!skipPopup) { - const confirmation = await callPopup(confirmationText, 'confirm'); - + const confirmation = await Popup.show.confirm(`Are you sure you want to import "${bookName}"?`, world_names.includes(bookName) ? 'It will overwrite the World/Lorebook with the same name.' : ''); if (!confirmation) { return; } @@ -4382,7 +4380,7 @@ jQuery(() => { $('#world_create_button').on('click', async () => { const tempName = getFreeWorldName(); - const finalName = await callPopup('

Create a new World Info?

Enter a name for the new file:', 'input', tempName); + const finalName = await Popup.show.input('Create a new World Info', 'Enter a name for the new file:', tempName); if (finalName) { await createNewWorldInfo(finalName, { interactive: true });