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

Experiment: A Modules API with optional static server dependency graph #56118

Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add the file and search blocks
  • Loading branch information
luisherranz committed Nov 14, 2023
commit ac7ed7d62c5edc2a565aa87d7c6ff6ca25546f1e
1 change: 0 additions & 1 deletion packages/block-library/src/file/block.json
Original file line number Diff line number Diff line change
@@ -72,7 +72,6 @@
},
"interactivity": true
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-file-editor",
"style": "wp-block-file"
}
25 changes: 12 additions & 13 deletions packages/block-library/src/file/index.php
Original file line number Diff line number Diff line change
@@ -16,19 +16,8 @@
*/
function render_block_core_file( $attributes, $content, $block ) {
$should_load_view_script = ! empty( $attributes['displayPreview'] );
$view_js_file = 'wp-block-file-view';
// If the script already exists, there is no point in removing it from viewScript.
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;

// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( ! $should_load_view_script && in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
}
// If the script is needed, but it was previously removed, add it again.
if ( $should_load_view_script && ! in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
}
if ( $should_load_view_script ) {
gutenberg_enqueue_module( '@wordpress/block-library/file-block' );
}

// Update object's aria-label attribute if present in block HTML.
@@ -96,5 +85,15 @@ function register_block_core_file() {
'render_callback' => 'render_block_core_file',
)
);

gutenberg_register_module(
'@wordpress/block-library/file-block',
'/wp-content/plugins/gutenberg/build/interactivity/file.min.js',
'frontend',
array(
'version' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ),
'dependencies' => array( '@wordpress/interactivity' ),
)
);
}
add_action( 'init', 'register_block_core_file' );
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
@@ -638,7 +638,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {

$should_load_view_script = ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;

// Load the modules.
// Load the module.
if ( $should_load_view_script ) {
gutenberg_enqueue_module( '@wordpress/block-library/navigation-block' );
}
1 change: 0 additions & 1 deletion packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
@@ -91,7 +91,6 @@
},
"html": false
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-search-editor",
"style": "wp-block-search"
}
45 changes: 12 additions & 33 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
@@ -85,21 +85,9 @@ function render_block_core_search( $attributes, $content, $block ) {
// Adding these attributes manually is needed until the Interactivity API SSR logic is added to core.
$input->set_attribute( 'aria-hidden', 'true' );
$input->set_attribute( 'tabindex', '-1' );
}

// If the script already exists, there is no point in removing it from viewScript.
$view_js_file = 'wp-block-search-view';
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;

// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( ! $is_expandable_searchfield && in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
}
// If the script is needed, but it was previously removed, add it again.
if ( $is_expandable_searchfield && ! in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
}
// Load the module.
gutenberg_enqueue_module( '@wordpress/block-library/search-block' );
}
}

@@ -203,27 +191,18 @@ function register_block_core_search() {
'render_callback' => 'render_block_core_search',
)
);
}
add_action( 'init', 'register_block_core_search' );

/**
* Ensure that the view script has the `wp-interactivity` dependency.
*
* @since 6.4.0
*
* @global WP_Scripts $wp_scripts
*/
function block_core_search_ensure_interactivity_dependency() {
global $wp_scripts;
if (
isset( $wp_scripts->registered['wp-block-search-view'] ) &&
! in_array( 'wp-interactivity', $wp_scripts->registered['wp-block-search-view']->deps, true )
) {
$wp_scripts->registered['wp-block-search-view']->deps[] = 'wp-interactivity';
}
gutenberg_register_module(
'@wordpress/block-library/search-block',
'/wp-content/plugins/gutenberg/build/interactivity/search.min.js',
'frontend',
array(
'version' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ),
'dependencies' => array( '@wordpress/interactivity' ),
)
);
}

add_action( 'wp_print_scripts', 'block_core_search_ensure_interactivity_dependency' );
add_action( 'init', 'register_block_core_search' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I really like how this simplifies the code of all these "interactive" blocks.


/**
* Builds the correct top level classnames for the 'core/search' block.
2 changes: 2 additions & 0 deletions tools/webpack/interactivity.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@ module.exports = {
navigation: './packages/block-library/src/navigation/view.js',
query: './packages/block-library/src/query/view.js',
image: './packages/block-library/src/image/view.js',
file: './packages/block-library/src/file/view.js',
search: './packages/block-library/src/search/view.js',
},
experiments: {
outputModule: true,