Skip to content

Commit 013b490

Browse files
akasunilakasunilt-hamanoaaronrobertshawrichtabor
authored
Cover Block: Add Image Resolution options (#62926)
* Add attribute for resolution of image in cover block * Add resolutionTool into cover block for background images * Relocate the resolution control in inspector control * Add image size class to image on save * Retain previous value of sizeSlug attribute on image change * Update url based on selected image size * Update setting panel using ToolsPanel component * Remove clear media button from setting panel * Remove Resolution Tool component from dimension panel * Update role of setting panel in unit test * Remove unneccessory code * Remove typecasting for boolean variable and update default value * Set default value to full size for media size slug attribute * clear whitespace and typos * Set default value for image resolution * Fix empty image sizes condition Unlinked contributors: Bladed3d. Co-authored-by: akasunil <sunil25393@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org> Co-authored-by: richtabor <richtabor@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org> Co-authored-by: codebymikey <codebymikey@git.wordpress.org>
1 parent 76f58cf commit 013b490

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ Add an image or video with a text overlay. ([Source](https://github.com/WordPres
246246
- **Name:** core/cover
247247
- **Category:** media
248248
- **Supports:** align, anchor, color (heading, text, ~~background~~, ~~enableContrastChecker~~), dimensions (aspectRatio), interactivity (clientNavigation), layout (~~allowJustification~~), shadow, spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
249-
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, isUserOverlayColor, minHeight, minHeightUnit, overlayColor, tagName, templateLock, url, useFeaturedImage
249+
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, isUserOverlayColor, minHeight, minHeightUnit, overlayColor, sizeSlug, tagName, templateLock, url, useFeaturedImage
250250

251251
## Details
252252

packages/block-library/src/cover/block.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
"tagName": {
7979
"type": "string",
8080
"default": "div"
81+
},
82+
"sizeSlug": {
83+
"type": "string"
8184
}
8285
},
8386
"usesContext": [ "postId", "postType" ],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_MEDIA_SIZE_SLUG = 'full';

packages/block-library/src/cover/edit/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
DEFAULT_BACKGROUND_COLOR,
4848
DEFAULT_OVERLAY_COLOR,
4949
} from './color-utils';
50+
import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants';
5051

5152
function getInnerBlocksTemplate( attributes ) {
5253
return [
@@ -100,6 +101,7 @@ function CoverEdit( {
100101
templateLock,
101102
tagName: TagName = 'div',
102103
isUserOverlayColor,
104+
sizeSlug,
103105
} = attributes;
104106

105107
const [ featuredImage ] = useEntityProp(
@@ -108,6 +110,7 @@ function CoverEdit( {
108110
'featured_media',
109111
postId
110112
);
113+
const { getSettings } = useSelect( blockEditorStore );
111114

112115
const { __unstableMarkNextChangeAsNotPersistent } =
113116
useDispatch( blockEditorStore );
@@ -199,6 +202,22 @@ function CoverEdit( {
199202
averageBackgroundColor
200203
);
201204

205+
if ( backgroundType === IMAGE_BACKGROUND_TYPE && mediaAttributes.id ) {
206+
const { imageDefaultSize } = getSettings();
207+
208+
// Try to use the previous selected image size if it's available
209+
// otherwise try the default image size or fallback to full size.
210+
if ( sizeSlug && newMedia?.sizes?.[ sizeSlug ] ) {
211+
mediaAttributes.sizeSlug = sizeSlug;
212+
mediaAttributes.url = newMedia?.sizes?.[ sizeSlug ]?.url;
213+
} else if ( newMedia?.sizes?.[ imageDefaultSize ] ) {
214+
mediaAttributes.sizeSlug = imageDefaultSize;
215+
mediaAttributes.url = newMedia?.sizes?.[ sizeSlug ]?.url;
216+
} else {
217+
mediaAttributes.sizeSlug = DEFAULT_MEDIA_SIZE_SLUG;
218+
}
219+
}
220+
202221
setAttributes( {
203222
...mediaAttributes,
204223
focalPoint: undefined,

packages/block-library/src/cover/edit/inspector-controls.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ import { useInstanceId } from '@wordpress/compose';
1919
import {
2020
InspectorControls,
2121
useSettings,
22+
store as blockEditorStore,
2223
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
2324
__experimentalUseGradient,
2425
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
2526
privateApis as blockEditorPrivateApis,
2627
} from '@wordpress/block-editor';
2728
import { __ } from '@wordpress/i18n';
29+
import { useSelect } from '@wordpress/data';
30+
import { store as coreStore } from '@wordpress/core-data';
2831

2932
/**
3033
* Internal dependencies
3134
*/
3235
import { COVER_MIN_HEIGHT, mediaPosition } from '../shared';
3336
import { unlock } from '../../lock-unlock';
3437
import { useToolsPanelDropdownMenuProps } from '../../utils/hooks';
38+
import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants';
3539

36-
const { cleanEmptyObject } = unlock( blockEditorPrivateApis );
40+
const { cleanEmptyObject, ResolutionTool } = unlock( blockEditorPrivateApis );
3741

3842
function CoverHeightInput( {
3943
onChange,
@@ -95,6 +99,7 @@ export default function CoverInspectorControls( {
9599
} ) {
96100
const {
97101
useFeaturedImage,
102+
id,
98103
dimRatio,
99104
focalPoint,
100105
hasParallax,
@@ -112,7 +117,38 @@ export default function CoverInspectorControls( {
112117
overlayColor,
113118
} = currentSettings;
114119

120+
const sizeSlug = attributes.sizeSlug || DEFAULT_MEDIA_SIZE_SLUG;
121+
115122
const { gradientValue, setGradient } = __experimentalUseGradient();
123+
const { getSettings } = useSelect( blockEditorStore );
124+
125+
const imageSizes = getSettings()?.imageSizes;
126+
127+
const image = useSelect(
128+
( select ) =>
129+
id && isImageBackground
130+
? select( coreStore ).getMedia( id, { context: 'view' } )
131+
: null,
132+
[ id, isImageBackground ]
133+
);
134+
135+
function updateImage( newSizeSlug ) {
136+
const newUrl = image?.media_details?.sizes?.[ newSizeSlug ]?.source_url;
137+
if ( ! newUrl ) {
138+
return null;
139+
}
140+
141+
setAttributes( {
142+
url: newUrl,
143+
sizeSlug: newSizeSlug,
144+
} );
145+
}
146+
147+
const imageSizeOptions = imageSizes
148+
?.filter(
149+
( { slug } ) => image?.media_details?.sizes?.[ slug ]?.source_url
150+
)
151+
?.map( ( { name, slug } ) => ( { value: slug, label: name } ) );
116152

117153
const toggleParallax = () => {
118154
setAttributes( {
@@ -175,6 +211,7 @@ export default function CoverInspectorControls( {
175211
focalPoint: undefined,
176212
isRepeated: false,
177213
alt: '',
214+
sizeSlug: undefined,
178215
} );
179216
} }
180217
dropdownMenuProps={ dropdownMenuProps }
@@ -284,6 +321,14 @@ export default function CoverInspectorControls( {
284321
/>
285322
</ToolsPanelItem>
286323
) }
324+
{ ! useFeaturedImage && !! imageSizeOptions?.length && (
325+
<ResolutionTool
326+
value={ sizeSlug }
327+
onChange={ updateImage }
328+
options={ imageSizeOptions }
329+
defaultValue={ DEFAULT_MEDIA_SIZE_SLUG }
330+
/>
331+
) }
287332
</ToolsPanel>
288333
) }
289334
</InspectorControls>

packages/block-library/src/cover/save.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export default function save( { attributes } ) {
4545
minHeight: minHeightProp,
4646
minHeightUnit,
4747
tagName: Tag,
48+
sizeSlug,
4849
} = attributes;
4950
const overlayColorClass = getColorClassName(
5051
'background-color',
@@ -95,6 +96,7 @@ export default function save( { attributes } ) {
9596
'wp-block-cover__image-background',
9697
id ? `wp-image-${ id }` : null,
9798
{
99+
[ `size-${ sizeSlug }` ]: sizeSlug,
98100
'has-parallax': hasParallax,
99101
'is-repeated': isRepeated,
100102
}

0 commit comments

Comments
 (0)