Skip to content

Commit

Permalink
feat(default-theme): Improve SwImage component when no thumbnail (#1936)
Browse files Browse the repository at this point in the history
  • Loading branch information
quando1910 committed Aug 12, 2022
1 parent 4a312ed commit 2c8b383
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/default-theme/src/cms/elements/CmsElementImage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-if="imagesUrl && imagesUrl.length">
<div v-if="(imagesUrl && imagesUrl.length) || imgUrl">
<SwLink
v-if="link"
:link="link"
Expand All @@ -8,6 +8,7 @@
>
<SwImage
:srcset="imagesUrl"
:src="imgUrl"
:title="title"
:alt="alt"
:loading="lazyLoad"
Expand All @@ -17,6 +18,7 @@
<SwImage
v-else
:srcset="imagesUrl"
:src="imgUrl"
:title="title"
:alt="alt"
:loading="lazyLoad"
Expand Down Expand Up @@ -61,6 +63,10 @@ export default {
return this.getMedia?.thumbnails ?? []
},
imgUrl() {
return getResizedImage(this.getMedia && this.getMedia.url)
},
lazyLoad() {
return "lazy"
},
Expand Down
13 changes: 10 additions & 3 deletions packages/default-theme/src/components/atoms/SwImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ export default {
resolution: item.width
})) ?? []
)
const imgUrl = computed(() => props.srcset?.reduce(function (res, thumb) {
return thumb.width < res.width ? thumb : res;
})?.url ?? props.src)
const imgUrl = computed(() => {
if (props.srcset && props.srcset.length) {
return props.srcset.reduce(
(res, thumb) => (thumb.width < res.width ? thumb : res),
props.srcset[0]
)?.url ?? props.src
} else {
return props.src
}
});
return {
placeholder: getPlaceholderImage(width, height),
Expand Down

0 comments on commit 2c8b383

Please sign in to comment.