@dtwojs/cloudinary 2.7.0

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.
@@ -0,0 +1,262 @@
1
+ <script setup lang="ts">
2
+ import { ref } from "kdu";
3
+ import { useHead } from "@kdhead/kdu";
4
+ import { useRuntimeConfig } from "#imports";
5
+ import { parseUrl } from "@cloudinary-util/util";
6
+ import { ConfigOptions } from "@cloudinary-util/url-loader";
7
+
8
+ export interface CloudinaryVideoPlayer {
9
+ on: Function;
10
+ }
11
+
12
+ export interface CloudinaryVideoPlayerOptions {
13
+ autoplayMode?: string;
14
+ cloud_name?: string;
15
+ colors?: CloudinaryVideoPlayerOptionsColors;
16
+ controls?: boolean;
17
+ fontFace?: string;
18
+ loop?: boolean;
19
+ muted?: boolean;
20
+ publicId: string;
21
+ secure?: boolean;
22
+ transformation?: Array<object> | object;
23
+ hideContextMenu?: boolean;
24
+ config?: ConfigOptions;
25
+ pictureInPictureToggle?: boolean;
26
+ }
27
+
28
+ export interface CloudinaryVideoPlayerOptionsColors {
29
+ accent?: string;
30
+ base?: string;
31
+ text?: string;
32
+ }
33
+
34
+ export interface CloudinaryVideoPlayerOptionsLogo {
35
+ logoImageUrl?: string;
36
+ logoOnclickUrl?: string;
37
+ showLogo?: boolean;
38
+ }
39
+
40
+ export interface CldVideoPlayerPropsLogo {
41
+ imageUrl?: CloudinaryVideoPlayerOptionsLogo["logoImageUrl"];
42
+ logo?: boolean;
43
+ onClickUrl?: CloudinaryVideoPlayerOptionsLogo["logoOnclickUrl"];
44
+ }
45
+
46
+ export type CldVideoPlayerProps = Pick<
47
+ CloudinaryVideoPlayerOptions,
48
+ | "colors"
49
+ | "controls"
50
+ | "fontFace"
51
+ | "loop"
52
+ | "muted"
53
+ | "transformation"
54
+ | "hideContextMenu"
55
+ > & {
56
+ autoPlay?: string;
57
+ className?: string;
58
+ height: string | number;
59
+ id?: string;
60
+ logo?: boolean | CldVideoPlayerPropsLogo;
61
+ onDataLoad?: Function;
62
+ onError?: Function;
63
+ onMetadataLoad?: Function;
64
+ onPause?: Function;
65
+ onPlay?: Function;
66
+ onEnded?: Function;
67
+ playerRef?: { value: CloudinaryVideoPlayer | null };
68
+ src: string;
69
+ version?: string;
70
+ videoRef?: { value: HTMLVideoElement | null };
71
+ quality?: string | number;
72
+ width: string | number;
73
+ config?: ConfigOptions;
74
+ pictureInPictureToggle?: boolean;
75
+ };
76
+
77
+ const props = withDefaults(defineProps<CldVideoPlayerProps>(), {
78
+ autoPlay: "never",
79
+ controls: true,
80
+ logo: true,
81
+ loop: false,
82
+ muted: false,
83
+ version: "1.10.6",
84
+ quality: "auto",
85
+ });
86
+
87
+ const idRef = ref(Math.ceil(Math.random() * 100000));
88
+
89
+ const {
90
+ autoPlay,
91
+ className,
92
+ colors,
93
+ controls,
94
+ fontFace,
95
+ height,
96
+ id,
97
+ logo,
98
+ loop,
99
+ muted,
100
+ onDataLoad,
101
+ onError,
102
+ onMetadataLoad,
103
+ onPause,
104
+ onPlay,
105
+ onEnded,
106
+ src,
107
+ transformation,
108
+ version,
109
+ quality,
110
+ width,
111
+ hideContextMenu,
112
+ config,
113
+ pictureInPictureToggle,
114
+ } = props as CldVideoPlayerProps;
115
+
116
+ const playerTransformations = Array.isArray(transformation)
117
+ ? transformation
118
+ : [transformation];
119
+
120
+ let publicId = src;
121
+
122
+ // If the publicId/src is a URL, attempt to parse it as a Cloudinary URL
123
+ // to get the public ID alone
124
+
125
+ if (publicId.startsWith("http")) {
126
+ try {
127
+ const parts = parseUrl(src);
128
+ if (typeof parts?.publicId === "string") {
129
+ publicId = parts?.publicId;
130
+ }
131
+ } catch (e) {
132
+ console.error(e);
133
+ }
134
+ }
135
+
136
+ playerTransformations.unshift({
137
+ quality,
138
+ });
139
+
140
+ const cloudinaryRef = ref<any>();
141
+ const defaultVideoRef = ref();
142
+ const videoRef = props.videoRef || defaultVideoRef;
143
+ const defaultPlayerRef = ref();
144
+ const playerRef = props.playerRef || defaultPlayerRef;
145
+
146
+ const playerId = id || `player-${publicId.replace("/", "-")}-${idRef.value}`;
147
+ let playerClassName = "cld-video-player cld-fluid";
148
+
149
+ if (className) {
150
+ playerClassName = `${playerClassName} ${className}`;
151
+ }
152
+
153
+ const events: Record<string, Function | undefined> = {
154
+ error: onError,
155
+ loadeddata: onDataLoad,
156
+ loadedmetadata: onMetadataLoad,
157
+ pause: onPause,
158
+ play: onPlay,
159
+ ended: onEnded,
160
+ };
161
+ function handleEvent(event: { type: "string" }) {
162
+ const activeEvent = events[event.type];
163
+
164
+ if (typeof activeEvent === "function") {
165
+ activeEvent({
166
+ player: playerRef.value,
167
+ video: videoRef.value,
168
+ });
169
+ }
170
+ }
171
+
172
+ const handleOnLoad = () => {
173
+ if ("cloudinary" in window) {
174
+ cloudinaryRef.value = window.cloudinary;
175
+
176
+ let logoOptions: CloudinaryVideoPlayerOptionsLogo = {};
177
+
178
+ if (typeof logo === "boolean") {
179
+ logoOptions.showLogo = logo;
180
+ } else if (typeof logo === "object") {
181
+ logoOptions = {
182
+ ...logoOptions,
183
+ showLogo: true,
184
+ logoImageUrl: logo.imageUrl,
185
+ logoOnclickUrl: logo.onClickUrl,
186
+ };
187
+ }
188
+
189
+ const playerOptions: CloudinaryVideoPlayerOptions = {
190
+ autoplayMode: autoPlay,
191
+ cloud_name: useRuntimeConfig().public.cloudinary.cloudName,
192
+ controls,
193
+ fontFace: fontFace || "",
194
+ loop,
195
+ muted,
196
+ publicId,
197
+ secure: true,
198
+ transformation: playerTransformations,
199
+ ...logoOptions,
200
+ hideContextMenu,
201
+ pictureInPictureToggle,
202
+ ...useRuntimeConfig().public.cloudinary.cloud,
203
+ ...useRuntimeConfig().public.cloudinary.url,
204
+ ...config,
205
+ };
206
+
207
+ if (typeof colors === "object") {
208
+ playerOptions.colors = colors;
209
+ }
210
+
211
+ playerRef.value = cloudinaryRef.value.videoPlayer(
212
+ videoRef.value,
213
+ playerOptions
214
+ );
215
+
216
+ Object.keys(events).forEach((key) => {
217
+ if (typeof events[key] === "function") {
218
+ playerRef.value?.on(key, handleEvent);
219
+ }
220
+ });
221
+ }
222
+ };
223
+
224
+ defineExpose({
225
+ playerRef,
226
+ videoRef,
227
+ });
228
+
229
+ useHead({
230
+ script: [
231
+ {
232
+ id: `cloudinary-videoplayer-${Math.floor(Math.random() * 100)}`,
233
+ src: `https://unpkg.com/cloudinary-video-player@${version}/dist/cld-video-player.min.js`,
234
+ onload: handleOnLoad,
235
+ onerror: (e) =>
236
+ console.error(
237
+ `Failed to load Cloudinary Video Player: ${(e as any).message}`
238
+ ),
239
+ },
240
+ ],
241
+ link: [
242
+ {
243
+ href: `https://unpkg.com/cloudinary-video-player@${
244
+ version || "1.10.6"
245
+ }/dist/cld-video-player.min.css`,
246
+ rel: "stylesheet",
247
+ },
248
+ ],
249
+ });
250
+ </script>
251
+
252
+ <template>
253
+ <div :style="{ width: '100%', aspectRatio: `${width} / ${height}` }">
254
+ <video
255
+ :id="playerId"
256
+ ref="videoRef"
257
+ :class="playerClassName"
258
+ :width="width"
259
+ :height="height"
260
+ />
261
+ </div>
262
+ </template>
@@ -0,0 +1,4 @@
1
+ import { ConstructUrlProps } from '@cloudinary-util/url-loader';
2
+ export declare const useCldImageUrl: (props: ConstructUrlProps) => {
3
+ url: string;
4
+ };
@@ -0,0 +1,39 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { constructCloudinaryUrl } from "@cloudinary-util/url-loader";
3
+ import dtwoPkg from "dtwo/package.json";
4
+ import pkg from "../../../package.json";
5
+ export const useCldImageUrl = (props) => {
6
+ if (!props.options.src) console.error("`[@dtwojs/cloudinary]`: Property `src` is missing");
7
+ const moduleConfig = useRuntimeConfig().public.cloudinary;
8
+ const cldCloudName = props.config?.cloud?.cloudName || moduleConfig.cloud?.cloudName || moduleConfig?.cloudName;
9
+ if (!cldCloudName) console.warn("`[@dtwojs/cloudinary]` Environment variable `CLOUDINARY_CLOUD_NAME` or property `cloudinary.cloudName` missing");
10
+ let cldOptions = {
11
+ options: {
12
+ // Have to spread the options because otherwise getting the error about props being updated while they are readonly.
13
+ ...props.options
14
+ },
15
+ config: {
16
+ url: moduleConfig.url,
17
+ cloud: {
18
+ cloudName: cldCloudName,
19
+ ...moduleConfig.cloud
20
+ },
21
+ ...props.config
22
+ },
23
+ analytics: false
24
+ };
25
+ if (moduleConfig.analytics) {
26
+ cldOptions = {
27
+ ...cldOptions,
28
+ analytics: Object.assign({
29
+ sdkCode: "D",
30
+ sdkSemver: `${pkg.version.split(".")[0]}.0.0`,
31
+ techVersion: `${dtwoPkg.version.split(".")[0]}.0.0`,
32
+ product: "B"
33
+ }, props.analytics)
34
+ };
35
+ }
36
+ return {
37
+ url: constructCloudinaryUrl(cldOptions)
38
+ };
39
+ };
@@ -0,0 +1,8 @@
1
+ import { ConfigOptions, AnalyticsOptions, VideoOptions } from '@cloudinary-util/url-loader';
2
+ export declare const useCldVideoUrl: (props: {
3
+ options: VideoOptions;
4
+ config?: ConfigOptions;
5
+ analytics?: AnalyticsOptions;
6
+ }) => {
7
+ url: string;
8
+ };
@@ -0,0 +1,40 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { constructCloudinaryUrl } from "@cloudinary-util/url-loader";
3
+ import dtwoPkg from "dtwo/package.json";
4
+ import pkg from "../../../package.json";
5
+ export const useCldVideoUrl = (props) => {
6
+ if (!props.options.src) console.error("`[@dtwojs/cloudinary]`: Property `src` is missing");
7
+ const moduleConfig = useRuntimeConfig().public.cloudinary;
8
+ const cldCloudName = props.config?.cloud?.cloudName || moduleConfig.cloud?.cloudName || moduleConfig?.cloudName;
9
+ if (!cldCloudName) console.warn("`[@dtwojs/cloudinary]` Environment variable `CLOUDINARY_CLOUD_NAME` or property `cloudinary.cloudName` missing");
10
+ let cldOptions = {
11
+ options: {
12
+ assetType: "video",
13
+ // Have to spread the options because otherwise getting the error about props being updated while they are readonly.
14
+ ...props.options
15
+ },
16
+ config: {
17
+ url: moduleConfig.url,
18
+ cloud: {
19
+ cloudName: cldCloudName,
20
+ ...moduleConfig.cloud
21
+ },
22
+ ...props.config
23
+ },
24
+ analytics: false
25
+ };
26
+ if (moduleConfig.analytics) {
27
+ cldOptions = {
28
+ ...cldOptions,
29
+ analytics: Object.assign({
30
+ sdkCode: "D",
31
+ sdkSemver: `${pkg.version.split(".")[0]}.0.0`,
32
+ techVersion: `${dtwoPkg.version.split(".")[0]}.0.0`,
33
+ product: "B"
34
+ }, props.analytics)
35
+ };
36
+ }
37
+ return {
38
+ url: constructCloudinaryUrl(cldOptions)
39
+ };
40
+ };
@@ -0,0 +1,10 @@
1
+
2
+ import { ModuleOptions } from './module'
3
+
4
+ declare module '@dtwo/schema' {
5
+ interface DtwoConfig { ['cloudinary']?: Partial<ModuleOptions> }
6
+ interface DtwoOptions { ['cloudinary']?: ModuleOptions }
7
+ }
8
+
9
+
10
+ export { ModuleOptions, default } from './module'
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@dtwojs/cloudinary",
3
+ "version": "2.7.0",
4
+ "description": "Cloudinary module for Dtwo",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/module.mjs",
11
+ "require": "./dist/module.cjs"
12
+ }
13
+ },
14
+ "repository": "d2js/module-cloudinary",
15
+ "author": "NKDuy",
16
+ "main": "./dist/module.cjs",
17
+ "types": "./dist/types.d.ts",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "keywords": [
22
+ "dtwo",
23
+ "module",
24
+ "dtwo-cloudinary",
25
+ "cloudinary",
26
+ "images",
27
+ "videos",
28
+ "optimization",
29
+ "plugins"
30
+ ],
31
+ "scripts": {
32
+ "prepack": "dtwo-module-build",
33
+ "dev": "dtwc dev playground",
34
+ "dev:build": "dtwc build playground",
35
+ "dev:prepare": "dtwo-module-build --stub && dtwc prepare playground",
36
+ "release": "yarn run prepack && changelogen --release --output=CHANGELOG.md && yarn publish && git push --follow-tags"
37
+ },
38
+ "dependencies": {
39
+ "@cloudinary-util/url-loader": "5.2.1",
40
+ "@cloudinary-util/types": "1.0.1",
41
+ "@dtwo/kit": "3.2.0",
42
+ "unpic-kdu": "0.0.42",
43
+ "defu": "6.1.2"
44
+ },
45
+ "devDependencies": {
46
+ "@dtwo/module-builder": "0.2.1",
47
+ "@dtwo/schema": "3.2.0",
48
+ "changelogen": "0.4.1",
49
+ "eslint": "8.34.0",
50
+ "dtwo": "3.2.0"
51
+ }
52
+ }