Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Support providing Block Context from non-interactive blocks #28

Merged
merged 34 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6ae4acb
Added a new static block to test static context
michalczaplinski Jun 29, 2022
3dafea2
Add view.js file to static-block and add frontend:true to attributes
michalczaplinski Jun 29, 2022
48c45fa
Remove the console.log(context)
michalczaplinski Jun 29, 2022
aa2b32a
Remove unnecessary view file
michalczaplinski Jul 4, 2022
1391bc6
Add context from static parent in bhe-child block
michalczaplinski Jul 5, 2022
4a5dd5a
Update block registation in static-block
michalczaplinski Jul 5, 2022
63e02cd
Update the block schemas to include hydrate: true
michalczaplinski Jul 5, 2022
e3b0480
Serialize/deserialize context
michalczaplinski Jul 5, 2022
1fcf22a
Add RichText fields to change context in editor
michalczaplinski Jul 5, 2022
82db94a
Add display:contents to static-context
michalczaplinski Jul 7, 2022
d3b5ba4
Remove the text from <static-context>
michalczaplinski Jul 7, 2022
f9b6207
Block context: support nesting & initially hidden blocks
michalczaplinski Jul 7, 2022
680270e
Remove useless editorStyles
michalczaplinski Jul 12, 2022
50a8611
Import style.css in index.js of static-block
michalczaplinski Jul 12, 2022
7afec63
Improve styling of all blocks
michalczaplinski Jul 12, 2022
907d7a0
Pass content to save() and fix inner blockSave
michalczaplinski Jul 12, 2022
5f5108d
Add better comments
michalczaplinski Jul 13, 2022
f6f6f4e
Remove unnecessary comment
michalczaplinski Jul 13, 2022
dec23e8
Remove the `hydrate: true`
michalczaplinski Jul 13, 2022
1f30029
Remove setTimeout from the static-context element
michalczaplinski Jul 13, 2022
91bbec0
Merge branch 'main' into feature/static-context
luisherranz Jul 22, 2022
e3c1491
Fix serialization and event name
luisherranz Jul 22, 2022
3a21b5c
Rename Static block to match the others
luisherranz Jul 22, 2022
6b32c37
Merge branch 'main' into feature/static-context
luisherranz Jul 22, 2022
fc6fa92
Refactor block naming
luisherranz Jul 25, 2022
3fd3b33
Add prettier-php
luisherranz Jul 25, 2022
059674a
Add non-frontend attribute
luisherranz Jul 25, 2022
e26773d
Remove difference between interactive and non-interactive blocks
luisherranz Jul 25, 2022
b48a65c
Use PHP to serialize non-interactive block context
luisherranz Jul 25, 2022
1772d9d
Remove extra custom-element
luisherranz Jul 25, 2022
34199ed
Unify everything in `gutenberg-block`
luisherranz Jul 25, 2022
8bfb215
Use sourced attribute for non-interactive block context again
luisherranz Jul 25, 2022
d3d6ed3
Merge remote-tracking branch 'origin/main' into feature/static-context
luisherranz Jul 25, 2022
48f88aa
Add new button and extra block support
luisherranz Jul 25, 2022
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
131 changes: 79 additions & 52 deletions block-hydration-experiments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,110 @@
/**
* Plugin Name: block-hydration-experiments
* Version: 0.1.0
* Requires at least: 5.9
* Requires PHP: 7.0
* Requires at least: 6.0
* Requires PHP: 5.6
* Author: Gutenberg Team
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: block-hydration-experiments
*/
function block_hydration_experiments_init() {
register_block_type( plugin_dir_path( __FILE__ ) . 'build/blocks/block-hydration-experiments-child/' );
register_block_type( plugin_dir_path( __FILE__ ) . 'build/blocks/block-hydration-experiments-parent/' );
function block_hydration_experiments_init()
{
register_block_type(
plugin_dir_path(__FILE__) . 'build/blocks/interactive-child/'
);
register_block_type(
plugin_dir_path(__FILE__) . 'build/blocks/interactive-parent/'
);
register_block_type(
plugin_dir_path(__FILE__) . 'build/blocks/non-interactive-parent/'
);
}
add_action( 'init', 'block_hydration_experiments_init' );
add_action('init', 'block_hydration_experiments_init');

function bhe_block_wrapper( $block_content, $block, $instance ) {
function bhe_block_wrapper($block_content, $block, $instance)
{
// We might want to use a flag from block.json as the criterion here.
if ( ! in_array(
$block['blockName'],
array(
'bhe/block-hydration-experiments-parent',
'bhe/block-hydration-experiments-child'
),
true
) ) {
if (
!in_array(
$block['blockName'],
[
'bhe/interactive-parent',
'bhe/interactive-child',
'bhe/non-interactive-parent',
],
true
)
) {
return $block_content;
}

$block_type = $instance->block_type;
$attr_definitions = $block_type->attributes;

$attributes = array();
$sourced_attributes = array();
foreach( $attr_definitions as $attr => $definition ) {
if ( ! empty( $definition['frontend'] ) ) {
if ( ! empty( $definition['source'] ) ) {
$sourced_attributes[ $attr ] = array(
"selector" => $definition['selector'], // TODO: Guard against unset.
"source" => $definition['source']
);
$attributes = [];
$sourced_attributes = [];
foreach ($attr_definitions as $attr => $definition) {
if (!empty($definition['frontend'])) {
if (!empty($definition['source'])) {
$sourced_attributes[$attr] = [
'selector' => $definition['selector'], // TODO: Guard against unset.
'source' => $definition['source'],
];
} else {
if ( array_key_exists ( $attr, $block['attrs'] ) ) {
$attributes[ $attr ] = $block['attrs'][$attr];
} else if ( isset( $definition['default'] ) ) {
$attributes[ $attr ] = $definition['default'];
if (array_key_exists($attr, $block['attrs'])) {
$attributes[$attr] = $block['attrs'][$attr];
} elseif (isset($definition['default'])) {
$attributes[$attr] = $definition['default'];
}
}
}
}

$previous_block_to_render = WP_Block_Supports::$block_to_render;
// TODO: The following is a bit hacky. If we stick with this technique, we might
// We might want to use a flag from block.json as the criterion here.
$hydration_technique = in_array(
$block['blockName'],
['bhe/interactive-parent', 'bhe/interactive-child'],
true
)
? 'idle'
: false;

// The following is a bit hacky. If we stick with this technique, we might
// want to change apply_block_supports() to accepts a block as its argument.
$previous_block_to_render = WP_Block_Supports::$block_to_render;
WP_Block_Supports::$block_to_render = $block;
$block_supports_attributes = WP_Block_Supports::get_instance()->apply_block_supports();
$block_props = WP_Block_Supports::get_instance()->apply_block_supports();
WP_Block_Supports::$block_to_render = $previous_block_to_render;

$block_wrapper = sprintf(
'<gutenberg-interactive-block ' .
'data-gutenberg-block-type="%1$s" ' .
'data-gutenberg-uses-block-context="%2$s" ' .
'data-gutenberg-provides-block-context="%3$s" ' .
'data-gutenberg-attributes="%4$s" ' .
'data-gutenberg-sourced-attributes="%5$s" ' .
'data-gutenberg-block-props="%6$s" ' .
'data-gutenberg-hydrate="idle">',
esc_attr( $block['blockName'] ),
esc_attr( json_encode( $block_type->uses_context ) ),
esc_attr( json_encode( $block_type->provides_context ) ),
esc_attr( json_encode( $attributes ) ),
esc_attr( json_encode( $sourced_attributes ) ),
esc_attr( json_encode( $block_supports_attributes ) )
) . '%1$s</gutenberg-interactive-block>';
$block_wrapper =
sprintf(
'<gutenberg-block ' .
'data-gutenberg-block-type="%1$s" ' .
'data-gutenberg-uses-block-context="%2$s" ' .
'data-gutenberg-provides-block-context="%3$s" ' .
'data-gutenberg-attributes="%4$s" ' .
'data-gutenberg-sourced-attributes="%5$s" ' .
'data-gutenberg-block-props="%6$s" ' .
'data-gutenberg-hydration="%7$s">',
esc_attr($block['blockName']),
esc_attr(json_encode($block_type->uses_context)),
esc_attr(json_encode($block_type->provides_context)),
esc_attr(json_encode($attributes)),
esc_attr(json_encode($sourced_attributes)),
esc_attr(json_encode($block_props)),
esc_attr($hydration_technique)
) . '%1$s</gutenberg-block>';

$template_wrapper = '<template class="gutenberg-inner-blocks">%1$s</template>';
$template_wrapper =
'<template class="gutenberg-inner-blocks">%1$s</template>';

$empty_template = sprintf( $template_wrapper, '' );
$template = sprintf( $template_wrapper, sprintf( $block_wrapper, $block_content . $empty_template ) );
return sprintf( $block_wrapper, $block_content . $template );
$empty_template = sprintf($template_wrapper, '');
$template = sprintf(
$template_wrapper,
sprintf($block_wrapper, $block_content . $empty_template)
);
return sprintf($block_wrapper, $block_content);
}

add_filter( 'render_block', 'bhe_block_wrapper', 10, 3 );
add_filter('render_block', 'bhe_block_wrapper', 10, 3);
56 changes: 56 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
"bracketSameLine": false,
"bracketSpacing": true,
"semi": true,
"arrowParens": "always"
"arrowParens": "always",
"phpVersion": "5.6"
},
"devDependencies": {
"@babel/core": "^7.17.10",
"@babel/preset-env": "^7.17.10",
"@prettier/plugin-php": "^0.18.9",
"@types/jest": "^27.5.1",
"@wordpress/blocks": "^11.7.0",
"@wordpress/element": "^4.3.0",
Expand Down
9 changes: 0 additions & 9 deletions src/blocks/block-hydration-experiments-child/style.scss

This file was deleted.

32 changes: 0 additions & 32 deletions src/blocks/block-hydration-experiments-parent/edit.js

This file was deleted.

40 changes: 0 additions & 40 deletions src/blocks/block-hydration-experiments-parent/frontend/index.js

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions src/blocks/block-hydration-experiments-parent/style.scss

This file was deleted.

Loading