Skip to content

Commit

Permalink
Pattern overrides: disallow override for image with caption/href (#62747
Browse files Browse the repository at this point in the history
)

Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: afercia <afercia@git.wordpress.org>
  • Loading branch information
6 people committed Jun 25, 2024
1 parent b9636e6 commit be24a52
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 9 additions & 2 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ export default function Image( {
</InspectorControls>
);

const arePatternOverridesEnabled =
metadata?.bindings?.__default?.source === 'core/pattern-overrides';

const {
lockUrlControls = false,
lockHrefControls = false,
Expand Down Expand Up @@ -470,7 +473,7 @@ export default function Image( {
lockHrefControls:
// Disable editing the link of the URL if the image is inside a pattern instance.
// This is a temporary solution until we support overriding the link on the frontend.
hasParentPattern,
hasParentPattern || arePatternOverridesEnabled,
lockCaption:
// Disable editing the caption if the image is inside a pattern instance.
// This is a temporary solution until we support overriding the caption on the frontend.
Expand Down Expand Up @@ -971,7 +974,11 @@ export default function Image( {
isSelected={ isSingleSelected }
insertBlocksAfter={ insertBlocksAfter }
label={ __( 'Image caption text' ) }
showToolbarButton={ isSingleSelected && hasNonContentControls }
showToolbarButton={
isSingleSelected &&
hasNonContentControls &&
! arePatternOverridesEnabled
}
readOnly={ lockCaption }
/>
</>
Expand Down
5 changes: 2 additions & 3 deletions packages/editor/src/hooks/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ const {
*/
const withPatternOverrideControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const isSupportedBlock = Object.keys(
PARTIAL_SYNCING_SUPPORTED_BLOCKS
).includes( props.name );
const isSupportedBlock =
!! PARTIAL_SYNCING_SUPPORTED_BLOCKS[ props.name ];

return (
<>
Expand Down
24 changes: 20 additions & 4 deletions packages/patterns/src/components/pattern-overrides-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function addBindings( bindings ) {
};
}

function PatternOverridesControls( { attributes, setAttributes } ) {
function PatternOverridesControls( {
attributes,
setAttributes,
name: blockName,
} ) {
const controlId = useId();
const [ showAllowOverridesModal, setShowAllowOverridesModal ] =
useState( false );
Expand Down Expand Up @@ -71,15 +75,25 @@ function PatternOverridesControls( { attributes, setAttributes } ) {
return null;
}

const hasUnsupportedImageAttributes =
blockName === 'core/image' &&
( !! attributes.caption?.length || !! attributes.href?.length );

const helpText = hasUnsupportedImageAttributes
? __(
`Overrides currently don't support image captions or links. Remove the caption or link first before enabling overrides.`
)
: __(
'Allow changes to this block throughout instances of this pattern.'
);

return (
<>
<InspectorControls group="advanced">
<BaseControl
id={ controlId }
label={ __( 'Overrides' ) }
help={ __(
'Allow changes to this block throughout instances of this pattern.'
) }
help={ helpText }
>
<Button
__next40pxDefaultSize
Expand All @@ -93,6 +107,8 @@ function PatternOverridesControls( { attributes, setAttributes } ) {
setShowAllowOverridesModal( true );
}
} }
disabled={ hasUnsupportedImageAttributes }
__experimentalIsFocusable
>
{ allowOverrides
? __( 'Disable overrides' )
Expand Down

0 comments on commit be24a52

Please sign in to comment.