Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ddryo committed Aug 12, 2023
1 parent 73eca1d commit 36109dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
21 changes: 21 additions & 0 deletions packages/block-library/src/image/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ const v4 = {
supports: {
anchor: true,
},
migrate( { width, height, ...attributes } ) {
return {
...attributes,
width: `${ width }px`,
height: `${ height }px`,
};
},
save( { attributes } ) {
const {
url,
Expand Down Expand Up @@ -480,6 +487,13 @@ const v5 = {
},
},
},
migrate( { width, height, ...attributes } ) {
return {
...attributes,
width: `${ width }px`,
height: `${ height }px`,
};
},
save( { attributes } ) {
const {
url,
Expand Down Expand Up @@ -654,6 +668,13 @@ const v6 = {
},
},
},
migrate( { width, height, ...attributes } ) {
return {
...attributes,
width: `${ width }px`,
height: `${ height }px`,
};
},
save( { attributes } ) {
const {
url,
Expand Down
21 changes: 17 additions & 4 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,30 @@ export default function save( { attributes } ) {
[ `wp-image-${ id }` ]: !! id,
} );

const imgStyle = { aspectRatio, objectFit: scale };

// Branch by whether width and height both have size.
// If the image block is very old and has not been migrated to a string type, the size will be int.
// Note that only width may be "auto".
if ( ! aspectRatio && height && width && 'auto' !== width ) {
const w = parseInt( width );
const h = parseInt( height );
if ( ! isNaN( w ) && ! isNaN( h ) ) {
imgStyle.aspectRatio = `${ w }/${ h }`;
}
} else {
if ( 'string' === typeof width ) imgStyle.width = width;
if ( 'string' === typeof height ) imgStyle.height = height;
}

const image = (
<img
src={ url }
alt={ alt }
className={ imageClasses || undefined }
style={ {
...borderProps.style,
aspectRatio,
objectFit: scale,
width,
height,
...imgStyle,
} }
title={ title }
/>
Expand Down

0 comments on commit 36109dd

Please sign in to comment.