@@ -3,176 +3,31 @@ import { useHead } from '@unhead/vue'
3
3
import { ref , watch } from ' vue'
4
4
import {
5
5
type ConfigOptions ,
6
+ type GetUploadWidgetOptions ,
7
+ type GenerateUploadWidgetResultCallback ,
6
8
generateSignatureCallback ,
7
9
generateUploadWidgetResultCallback ,
8
10
getUploadWidgetOptions ,
9
11
UPLOAD_WIDGET_EVENTS ,
10
12
} from ' @cloudinary-util/url-loader'
11
13
import type { CloudinaryUploadWidgetResults } from ' @cloudinary-util/types'
14
+ import { triggerOnIdle } from ' ../util/triggerOnIdle'
12
15
import { useRuntimeConfig } from ' #imports'
13
16
14
- export interface CldUploadWidgetProps {
15
- // eslint-disable-next-line @typescript-eslint/ban-types
16
- onClose? : Function
17
- // eslint-disable-next-line @typescript-eslint/ban-types
18
- onError? : Function
19
- // eslint-disable-next-line @typescript-eslint/ban-types
20
- onOpen? : Function
21
- // eslint-disable-next-line @typescript-eslint/ban-types
22
- onUpload? : Function
23
- // eslint-disable-next-line @typescript-eslint/ban-types
24
- onAbort? : Function
25
- // eslint-disable-next-line @typescript-eslint/ban-types
26
- onBatchCancelled? : Function
27
- // eslint-disable-next-line @typescript-eslint/ban-types
28
- onDisplayChanged? : Function
29
- // eslint-disable-next-line @typescript-eslint/ban-types
30
- onPublicId? : Function
31
- // eslint-disable-next-line @typescript-eslint/ban-types
32
- onQueuesEnd? : Function
33
- // eslint-disable-next-line @typescript-eslint/ban-types
34
- onQueuesStart? : Function
35
- // eslint-disable-next-line @typescript-eslint/ban-types
36
- onRetry? : Function
37
- // eslint-disable-next-line @typescript-eslint/ban-types
38
- onShowCompleted? : Function
39
- // eslint-disable-next-line @typescript-eslint/ban-types
40
- onSourceChanged? : Function
41
- // eslint-disable-next-line @typescript-eslint/ban-types
42
- onSuccess? : Function
43
- // eslint-disable-next-line @typescript-eslint/ban-types
44
- onTags? : Function
45
- // eslint-disable-next-line @typescript-eslint/ban-types
46
- onUploadAdded? : Function
47
- options? : CldUploadWidgetPropsOptions
17
+ export interface CldUploadWidgetProps
18
+ extends GenerateUploadWidgetResultCallback {
19
+ options? : GetUploadWidgetOptions
48
20
signatureEndpoint? : URL | RequestInfo
49
21
uploadPreset? : string
50
22
config? : ConfigOptions
51
23
tags? : Array <string >
52
24
}
53
25
54
- // Parameters sourced from:
55
- // https://cloudinary.com/documentation/upload_widget_reference#parameters
56
-
57
- export interface CldUploadWidgetPropsOptions {
58
- // Widget
59
-
60
- encryption? : {
61
- key: string
62
- iv: string
63
- }
64
- defaultSource? : string
65
- maxFiles? : number
66
- multiple? : boolean
67
- sources? : Array <
68
- | ' camera'
69
- | ' dropbox'
70
- | ' facebook'
71
- | ' gettyimages'
72
- | ' google_drive'
73
- | ' image_search'
74
- | ' instagram'
75
- | ' istock'
76
- | ' local'
77
- | ' shutterstock'
78
- | ' unsplash'
79
- | ' url'
80
- >
81
-
82
- // Cropping
83
-
84
- cropping? : boolean
85
- croppingAspectRatio? : number
86
- croppingCoordinatesMode? : string
87
- croppingDefaultSelectionRatio? : number
88
- croppingShowBackButton? : boolean
89
- croppingShowDimensions? : boolean
90
- showSkipCropButton? : boolean
91
-
92
- // Sources
93
-
94
- dropboxAppKey? : string
95
- facebookAppId? : string
96
- googleApiKey? : string
97
- googleDriveClientId? : string
98
- instagramClientId? : string
99
- searchByRights? : boolean
100
- searchBySites? : Array <string >
101
-
102
- // Upload
103
-
104
- context? : object
105
- folder? : string
106
- publicId? : string
107
- resourceType? : string
108
- tags? : Array <string >
109
- // eslint-disable-next-line @typescript-eslint/ban-types
110
- uploadSignature? : string | Function
111
- uploadSignatureTimestamp? : number
112
-
113
- // Client Side
114
-
115
- clientAllowedFormats? : Array <string >
116
- croppingValidateDimensions? : boolean
117
- maxChunkSize? : number
118
- maxImageFileSize? : number
119
- maxImageHeight? : number
120
- maxImageWidth? : number
121
- maxFileSize? : number
122
- maxRawFileSize? : number
123
- maxVideoFileSize? : number
124
- minImageHeight? : number
125
- minImageWidth? : number
126
- validateMaxWidthHeight? : boolean
127
-
128
- // Containing Page
129
-
130
- fieldName? : string
131
- form? : string
132
- thumbnails? : string
133
- thumbnailTransformation? : string | Array <object >
134
-
135
- // Customization
136
-
137
- buttonCaption? : string
138
- buttonClass? : string
139
- text? : object
140
- theme? : string
141
- styles? : object
142
-
143
- // Advanced
144
-
145
- autoMinimize? : boolean
146
- // eslint-disable-next-line @typescript-eslint/ban-types
147
- getTags? : Function
148
- // eslint-disable-next-line @typescript-eslint/ban-types
149
- getUploadPresets? : Function
150
- inlineContainer? : any // string or DOM element
151
- language? : string
152
- // eslint-disable-next-line @typescript-eslint/ban-types
153
- preBatch? : Function
154
- // eslint-disable-next-line @typescript-eslint/ban-types
155
- prepareUploadParams? : Function
156
- queueViewPosition? : string
157
- showAdvancedOptions? : boolean
158
- showCompletedButton? : boolean
159
- showInsecurePreview? : boolean
160
- showPoweredBy? : boolean
161
- showUploadMoreButton? : boolean
162
- singleUploadAutoClose? : boolean
163
- }
164
-
165
- export interface CldUploadWidgetResults {
166
- event: string
167
- info: string
168
- }
169
-
170
- function triggerOnIdle(callback : any ) {
171
- if (window && ' requestIdleCallback' in window ) {
172
- return requestIdleCallback (callback )
173
- }
174
- return setTimeout (() => callback (), 1 )
175
- }
26
+ type UploadActionFunction = (
27
+ results : CloudinaryUploadWidgetResults ,
28
+ data ? : any
29
+ ) => object
30
+ type CallbackFunction = (data ? : string []) => object
176
31
177
32
const props = defineProps <CldUploadWidgetProps >()
178
33
@@ -254,6 +109,7 @@ const uploadOptions = getUploadWidgetOptions(
254
109
{
255
110
cloud: {
256
111
cloudName: useRuntimeConfig ().public .cloudinary .cloudName ,
112
+ apiKey: useRuntimeConfig ().public .cloudinary .apiKey ,
257
113
},
258
114
... options ,
259
115
... instanceMethods ,
@@ -265,11 +121,8 @@ const resultsCallback = generateUploadWidgetResultCallback({
265
121
onError : (uploadError ) => {
266
122
error .value = uploadError
267
123
268
- if (typeof onError === ' function' ) {
269
- onError (uploadError , {
270
- widget: widget .value .current ,
271
- ... instanceMethods ,
272
- })
124
+ if (typeof onError === ' function' && results .value ) {
125
+ onError (uploadError , results .value )
273
126
}
274
127
},
275
128
onResult : (uploadResult ) => {
@@ -285,8 +138,7 @@ const resultsCallback = generateUploadWidgetResultCallback({
285
138
typeof widgetEvent === ' string'
286
139
&& typeof props [widgetEvent ] === ' function'
287
140
) {
288
- // eslint-disable-next-line @typescript-eslint/ban-types
289
- const callback = props [widgetEvent ] as Function
141
+ const callback = props [widgetEvent ] as UploadActionFunction
290
142
callback (uploadResult , {
291
143
widget: widget .value .current ,
292
144
... instanceMethods ,
@@ -296,8 +148,7 @@ const resultsCallback = generateUploadWidgetResultCallback({
296
148
const widgetEventAction = ` ${widgetEvent }Action ` as keyof typeof props
297
149
298
150
if (widgetEventAction && typeof props [widgetEventAction ] === ' function' ) {
299
- // eslint-disable-next-line @typescript-eslint/ban-types
300
- const action = props [widgetEventAction ] as Function
151
+ const action = props [widgetEventAction ] as UploadActionFunction
301
152
action (uploadResult )
302
153
}
303
154
},
@@ -306,8 +157,7 @@ const resultsCallback = generateUploadWidgetResultCallback({
306
157
if (props .tags ?.length ) {
307
158
uploadOptions .showAdvancedOptions = true
308
159
309
- // eslint-disable-next-line @typescript-eslint/ban-types
310
- uploadOptions .getTags = (cb : Function , prefix : string ) =>
160
+ uploadOptions .getTags = (cb : CallbackFunction , prefix : string ) =>
311
161
cb (prefix ? props .tags ?.filter (t => ! t .indexOf (prefix )) : props .tags )
312
162
}
313
163
@@ -322,7 +172,7 @@ watch(results, () => {
322
172
&& results .value .info === ' hidden'
323
173
324
174
if (isSuccess && typeof onUpload === ' function' ) {
325
- onUpload (results , widget .value )
175
+ onUpload (results .value )
326
176
}
327
177
328
178
if (isClosed && typeof onClose === ' function' ) {
@@ -332,7 +182,7 @@ watch(results, () => {
332
182
333
183
watch (error , () => {
334
184
if (error .value && typeof onError === ' function' ) {
335
- onError (error , widget .value )
185
+ onError (error . value , widget .value )
336
186
}
337
187
})
338
188
@@ -344,7 +194,9 @@ watch(error, () => {
344
194
function handleOnLoad() {
345
195
isScriptLoading .value = false
346
196
if (! cloudinary .value ) {
347
- cloudinary .value = (window as any ).cloudinary
197
+ if (' cloudinary' in window ) {
198
+ cloudinary .value = window .cloudinary
199
+ }
348
200
}
349
201
350
202
// To help improve load time of the widget on first instance, use requestIdleCallback
0 commit comments