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

Use Interactivity API for frontend functionality #13704

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2ea017c
Update `block.json` with interactivity support
Swanand01 May 30, 2024
2c2fb3a
Add directives and pass context
Swanand01 May 30, 2024
4e8aea7
Add `viewScript`
Swanand01 May 30, 2024
8054547
Temporarily disable the use of the `Lightbox` class
Swanand01 May 30, 2024
4703cc5
Add directives to <amp-story=player /> in Embed and Singleton
Swanand01 Jun 6, 2024
88b77cf
Update package-lock.json
Swanand01 Jun 6, 2024
11714ea
Fix lightboxElement being null
Swanand01 Jun 6, 2024
a390a62
Add Interactivity API only on WP versions greater than 6.5
Swanand01 Jun 6, 2024
f156aa5
Uncomment creating Lightbox instances
Swanand01 Jun 6, 2024
a97a915
Change registered to enqueued
Swanand01 Jun 6, 2024
7e3350e
Remove interactive attributes from Embed and AMP context
Swanand01 Jun 7, 2024
6238a47
Add file entry for `stories-block-view` in `webpack.config.js`
Swanand01 Jun 11, 2024
279eae0
Remove `view.js` and add `index.js`
Swanand01 Jun 11, 2024
2e8315c
Change `viewScriptModule`
Swanand01 Jun 11, 2024
be38368
Add package and package-lock
Swanand01 Jun 11, 2024
b7612b6
Fix `package.json` linting issues
Swanand01 Jun 11, 2024
bdb779d
Use `get_bloginfo( 'version' )` instead of `global $wp_version`
Swanand01 Jun 12, 2024
9f376b9
Add entry for `webStoriesBlockView`
Swanand01 Jun 12, 2024
602d5d1
Update `viewScriptModule` with the built file
Swanand01 Jun 12, 2024
97c82f7
Use `wp_interactivity_process_directives` to support classic themes
Swanand01 Jun 12, 2024
9ac7bb0
Merge branch 'main' into use-interactivity-api-for-frontend
Swanand01 Jun 12, 2024
abae778
Remove `ReactRefreshWebpackPlugin()` from `webStoriesBlockView`
Swanand01 Jun 12, 2024
6420ad7
Add 'audio/aac' to test_get_allowed_mime_types_multisite
Swanand01 Jun 14, 2024
8f9577b
Remove 'audio/aac' from test_get_allowed_mime_types_multisite
Swanand01 Jun 14, 2024
3ad3319
Merge branch 'main' into use-interactivity-api-for-frontend
swissspidy Dec 5, 2024
6dab4fb
Remove pre 6.5 code
swissspidy Dec 5, 2024
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
6 changes: 4 additions & 2 deletions blocks/embed/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
}
},
"supports": {
"align": ["wide", "full", "left", "right", "center"]
}
"align": ["wide", "full", "left", "right", "center"],
"interactivity": true
},
"viewScriptModule": "file:../../assets/js/web-stories-block-view.js"
}
2 changes: 2 additions & 0 deletions includes/Renderer/Stories/Carousel_Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ function () {
<?php
$content = (string) ob_get_clean();

$content = wp_interactivity_process_directives( $content );

/**
* Filters the Carousel renderer stories content.
*
Expand Down
2 changes: 2 additions & 0 deletions includes/Renderer/Stories/Generic_Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ function () {
$view_type = $this->get_view_type();
$content = (string) ob_get_clean();

$content = wp_interactivity_process_directives( $content );

/**
* Filters the Generic renderer stories content.
*
Expand Down
37 changes: 26 additions & 11 deletions includes/Renderer/Stories/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
*/
public const STYLE_HANDLE = 'web-stories-list-styles';

/**
* Web Stories stylesheet handle.
*/
public const LIGHTBOX_SCRIPT_HANDLE = 'web-stories-lightbox';

/**
* Number of instances invoked. Kept it static to keep track.
*/
Expand Down Expand Up @@ -277,9 +272,6 @@
// Web Stories styles for AMP and non-AMP pages.
$this->assets->register_style_asset( self::STYLE_HANDLE );

// Web Stories lightbox script.
$this->assets->register_script_asset( self::LIGHTBOX_SCRIPT_HANDLE, [ AMP_Story_Player_Assets::SCRIPT_HANDLE ] );

if ( \defined( 'AMPFORWP_VERSION' ) ) {
add_action( 'amp_post_template_css', [ $this, 'add_amp_post_template_css' ] );
}
Expand Down Expand Up @@ -377,9 +369,22 @@
} else {
$this->assets->enqueue_style( AMP_Story_Player_Assets::SCRIPT_HANDLE );
$this->assets->enqueue_script( AMP_Story_Player_Assets::SCRIPT_HANDLE );
$this->assets->enqueue_script_asset( self::LIGHTBOX_SCRIPT_HANDLE );
?>
<div class="<?php echo esc_attr( $single_story_classes ); ?>">
<div
class="<?php echo esc_attr( $single_story_classes ); ?>"

Check warning on line 374 in includes/Renderer/Stories/Renderer.php

View check run for this annotation

Codecov / codecov/patch

includes/Renderer/Stories/Renderer.php#L373-L374

Added lines #L373 - L374 were not covered by tests
data-wp-interactive="web-stories-block"
<?php
echo esc_attr(
wp_interactivity_data_wp_context(
[
'instanceId' => $this->instance_id,
]
)
);

Check warning on line 383 in includes/Renderer/Stories/Renderer.php

View check run for this annotation

Codecov / codecov/patch

includes/Renderer/Stories/Renderer.php#L377-L383

Added lines #L377 - L383 were not covered by tests
?>
data-wp-on--click="actions.open"
data-wp-on-window--popstate="actions.onPopstate"
>

Check warning on line 387 in includes/Renderer/Stories/Renderer.php

View check run for this annotation

Codecov / codecov/patch

includes/Renderer/Stories/Renderer.php#L385-L387

Added lines #L385 - L387 were not covered by tests
<?php $this->render_story_with_poster(); ?>
</div>
<?php
Expand Down Expand Up @@ -410,7 +415,14 @@
];
?>
<div class="web-stories-list__lightbox">
<amp-story-player width="3.6" height="6" layout="responsive">
<amp-story-player
width="3.6"
height="6"
layout="responsive"
data-wp-interactive="web-stories-block"
data-wp-on--amp-story-player-close="actions.close"
data-wp-on--navigation="actions.navigation"
>

