@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,149 @@
1
+ <script setup lang="ts">
2
+ export interface CldUploadWidgetPropsOptions {
3
+ // Widget
4
+
5
+ encryption?: {
6
+ key: string;
7
+ iv: string;
8
+ };
9
+ defaultSource?: string;
10
+ maxFiles?: number;
11
+ multiple?: boolean;
12
+ sources?: Array<
13
+ | "camera"
14
+ | "dropbox"
15
+ | "facebook"
16
+ | "gettyimages"
17
+ | "google_drive"
18
+ | "image_search"
19
+ | "instagram"
20
+ | "istock"
21
+ | "local"
22
+ | "shutterstock"
23
+ | "unsplash"
24
+ | "url"
25
+ >;
26
+
27
+ // Cropping
28
+
29
+ cropping?: boolean;
30
+ croppingAspectRatio?: number;
31
+ croppingCoordinatesMode?: string;
32
+ croppingDefaultSelectionRatio?: number;
33
+ croppingShowBackButton?: boolean;
34
+ croppingShowDimensions?: boolean;
35
+ showSkipCropButton?: boolean;
36
+
37
+ // Sources
38
+
39
+ dropboxAppKey?: string;
40
+ facebookAppId?: string;
41
+ googleApiKey?: string;
42
+ googleDriveClientId?: string;
43
+ instagramClientId?: string;
44
+ searchByRights?: boolean;
45
+ searchBySites?: Array<string>;
46
+
47
+ // Upload
48
+
49
+ context?: object;
50
+ folder?: string;
51
+ publicId?: string;
52
+ resourceType?: string;
53
+ tags?: Array<string>;
54
+ uploadSignature?: string | Function;
55
+ uploadSignatureTimestamp?: number;
56
+
57
+ // Client Side
58
+
59
+ clientAllowedFormats?: Array<string>;
60
+ croppingValidateDimensions?: boolean;
61
+ maxChunkSize?: number;
62
+ maxImageFileSize?: number;
63
+ maxImageHeight?: number;
64
+ maxImageWidth?: number;
65
+ maxFileSize?: number;
66
+ maxRawFileSize?: number;
67
+ maxVideoFileSize?: number;
68
+ minImageHeight?: number;
69
+ minImageWidth?: number;
70
+ validateMaxWidthHeight?: boolean;
71
+
72
+ // Containing Page
73
+
74
+ fieldName?: string;
75
+ form?: string;
76
+ thumbnails?: string;
77
+ thumbnailTransformation?: string | Array<object>;
78
+
79
+ // Customization
80
+
81
+ buttonCaption?: string;
82
+ buttonClass?: string;
83
+ text?: object;
84
+ theme?: string;
85
+ styles?: object;
86
+
87
+ // Advanced
88
+
89
+ autoMinimize?: boolean;
90
+ getTags?: Function;
91
+ getUploadPresets?: Function;
92
+ inlineContainer?: any; // string or DOM element
93
+ language?: string;
94
+ preBatch?: Function;
95
+ prepareUploadParams?: Function;
96
+ queueViewPosition?: string;
97
+ showAdvancedOptions?: boolean;
98
+ showCompletedButton?: boolean;
99
+ showInsecurePreview?: boolean;
100
+ showPoweredBy?: boolean;
101
+ showUploadMoreButton?: boolean;
102
+ singleUploadAutoClose?: boolean;
103
+ }
104
+
105
+ export interface CldUploadWidgetProps {
106
+ onClose?: Function;
107
+ onError?: Function;
108
+ onOpen?: Function;
109
+ onUpload?: Function;
110
+ onClick?: Function;
111
+ onAbort?: Function;
112
+ onBatchCancelled?: Function;
113
+ onDisplayChanged?: Function;
114
+ onPublicId?: Function;
115
+ onQueuesEnd?: Function;
116
+ onQueuesStart?: Function;
117
+ onRetry?: Function;
118
+ onShowCompleted?: Function;
119
+ onSourceChanged?: Function;
120
+ onSuccess?: Function;
121
+ onTags?: Function;
122
+ onUploadAdded?: Function;
123
+ options?: CldUploadWidgetPropsOptions;
124
+ signatureEndpoint?: URL | RequestInfo;
125
+ uploadPreset?: string;
126
+ }
127
+
128
+ defineProps<CldUploadWidgetProps>();
129
+ </script>
130
+
131
+ <template>
132
+ <CldUploadWidget
133
+ k-slot="{ open, isLoading }"
134
+ :upload-preset="uploadPreset"
135
+ >
136
+ <button
137
+ type="button"
138
+ k-bind="$attrs"
139
+ :disabled="isLoading"
140
+ @click="(e: Event) => {
141
+ e.preventDefault();
142
+ open();
143
+ if (typeof onClick === 'function') onClick(e)
144
+ }"
145
+ >
146
+ <slot />
147
+ </button>
148
+ </CldUploadWidget>
149
+ </template>
@@ -0,0 +1,410 @@
1
+ <script setup lang="ts">
2
+ import { useHead } from "@kdhead/kdu";
3
+ import { ref, watch } from "kdu";
4
+ import { useRuntimeConfig } from "#imports";
5
+
6
+ export interface CldUploadWidgetProps {
7
+ onClose?: Function;
8
+ onError?: Function;
9
+ onOpen?: Function;
10
+ onUpload?: Function;
11
+ onAbort?: Function;
12
+ onBatchCancelled?: Function;
13
+ onDisplayChanged?: Function;
14
+ onPublicId?: Function;
15
+ onQueuesEnd?: Function;
16
+ onQueuesStart?: Function;
17
+ onRetry?: Function;
18
+ onShowCompleted?: Function;
19
+ onSourceChanged?: Function;
20
+ onSuccess?: Function;
21
+ onTags?: Function;
22
+ onUploadAdded?: Function;
23
+ options?: CldUploadWidgetPropsOptions;
24
+ signatureEndpoint?: URL | RequestInfo;
25
+ uploadPreset?: string;
26
+ }
27
+
28
+ // Parameters sourced from:
29
+ // https://cloudinary.com/documentation/upload_widget_reference#parameters
30
+
31
+ export interface CldUploadWidgetPropsOptions {
32
+ // Widget
33
+
34
+ encryption?: {
35
+ key: string;
36
+ iv: string;
37
+ };
38
+ defaultSource?: string;
39
+ maxFiles?: number;
40
+ multiple?: boolean;
41
+ sources?: Array<
42
+ | "camera"
43
+ | "dropbox"
44
+ | "facebook"
45
+ | "gettyimages"
46
+ | "google_drive"
47
+ | "image_search"
48
+ | "instagram"
49
+ | "istock"
50
+ | "local"
51
+ | "shutterstock"
52
+ | "unsplash"
53
+ | "url"
54
+ >;
55
+
56
+ // Cropping
57
+
58
+ cropping?: boolean;
59
+ croppingAspectRatio?: number;
60
+ croppingCoordinatesMode?: string;
61
+ croppingDefaultSelectionRatio?: number;
62
+ croppingShowBackButton?: boolean;
63
+ croppingShowDimensions?: boolean;
64
+ showSkipCropButton?: boolean;
65
+
66
+ // Sources
67
+
68
+ dropboxAppKey?: string;
69
+ facebookAppId?: string;
70
+ googleApiKey?: string;
71
+ googleDriveClientId?: string;
72
+ instagramClientId?: string;
73
+ searchByRights?: boolean;
74
+ searchBySites?: Array<string>;
75
+
76
+ // Upload
77
+
78
+ context?: object;
79
+ folder?: string;
80
+ publicId?: string;
81
+ resourceType?: string;
82
+ tags?: Array<string>;
83
+ uploadSignature?: string | Function;
84
+ uploadSignatureTimestamp?: number;
85
+
86
+ // Client Side
87
+
88
+ clientAllowedFormats?: Array<string>;
89
+ croppingValidateDimensions?: boolean;
90
+ maxChunkSize?: number;
91
+ maxImageFileSize?: number;
92
+ maxImageHeight?: number;
93
+ maxImageWidth?: number;
94
+ maxFileSize?: number;
95
+ maxRawFileSize?: number;
96
+ maxVideoFileSize?: number;
97
+ minImageHeight?: number;
98
+ minImageWidth?: number;
99
+ validateMaxWidthHeight?: boolean;
100
+
101
+ // Containing Page
102
+
103
+ fieldName?: string;
104
+ form?: string;
105
+ thumbnails?: string;
106
+ thumbnailTransformation?: string | Array<object>;
107
+
108
+ // Customization
109
+
110
+ buttonCaption?: string;
111
+ buttonClass?: string;
112
+ text?: object;
113
+ theme?: string;
114
+ styles?: object;
115
+
116
+ // Advanced
117
+
118
+ autoMinimize?: boolean;
119
+ getTags?: Function;
120
+ getUploadPresets?: Function;
121
+ inlineContainer?: any; // string or DOM element
122
+ language?: string;
123
+ preBatch?: Function;
124
+ prepareUploadParams?: Function;
125
+ queueViewPosition?: string;
126
+ showAdvancedOptions?: boolean;
127
+ showCompletedButton?: boolean;
128
+ showInsecurePreview?: boolean;
129
+ showPoweredBy?: boolean;
130
+ showUploadMoreButton?: boolean;
131
+ singleUploadAutoClose?: boolean;
132
+ }
133
+
134
+ export interface CldUploadWidgetResults {
135
+ event: string;
136
+ info: string;
137
+ }
138
+
139
+ function triggerOnIdle(callback: any) {
140
+ if (window && "requestIdleCallback" in window) {
141
+ return requestIdleCallback(callback);
142
+ }
143
+ return setTimeout(() => callback(), 1);
144
+ }
145
+
146
+ const WIDGET_WATCHED_EVENTS = ["success", "display-changed"];
147
+
148
+ const props = defineProps<CldUploadWidgetProps>();
149
+
150
+ // eslint-disable-next-line kdu/no-setup-props-destructure
151
+ const {
152
+ onClose,
153
+ onError,
154
+ onOpen,
155
+ onUpload,
156
+ options,
157
+ signatureEndpoint,
158
+ uploadPreset,
159
+ onAbort,
160
+ onBatchCancelled,
161
+ onDisplayChanged,
162
+ onPublicId,
163
+ onQueuesEnd,
164
+ onQueuesStart,
165
+ onRetry,
166
+ onShowCompleted,
167
+ onSourceChanged,
168
+ onSuccess,
169
+ onTags,
170
+ onUploadAdded,
171
+ } = props;
172
+
173
+ const cloudinary = ref();
174
+ const widget = ref();
175
+
176
+ const signed = !!signatureEndpoint;
177
+
178
+ const error = ref(undefined);
179
+ const results = ref<CldUploadWidgetResults | undefined>(undefined);
180
+ const isScriptLoading = ref(true);
181
+
182
+ // When creating a signed upload, you need to provide both your Cloudinary API Key
183
+ // as well as a signature generator function that will sign any paramters
184
+ // either on page load or during the upload process. Read more about signed uploads at:
185
+ // https://cloudinary.com/documentation/upload_widget#signed_uploads
186
+
187
+ const instanceMethods = {
188
+ close,
189
+ destroy,
190
+ hide,
191
+ isDestroyed,
192
+ isMinimized,
193
+ isShowing,
194
+ minimize,
195
+ open,
196
+ show,
197
+ update,
198
+ abort: onAbort,
199
+ "batch-cancelled": onBatchCancelled,
200
+ "display-changed": onDisplayChanged,
201
+ publicid: onPublicId,
202
+ "queues-end": onQueuesEnd,
203
+ "queues-start": onQueuesStart,
204
+ retry: onRetry,
205
+ "show-completed": onShowCompleted,
206
+ "source-changed": onSourceChanged,
207
+ success: onSuccess,
208
+ tags: onTags,
209
+ "upload-added": onUploadAdded,
210
+ };
211
+
212
+ const uploadOptions = {
213
+ cloudName: useRuntimeConfig().public.cloudinary.cloudName,
214
+ uploadPreset:
215
+ uploadPreset || useRuntimeConfig().public.cloudinary.uploadPreset,
216
+ apiKey: useRuntimeConfig().public.cloudinary.apiKey,
217
+ ...options,
218
+ ...instanceMethods,
219
+ };
220
+
221
+ if (signed) {
222
+ uploadOptions.uploadSignature = generateSignature;
223
+
224
+ if (!uploadOptions.apiKey) {
225
+ console.warn(`Missing dependency: Signed Upload requires an API key`);
226
+ }
227
+ }
228
+
229
+ // Handle result states and callbacks
230
+
231
+ watch(results, () => {
232
+ if (typeof results.value === "undefined") return;
233
+
234
+ const isSuccess = results.value?.event === "success";
235
+ const isClosed =
236
+ results.value?.event === "display-changed" &&
237
+ results.value.info === "hidden";
238
+
239
+ if (isSuccess && typeof onUpload === "function") {
240
+ onUpload(results, widget.value);
241
+ }
242
+
243
+ if (isClosed && typeof onClose === "function") {
244
+ onClose(widget.value);
245
+ }
246
+ });
247
+
248
+ watch(error, () => {
249
+ if (error.value && typeof onError === "function") {
250
+ onError(error, widget.value);
251
+ }
252
+ });
253
+
254
+ /**
255
+ * handleOnLoad
256
+ * @description Stores the Cloudinary window instance to a ref when the widget script loads
257
+ */
258
+
259
+ function handleOnLoad() {
260
+ isScriptLoading.value = false;
261
+ if (!cloudinary.value) {
262
+ cloudinary.value = (window as any).cloudinary;
263
+ }
264
+
265
+ // To help improve load time of the widget on first instance, use requestIdleCallback
266
+ // to trigger widget creation. Optional.
267
+
268
+ triggerOnIdle(() => {
269
+ if (!widget.value) {
270
+ widget.value = createWidget();
271
+ }
272
+ });
273
+ }
274
+
275
+ /**
276
+ * generateSignature
277
+ * @description Makes a request to an endpoint to sign Cloudinary parameters as part of widget creation
278
+ */
279
+
280
+ function generateSignature(callback: Function, paramsToSign: object) {
281
+ if (typeof signatureEndpoint === "undefined") {
282
+ throw Error("Failed to generate signature: signatureEndpoint undefined.");
283
+ }
284
+ fetch(signatureEndpoint, {
285
+ method: "POST",
286
+ headers: {
287
+ "Content-Type": "application/json",
288
+ },
289
+ body: JSON.stringify({
290
+ paramsToSign,
291
+ }),
292
+ })
293
+ .then((r) => r.json())
294
+ .then(({ signature }) => {
295
+ callback(signature);
296
+ });
297
+ }
298
+
299
+ /**
300
+ * createWidget
301
+ * @description Creates a new instance of the Cloudinary widget and stores in a ref
302
+ */
303
+
304
+ function createWidget() {
305
+ return cloudinary.value?.createUploadWidget(
306
+ uploadOptions,
307
+ (uploadError: any, uploadResult: any) => {
308
+ // The callback is a bit more chatty than failed or success so
309
+ // only trigger when one of those are the case. You can additionally
310
+ // create a separate handler such as onEvent and trigger it on
311
+ // ever occurrence
312
+
313
+ if (typeof uploadError !== "undefined") {
314
+ error.value = uploadError;
315
+ }
316
+
317
+ if (WIDGET_WATCHED_EVENTS.includes(uploadResult?.event)) {
318
+ results.value = uploadResult;
319
+ }
320
+ }
321
+ );
322
+ }
323
+
324
+ function invokeInstanceMethod(method: string) {
325
+ if (!widget.value) {
326
+ widget.value = createWidget();
327
+ }
328
+
329
+ if (typeof widget?.value[method] === "function") {
330
+ widget.value[method]();
331
+ }
332
+ }
333
+
334
+ function close() {
335
+ invokeInstanceMethod("close");
336
+ }
337
+
338
+ function destroy() {
339
+ invokeInstanceMethod("destroy");
340
+ }
341
+
342
+ function hide() {
343
+ invokeInstanceMethod("hide");
344
+ }
345
+
346
+ function isDestroyed() {
347
+ invokeInstanceMethod("isDestroyed");
348
+ }
349
+
350
+ function isMinimized() {
351
+ invokeInstanceMethod("isMinimized");
352
+ }
353
+
354
+ function isShowing() {
355
+ invokeInstanceMethod("isShowing");
356
+ }
357
+
358
+ function minimize() {
359
+ invokeInstanceMethod("minimize");
360
+ }
361
+
362
+ function show() {
363
+ invokeInstanceMethod("show");
364
+ }
365
+
366
+ function update() {
367
+ invokeInstanceMethod("update");
368
+ }
369
+ function open() {
370
+ invokeInstanceMethod("open");
371
+
372
+ if (typeof onOpen === "function") {
373
+ onOpen(widget.value);
374
+ }
375
+ }
376
+
377
+ useHead({
378
+ script: [
379
+ {
380
+ id: `cloudinary-uploadwidget-${Math.floor(Math.random() * 100)}`,
381
+ src: "https://widget.cloudinary.com/v2.0/global/all.js",
382
+ onload: handleOnLoad,
383
+ onerror: (e) =>
384
+ console.error(
385
+ `Failed to load Cloudinary Upload Widget: ${(e as any).message}`
386
+ ),
387
+ },
388
+ ],
389
+ });
390
+ </script>
391
+
392
+ <template>
393
+ <slot
394
+ :cloudinary="cloudinary"
395
+ :widget="widget"
396
+ :open="open"
397
+ :update="update"
398
+ :show="show"
399
+ :is-showing="isShowing"
400
+ :is-minimized="isMinimized"
401
+ :is-destroyed="isDestroyed"
402
+ :destroy="destroy"
403
+ :close="close"
404
+ :hide="hide"
405
+ :minimize="minimize"
406
+ :results="results"
407
+ :error="error"
408
+ :is-loading="isScriptLoading"
409
+ />
410
+ </template>