mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 19:48:08 +00:00
feat: Improve image loading for thumbnails (#5823)
This commit is contained in:
committed by
GitHub
parent
e2059cfc5b
commit
d39ace5a6b
@@ -10,13 +10,14 @@ describe('Thumbnail.vue', () => {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
hasImageLoaded: true,
|
||||||
imgError: false,
|
imgError: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
expect(wrapper.find('.user-thumbnail').exists()).toBe(true);
|
expect(wrapper.find('.user-thumbnail').exists()).toBe(true);
|
||||||
const avatarComponent = wrapper.findComponent(Avatar);
|
const avatarComponent = wrapper.findComponent(Avatar);
|
||||||
expect(avatarComponent.exists()).toBe(false);
|
expect(avatarComponent.isVisible()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should render the avatar component if invalid image is passed', () => {
|
it('should render the avatar component if invalid image is passed', () => {
|
||||||
@@ -26,13 +27,14 @@ describe('Thumbnail.vue', () => {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
hasImageLoaded: true,
|
||||||
imgError: true,
|
imgError: true,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
expect(wrapper.find('.avatar-container').exists()).toBe(true);
|
expect(wrapper.find('#image').exists()).toBe(false);
|
||||||
const avatarComponent = wrapper.findComponent(Avatar);
|
const avatarComponent = wrapper.findComponent(Avatar);
|
||||||
expect(avatarComponent.exists()).toBe(true);
|
expect(avatarComponent.isVisible()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should the initial of the name if no image is passed', () => {
|
it('should the initial of the name if no image is passed', () => {
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
<template>
|
<template>
|
||||||
<div :class="thumbnailBoxClass" :style="{ height: size, width: size }">
|
<div :class="thumbnailBoxClass" :style="{ height: size, width: size }">
|
||||||
|
<!-- Using v-show instead of v-if to avoid flickering as v-if removes dom elements. -->
|
||||||
<img
|
<img
|
||||||
v-if="!imgError && src"
|
v-show="shouldShowImage"
|
||||||
:src="src"
|
:src="src"
|
||||||
:class="thumbnailClass"
|
:class="thumbnailClass"
|
||||||
|
@load="onImgLoad"
|
||||||
@error="onImgError"
|
@error="onImgError"
|
||||||
/>
|
/>
|
||||||
<Avatar
|
<Avatar
|
||||||
v-else
|
v-show="!shouldShowImage"
|
||||||
:username="userNameWithoutEmoji"
|
:username="userNameWithoutEmoji"
|
||||||
:class="thumbnailClass"
|
:class="thumbnailClass"
|
||||||
:size="avatarSize"
|
:size="avatarSize"
|
||||||
@@ -77,6 +79,7 @@ export default {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
hasImageLoaded: false,
|
||||||
imgError: false,
|
imgError: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -124,6 +127,15 @@ export default {
|
|||||||
const boxClass = this.variant === 'circle' ? 'is-rounded' : '';
|
const boxClass = this.variant === 'circle' ? 'is-rounded' : '';
|
||||||
return `user-thumbnail-box ${boxClass}`;
|
return `user-thumbnail-box ${boxClass}`;
|
||||||
},
|
},
|
||||||
|
shouldShowImage() {
|
||||||
|
if (!this.src) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (this.hasImageLoaded) {
|
||||||
|
return !this.imgError;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
src(value, oldValue) {
|
src(value, oldValue) {
|
||||||
@@ -136,6 +148,9 @@ export default {
|
|||||||
onImgError() {
|
onImgError() {
|
||||||
this.imgError = true;
|
this.imgError = true;
|
||||||
},
|
},
|
||||||
|
onImgLoad() {
|
||||||
|
this.hasImageLoaded = true;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
@@ -159,6 +174,7 @@ export default {
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
|
vertical-align: initial;
|
||||||
|
|
||||||
&.border {
|
&.border {
|
||||||
border: 1px solid white;
|
border: 1px solid white;
|
||||||
|
|||||||
Reference in New Issue
Block a user