Merge pull request #2834 from SillyTavern/replace-ajax

Replace ajax with fetch in character create/edit
This commit is contained in:
Cohee 2024-09-12 22:06:10 +03:00 committed by GitHub
commit 7c383e3218
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7892,13 +7892,19 @@ async function createOrEditCharacter(e) {
formData.set('avatar', convertedFile);
}
const headers = getRequestHeaders();
delete headers['Content-Type'];
if ($('#form_create').attr('actiontype') == 'createcharacter') {
if (String($('#character_name_pole').val()).length > 0) {
if (String($('#character_name_pole').val()).length === 0) {
toastr.error('Name is required');
return;
}
if (is_group_generating || is_send_press) {
toastr.error('Cannot create characters while generating. Stop the request and try again.', 'Creation aborted');
throw new Error('Cannot import character while generating');
return;
}
try {
//if the character name text area isn't empty (only posible when creating a new character)
let url = '/api/characters/create';
@ -7913,18 +7919,19 @@ async function createOrEditCharacter(e) {
formData.append('extensions', JSON.stringify(create_save.extensions));
await jQuery.ajax({
type: 'POST',
url: url,
data: formData,
beforeSend: function () {
$('#create_button').attr('disabled', String(true));
$('#create_button').attr('value', '⏳');
},
cache: false,
contentType: false,
processData: false,
success: async function (html) {
const fetchResult = await fetch(url, {
method: 'POST',
headers: headers,
body: formData,
cache: 'no-cache',
});
if (!fetchResult.ok) {
throw new Error('Fetch result is not ok');
}
const avatarId = await fetchResult.text();
$('#character_cross').trigger('click'); //closes the advanced character editing popup
const fields = [
{ id: '#character_name_pole', callback: value => create_save.name = value },
@ -7959,33 +7966,29 @@ async function createOrEditCharacter(e) {
create_save.avatar = '';
$('#create_button').removeAttr('disabled');
$('#add_avatar_button').replaceWith(
$('#add_avatar_button').val('').clone(true),
);
$('#create_button').attr('value', '✅');
let oldSelectedChar = null;
if (this_chid !== undefined) {
oldSelectedChar = characters[this_chid].avatar;
}
console.log(`new avatar id: ${html}`);
createTagMapFromList('#tagList', html);
console.log(`new avatar id: ${avatarId}`);
createTagMapFromList('#tagList', avatarId);
await getCharacters();
select_rm_info('char_create', html, oldSelectedChar);
select_rm_info('char_create', avatarId, oldSelectedChar);
crop_data = undefined;
},
error: function (jqXHR, exception) {
$('#create_button').removeAttr('disabled');
},
});
} else {
toastr.error('Name is required');
} catch (error) {
console.error('Error creating character', error);
toastr.error('Failed to create character');
}
} else {
try {
let url = '/api/characters/edit';
if (crop_data != undefined) {
@ -8000,19 +8003,16 @@ async function createOrEditCharacter(e) {
}
}
await jQuery.ajax({
type: 'POST',
url: url,
data: formData,
beforeSend: function () {
$('#create_button').attr('disabled', String(true));
$('#create_button').attr('value', 'Save');
},
cache: false,
contentType: false,
processData: false,
success: async function (html) {
$('#create_button').removeAttr('disabled');
const fetchResult = await fetch(url, {
method: 'POST',
headers: headers,
body: formData,
cache: 'no-cache',
});
if (!fetchResult.ok) {
throw new Error('Fetch result is not ok');
}
await getOneCharacter(formData.get('avatar_url'));
favsToHotswap(); // Update fav state
@ -8022,7 +8022,7 @@ async function createOrEditCharacter(e) {
);
$('#create_button').attr('value', 'Save');
crop_data = undefined;
eventSource.emit(event_types.CHARACTER_EDITED, { detail: { id: this_chid, character: characters[this_chid] } });
await eventSource.emit(event_types.CHARACTER_EDITED, { detail: { id: this_chid, character: characters[this_chid] } });
// Recreate the chat if it hasn't been used at least once (i.e. with continue).
const message = getFirstMessage();
@ -8042,13 +8042,10 @@ async function createOrEditCharacter(e) {
await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, messageId);
await saveChatConditional();
}
},
error: function (jqXHR, exception) {
$('#create_button').removeAttr('disabled');
console.log('Error! Either a file with the same name already existed, or the image file provided was in an invalid format. Double check that the image is not a webp.');
} catch (error) {
console.log(error);
toastr.error('Something went wrong while saving the character, or the image file provided was in an invalid format. Double check that the image is not a webp.');
},
});
}
}
}