diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js
index 6331d33c27a7bb..0608da511dcc8f 100644
--- a/packages/block-library/src/block/edit.js
+++ b/packages/block-library/src/block/edit.js
@@ -147,6 +147,13 @@ function setBlockEditMode( setEditMode, blocks, mode ) {
} );
}
+function getHasOverridableBlocks( blocks ) {
+ return blocks.some( ( block ) => {
+ if ( isPartiallySynced( block ) ) return true;
+ return getHasOverridableBlocks( block.innerBlocks );
+ } );
+}
+
export default function ReusableBlockEdit( {
name,
attributes: { ref, overrides },
@@ -208,15 +215,23 @@ export default function ReusableBlockEdit( {
[ innerBlocks, setBlockEditingMode ]
);
- // Apply the initial overrides from the pattern block to the inner blocks.
- useEffect( () => {
- const initialBlocks =
+ const hasOverridableBlocks = useMemo(
+ () => getHasOverridableBlocks( innerBlocks ),
+ [ innerBlocks ]
+ );
+
+ const initialBlocks = useMemo(
+ () =>
// Clone the blocks to generate new client IDs.
editedRecord.blocks?.map( ( block ) => cloneBlock( block ) ) ??
( editedRecord.content && typeof editedRecord.content !== 'function'
? parse( editedRecord.content )
- : [] );
+ : [] ),
+ [ editedRecord.blocks, editedRecord.content ]
+ );
+ // Apply the initial overrides from the pattern block to the inner blocks.
+ useEffect( () => {
defaultValuesRef.current = {};
const editingMode = getBlockEditingMode( patternClientId );
// Replace the contents of the blocks with the overrides.
@@ -237,7 +252,7 @@ export default function ReusableBlockEdit( {
}, [
__unstableMarkNextChangeAsNotPersistent,
patternClientId,
- editedRecord,
+ initialBlocks,
replaceInnerBlocks,
registry,
getBlockEditingMode,
@@ -293,6 +308,12 @@ export default function ReusableBlockEdit( {
editOriginalProps.onClick( event );
};
+ const resetOverrides = () => {
+ if ( overrides ) {
+ replaceInnerBlocks( patternClientId, initialBlocks );
+ }
+ };
+
let children = null;
if ( hasAlreadyRendered ) {
@@ -333,6 +354,21 @@ export default function ReusableBlockEdit( {
) }
+
+ { hasOverridableBlocks && (
+