[O] Fix photoswipe dimension requirement in a better way

https://github.com/dimsemenov/PhotoSwipe/issues/741#issuecomment-568915867
This commit is contained in:
Hykilpikonna
2019-12-25 12:26:28 -05:00
parent b8471a6e6e
commit e77e2bbf99
3 changed files with 28 additions and 42 deletions
+4 -37
View File
@@ -15,9 +15,6 @@ class Artwork
format: string; format: string;
description: string; description: string;
width: number;
height: number;
constructor(date: string, title: string, format: string, description: string) constructor(date: string, title: string, format: string, description: string)
{ {
this.rawDate = date; this.rawDate = date;
@@ -52,23 +49,6 @@ class Artwork
return './404.jpg' return './404.jpg'
} }
} }
loadDimensions()
{
let img = new Image();
img.onload = () =>
{
this.width = img.width;
this.height = img.height;
};
img.src = this.imgFull;
return this;
}
get isLoaded()
{
return this.width != undefined && this.height != undefined;
}
} }
@Component({components: {PhotoSwipper}}) @Component({components: {PhotoSwipper}})
@@ -83,9 +63,6 @@ export default class App extends Vue
// Responsive view // Responsive view
responsive = new Responsive(30, 2); responsive = new Responsive(30, 2);
// Loaded
loaded = false;
/** /**
* Initialize * Initialize
*/ */
@@ -103,17 +80,11 @@ export default class App extends Vue
if (a.format == null) a.format = config.artwork.default_format; if (a.format == null) a.format = config.artwork.default_format;
// Add it // Add it
this.artworks.push(new Artwork(a.date, a.title, a.format, a.description).loadDimensions()); this.artworks.push(new Artwork(a.date, a.title, a.format, a.description));
}); });
// Sort by date // Sort by date
this.artworks.sort((a, b) => b.date.getTime() - a.date.getTime()); this.artworks.sort((a, b) => b.date.getTime() - a.date.getTime());
// Check loaded
pWaitFor(() => this.artworks.every(a => a.isLoaded)).then(() =>
{
this.loaded = true;
});
} }
/** /**
@@ -151,25 +122,21 @@ export default class App extends Vue
} }
} }
get swipeItems() swipeItems()
{ {
return this.artworks.map(a => return this.artworks.map(a =>
{ {
return { return {
src: a.imgFull, src: a.imgFull,
alt: a.title, alt: a.title,
title: a.description ? a.title + ': ' + a.description : a.title, title: a.description ? a.title + ': ' + a.description : a.title
w: a.width,
h: a.height
} }
}) })
} }
onImageClick(index: number) onImageClick(index: number)
{ {
if (!this.loaded) return;
// @ts-ignore // @ts-ignore
this.$refs.ps.show(this.swipeItems, {index: index}); this.$refs.ps.show(this.swipeItems(), {index: index});
} }
} }
+1 -1
View File
@@ -18,7 +18,7 @@
</el-card> </el-card>
</div> </div>
<photo-swipper v-if="loaded" ref="ps"/> <photo-swipper ref="ps"/>
<div id="footer" :style="footerStyle"> <div id="footer" :style="footerStyle">
+23 -4
View File
@@ -59,14 +59,33 @@
@Component @Component
export default class PhotoSwipper extends Vue export default class PhotoSwipper extends Vue
{ {
ps: PhotoSwipe<any>;
show(items: any[], options: any) show(items: any[], options: any)
{ {
let ps = new PhotoSwipe(this.$refs.pswp as any, PhotoSwipeUI_Default, items, options); this.update(items);
ps.init(); this.ps = new PhotoSwipe(this.$refs.pswp as any, PhotoSwipeUI_Default, items, options);
this.ps.init();
window.addEventListener("resize", () => this.update(this.ps.items));
}
update(items: PhotoSwipe.Item[])
{
// Update inner width and inner height
items.forEach(item =>
{
item.w = window.innerWidth;
item.h = window.innerHeight;
})
} }
} }
</script> </script>
<style lang="scss" scoped> <style lang="scss">
.pswp img
{
max-width: none !important;
object-fit: contain !important;
}
</style> </style>