Merge pull request #2752 from SillyTavern/del-swipe-fix Fix deleting of swipe being inconsistent via button, matching the `delswipe` slash command now

daf8c827f7d2eeeac78b1f5035e469270d14f3ba

Wolfsblvt <wolfsblvt@gmail.com>

Signed
2 files changed, +54 -40Showing whitespace changes
public/script.js+50 -10
@@ -7454,6 +7454,53 @@ export function hideSwipeButtons() {
7454 $('#chat').find('.swipe_left').css('display', 'none');7454 $('#chat').find('.swipe_left').css('display', 'none');
7455}7455}
74567456
7457/**
7458 * Deletes a swipe from the chat.
7459 *
7460 * @param {number?} swipeId - The ID of the swipe to delete. If not provided, the current swipe will be deleted.
7461 * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion.
7462 */
7463export async function deleteSwipe(swipeId = null) {
7464 if (swipeId && (isNaN(swipeId) || swipeId < 0)) {
7465 toastr.warning('Invalid swipe ID: ' + swipeId);
7466 return;
7467 }
7468
7469 const lastMessage = chat[chat.length - 1];
7470 if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
7471 toastr.warning('No messages to delete swipes from.');
7472 return;
7473 }
7474
7475 if (lastMessage.swipes.length <= 1) {
7476 toastr.warning('Can\'t delete the last swipe.');
7477 return;
7478 }
7479
7480 swipeId = swipeId ?? lastMessage.swipe_id;
7481
7482 if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
7483 toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
7484 return;
7485 }
7486
7487 lastMessage.swipes.splice(swipeId, 1);
7488
7489 if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
7490 lastMessage.swipe_info.splice(swipeId, 1);
7491 }
7492
7493 // Select the next swip, or the one before if it was the last one
7494 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
7495 lastMessage.swipe_id = newSwipeId;
7496 lastMessage.mes = lastMessage.swipes[newSwipeId];
7497
7498 await saveChatConditional();
7499 await reloadCurrentChat();
7500
7501 return newSwipeId;
7502}
7503
7457export async function saveMetadata() {7504export async function saveMetadata() {
7458 if (selected_group) {7505 if (selected_group) {
7459 await editGroup(selected_group, true, false);7506 await editGroup(selected_group, true, false);
@@ -10277,19 +10324,12 @@ jQuery(async function () {
10277 if (deleteOnlySwipe) {10324 if (deleteOnlySwipe) {
10278 const message = chat[this_edit_mes_id];10325 const message = chat[this_edit_mes_id];
10279 const swipe_id = message.swipe_id;10326 const swipe_id = message.swipe_id;
10280 message.swipes.splice(swipe_id, 1);10327 await deleteSwipe(swipe_id);
10281 if (Array.isArray(message.swipe_info) && message.swipe_info.length) {10328 return;
10282 message.swipe_info.splice(swipe_id, 1);
10283 }
10284 if (swipe_id > 0) {
10285 $('.swipe_left:last').click();
10286 } else {
10287 $('.swipe_right:last').click();
10288 }10329 }
10289 } else {10330
10290 chat.splice(this_edit_mes_id, 1);10331 chat.splice(this_edit_mes_id, 1);
10291 messageElement.remove();10332 messageElement.remove();
10292 }
1029310333
10294 let startFromZero = Number(this_edit_mes_id) === 0;10334 let startFromZero = Number(this_edit_mes_id) === 0;
1029510335
public/scripts/slash-commands.js+4 -30
@@ -11,6 +11,7 @@ import {
11 comment_avatar,11 comment_avatar,
12 deactivateSendButtons,12 deactivateSendButtons,
13 default_avatar,13 default_avatar,
14 deleteSwipe,
14 eventSource,15 eventSource,
15 event_types,16 event_types,
16 extension_prompt_roles,17 extension_prompt_roles,
@@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) {
2309}2310}
23102311
2311async function deleteSwipeCallback(_, arg) {2312async function deleteSwipeCallback(_, arg) {
2312 const lastMessage = chat[chat.length - 1];2313 // Take the provided argument. Null if none provided, which will target the current swipe.
23132314 const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : null;
2314 if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
2315 toastr.warning('No messages to delete swipes from.');
2316 return '';
2317 }
2318
2319 if (lastMessage.swipes.length <= 1) {
2320 toastr.warning('Can\'t delete the last swipe.');
2321 return '';
2322 }
2323
2324 const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : lastMessage.swipe_id;
23252315
2326 if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {2316 const newSwipeId = await deleteSwipe(swipeId);
2327 toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
2328 return '';
2329 }
2330
2331 lastMessage.swipes.splice(swipeId, 1);
2332
2333 if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
2334 lastMessage.swipe_info.splice(swipeId, 1);
2335 }
2336
2337 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
2338 lastMessage.swipe_id = newSwipeId;
2339 lastMessage.mes = lastMessage.swipes[newSwipeId];
2340
2341 await saveChatConditional();
2342 await reloadCurrentChat();
23432317
2344 return String(newSwipeId);2318 return String(newSwipeId);
2345}2319}