Fix deleting swipes overwriting reasoning - Well, someone forgot about syncing extras and mes data again.... - Built the oppositive function of `syncMesToSwipe`, so we can now call `syncSwipeToMes` Fixes #3787

fef36bfc39b6dc636fe7eb0988a3bd4ec8f2ad72

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +64 -4Showing whitespace changes
public/script.js+64 -4
@@ -6261,7 +6261,6 @@ export function syncMesToSwipe(messageId = null) {
6261 }6261 }
62626262
6263 const targetMessage = chat[targetMessageId];6263 const targetMessage = chat[targetMessageId];
6264
6265 if (!targetMessage) {6264 if (!targetMessage) {
6266 return false;6265 return false;
6267 }6266 }
@@ -6295,6 +6294,68 @@ export function syncMesToSwipe(messageId = null) {
6295}6294}
62966295
6297/**6296/**
6297 * Syncs swipe data back to the message data at the given message ID (or the last message if no ID is given).
6298 * If the swipe ID is not provided, the current swipe ID in the message object is used.
6299 *
6300 * If the swipe data is invalid in some way, this function will exit out without doing anything.
6301 * @param {number?} [messageId=null] - The ID of the message to sync with the swipe data. If no ID is given, the last message is used.
6302 * @param {number?} [swipeId=null] - The ID of the swipe to sync. If no ID is given, the current swipe ID in the message object is used.
6303 * @returns {boolean} Whether the swipe data was successfully synced to the message
6304 */
6305export function syncSwipeToMes(messageId = null, swipeId = null) {
6306 if (!chat.length) {
6307 return false;
6308 }
6309
6310 const targetMessageId = messageId ?? chat.length - 1;
6311 if (targetMessageId >= chat.length || targetMessageId < 0) {
6312 console.warn(`[syncSwipeToMes] Invalid message ID: ${messageId}`);
6313 return false;
6314 }
6315
6316 const targetMessage = chat[targetMessageId];
6317 if (!targetMessage) {
6318 return false;
6319 }
6320
6321 if (swipeId !== null) {
6322 if (isNaN(swipeId) || swipeId < 0) {
6323 console.warn(`[syncSwipeToMes] Invalid swipe ID: ${swipeId}`);
6324 return false;
6325 }
6326 targetMessage.swipe_id = swipeId;
6327 }
6328
6329 // No swipe data there yet, exit out
6330 if (typeof targetMessage.swipe_id !== 'number') {
6331 return false;
6332 }
6333 // If swipes structure is invalid, exit out
6334 if (!Array.isArray(targetMessage.swipe_info) || !Array.isArray(targetMessage.swipes)) {
6335 return false;
6336 }
6337
6338 const targetSwipeId = targetMessage.swipe_id;
6339 if (!targetMessage.swipes[targetSwipeId] || !targetMessage.swipe_info[targetSwipeId]) {
6340 console.warn(`[syncSwipeToMes] Invalid swipe ID: ${targetSwipeId}`);
6341 return false;
6342 }
6343
6344 const targetSwipeInfo = targetMessage.swipe_info[targetSwipeId];
6345 if (typeof targetSwipeInfo !== 'object') {
6346 return false;
6347 }
6348
6349 targetMessage.mes = targetMessage.swipes[targetSwipeId];
6350 targetMessage.send_date = targetSwipeInfo.send_date;
6351 targetMessage.gen_started = targetSwipeInfo.gen_started;
6352 targetMessage.gen_finished = targetSwipeInfo.gen_finished;
6353 targetMessage.extra = structuredClone(targetSwipeInfo.extra);
6354
6355 return true;
6356}
6357
6358/**
6298 * Saves the image to the message object.6359 * Saves the image to the message object.
6299 * @param {ParsedImage} img Image object6360 * @param {ParsedImage} img Image object
6300 * @param {object} mes Chat message object6361 * @param {object} mes Chat message object
@@ -8293,10 +8354,9 @@ export async function deleteSwipe(swipeId = null) {
8293 lastMessage.swipe_info.splice(swipeId, 1);8354 lastMessage.swipe_info.splice(swipeId, 1);
8294 }8355 }
82958356
8296 // Select the next swip, or the one before if it was the last one8357 // Select the next swipe, or the one before if it was the last one
8297 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);8358 const newSwipeId = Math.min(swipeId, lastMessage.swipes.length - 1);
8298 lastMessage.swipe_id = newSwipeId;8359 syncSwipeToMes(null, newSwipeId);
8299 lastMessage.mes = lastMessage.swipes[newSwipeId];
83008360
8301 await saveChatConditional();8361 await saveChatConditional();
8302 await reloadCurrentChat();8362 await reloadCurrentChat();