Skip to content

Commit

Permalink
Revert "Use a patch format and support linkTarget of core/button
Browse files Browse the repository at this point in the history
…for Pattern Overrides (#58165)" (#58489)

This reverts commit a0fccd1.
  • Loading branch information
talldan authored Jan 31, 2024
1 parent 1d995de commit 05eb127
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 131 deletions.
19 changes: 2 additions & 17 deletions lib/compat/wordpress-6.5/block-bindings/sources/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,8 @@ function gutenberg_block_bindings_pattern_overrides_callback( $source_attrs, $bl
if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) {
return null;
}
$block_id = $block_instance->attributes['metadata']['id'];
$attribute_override = _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null );
if ( null === $attribute_override ) {
return null;
}
switch ( $attribute_override[0] ) {
case 0: // remove
/**
* TODO: This currently doesn't remove the attribute, but only set it to an empty string.
* It's a temporary solution until the block binding API supports different operations.
*/
return '';
case 1: // replace
return $attribute_override[1];
default:
return null;
}
$block_id = $block_instance->attributes['metadata']['id'];
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null );
}

function gutenberg_register_block_bindings_pattern_overrides_source() {
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.5/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function gutenberg_process_block_bindings( $block_content, $block, $block_instan
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'url', 'title', 'alt' ),
'core/button' => array( 'url', 'text', 'linkTarget' ),
'core/button' => array( 'url', 'text' ),
);

// If the block doesn't have the bindings property or isn't one of the allowed block types, return.
Expand Down
56 changes: 8 additions & 48 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,6 @@ const useInferredLayout = ( blocks, parentLayout ) => {
}, [ blocks, parentLayout ] );
};

/**
* Enum for patch operations.
* We use integers here to minimize the size of the serialized data.
* This has to be deserialized accordingly on the server side.
* See block-bindings/sources/pattern.php
*/
const PATCH_OPERATIONS = {
/** @type {0} */
Remove: 0,
/** @type {1} */
Replace: 1,
// Other operations are reserved for future use. (e.g. Add)
};

/**
* @typedef {[typeof PATCH_OPERATIONS.Remove]} RemovePatch
* @typedef {[typeof PATCH_OPERATIONS.Replace, unknown]} ReplacePatch
* @typedef {RemovePatch | ReplacePatch} OverridePatch
*/

function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
return blocks.map( ( block ) => {
const innerBlocks = applyInitialOverrides(
Expand All @@ -124,15 +104,9 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
defaultValues[ blockId ] ??= {};
defaultValues[ blockId ][ attributeKey ] =
block.attributes[ attributeKey ];
/** @type {OverridePatch} */
const overrideAttribute = overrides[ blockId ]?.[ attributeKey ];
if ( ! overrideAttribute ) {
continue;
}
if ( overrideAttribute[ 0 ] === PATCH_OPERATIONS.Remove ) {
delete newAttributes[ attributeKey ];
} else if ( overrideAttribute[ 0 ] === PATCH_OPERATIONS.Replace ) {
newAttributes[ attributeKey ] = overrideAttribute[ 1 ];
if ( overrides[ blockId ]?.[ attributeKey ] !== undefined ) {
newAttributes[ attributeKey ] =
overrides[ blockId ][ attributeKey ];
}
}
return {
Expand All @@ -144,14 +118,13 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
}

function getOverridesFromBlocks( blocks, defaultValues ) {
/** @type {Record<string, Record<string, OverridePatch>>} */
/** @type {Record<string, Record<string, unknown>>} */
const overrides = {};
for ( const block of blocks ) {
Object.assign(
overrides,
getOverridesFromBlocks( block.innerBlocks, defaultValues )
);
/** @type {string} */
const blockId = block.attributes.metadata?.id;
if ( ! isPartiallySynced( block ) || ! blockId ) continue;
const attributes = getPartiallySyncedAttributes( block );
Expand All @@ -161,23 +134,10 @@ function getOverridesFromBlocks( blocks, defaultValues ) {
defaultValues[ blockId ][ attributeKey ]
) {
overrides[ 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 ] = [
PATCH_OPERATIONS.Remove,
];
} else {
/** @type {ReplacePatch} */
overrides[ blockId ][ attributeKey ] = [
PATCH_OPERATIONS.Replace,
block.attributes[ attributeKey ],
];
}
// TODO: We need a way to represent `undefined` in the serialized overrides.
// Also see: https://github.com/WordPress/gutenberg/pull/57249#discussion_r1452987871
overrides[ blockId ][ attributeKey ] =
block.attributes[ attributeKey ];
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/patterns/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const PARTIAL_SYNCING_SUPPORTED_BLOCKS = {
'core/button': {
text: __( 'Text' ),
url: __( 'URL' ),
linkTarget: __( 'Link Target' ),
},
'core/image': {
url: __( 'URL' ),
Expand Down
66 changes: 2 additions & 64 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ test.describe( 'Pattern Overrides', () => {
ref: patternId,
overrides: {
[ editableParagraphId ]: {
content: [ 1, 'I would word it this way' ],
content: 'I would word it this way',
},
},
},
Expand All @@ -183,7 +183,7 @@ test.describe( 'Pattern Overrides', () => {
ref: patternId,
overrides: {
[ editableParagraphId ]: {
content: [ 1, 'This one is different' ],
content: 'This one is different',
},
},
},
Expand Down Expand Up @@ -259,66 +259,4 @@ test.describe( 'Pattern Overrides', () => {
},
] );
} );

test( 'Supports `undefined` attribute values in patterns', async ( {
page,
admin,
editor,
requestUtils,
} ) => {
const buttonId = 'button-id';
const { id } = await requestUtils.createBlock( {
title: 'Pattern with overrides',
content: `<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"metadata":{"id":"${ buttonId }","bindings":{"text":{"source":"core/pattern-overrides"},"url":{"source":"core/pattern-overrides"},"linkTarget":{"source":"core/pattern-overrides"}}}} -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="http://wp.org" target="_blank" rel="noreferrer noopener">wp.org</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`,
status: 'publish',
} );

await admin.createNewPost();

await editor.insertBlock( {
name: 'core/block',
attributes: { ref: id },
} );

await editor.canvas
.getByRole( 'document', { name: 'Block: Button' } )
.getByRole( 'textbox', { name: 'Button text' } )
.focus();

await expect(
page.getByRole( 'link', { name: 'wp.org' } )
).toContainText( 'opens in a new tab' );

const openInNewTabCheckbox = page.getByRole( 'checkbox', {
name: 'Open in new tab',
} );
await expect( openInNewTabCheckbox ).toBeChecked();

await openInNewTabCheckbox.setChecked( false );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/block',
attributes: {
ref: id,
overrides: {
[ buttonId ]: {
linkTarget: [ 0 ],
},
},
},
},
] );

const postId = await editor.publishPost();
await page.goto( `/?p=${ postId }` );

const link = page.getByRole( 'link', { name: 'wp.org' } );
await expect( link ).toHaveAttribute( 'href', 'http://wp.org' );
await expect( link ).toHaveAttribute( 'target', '' );
} );
} );

1 comment on commit 05eb127

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 05eb127.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7724081794
📝 Reported issues:

Please sign in to comment.