Skip to content

Commit 7ad4330

Browse files
authored
Merge pull request #69 from magento-tsg-csl3/MC-41569
MC-41569: "Image Size" sporadically changing from "%" to pixels
2 parents 4d96ece + 83d3292 commit 7ad4330

File tree

3 files changed

+85
-8
lines changed
  • app/code/Magento/PageBuilder/view/adminhtml/web
  • dev/tests/js/jasmine/tests/app/code/Magento/PageBuilder/view/adminhtml/web/js/utils

3 files changed

+85
-8
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/utils/editor.js

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/utils/editor.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,18 @@ export function parseAttributesString(attributes: string): { [key: string]: stri
173173
*/
174174
export function lockImageSize(element: HTMLElement) {
175175
[].slice.call(element.querySelectorAll("img")).forEach((image: HTMLImageElement) => {
176-
image.style.width = `${image.width}px`;
177-
image.style.height = `${image.height}px`;
176+
if (image.style.width.length === 0) {
177+
image.style.width = /^\d+$/.test(image.getAttribute("width")) ?
178+
image.getAttribute("width") + "px" :
179+
image.getAttribute("width");
180+
image.setAttribute("data-width-locked", "true");
181+
}
182+
if (image.style.height.length === 0) {
183+
image.style.height = /^\d+$/.test(image.getAttribute("height")) ?
184+
image.getAttribute("height") + "px" :
185+
image.getAttribute("height");
186+
image.setAttribute("data-height-locked", "true");
187+
}
178188
});
179189
}
180190

@@ -185,8 +195,14 @@ export function lockImageSize(element: HTMLElement) {
185195
*/
186196
export function unlockImageSize(element: HTMLElement) {
187197
[].slice.call(element.querySelectorAll("img")).forEach((image: HTMLImageElement) => {
188-
image.style.width = null;
189-
image.style.height = null;
198+
if (image.getAttribute("data-width-locked")) {
199+
image.style.width = null;
200+
image.removeAttribute("data-width-locked");
201+
}
202+
if (image.getAttribute("data-height-locked")) {
203+
image.style.height = null;
204+
image.removeAttribute("data-height-locked");
205+
}
190206
});
191207
}
192208

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint-disable max-nested-callbacks */
7+
define([
8+
'Magento_PageBuilder/js/utils/editor',
9+
'jquery'
10+
], function (utils, $) {
11+
'use strict';
12+
13+
describe('Magento_PageBuilder/js/utils/editor.js', function () {
14+
describe('lockImageSize', function () {
15+
it('Should not change the defined image sizes', function () {
16+
var image = document.createElement('img'),
17+
element = document.createElement('div');
18+
19+
image.setAttribute('width', '100%');
20+
$(element).append(image);
21+
22+
utils.lockImageSize(element);
23+
expect(image.style.width).toEqual('100%');
24+
});
25+
});
26+
27+
describe('unlockImageSize', function () {
28+
it('Should unlock locked images sizes only', function () {
29+
var image = document.createElement('img'),
30+
element = document.createElement('div');
31+
32+
$(image).css({
33+
width: '100%',
34+
height: '100%'
35+
});
36+
$(image).attr('data-height-locked', 'true');
37+
$(element).append(image);
38+
39+
utils.unlockImageSize(element);
40+
expect(image.style.width).toEqual('100%');
41+
expect(image.style.height).toEqual('');
42+
});
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)