Bug fix for swapping frames

This commit is contained in:
Dennis Postma 2025-04-06 03:14:33 +02:00
parent 08bb5ebc91
commit 702d4f38ea

View File

@ -441,14 +441,27 @@
const currentIndex = sprites.value.findIndex(sprite => sprite.id === id);
if (currentIndex === -1) return;
// Create a new sprites array
// If we're trying to move to the same position, do nothing
if (currentIndex === newIndex) return;
// Create a copy of the sprites array
const newSprites = [...sprites.value];
// Remove the sprite from its current position
const [movedSprite] = newSprites.splice(currentIndex, 1);
// Perform a swap between the two positions
if (newIndex < sprites.value.length) {
// Get references to both sprites
const movingSprite = { ...newSprites[currentIndex] };
const targetSprite = { ...newSprites[newIndex] };
// Insert the sprite at the new position
newSprites.splice(newIndex, 0, movedSprite);
// Swap them
newSprites[currentIndex] = targetSprite;
newSprites[newIndex] = movingSprite;
} else {
// If dragging to an empty cell (beyond the array length)
// Use the original reordering logic
const [movedSprite] = newSprites.splice(currentIndex, 1);
newSprites.splice(newIndex, 0, movedSprite);
}
// Update the sprites array
sprites.value = newSprites;