Skip to content

Commit 18f192e

Browse files
committed
Block Support: Add anchor support for dynamic blocks
1 parent 5cca67b commit 18f192e

File tree

63 files changed

+184
-61
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+184
-61
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 60 additions & 60 deletions

lib/block-supports/anchor.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Anchor block support flag.
4+
*
5+
* @package gutenberg
6+
*/
7+
8+
/**
9+
* Registers the anchor block attribute for block types that support it.
10+
*
11+
* @param WP_Block_Type $block_type Block Type.
12+
*/
13+
function gutenberg_register_anchor_support( $block_type ) {
14+
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
15+
16+
if ( ! $has_anchor_support ) {
17+
return;
18+
}
19+
20+
if ( ! $block_type->attributes ) {
21+
$block_type->attributes = array();
22+
}
23+
24+
if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
25+
$block_type->attributes['anchor'] = array(
26+
'type' => 'string',
27+
);
28+
}
29+
}
30+
31+
/**
32+
* Add the anchor to the output.
33+
*
34+
* @param WP_Block_Type $block_type Block Type.
35+
* @param array $block_attributes Block attributes.
36+
*
37+
* @return array Block anchor.
38+
*/
39+
function gutenberg_apply_anchor_support( $block_type, $block_attributes ) {
40+
if ( ! $block_attributes ) {
41+
return array();
42+
}
43+
44+
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
45+
if ( ! $has_anchor_support ) {
46+
return array();
47+
}
48+
49+
$has_anchor = array_key_exists( 'anchor', $block_attributes );
50+
if ( ! $has_anchor ) {
51+
return array();
52+
}
53+
return array( 'id' => $block_attributes['anchor'] );
54+
}
55+
56+
// Register the block support.
57+
WP_Block_Supports::get_instance()->register(
58+
'anchor',
59+
array(
60+
'register_attribute' => 'gutenberg_register_anchor_support',
61+
'apply' => 'gutenberg_apply_anchor_support',
62+
)
63+
);

lib/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ function gutenberg_is_experiment_enabled( $name ) {
151151
require __DIR__ . '/block-supports/shadow.php';
152152
require __DIR__ . '/block-supports/background.php';
153153
require __DIR__ . '/block-supports/block-style-variations.php';
154+
require __DIR__ . '/block-supports/anchor.php';
154155
require __DIR__ . '/block-supports/aria-label.php';
155156

156157
// Data views.

packages/block-library/src/archives/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"supports": {
2828
"align": true,
29+
"anchor": true,
2930
"__experimentalBorder": {
3031
"radius": true,
3132
"color": true,

packages/block-library/src/avatar/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"supports": {
2828
"html": false,
2929
"align": true,
30+
"anchor": true,
3031
"alignWide": false,
3132
"spacing": {
3233
"margin": true,

packages/block-library/src/calendar/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"supports": {
1919
"align": true,
20+
"anchor": true,
2021
"html": false,
2122
"color": {
2223
"link": true,

packages/block-library/src/categories/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"usesContext": [ "enhancedPagination" ],
4545
"supports": {
4646
"align": true,
47+
"anchor": true,
4748
"html": false,
4849
"spacing": {
4950
"margin": true,

packages/block-library/src/comment-author-avatar/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"usesContext": [ "commentId" ],
2222
"supports": {
23+
"anchor": true,
2324
"html": false,
2425
"inserter": false,
2526
"__experimentalBorder": {

packages/block-library/src/comment-author-name/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"usesContext": [ "commentId" ],
2424
"supports": {
25+
"anchor": true,
2526
"html": false,
2627
"spacing": {
2728
"margin": true,

packages/block-library/src/comment-content/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
}
1515
},
1616
"supports": {
17+
"anchor": true,
1718
"color": {
1819
"gradients": true,
1920
"link": true,

0 commit comments

Comments
 (0)