Commit 7e38a91d authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '197325-design-view-better-resizing-support-for-zoomed-in-designs' into 'master'

Design view: better resizing support for zoomed-in designs

See merge request gitlab-org/gitlab!25406
parents 58ae718f 8383030a
...@@ -37,7 +37,11 @@ export default { ...@@ -37,7 +37,11 @@ export default {
this.onImgLoad(); this.onImgLoad();
this.resizeThrottled = _.throttle(() => { this.resizeThrottled = _.throttle(() => {
this.onWindowResize(); // 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); }, 400);
window.addEventListener('resize', this.resizeThrottled, false); window.addEventListener('resize', this.resizeThrottled, false);
}, },
...@@ -45,15 +49,6 @@ export default { ...@@ -45,15 +49,6 @@ export default {
onImgLoad() { onImgLoad() {
requestIdleCallback(this.setBaseImageSize, { timeout: 1000 }); requestIdleCallback(this.setBaseImageSize, { timeout: 1000 });
}, },
onWindowResize() {
const { contentImg } = this.$refs;
if (!contentImg) return;
this.onResize({
width: contentImg.offsetWidth,
height: contentImg.offsetHeight,
});
},
setBaseImageSize() { setBaseImageSize() {
const { contentImg } = this.$refs; const { contentImg } = this.$refs;
if (!contentImg || contentImg.offsetHeight === 0 || contentImg.offsetWidth === 0) return; if (!contentImg || contentImg.offsetHeight === 0 || contentImg.offsetWidth === 0) return;
...@@ -68,6 +63,13 @@ export default { ...@@ -68,6 +63,13 @@ export default {
this.$emit('resize', { width, height }); this.$emit('resize', { width, height });
}, },
zoom(amount) { zoom(amount) {
if (amount === 1) {
this.imageStyle = null;
this.$nextTick(() => {
this.setBaseImageSize();
});
return;
}
const width = this.baseImageSize.width * amount; const width = this.baseImageSize.width * amount;
const height = this.baseImageSize.height * amount; const height = this.baseImageSize.height * amount;
......
---
title: Improve zoom reset after resizing window in Design Management
merge_request: 25406
author:
type: added
...@@ -53,6 +53,9 @@ describe('Design management large image component', () => { ...@@ -53,6 +53,9 @@ describe('Design management large image component', () => {
}); });
describe('zoom', () => { describe('zoom', () => {
const baseImageWidth = 100;
const baseImageHeight = 100;
beforeEach(() => { beforeEach(() => {
createComponent( createComponent(
{ {
...@@ -62,22 +65,32 @@ describe('Design management large image component', () => { ...@@ -62,22 +65,32 @@ describe('Design management large image component', () => {
}, },
{ {
imageStyle: { imageStyle: {
width: '100px', width: `${baseImageWidth}px`,
height: '100px', height: `${baseImageHeight}px`,
}, },
baseImageSize: { baseImageSize: {
width: 100, width: baseImageWidth,
height: 100, height: baseImageHeight,
}, },
}, },
); );
jest
.spyOn(wrapper.vm.$refs.contentImg, 'offsetWidth', 'get')
.mockImplementation(() => baseImageWidth);
jest
.spyOn(wrapper.vm.$refs.contentImg, 'offsetHeight', 'get')
.mockImplementation(() => baseImageHeight);
}); });
it('emits @resize event on zoom', () => { it('emits @resize event on zoom', () => {
wrapper.vm.zoom(2); const zoomAmount = 2;
wrapper.vm.zoom(zoomAmount);
return wrapper.vm.$nextTick().then(() => { return wrapper.vm.$nextTick().then(() => {
expect(wrapper.emitted('resize')).toEqual([[{ width: 200, height: 200 }]]); expect(wrapper.emitted('resize')).toEqual([
[{ width: baseImageWidth * zoomAmount, height: baseImageHeight * zoomAmount }],
]);
}); });
}); });
...@@ -85,13 +98,19 @@ describe('Design management large image component', () => { ...@@ -85,13 +98,19 @@ describe('Design management large image component', () => {
wrapper.vm.zoom(1); wrapper.vm.zoom(1);
return wrapper.vm.$nextTick().then(() => { return wrapper.vm.$nextTick().then(() => {
expect(wrapper.emitted('resize')).toEqual([[{ width: 100, height: 100 }]]); expect(wrapper.emitted('resize')).toEqual([
[{ width: baseImageWidth, height: baseImageHeight }],
]);
}); });
}); });
it('sets image style when zoomed', () => { it('sets image style when zoomed', () => {
wrapper.vm.zoom(2); const zoomAmount = 2;
expect(wrapper.vm.imageStyle).toEqual({ width: '200px', height: '200px' }); wrapper.vm.zoom(zoomAmount);
expect(wrapper.vm.imageStyle).toEqual({
width: `${baseImageWidth * zoomAmount}px`,
height: `${baseImageHeight * zoomAmount}px`,
});
return wrapper.vm.$nextTick().then(() => { return wrapper.vm.$nextTick().then(() => {
expect(wrapper.element).toMatchSnapshot(); expect(wrapper.element).toMatchSnapshot();
}); });
......
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