Skip to content

Commit

Permalink
Allow heading and button in Pattern Overrides (#57789)
Browse files Browse the repository at this point in the history
* Allow heading and button in Pattern Overrides

* Allow url for button
  • Loading branch information
kevin940726 authored Jan 17, 2024
1 parent 6c0cd11 commit e84b38d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/block-supports/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_pattern_support( $block_type ) {
$pattern_support = 'core/paragraph' === $block_type->name ? true : false;
global $block_bindings_allowed_blocks;
$pattern_support = array_key_exists( $block_type->name, $block_bindings_allowed_blocks );

if ( $pattern_support ) {
if ( ! $block_type->uses_context ) {
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@wordpress/interactivity": "file:../interactivity",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/notices": "file:../notices",
"@wordpress/patterns": "file:../patterns",
"@wordpress/primitives": "file:../primitives",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/reusable-blocks": "file:../reusable-blocks",
Expand Down
10 changes: 8 additions & 2 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
store as blockEditorStore,
BlockControls,
} from '@wordpress/block-editor';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { parse, cloneBlock } from '@wordpress/blocks';

/**
Expand All @@ -35,10 +36,13 @@ import { parse, cloneBlock } from '@wordpress/blocks';
import { unlock } from '../lock-unlock';

const { useLayoutClasses } = unlock( blockEditorPrivateApis );
const { PARTIAL_SYNCING_SUPPORTED_BLOCKS } = unlock( patternsPrivateApis );

function isPartiallySynced( block ) {
return (
'core/paragraph' === block.name &&
Object.keys( PARTIAL_SYNCING_SUPPORTED_BLOCKS ).includes(
block.name
) &&
!! block.attributes.metadata?.bindings &&
Object.values( block.attributes.metadata.bindings ).some(
( binding ) => binding.source.name === 'pattern_attributes'
Expand Down Expand Up @@ -100,7 +104,7 @@ function applyInitialOverrides( blocks, overrides = {}, defaultValues ) {
defaultValues[ blockId ] ??= {};
defaultValues[ blockId ][ attributeKey ] =
block.attributes[ attributeKey ];
if ( overrides[ blockId ] ) {
if ( overrides[ blockId ]?.[ attributeKey ] !== undefined ) {
newAttributes[ attributeKey ] =
overrides[ blockId ][ attributeKey ];
}
Expand Down Expand Up @@ -130,6 +134,8 @@ function getOverridesFromBlocks( blocks, defaultValues ) {
defaultValues[ blockId ][ attributeKey ]
) {
overrides[ blockId ] ??= {};
// 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
5 changes: 5 additions & 0 deletions packages/patterns/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export const PATTERN_SYNC_TYPES = {
// TODO: This should not be hardcoded. Maybe there should be a config and/or an UI.
export const PARTIAL_SYNCING_SUPPORTED_BLOCKS = {
'core/paragraph': { content: __( 'Content' ) },
'core/heading': { content: __( 'Content' ) },
'core/button': {
text: __( 'Text' ),
url: __( 'URL' ),
},
};

0 comments on commit e84b38d

Please sign in to comment.