Skip to content

Commit

Permalink
Change the pattern block overrides attribute name to content
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz committed Jan 25, 2024
1 parent b421fb7 commit 5bc9daa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Create and save content to reuse across your site. Update the pattern, and the c
- **Name:** core/block
- **Category:** reusable
- **Supports:** ~~customClassName~~, ~~html~~, ~~inserter~~, ~~renaming~~
- **Attributes:** overrides, ref
- **Attributes:** content, ref

## Button

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"ref": {
"type": "number"
},
"overrides": {
"content": {
"type": "object"
}
},
Expand Down
28 changes: 14 additions & 14 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ const PATCH_OPERATIONS = {
* @typedef {RemovePatch | ReplacePatch} OverridePatch
*/

function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
function applyInitialOverrides( blocks, content = {}, defaultValues ) {
return blocks.map( ( block ) => {
const innerBlocks = applyInitialOverrides(
block.innerBlocks,
overrides,
content,
defaultValues
);
const blockId = block.attributes.metadata?.id;
Expand All @@ -125,7 +125,7 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
defaultValues[ blockId ][ attributeKey ] =
block.attributes[ attributeKey ];
/** @type {OverridePatch} */
const overrideAttribute = overrides[ blockId ]?.[ attributeKey ];
const overrideAttribute = content[ blockId ]?.[ attributeKey ];
if ( ! overrideAttribute ) {
continue;
}
Expand All @@ -145,10 +145,10 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {

function getOverridesFromBlocks( blocks, defaultValues ) {
/** @type {Record<string, Record<string, OverridePatch>>} */
const overrides = {};
const content = {};
for ( const block of blocks ) {
Object.assign(
overrides,
content,
getOverridesFromBlocks( block.innerBlocks, defaultValues )
);
/** @type {string} */
Expand All @@ -160,28 +160,28 @@ function getOverridesFromBlocks( blocks, defaultValues ) {
block.attributes[ attributeKey ] !==
defaultValues[ blockId ][ attributeKey ]
) {
overrides[ blockId ] ??= {};
content[ blockId ] ??= {};
/**
* Create a patch operation for the binding attribute.
* We use a tuple here to minimize the size of the serialized data.
* The first item is the operation type, the second item is the value if any.
*/
if ( block.attributes[ attributeKey ] === undefined ) {
/** @type {RemovePatch} */
overrides[ blockId ][ attributeKey ] = [
content[ blockId ][ attributeKey ] = [
PATCH_OPERATIONS.Remove,
];
} else {
/** @type {ReplacePatch} */
overrides[ blockId ][ attributeKey ] = [
content[ blockId ][ attributeKey ] = [
PATCH_OPERATIONS.Replace,
block.attributes[ attributeKey ],
];
}
}
}
}
return Object.keys( overrides ).length > 0 ? overrides : undefined;
return Object.keys( content ).length > 0 ? content : undefined;
}

function setBlockEditMode( setEditMode, blocks, mode ) {
Expand All @@ -202,7 +202,7 @@ function getHasOverridableBlocks( blocks ) {

export default function ReusableBlockEdit( {
name,
attributes: { ref, overrides },
attributes: { ref, content },
__unstableParentLayout: parentLayout,
clientId: patternClientId,
setAttributes,
Expand All @@ -215,7 +215,7 @@ export default function ReusableBlockEdit( {
ref
);
const isMissing = hasResolved && ! record;
const initialOverrides = useRef( overrides );
const initialOverrides = useRef( content );
const defaultValuesRef = useRef( {} );

const {
Expand Down Expand Up @@ -338,7 +338,7 @@ export default function ReusableBlockEdit( {
prevBlocks = blocks;
syncDerivedUpdates( () => {
setAttributes( {
overrides: getOverridesFromBlocks(
content: getOverridesFromBlocks(
blocks,
defaultValuesRef.current
),
Expand All @@ -354,7 +354,7 @@ export default function ReusableBlockEdit( {
};

const resetOverrides = () => {
if ( overrides ) {
if ( content ) {
replaceInnerBlocks( patternClientId, initialBlocks );
}
};
Expand Down Expand Up @@ -405,7 +405,7 @@ export default function ReusableBlockEdit( {
<ToolbarGroup>
<ToolbarButton
onClick={ resetOverrides }
disabled={ ! overrides }
disabled={ ! content }
__experimentalIsFocusable
>
{ __( 'Reset to original' ) }
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function render_block_core_block( $attributes ) {
$content = $wp_embed->run_shortcode( $reusable_block->post_content );
$content = $wp_embed->autoembed( $content );

$has_pattern_overrides = isset( $attributes['overrides'] );
$has_pattern_overrides = isset( $attributes['content'] );

/**
* We set the `pattern/overrides` context through the `render_block_context`
Expand All @@ -55,7 +55,7 @@ function render_block_core_block( $attributes ) {
*/
if ( $has_pattern_overrides ) {
$filter_block_context = static function ( $context ) use ( $attributes ) {
$context['pattern/overrides'] = $attributes['overrides'];
$context['pattern/overrides'] = $attributes['content'];
return $context;
};
add_filter( 'render_block_context', $filter_block_context, 1 );
Expand Down

0 comments on commit 5bc9daa

Please sign in to comment.