From be24a521bc59153d3f5efafecd3134a2ff8a8328 Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:36:20 +0300 Subject: [PATCH] Pattern overrides: disallow override for image with caption/href (#62747) Co-authored-by: ellatrix Co-authored-by: talldan Co-authored-by: SantosGuillamot Co-authored-by: carolinan Co-authored-by: afercia --- packages/block-library/src/image/image.js | 11 +++++++-- .../editor/src/hooks/pattern-overrides.js | 5 ++-- .../components/pattern-overrides-controls.js | 24 +++++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/image/image.js b/packages/block-library/src/image/image.js index 6095ef2fee24d..c96eb4e45117d 100644 --- a/packages/block-library/src/image/image.js +++ b/packages/block-library/src/image/image.js @@ -426,6 +426,9 @@ export default function Image( { ); + const arePatternOverridesEnabled = + metadata?.bindings?.__default?.source === 'core/pattern-overrides'; + const { lockUrlControls = false, lockHrefControls = false, @@ -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. @@ -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 } /> diff --git a/packages/editor/src/hooks/pattern-overrides.js b/packages/editor/src/hooks/pattern-overrides.js index 485cc3725d8d7..36a67bb9c5d24 100644 --- a/packages/editor/src/hooks/pattern-overrides.js +++ b/packages/editor/src/hooks/pattern-overrides.js @@ -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 ( <> diff --git a/packages/patterns/src/components/pattern-overrides-controls.js b/packages/patterns/src/components/pattern-overrides-controls.js index 9869c5b072c85..e2c74e7696583 100644 --- a/packages/patterns/src/components/pattern-overrides-controls.js +++ b/packages/patterns/src/components/pattern-overrides-controls.js @@ -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 ); @@ -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 ( <>