Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Featured Image: Add size selector #38044

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ Display a post's featured image. ([Source](https://github.com/WordPress/gutenber
- **Name:** core/post-featured-image
- **Category:** theme
- **Supports:** align (center, full, left, right, wide), color (~~background~~, ~~text~~), spacing (margin, padding), ~~html~~
- **Attributes:** height, isLink, scale, width
- **Attributes:** height, isLink, scale, sizeSlug, width

## Post Navigation Link

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"scale": {
"type": "string",
"default": "cover"
},
"sizeSlug": {
"type": "string"
}
},
"usesContext": [ "postId", "postType", "queryId" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __, _x } from '@wordpress/i18n';
import {
SelectControl,
__experimentalUnitControl as UnitControl,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
Expand Down Expand Up @@ -32,6 +33,7 @@ const SCALE_OPTIONS = (
);

const DEFAULT_SCALE = 'cover';
const DEFAULT_SIZE = 'full';

const scaleHelp = {
cover: __(
Expand All @@ -47,8 +49,9 @@ const scaleHelp = {

const DimensionControls = ( {
clientId,
attributes: { width, height, scale },
attributes: { width, height, scale, sizeSlug },
setAttributes,
imageSizeOptions = [],
} ) => {
const defaultUnits = [ 'px', '%', 'vw', 'em', 'rem' ];
const units = useCustomUnits( {
Expand Down Expand Up @@ -143,6 +146,29 @@ const DimensionControls = ( {
</ToggleGroupControl>
</ToolsPanelItem>
) }
{ !! imageSizeOptions.length && (
<ToolsPanelItem
hasValue={ () => !! sizeSlug && sizeSlug !== DEFAULT_SIZE }
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
label={ __( 'Size' ) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use Image size rather than Size? That matches the labeling in the Image block for example:
Screen Shot 2022-01-19 at 10 51 51 AM

The added helptext is great in the panel, but it feels a bit confusing to see Size listed alongside Width and Height in the ToolsPanel menu where you lack that context.

Screen Shot 2022-01-19 at 10 52 22 AM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any preference here, but since we're trying to unify image dimensions controls across the blocks would be nice to agree on a single ToolsPanelItem label.

@jasmussen, @jameskoster, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mockup (#38068 inspired by and paired with #38050) I changed it from Image size to Size, exactly because "Image size" as a term seems to suggest dimensions (width and height). However I can see how "Size" on its own isn't necessarily better 🤔

This might be one of the situations where we keep the "Image size" label that it's had for a long time, and then seeking a better name be a part of addressing #38050.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the label. I'm going to merge this, and we can change labels back after settings unification.

onDeselect={ () =>
setAttributes( { sizeSlug: undefined } )
}
resetAllFilter={ () => ( {
sizeSlug: undefined,
} ) }
isShownByDefault={ false }
panelId={ clientId }
>
<SelectControl
label={ __( 'Size' ) }
value={ sizeSlug || DEFAULT_SIZE }
options={ imageSizeOptions }
onChange={ ( nextSizeSlug ) =>
setAttributes( { sizeSlug: nextSizeSlug } )
}
/>
</ToolsPanelItem>
) }
</InspectorControls>
);
};
Expand Down
28 changes: 25 additions & 3 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
MediaPlaceholder,
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { upload } from '@wordpress/icons';
Expand Down Expand Up @@ -46,14 +47,20 @@ const placeholderChip = (
</div>
);

function getMediaSourceUrlBySizeSlug( media, slug ) {
return (
media?.media_details?.sizes?.[ slug ]?.source_url || media?.source_url
);
}

function PostFeaturedImageDisplay( {
clientId,
attributes,
setAttributes,
context: { postId, postType, queryId },
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const { isLink, height, width, scale } = attributes;
const { isLink, height, width, scale, sizeSlug } = attributes;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postType,
Expand All @@ -67,6 +74,20 @@ function PostFeaturedImageDisplay( {
select( coreStore ).getMedia( featuredImage, { context: 'view' } ),
[ featuredImage ]
);
const mediaUrl = getMediaSourceUrlBySizeSlug( media, sizeSlug );

const imageSizes = useSelect(
( select ) => select( blockEditorStore ).getSettings().imageSizes,
[]
);
const imageSizeOptions = imageSizes
.filter( ( { slug } ) => {
return media?.media_details?.sizes?.[ slug ]?.source_url;
} )
.map( ( { name, slug } ) => ( {
value: slug,
label: name,
} ) );
Comment on lines +83 to +90
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably memoize this value, but I'm not 100% convinced that it won't be premature optimization.


const blockProps = useBlockProps( {
style: { width, height },
Expand Down Expand Up @@ -98,6 +119,7 @@ function PostFeaturedImageDisplay( {
clientId={ clientId }
attributes={ attributes }
setAttributes={ setAttributes }
imageSizeOptions={ imageSizeOptions }
/>
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
Expand Down Expand Up @@ -156,7 +178,7 @@ function PostFeaturedImageDisplay( {
placeholderChip
) : (
<img
src={ media.source_url }
src={ mediaUrl }
alt={ media.alt_text || __( 'Featured image' ) }
style={ { height, objectFit: height && scale } }
/>
Expand All @@ -170,7 +192,7 @@ function PostFeaturedImageDisplay( {
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ featuredImage }
mediaURL={ media.source_url }
mediaURL={ mediaUrl }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="image/*"
onSelect={ onSelectImage }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
}
$post_ID = $block->context['postId'];

$featured_image = get_the_post_thumbnail( $post_ID );
$size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
$featured_image = get_the_post_thumbnail( $post_ID, $size_slug );
if ( ! $featured_image ) {
return '';
}
Expand Down