Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns: Change the pattern block overrides attribute name to content #58285

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
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 @@ Reuse this design across your site. ([Source](https://github.com/WordPress/guten
- **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' ) }
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
6 changes: 3 additions & 3 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ test.describe( 'Pattern Overrides', () => {
name: 'core/block',
attributes: {
ref: patternId,
overrides: {
content: {
[ editableParagraphId ]: {
content: [ 1, 'I would word it this way' ],
},
Expand All @@ -181,7 +181,7 @@ test.describe( 'Pattern Overrides', () => {
name: 'core/block',
attributes: {
ref: patternId,
overrides: {
content: {
[ editableParagraphId ]: {
content: [ 1, 'This one is different' ],
},
Expand Down Expand Up @@ -305,7 +305,7 @@ test.describe( 'Pattern Overrides', () => {
name: 'core/block',
attributes: {
ref: id,
overrides: {
content: {
[ buttonId ]: {
linkTarget: [ 0 ],
},
Expand Down
Loading