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

Interactivity API: migration to the new store() API #55459

Merged
merged 66 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
a70b625
Copy new `store()` implementation
DAreRodz Oct 16, 2023
961bede
Enable TS for the interactivity API
DAreRodz Oct 16, 2023
81a7f46
Update directive bind tests
DAreRodz Oct 16, 2023
af4db12
Update runtime with latest changes
DAreRodz Oct 17, 2023
a5b3f95
Replace wp-effect tests with wp-watch tests
DAreRodz Oct 17, 2023
3192bf7
Update wp-body tests
DAreRodz Oct 17, 2023
bb63c83
Update wp-class tests
DAreRodz Oct 17, 2023
fad2b9d
Update wp-context tests
DAreRodz Oct 17, 2023
d9c1532
Update wp-init tests
DAreRodz Oct 17, 2023
da59bb8
Fix wp-key assignment
DAreRodz Oct 17, 2023
74b665a
Update wp-key tests
DAreRodz Oct 17, 2023
3f6b0b8
Update wp-on tests
DAreRodz Oct 17, 2023
a97a1f2
Update directive priorities tests
DAreRodz Oct 17, 2023
72f263a
Update directive slot tests
DAreRodz Oct 17, 2023
95181f6
Update wp-style tests
DAreRodz Oct 17, 2023
32e6d9e
Update wp-text tests
DAreRodz Oct 17, 2023
28ab539
Update negation operator tests
DAreRodz Oct 17, 2023
b067f8e
Update router navigate tests
DAreRodz Oct 17, 2023
7d8f587
Refactor tests for router regions
DAreRodz Oct 17, 2023
d93943a
Removed afterLoad tests
DAreRodz Oct 17, 2023
3a34ae1
Fix initial state tag id
DAreRodz Oct 17, 2023
4d2f9c8
Update initial state tests
DAreRodz Oct 17, 2023
6d35efb
Update tovdom-islands tests
DAreRodz Oct 17, 2023
3c3d00e
Update tovdom tests
DAreRodz Oct 17, 2023
519880f
Replace store with initial state in PHP
DAreRodz Oct 18, 2023
98afc53
Fix wp prefixes in initial state
DAreRodz Oct 18, 2023
c1bbf9f
Add types declaration to package.json so that TS works
michalczaplinski Oct 19, 2023
51bf723
Add `wp-each` directive in the new store API
SantosGuillamot Oct 19, 2023
933c4ac
Add workaround for store API SDR
SantosGuillamot Oct 20, 2023
05432dd
Modify children instead of replacing the element
DAreRodz Oct 24, 2023
7f517e0
Migrate File block to the new store API
DAreRodz Nov 9, 2023
261d9ba
Refactor Image block to use the new store API
DAreRodz Nov 9, 2023
905fe3c
Update runtime
DAreRodz Nov 14, 2023
8a9bd47
Fix file namespace
DAreRodz Nov 14, 2023
37aa4ce
Finish migrating the Image block
DAreRodz Nov 14, 2023
01a285a
Update Navigation block
DAreRodz Nov 14, 2023
9a8dacd
Replace effect with watch in Image
DAreRodz Nov 14, 2023
f4371e3
Fix namespace assignment
DAreRodz Nov 14, 2023
22044b7
Merge branch 'trunk' into try/interactivity-api-new-store-api
DAreRodz Nov 14, 2023
f30cc5d
Fix image state getter names
DAreRodz Nov 14, 2023
6f6e4d0
Fix getters in navigation state
DAreRodz Nov 14, 2023
e4210bb
Update Query block
DAreRodz Nov 14, 2023
3c69837
Fix directives for Navigation block
DAreRodz Nov 14, 2023
3fa96f0
Add missing store namespace
DAreRodz Nov 14, 2023
a16d957
Allow forward slashes in namespaces
DAreRodz Nov 14, 2023
b46004d
Migrate Search block
DAreRodz Nov 14, 2023
3466362
Merge branch 'trunk' into try/interactivity-api-new-store-api
DAreRodz Nov 27, 2023
7e5580c
Update Initial State and its tests
DAreRodz Nov 27, 2023
fe7453c
Revert "Add `wp-each` directive in the new store API"
DAreRodz Nov 27, 2023
b1b2c02
Revert "Update Initial State and its tests"
DAreRodz Nov 27, 2023
8043834
Revert "Replace store with initial state in PHP"
DAreRodz Nov 27, 2023
d07129d
Revert "Add workaround for store API SDR"
DAreRodz Nov 27, 2023
d2d2e94
Fix phpcs errors
DAreRodz Nov 27, 2023
ac52c77
Merge branch 'trunk' into try/interactivity-api-new-store-api
DAreRodz Nov 27, 2023
67cd96c
Update Query phpunit tests
DAreRodz Nov 27, 2023
f3db18d
Fix Query's Interactivity API context definition
DAreRodz Nov 27, 2023
cbc7a3f
Fix action name in Navigation
DAreRodz Nov 27, 2023
0924a46
Make `double()` prop a getter
DAreRodz Nov 28, 2023
83dd4b0
Fix `double()` prop definition
DAreRodz Nov 28, 2023
93d97b0
Rename effects to callbacks in tests
DAreRodz Nov 28, 2023
3010ab1
Rename effects to callbacks in blocks
DAreRodz Nov 28, 2023
931357f
Rename effects to callbacks in unit tests
DAreRodz Nov 28, 2023
bea1649
Move callbacks at the end of the store definition
DAreRodz Nov 28, 2023
c0664b6
Remove `layout-init` directive
DAreRodz Nov 28, 2023
9869b64
Add default args for `openMenu` and `closeMenu`
DAreRodz Nov 28, 2023
38f804b
Change Interactivity Store script tag ID
DAreRodz Nov 28, 2023
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
38 changes: 17 additions & 21 deletions lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,25 @@ private static function get_responsive_container_markup( $attributes, $inner_blo
$close_button_directives = '';
if ( $should_load_view_script ) {
$open_button_directives = '
data-wp-on--click="actions.core.navigation.openMenuOnClick"
data-wp-on--keydown="actions.core.navigation.handleMenuKeydown"
data-wp-on--click="actions.openMenuOnClick"
data-wp-on--keydown="actions.handleMenuKeydown"
';
$responsive_container_directives = '
data-wp-class--has-modal-open="selectors.core.navigation.isMenuOpen"
data-wp-class--is-menu-open="selectors.core.navigation.isMenuOpen"
data-wp-effect="effects.core.navigation.initMenu"
data-wp-on--keydown="actions.core.navigation.handleMenuKeydown"
data-wp-on--focusout="actions.core.navigation.handleMenuFocusout"
data-wp-class--has-modal-open="state.isMenuOpen"
data-wp-class--is-menu-open="state.isMenuOpen"
data-wp-watch="effects.initMenu"
data-wp-on--keydown="actions.handleMenuKeydown"
data-wp-on--focusout="actions.handleMenuFocusout"
tabindex="-1"
';
$responsive_dialog_directives = '
data-wp-bind--aria-modal="selectors.core.navigation.ariaModal"
data-wp-bind--aria-label="selectors.core.navigation.ariaLabel"
data-wp-bind--role="selectors.core.navigation.roleAttribute"
data-wp-effect="effects.core.navigation.focusFirstElement"
data-wp-bind--aria-modal="state.ariaModal"
data-wp-bind--aria-label="state.ariaLabel"
data-wp-bind--role="state.roleAttribute"
data-wp-watch="effects.focusFirstElement"
';
$close_button_directives = '
data-wp-on--click="actions.core.navigation.closeMenuOnClick"
data-wp-on--click="actions.closeMenuOnClick"
';
}

Expand Down Expand Up @@ -521,19 +521,15 @@ private static function get_nav_element_directives( $should_load_view_script ) {
// When adding to this array be mindful of security concerns.
$nav_element_context = wp_json_encode(
array(
'core' => array(
'navigation' => array(
'overlayOpenedBy' => array(),
'type' => 'overlay',
'roleAttribute' => '',
'ariaLabel' => __( 'Menu' ),
),
),
'overlayOpenedBy' => array(),
'type' => 'overlay',
'roleAttribute' => '',
'ariaLabel' => __( 'Menu' ),
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP
);
return '
data-wp-interactive
data-wp-interactive=\'{"namespace":"core/navigation"}\'
data-wp-context=\'' . $nav_element_context . '\'
';
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/file/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ static function ( $matches ) {
if ( $should_load_view_script ) {
$processor = new WP_HTML_Tag_Processor( $content );
$processor->next_tag();
$processor->set_attribute( 'data-wp-interactive', '' );
$processor->set_attribute( 'data-wp-interactive', '{"namespace":"core/file"}' );
$processor->next_tag( 'object' );
$processor->set_attribute( 'data-wp-bind--hidden', '!selectors.core.file.hasPdfPreview' );
$processor->set_attribute( 'data-wp-bind--hidden', '!state.hasPdfPreview' );
$processor->set_attribute( 'hidden', true );
return $processor->get_updated_html();
}
Expand Down
12 changes: 5 additions & 7 deletions packages/block-library/src/file/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { store } from '@wordpress/interactivity';
/**
* Internal dependencies
*/
import { browserSupportsPdfs as hasPdfPreview } from './utils';
import { browserSupportsPdfs } from './utils';

store( {
selectors: {
core: {
file: {
hasPdfPreview,
},
store( 'core/file', {
state: {
get hasPdfPreview() {
return browserSupportsPdfs();
},
},
} );
80 changes: 38 additions & 42 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,23 @@ function block_core_image_render_lightbox( $block_content, $block ) {
$w = new WP_HTML_Tag_Processor( $block_content );
$w->next_tag( 'figure' );
$w->add_class( 'wp-lightbox-container' );
$w->set_attribute( 'data-wp-interactive', true );
$w->set_attribute( 'data-wp-interactive', '{"namespace":"core/image"}' );

$w->set_attribute(
'data-wp-context',
sprintf(
'{ "core":
{ "image":
{ "imageLoaded": false,
"initialized": false,
"lightboxEnabled": false,
"hideAnimationEnabled": false,
"preloadInitialized": false,
"lightboxAnimation": "%s",
"imageUploadedSrc": "%s",
"imageCurrentSrc": "",
"targetWidth": "%s",
"targetHeight": "%s",
"scaleAttr": "%s",
"dialogLabel": "%s"
}
}
'{ "imageLoaded": false,
"initialized": false,
"lightboxEnabled": false,
"hideAnimationEnabled": false,
"preloadInitialized": false,
"lightboxAnimation": "%s",
"imageUploadedSrc": "%s",
"imageCurrentSrc": "",
"targetWidth": "%s",
"targetHeight": "%s",
"scaleAttr": "%s",
"dialogLabel": "%s"
}',
$lightbox_animation,
$img_uploaded_src,
Expand All @@ -218,14 +214,14 @@ function block_core_image_render_lightbox( $block_content, $block ) {
)
);
$w->next_tag( 'img' );
$w->set_attribute( 'data-wp-init', 'effects.core.image.initOriginImage' );
$w->set_attribute( 'data-wp-on--load', 'actions.core.image.handleLoad' );
$w->set_attribute( 'data-wp-effect', 'effects.core.image.setButtonStyles' );
$w->set_attribute( 'data-wp-init', 'effects.initOriginImage' );
$w->set_attribute( 'data-wp-on--load', 'actions.handleLoad' );
$w->set_attribute( 'data-wp-watch', 'effects.setButtonStyles' );
// We need to set an event callback on the `img` specifically
// because the `figure` element can also contain a caption, and
// we don't want to trigger the lightbox when the caption is clicked.
$w->set_attribute( 'data-wp-on--click', 'actions.core.image.showLightbox' );
$w->set_attribute( 'data-wp-effect--setStylesOnResize', 'effects.core.image.setStylesOnResize' );
$w->set_attribute( 'data-wp-on--click', 'actions.showLightbox' );
$w->set_attribute( 'data-wp-watch--setStylesOnResize', 'effects.setStylesOnResize' );
$body_content = $w->get_updated_html();

// Add a button alongside image in the body content.
Expand All @@ -239,9 +235,9 @@ class="lightbox-trigger"
type="button"
aria-haspopup="dialog"
aria-label="' . esc_attr( $aria_label ) . '"
data-wp-on--click="actions.core.image.showLightbox"
data-wp-style--right="context.core.image.imageButtonRight"
data-wp-style--top="context.core.image.imageButtonTop"
data-wp-on--click="actions.showLightbox"
data-wp-style--right="context.imageButtonRight"
data-wp-style--top="context.imageButtonTop"
>
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
Expand All @@ -267,8 +263,8 @@ class="lightbox-trigger"
// use the exact same image as in the content when the lightbox is first opened while
// we wait for the larger image to load.
$m->set_attribute( 'src', '' );
$m->set_attribute( 'data-wp-bind--src', 'context.core.image.imageCurrentSrc' );
$m->set_attribute( 'data-wp-style--object-fit', 'selectors.core.image.lightboxObjectFit' );
$m->set_attribute( 'data-wp-bind--src', 'context.imageCurrentSrc' );
$m->set_attribute( 'data-wp-style--object-fit', 'state.lightboxObjectFit' );
$initial_image_content = $m->get_updated_html();

$q = new WP_HTML_Tag_Processor( $block_content );
Expand All @@ -283,8 +279,8 @@ class="lightbox-trigger"
// and Chrome (see https://github.com/WordPress/gutenberg/pull/52765#issuecomment-1674008151). Until that
// is resolved, manually setting the 'src' seems to be the best solution to load the large image on demand.
$q->set_attribute( 'src', '' );
$q->set_attribute( 'data-wp-bind--src', 'selectors.core.image.enlargedImgSrc' );
$q->set_attribute( 'data-wp-style--object-fit', 'selectors.core.image.lightboxObjectFit' );
$q->set_attribute( 'data-wp-bind--src', 'state.enlargedImgSrc' );
$q->set_attribute( 'data-wp-style--object-fit', 'state.lightboxObjectFit' );
$enlarged_image_content = $q->get_updated_html();

// If the current theme does NOT have a `theme.json`, or the colors are not defined,
Expand All @@ -307,21 +303,21 @@ class="lightbox-trigger"

$lightbox_html = <<<HTML
<div data-wp-body="" class="wp-lightbox-overlay $lightbox_animation"
data-wp-bind--role="selectors.core.image.roleAttribute"
data-wp-bind--aria-label="selectors.core.image.dialogLabel"
data-wp-class--initialized="context.core.image.initialized"
data-wp-class--active="context.core.image.lightboxEnabled"
data-wp-class--hideAnimationEnabled="context.core.image.hideAnimationEnabled"
data-wp-bind--aria-modal="selectors.core.image.ariaModal"
data-wp-effect="effects.core.image.initLightbox"
data-wp-on--keydown="actions.core.image.handleKeydown"
data-wp-on--touchstart="actions.core.image.handleTouchStart"
data-wp-on--touchmove="actions.core.image.handleTouchMove"
data-wp-on--touchend="actions.core.image.handleTouchEnd"
data-wp-on--click="actions.core.image.hideLightbox"
data-wp-bind--role="state.roleAttribute"
data-wp-bind--aria-label="state.dialogLabel"
data-wp-class--initialized="context.initialized"
data-wp-class--active="context.lightboxEnabled"
data-wp-class--hideAnimationEnabled="context.hideAnimationEnabled"
data-wp-bind--aria-modal="state.ariaModal"
data-wp-watch="effects.initLightbox"
data-wp-on--keydown="actions.handleKeydown"
data-wp-on--touchstart="actions.handleTouchStart"
data-wp-on--touchmove="actions.handleTouchMove"
data-wp-on--touchend="actions.handleTouchEnd"
data-wp-on--click="actions.hideLightbox"
tabindex="-1"
>
<button type="button" aria-label="$close_button_label" style="fill: $close_button_color" class="close-button" data-wp-on--click="actions.core.image.hideLightbox">
<button type="button" aria-label="$close_button_label" style="fill: $close_button_color" class="close-button" data-wp-on--click="actions.hideLightbox">
$close_button_icon
</button>
<div class="lightbox-image-container">$initial_image_content</div>
Expand Down
Loading
Loading