Skip to content

Commit

Permalink
feat: add video
Browse files Browse the repository at this point in the history
  • Loading branch information
artKozinets committed Nov 7, 2024
1 parent c3b53ad commit 4a418c8
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
71 changes: 71 additions & 0 deletions blocks/video/video.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
.video {
text-align: center;
max-width: 900px;
margin: 24px auto;
}

.video[data-embed-loaded='false']:not(.placeholder) {
/* reserve an approximate space to avoid extensive layout shifts */
aspect-ratio: 16 / 9;
}

.video > div {
display: flex;
justify-content: center;
}

.video video {
max-width: 100%;
}

.video .video-placeholder {
width: 100%;
aspect-ratio: 16 / 9;
position: relative;
}

.video .video-placeholder > * {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
inset: 0;
}

.video[data-embed-loaded='true'] .video-placeholder,
.video[data-embed-loaded='false'] .video-placeholder + * {
visibility: hidden;
height: 0;
width: 0;
}

.video .video-placeholder picture img {
width: 100%;
height: 100%;
object-fit: cover;
}

.video .video-placeholder-play button {
position: relative;
display: block;
width: 44px;
height: 44px;
border-radius: 50%;
outline: 2px solid;
padding: 0;
}

.video .video-placeholder-play button::before {
content: '';
display: block;
box-sizing: border-box;
position: absolute;
width: 0;
height: 24px;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
border-left: 18px solid;
top: 50%;
left: calc(50% + 2px);
transform: translate(-50%, -50%);
}
145 changes: 145 additions & 0 deletions blocks/video/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Video Block
* Show a video referenced by a link
* https://www.hlx.live/developer/block-collection/video
*/

const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');

function embedYoutube(url, autoplay, background) {
const usp = new URLSearchParams(url.search);
let suffix = '';
if (background || autoplay) {
const suffixParams = {
autoplay: autoplay ? '1' : '0',
mute: background ? '1' : '0',
controls: background ? '0' : '1',
disablekb: background ? '1' : '0',
loop: background ? '1' : '0',
playsinline: background ? '1' : '0',
};
suffix = `&${Object.entries(suffixParams).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}`;
}
let vid = usp.get('v') ? encodeURIComponent(usp.get('v')) : '';
const embed = url.pathname;
if (url.origin.includes('youtu.be')) {
[, vid] = url.pathname.split('/');
}

const temp = document.createElement('div');
temp.innerHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
<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;"
allow="autoplay; fullscreen; picture-in-picture; encrypted-media; accelerometer; gyroscope; picture-in-picture" allowfullscreen="" scrolling="no" title="Content from Youtube" loading="lazy"></iframe>
</div>`;
return temp.children.item(0);
}

function embedVimeo(url, autoplay, background) {
const [, video] = url.pathname.split('/');
let suffix = '';
if (background || autoplay) {
const suffixParams = {
autoplay: autoplay ? '1' : '0',
background: background ? '1' : '0',
};
suffix = `?${Object.entries(suffixParams).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}`;
}
const temp = document.createElement('div');
temp.innerHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
<iframe src="https://player.vimeo.com/video/${video}${suffix}"
style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen
title="Content from Vimeo" loading="lazy"></iframe>
</div>`;
return temp.children.item(0);
}

function getVideoElement(source, autoplay, background) {
const video = document.createElement('video');
video.setAttribute('controls', '');
if (autoplay) video.setAttribute('autoplay', '');
if (background) {
video.setAttribute('loop', '');
video.setAttribute('playsinline', '');
video.removeAttribute('controls');
video.addEventListener('canplay', () => {
video.muted = true;
if (autoplay) video.play();
});
}

const sourceEl = document.createElement('source');
sourceEl.setAttribute('src', source);
sourceEl.setAttribute('type', `video/${source.split('.').pop()}`);
video.append(sourceEl);

return video;
}

const loadVideoEmbed = (block, link, autoplay, background) => {
if (block.dataset.embedLoaded === 'true') {
return;
}
const url = new URL(link);

const isYoutube = link.includes('youtube') || link.includes('youtu.be');
const isVimeo = link.includes('vimeo');

if (isYoutube) {
const embedWrapper = embedYoutube(url, autoplay, background);
block.append(embedWrapper);
embedWrapper.querySelector('iframe').addEventListener('load', () => {
block.dataset.embedLoaded = true;
});
} else if (isVimeo) {
const embedWrapper = embedVimeo(url, autoplay, background);
block.append(embedWrapper);
embedWrapper.querySelector('iframe').addEventListener('load', () => {
block.dataset.embedLoaded = true;
});
} else {
const videoEl = getVideoElement(link, autoplay, background);
block.append(videoEl);
videoEl.addEventListener('canplay', () => {
block.dataset.embedLoaded = true;
});
}
};

export default async function decorate(block) {
const placeholder = block.querySelector('picture');
const link = block.querySelector('a').href;
block.textContent = '';
block.dataset.embedLoaded = false;

const autoplay = block.classList.contains('autoplay');
if (placeholder) {
block.classList.add('placeholder');
const wrapper = document.createElement('div');
wrapper.className = 'video-placeholder';
wrapper.append(placeholder);

if (!autoplay) {
wrapper.insertAdjacentHTML(
'beforeend',
'<div class="video-placeholder-play"><button type="button" title="Play"></button></div>',
);
wrapper.addEventListener('click', () => {
wrapper.remove();
loadVideoEmbed(block, link, true, false);
});
}
block.append(wrapper);
}

if (!placeholder || autoplay) {
const observer = new IntersectionObserver((entries) => {
if (entries.some((e) => e.isIntersecting)) {
observer.disconnect();
const playOnLoad = autoplay && !prefersReducedMotion.matches;
loadVideoEmbed(block, link, playOnLoad, autoplay);
}
});
observer.observe(block);
}
}

0 comments on commit 4a418c8

Please sign in to comment.