| 7942 | 7942 | /** |
| 7943 | 7943 | * Deletes a swipe from the chat. |
| 7944 | 7944 | * |
| 7945 | 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 | 7947 | * @returns {Promise<number>|undefined} - The ID of the new swipe after deletion. |
| 7947 | 7948 | */ |
| 7948 | 7949 | export async function deleteSwipe(swipeId = null, messageId = chat.length - 1) { |
| 7949 | 7950 | if (swipeId && (isNaN(swipeId) || swipeId < 0)) { |
| 7950 | 7951 | toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`); |
| 7951 | 7952 | return; |
| 7952 | 7953 | } |
| 7953 | 7954 | |
| 7954 | 7955 | const lastMessagemessage = chat[chat.length - 1messageId]; |
| 7955 | 7956 | if (!lastMessagemessage || !Array.isArray(lastMessagemessage.swipes) || !lastMessagemessage.swipes.length) { |
| 7956 | 7957 | toastr.warning(t`No messages to delete swipes from.`); |
| 7957 | 7958 | return; |
| 7958 | 7959 | } |
| 7959 | 7960 | |
| 7960 | 7961 | if (lastMessagemessage.swipes.length <= 1) { |
| 7961 | 7962 | toastr.warning(t`Can't delete the last swipe.`); |
| 7962 | 7963 | return; |
| 7963 | 7964 | } |
| 7964 | 7965 | |
| 7965 | 7966 | swipeId = swipeId ?? lastMessagemessage.swipe_id; |
| 7966 | 7967 | |
| 7967 | 7968 | if (swipeId < 0 || swipeId >= lastMessagemessage.swipes.length) { |
| 7968 | 7969 | toastr.warning(t`Invalid swipe ID: ${swipeId + 1}`); |
| 7969 | 7970 | return; |
| 7970 | 7971 | } |
| 7971 | 7972 | |
| 7972 | 7973 | lastMessagemessage.swipes.splice(swipeId, 1); |
| 7973 | 7974 | |
| 7974 | 7975 | if (Array.isArray(lastMessagemessage.swipe_info) && lastMessagemessage.swipe_info.length) { |
| 7975 | 7976 | lastMessagemessage.swipe_info.splice(swipeId, 1); |
| 7976 | 7977 | } |
| 7977 | 7978 | |
| 7978 | 7979 | // Select the next swipe, or the one before if it was the last one |
| 7979 | 7980 | const newSwipeId = Math.min(swipeId, lastMessagemessage.swipes.length - 1); |
| 7980 | 7981 | syncSwipeToMes(nullmessageId, newSwipeId); |
| 7981 | 7982 | |
| 7982 | 7983 | await eventSource.emit(event_types.MESSAGE_SWIPE_DELETED, { messageId, swipeId, newSwipeId }); |
| 7983 | 7984 | |
| 7984 | 7985 | await saveChatConditional(); |
| 7985 | 7986 | await reloadCurrentChat(); |