4
4
* https://www.hlx.live/developer/block-collection/video
5
5
*/
6
6
7
- function embedYoutube ( url , autoplay ) {
7
+ function embedYoutube ( url , replacePlaceholder , autoplay ) {
8
8
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
+ }
10
21
let vid = usp . get ( 'v' ) ? encodeURIComponent ( usp . get ( 'v' ) ) : '' ;
11
22
const embed = url . pathname ;
12
23
if ( url . origin . includes ( 'youtu.be' ) ) {
13
24
[ , vid ] = url . pathname . split ( '/' ) ;
14
25
}
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%;">
16
29
<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;"
17
30
allow="autoplay; fullscreen; picture-in-picture; encrypted-media; accelerometer; gyroscope; picture-in-picture" allowfullscreen="" scrolling="no" title="Content from Youtube" loading="lazy"></iframe>
18
31
</div>` ;
32
+ return temp . children . item ( 0 ) ;
19
33
}
20
34
21
- function embedVimeo ( url , autoplay ) {
35
+ function embedVimeo ( url , replacePlaceholder , autoplay ) {
22
36
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%;">
25
47
<iframe src="https://player.vimeo.com/video/${ video } ${ suffix } "
26
48
style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;"
27
49
frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen
28
50
title="Content from Vimeo" loading="lazy"></iframe>
29
51
</div>` ;
52
+ return temp . children . item ( 0 ) ;
30
53
}
31
54
32
- function getVideoElement ( source , autoplay ) {
55
+ function getVideoElement ( source , replacePlaceholder , autoplay ) {
33
56
const video = document . createElement ( 'video' ) ;
34
57
video . setAttribute ( 'controls' , '' ) ;
35
58
video . dataset . loading = 'true' ;
36
59
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
+ }
38
72
39
73
const sourceEl = document . createElement ( 'source' ) ;
40
74
sourceEl . setAttribute ( 'src' , source ) ;
@@ -44,7 +78,7 @@ function getVideoElement(source, autoplay) {
44
78
return video ;
45
79
}
46
80
47
- const loadVideoEmbed = ( block , link , autoplay ) => {
81
+ const loadVideoEmbed = ( block , link , replacePlaceholder , autoplay ) => {
48
82
if ( block . dataset . embedIsLoaded ) {
49
83
return ;
50
84
}
@@ -54,14 +88,15 @@ const loadVideoEmbed = (block, link, autoplay) => {
54
88
const isVimeo = link . includes ( 'vimeo' ) ;
55
89
const isMp4 = link . includes ( '.mp4' ) ;
56
90
91
+ let embedEl ;
57
92
if ( isYoutube ) {
58
- block . innerHTML = embedYoutube ( url , autoplay ) ;
93
+ embedEl = embedYoutube ( url , replacePlaceholder , autoplay ) ;
59
94
} else if ( isVimeo ) {
60
- block . innerHTML = embedVimeo ( url , autoplay ) ;
95
+ embedEl = embedVimeo ( url , replacePlaceholder , autoplay ) ;
61
96
} else if ( isMp4 ) {
62
- block . textContent = '' ;
63
- block . append ( getVideoElement ( link , autoplay ) ) ;
97
+ embedEl = getVideoElement ( link , replacePlaceholder , autoplay ) ;
64
98
}
99
+ block . replaceChildren ( embedEl ) ;
65
100
66
101
block . dataset . embedIsLoaded = true ;
67
102
} ;
@@ -77,15 +112,15 @@ export default async function decorate(block) {
77
112
wrapper . innerHTML = '<div class="video-placeholder-play"><button type="button" title="Play"></button></div>' ;
78
113
wrapper . prepend ( placeholder ) ;
79
114
wrapper . addEventListener ( 'click' , ( ) => {
80
- loadVideoEmbed ( block , link , true ) ;
115
+ loadVideoEmbed ( block , link , true , false ) ;
81
116
} ) ;
82
117
block . append ( wrapper ) ;
83
118
} else {
84
119
block . classList . add ( 'lazy-loading' ) ;
85
120
const observer = new IntersectionObserver ( ( entries ) => {
86
121
if ( entries . some ( ( e ) => e . isIntersecting ) ) {
87
122
observer . disconnect ( ) ;
88
- loadVideoEmbed ( block , link , false ) ;
123
+ loadVideoEmbed ( block , link , false , block . classList . contains ( 'autoplay' ) ) ;
89
124
block . classList . remove ( 'lazy-loading' ) ;
90
125
}
91
126
} ) ;
0 commit comments