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, +56 -42Ignore whitespace
public/script.js+52 -12
@@ -7454,6 +7454,53 @@ export function hideSwipeButtons() {
74547454 $('#chat').find('.swipe_left').css('display', 'none');
74557455}
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+ */
7463+export 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+
74577504export async function saveMetadata() {
74587505 if (selected_group) {
74597506 await editGroup(selected_group, true, false);
@@ -10277,20 +10324,13 @@ jQuery(async function () {
1027710324 if (deleteOnlySwipe) {
1027810325 const message = chat[this_edit_mes_id];
1027910326 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- }
10289- } else {
10290- chat.splice(this_edit_mes_id, 1);
10291- messageElement.remove();
1029210329 }
1029310330
10331+ chat.splice(this_edit_mes_id, 1);
10332+ messageElement.remove();
10333+
1029410334 let startFromZero = Number(this_edit_mes_id) === 0;
1029510335
1029610336 this_edit_mes_id = undefined;
public/scripts/slash-commands.js+4 -30
@@ -11,6 +11,7 @@ import {
1111 comment_avatar,
1212 deactivateSendButtons,
1313 default_avatar,
14+ deleteSwipe,
1415 eventSource,
1516 event_types,
1617 extension_prompt_roles,
@@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) {
23092310}
23102311
23112312async 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.
2314+ const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : null;
23132315
2314- if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {
2316+ const newSwipeId = await deleteSwipe(swipeId);
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;
2325-
2326- if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
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
23442318 return String(newSwipeId);
23452319}