Skip to content

Commit 5121df0

Browse files
feat: SNI-6952 move cldPoster to html video layer
1 parent 195e259 commit 5121df0

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

packages/html/src/layers/htmlVideoLayer.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Plugins, HtmlPluginState, VideoSources, VideoType} from '../types.js'
22
import cloneDeep from 'lodash.clonedeep'
3-
import {CloudinaryVideo} from "@cloudinary/url-gen";
3+
import {CloudinaryImage, CloudinaryVideo} from "@cloudinary/url-gen";
44
import {render} from '../utils/render.js';
55
import {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
});

packages/react/src/AdvancedVideo.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class AdvancedVideo extends Component <VideoProps> {
8585
this.props.cldVid,
8686
this.props.sources,
8787
this.props.plugins,
88-
this.getVideoAttributes()
88+
this.getVideoAttributes(),
89+
this.props.cldPoster
8990
)
9091
}
9192

@@ -96,7 +97,13 @@ class AdvancedVideo extends Component <VideoProps> {
9697
componentDidUpdate() {
9798
cancelCurrentlyRunningPlugins(this.htmlVideoLayerInstance.htmlPluginState);
9899
// call html layer to update the dom again with plugins and reset toBeCanceled
99-
this.htmlVideoLayerInstance.update(this.props.cldVid, this.props.sources, this.props.plugins, this.getVideoAttributes())
100+
this.htmlVideoLayerInstance.update(
101+
this.props.cldVid,
102+
this.props.sources,
103+
this.props.plugins,
104+
this.getVideoAttributes(),
105+
this.props.cldPoster
106+
)
100107
}
101108

102109
/**
@@ -116,16 +123,7 @@ class AdvancedVideo extends Component <VideoProps> {
116123
if (key in this.props) {
117124
result[key] = this.props[key];
118125
}
119-
})
120-
if (this.props.cldPoster === 'auto') {
121-
result['poster'] = this.props.cldVid
122-
.quality('auto')
123-
.format('jpg')
124-
.addTransformation('so_auto')
125-
.toURL()
126-
} else if (this.props.cldPoster instanceof CloudinaryImage) {
127-
result['poster'] = this.props.cldPoster.toURL();
128-
}
126+
});
129127

130128
return result;
131129
}

0 commit comments

Comments
 (0)