From 2f6f643f27e9be23b0e22d71e227b376f8f3b50a Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 26 Jan 2024 10:36:12 +1300 Subject: [PATCH 1/2] Change the pattern block overrides attribute name to `content` --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/block/block.json | 2 +- packages/block-library/src/block/edit.js | 28 ++++++++++----------- packages/block-library/src/block/index.php | 4 +-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 0708ee342593e..c2f30d95dd211 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -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 diff --git a/packages/block-library/src/block/block.json b/packages/block-library/src/block/block.json index 7eac2b705ba1f..ac0790abd089f 100644 --- a/packages/block-library/src/block/block.json +++ b/packages/block-library/src/block/block.json @@ -11,7 +11,7 @@ "ref": { "type": "number" }, - "overrides": { + "content": { "type": "object" } }, diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index c940f36e1a8b8..6f9a61dc14b02 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -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; @@ -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; } @@ -145,10 +145,10 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) { function getOverridesFromBlocks( blocks, defaultValues ) { /** @type {Record>} */ - const overrides = {}; + const content = {}; for ( const block of blocks ) { Object.assign( - overrides, + content, getOverridesFromBlocks( block.innerBlocks, defaultValues ) ); /** @type {string} */ @@ -160,7 +160,7 @@ 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. @@ -168,12 +168,12 @@ function getOverridesFromBlocks( blocks, defaultValues ) { */ 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 ], ]; @@ -181,7 +181,7 @@ function getOverridesFromBlocks( blocks, defaultValues ) { } } } - return Object.keys( overrides ).length > 0 ? overrides : undefined; + return Object.keys( content ).length > 0 ? content : undefined; } function setBlockEditMode( setEditMode, blocks, mode ) { @@ -202,7 +202,7 @@ function getHasOverridableBlocks( blocks ) { export default function ReusableBlockEdit( { name, - attributes: { ref, overrides }, + attributes: { ref, content }, __unstableParentLayout: parentLayout, clientId: patternClientId, setAttributes, @@ -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 { @@ -338,7 +338,7 @@ export default function ReusableBlockEdit( { prevBlocks = blocks; syncDerivedUpdates( () => { setAttributes( { - overrides: getOverridesFromBlocks( + content: getOverridesFromBlocks( blocks, defaultValuesRef.current ), @@ -354,7 +354,7 @@ export default function ReusableBlockEdit( { }; const resetOverrides = () => { - if ( overrides ) { + if ( content ) { replaceInnerBlocks( patternClientId, initialBlocks ); } }; @@ -405,7 +405,7 @@ export default function ReusableBlockEdit( { { __( 'Reset' ) } diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 444001fa49859..dd94e94ecaeb5 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -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` @@ -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 ); From 5d45364cc9fbd258b2143e0290df2229db1f3e76 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 26 Jan 2024 11:17:33 +1300 Subject: [PATCH 2/2] fix e2es --- test/e2e/specs/editor/various/pattern-overrides.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/e2e/specs/editor/various/pattern-overrides.spec.js b/test/e2e/specs/editor/various/pattern-overrides.spec.js index 417827a907158..34998a1fd339b 100644 --- a/test/e2e/specs/editor/various/pattern-overrides.spec.js +++ b/test/e2e/specs/editor/various/pattern-overrides.spec.js @@ -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' ], }, @@ -181,7 +181,7 @@ test.describe( 'Pattern Overrides', () => { name: 'core/block', attributes: { ref: patternId, - overrides: { + content: { [ editableParagraphId ]: { content: [ 1, 'This one is different' ], }, @@ -305,7 +305,7 @@ test.describe( 'Pattern Overrides', () => { name: 'core/block', attributes: { ref: id, - overrides: { + content: { [ buttonId ]: { linkTarget: [ 0 ], },