viewer: fix deletion of last photo (fix #269)

cap
Varun Patil 2022-11-30 07:08:38 -08:00
parent 50fcbb4a7a
commit b038ab3091
1 changed files with 22 additions and 4 deletions

View File

@ -814,24 +814,42 @@ export default class Viewer extends Mixins(GlobalMixin) {
/** Delete this photo and refresh */ /** Delete this photo and refresh */
private async deleteCurrent() { private async deleteCurrent() {
const idx = this.photoswipe.currIndex - this.globalAnchor; let idx = this.photoswipe.currIndex - this.globalAnchor;
const photo = this.list[idx];
if (!photo) return;
// Delete with WebDAV // Delete with WebDAV
try { try {
this.updateLoading(1); this.updateLoading(1);
for await (const p of dav.deletePhotos([this.list[idx]])) { for await (const p of dav.deletePhotos([photo])) {
if (!p[0]) return; if (!p[0]) return;
} }
} finally { } finally {
this.updateLoading(-1); this.updateLoading(-1);
} }
const spliced = this.list.splice(idx, 1); // Remove from main view
this.deleted([photo]);
// If this is the only photo, close viewer
if (this.list.length === 1) {
return this.close();
}
// If this is the last photo, move to the previous photo first
// https://github.com/pulsejet/memories/issues/269
if (idx === this.list.length - 1) {
this.photoswipe.prev();
// Some photos might lazy load, so recompute idx for the next element
idx = this.photoswipe.currIndex + 1 - this.globalAnchor;
}
this.list.splice(idx, 1);
this.globalCount--; this.globalCount--;
for (let i = idx - 3; i <= idx + 3; i++) { for (let i = idx - 3; i <= idx + 3; i++) {
this.photoswipe.refreshSlideContent(i + this.globalAnchor); this.photoswipe.refreshSlideContent(i + this.globalAnchor);
} }
this.deleted(spliced);
} }
/** Is the current photo a favorite */ /** Is the current photo a favorite */