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

Cover Block: Add Image Resolution options #62926

Open
wants to merge 23 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
05c8526
Add attribute for resolution of image in cover block
akasunil Jun 27, 2024
c686246
Add resolutionTool into cover block for background images
akasunil Jun 27, 2024
645afb9
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Jul 2, 2024
6212d27
Relocate the resolution control in inspector control
akasunil Jul 2, 2024
615bb8c
Add image size class to image on save
akasunil Jul 2, 2024
b3266d9
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Jul 2, 2024
63e1c17
Retain previous value of sizeSlug attribute on image change
akasunil Jul 2, 2024
4da9dac
Update url based on selected image size
akasunil Jul 2, 2024
e22fc65
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 6, 2024
715420e
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 22, 2024
cc5860b
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 22, 2024
211a5f2
Update setting panel using ToolsPanel component
akasunil Aug 22, 2024
7a50d21
Remove clear media button from setting panel
akasunil Aug 22, 2024
819f489
Synced with trunk and resolved conflicts
akasunil Aug 24, 2024
8ae99b3
Remove Resolution Tool component from dimension panel
akasunil Aug 24, 2024
be4df96
Update role of setting panel in unit test
akasunil Aug 24, 2024
726f286
Remove unneccessory code
akasunil Aug 25, 2024
52bbd22
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 25, 2024
4620e0e
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/issue…
akasunil Sep 13, 2024
d46c666
Remove typecasting for boolean variable and update default value
akasunil Sep 13, 2024
9ed79ba
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/issue…
akasunil Sep 13, 2024
e78f907
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/issue…
akasunil Sep 16, 2024
18c3e93
Set default value to full size for media size slug attribute
akasunil Sep 16, 2024
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 @@ -246,7 +246,7 @@ Add an image or video with a text overlay. ([Source](https://github.com/WordPres
- **Name:** core/cover
- **Category:** media
- **Supports:** align, anchor, color (heading, text, ~~background~~, ~~enableContrastChecker~~), dimensions (aspectRatio), interactivity (clientNavigation), layout (~~allowJustification~~), shadow, spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, isUserOverlayColor, minHeight, minHeightUnit, overlayColor, tagName, templateLock, url, useFeaturedImage
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, isUserOverlayColor, minHeight, minHeightUnit, overlayColor, sizeSlug, tagName, templateLock, url, useFeaturedImage

## Details

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
"tagName": {
"type": "string",
"default": "div"
},
"sizeSlug": {
"type": "string"
}
},
"usesContext": [ "postId", "postType" ],
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/cover/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_MEDIA_SIZE_SLUG = 'full';
19 changes: 19 additions & 0 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
DEFAULT_BACKGROUND_COLOR,
DEFAULT_OVERLAY_COLOR,
} from './color-utils';
import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants';

