Persist current page on persona actions

This commit is contained in:
Cohee 2024-01-31 11:01:50 +02:00
parent be62903adb
commit fa73c523f0
2 changed files with 20 additions and 9 deletions

View File

@ -821,7 +821,7 @@ async function firstLoadInit() {
await readSecretState(); await readSecretState();
await getClientVersion(); await getClientVersion();
await getSettings(); await getSettings();
await getUserAvatars(); await getUserAvatars(true, user_avatar);
await getCharacters(); await getCharacters();
await getBackgrounds(); await getBackgrounds();
await initTokenizers(); await initTokenizers();
@ -5400,9 +5400,10 @@ function changeMainAPI() {
/** /**
* Gets a list of user avatars. * Gets a list of user avatars.
* @param {boolean} doRender Whether to render the list * @param {boolean} doRender Whether to render the list
* @param {string} openPageAt Item to be opened at
* @returns {Promise<string[]>} List of avatar file names * @returns {Promise<string[]>} List of avatar file names
*/ */
export async function getUserAvatars(doRender = true) { export async function getUserAvatars(doRender = true, openPageAt = '') {
const response = await fetch('/getuseravatars', { const response = await fetch('/getuseravatars', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
@ -5418,10 +5419,11 @@ export async function getUserAvatars(doRender = true) {
const storageKey = 'Personas_PerPage'; const storageKey = 'Personas_PerPage';
const listId = '#user_avatar_block'; const listId = '#user_avatar_block';
const perPage = Number(localStorage.getItem(storageKey)) || 5;
$('#persona_pagination_container').pagination({ $('#persona_pagination_container').pagination({
dataSource: entities, dataSource: entities,
pageSize: Number(localStorage.getItem(storageKey)) || 5, pageSize: perPage,
sizeChangerOptions: [5, 10, 25, 50, 100, 250, 500, 1000], sizeChangerOptions: [5, 10, 25, 50, 100, 250, 500, 1000],
pageRange: 1, pageRange: 1,
pageNumber: savePersonasPage || 1, pageNumber: savePersonasPage || 1,
@ -5450,6 +5452,15 @@ export async function getUserAvatars(doRender = true) {
}, },
}); });
if (openPageAt) {
const avatarIndex = entities.indexOf(openPageAt);
const page = Math.floor(avatarIndex / perPage) + 1;
if (avatarIndex !== -1 && page > 1) {
$('#persona_pagination_container').pagination('go', page);
}
}
return allEntities; return allEntities;
} }
} }
@ -5563,7 +5574,7 @@ async function uploadUserAvatar(e) {
} }
crop_data = undefined; crop_data = undefined;
await getUserAvatars(); await getUserAvatars(true, name || data.path);
}, },
error: (jqXHR, exception) => { }, error: (jqXHR, exception) => { },
}); });

View File

@ -47,7 +47,7 @@ async function uploadUserAvatar(url, name) {
contentType: false, contentType: false,
processData: false, processData: false,
success: async function () { success: async function () {
await getUserAvatars(); await getUserAvatars(true, name);
}, },
}); });
} }
@ -157,7 +157,7 @@ export async function convertCharacterToPersona(characterId = null) {
toastr.success(`You can now select ${name} as a persona in the Persona Management menu.`, 'Persona Created'); toastr.success(`You can now select ${name} as a persona in the Persona Management menu.`, 'Persona Created');
// Refresh the persona selector // Refresh the persona selector
await getUserAvatars(); await getUserAvatars(true, overwriteName);
// Reload the persona description // Reload the persona description
setPersonaDescription(); setPersonaDescription();
} }
@ -202,7 +202,7 @@ export function autoSelectPersona(name) {
export async function updatePersonaNameIfExists(avatarId, newName) { export async function updatePersonaNameIfExists(avatarId, newName) {
if (avatarId in power_user.personas) { if (avatarId in power_user.personas) {
power_user.personas[avatarId] = newName; power_user.personas[avatarId] = newName;
await getUserAvatars(); await getUserAvatars(true, avatarId);
saveSettingsDebounced(); saveSettingsDebounced();
console.log(`Updated persona name for ${avatarId} to ${newName}`); console.log(`Updated persona name for ${avatarId} to ${newName}`);
} else { } else {
@ -256,7 +256,7 @@ async function bindUserNameToPersona(e) {
} }
saveSettingsDebounced(); saveSettingsDebounced();
await getUserAvatars(); await getUserAvatars(true, avatarId);
setPersonaDescription(); setPersonaDescription();
} }
@ -479,7 +479,7 @@ async function setDefaultPersona(e) {
} }
saveSettingsDebounced(); saveSettingsDebounced();
await getUserAvatars(); await getUserAvatars(true, avatarId);
} }
function updateUserLockIcon() { function updateUserLockIcon() {