Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Gallery opening other image after closing #27957

Merged
merged 18 commits into from
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions apps/meteor/app/ui/client/views/app/photoswipeContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const initGallery = async (items: Slide[], options: PhotoSwipe.Options): Promise
import('./photoswipeContent.html'),
]);

Blaze.render(Template.photoswipeContent, document.body);
const view = Blaze.render(Template.photoswipeContent, document.body);

if (!currentGallery) {
const container = document.getElementById('pswp');
Expand All @@ -91,6 +91,7 @@ const initGallery = async (items: Slide[], options: PhotoSwipe.Options): Promise
currentGallery = new PhotoSwipe(container, PhotoSwipeUIDefault, items, options);

currentGallery.listen('destroy', () => {
Blaze.remove(view);
currentGallery = null;
});

Expand Down Expand Up @@ -121,38 +122,49 @@ const createEventListenerFor =

const { currentTarget } = event;

Array.from(document.querySelectorAll(className))
.sort((a, b) => {
if (a === currentTarget) {
return -1;
}

if (b === currentTarget) {
return 1;
}

return 0;
})
.map((element) => fromElementToSlide(element))
.reduce(
(p, curr) =>
p
.then(() => curr)
.then(async (slide) => {
if (!slide) {
return;
}
const galleryItems = Array.from(document.querySelectorAll(className));

if (!currentGallery) {
return initGallery([slide], defaultGalleryOptions);
}
const sortedElements = galleryItems.sort((a, b) => {
if (a === currentTarget) {
return -1;
}

currentGallery.items.push(slide);
currentGallery.invalidateCurrItems();
currentGallery.updateSize(true);
}),
Promise.resolve(),
);
if (b === currentTarget) {
return 1;
}

return 0;
});

const slidePromises = sortedElements.map((element) => fromElementToSlide(element));

let hasOpenedGallery = false;

slidePromises.reduce(
(p, curr) =>
p
.then(() => curr)
.then(async (slide) => {
if (!slide) {
return;
}

if (!currentGallery) {
// If the gallery doesn't exist and has been opened this run the user closed it before all promises ran.
// This means it shouldn't be opened again.
if (hasOpenedGallery) {
return;
}
hasOpenedGallery = true;
return initGallery([slide], defaultGalleryOptions);
}

currentGallery.items.push(slide);
currentGallery.invalidateCurrItems();
currentGallery.updateSize(true);
}),
Promise.resolve(),
);
};

Meteor.startup(() => {
Expand Down