refactor(gallery): load PhotoSwipe lib in HTML

This commit is contained in:
Jimmy Cai 2020-12-22 19:13:11 +01:00
parent bf12e3b63b
commit 9acba47e08
No known key found for this signature in database
GPG Key ID: 3EA408E527F37B18
2 changed files with 14 additions and 22 deletions

View File

@ -21,16 +21,16 @@ class StackGallery {
private items: PhotoSwipeItem[] = [];
constructor(container: HTMLElement, galleryUID = 1) {
if (window.PhotoSwipe == undefined) {
console.error("PhotoSwipe lib not loaded.");
return;
}
this.galleryUID = galleryUID;
StackGallery.createGallery(container);
this.loadItems(container);
StackGallery.loadPS().then(() => {
const pswp = document.querySelector('.pswp') as HTMLDivElement;
pswp.style.removeProperty('display');
this.bindClick();
});
this.bindClick();
}
private loadItems(container: HTMLElement) {
@ -109,20 +109,6 @@ class StackGallery {
}
}
/**
* Load PhotoSwipe library dynamically
*/
public static loadPS() {
const tasks = [
loadScript("https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"),
loadScript("https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"),
loadStyle("https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"),
loadStyle("https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css")
];
return Promise.all(tasks);
}
public open(index: number) {
const pswp = document.querySelector('.pswp') as HTMLDivElement;
const ps = new window.PhotoSwipe(pswp, window.PhotoSwipeUI_Default, this.items, {

View File

@ -1,5 +1,5 @@
<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true" style="display:none">
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Background of PhotoSwipe.
It's a separate element as animating opacity is faster than rgba(). -->
@ -64,3 +64,9 @@
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.css">