1- import { Plugins , HtmlPluginState , VideoSources , VideoType } from '../types.js'
1+ import { Plugins , HtmlPluginState , VideoSources , VideoType , VideoPoster } from '../types.js'
22import cloneDeep from 'lodash.clonedeep'
33import { CloudinaryVideo } from "@cloudinary/url-gen" ;
44import { render } from '../utils/render.js' ;
@@ -13,7 +13,7 @@ export class HtmlVideoLayer{
1313 mimeType = 'video' ;
1414 mimeSubTypes = VIDEO_MIME_TYPES ;
1515
16- constructor ( element : HTMLVideoElement | null , userCloudinaryVideo : CloudinaryVideo , sources : VideoSources , plugins ?: Plugins , videoAttributes ?: object ) {
16+ constructor ( element : HTMLVideoElement | null , userCloudinaryVideo : CloudinaryVideo , sources : VideoSources , plugins ?: Plugins , videoAttributes ?: object , userCloudinaryPoster ?: VideoPoster ) {
1717 this . videoElement = element ;
1818 this . originalVideo = userCloudinaryVideo ;
1919 this . htmlPluginState = { cleanupCallbacks :[ ] , pluginEventSubscription : [ ] } ;
@@ -23,7 +23,7 @@ export class HtmlVideoLayer{
2323 . then ( ( ) => { // when resolved updates sources
2424 this . htmlPluginState . pluginEventSubscription . forEach ( fn => { fn ( ) } ) ;
2525
26- this . setVideoAttributes ( videoAttributes ) ;
26+ this . setVideoAttributes ( videoAttributes , userCloudinaryPoster ) ;
2727 this . handleSourceToVideo ( pluginCloudinaryVideo , sources )
2828 } ) ;
2929
@@ -104,14 +104,22 @@ export class HtmlVideoLayer{
104104 * In case of poster, sets the poster.
105105 * @param videoAttributes {object} Supported attributes: controls, loop, muted, poster, preload, autoplay, playsinline
106106 */
107- setVideoAttributes ( videoAttributes : object ) {
108- if ( videoAttributes ) {
109- for ( const [ key , value ] of Object . entries ( videoAttributes ) ) {
110- // Boolean attributes are considered to be true if they're present on the element at all.
111- // You should set value to the empty string ("") or the attribute's name.
112- // See https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
113- value && this . videoElement . setAttribute ( key , key === 'poster' ? value : '' ) ;
114- }
107+ setVideoAttributes ( videoAttributes : object = { } , userCloudinaryPoster ?: VideoPoster ) {
108+ if ( userCloudinaryPoster === 'auto' ) {
109+ const posterCloudinaryVideo = cloneDeep ( this . originalVideo ) ;
110+ videoAttributes [ 'poster' ] = posterCloudinaryVideo
111+ . quality ( 'auto' )
112+ . format ( 'jpg' )
113+ . addTransformation ( 'so_auto' )
114+ . toURL ( )
115+ } else if ( userCloudinaryPoster ) {
116+ videoAttributes [ 'poster' ] = userCloudinaryPoster . toURL ?.( ) ;
117+ }
118+ for ( const [ key , value ] of Object . entries ( videoAttributes ) ) {
119+ // Boolean attributes are considered to be true if they're present on the element at all.
120+ // You should set value to the empty string ("") or the attribute's name.
121+ // See https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
122+ value && this . videoElement . setAttribute ( key , key === 'poster' ? value : '' ) ;
115123 }
116124 }
117125
@@ -122,14 +130,14 @@ export class HtmlVideoLayer{
122130 * @param plugins
123131 * @param videoAttributes
124132 */
125- update ( updatedCloudinaryVideo : CloudinaryVideo , sources : VideoSources , plugins ?: Plugins , videoAttributes ?: object ) {
133+ update ( updatedCloudinaryVideo : CloudinaryVideo , sources : VideoSources , plugins ?: Plugins , videoAttributes ?: object , userCloudinaryPoster ?: VideoPoster ) {
126134 if ( updatedCloudinaryVideo !== this . originalVideo ) {
127135 const sourcesToDelete = this . videoElement . getElementsByTagName ( "SOURCE" ) ;
128136 while ( sourcesToDelete [ 0 ] ) sourcesToDelete [ 0 ] . parentNode . removeChild ( sourcesToDelete [ 0 ] ) ;
129137
130138 render ( this . videoElement , updatedCloudinaryVideo , plugins , this . htmlPluginState )
131139 . then ( ( ) => { // when resolved updates sources
132- this . setVideoAttributes ( videoAttributes ) ;
140+ this . setVideoAttributes ( videoAttributes , userCloudinaryPoster ) ;
133141 this . handleSourceToVideo ( updatedCloudinaryVideo , sources ) ;
134142 this . videoElement . load ( ) ;
135143 } ) ;
0 commit comments