66
77import { loadScript } from '../../scripts/aem.js' ;
88
9- const getDefaultEmbed = ( url ) => `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;">
9+ const getEmbedContainerStyle = ( fixedHeight ) => ( fixedHeight
10+ ? `left: 0; width: 100%; height: ${ fixedHeight } px; position: relative;`
11+ : 'left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%;' ) ;
12+
13+ const getDefaultEmbed = ( url , fixedHeight ) => `<div style="${ getEmbedContainerStyle ( fixedHeight ) } ">
1014 <iframe src="${ url . href } " style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;" allowfullscreen=""
1115 scrolling="no" allow="encrypted-media" title="Content from ${ url . hostname } " loading="lazy">
1216 </iframe>
1317 </div>` ;
1418
15- const embedYoutube = ( url , autoplay ) => {
19+ const embedYoutube = ( url , autoplay , fixedHeight ) => {
1620 const usp = new URLSearchParams ( url . search ) ;
1721 const suffix = autoplay ? '&muted=1&autoplay=1' : '' ;
1822 let vid = usp . get ( 'v' ) ? encodeURIComponent ( usp . get ( 'v' ) ) : '' ;
1923 const embed = url . pathname ;
2024 if ( url . origin . includes ( 'youtu.be' ) ) {
2125 [ , vid ] = url . pathname . split ( '/' ) ;
2226 }
23- const embedHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%; ">
27+ const embedHTML = `<div style="${ getEmbedContainerStyle ( fixedHeight ) } ">
2428 <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;"
2529 allow="autoplay; fullscreen; picture-in-picture; encrypted-media; accelerometer; gyroscope; picture-in-picture" allowfullscreen="" scrolling="no" title="Content from Youtube" loading="lazy"></iframe>
2630 </div>` ;
2731 return embedHTML ;
2832} ;
2933
30- const embedVimeo = ( url , autoplay ) => {
34+ const embedVimeo = ( url , autoplay , fixedHeight ) => {
3135 const [ , video ] = url . pathname . split ( '/' ) ;
3236 const suffix = autoplay ? '?muted=1&autoplay=1' : '' ;
33- const embedHTML = `<div style="left: 0; width: 100%; height: 0; position: relative; padding-bottom: 56.25%; ">
37+ const embedHTML = `<div style="${ getEmbedContainerStyle ( fixedHeight ) } ">
3438 <iframe src="https://player.vimeo.com/video/${ video } ${ suffix } "
3539 style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
3640 frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen
@@ -45,7 +49,17 @@ const embedTwitter = (url) => {
4549 return embedHTML ;
4650} ;
4751
48- const loadEmbed = ( block , link , autoplay ) => {
52+ const getFixedHeight = ( block ) => {
53+ const fixedHeightClass = [ ...block . classList ]
54+ . find ( ( className ) => / ^ (?: f i x e d h e i g h t - | f i x e d - h e i g h t - | h - ) ? \d + (?: p x ) ? $ / i. test ( className ) ) ;
55+ if ( ! fixedHeightClass ) return null ;
56+
57+ const match = fixedHeightClass . match ( / ( \d + ) / ) ;
58+ const value = match ? Number ( match [ 1 ] ) : NaN ;
59+ return Number . isFinite ( value ) && value > 0 ? value : null ;
60+ } ;
61+
62+ const loadEmbed = ( block , link , autoplay , fixedHeight ) => {
4963 if ( block . classList . contains ( 'embed-is-loaded' ) ) {
5064 return ;
5165 }
@@ -68,10 +82,10 @@ const loadEmbed = (block, link, autoplay) => {
6882 const config = EMBEDS_CONFIG . find ( ( e ) => e . match . some ( ( match ) => link . includes ( match ) ) ) ;
6983 const url = new URL ( link ) ;
7084 if ( config ) {
71- block . innerHTML = config . embed ( url , autoplay ) ;
85+ block . innerHTML = config . embed ( url , autoplay , fixedHeight ) ;
7286 block . classList = `block embed embed-${ config . match [ 0 ] } ` ;
7387 } else {
74- block . innerHTML = getDefaultEmbed ( url ) ;
88+ block . innerHTML = getDefaultEmbed ( url , fixedHeight ) ;
7589 block . classList = 'block embed' ;
7690 }
7791 block . classList . add ( 'embed-is-loaded' ) ;
@@ -80,22 +94,27 @@ const loadEmbed = (block, link, autoplay) => {
8094export default function decorate ( block ) {
8195 const placeholder = block . querySelector ( 'picture' ) ;
8296 const link = block . querySelector ( 'a' ) . href ;
97+ const fixedHeight = getFixedHeight ( block ) ;
8398 block . textContent = '' ;
8499
85100 if ( placeholder ) {
86101 const wrapper = document . createElement ( 'div' ) ;
87102 wrapper . className = 'embed-placeholder' ;
103+ if ( fixedHeight ) {
104+ wrapper . style . aspectRatio = 'auto' ;
105+ wrapper . style . height = `${ fixedHeight } px` ;
106+ }
88107 wrapper . innerHTML = '<div class="embed-placeholder-play"><button type="button" title="Play"></button></div>' ;
89108 wrapper . prepend ( placeholder ) ;
90109 wrapper . addEventListener ( 'click' , ( ) => {
91- loadEmbed ( block , link , true ) ;
110+ loadEmbed ( block , link , true , fixedHeight ) ;
92111 } ) ;
93112 block . append ( wrapper ) ;
94113 } else {
95114 const observer = new IntersectionObserver ( ( entries ) => {
96115 if ( entries . some ( ( e ) => e . isIntersecting ) ) {
97116 observer . disconnect ( ) ;
98- loadEmbed ( block , link ) ;
117+ loadEmbed ( block , link , false , fixedHeight ) ;
99118 }
100119 } ) ;
101120 observer . observe ( block ) ;
0 commit comments