@@ -256,6 +256,47 @@ const Controls = props => {
256256 }
257257 return ''
258258 } ) ( ) }
259+ onChangeUnit = { ( unit , attributeName , oldUnit ) => {
260+ // When the unit is changed, we need to adjust the width
261+ // so that the image does not get distorted.
262+ if ( isImageBlock && deviceType === 'Desktop' ) {
263+ // Switching from % to px
264+ if ( oldUnit === '%' && unit === 'px' ) {
265+ // If image is too small, use the original image width
266+ if ( imageBlockWidth && attributes . imageWidthAttribute < imageBlockWidth ) {
267+ return setAttributes ( {
268+ imageWidth : attributes . imageWidthAttribute ,
269+ [ attributeName ] : unit ,
270+ } )
271+ }
272+ // If the width is larger than the block width, reset to 100% / width of block
273+ if ( attributes . imageWidth === '' ) {
274+ return setAttributes ( {
275+ imageWidth : imageBlockWidth ,
276+ [ attributeName ] : unit ,
277+ } )
278+ }
279+ // Switching from px to %
280+ } else if ( oldUnit === 'px' && unit === '%' ) {
281+ // If the image is larger than the block width, reset to 100%
282+ if ( imageBlockWidth && attributes . imageWidthAttribute > imageBlockWidth ) {
283+ return setAttributes ( {
284+ imageWidth : '' ,
285+ [ attributeName ] : unit ,
286+ } )
287+ }
288+ // If image goes past 100$, reset to 100%
289+ if ( attributes . imageWidth > 100 ) {
290+ return setAttributes ( {
291+ imageWidth : '' ,
292+ [ attributeName ] : unit ,
293+ } )
294+ }
295+ }
296+ }
297+ // Normal saving behavior.
298+ setAttributes ( { [ attributeName ] : unit } )
299+ } }
259300 responsive = "all"
260301 onOverrideReset = { ( ) => {
261302 // When resetting and in desktop, adjust the width so that we get the right "reset" value. Logic:
0 commit comments