Skip to content

Commit

Permalink
Allow the heading block and the button block to be partially synced
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Dec 1, 2023
1 parent 16eb009 commit 5924426
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
21 changes: 12 additions & 9 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ function gutenberg_render_block_connections( $block_content, $block, $block_inst
// Allowlist of blocks that support block connections.
// Currently, we only allow the following blocks and attributes:
// - Paragraph: content.
// - Heading: content.
// - Button: text.
// - Image: url.
$blocks_attributes_allowlist = array(
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/button' => array( 'text' ),
'core/image' => array( 'url' ),
);

// Whitelist of the block types that support block connections.
// Currently, we only allow the Paragraph and Image blocks to use block connections.
if ( ! in_array( $block['blockName'], array_keys( $blocks_attributes_allowlist ), true ) ) {
return $block_content;
}
Expand Down Expand Up @@ -168,14 +171,14 @@ function gutenberg_render_block_connections( $block_content, $block, $block_inst
continue;
}

$tags = new WP_HTML_Tag_Processor( $block_content );
$found = $tags->next_tag(
array(
// TODO: In the future, when blocks other than Paragraph and Image are
// supported, we should build the full query from CSS selector.
'tag_name' => $block_type->attributes[ $attribute_name ]['selector'],
)
);
$selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] );
$found = false;
while ( ! $found && count( $selectors ) > 0 ) {
$tags = new WP_HTML_Tag_Processor( $block_content );
// TODO: In the future, when blocks are supported,
// we should build the full query from CSS selector.
$found = $tags->next_tag( trim( array_shift( $selectors ) ) );
}
if ( ! $found ) {
return $block_content;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
"width": true
}
},
"__experimentalSelector": ".wp-block-button .wp-block-button__link"
"__experimentalSelector": ".wp-block-button .wp-block-button__link",
"__experimentalConnections": true
},
"styles": [
{ "name": "fill", "label": "Fill", "isDefault": true },
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
}
},
"__unstablePasteTextInline": true,
"__experimentalSlashInserter": true
"__experimentalSlashInserter": true,
"__experimentalConnections": true
},
"editorStyle": "wp-block-heading-editor",
"style": "wp-block-heading"
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/hooks/pattern-partial-syncing.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {
/**
* Override the default edit UI to include a new block inspector control for
* assigning a partial syncing controls to supported blocks in the pattern editor.
* Currently, only the `core/paragraph` block is supported.
*
* @param {Component} BlockEdit Original component.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/patterns/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ 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' ) },
};

0 comments on commit 5924426

Please sign in to comment.