11import { Plugins , HtmlPluginState , VideoSources , VideoType } from '../types.js'
22import cloneDeep from 'lodash.clonedeep'
3- import { CloudinaryVideo } from "@cloudinary/url-gen" ;
3+ import { CloudinaryImage , CloudinaryVideo } from "@cloudinary/url-gen" ;
44import { render } from '../utils/render.js' ;
55import { VIDEO_MIME_TYPES } from "../utils/internalConstants.js" ;
66
@@ -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 ?: CloudinaryImage | 'auto' ) {
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,21 @@ 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 ?: CloudinaryImage | 'auto' ) {
108+ if ( userCloudinaryPoster === 'auto' ) {
109+ videoAttributes [ 'poster' ] = this . originalVideo
110+ . quality ( 'auto' )
111+ . format ( 'jpg' )
112+ . addTransformation ( 'so_auto' )
113+ . toURL ( )
114+ } else if ( userCloudinaryPoster ) {
115+ videoAttributes [ 'poster' ] = userCloudinaryPoster . toURL ?.( ) ;
116+ }
117+ for ( const [ key , value ] of Object . entries ( videoAttributes ) ) {
118+ // Boolean attributes are considered to be true if they're present on the element at all.
119+ // You should set value to the empty string ("") or the attribute's name.
120+ // See https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
121+ value && this . videoElement . setAttribute ( key , key === 'poster' ? value : '' ) ;
115122 }
116123 }
117124
@@ -122,14 +129,14 @@ export class HtmlVideoLayer{
122129 * @param plugins
123130 * @param videoAttributes
124131 */
125- update ( updatedCloudinaryVideo : CloudinaryVideo , sources : VideoSources , plugins ?: Plugins , videoAttributes ?: object ) {
132+ update ( updatedCloudinaryVideo : CloudinaryVideo , sources : VideoSources , plugins ?: Plugins , videoAttributes ?: object , userCloudinaryPoster ?: CloudinaryImage | 'auto' ) {
126133 if ( updatedCloudinaryVideo !== this . originalVideo ) {
127134 const sourcesToDelete = this . videoElement . getElementsByTagName ( "SOURCE" ) ;
128135 while ( sourcesToDelete [ 0 ] ) sourcesToDelete [ 0 ] . parentNode . removeChild ( sourcesToDelete [ 0 ] ) ;
129136
130137 render ( this . videoElement , updatedCloudinaryVideo , plugins , this . htmlPluginState )
131138 . then ( ( ) => { // when resolved updates sources
132- this . setVideoAttributes ( videoAttributes ) ;
139+ this . setVideoAttributes ( videoAttributes , userCloudinaryPoster ) ;
133140 this . handleSourceToVideo ( updatedCloudinaryVideo , sources ) ;
134141 this . videoElement . load ( ) ;
135142 } ) ;
0 commit comments