deleteSwipe now emits the deleted message Id. (#4609) * deleteSwipe now emits the deleted message Id. * Fixed: this_edit_mes_id can be a string (don't ask). Either wrap it in parseInt or Number function https://github.com/SillyTavern/SillyTavern/pull/4609#discussion_r2405534766 I think it's better to use messageId in naming. Most of weirdly abbreviated params and variables come way back from TavernAI code. https://github.com/SillyTavern/SillyTavern/pull/4609#discussion_r2405537550 * Revert outside number conversions --------- Co-authored-by: user <user@exmaple.com> Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

befb2ffb78612198ca1bf95b10f84e55ba027e15

DeclineThyself <FallenHaze@tutamail.com>

Signed
1 files changed, +15 -14Ignore whitespace
public/script.js+15 -14
@@ -7942,44 +7942,45 @@ export function hideSwipeButtons() {
7942/**7942/**
7943 * Deletes a swipe from the chat.7943 * Deletes a swipe from the chat.
7944 *7944 *
7945 * @param {number?} swipeId - The ID of the swipe to delete. If not provided, the current swipe will be deleted.7945 * @param {number?} [swipeId = null] - The ID of the swipe to delete. If not provided, the current swipe will be deleted.
7946 * @param {number?} [messageId = chat.length - 1] - The ID of the message to delete from. If not provided, the last message will be targeted.
7946 * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion.7947 * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion.
7947 */7948 */
7948export async function deleteSwipe(swipeId = null) {7949export async function deleteSwipe(swipeId = null, messageId = chat.length - 1) {
7949 if (swipeId && (isNaN(swipeId) || swipeId < 0)) {7950 if (swipeId && (isNaN(swipeId) || swipeId < 0)) {
7950 toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`);7951 toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`);
7951 return;7952 return;
7952 }7953 }
79537954
7954 const lastMessage = chat[chat.length - 1];7955 const message = chat[messageId];
7955 if (!lastMessage || !Array.isArray(lastMessage.swipes) || !lastMessage.swipes.length) {7956 if (!message || !Array.isArray(message.swipes) || !message.swipes.length) {
7956 toastr.warning(t`No messages to delete swipes from.`);7957 toastr.warning(t`No messages to delete swipes from.`);
7957 return;7958 return;
7958 }7959 }
79597960
7960 if (lastMessage.swipes.length <= 1) {7961 if (message.swipes.length <= 1) {
7961 toastr.warning(t`Can't delete the last swipe.`);7962 toastr.warning(t`Can't delete the last swipe.`);
7962 return;7963 return;
7963 }7964 }
79647965
7965 swipeId = swipeId ?? lastMessage.swipe_id;7966 swipeId = swipeId ?? message.swipe_id;
79667967
7967 if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {7968 if (swipeId < 0 || swipeId >= message.swipes.length) {
7968 toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`);7969 toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`);
7969 return;7970 return;
7970 }7971 }
79717972
7972 lastMessage.swipes.splice(swipeId, 1);7973 message.swipes.splice(swipeId, 1);
79737974
7974 if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {7975 if (Array.isArray(message.swipe_info) && message.swipe_info.length) {
7975 lastMessage.swipe_info.splice(swipeId, 1);7976 message.swipe_info.splice(swipeId, 1);
7976 }7977 }
79777978
7978 // Select the next swipe, or the one before if it was the last one7979 // Select the next swipe, or the one before if it was the last one
7979 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);7980 const newSwipeId = Math.min(swipeId, message.swipes.length - 1);
7980 syncSwipeToMes(null, newSwipeId);7981 syncSwipeToMes(messageId, newSwipeId);
79817982
7982 await eventSource.emit(event_types.MESSAGE_SWIPE_DELETED, { swipeId, newSwipeId });7983 await eventSource.emit(event_types.MESSAGE_SWIPE_DELETED, { messageId, swipeId, newSwipeId });
79837984
7984 await saveChatConditional();7985 await saveChatConditional();
7985 await reloadCurrentChat();7986 await reloadCurrentChat();
@@ -10486,7 +10487,7 @@ jQuery(async function () {
10486 if (deleteOnlySwipe) {10487 if (deleteOnlySwipe) {
10487 const message = chat[this_edit_mes_id];10488 const message = chat[this_edit_mes_id];
10488 const swipe_id = message.swipe_id;10489 const swipe_id = message.swipe_id;
10489 await deleteSwipe(swipe_id);10490 await deleteSwipe(swipe_id, Number(this_edit_mes_id));
10490 return;10491 return;
10491 }10492 }
1049210493