Skip to content

Commit 4ad73c5

Browse files
authored
feat: CldVideoPlayer - getVideoPlayerOptions (#477)
# Description Updates CldVideoPlayer to use getVideoPlayerProps from the @cloudinary-util/url-loader package to abstract how we ultimately construct the player options object. This will allow other frameworks (Nuxt, Svelte) to share the same code and maintain parity at a higher rate. <!-- Specify above which issue this fixes by referencing the issue number (`#<ISSUE_NUMBER>`) or issue URL. --> <!-- Example: Fixes https://github.com/cloudinary-community/next-cloudinary/issues/<ISSUE_NUMBER> --> ## Type of change <!-- Please select all options that are applicable. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Fix or improve the documentation - [ ] This change requires a documentation update # Checklist <!-- These must all be followed and checked. --> - [ ] I have followed the contributing guidelines of this project as mentioned in [CONTRIBUTING.md](/CONTRIBUTING.md) - [ ] I have created an [issue](https://github.com/cloudinary-community/next-cloudinary/issues) ticket for this PR - [ ] I have checked to ensure there aren't other open [Pull Requests](https://github.com/cloudinary-community/next-cloudinary/pulls) for the same update/change? - [ ] I have performed a self-review of my own code - [ ] I have run tests locally to ensure they all pass - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes needed to the documentation
1 parent 6ac19a3 commit 4ad73c5

File tree

3 files changed

+19
-154
lines changed

3 files changed

+19
-154
lines changed

next-cloudinary/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"test:watch": "vitest"
1717
},
1818
"dependencies": {
19-
"@cloudinary-util/types": "1.0.3",
20-
"@cloudinary-util/url-loader": "5.2.2",
19+
"@cloudinary-util/types": "1.0.5",
20+
"@cloudinary-util/url-loader": "5.3.0",
2121
"@cloudinary-util/util": "^3.0.0",
2222
"@tsconfig/recommended": "^1.0.3"
2323
},

next-cloudinary/src/components/CldVideoPlayer/CldVideoPlayer.tsx

Lines changed: 7 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React, {useRef, MutableRefObject, useEffect} from 'react';
22
import Script from 'next/script';
33
import Head from 'next/head';
4-
import { parseUrl } from '@cloudinary-util/util';
5-
import { CloudinaryVideoPlayer, CloudinaryVideoPlayerOptionsLogo, CloudinaryVideoPlayerOptions, } from '@cloudinary-util/types';
4+
import { CloudinaryVideoPlayer } from '@cloudinary-util/types';
5+
import { getVideoPlayerOptions } from '@cloudinary-util/url-loader';
66

77
import { CldVideoPlayerProps } from './CldVideoPlayer.types';
88

9-
import { getCldImageUrl } from '../../helpers/getCldImageUrl';
10-
import { getCldVideoUrl } from '../../helpers/getCldVideoUrl';
119
import { getCloudinaryConfig } from "../../lib/cloudinary";
1210

