Skip to content

Commit

Permalink
Add video autoplay support in 'embed' block
Browse files Browse the repository at this point in the history
  • Loading branch information
Satya Deep Maheshwari committed Jun 3, 2024
1 parent 75f3bcd commit 4aaa411
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion blocks/embed/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,26 @@ const loadEmbed = (block, link, autoplay) => {
} else {
block.innerHTML = getDefaultEmbed(url);
block.classList = 'block embed';
const videoConfig = {
autoplay,
};
window.addEventListener('message', (event) => {
switch (event?.data?.name) {
case 'video-config':
event.source.window.postMessage(JSON.stringify(videoConfig), '*');
break;
default:
break;
}
});
}
block.classList.add('embed-is-loaded');
};

export default function decorate(block) {
const placeholder = block.querySelector('picture');
const link = block.querySelector('a').href;

block.textContent = '';

if (placeholder) {
Expand All @@ -105,7 +118,11 @@ export default function decorate(block) {
const observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
observer.disconnect();
loadEmbed(block, link);
// Check if link has a query parameter autoplay
const url = new URL(link);
const autoplay = url.searchParams.get('autoplay');

loadEmbed(block, link, autoplay);
}
});
observer.observe(block);
Expand Down

0 comments on commit 4aaa411

Please sign in to comment.