From 78b5a27097e814b074cdcb24c29d86e3c1462d86 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 29 Jan 2024 10:43:26 +1300 Subject: [PATCH 1/3] Prevent exception if a pattern with overrides is nested in another pattern --- packages/block-library/src/block/edit.js | 69 ++++++++++++++++-------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index ae1cca181561f3..06bfbfe082c205 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -121,6 +121,12 @@ function getOverridesFromBlocks( blocks, defaultValues ) { /** @type {Record>} */ const overrides = {}; for ( const block of blocks ) { + if ( + block.name === 'core/block' && + getHasOverridableBlocks( block.innerBlocks ) + ) { + continue; + } Object.assign( overrides, getOverridesFromBlocks( block.innerBlocks, defaultValues ) @@ -185,28 +191,37 @@ export default function ReusableBlockEdit( { } = useDispatch( blockEditorStore ); const { syncDerivedUpdates } = unlock( useDispatch( blockEditorStore ) ); - const { innerBlocks, userCanEdit, getBlockEditingMode, getPostLinkProps } = - useSelect( - ( select ) => { - const { canUser } = select( coreStore ); - const { - getBlocks, - getBlockEditingMode: editingMode, - getSettings, - } = select( blockEditorStore ); - const blocks = getBlocks( patternClientId ); - const canEdit = canUser( 'update', 'blocks', ref ); - - // For editing link to the site editor if the theme and user permissions support it. - return { - innerBlocks: blocks, - userCanEdit: canEdit, - getBlockEditingMode: editingMode, - getPostLinkProps: getSettings().getPostLinkProps, - }; - }, - [ patternClientId, ref ] - ); + const { + innerBlocks, + userCanEdit, + getBlockEditingMode, + getPostLinkProps, + hasParentPattern, + } = useSelect( + ( select ) => { + const { canUser } = select( coreStore ); + const { + getBlocks, + getBlockEditingMode: editingMode, + getSettings, + getBlockParentsByBlockName, + } = select( blockEditorStore ); + const blocks = getBlocks( patternClientId ); + const canEdit = canUser( 'update', 'blocks', ref ); + + // For editing link to the site editor if the theme and user permissions support it. + return { + innerBlocks: blocks, + userCanEdit: canEdit, + getBlockEditingMode: editingMode, + getPostLinkProps: getSettings().getPostLinkProps, + hasParentPattern: + getBlockParentsByBlockName( patternClientId, 'core/block' ) + .length > 0, + }; + }, + [ patternClientId, ref ] + ); const editOriginalProps = getPostLinkProps ? getPostLinkProps( { @@ -321,6 +336,16 @@ export default function ReusableBlockEdit( { let children = null; + if ( hasOverridableBlocks && hasParentPattern ) { + children = ( + + { __( + 'You cannot nest a pattern with overrides inside another pattern.' + ) } + + ); + } + if ( hasAlreadyRendered ) { children = ( From ceb93a7caa9a4fbc8c289fbb0800677f58e52039 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 30 Jan 2024 12:38:28 +1300 Subject: [PATCH 2/3] Update the message to say you can't edit the pattern rather than you can't nest, as the nested patterns do display ok in frontend --- packages/block-library/src/block/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index 06bfbfe082c205..348082659002b8 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -340,7 +340,7 @@ export default function ReusableBlockEdit( { children = ( { __( - 'You cannot nest a pattern with overrides inside another pattern.' + 'You cannot edit a pattern with overrides when it is nested in another pattern.' ) } ); From 2c6eeb5d3fb1ff980fb74ccac2543686bb45b065 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 1 Feb 2024 11:53:51 +1300 Subject: [PATCH 3/3] Show the nested pattern blocks but disable editing --- packages/block-library/src/block/edit.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index 348082659002b8..595e3864a987bc 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -230,10 +230,14 @@ export default function ReusableBlockEdit( { } ) : {}; - useEffect( - () => setBlockEditMode( setBlockEditingMode, innerBlocks ), - [ innerBlocks, setBlockEditingMode ] - ); + useEffect( () => { + if ( hasParentPattern ) { + setBlockEditMode( setBlockEditingMode, innerBlocks, 'disabled' ); + return; + } + + setBlockEditMode( setBlockEditingMode, innerBlocks ); + }, [ hasParentPattern, innerBlocks, setBlockEditingMode ] ); const hasOverridableBlocks = useMemo( () => getHasOverridableBlocks( innerBlocks ), @@ -336,16 +340,6 @@ export default function ReusableBlockEdit( { let children = null; - if ( hasOverridableBlocks && hasParentPattern ) { - children = ( - - { __( - 'You cannot edit a pattern with overrides when it is nested in another pattern.' - ) } - - ); - } - if ( hasAlreadyRendered ) { children = (