Merge pull request #2752 from SillyTavern/del-swipe-fix Fix deleting of swipe being inconsistent via button, matching the `delswipe` slash command now
Signed| @@ -7454,6 +7454,53 @@ export function hideSwipeButtons() { | ||
| 7454 | 7454 | $('#chat').find('.swipe_left').css('display', 'none'); |
| 7455 | 7455 | } |
| 7456 | 7456 | |
| 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 | + | |
| 7457 | 7504 | export async function saveMetadata() { |
| 7458 | 7505 | if (selected_group) { |
| 7459 | 7506 | await editGroup(selected_group, true, false); |
| @@ -10277,20 +10324,13 @@ jQuery(async function () { | ||
| 10277 | 10324 | if (deleteOnlySwipe) { |
| 10278 | 10325 | const message = chat[this_edit_mes_id]; |
| 10279 | 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 | - } | |
| 10289 | - } else { | |
| 10290 | - chat.splice(this_edit_mes_id, 1); | |
| 10291 | - messageElement.remove(); | |
| 10292 | 10329 | } |
| 10293 | 10330 | |
| 10331 | + chat.splice(this_edit_mes_id, 1); | |
| 10332 | + messageElement.remove(); | |
| 10333 | + | |
| 10294 | 10334 | let startFromZero = Number(this_edit_mes_id) === 0; |
| 10295 | 10335 | |
| 10296 | 10336 | this_edit_mes_id = undefined; |
| @@ -11,6 +11,7 @@ import { | ||
| 11 | 11 | comment_avatar, |
| 12 | 12 | deactivateSendButtons, |
| 13 | 13 | default_avatar, |
| 14 | + deleteSwipe, | |
| 14 | 15 | eventSource, |
| 15 | 16 | event_types, |
| 16 | 17 | extension_prompt_roles, |
| @@ -2309,37 +2310,10 @@ async function addSwipeCallback(args, value) { | ||
| 2309 | 2310 | } |
| 2310 | 2311 | |
| 2311 | 2312 | async 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; | |
| 2313 | 2315 | |
| 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(); | |
| 2343 | 2317 | |
| 2344 | 2318 | return String(newSwipeId); |
| 2345 | 2319 | } |