Skip to content

Commit

Permalink
Respect custom aspect ratio (#52286)
Browse files Browse the repository at this point in the history
* add image width and height via css inline style, update width and height attrs to be string

* keep width and height as attributes too, keep the attributes as numbers

* updates image fixtures
  • Loading branch information
draganescu authored and tellthemachines committed Jul 11, 2023
1 parent 223feaa commit a1c08a0
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 8 deletions.
99 changes: 97 additions & 2 deletions packages/block-library/src/image/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { RichText, useBlockProps } from '@wordpress/block-editor';
import {
RichText,
useBlockProps,
__experimentalGetElementClassName as getBorderClassesAndStyles,
} from '@wordpress/block-editor';

/**
* Deprecation for adding the `wp-image-${id}` class to the image block for
Expand Down Expand Up @@ -539,4 +543,95 @@ const v5 = {
},
};

export default [ v5, v4, v3, v2, v1 ];
/**
* Deprecation for adding width and height as style rules on the inner img.
* It also updates the widht and height attributes to be strings instead of numbers.
*
* @see https://github.com/WordPress/gutenberg/pull/31366
*/
const v6 = {
save( { attributes } ) {
const {
url,
alt,
caption,
align,
href,
rel,
linkClass,
width,
height,
aspectRatio,
scale,
id,
linkTarget,
sizeSlug,
title,
} = attributes;

const newRel = ! rel ? undefined : rel;
const borderProps = getBorderClassesAndStyles( attributes );

const classes = classnames( {
[ `align${ align }` ]: align,
[ `size-${ sizeSlug }` ]: sizeSlug,
'is-resized': width || height,
'has-custom-border':
!! borderProps.className ||
( borderProps.style &&
Object.keys( borderProps.style ).length > 0 ),
} );

const imageClasses = classnames( borderProps.className, {
[ `wp-image-${ id }` ]: !! id,
} );

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

const figure = (
<>
{ href ? (
<a
className={ linkClass }
href={ href }
target={ linkTarget }
rel={ newRel }
>
{ image }
</a>
) : (
image
) }
{ ! RichText.isEmpty( caption ) && (
<RichText.Content
className={ getBorderClassesAndStyles( 'caption' ) }
tagName="figcaption"
value={ caption }
/>
) }
</>
);

return (
<figure { ...useBlockProps.save( { className: classes } ) }>
{ figure }
</figure>
);
},
};

export default [ v6, v5, v4, v3, v2, v1 ];
2 changes: 2 additions & 0 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export default function save( { attributes } ) {
...borderProps.style,
aspectRatio,
objectFit: scale,
width,
height,
} }
width={ width }
height={ height }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"name": "core/image",
"isValid": true,
"attributes": {
"align": "left",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": []
"caption": [],
"align": "left"
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"name": "core/image",
"isValid": true,
"attributes": {
"align": "left",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": [],
"align": "left",
"width": 100,
"height": 100
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","width":100,"height":100} -->
<figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" width="100" height="100"/></figure>
<figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" style="width:100px;height:100px" width="100" height="100"/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","width":100,"height":100} -->
<figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" width="100" height="100"/></figure>
<figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" style="width:100px;height:100px" width="100" height="100"/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","id":13,"width":358,"height":164,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image alignleft size-large is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-13" width="358" height="164"/></figure>
<figure class="wp-block-image alignleft size-large is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-13" style="width:358px;height:164px" width="358" height="164"/></figure>
<!-- /wp:image -->

0 comments on commit a1c08a0

Please sign in to comment.