Commit d79dd415 authored by Rajat Jain's avatar Rajat Jain

Zoom into design image upto 100% of actual size

Default zoom functionality does not take natural height
width in account to compute the zoom. Now we'd allow to zoom
in upto 100% actual size.

Changelog: changed
parent 10932045
<script> <script>
import { GlButtonGroup, GlButton } from '@gitlab/ui'; import { GlButtonGroup, GlButton } from '@gitlab/ui';
const SCALE_STEP_SIZE = 0.2;
const DEFAULT_SCALE = 1; const DEFAULT_SCALE = 1;
const MIN_SCALE = 1; const MIN_SCALE = 1;
const MAX_SCALE = 2; const ZOOM_LEVELS = 5;
export default { export default {
components: { components: {
GlButtonGroup, GlButtonGroup,
GlButton, GlButton,
}, },
props: {
maxScale: {
type: Number,
required: true,
},
},
data() { data() {
return { return {
scale: DEFAULT_SCALE, scale: DEFAULT_SCALE,
...@@ -24,7 +29,10 @@ export default { ...@@ -24,7 +29,10 @@ export default {
return this.scale === DEFAULT_SCALE; return this.scale === DEFAULT_SCALE;
}, },
disableIncrease() { disableIncrease() {
return this.scale >= MAX_SCALE; return this.scale >= this.maxScale;
},
stepSize() {
return (this.maxScale - MIN_SCALE) / ZOOM_LEVELS;
}, },
}, },
methods: { methods: {
...@@ -37,10 +45,10 @@ export default { ...@@ -37,10 +45,10 @@ export default {
this.$emit('scale', this.scale); this.$emit('scale', this.scale);
}, },
incrementScale() { incrementScale() {
this.setScale(this.scale + SCALE_STEP_SIZE); this.setScale(Math.min(this.scale + this.stepSize, this.maxScale));
}, },
decrementScale() { decrementScale() {
this.setScale(this.scale - SCALE_STEP_SIZE); this.setScale(Math.max(this.scale - this.stepSize, MIN_SCALE));
}, },
resetScale() { resetScale() {
this.setScale(DEFAULT_SCALE); this.setScale(DEFAULT_SCALE);
......
...@@ -57,6 +57,7 @@ export default { ...@@ -57,6 +57,7 @@ export default {
methods: { methods: {
onImgLoad() { onImgLoad() {
requestIdleCallback(this.setBaseImageSize, { timeout: 1000 }); requestIdleCallback(this.setBaseImageSize, { timeout: 1000 });
requestIdleCallback(this.setImageNaturalScale, { timeout: 1000 });
performanceMarkAndMeasure({ performanceMarkAndMeasure({
measures: [ measures: [
{ {
...@@ -79,6 +80,27 @@ export default { ...@@ -79,6 +80,27 @@ export default {
}; };
this.onResize({ width: this.baseImageSize.width, height: this.baseImageSize.height }); this.onResize({ width: this.baseImageSize.width, height: this.baseImageSize.height });
}, },
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,
);
},
onResize({ width, height }) { onResize({ width, height }) {
this.$emit('resize', { width, height }); this.$emit('resize', { width, height });
}, },
......
...@@ -46,6 +46,7 @@ import { ...@@ -46,6 +46,7 @@ import {
import { trackDesignDetailView, servicePingDesignDetailView } from '../../utils/tracking'; import { trackDesignDetailView, servicePingDesignDetailView } from '../../utils/tracking';
const DEFAULT_SCALE = 1; const DEFAULT_SCALE = 1;
const DEFAULT_MAX_SCALE = 2;
export default { export default {
components: { components: {
...@@ -96,6 +97,7 @@ export default { ...@@ -96,6 +97,7 @@ export default {
scale: DEFAULT_SCALE, scale: DEFAULT_SCALE,
resolvedDiscussionsExpanded: false, resolvedDiscussionsExpanded: false,
prevCurrentUserTodos: null, prevCurrentUserTodos: null,
maxScale: DEFAULT_MAX_SCALE,
}; };
}, },
apollo: { apollo: {
...@@ -328,6 +330,9 @@ export default { ...@@ -328,6 +330,9 @@ export default {
toggleResolvedComments() { toggleResolvedComments() {
this.resolvedDiscussionsExpanded = !this.resolvedDiscussionsExpanded; this.resolvedDiscussionsExpanded = !this.resolvedDiscussionsExpanded;
}, },
setMaxScale(event) {
this.maxScale = 1 / event;
},
}, },
createImageDiffNoteMutation, createImageDiffNoteMutation,
DESIGNS_ROUTE_NAME, DESIGNS_ROUTE_NAME,
...@@ -376,12 +381,13 @@ export default { ...@@ -376,12 +381,13 @@ export default {
@openCommentForm="openCommentForm" @openCommentForm="openCommentForm"
@closeCommentForm="closeCommentForm" @closeCommentForm="closeCommentForm"
@moveNote="onMoveNote" @moveNote="onMoveNote"
@setMaxScale="setMaxScale"
/> />
<div <div
class="design-scaler-wrapper gl-absolute gl-mb-6 gl-display-flex gl-justify-content-center gl-align-items-center" class="design-scaler-wrapper gl-absolute gl-mb-6 gl-display-flex gl-justify-content-center gl-align-items-center"
> >
<design-scaler @scale="scale = $event" /> <design-scaler :max-scale="maxScale" @scale="scale = $event" />
</div> </div>
</div> </div>
<design-sidebar <design-sidebar
......
...@@ -13,7 +13,11 @@ describe('Design management design scaler component', () => { ...@@ -13,7 +13,11 @@ describe('Design management design scaler component', () => {
const setScale = (scale) => wrapper.vm.setScale(scale); const setScale = (scale) => wrapper.vm.setScale(scale);
const createComponent = () => { const createComponent = () => {
wrapper = shallowMount(DesignScaler); wrapper = shallowMount(DesignScaler, {
propsData: {
maxScale: 2,
},
});
}; };
beforeEach(() => { beforeEach(() => {
...@@ -61,6 +65,18 @@ describe('Design management design scaler component', () => { ...@@ -61,6 +65,18 @@ describe('Design management design scaler component', () => {
expect(wrapper.emitted('scale')).toEqual([[1.2]]); expect(wrapper.emitted('scale')).toEqual([[1.2]]);
}); });
it('computes & increments correct stepSize based on maxScale', async () => {
wrapper.setProps({ maxScale: 11 });
await wrapper.vm.$nextTick();
getIncreaseScaleButton().vm.$emit('click');
await wrapper.vm.$nextTick();
expect(wrapper.emitted().scale[0][0]).toBe(3);
});
describe('when `scale` value is 1', () => { describe('when `scale` value is 1', () => {
it('disables the "reset" button', () => { it('disables the "reset" button', () => {
const resetButton = getResetScaleButton(); const resetButton = getResetScaleButton();
...@@ -77,7 +93,7 @@ describe('Design management design scaler component', () => { ...@@ -77,7 +93,7 @@ describe('Design management design scaler component', () => {
}); });
}); });
describe('when `scale` value is 2 (maximum)', () => { describe('when `scale` value is maximum', () => {
beforeEach(async () => { beforeEach(async () => {
setScale(2); setScale(2);
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
......
...@@ -25,7 +25,9 @@ exports[`Design management design index page renders design index 1`] = ` ...@@ -25,7 +25,9 @@ exports[`Design management design index page renders design index 1`] = `
<div <div
class="design-scaler-wrapper gl-absolute gl-mb-6 gl-display-flex gl-justify-content-center gl-align-items-center" class="design-scaler-wrapper gl-absolute gl-mb-6 gl-display-flex gl-justify-content-center gl-align-items-center"
> >
<design-scaler-stub /> <design-scaler-stub
maxscale="2"
/>
</div> </div>
</div> </div>
...@@ -186,7 +188,9 @@ exports[`Design management design index page with error GlAlert is rendered in c ...@@ -186,7 +188,9 @@ exports[`Design management design index page with error GlAlert is rendered in c
<div <div
class="design-scaler-wrapper gl-absolute gl-mb-6 gl-display-flex gl-justify-content-center gl-align-items-center" class="design-scaler-wrapper gl-absolute gl-mb-6 gl-display-flex gl-justify-content-center gl-align-items-center"
> >
<design-scaler-stub /> <design-scaler-stub
maxscale="2"
/>
</div> </div>
</div> </div>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment