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

Add anchor support to dynamic blocks #24699

Closed
wants to merge 3 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
50 changes: 50 additions & 0 deletions lib/block-supports/anchor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Anchor block support flag.
*
* @package gutenberg
*/

/**
* Registers the anchor block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_anchor_support( $block_type ) {
$has_anchor_support = gutenberg_experimental_get( $block_type->supports, array( 'anchor' ), true );

if ( $has_anchor_support ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
$block_type->attributes['anchor'] = array(
'type' => 'string',
);
}
}

}

/**
* Add the anchor to the output.
*
* @param array $attributes Comprehensive list of attributes to be applied.
* @param array $block_attributes Block attributes.
* @param WP_Block_Type $block_type Block Type.
*
* @return array Block anchor.
*/
function gutenberg_apply_anchor_support( $attributes, $block_attributes, $block_type ) {
$has_anchor_support = gutenberg_experimental_get( $block_type->supports, array( 'anchor' ), true );
if ( $has_anchor_support ) {
$has_anchor = array_key_exists( 'anchor', $block_attributes );

if ( $has_anchor ) {
$attributes['anchor'] = $block_attributes['anchor'];
}
}

return $attributes;
}
8 changes: 7 additions & 1 deletion lib/block-supports/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function gutenberg_register_block_supports() {
gutenberg_register_colors_support( $block_type );
gutenberg_register_typography_support( $block_type );
gutenberg_register_custom_classname_support( $block_type );
gutenberg_register_anchor_support( $block_type );
}
}

Expand Down Expand Up @@ -47,6 +48,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) {
$attributes = gutenberg_apply_typography_support( $attributes, $block['attrs'], $block_type );
$attributes = gutenberg_apply_alignment_support( $attributes, $block['attrs'], $block_type );
$attributes = gutenberg_apply_custom_classname_support( $attributes, $block['attrs'], $block_type );
$attributes = gutenberg_apply_anchor_support( $attributes, $block['attrs'], $block_type );

if ( ! count( $attributes ) ) {
return $block_content;
Expand Down Expand Up @@ -81,7 +83,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) {
$styles_to_add = array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array();
$new_styles = array_unique( array_map( 'gutenberg_normalize_css_rule', array_filter( array_merge( $current_styles, $styles_to_add ) ) ) );

// Apply new styles and classes.
// Apply new styles, classes and anchor.
if ( ! empty( $new_classes ) ) {
$block_root->setAttribute( 'class', esc_attr( implode( ' ', $new_classes ) ) );
}
Expand All @@ -90,6 +92,10 @@ function gutenberg_apply_block_supports( $block_content, $block ) {
$block_root->setAttribute( 'style', esc_attr( implode( '; ', $new_styles ) . ';' ) );
}

if ( ! empty( $attributes['anchor'] ) ) {
$block_root->setAttribute( 'id', esc_attr( $attributes['anchor'] ) );
}

return str_replace( array( $wrapper_left, $wrapper_right ), '', $dom->saveHtml() );
}
add_filter( 'render_block', 'gutenberg_apply_block_supports', 10, 2 );
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ function gutenberg_is_experiment_enabled( $name ) {
require dirname( __FILE__ ) . '/block-supports/typography.php';
require dirname( __FILE__ ) . '/block-supports/custom-classname.php';
require dirname( __FILE__ ) . '/block-supports/generated-classname.php';
require dirname( __FILE__ ) . '/block-supports/anchor.php';
14 changes: 10 additions & 4 deletions packages/block-editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ export function addAttribute( settings ) {
}
if ( hasBlockSupport( settings, 'anchor' ) ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
anchor: {
let anchor = {
type: 'string',
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure why we need this line, this should already be added with PHP.

};
if ( settings.save.length ) {
anchor = {
type: 'string',
source: 'attribute',
attribute: 'id',
selector: '*',
},
};
}
settings.attributes = {
...settings.attributes,
anchor,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/archives/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false
}
}
3 changes: 2 additions & 1 deletion packages/block-library/src/calendar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
},
"supports": {
"align": true
"align": true,
"anchor": true
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/latest-comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/rss/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false
}
}
3 changes: 2 additions & 1 deletion packages/block-library/src/tag-cloud/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"supports": {
"html": false,
"align": true
"align": true,
"anchor": true
}
}