Fix image deletion to remove files from disk
Critical fix for desktop mode: - Updated deleteSelectedImages() to actually delete image files from disk - Desktop mode now uses fileManager.deleteImage() to remove physical files - Added proper category detection (task/consequence) for file deletion - Enhanced error handling for file deletion operations - Added different success messages for desktop vs web mode - Maintains backward compatibility with web mode (storage-only deletion) Previously: - Images imported to images/tasks/ and images/consequences/ directories - Deletion only removed references from localStorage - Files remained on disk, causing clutter and storage waste Now: - Desktop deletion removes both file references AND physical files - Clean filesystem management with no orphaned files - Proper feedback about file vs reference deletion - Robust error handling for file system operations
This commit is contained in:
parent
d060f46292
commit
85ea7f8938
75
game.js
75
game.js
|
|
@ -1628,7 +1628,7 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
|
|||
});
|
||||
}
|
||||
|
||||
deleteSelectedImages(tabType = 'task') {
|
||||
async deleteSelectedImages(tabType = 'task') {
|
||||
const galleryId = tabType === 'task' ? 'task-images-gallery' : 'consequence-images-gallery';
|
||||
const gallery = document.getElementById(galleryId);
|
||||
const selectedItems = gallery.querySelectorAll('.image-item.selected');
|
||||
|
|
@ -1644,6 +1644,7 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
|
|||
|
||||
let customImages = this.dataManager.get('customImages') || { task: [], consequence: [] };
|
||||
let deletedCount = 0;
|
||||
let fileDeletedCount = 0;
|
||||
|
||||
// Handle both old array format and new object format
|
||||
if (Array.isArray(customImages)) {
|
||||
|
|
@ -1651,30 +1652,59 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
|
|||
customImages = { task: customImages, consequence: [] };
|
||||
}
|
||||
|
||||
selectedItems.forEach(item => {
|
||||
// If we're in desktop mode, also delete the actual files
|
||||
const isDesktop = this.fileManager && this.fileManager.isElectron;
|
||||
|
||||
for (const item of selectedItems) {
|
||||
const imagePath = item.dataset.imagePath;
|
||||
|
||||
// Find and remove from both categories
|
||||
['task', 'consequence'].forEach(category => {
|
||||
const initialLength = customImages[category].length;
|
||||
customImages[category] = customImages[category].filter(img => {
|
||||
if (typeof img === 'string') {
|
||||
return img !== imagePath; // Old format: direct string comparison
|
||||
} else {
|
||||
// New format: compare against cachedPath or originalName
|
||||
const imgPath = img.cachedPath || img.originalName;
|
||||
return imgPath !== imagePath;
|
||||
// Determine which category this image belongs to
|
||||
let imageCategory = 'task'; // default
|
||||
if (imagePath.includes('consequences') || imagePath.includes('consequence')) {
|
||||
imageCategory = 'consequence';
|
||||
} else if (imagePath.includes('tasks') || imagePath.includes('task')) {
|
||||
imageCategory = 'task';
|
||||
}
|
||||
|
||||
// Delete the actual file if in desktop mode
|
||||
if (isDesktop) {
|
||||
try {
|
||||
const fileDeleted = await this.fileManager.deleteImage(imagePath, imageCategory);
|
||||
if (fileDeleted) {
|
||||
fileDeletedCount++;
|
||||
deletedCount++; // File manager handles storage cleanup
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting file:', error);
|
||||
}
|
||||
} else {
|
||||
// Web mode - only remove from storage
|
||||
['task', 'consequence'].forEach(category => {
|
||||
const initialLength = customImages[category].length;
|
||||
customImages[category] = customImages[category].filter(img => {
|
||||
if (typeof img === 'string') {
|
||||
return img !== imagePath; // Old format: direct string comparison
|
||||
} else {
|
||||
// New format: compare against cachedPath or originalName
|
||||
const imgPath = img.cachedPath || img.originalName;
|
||||
return imgPath !== imagePath;
|
||||
}
|
||||
});
|
||||
|
||||
if (customImages[category].length < initialLength) {
|
||||
deletedCount++;
|
||||
}
|
||||
});
|
||||
|
||||
if (customImages[category].length < initialLength) {
|
||||
deletedCount++;
|
||||
}
|
||||
});
|
||||
}); if (deletedCount > 0) {
|
||||
}
|
||||
}
|
||||
|
||||
// Update storage if not in desktop mode (desktop mode handles this in deleteImage)
|
||||
if (!isDesktop && deletedCount > 0) {
|
||||
this.dataManager.set('customImages', customImages);
|
||||
|
||||
// Also clean up any disabled image entries for deleted images
|
||||
}
|
||||
|
||||
// Clean up disabled image entries for deleted images
|
||||
if (deletedCount > 0) {
|
||||
let disabledImages = this.dataManager.get('disabledImages') || [];
|
||||
const originalDisabledLength = disabledImages.length;
|
||||
|
||||
|
|
@ -1688,7 +1718,10 @@ ${usagePercent > 85 ? '⚠️ Storage getting full - consider deleting some imag
|
|||
this.dataManager.set('disabledImages', disabledImages);
|
||||
}
|
||||
|
||||
this.showNotification(`${deletedCount} custom image(s) deleted successfully!`, 'success');
|
||||
const message = isDesktop
|
||||
? `${fileDeletedCount} image file(s) and references deleted successfully!`
|
||||
: `${deletedCount} custom image(s) deleted successfully!`;
|
||||
this.showNotification(message, 'success');
|
||||
this.loadImageGallery(); // Refresh the gallery
|
||||
} else {
|
||||
this.showNotification('No custom images were selected. Default images cannot be deleted.', 'warning');
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
Loading…
Reference in New Issue