-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
File: Add experimental integration with Interactivity API #50377
Changes from 1 commit
a407918
720bbe1
e12f9f4
4798578
2d1c63b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Temporary compatibility shims for block APIs present in Gutenberg. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Adds Interactivity API directives to the File block markup using the Tag Processor. | ||
* | ||
* @param string $block_content Markup of the File block. | ||
* @param array $block The full block, including name and attributes. | ||
* @param WP_Block $instance The block instance. | ||
* | ||
* @return string File block markup with the directives injected when applicable. | ||
*/ | ||
function gutenberg_block_core_file_add_directives_to_content( $block_content, $block, $instance ) { | ||
if ( empty( $instance->attributes['displayPreview'] ) ) { | ||
return $block_content; | ||
} | ||
$processor = new WP_HTML_Tag_Processor( $block_content ); | ||
$processor->next_tag(); | ||
$processor->set_attribute( 'data-wp-init', 'effects.core.file.init' ); | ||
return $processor->get_updated_html(); | ||
} | ||
add_filter( 'render_block_core/file', 'gutenberg_block_core_file_add_directives_to_content', 10, 3 ); | ||
|
||
/** | ||
* Replaces view script for the File block with version using Interactivity API. | ||
* | ||
* @param array $metadata Block metadata as read in via block.json. | ||
* | ||
* @return array Filtered block type metadata. | ||
*/ | ||
function gutenberg_block_core_file_update_view_script( $metadata ) { | ||
if ( 'core/file' === $metadata['name'] && str_contains( $metadata['file'], 'build/block-library/blocks' ) ) { | ||
$metadata['viewScript'] = array( 'file:./interactivity.min.js' ); | ||
} | ||
return $metadata; | ||
} | ||
add_filter( 'block_type_metadata', 'gutenberg_block_core_file_update_view_script', 10, 1 ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store } from '../../utils/interactivity'; | ||
import { hidePdfEmbedsOnUnsupportedBrowsers } from '../utils'; | ||
|
||
store( { | ||
effects: { | ||
core: { | ||
file: { | ||
init() { | ||
hidePdfEmbedsOnUnsupportedBrowsers(); | ||
}, | ||
}, | ||
}, | ||
}, | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,13 +221,14 @@ module.exports = [ | |
}, | ||
{ | ||
entry: { | ||
file: './packages/block-library/src/file/interactivity.js', | ||
navigation: | ||
'./packages/block-library/src/navigation/interactivity.js', | ||
}, | ||
output: { | ||
devtoolNamespace: 'wp', | ||
filename: './build/block-library/interactive-blocks/[name].min.js', | ||
path: join( __dirname, '..', '..' ), | ||
filename: './blocks/[name]/interactivity.min.js', | ||
path: join( __dirname, '..', '..', 'build', 'block-library' ), | ||
}, | ||
optimization: { | ||
runtimeChunk: { | ||
|
@@ -238,12 +239,14 @@ module.exports = [ | |
vendors: { | ||
name: 'vendors', | ||
test: /[\\/]node_modules[\\/]/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to change this to the list of dependencies, even if we have to do it manually. Maybe in another PR :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I recall correctly, @DAreRodz tried listing dependencies, but you also need to list their dependencies and so on. It might be difficult to maintain, but it's worth trying. |
||
filename: './interactivity/[name].min.js', | ||
minSize: 0, | ||
chunks: 'all', | ||
}, | ||
interactivity: { | ||
name: 'interactivity', | ||
runtime: { | ||
name: 'runtime', | ||
test: /[\\/]utils\/interactivity[\\/]/, | ||
filename: './interactivity/[name].min.js', | ||
chunks: 'all', | ||
minSize: 0, | ||
priority: -10, | ||
|
@@ -279,5 +282,12 @@ module.exports = [ | |
}, | ||
], | ||
}, | ||
plugins: [ | ||
...plugins, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Integrates webpack plugins used with all other configs:
|
||
new DependencyExtractionWebpackPlugin( { | ||
__experimentalInjectInteractivityRuntime: true, | ||
injectPolyfill: false, | ||
} ), | ||
].filter( Boolean ), | ||
}, | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to explore how we could automate registering the runtime based on the asset files that we use for all other scripts built with webpack. In particular, the version generate is very important as it only changes when the file's content changes, so it's essential for proper caching in WordPress core.