debian-mirror-gitlab/app/assets/javascripts/design_management/components/image.vue

145 lines
3.4 KiB
Vue
Raw Normal View History

2020-05-24 23:13:21 +05:30
<script>
import { GlIcon } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { throttle } from 'lodash';
2021-10-27 15:23:28 +05:30
import { DESIGN_MARK_APP_START, DESIGN_MAIN_IMAGE_OUTPUT } from '~/performance/constants';
import { performanceMarkAndMeasure } from '~/performance/utils';
2020-05-24 23:13:21 +05:30
export default {
components: {
GlIcon,
},
props: {
image: {
type: String,
required: false,
default: '',
},
name: {
type: String,
required: false,
default: '',
},
scale: {
type: Number,
required: false,
default: 1,
},
},
data() {
return {
baseImageSize: null,
imageStyle: null,
imageError: false,
};
},
watch: {
scale(val) {
this.zoom(val);
},
},
beforeDestroy() {
window.removeEventListener('resize', this.resizeThrottled, false);
},
mounted() {
2021-10-27 15:23:28 +05:30
if (!this.image) {
this.onImgLoad();
}
2020-05-24 23:13:21 +05:30
this.resizeThrottled = throttle(() => {
// NOTE: if imageStyle is set, then baseImageSize
// won't change due to resize. We must still emit a
// `resize` event so that the parent can handle
// resizes appropriately (e.g. for design_overlay)
this.setBaseImageSize();
}, 400);
window.addEventListener('resize', this.resizeThrottled, false);
},
methods: {
onImgLoad() {
requestIdleCallback(this.setBaseImageSize, { timeout: 1000 });
2021-11-11 11:23:49 +05:30
requestIdleCallback(this.setImageNaturalScale, { timeout: 1000 });
2021-10-27 15:23:28 +05:30
performanceMarkAndMeasure({
measures: [
{
name: DESIGN_MAIN_IMAGE_OUTPUT,
start: DESIGN_MARK_APP_START,
},
],
});
2020-05-24 23:13:21 +05:30
},
onImgError() {
this.imageError = true;
},
setBaseImageSize() {
const { contentImg } = this.$refs;
if (!contentImg || contentImg.offsetHeight === 0 || contentImg.offsetWidth === 0) return;
this.baseImageSize = {
height: contentImg.offsetHeight,
width: contentImg.offsetWidth,
};
this.onResize({ width: this.baseImageSize.width, height: this.baseImageSize.height });
},
2021-11-11 11:23:49 +05:30
setImageNaturalScale() {
const { contentImg } = this.$refs;
if (!contentImg) {
return;
}
const { naturalHeight, naturalWidth } = contentImg;
// In case image 404s
if (naturalHeight === 0 || naturalWidth === 0) {
return;
}
const { height, width } = this.baseImageSize;
this.$parent.$emit(
'setMaxScale',
Math.round(((height + width) / (naturalHeight + naturalWidth)) * 100) / 100,
);
},
2020-05-24 23:13:21 +05:30
onResize({ width, height }) {
this.$emit('resize', { width, height });
},
zoom(amount) {
if (amount === 1) {
this.imageStyle = null;
this.$nextTick(() => {
this.setBaseImageSize();
});
return;
}
const width = this.baseImageSize.width * amount;
const height = this.baseImageSize.height * amount;
this.imageStyle = {
width: `${width}px`,
height: `${height}px`,
};
this.onResize({ width, height });
},
},
};
</script>
<template>
2021-01-03 14:25:43 +05:30
<div class="gl-mx-auto gl-my-auto js-design-image">
<gl-icon v-if="imageError" class="gl-text-gray-200" name="media-broken" :size="48" />
2020-05-24 23:13:21 +05:30
<img
v-show="!imageError"
ref="contentImg"
class="mh-100"
:src="image"
:alt="name"
:style="imageStyle"
:class="{ 'img-fluid': !imageStyle }"
@error="onImgError"
@load="onImgLoad"
/>
</div>
</template>