Skip to content
Merged
Changes from 3 commits
Commits
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
45 changes: 40 additions & 5 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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.
Expand All @@ -237,7 +252,7 @@ export default function ReusableBlockEdit( {
}, [
__unstableMarkNextChangeAsNotPersistent,
patternClientId,
editedRecord,
initialBlocks,
replaceInnerBlocks,
registry,
getBlockEditingMode,
Expand Down Expand Up @@ -293,6 +308,12 @@ export default function ReusableBlockEdit( {
editOriginalProps.onClick( event );
};

const resetOverrides = () => {
if ( overrides ) {
replaceInnerBlocks( patternClientId, initialBlocks );
}
};

let children = null;

if ( hasAlreadyRendered ) {
Expand Down Expand Up @@ -333,6 +354,20 @@ export default function ReusableBlockEdit( {
</ToolbarGroup>
</BlockControls>
) }

{ hasOverridableBlocks && (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ resetOverrides }
aria-disabled={ overrides ? 'false' : 'true' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that Buttons are a bit unusual in that you have to pass disabled and __experimentalIsFocusable in order for it to retain focus.

Pushed a commit that does that.

>
{ __( 'Reset to original' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
) }

{ children === null ? (
<div { ...innerBlocksProps } />
) : (
Expand Down