Merge pull request #2752 from SillyTavern/del-swipe-fix

Fix deleting of swipe being inconsistent via button, matching the `delswipe` slash command now
This commit is contained in:
Wolfsblvt 2024-09-02 00:01:37 +02:00 committed by GitHub
commit daf8c827f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 42 deletions

View File

@ -7454,6 +7454,53 @@ export function hideSwipeButtons() {
$('#chat').find('.swipe_left').css('display', 'none');
}
/**
* Deletes a swipe from the chat.
*
* @param {number?} swipeId - The ID of the swipe to delete. If not provided, the current swipe will be deleted.
* @returns {Promise<number>|undefined} - The ID of the new swipe after deletion.
*/
export async function deleteSwipe(swipeId = null) {
if (swipeId && (isNaN(swipeId) || swipeId < 0)) {
toastr.warning('Invalid swipe ID: ' + swipeId);
return;
}
const lastMessage = chat[chat.length - 1];
if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
toastr.warning('No messages to delete swipes from.');
return;
}
if (lastMessage.swipes.length <= 1) {
toastr.warning('Can\'t delete the last swipe.');
return;
}
swipeId = swipeId ?? lastMessage.swipe_id;
if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
return;
}
lastMessage.swipes.splice(swipeId, 1);
if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
lastMessage.swipe_info.splice(swipeId, 1);
}
// Select the next swip, or the one before if it was the last one
const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
lastMessage.swipe_id = newSwipeId;
lastMessage.mes = lastMessage.swipes[newSwipeId];
await saveChatConditional();
await reloadCurrentChat();
return newSwipeId;
}
export async function saveMetadata() {
if (selected_group) {
await editGroup(selected_group, true, false);
@ -10277,20 +10324,13 @@ jQuery(async function () {
if (deleteOnlySwipe) {
const message = chat[this_edit_mes_id];
const swipe_id = message.swipe_id;
message.swipes.splice(swipe_id, 1);
if (Array.isArray(message.swipe_info) && message.swipe_info.length) {
message.swipe_info.splice(swipe_id, 1);
}
if (swipe_id > 0) {
$('.swipe_left:last').click();
} else {
$('.swipe_right:last').click();
}
} else {
chat.splice(this_edit_mes_id, 1);
messageElement.remove();
await deleteSwipe(swipe_id);
return;
}
chat.splice(this_edit_mes_id, 1);
messageElement.remove();
let startFromZero = Number(this_edit_mes_id) === 0;
this_edit_mes_id = undefined;

View File

@ -11,6 +11,7 @@ import {
comment_avatar,
deactivateSendButtons,
default_avatar,
deleteSwipe,
eventSource,
event_types,
extension_prompt_roles,
@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) {
}
async function deleteSwipeCallback(_, arg) {
const lastMessage = chat[chat.length - 1];
// Take the provided argument. Null if none provided, which will target the current swipe.
const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : null;
if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
toastr.warning('No messages to delete swipes from.');
return '';
}
if (lastMessage.swipes.length <= 1) {
toastr.warning('Can\'t delete the last swipe.');
return '';
}
const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : lastMessage.swipe_id;
if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
return '';
}
lastMessage.swipes.splice(swipeId, 1);
if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
lastMessage.swipe_info.splice(swipeId, 1);
}
const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
lastMessage.swipe_id = newSwipeId;
lastMessage.mes = lastMessage.swipes[newSwipeId];
await saveChatConditional();
await reloadCurrentChat();
const newSwipeId = await deleteSwipe(swipeId);
return String(newSwipeId);
}