Add placeholder aspect ratio
This commit is contained in:
parent
3cef9d10c9
commit
f4700635e3
|
|
@ -1,7 +1,9 @@
|
||||||
<template>
|
<template>
|
||||||
<wagtail-image
|
<wagtail-image
|
||||||
:src="value.path"
|
:src="value.url"
|
||||||
alt=""
|
alt=""
|
||||||
|
:original-height="value.height"
|
||||||
|
:original-width="value.width"
|
||||||
class="image-block"
|
class="image-block"
|
||||||
@click="openFullscreen"
|
@click="openFullscreen"
|
||||||
></wagtail-image>
|
></wagtail-image>
|
||||||
|
|
@ -15,7 +17,7 @@ export default {
|
||||||
components: { WagtailImage },
|
components: { WagtailImage },
|
||||||
methods: {
|
methods: {
|
||||||
openFullscreen() {
|
openFullscreen() {
|
||||||
this.$store.dispatch('showFullscreenImage', this.value.path);
|
this.$store.dispatch('showFullscreenImage', this.value.url);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,18 @@
|
||||||
<img
|
<img
|
||||||
:src="modifiedUrl"
|
:src="modifiedUrl"
|
||||||
:alt="alt"
|
:alt="alt"
|
||||||
class="wagtail-image__image"
|
:class="['wagtail-image__image', { loaded: loaded }]"
|
||||||
loading="eager"
|
loading="eager"
|
||||||
v-show="loaded"
|
v-show="loaded"
|
||||||
ref="imgElement"
|
ref="imgElement"
|
||||||
@load="loaded = true"
|
@load="handleLoad"
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
class="wagtail-image__placeholder"
|
class="wagtail-image__placeholder"
|
||||||
|
:style="placeholderStyle"
|
||||||
v-show="!loaded"
|
v-show="!loaded"
|
||||||
></div>
|
></div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
@ -22,6 +24,8 @@ import { ref, computed, onMounted } from 'vue';
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
src: String,
|
src: String,
|
||||||
alt: String(''),
|
alt: String(''),
|
||||||
|
originalWidth: Number,
|
||||||
|
originalHeight: Number,
|
||||||
});
|
});
|
||||||
|
|
||||||
const imgElement = ref(null);
|
const imgElement = ref(null);
|
||||||
|
|
@ -30,7 +34,6 @@ const height = ref(0);
|
||||||
const loaded = ref(false);
|
const loaded = ref(false);
|
||||||
|
|
||||||
const modifiedUrl = computed(() => {
|
const modifiedUrl = computed(() => {
|
||||||
console.log('x, y, hdpi, retina:', width.value, height.value, isHighDensity(), isRetina());
|
|
||||||
const density = isHighDensity() ? 2 : 1;
|
const density = isHighDensity() ? 2 : 1;
|
||||||
|
|
||||||
if (width.value) {
|
if (width.value) {
|
||||||
|
|
@ -52,6 +55,32 @@ const updateDimensions = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleLoad = () => {
|
||||||
|
loaded.value = true; // Set loaded to true when the image loads
|
||||||
|
};
|
||||||
|
|
||||||
|
const placeholderStyle = computed(() => {
|
||||||
|
const styles = {
|
||||||
|
width: '100%',
|
||||||
|
height: '100%',
|
||||||
|
};
|
||||||
|
if (width.value) {
|
||||||
|
const scalingFactor = width.value / props.originalWidth;
|
||||||
|
console.log(props.originalWidth, width.value);
|
||||||
|
const scaledHeight = Math.round(props.originalHeight * scalingFactor);
|
||||||
|
const scaledWidth = Math.round(props.originalWidth * scalingFactor);
|
||||||
|
|
||||||
|
if (props.originalWidth) {
|
||||||
|
styles.width = `${scaledWidth}px`;
|
||||||
|
}
|
||||||
|
if (props.originalHeight) {
|
||||||
|
styles.height = `${scaledHeight}px`;
|
||||||
|
}
|
||||||
|
console.log(styles);
|
||||||
|
return styles;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const isHighDensity = () => {
|
const isHighDensity = () => {
|
||||||
return (
|
return (
|
||||||
(window.matchMedia &&
|
(window.matchMedia &&
|
||||||
|
|
@ -88,12 +117,10 @@ onMounted(updateDimensions);
|
||||||
.wagtail-image {
|
.wagtail-image {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
|
||||||
|
|
||||||
&__image {
|
&__image {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: auto; /* Keep the image's aspect ratio intact */
|
//height: 100%;
|
||||||
min-height: 100%;
|
height: auto; /* Keep the image's aspect ratio intact */
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
&__placeholder {
|
&__placeholder {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue