next-cloudinary 6.13.0 → 7.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CldImage-37af4d37.esm.js +138 -0
- package/dist/CldImage-6cc29250.cjs.js +146 -0
- package/dist/CldUploadButton-196b419b.esm.js +101 -0
- package/dist/CldUploadButton-db9b095b.cjs.js +109 -0
- package/dist/CldUploadWidget-496b6240.cjs.js +215 -0
- package/dist/CldUploadWidget-d1442bef.esm.js +207 -0
- package/dist/CldVideoPlayer-25af8fc2.cjs.js +149 -0
- package/dist/CldVideoPlayer-d3572d86.esm.js +140 -0
- package/dist/cloudinary-50fd58f7.esm.js +134 -0
- package/dist/cloudinary-c07e74d7.cjs.js +141 -0
- package/dist/cloudinary-loader-b7898036.cjs.js +62 -0
- package/dist/cloudinary-loader-d86c263a.esm.js +59 -0
- package/dist/declarations/src/components/CldImage/CldImage.d.ts +14 -0
- package/dist/declarations/src/components/CldImage/CldImage.d.ts.map +1 -0
- package/dist/declarations/src/components/CldImage/index.d.ts +3 -0
- package/dist/declarations/src/components/CldImage/index.d.ts.map +1 -0
- package/dist/declarations/src/components/CldOgImage/CldOgImage.d.ts +9 -0
- package/dist/declarations/src/components/CldOgImage/CldOgImage.d.ts.map +1 -0
- package/dist/declarations/src/components/CldOgImage/index.d.ts +3 -0
- package/dist/declarations/src/components/CldOgImage/index.d.ts.map +1 -0
- package/dist/declarations/src/components/CldUploadButton/CldUploadButton.d.ts +9 -0
- package/dist/declarations/src/components/CldUploadButton/CldUploadButton.d.ts.map +1 -0
- package/dist/declarations/src/components/CldUploadButton/index.d.ts +3 -0
- package/dist/declarations/src/components/CldUploadButton/index.d.ts.map +1 -0
- package/dist/declarations/src/components/CldUploadWidget/CldUploadWidget.d.ts +4 -0
- package/dist/declarations/src/components/CldUploadWidget/CldUploadWidget.d.ts.map +1 -0
- package/dist/declarations/src/components/CldUploadWidget/CldUploadWidget.types.d.ts +59 -0
- package/dist/declarations/src/components/CldUploadWidget/CldUploadWidget.types.d.ts.map +1 -0
- package/dist/declarations/src/components/CldUploadWidget/index.d.ts +3 -0
- package/dist/declarations/src/components/CldUploadWidget/index.d.ts.map +1 -0
- package/dist/declarations/src/components/CldVideoPlayer/CldVideoPlayer.d.ts +4 -0
- package/dist/declarations/src/components/CldVideoPlayer/CldVideoPlayer.d.ts.map +1 -0
- package/dist/declarations/src/components/CldVideoPlayer/CldVideoPlayer.types.d.ts +28 -0
- package/dist/declarations/src/components/CldVideoPlayer/CldVideoPlayer.types.d.ts.map +1 -0
- package/dist/declarations/src/components/CldVideoPlayer/index.d.ts +3 -0
- package/dist/declarations/src/components/CldVideoPlayer/index.d.ts.map +1 -0
- package/dist/declarations/src/helpers/getCldImageUrl.d.ts +9 -0
- package/dist/declarations/src/helpers/getCldImageUrl.d.ts.map +1 -0
- package/dist/declarations/src/helpers/getCldOgImageUrl.d.ts +7 -0
- package/dist/declarations/src/helpers/getCldOgImageUrl.d.ts.map +1 -0
- package/dist/declarations/src/helpers/getCldVideoUrl.d.ts +9 -0
- package/dist/declarations/src/helpers/getCldVideoUrl.d.ts.map +1 -0
- package/dist/declarations/src/index.d.ts +21 -0
- package/dist/declarations/src/index.d.ts.map +1 -0
- package/dist/declarations/src/loaders/cloudinary-loader.d.ts +15 -0
- package/dist/declarations/src/loaders/cloudinary-loader.d.ts.map +1 -0
- package/dist/next-cloudinary.cjs.d.mts +2 -0
- package/dist/next-cloudinary.cjs.d.ts +2 -0
- package/dist/next-cloudinary.cjs.js +165 -0
- package/dist/next-cloudinary.cjs.mjs +11 -0
- package/dist/next-cloudinary.esm.js +134 -0
- package/package.json +50 -12
- package/dist/index.d.mts +0 -152
- package/dist/index.d.ts +0 -152
- package/dist/index.js +0 -4
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -4
- package/dist/index.mjs.map +0 -1
- package/plugins/copy-assets.ts +0 -80
- package/tsconfig.json +0 -23
- package/tsup.config.ts +0 -18
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useRef, useState, useEffect } from 'react';
|
|
3
|
+
import Script from 'next/script';
|
|
4
|
+
import { generateSignatureCallback, getUploadWidgetOptions, generateUploadWidgetResultCallback, UPLOAD_WIDGET_EVENTS } from '@cloudinary-util/url-loader';
|
|
5
|
+
import { g as getCloudinaryConfig } from './cloudinary-50fd58f7.esm.js';
|
|
6
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
7
|
+
import 'next/package.json';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* triggerOnIdle
|
|
11
|
+
* @see MDN Polyfill https://github.com/behnammodi/polyfill/blob/master/window.polyfill.js#L7-L24
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function triggerOnIdle(callback) {
|
|
15
|
+
if (typeof window !== undefined && 'requestIdleCallback' in window) {
|
|
16
|
+
return requestIdleCallback(callback);
|
|
17
|
+
}
|
|
18
|
+
return setTimeout(() => callback(), 1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const CldUploadWidget = ({
|
|
22
|
+
children,
|
|
23
|
+
config,
|
|
24
|
+
onError,
|
|
25
|
+
onOpen,
|
|
26
|
+
onUpload,
|
|
27
|
+
options,
|
|
28
|
+
signatureEndpoint,
|
|
29
|
+
uploadPreset,
|
|
30
|
+
...props
|
|
31
|
+
}) => {
|
|
32
|
+
const cloudinary = useRef();
|
|
33
|
+
const widget = useRef();
|
|
34
|
+
const [error, setError] = useState(undefined);
|
|
35
|
+
const [results, setResults] = useState(undefined);
|
|
36
|
+
const [isScriptLoading, setIsScriptLoading] = useState(true);
|
|
37
|
+
|
|
38
|
+
// When creating a signed upload, you need to provide both your Cloudinary API Key
|
|
39
|
+
// as well as a signature generator function that will sign any paramters
|
|
40
|
+
// either on page load or during the upload process. Read more about signed uploads at:
|
|
41
|
+
// https://cloudinary.com/documentation/upload_widget#signed_uploads
|
|
42
|
+
|
|
43
|
+
const cloudinaryConfig = getCloudinaryConfig(config);
|
|
44
|
+
const uploadSignature = signatureEndpoint && generateSignatureCallback({
|
|
45
|
+
signatureEndpoint: String(signatureEndpoint),
|
|
46
|
+
fetch
|
|
47
|
+
});
|
|
48
|
+
const uploadOptions = getUploadWidgetOptions({
|
|
49
|
+
uploadPreset: uploadPreset || process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET,
|
|
50
|
+
uploadSignature,
|
|
51
|
+
...options
|
|
52
|
+
}, cloudinaryConfig);
|
|
53
|
+
const resultsCallback = generateUploadWidgetResultCallback({
|
|
54
|
+
onError: uploadError => {
|
|
55
|
+
setError(uploadError);
|
|
56
|
+
if (typeof onError === 'function') {
|
|
57
|
+
onError(uploadError, {
|
|
58
|
+
widget: widget.current,
|
|
59
|
+
...instanceMethods
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
onResult: uploadResult => {
|
|
64
|
+
if (typeof uploadResult?.event !== 'string') return;
|
|
65
|
+
setResults(uploadResult);
|
|
66
|
+
const widgetEvent = UPLOAD_WIDGET_EVENTS[uploadResult.event];
|
|
67
|
+
if (typeof widgetEvent === 'string' && typeof props[widgetEvent] === 'function') {
|
|
68
|
+
const callback = props[widgetEvent];
|
|
69
|
+
callback(uploadResult, {
|
|
70
|
+
widget: widget.current,
|
|
71
|
+
...instanceMethods
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const widgetEventAction = `${widgetEvent}Action`;
|
|
75
|
+
if (widgetEventAction && typeof props[widgetEventAction] === 'function') {
|
|
76
|
+
const action = props[widgetEventAction];
|
|
77
|
+
action(uploadResult);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Handle result states and callbacks
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (typeof results === 'undefined') return;
|
|
86
|
+
const isSuccess = results.event === 'success';
|
|
87
|
+
if (isSuccess && typeof onUpload === 'function') {
|
|
88
|
+
if (process.env.NODE_ENVIRONMENT === 'development') {
|
|
89
|
+
console.warn('The onUpload callback is deprecated. Please use onSuccess instead.');
|
|
90
|
+
}
|
|
91
|
+
onUpload(results, widget.current);
|
|
92
|
+
}
|
|
93
|
+
}, [results]);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* handleOnLoad
|
|
97
|
+
* @description Stores the Cloudinary window instance to a ref when the widget script loads
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
function handleOnLoad() {
|
|
101
|
+
setIsScriptLoading(false);
|
|
102
|
+
if (!cloudinary.current) {
|
|
103
|
+
cloudinary.current = window.cloudinary;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// To help improve load time of the widget on first instance, use requestIdleCallback
|
|
107
|
+
// to trigger widget creation. Optional.
|
|
108
|
+
|
|
109
|
+
triggerOnIdle(() => {
|
|
110
|
+
if (!widget.current) {
|
|
111
|
+
widget.current = createWidget();
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
return () => {
|
|
117
|
+
widget.current?.destroy();
|
|
118
|
+
widget.current = undefined;
|
|
119
|
+
};
|
|
120
|
+
}, []);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Instance Methods
|
|
124
|
+
* Gives the ability to interface directly with the Upload Widget instance methods like open and close
|
|
125
|
+
* https://cloudinary.com/documentation/upload_widget_reference#instance_methods
|
|
126
|
+
*/
|
|
127
|
+
|
|
128
|
+
function invokeInstanceMethod(method, options = []) {
|
|
129
|
+
if (!widget.current) {
|
|
130
|
+
widget.current = createWidget();
|
|
131
|
+
}
|
|
132
|
+
if (typeof widget?.current[method] === "function") {
|
|
133
|
+
return widget.current[method](...options);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function close(options) {
|
|
137
|
+
invokeInstanceMethod('close', [options]);
|
|
138
|
+
}
|
|
139
|
+
function destroy(options) {
|
|
140
|
+
return invokeInstanceMethod('destroy', [options]);
|
|
141
|
+
}
|
|
142
|
+
function hide() {
|
|
143
|
+
invokeInstanceMethod('hide');
|
|
144
|
+
}
|
|
145
|
+
function isDestroyed() {
|
|
146
|
+
return invokeInstanceMethod('isDestroyed');
|
|
147
|
+
}
|
|
148
|
+
function isMinimized() {
|
|
149
|
+
return invokeInstanceMethod('isMinimized');
|
|
150
|
+
}
|
|
151
|
+
function isShowing() {
|
|
152
|
+
return invokeInstanceMethod('isShowing');
|
|
153
|
+
}
|
|
154
|
+
function minimize() {
|
|
155
|
+
invokeInstanceMethod('minimize');
|
|
156
|
+
}
|
|
157
|
+
function open(widgetSource, options) {
|
|
158
|
+
invokeInstanceMethod('open', [widgetSource, options]);
|
|
159
|
+
if (typeof onOpen === 'function') {
|
|
160
|
+
onOpen(widget.current);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function show() {
|
|
164
|
+
invokeInstanceMethod('show');
|
|
165
|
+
}
|
|
166
|
+
function update() {
|
|
167
|
+
invokeInstanceMethod('update');
|
|
168
|
+
}
|
|
169
|
+
const instanceMethods = {
|
|
170
|
+
close,
|
|
171
|
+
destroy,
|
|
172
|
+
hide,
|
|
173
|
+
isDestroyed,
|
|
174
|
+
isMinimized,
|
|
175
|
+
isShowing,
|
|
176
|
+
minimize,
|
|
177
|
+
open,
|
|
178
|
+
show,
|
|
179
|
+
update
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* createWidget
|
|
184
|
+
* @description Creates a new instance of the Cloudinary widget and stores in a ref
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
function createWidget() {
|
|
188
|
+
return cloudinary.current?.createUploadWidget(uploadOptions, resultsCallback);
|
|
189
|
+
}
|
|
190
|
+
return /*#__PURE__*/jsxs(Fragment, {
|
|
191
|
+
children: [typeof children === 'function' && children({
|
|
192
|
+
cloudinary: cloudinary.current,
|
|
193
|
+
widget: widget.current,
|
|
194
|
+
results,
|
|
195
|
+
error,
|
|
196
|
+
isLoading: isScriptLoading,
|
|
197
|
+
...instanceMethods
|
|
198
|
+
}), /*#__PURE__*/jsx(Script, {
|
|
199
|
+
id: `cloudinary-uploadwidget-${Math.floor(Math.random() * 100)}`,
|
|
200
|
+
src: "https://upload-widget.cloudinary.com/global/all.js",
|
|
201
|
+
onLoad: handleOnLoad,
|
|
202
|
+
onError: e => console.error(`Failed to load Cloudinary Upload Widget: ${e.message}`)
|
|
203
|
+
})]
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export { CldUploadWidget as default };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var react = require('react');
|
|
7
|
+
var Script = require('next/script');
|
|
8
|
+
var Head = require('next/head');
|
|
9
|
+
var urlLoader = require('@cloudinary-util/url-loader');
|
|
10
|
+
var cloudinary = require('./cloudinary-c07e74d7.cjs.js');
|
|
11
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
12
|
+
require('next/package.json');
|
|
13
|
+
|
|
14
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
var Script__default = /*#__PURE__*/_interopDefault(Script);
|
|
17
|
+
var Head__default = /*#__PURE__*/_interopDefault(Head);
|
|
18
|
+
|
|
19
|
+
let playerInstances = [];
|
|
20
|
+
const PLAYER_VERSION = '1.11.1';
|
|
21
|
+
const CldVideoPlayer = props => {
|
|
22
|
+
const {
|
|
23
|
+
className,
|
|
24
|
+
config,
|
|
25
|
+
height,
|
|
26
|
+
id,
|
|
27
|
+
onDataLoad,
|
|
28
|
+
onError,
|
|
29
|
+
onMetadataLoad,
|
|
30
|
+
onPause,
|
|
31
|
+
onPlay,
|
|
32
|
+
onEnded,
|
|
33
|
+
width
|
|
34
|
+
} = props;
|
|
35
|
+
const cloudinaryConfig = cloudinary.getCloudinaryConfig(config);
|
|
36
|
+
const playerOptions = urlLoader.getVideoPlayerOptions(props, cloudinaryConfig);
|
|
37
|
+
const {
|
|
38
|
+
publicId
|
|
39
|
+
} = playerOptions;
|
|
40
|
+
if (typeof publicId === 'undefined') {
|
|
41
|
+
throw new Error('Video Player requires a Public ID or Cloudinary URL - please specify a src prop');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Setup the refs and allow for the caller to pass through their
|
|
45
|
+
// own ref instance
|
|
46
|
+
|
|
47
|
+
const cloudinaryRef = react.useRef();
|
|
48
|
+
const defaultVideoRef = react.useRef();
|
|
49
|
+
const videoRef = props.videoRef || defaultVideoRef;
|
|
50
|
+
const defaultPlayerRef = react.useRef();
|
|
51
|
+
const playerRef = props.playerRef || defaultPlayerRef;
|
|
52
|
+
const playerId = id || `player-${publicId.replace('/', '-')}`;
|
|
53
|
+
let playerClassName = 'cld-video-player cld-fluid';
|
|
54
|
+
if (className) {
|
|
55
|
+
playerClassName = `${playerClassName} ${className}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Check if the same id is being used for multiple video instances.
|
|
59
|
+
const checkForMultipleInstance = playerInstances.filter(id => id === playerId).length > 1;
|
|
60
|
+
if (checkForMultipleInstance) {
|
|
61
|
+
console.warn(`Multiple instances of the same video detected on the
|
|
62
|
+
page which may cause some features to not work.
|
|
63
|
+
Try adding a unique id to each player.`);
|
|
64
|
+
} else {
|
|
65
|
+
playerInstances.push(playerId);
|
|
66
|
+
}
|
|
67
|
+
const events = {
|
|
68
|
+
error: onError,
|
|
69
|
+
loadeddata: onDataLoad,
|
|
70
|
+
loadedmetadata: onMetadataLoad,
|
|
71
|
+
pause: onPause,
|
|
72
|
+
play: onPlay,
|
|
73
|
+
ended: onEnded
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* handleEvent
|
|
78
|
+
* @description Event handler for all player events
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
function handleEvent(event) {
|
|
82
|
+
const activeEvent = events[event.type];
|
|
83
|
+
if (typeof activeEvent === 'function') {
|
|
84
|
+
activeEvent(getPlayerRefs());
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* handleOnLoad
|
|
90
|
+
* @description Stores the Cloudinary window instance to a ref when the widget script loads
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
function handleOnLoad() {
|
|
94
|
+
if ('cloudinary' in window) {
|
|
95
|
+
cloudinaryRef.current = window.cloudinary;
|
|
96
|
+
playerRef.current = cloudinaryRef.current.videoPlayer(videoRef.current, playerOptions);
|
|
97
|
+
Object.keys(events).forEach(key => {
|
|
98
|
+
if (typeof events[key] === 'function') {
|
|
99
|
+
playerRef.current?.on(key, handleEvent);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
react.useEffect(() => {
|
|
105
|
+
return () => {
|
|
106
|
+
//@ts-ignore
|
|
107
|
+
playerRef.current?.videojs.cloudinary.dispose();
|
|
108
|
+
playerInstances = playerInstances.filter(id => id !== playerId);
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
*getPlayerRefs
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
function getPlayerRefs() {
|
|
117
|
+
return {
|
|
118
|
+
player: playerRef.current,
|
|
119
|
+
video: videoRef.current
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
|
|
123
|
+
children: [/*#__PURE__*/jsxRuntime.jsx(Head__default["default"], {
|
|
124
|
+
children: /*#__PURE__*/jsxRuntime.jsx("link", {
|
|
125
|
+
href: `https://unpkg.com/cloudinary-video-player@${PLAYER_VERSION}/dist/cld-video-player.min.css`,
|
|
126
|
+
rel: "stylesheet"
|
|
127
|
+
})
|
|
128
|
+
}), /*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
129
|
+
style: {
|
|
130
|
+
width: '100%',
|
|
131
|
+
aspectRatio: `${width} / ${height}`
|
|
132
|
+
},
|
|
133
|
+
children: [/*#__PURE__*/jsxRuntime.jsx("video", {
|
|
134
|
+
ref: videoRef,
|
|
135
|
+
id: playerId,
|
|
136
|
+
className: playerClassName,
|
|
137
|
+
width: width,
|
|
138
|
+
height: height
|
|
139
|
+
}), /*#__PURE__*/jsxRuntime.jsx(Script__default["default"], {
|
|
140
|
+
id: `cloudinary-videoplayer-${playerId}-${Math.floor(Math.random() * 100)}`,
|
|
141
|
+
src: `https://unpkg.com/cloudinary-video-player@${PLAYER_VERSION}/dist/cld-video-player.min.js`,
|
|
142
|
+
onLoad: handleOnLoad,
|
|
143
|
+
onError: e => console.error(`Failed to load Cloudinary Video Player: ${e.message}`)
|
|
144
|
+
})]
|
|
145
|
+
})]
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
exports["default"] = CldVideoPlayer;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useRef, useEffect } from 'react';
|
|
3
|
+
import Script from 'next/script';
|
|
4
|
+
import Head from 'next/head';
|
|
5
|
+
import { getVideoPlayerOptions } from '@cloudinary-util/url-loader';
|
|
6
|
+
import { g as getCloudinaryConfig } from './cloudinary-50fd58f7.esm.js';
|
|
7
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
8
|
+
import 'next/package.json';
|
|
9
|
+
|
|
10
|
+
let playerInstances = [];
|
|
11
|
+
const PLAYER_VERSION = '1.11.1';
|
|
12
|
+
const CldVideoPlayer = props => {
|
|
13
|
+
const {
|
|
14
|
+
className,
|
|
15
|
+
config,
|
|
16
|
+
height,
|
|
17
|
+
id,
|
|
18
|
+
onDataLoad,
|
|
19
|
+
onError,
|
|
20
|
+
onMetadataLoad,
|
|
21
|
+
onPause,
|
|
22
|
+
onPlay,
|
|
23
|
+
onEnded,
|
|
24
|
+
width
|
|
25
|
+
} = props;
|
|
26
|
+
const cloudinaryConfig = getCloudinaryConfig(config);
|
|
27
|
+
const playerOptions = getVideoPlayerOptions(props, cloudinaryConfig);
|
|
28
|
+
const {
|
|
29
|
+
publicId
|
|
30
|
+
} = playerOptions;
|
|
31
|
+
if (typeof publicId === 'undefined') {
|
|
32
|
+
throw new Error('Video Player requires a Public ID or Cloudinary URL - please specify a src prop');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Setup the refs and allow for the caller to pass through their
|
|
36
|
+
// own ref instance
|
|
37
|
+
|
|
38
|
+
const cloudinaryRef = useRef();
|
|
39
|
+
const defaultVideoRef = useRef();
|
|
40
|
+
const videoRef = props.videoRef || defaultVideoRef;
|
|
41
|
+
const defaultPlayerRef = useRef();
|
|
42
|
+
const playerRef = props.playerRef || defaultPlayerRef;
|
|
43
|
+
const playerId = id || `player-${publicId.replace('/', '-')}`;
|
|
44
|
+
let playerClassName = 'cld-video-player cld-fluid';
|
|
45
|
+
if (className) {
|
|
46
|
+
playerClassName = `${playerClassName} ${className}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Check if the same id is being used for multiple video instances.
|
|
50
|
+
const checkForMultipleInstance = playerInstances.filter(id => id === playerId).length > 1;
|
|
51
|
+
if (checkForMultipleInstance) {
|
|
52
|
+
console.warn(`Multiple instances of the same video detected on the
|
|
53
|
+
page which may cause some features to not work.
|
|
54
|
+
Try adding a unique id to each player.`);
|
|
55
|
+
} else {
|
|
56
|
+
playerInstances.push(playerId);
|
|
57
|
+
}
|
|
58
|
+
const events = {
|
|
59
|
+
error: onError,
|
|
60
|
+
loadeddata: onDataLoad,
|
|
61
|
+
loadedmetadata: onMetadataLoad,
|
|
62
|
+
pause: onPause,
|
|
63
|
+
play: onPlay,
|
|
64
|
+
ended: onEnded
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* handleEvent
|
|
69
|
+
* @description Event handler for all player events
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
function handleEvent(event) {
|
|
73
|
+
const activeEvent = events[event.type];
|
|
74
|
+
if (typeof activeEvent === 'function') {
|
|
75
|
+
activeEvent(getPlayerRefs());
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* handleOnLoad
|
|
81
|
+
* @description Stores the Cloudinary window instance to a ref when the widget script loads
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
function handleOnLoad() {
|
|
85
|
+
if ('cloudinary' in window) {
|
|
86
|
+
cloudinaryRef.current = window.cloudinary;
|
|
87
|
+
playerRef.current = cloudinaryRef.current.videoPlayer(videoRef.current, playerOptions);
|
|
88
|
+
Object.keys(events).forEach(key => {
|
|
89
|
+
if (typeof events[key] === 'function') {
|
|
90
|
+
playerRef.current?.on(key, handleEvent);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
return () => {
|
|
97
|
+
//@ts-ignore
|
|
98
|
+
playerRef.current?.videojs.cloudinary.dispose();
|
|
99
|
+
playerInstances = playerInstances.filter(id => id !== playerId);
|
|
100
|
+
};
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
*getPlayerRefs
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
function getPlayerRefs() {
|
|
108
|
+
return {
|
|
109
|
+
player: playerRef.current,
|
|
110
|
+
video: videoRef.current
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return /*#__PURE__*/jsxs(Fragment, {
|
|
114
|
+
children: [/*#__PURE__*/jsx(Head, {
|
|
115
|
+
children: /*#__PURE__*/jsx("link", {
|
|
116
|
+
href: `https://unpkg.com/cloudinary-video-player@${PLAYER_VERSION}/dist/cld-video-player.min.css`,
|
|
117
|
+
rel: "stylesheet"
|
|
118
|
+
})
|
|
119
|
+
}), /*#__PURE__*/jsxs("div", {
|
|
120
|
+
style: {
|
|
121
|
+
width: '100%',
|
|
122
|
+
aspectRatio: `${width} / ${height}`
|
|
123
|
+
},
|
|
124
|
+
children: [/*#__PURE__*/jsx("video", {
|
|
125
|
+
ref: videoRef,
|
|
126
|
+
id: playerId,
|
|
127
|
+
className: playerClassName,
|
|
128
|
+
width: width,
|
|
129
|
+
height: height
|
|
130
|
+
}), /*#__PURE__*/jsx(Script, {
|
|
131
|
+
id: `cloudinary-videoplayer-${playerId}-${Math.floor(Math.random() * 100)}`,
|
|
132
|
+
src: `https://unpkg.com/cloudinary-video-player@${PLAYER_VERSION}/dist/cld-video-player.min.js`,
|
|
133
|
+
onLoad: handleOnLoad,
|
|
134
|
+
onError: e => console.error(`Failed to load Cloudinary Video Player: ${e.message}`)
|
|
135
|
+
})]
|
|
136
|
+
})]
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export { CldVideoPlayer as default };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import nextPkg from 'next/package.json';
|
|
2
|
+
|
|
3
|
+
var pkg = {
|
|
4
|
+
name: "next-cloudinary",
|
|
5
|
+
version: "7.0.0-beta.2",
|
|
6
|
+
license: "MIT",
|
|
7
|
+
main: "dist/next-cloudinary.cjs.js",
|
|
8
|
+
module: "dist/next-cloudinary.esm.js",
|
|
9
|
+
exports: {
|
|
10
|
+
".": {
|
|
11
|
+
types: {
|
|
12
|
+
"import": "./dist/next-cloudinary.cjs.mjs",
|
|
13
|
+
"default": "./dist/next-cloudinary.cjs.js"
|
|
14
|
+
},
|
|
15
|
+
module: "./dist/next-cloudinary.esm.js",
|
|
16
|
+
"import": "./dist/next-cloudinary.cjs.mjs",
|
|
17
|
+
"default": "./dist/next-cloudinary.cjs.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
"./cld-video-player.css": "./dist/cld-video-player.css",
|
|
21
|
+
"./dist/cld-video-player.css": "./dist/cld-video-player.css",
|
|
22
|
+
"./cloudinary_icon_for_black_bg.svg": "./dist/cloudinary_icon_for_black_bg.svg",
|
|
23
|
+
"./dist/cloudinary_icon_for_black_bg.svg": "./dist/cloudinary_icon_for_black_bg.svg",
|
|
24
|
+
"./cloudinary_icon_for_white_bg.svg": "./dist/cloudinary_icon_for_white_bg.svg",
|
|
25
|
+
"./dist/cloudinary_icon_for_white_bg.svg": "./dist/cloudinary_icon_for_white_bg.svg"
|
|
26
|
+
},
|
|
27
|
+
types: "./dist/next-cloudinary.cjs.d.ts",
|
|
28
|
+
files: [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
scripts: {
|
|
32
|
+
build: "preconstruct build && tsx scripts/copy-assets.ts",
|
|
33
|
+
dev: "preconstruct watch",
|
|
34
|
+
prepublishOnly: "cp ../README.md . && cp ../LICENSE . && pnpm build",
|
|
35
|
+
postpublish: "rm ./README.md && rm ./LICENSE",
|
|
36
|
+
test: "vitest run",
|
|
37
|
+
"test:app": "NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=\"test\" pnpm build && cd tests/nextjs-app && pnpm build",
|
|
38
|
+
"test:watch": "vitest"
|
|
39
|
+
},
|
|
40
|
+
dependencies: {
|
|
41
|
+
"@cloudinary-util/types": "1.5.8",
|
|
42
|
+
"@cloudinary-util/url-loader": "5.10.3",
|
|
43
|
+
"@cloudinary-util/util": "^3.3.2"
|
|
44
|
+
},
|
|
45
|
+
devDependencies: {
|
|
46
|
+
"@babel/core": "^7.25.2",
|
|
47
|
+
"@babel/preset-env": "^7.25.4",
|
|
48
|
+
"@babel/preset-react": "^7.24.7",
|
|
49
|
+
"@babel/preset-typescript": "^7.24.7",
|
|
50
|
+
"@tsconfig/recommended": "^1.0.7",
|
|
51
|
+
"@types/node": "^22.0.2",
|
|
52
|
+
"@types/react": "^18.3.3",
|
|
53
|
+
"@types/react-dom": "^18.3.0",
|
|
54
|
+
dotenv: "^16.4.5",
|
|
55
|
+
mkdirp: "^3.0.1",
|
|
56
|
+
tsx: "^4.19.1",
|
|
57
|
+
typescript: "^5.5.4",
|
|
58
|
+
vitest: "^2.0.5"
|
|
59
|
+
},
|
|
60
|
+
peerDependencies: {
|
|
61
|
+
next: "^12 || ^13 || ^14 || >=15.0.0-rc",
|
|
62
|
+
react: "^17 || ^18 || >=19.0.0-beta"
|
|
63
|
+
},
|
|
64
|
+
preconstruct: {
|
|
65
|
+
exports: {
|
|
66
|
+
importConditionDefaultExport: "default",
|
|
67
|
+
extra: {
|
|
68
|
+
"./cld-video-player.css": "./dist/cld-video-player.css",
|
|
69
|
+
"./dist/cld-video-player.css": "./dist/cld-video-player.css",
|
|
70
|
+
"./cloudinary_icon_for_black_bg.svg": "./dist/cloudinary_icon_for_black_bg.svg",
|
|
71
|
+
"./dist/cloudinary_icon_for_black_bg.svg": "./dist/cloudinary_icon_for_black_bg.svg",
|
|
72
|
+
"./cloudinary_icon_for_white_bg.svg": "./dist/cloudinary_icon_for_white_bg.svg",
|
|
73
|
+
"./dist/cloudinary_icon_for_white_bg.svg": "./dist/cloudinary_icon_for_white_bg.svg"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
|
|
77
|
+
importsConditions: true
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const NEXT_CLOUDINARY_ANALYTICS_PRODUCT_ID = 'A';
|
|
83
|
+
const NEXT_CLOUDINARY_ANALYTICS_ID = 'V';
|
|
84
|
+
const NEXT_VERSION = normalizeVersion(nextPkg.version);
|
|
85
|
+
const NEXT_CLOUDINARY_VERSION = normalizeVersion(pkg.version);
|
|
86
|
+
function normalizeVersion(version) {
|
|
87
|
+
let normalized = version;
|
|
88
|
+
if (normalized.includes('-')) {
|
|
89
|
+
normalized = normalized.split('-')[0];
|
|
90
|
+
}
|
|
91
|
+
return normalized;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* getCloudinaryConfig
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
function getCloudinaryConfig(config) {
|
|
99
|
+
const cloudName = config?.cloud?.cloudName ?? process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME;
|
|
100
|
+
if (!cloudName) {
|
|
101
|
+
throw new Error('A Cloudinary Cloud name is required, please make sure NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME is set and configured in your environment.');
|
|
102
|
+
}
|
|
103
|
+
const apiKey = config?.cloud?.apiKey ?? process.env.NEXT_PUBLIC_CLOUDINARY_API_KEY;
|
|
104
|
+
const secureDistribution = config?.url?.secureDistribution ?? process.env.NEXT_PUBLIC_CLOUDINARY_SECURE_DISTRIBUTION;
|
|
105
|
+
const privateCdn = config?.url?.privateCdn ?? process.env.NEXT_PUBLIC_CLOUDINARY_PRIVATE_CDN;
|
|
106
|
+
return Object.assign({
|
|
107
|
+
cloud: {
|
|
108
|
+
...config?.cloud,
|
|
109
|
+
apiKey,
|
|
110
|
+
cloudName
|
|
111
|
+
},
|
|
112
|
+
url: {
|
|
113
|
+
...config?.url,
|
|
114
|
+
secureDistribution,
|
|
115
|
+
privateCdn
|
|
116
|
+
}
|
|
117
|
+
}, config);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* getCloudinaryAnalytics
|
|
122
|
+
*/
|
|
123
|
+
|
|
124
|
+
function getCloudinaryAnalytics(analytics) {
|
|
125
|
+
return Object.assign({
|
|
126
|
+
product: NEXT_CLOUDINARY_ANALYTICS_PRODUCT_ID,
|
|
127
|
+
sdkCode: NEXT_CLOUDINARY_ANALYTICS_ID,
|
|
128
|
+
sdkSemver: NEXT_CLOUDINARY_VERSION,
|
|
129
|
+
techVersion: NEXT_VERSION,
|
|
130
|
+
feature: ''
|
|
131
|
+
}, analytics);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { getCloudinaryAnalytics as a, getCloudinaryConfig as g };
|