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
1 changed files with 132 additions and 135 deletions

View File

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