Remove all connections button for the popups

9a79c6b1a4138c04864d2664a0f7078b76b9994a

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +50 -6Ignore whitespace
public/script.js+2 -0
@@ -236,6 +236,7 @@ import {
236236 updatePersonaConnectionsAvatarList,
237237 getConnectedPersonas,
238238 askForPersonaSelection,
239+ getCurrentConnectionObj,
239240} from './scripts/personas.js';
240241import { getBackgrounds, initBackgrounds, loadBackgroundSettings, background_settings } from './scripts/backgrounds.js';
241242import { hideLoader, showLoader } from './scripts/loader.js';
@@ -9995,6 +9996,7 @@ jQuery(async function () {
99959996 const selectedPersona = await askForPersonaSelection(t`Persona Connections`, message, connections, {
99969997 okButton: t`Ok`,
99979998 highlightPersonas: true,
9999+ targetedChar: getCurrentConnectionObj(),
999810000 shiftClickHandler: (element, ev) => {
999910001
1000010002 const personaId = $(element).attr('data-pid');
public/scripts/personas.js+48 -6
@@ -561,9 +561,10 @@ export function updatePersonaConnectionsAvatarList() {
561561 * @param {string} [options.okButton='None'] - The label for the OK button
562562 * @param {(element: HTMLElement, ev: MouseEvent) => any} [options.shiftClickHandler] - A function to handle shift-click
563563 * @param {boolean|string[]} [options.highlightPersonas=false] - Whether to highlight personas - either by providing a list of persona keys, or true to highlight all present in current chat
564+ * @param {PersonaConnection} [options.targetedChar] - The targeted character or gorup for this persona selection
564565 * @returns {Promise<string?>} - A promise that resolves to the selected persona id or null if no selection was made
565566 */
566567export async function askForPersonaSelection(title, text, personas, { okButton = 'None', shiftClickHandler = undefined, highlightPersonas = false, targetedChar = undefined } = {}) {
567568 const content = document.createElement('div');
568569 const titleElement = document.createElement('h3');
569570 titleElement.textContent = title;
@@ -581,7 +582,7 @@ export async function askForPersonaSelection(title, text, personas, { okButton =
581582 if (personas.length > 0)
582583 buildPersonaAvatarList(personaListBlock, personas, { interactable: true });
583584 else
584585 personaListBlock.textContent = 't`[NoCurrently no personas connected]'`;
585586
586587 const personasToHighlight = highlightPersonas instanceof Array ? highlightPersonas : (highlightPersonas ? getPersonasOfCurrentChat() : []);
587588
@@ -605,7 +606,35 @@ export async function askForPersonaSelection(title, text, personas, { okButton =
605606 }
606607 });
607608
608- const popup = new Popup(content, POPUP_TYPE.TEXT, '', { okButton: okButton });
609+ /** @type {import('./popup.js').CustomPopupButton[]} */
610+ const customButtons = [];
611+ if (targetedChar) {
612+ customButtons.push({
613+ text: t`Remove All Connections`,
614+ result: 2,
615+ action: () => {
616+ for (const [personaId, description] of Object.entries(power_user.persona_descriptions)) {
617+ /** @type {PersonaConnection[]} */
618+ const connections = description.connections;
619+ if (connections) {
620+ power_user.persona_descriptions[personaId].connections = connections.filter(c => {
621+ if (targetedChar.type == c.type && targetedChar.id == c.id) return false;
622+ return true;
623+ });
624+ }
625+ }
626+
627+ saveSettingsDebounced();
628+ updatePersonaConnectionsAvatarList();
629+ if (power_user.persona_show_notifications) {
630+ const name = targetedChar.type == 'character' ? characters[targetedChar.id]?.name : groups[targetedChar.id]?.name;
631+ toastr.info(t`All connections to ${name} have been removed.`, t`Personas unlocked`);
632+ }
633+ },
634+ });
635+ }
636+
637+ const popup = new Popup(content, POPUP_TYPE.TEXT, '', { okButton: okButton, customButtons: customButtons });
609638 const result = await popup.show();
610639 return Number(result) > 100 ? personas[Number(result) - 100] : null;
611640}
@@ -903,8 +932,7 @@ async function lockPersona(type = 'chat') {
903932 break;
904933 }
905934 case 'character': {
906- const newConnection = menu_type === 'character_edit' ? { type: 'character', id: characters[this_chid]?.avatar } :
935+ const newConnection = getCurrentConnectionObj();
907- menu_type === 'group_edit' ? { type: 'group', id: selected_group } : null;
908936 /** @type {PersonaConnection[]} */
909937 const connections = power_user.persona_descriptions[user_avatar].connections?.filter(c => !isPersonaConnectionLocked(c)) ?? [];
910938 if (newConnection && newConnection.id) {
@@ -1249,7 +1277,7 @@ async function loadPersonaForCurrentChat() {
12491277 } else {
12501278 chatPersona = await askForPersonaSelection(t`Select Persona`,
12511279 t`Multiple personas are connected to this character.\nSelect a persona to use for this chat.`,
12521280 connectedPersonas, { highlightPersonas: true, targetedChar: getCurrentConnectionObj() });
12531281 }
12541282 }
12551283
@@ -1303,6 +1331,20 @@ export function getConnectedPersonas(characterKey = undefined) {
13031331 return connectedPersonas;
13041332}
13051333
1334+/**
1335+ * Retrieves the current connection object based on whether the current chat is with a char or a group.
1336+ *
1337+ * @returns {PersonaConnection} An object representing the current connection
1338+ */
1339+
1340+export function getCurrentConnectionObj() {
1341+ if (menu_type === 'group_edit')
1342+ return { type: 'group', id: selected_group };
1343+ if (menu_type == 'character_edit')
1344+ return { type: 'character', id: characters[this_chid]?.avatar };
1345+ return null;
1346+}
1347+
13061348function onBackupPersonas() {
13071349 const timestamp = new Date().toISOString().split('T')[0].replace(/-/g, '');
13081350 const filename = `personas_${timestamp}.json`;