Skip to content

Commit

Permalink
Add the file and search blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Nov 14, 2023
1 parent 64fd8ac commit ac7ed7d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 49 deletions.
1 change: 0 additions & 1 deletion packages/block-library/src/file/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Up @@ -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' );
}
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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' );
}
}

Expand Down Expand Up @@ -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' );

/**
* Builds the correct top level classnames for the 'core/search' block.
Expand Down
2 changes: 2 additions & 0 deletions tools/webpack/interactivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ac7ed7d

Please sign in to comment.