Skip to content

Commit

Permalink
Support block attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jun 7, 2023
1 parent 8bd4f28 commit 46e30e5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
17 changes: 11 additions & 6 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,27 @@ function gutenberg_register_auto_inserted_blocks( $settings, $metadata ) {
);

$auto_insert = $metadata['autoInsert'];
foreach ( $auto_insert as $block => $position ) {
foreach ( $auto_insert as $block_name => $block_data ) {
$position = $block_data['position'];
if ( ! isset( $property_mappings[ $position ] ) ) {
continue;
}

$mapped_position = $property_mappings[ $position ];

$inserted_block = array(
'blockName' => $metadata['name'],
'attrs' => $block_data['attrs'],
);
// TODO: In the long run, we'd likely want some sort of registry for auto-inserted blocks.
if ( 'before' === $mapped_position || 'after' === $mapped_position ) {
$inserter = gutenberg_auto_insert_blocks( $mapped_position, array( 'blockName' => $metadata['name'] ) );
add_filter( "render_block_$block", $inserter, 10, 2 );
$inserter = gutenberg_auto_insert_blocks( $mapped_position, $inserted_block );
add_filter( "render_block_$block_name", $inserter, 10, 2 );
} elseif ( 'first_child' === $mapped_position || 'last_child' === $mapped_position ) {
$inserter = gutenberg_auto_insert_child_block( $mapped_position, array( 'blockName' => $metadata['name'] ) );
add_filter( "render_block_data_$block", $inserter, 10, 2 );
$inserter = gutenberg_auto_insert_child_block( $mapped_position, $inserted_block );
add_filter( "render_block_data_$block_name", $inserter, 10, 2 );
}
$settings['auto_insert'][ $block ] = $mapped_position;
$settings['auto_insert'][ $block_name ] = $mapped_position;
}

return $settings;
Expand Down
13 changes: 11 additions & 2 deletions packages/block-library/src/avatar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@
"editorStyle": "wp-block-avatar",
"style": "wp-block-avatar",
"autoInsert": {
"core/post-content": "after",
"core/comment-template": "lastChild"
"core/comment-template": {
"position": "lastChild",
"attrs": {
"size": 40,
"style": {
"border": {
"radius": "10px"
}
}
}
}
}
}
11 changes: 10 additions & 1 deletion packages/block-library/src/social-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@
"reusable": false,
"html": false
},
"editorStyle": "wp-block-social-link-editor"
"editorStyle": "wp-block-social-link-editor",
"autoInsert": {
"core/post-content": {
"position": "after",
"attrs": {
"service": "wordpress",
"url": "https://wordpress.org/"
}
}
}
}
19 changes: 17 additions & 2 deletions schemas/json/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,24 @@
"description": "Blocks to auto-insert this block next to.",
"patternProperties": {
"[a-zA-Z]": {
"type": "string",
"type": "object",
"description": "Position relative to the block to auto-insert this block next to.",
"pattern": "^(nextSibling|previousSibling|firstChild|lastChild)$"
"properties": {
"position": {
"type": "string",
"description": "Position relative to the block to auto-insert this block next to.",
"enum": [
"before",
"after",
"firstChild",
"lastChild"
]
},
"attrs": {
"type": "object",
"description": "Attributes for the auto-inserted block."
}
}
}
}
}
Expand Down

0 comments on commit 46e30e5

Please sign in to comment.