function getInnerBlocksTemplate( attributes ) {
return [
Expand Down Expand Up @@ -99,6 +100,7 @@ function CoverEdit( {
templateLock,
tagName: TagName = 'div',
isUserOverlayColor,
sizeSlug,
} = attributes;

const [ featuredImage ] = useEntityProp(
Expand All @@ -107,6 +109,7 @@ function CoverEdit( {
'featured_media',
postId
);
const { getSettings } = useSelect( blockEditorStore );

const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
Expand Down Expand Up @@ -198,6 +201,22 @@ function CoverEdit( {
averageBackgroundColor
);

if ( backgroundType === IMAGE_BACKGROUND_TYPE && mediaAttributes.id ) {
const { imageDefaultSize } = getSettings();

// Try to use the previous selected image size if its available
// otherwise try the default image size or fallback full size.
if ( sizeSlug && newMedia?.sizes?.[ sizeSlug ] ) {
mediaAttributes.sizeSlug = sizeSlug;
mediaAttributes.url = newMedia?.sizes?.[ sizeSlug ]?.url;
} else if ( newMedia?.sizes?.[ imageDefaultSize ] ) {
mediaAttributes.sizeSlug = imageDefaultSize;
mediaAttributes.url = newMedia?.sizes?.[ sizeSlug ]?.url;
} else {
mediaAttributes.sizeSlug = DEFAULT_MEDIA_SIZE_SLUG;
}
}

setAttributes( {
...mediaAttributes,
focalPoint: undefined,
Expand Down
192 changes: 148 additions & 44 deletions packages/block-library/src/cover/edit/inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { useMemo } from '@wordpress/element';
import {
ExternalLink,
FocalPointPicker,
PanelBody,
RangeControl,
TextareaControl,
ToggleControl,
SelectControl,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalUnitControl as UnitControl,
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
Expand All @@ -19,20 +19,25 @@ import { useInstanceId } from '@wordpress/compose';
import {
InspectorControls,
useSettings,
store as blockEditorStore,
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
__experimentalUseGradient,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { COVER_MIN_HEIGHT, mediaPosition } from '../shared';
import { unlock } from '../../lock-unlock';
import { useToolsPanelDropdownMenuProps } from '../../utils/hooks';
import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants';

const { cleanEmptyObject } = unlock( blockEditorPrivateApis );
const { cleanEmptyObject, ResolutionTool } = unlock( blockEditorPrivateApis );

function CoverHeightInput( {
onChange,
Expand Down Expand Up @@ -94,6 +99,7 @@ export default function CoverInspectorControls( {
} ) {
const {
useFeaturedImage,
id,
dimRatio,
focalPoint,
hasParallax,
Expand All @@ -111,7 +117,38 @@ export default function CoverInspectorControls( {
overlayColor,
} = currentSettings;

const sizeSlug = attributes.sizeSlug || DEFAULT_MEDIA_SIZE_SLUG;

const { gradientValue, setGradient } = __experimentalUseGradient();
const { getSettings } = useSelect( blockEditorStore );

const imageSizes = getSettings().imageSizes;

const image = useSelect(
( select ) =>
id && isImageBackground
? select( coreStore ).getMedia( id, { context: 'view' } )
: null,
[ id, isImageBackground ]
);

function updateImage( newSizeSlug ) {
const newUrl = image?.media_details?.sizes?.[ newSizeSlug ]?.source_url;
if ( ! newUrl ) {
return null;
}

setAttributes( {
url: newUrl,
sizeSlug: newSizeSlug,
} );
}

const imageSizeOptions = imageSizes
.filter(
( { slug } ) => image?.media_details?.sizes?.[ slug ]?.source_url
)
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );

const toggleParallax = () => {
setAttributes( {
Expand Down Expand Up @@ -160,72 +197,139 @@ export default function CoverInspectorControls( {
),
};

const dropdownMenuProps = useToolsPanelDropdownMenuProps();

return (
<>
<InspectorControls>
{ !! url && (
<PanelBody title={ __( 'Settings' ) }>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
hasParallax: false,
focalPoint: undefined,
isRepeated: false,
alt: '',
sizeSlug: undefined,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
>
{ isImageBackground && (
<>
<ToggleControl
__nextHasNoMarginBottom
<ToolsPanelItem
label={ __( 'Fixed background' ) }
checked={ hasParallax }
onChange={ toggleParallax }
/>
isShownByDefault
hasValue={ () => hasParallax }
onDeselect={ () =>
setAttributes( {
hasParallax: false,
focalPoint: undefined,
} )
}
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Fixed background' ) }
checked={ hasParallax }
onChange={ toggleParallax }
/>
</ToolsPanelItem>

<ToggleControl
__nextHasNoMarginBottom
<ToolsPanelItem
label={ __( 'Repeated background' ) }
checked={ isRepeated }
onChange={ toggleIsRepeated }
/>
isShownByDefault
hasValue={ () => isRepeated }
onDeselect={ () =>
setAttributes( {
isRepeated: false,
} )
}
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Repeated background' ) }
checked={ isRepeated }
onChange={ toggleIsRepeated }
/>
</ToolsPanelItem>
</>
) }

{ showFocalPointPicker && (
<FocalPointPicker
__nextHasNoMarginBottom
<ToolsPanelItem
label={ __( 'Focal point' ) }
url={ url }
value={ focalPoint }
onDragStart={ imperativeFocalPointPreview }
onDrag={ imperativeFocalPointPreview }
onChange={ ( newFocalPoint ) =>
isShownByDefault
hasValue={ () => !! focalPoint }
onDeselect={ () =>
setAttributes( {
focalPoint: newFocalPoint,
focalPoint: undefined,
} )
}
/>
>
<FocalPointPicker
__nextHasNoMarginBottom
label={ __( 'Focal point' ) }
url={ url }
value={ focalPoint }
onDragStart={ imperativeFocalPointPreview }
onDrag={ imperativeFocalPointPreview }
onChange={ ( newFocalPoint ) =>
setAttributes( {
focalPoint: newFocalPoint,
} )
}
/>
</ToolsPanelItem>
) }
{ ! useFeaturedImage && url && ! isVideoBackground && (
<TextareaControl
__nextHasNoMarginBottom
<ToolsPanelItem
label={ __( 'Alternative text' ) }
value={ alt }
onChange={ ( newAlt ) =>
setAttributes( { alt: newAlt } )
isShownByDefault
hasValue={ () => !! alt }
onDeselect={ () =>
setAttributes( { alt: '' } )
}
help={
<>
<ExternalLink
href={
// translators: Localized tutorial, if one exists. W3C Web Accessibility Initiative link has list of existing translations.
__(
'https://www.w3.org/WAI/tutorials/images/decision-tree/'
)
}
>
>
<TextareaControl
__nextHasNoMarginBottom
label={ __( 'Alternative text' ) }
value={ alt }
onChange={ ( newAlt ) =>
setAttributes( { alt: newAlt } )
}
help={
<>
<ExternalLink
href={
// translators: Localized tutorial, if one exists. W3C Web Accessibility Initiative link has list of existing translations.
__(
'https://www.w3.org/WAI/tutorials/images/decision-tree/'
)
}
>
{ __(
'Describe the purpose of the image.'
) }
</ExternalLink>
<br />
{ __(
'Describe the purpose of the image.'
'Leave empty if decorative.'
) }
</ExternalLink>
<br />
{ __( 'Leave empty if decorative.' ) }
</>
}
</>
}
/>
</ToolsPanelItem>
) }
{ ! useFeaturedImage && !! imageSizeOptions.length && (
<ResolutionTool
value={ sizeSlug }
onChange={ updateImage }
options={ imageSizeOptions }
/>
) }
</PanelBody>
</ToolsPanel>
) }
</InspectorControls>
{ colorGradientSettings.hasColorsOrGradients && (
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function save( { attributes } ) {
minHeight: minHeightProp,
minHeightUnit,
tagName: Tag,
sizeSlug,
} = attributes;
const overlayColorClass = getColorClassName(
'background-color',
Expand Down Expand Up @@ -95,6 +96,7 @@ export default function save( { attributes } ) {
'wp-block-cover__image-background',
id ? `wp-image-${ id }` : null,
{
[ `size-${ sizeSlug }` ]: sizeSlug,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/cover/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe( 'Cover block', () => {
test( 'does not display media settings panel if url is not set', async () => {
await setup();
expect(
screen.queryByRole( 'button', {
screen.queryByRole( 'heading', {
name: 'Settings',
} )
).not.toBeInTheDocument();
Expand All @@ -202,7 +202,7 @@ describe( 'Cover block', () => {

await selectBlock( 'Block: Cover' );
expect(
screen.getByRole( 'button', {
screen.getByRole( 'heading', {
name: 'Settings',
} )
).toBeInTheDocument();
Expand Down
Loading