Check warning on line 425 in includes/Renderer/Stories/Renderer.php

View check run for this annotation

Codecov / codecov/patch

includes/Renderer/Stories/Renderer.php#L418-L425

Added lines #L418 - L425 were not covered by tests
<script type="application/json">
<?php echo wp_json_encode( $data ); ?>
</script>
Expand Down Expand Up @@ -832,6 +844,9 @@
width="3.6"
height="6"
layout="responsive"
data-wp-interactive="web-stories-block"
data-wp-on--amp-story-player-close="actions.close"
data-wp-on--navigation="actions.navigation"
>
<a href="<?php echo esc_url( $story->get_url() ); ?>" <?php $this->render_link_attributes(); ?>><?php echo esc_html( $story->get_title() ); ?></a>
</amp-story-player>
Expand Down
3 changes: 2 additions & 1 deletion includes/Renderer/Story/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public function render( array $args = [] ): string {
<amp-story-player
width="<?php echo esc_attr( $args['width'] ); ?>"
height="<?php echo esc_attr( $args['height'] ); ?>"
layout="intrinsic">
layout="intrinsic"
>
<a href="<?php echo esc_url( $url ); ?>">
<?php if ( $poster ) { ?>
<img
Expand Down
10 changes: 8 additions & 2 deletions includes/Renderer/Story/Singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use Google\Web_Stories\Assets;
use Google\Web_Stories\Embed_Base;
use Google\Web_Stories\Model\Story;
use Google\Web_Stories\Renderer\Stories\Renderer;

Check failure on line 35 in includes/Renderer/Story/Singleton.php

View workflow job for this annotation

GitHub Actions / Lint

Type Google\Web_Stories\Renderer\Stories\Renderer is not used in this file.

/**
* Class Singleton
Expand Down Expand Up @@ -115,7 +115,6 @@

$this->assets->enqueue_style( AMP_Story_Player_Assets::SCRIPT_HANDLE );
$this->assets->enqueue_script( AMP_Story_Player_Assets::SCRIPT_HANDLE );
$this->assets->enqueue_script_asset( Renderer::LIGHTBOX_SCRIPT_HANDLE );
$this->assets->enqueue_style_asset( Embed_Base::SCRIPT_HANDLE );

ob_start();
Expand Down Expand Up @@ -144,7 +143,14 @@
?>
<div class="web-stories-singleton__lightbox-wrapper <?php echo esc_attr( 'ws-lightbox-singleton-' . $this->instance_id ); ?>">
<div class="web-stories-singleton__lightbox">
<amp-story-player width="3.6" height="6" layout="responsive">
<amp-story-player
width="3.6"
height="6"
layout="responsive"
data-wp-interactive="web-stories-block"
data-wp-on--amp-story-player-close="actions.close"
data-wp-on--navigation="actions.navigation"
>
<script type="application/json">
<?php echo wp_json_encode( $data ); ?>
</script>
Expand Down
61 changes: 61 additions & 0 deletions package-lock.json

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

33 changes: 33 additions & 0 deletions packages/stories-block-view/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "stories-block-view",
"description": "Web Stories package to view a story",
"version": "1.0.0",
"author": {
"name": "Google",
"url": "https://creators.google/"
},
"license": "Apache-2.0",
"keywords": [
"web stories"
],
"homepage": "https://github.com/GoogleForCreators/web-stories-wp/blob/main/packages/stories-block-view/README.md",
"repository": {
"type": "git",
"url": "git+https://github.com/GoogleForCreators/web-stories-wp.git",
"directory": "packages/stories-block-view"
},
"bugs": {
"url": "https://github.com/GoogleForCreators/web-stories-wp/issues"
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"types": "dist-types/index.d.ts",
"source": "src/index.ts",
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"dependencies": {
"@wordpress/interactivity": "^6.0.0"
}
}
122 changes: 122 additions & 0 deletions packages/stories-block-view/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* WordPress dependencies
*/
import { store, getContext, getElement } from '@wordpress/interactivity';

const { state, actions } = store('web-stories-block', {

Check warning on line 22 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L22

Added line #L22 was not covered by tests
state: {
lightboxElement: null,
player: null,
stories: null,
instanceId: null,
currentLocation: null,
},
actions: {
storyContentReady: (storyObject, callback, maxRetries = 5) => {
const stateIntervalObj = setInterval(() => {
if (storyObject.storyContentLoaded === true) {
window.clearInterval(stateIntervalObj);
callback();

Check warning on line 35 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L32-L35

Added lines #L32 - L35 were not covered by tests
}
if (!--maxRetries) {
window.clearInterval(stateIntervalObj);

Check warning on line 38 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L37-L38

Added lines #L37 - L38 were not covered by tests
}
}, 250);
},
close: () => {
history.pushState({}, '', state.currentLocation);

Check warning on line 43 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L43

Added line #L43 was not covered by tests

const { player, lightboxElement } = state;
if (!player) {
return;

Check warning on line 47 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L45-L47

Added lines #L45 - L47 were not covered by tests
}

// Rewind the story and pause there upon closing the lightbox.
player.rewind();
player.pause();
player.mute();

Check warning on line 53 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L51-L53

Added lines #L51 - L53 were not covered by tests

lightboxElement.classList.toggle('show');
document.body.classList.toggle('web-stories-lightbox-open');

Check warning on line 56 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L55-L56

Added lines #L55 - L56 were not covered by tests
},
open: (event) => {
event.preventDefault();
const { ref: card } = getElement();
const context = JSON.parse(getContext().replaceAll("'", ''));

Check warning on line 61 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L59-L61

Added lines #L59 - L61 were not covered by tests
const lightboxElement =
document.querySelector(

Check warning on line 63 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L63

Added line #L63 was not covered by tests
`.ws-lightbox-${context.instanceId} .web-stories-list__lightbox`
) ||
document.querySelector(
`.ws-lightbox-${context.instanceId} .web-stories-singleton__lightbox`
);
const player = lightboxElement.querySelector('amp-story-player');
const stories = player.getStories();

Check warning on line 70 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L69-L70

Added lines #L69 - L70 were not covered by tests

state.lightboxElement = lightboxElement;
state.player = player;
state.stories = stories;
state.instanceId = context.instanceId;
state.currentLocation = location.href;

Check warning on line 76 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L72-L76

Added lines #L72 - L76 were not covered by tests

const storyObject = stories.find(
(story) => story.href === card.querySelector('a').href

Check warning on line 79 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L78-L79

Added lines #L78 - L79 were not covered by tests
);

player.show(storyObject.href);
player.play();

Check warning on line 83 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L82-L83

Added lines #L82 - L83 were not covered by tests

actions.storyContentReady(storyObject, () => {
history.pushState({}, '', storyObject.href);

Check warning on line 86 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L85-L86

Added lines #L85 - L86 were not covered by tests
});

lightboxElement.classList.toggle('show');
document.body.classList.toggle('web-stories-lightbox-open');

Check warning on line 90 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L89-L90

Added lines #L89 - L90 were not covered by tests
},
navigation: (event) => {
const storyObject = state.stories[event.detail.index];
if (storyObject && storyObject.href !== document.location.href) {
actions.storyContentReady(storyObject, () => {
history.pushState({}, '', storyObject.href);

Check warning on line 96 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L93-L96

Added lines #L93 - L96 were not covered by tests
});
}
},
onPopstate: () => {
const lightboxElement = state.lightboxElement;
const player = state.player;
const isLightboxOpen = lightboxElement.classList.contains('show');

Check warning on line 103 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L101-L103

Added lines #L101 - L103 were not covered by tests

const storyObject = state.stories.find(
(story) => story.href === document.location.href

Check warning on line 106 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L105-L106

Added lines #L105 - L106 were not covered by tests
);

if (storyObject) {
if (!isLightboxOpen) {
lightboxElement.classList.toggle('show');
document.body.classList.toggle('web-stories-lightbox-open');
player.play();

Check warning on line 113 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L109-L113

Added lines #L109 - L113 were not covered by tests
}

player.show(storyObject.href);
} else if (isLightboxOpen) {
actions.close();

Check warning on line 118 in packages/stories-block-view/src/index.js

View check run for this annotation

Codecov / codecov/patch

packages/stories-block-view/src/index.js#L116-L118

Added lines #L116 - L118 were not covered by tests
}
},
},
});
3 changes: 0 additions & 3 deletions packages/stories-lightbox/README.md

This file was deleted.

Loading
Loading