Skip to content

Commit

Permalink
Add a remove button
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb committed Nov 16, 2024
1 parent aa95f63 commit d6d8da9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/app/photo-collage/photo-collage.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,12 @@ canvas {
transform: scale(1.1);
background: white;
}

.remove-btn {
background: #ff4444;
color: white;
}

.remove-btn:hover {
background: #ff6666;
}
3 changes: 3 additions & 0 deletions src/app/photo-collage/photo-collage.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<button (click)="moveToBack(hoveredPhoto)" class="control-btn"></button>
<button (click)="moveToFront(hoveredPhoto)" class="control-btn"></button>
</div>
<div class="controls-row">
<button (click)="removePhoto(hoveredPhoto)" class="control-btn remove-btn">×</button>
</div>
</div>
</div>
<input #fileInput
Expand Down
17 changes: 17 additions & 0 deletions src/app/photo-collage/photo-collage.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,23 @@ export class PhotoCollageComponent implements AfterViewInit, OnDestroy {
this.render();
}

removePhoto(photo: PhotoElement) {
// Find and remove the photo from the array
const index = this.photos.findIndex(p => p.id === photo.id);
if (index !== -1) {
// Remove the photo's object URL to prevent memory leaks
URL.revokeObjectURL(photo.url);
this.photos.splice(index, 1);

// Reset hovered photo if it was the removed one
if (this.hoveredPhoto && this.hoveredPhoto.id === photo.id) {
this.hoveredPhoto = null;
}

this.render();
}
}

private applyGridLayout() {
if (!this.photos.length) return;

Expand Down

0 comments on commit d6d8da9

Please sign in to comment.