Skip to content

Commit 98b7b86

Browse files
authored
Merge pull request #62 from adobe/video-autoplay
separate autoplay from placeholder, correct url params for embeds to w…
2 parents a8a2cf0 + 43ef1ef commit 98b7b86

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

blocks/video/video.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,71 @@
44
* https://www.hlx.live/developer/block-collection/video
55
*/
66

7-
function embedYoutube(url, autoplay) {
7+
function embedYoutube(url, replacePlaceholder, autoplay) {
88
const usp = new URLSearchParams(url.search);
9-
const suffix = autoplay ? '&muted=1&autoplay=1' : '';
9+
let suffix = '';
10+
if (replacePlaceholder || autoplay) {
11+
const suffixParams = {
12+
autoplay: '1',
13+
mute: autoplay ? '1' : '0',
14+
controls: autoplay ? '0' : '1',
15+
disablekb: autoplay ? '1' : '0',
16+
loop: autoplay ? '1' : '0',
17+
playsinline: autoplay ? '1' : '0',
18+
};
19+
suffix = `&${Object.entries(suffixParams).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}`;
20+
}
1021
let vid = usp.get('v') ? encodeURIComponent(usp.get('v')) : '';
1122
const embed = url.pathname;
1223
if (url.origin.includes('youtu.be')) {
1324
[, vid] = url.pathname.split('/');
1425
}
15-
return `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
26+
27+
const temp = document.createElement('div');
28+
temp.innerHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
1629
<iframe src="https://www.youtube.com${vid ? `/embed/${vid}?rel=0&v=${vid}${suffix}` : embed}" style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
1730
allow="autoplay; fullscreen; picture-in-picture; encrypted-media; accelerometer; gyroscope; picture-in-picture" allowfullscreen="" scrolling="no" title="Content from Youtube" loading="lazy"></iframe>
1831
</div>`;
32+
return temp.children.item(0);
1933
}
2034

21-
function embedVimeo(url, autoplay) {
35+
function embedVimeo(url, replacePlaceholder, autoplay) {
2236
const [, video] = url.pathname.split('/');
23-
const suffix = autoplay ? '?muted=1&autoplay=1' : '';
24-
return `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
37+
let suffix = '';
38+
if (replacePlaceholder || autoplay) {
39+
const suffixParams = {
40+
autoplay: '1',
41+
background: autoplay ? '1' : '0',
42+
};
43+
suffix = `?${Object.entries(suffixParams).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}`;
44+
}
45+
const temp = document.createElement('div');
46+
temp.innerHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
2547
<iframe src="https://player.vimeo.com/video/${video}${suffix}"
2648
style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
2749
frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen
2850
title="Content from Vimeo" loading="lazy"></iframe>
2951
</div>`;
52+
return temp.children.item(0);
3053
}
3154

32-
function getVideoElement(source, autoplay) {
55+
function getVideoElement(source, replacePlaceholder, autoplay) {
3356
const video = document.createElement('video');
3457
video.setAttribute('controls', '');
3558
video.dataset.loading = 'true';
3659
video.addEventListener('loadedmetadata', () => delete video.dataset.loading);
37-
if (autoplay) video.setAttribute('autoplay', '');
60+
if (autoplay || replacePlaceholder) {
61+
video.setAttribute('autoplay', '');
62+
if (autoplay) {
63+
video.setAttribute('loop', '');
64+
video.setAttribute('playsinline', '');
65+
video.removeAttribute('controls');
66+
video.addEventListener('canplay', () => {
67+
video.muted = true;
68+
video.play();
69+
});
70+
}
71+
}
3872

3973
const sourceEl = document.createElement('source');
4074
sourceEl.setAttribute('src', source);
@@ -44,7 +78,7 @@ function getVideoElement(source, autoplay) {
4478
return video;
4579
}
4680

47-
const loadVideoEmbed = (block, link, autoplay) => {
81+
const loadVideoEmbed = (block, link, replacePlaceholder, autoplay) => {
4882
if (block.dataset.embedIsLoaded) {
4983
return;
5084
}
@@ -54,14 +88,15 @@ const loadVideoEmbed = (block, link, autoplay) => {
5488
const isVimeo = link.includes('vimeo');
5589
const isMp4 = link.includes('.mp4');
5690

91+
let embedEl;
5792
if (isYoutube) {
58-
block.innerHTML = embedYoutube(url, autoplay);
93+
embedEl = embedYoutube(url, replacePlaceholder, autoplay);
5994
} else if (isVimeo) {
60-
block.innerHTML = embedVimeo(url, autoplay);
95+
embedEl = embedVimeo(url, replacePlaceholder, autoplay);
6196
} else if (isMp4) {
62-
block.textContent = '';
63-
block.append(getVideoElement(link, autoplay));
97+
embedEl = getVideoElement(link, replacePlaceholder, autoplay);
6498
}
99+
block.replaceChildren(embedEl);
65100

66101
block.dataset.embedIsLoaded = true;
67102
};
@@ -77,15 +112,15 @@ export default async function decorate(block) {
77112
wrapper.innerHTML = '<div class="video-placeholder-play"><button type="button" title="Play"></button></div>';
78113
wrapper.prepend(placeholder);
79114
wrapper.addEventListener('click', () => {
80-
loadVideoEmbed(block, link, true);
115+
loadVideoEmbed(block, link, true, false);
81116
});
82117
block.append(wrapper);
83118
} else {
84119
block.classList.add('lazy-loading');
85120
const observer = new IntersectionObserver((entries) => {
86121
if (entries.some((e) => e.isIntersecting)) {
87122
observer.disconnect();
88-
loadVideoEmbed(block, link, false);
123+
loadVideoEmbed(block, link, false, block.classList.contains('autoplay'));
89124
block.classList.remove('lazy-loading');
90125
}
91126
});

0 commit comments

Comments
 (0)