1311
let playerInstances: string[] = [];
@@ -17,56 +15,28 @@ const PLAYER_VERSION = '1.11.1';
1715
const CldVideoPlayer = (props: CldVideoPlayerProps) => {
1816

1917
const {
20-
autoplay,
2118
className,
22-
colors,
2319
config,
24-
controls = true,
25-
fontFace,
2620
height,
2721
id,
28-
language,
29-
languages,
30-
logo = true,
31-
loop = false,
32-
muted = false,
3322
onDataLoad,
3423
onError,
3524
onMetadataLoad,
3625
onPause,
3726
onPlay,
3827
onEnded,
39-
poster,
40-
src,
41-
sourceTypes,
42-
transformation,
43-
quality = 'auto',
4428
width,
45-
...otherCldVidPlayerOptions
4629
} = props as CldVideoPlayerProps;
4730

48-
const playerTransformations = Array.isArray(transformation) ? transformation : [transformation];
49-
let publicId: string = src || "";
5031

32+
const cloudinaryConfig = getCloudinaryConfig(config);
33+
const playerOptions = getVideoPlayerOptions(props, cloudinaryConfig);
34+
const { publicId } = playerOptions;
5135

52-
// If the publicId/src is a URL, attempt to parse it as a Cloudinary URL
53-
// to get the public ID alone
54-
55-
if ( publicId.startsWith('http') ) {
56-
try {
57-
const parts = parseUrl(src);
58-
if ( typeof parts?.publicId === 'string' ) {
59-
publicId = parts?.publicId;
60-
}
61-
} catch(e) {}
36+
if ( typeof publicId === 'undefined' ) {
37+
throw new Error('Video Player requires a Public ID or Cloudinary URL - please specify a src prop');
6238
}
6339

64-
// Set default transformations for the player
65-
66-
playerTransformations.unshift({
67-
quality
68-
});
69-
7040
// Setup the refs and allow for the caller to pass through their
7141
// own ref instance
7242

@@ -123,95 +93,6 @@ const CldVideoPlayer = (props: CldVideoPlayerProps) => {
12393
function handleOnLoad() {
12494
if ( 'cloudinary' in window ) {
12595
cloudinaryRef.current = window.cloudinary;
126-
let logoOptions: CloudinaryVideoPlayerOptionsLogo = {};
127-
128-
if ( typeof logo === 'boolean' ) {
129-
logoOptions.showLogo = logo;
130-
} else if ( typeof logo === 'object' ) {
131-
logoOptions = {
132-
...logoOptions,
133-
showLogo: true,
134-
logoImageUrl: logo.imageUrl,
135-
logoOnclickUrl: logo.onClickUrl
136-
}
137-
}
138-
139-
// Parse the value passed to 'autoplay';
140-
// if its a boolean or a boolean passed as string ("true") set it directly to browser standard prop autoplay else fallback to default;
141-
// if its a string and not a boolean passed as string ("true") set it to cloudinary video player autoplayMode prop else fallback to undefined;
142-
143-
let autoPlayValue: boolean | 'true' | 'false' = false;
144-
let autoplayModeValue: string | undefined = undefined;
145-
146-
if (typeof autoplay === 'boolean' || autoplay === 'true' || autoplay === 'false') {
147-
autoPlayValue = autoplay
148-
}
149-
150-
if (typeof autoplay === 'string' && autoplay !== 'true' && autoplay !== 'false') {
151-
autoplayModeValue = autoplay;
152-
}
153-
154-
const cloudinaryConfig = getCloudinaryConfig(config);
155-
const { cloudName } = cloudinaryConfig?.cloud;
156-
const { secureDistribution, privateCdn } = cloudinaryConfig?.url;
157-
158-
let playerOptions: CloudinaryVideoPlayerOptions = {
159-
cloud_name: cloudName,
160-
privateCdn,
161-
secureDistribution,
162-
163-
autoplayMode: autoplayModeValue,
164-
autoplay: autoPlayValue,
165-
controls,
166-
fontFace: fontFace || '',
167-
language,
168-
languages,
169-
loop,
170-
muted,
171-
publicId,
172-
width,
173-
height,
174-
aspectRatio: `${width}:${height}`,
175-
transformation: playerTransformations,
176-
...logoOptions,
177-
...otherCldVidPlayerOptions
178-
};
179-
180-
if ( Array.isArray(sourceTypes) ) {
181-
playerOptions.sourceTypes = sourceTypes;
182-
}
183-
184-
if ( typeof colors === 'object' ) {
185-
playerOptions.colors = colors;
186-
}
187-
188-
if ( typeof poster === 'string' ) {
189-
// If poster is a string, assume it's either a public ID
190-
// or a remote URL, in either case pass to `publicId`
191-
playerOptions.posterOptions = {
192-
publicId: poster
193-
};
194-
} else if ( typeof poster === 'object' ) {
195-
// If poster is an object, we can either customize the
196-
// automatically generated image from the video or generate
197-
// a completely new image from a separate public ID, so look
198-
// to see if the src is explicitly set to determine whether
199-
// or not to use the video's ID or just pass things along
200-
if ( typeof poster.src !== 'string' ) {
201-
playerOptions.posterOptions = {
202-
publicId: getCldVideoUrl({
203-
...poster,
204-
src: publicId,
205-
format: 'auto:image',
206-
})
207-
};
208-
} else {
209-
playerOptions.posterOptions = {
210-
publicId: getCldImageUrl(poster)
211-
};
212-
}
213-
}
214-
21596
playerRef.current = cloudinaryRef.current.videoPlayer(videoRef.current, playerOptions);
21697

21798
Object.keys(events).forEach((key) => {

pnpm-lock.yaml

Lines changed: 10 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)