next-cloudinary 0.1.1 → 0.3.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.
@@ -1,6 +1,9 @@
1
1
  import Image from 'next/image';
2
2
  import { Cloudinary } from '@cloudinary/url-gen';
3
- import { jsx } from 'react/jsx-runtime';
3
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
+ import Head from 'next/head';
5
+ import { useRef } from 'react';
6
+ import Script from 'next/script';
4
7
 
5
8
  function _extends() {
6
9
  _extends = Object.assign ? Object.assign.bind() : function (target) {
@@ -34,96 +37,32 @@ function _objectWithoutPropertiesLoose(source, excluded) {
34
37
  return target;
35
38
  }
36
39
 
37
- function _readOnlyError(name) {
38
- throw new TypeError("\"" + name + "\" is read-only");
39
- }
40
-
41
- var cld$1 = new Cloudinary({
42
- cloud: {
43
- cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
44
- },
45
- url: {
46
- // Used to avoid issues with SSR particularly for the blurred placeholder
47
- analytics: false
48
- }
49
- });
50
- /**
51
- * Retrieves the public id of a cloudiary image url. If no url is recognized it returns the parameter it self.
52
- * If it's recognized that is a url and it's not possible to get the public id, it warns the user.
53
- *
54
- * @param {string} src The cloudiary url or public id.
55
- *
56
- * @return {string} The images public id
57
- */
58
-
59
- function getPublicId(src) {
60
- if (src.includes('res.cloudinary.com')) {
61
- var regexWithTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(.*)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
62
- var regexWithoutTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
63
- var withTransformations = src.match(regexWithTransformations);
64
- var withoutTransformations = src.match(regexWithoutTransformations);
65
-
66
- if (withTransformations) {
67
- return withTransformations[withTransformations.length - 1];
68
- } else if (withoutTransformations) {
69
- return withoutTransformations[withoutTransformations.length - 1];
70
- } else {
71
- console.warn("Not possible to retrieve the publicUrl from " + src + ", make sure it's a valid cloudinary image url.");
72
- }
73
- }
74
-
75
- return src;
76
- }
77
- /**
78
- * createPlaceholderUrl
79
- */
80
-
81
- function createPlaceholderUrl(_ref) {
82
- var src = _ref.src,
83
- placeholder = _ref.placeholder;
84
- var cldImage = cld$1.image(src).resize('c_limit,w_100').delivery('q_1').format('auto');
85
-
86
- if (placeholder === 'grayscale') {
87
- cldImage.effect('e_grayscale');
88
- }
89
-
90
- if (placeholder.includes('color:')) {
91
- var color = placeholder.split(':').splice(1).join(':');
92
- cldImage.effect('e_grayscale');
93
- cldImage.effect("e_colorize:60,co_" + color);
94
- }
95
-
96
- return cldImage.toURL();
97
- }
98
-
99
40
  var cropsGravityAuto = ['crop', 'fill', 'lfill', 'fill_pad', 'thumb'];
100
41
  var props$6 = ['crop', 'gravity'];
101
42
  function plugin$6(_temp) {
102
43
  var _ref = _temp === void 0 ? {} : _temp,
103
44
  cldImage = _ref.cldImage,
104
- options = _ref.options,
105
- cldOptions = _ref.cldOptions;
45
+ options = _ref.options;
106
46
 
107
47
  var width = options.width,
108
- height = options.height;
109
- var _cldOptions$crop = cldOptions.crop,
110
- crop = _cldOptions$crop === void 0 ? 'limit' : _cldOptions$crop,
111
- gravity = cldOptions.gravity;
48
+ height = options.height,
49
+ _options$crop = options.crop,
50
+ crop = _options$crop === void 0 ? 'limit' : _options$crop;
112
51
  var transformationString = "c_" + crop + ",w_" + width;
113
52
 
114
- if (!gravity && cropsGravityAuto.includes(crop)) {
115
- _readOnlyError("gravity");
53
+ if (!options.gravity && cropsGravityAuto.includes(crop)) {
54
+ options.gravity = 'auto';
116
55
  }
117
56
 
118
57
  if (!['limit'].includes(crop)) {
119
58
  transformationString = transformationString + ",h_" + height;
120
59
  }
121
60
 
122
- if (gravity) {
123
- if (gravity === 'auto' && !cropsGravityAuto.includes(crop)) {
61
+ if (options.gravity) {
62
+ if (options.gravity === 'auto' && !cropsGravityAuto.includes(crop)) {
124
63
  console.warn('Auto gravity can only be used with crop, fill, lfill, fill_pad or thumb. Not applying gravity.');
125
64
  } else {
126
- transformationString = transformationString + ",g_" + gravity;
65
+ transformationString = transformationString + ",g_" + options.gravity;
127
66
  }
128
67
  }
129
68
 
@@ -188,20 +127,16 @@ var props$5 = params.map(function (param) {
188
127
  function plugin$5(_temp) {
189
128
  var _ref = _temp === void 0 ? {} : _temp,
190
129
  cldImage = _ref.cldImage,
191
- cldOptions = _ref.cldOptions;
130
+ options = _ref.options;
192
131
 
193
132
  params.forEach(function (key) {
194
133
  var prop = key.prop || key;
195
134
  var effect = key.effect || key;
196
135
 
197
- if (prop === 'oilPaint' && cldOptions[prop]) {
198
- console.log('cldOptions[prop]', cldOptions[prop]);
199
- }
200
-
201
- if (cldOptions[prop] === true) {
136
+ if (options[prop] === true) {
202
137
  cldImage.effect("e_" + effect);
203
- } else if (typeof cldOptions[prop] === 'string') {
204
- cldImage.effect("e_" + effect + ":" + cldOptions[prop]);
138
+ } else if (typeof options[prop] === 'string') {
139
+ cldImage.effect("e_" + effect + ":" + options[prop]);
205
140
  }
206
141
  });
207
142
  }
@@ -266,27 +201,55 @@ var text = {
266
201
  }
267
202
  };
268
203
 
269
- var _excluded$1 = ["publicId", "position", "text", "effects"];
270
- var props$4 = ['overlays'];
204
+ var _excluded$3 = ["publicId", "position", "text", "effects"];
205
+ var props$4 = ['text', 'overlays'];
206
+ var DEFAULT_TEXT_OPTIONS = {
207
+ color: 'black',
208
+ fontFamily: 'Arial',
209
+ fontSize: 200,
210
+ fontWeight: 'bold'
211
+ };
271
212
  function plugin$4(_temp) {
272
213
  var _ref = _temp === void 0 ? {} : _temp,
273
214
  cldImage = _ref.cldImage,
274
- cldOptions = _ref.cldOptions;
215
+ options = _ref.options;
275
216
 
276
- var _cldOptions$overlays = cldOptions.overlays,
277
- overlays = _cldOptions$overlays === void 0 ? [] : _cldOptions$overlays;
217
+ var text$1 = options.text,
218
+ _options$overlays = options.overlays,
219
+ overlays = _options$overlays === void 0 ? [] : _options$overlays;
278
220
  var type = 'overlay';
279
221
  var typeQualifier = 'l';
280
- overlays.forEach(function (_ref2) {
222
+
223
+ if (Array.isArray(overlays)) {
224
+ overlays.forEach(applyOverlay);
225
+ }
226
+
227
+ if (typeof text$1 === 'string') {
228
+ applyOverlay({
229
+ text: _extends({}, DEFAULT_TEXT_OPTIONS, {
230
+ text: text$1
231
+ })
232
+ });
233
+ } else if (typeof text$1 === 'object') {
234
+ applyOverlay({
235
+ text: _extends({}, DEFAULT_TEXT_OPTIONS, text$1)
236
+ });
237
+ }
238
+ /**
239
+ * applyOverlay
240
+ */
241
+
242
+
243
+ function applyOverlay(_ref2) {
281
244
  var publicId = _ref2.publicId,
282
245
  position$1 = _ref2.position,
283
246
  text$1 = _ref2.text,
284
247
  _ref2$effects = _ref2.effects,
285
248
  layerEffects = _ref2$effects === void 0 ? [] : _ref2$effects,
286
- options = _objectWithoutPropertiesLoose(_ref2, _excluded$1);
249
+ options = _objectWithoutPropertiesLoose(_ref2, _excluded$3);
287
250
 
288
251
  var hasPublicId = typeof publicId === 'string';
289
- var hasText = typeof text$1 === 'object';
252
+ var hasText = typeof text$1 === 'object' || typeof text$1 === 'string';
290
253
  var hasPosition = typeof position$1 === 'object';
291
254
 
292
255
  if (!hasPublicId && !hasText) {
@@ -324,6 +287,12 @@ function plugin$4(_temp) {
324
287
  }); // Text styling
325
288
 
326
289
  if (hasText) {
290
+ if (typeof text$1 === 'string') {
291
+ text$1 = _extends({}, DEFAULT_TEXT_OPTIONS, {
292
+ text: text$1
293
+ });
294
+ }
295
+
327
296
  var textTransformations = [];
328
297
  Object.keys(text$1).forEach(function (key) {
329
298
  if (!text[key]) return;
@@ -352,7 +321,7 @@ function plugin$4(_temp) {
352
321
 
353
322
  layerTransformation = layerTransformation + "," + primary$1.join(','); // Add all applied transformations
354
323
 
355
- layerTransformation = layerTransformation + "/fl_layer_apply";
324
+ layerTransformation = layerTransformation + "/fl_layer_apply,fl_no_overflow";
356
325
 
357
326
  if (applied.length > 0) {
358
327
  layerTransformation = layerTransformation + "," + applied.join(',');
@@ -360,12 +329,13 @@ function plugin$4(_temp) {
360
329
 
361
330
 
362
331
  cldImage.addTransformation(layerTransformation);
363
- });
332
+ }
364
333
  }
365
334
 
366
335
  var overlaysPlugin = {
367
336
  __proto__: null,
368
337
  props: props$4,
338
+ DEFAULT_TEXT_OPTIONS: DEFAULT_TEXT_OPTIONS,
369
339
  plugin: plugin$4
370
340
  };
371
341
 
@@ -373,10 +343,10 @@ var props$3 = ['rawTransformations'];
373
343
  function plugin$3(_temp) {
374
344
  var _ref = _temp === void 0 ? {} : _temp,
375
345
  cldImage = _ref.cldImage,
376
- cldOptions = _ref.cldOptions;
346
+ options = _ref.options;
377
347
 
378
- var _cldOptions$rawTransf = cldOptions.rawTransformations,
379
- rawTransformations = _cldOptions$rawTransf === void 0 ? [] : _cldOptions$rawTransf;
348
+ var _options$rawTransform = options.rawTransformations,
349
+ rawTransformations = _options$rawTransform === void 0 ? [] : _options$rawTransform;
380
350
  rawTransformations.forEach(function (transformation) {
381
351
  cldImage.addTransformation(transformation);
382
352
  });
@@ -392,10 +362,10 @@ var props$2 = ['removeBackground'];
392
362
  function plugin$2(_temp) {
393
363
  var _ref = _temp === void 0 ? {} : _temp,
394
364
  cldImage = _ref.cldImage,
395
- cldOptions = _ref.cldOptions;
365
+ options = _ref.options;
396
366
 
397
- var _cldOptions$removeBac = cldOptions.removeBackground,
398
- removeBackground = _cldOptions$removeBac === void 0 ? false : _cldOptions$removeBac;
367
+ var _options$removeBackgr = options.removeBackground,
368
+ removeBackground = _options$removeBackgr === void 0 ? false : _options$removeBackgr;
399
369
 
400
370
  if (removeBackground) {
401
371
  cldImage.effect('e_background_removal');
@@ -408,23 +378,50 @@ var removeBackgroundPlugin = {
408
378
  plugin: plugin$2
409
379
  };
410
380
 
411
- var _excluded = ["publicId", "type", "position", "text", "effects"];
412
- var props$1 = ['underlays'];
381
+ var _excluded$2 = ["publicId", "type", "position", "text", "effects"];
382
+ var props$1 = ['underlay', 'underlays'];
413
383
  function plugin$1(_temp) {
414
384
  var _ref = _temp === void 0 ? {} : _temp,
415
385
  cldImage = _ref.cldImage,
416
- cldOptions = _ref.cldOptions;
386
+ options = _ref.options;
417
387
 
418
- var _cldOptions$underlays = cldOptions.underlays,
419
- underlays = _cldOptions$underlays === void 0 ? [] : _cldOptions$underlays;
388
+ var underlay = options.underlay,
389
+ _options$underlays = options.underlays,
390
+ underlays = _options$underlays === void 0 ? [] : _options$underlays;
420
391
  var typeQualifier = 'u';
421
- underlays.forEach(function (_ref2) {
392
+
393
+ if (Array.isArray(underlays)) {
394
+ underlays.forEach(applyUnderlay);
395
+ }
396
+
397
+ if (typeof underlay === 'string') {
398
+ var underlayOptions = {
399
+ publicId: underlay,
400
+ crop: 'fill'
401
+ };
402
+
403
+ if (options.width) {
404
+ underlayOptions.width = options.width;
405
+ }
406
+
407
+ if (options.height) {
408
+ underlayOptions.height = options.height;
409
+ }
410
+
411
+ applyUnderlay(underlayOptions);
412
+ }
413
+ /**
414
+ * applyUnderlay
415
+ */
416
+
417
+
418
+ function applyUnderlay(_ref2) {
422
419
  var publicId = _ref2.publicId,
423
420
  type = _ref2.type,
424
421
  position$1 = _ref2.position,
425
422
  _ref2$effects = _ref2.effects,
426
423
  layerEffects = _ref2$effects === void 0 ? [] : _ref2$effects,
427
- options = _objectWithoutPropertiesLoose(_ref2, _excluded);
424
+ options = _objectWithoutPropertiesLoose(_ref2, _excluded$2);
428
425
 
429
426
  var hasPublicId = typeof publicId === 'string';
430
427
  var hasPosition = typeof position$1 === 'object';
@@ -467,7 +464,7 @@ function plugin$1(_temp) {
467
464
 
468
465
  layerTransformation = layerTransformation + "," + primary$1.join(','); // Add all applied transformations
469
466
 
470
- layerTransformation = layerTransformation + "/fl_layer_apply";
467
+ layerTransformation = layerTransformation + "/fl_layer_apply,fl_no_overflow";
471
468
 
472
469
  if (applied.length > 0) {
473
470
  layerTransformation = layerTransformation + "," + applied.join(',');
@@ -475,7 +472,7 @@ function plugin$1(_temp) {
475
472
 
476
473
 
477
474
  cldImage.addTransformation(layerTransformation);
478
- });
475
+ }
479
476
  }
480
477
 
481
478
  var underlaysPlugin = {
@@ -488,11 +485,11 @@ var props = ['zoompan'];
488
485
  function plugin(_temp) {
489
486
  var _ref = _temp === void 0 ? {} : _temp,
490
487
  cldImage = _ref.cldImage,
491
- cldOptions = _ref.cldOptions;
488
+ options = _ref.options;
492
489
 
493
- var _cldOptions$zoompan = cldOptions.zoompan,
494
- zoompan = _cldOptions$zoompan === void 0 ? false : _cldOptions$zoompan;
495
- var options = {};
490
+ var _options$zoompan = options.zoompan,
491
+ zoompan = _options$zoompan === void 0 ? false : _options$zoompan;
492
+ var overrides = {};
496
493
 
497
494
  if (zoompan === true) {
498
495
  cldImage.effect('e_zoompan');
@@ -525,11 +522,11 @@ function plugin(_temp) {
525
522
  }
526
523
 
527
524
  if (zoompan !== false) {
528
- options.format = 'gif';
525
+ overrides.format = 'gif';
529
526
  }
530
527
 
531
528
  return {
532
- options: options
529
+ options: overrides
533
530
  };
534
531
  }
535
532
 
@@ -539,15 +536,18 @@ var zoompanPlugin = {
539
536
  plugin: plugin
540
537
  };
541
538
 
542
- var cld;
543
539
  var transformationPlugins = [// Background Removal must always come first
544
540
  removeBackgroundPlugin, croppingPlugin, effectsPlugin, overlaysPlugin, underlaysPlugin, zoompanPlugin, // Raw transformations needs to be last simply to make sure
545
541
  // it's always expected to applied the same way
546
542
  rawTransformationsPlugin];
547
- function cloudinaryLoader(defaultOptions, cldOptions, cldConfig) {
548
- if (cldConfig === void 0) {
549
- cldConfig = {};
550
- }
543
+ var cld;
544
+ /**
545
+ * constructCloudinaryUrl
546
+ */
547
+
548
+ function constructCloudinaryUrl(_ref) {
549
+ var options = _ref.options,
550
+ config = _ref.config;
551
551
 
552
552
  if (!cld) {
553
553
  cld = new Cloudinary(_extends({
@@ -558,31 +558,98 @@ function cloudinaryLoader(defaultOptions, cldOptions, cldConfig) {
558
558
  // Used to avoid issues with SSR particularly for the blurred placeholder
559
559
  analytics: false
560
560
  }
561
- }, cldConfig));
561
+ }, config));
562
562
  }
563
563
 
564
- var options = _extends({
565
- format: 'auto',
566
- quality: 'auto'
567
- }, defaultOptions);
568
-
569
564
  var publicId = getPublicId(options.src);
570
565
  var cldImage = cld.image(publicId);
571
- transformationPlugins.forEach(function (_ref) {
572
- var plugin = _ref.plugin;
566
+ transformationPlugins.forEach(function (_ref2) {
567
+ var plugin = _ref2.plugin;
573
568
 
574
- var _ref2 = plugin({
569
+ var _ref3 = plugin({
575
570
  cldImage: cldImage,
576
- options: options,
577
- cldOptions: cldOptions
571
+ options: options
578
572
  }) || {},
579
- pluginOptions = _ref2.options;
573
+ pluginOptions = _ref3.options;
580
574
 
581
575
  if (pluginOptions != null && pluginOptions.format) {
582
576
  options.format = pluginOptions.format;
583
577
  }
584
578
  });
585
- return cldImage.format(options.format).delivery("q_" + options.quality).toURL();
579
+ return cldImage.setDeliveryType(options.deliveryType || 'upload').format(options.format || 'auto').delivery("q_" + (options.quality || 'auto')).toURL();
580
+ }
581
+ /**
582
+ * Retrieves the public id of a cloudiary image url. If no url is recognized it returns the parameter it self.
583
+ * If it's recognized that is a url and it's not possible to get the public id, it warns the user.
584
+ *
585
+ * @param {string} src The cloudiary url or public id.
586
+ *
587
+ * @return {string} The images public id
588
+ */
589
+
590
+ function getPublicId(src) {
591
+ if (typeof src !== 'string') {
592
+ throw new Error("Invalid src of type " + typeof src);
593
+ }
594
+
595
+ if (src.includes('res.cloudinary.com')) {
596
+ var regexWithTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(.*)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
597
+ var regexWithoutTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
598
+ var withTransformations = src.match(regexWithTransformations);
599
+ var withoutTransformations = src.match(regexWithoutTransformations);
600
+
601
+ if (withTransformations) {
602
+ return withTransformations[withTransformations.length - 1];
603
+ } else if (withoutTransformations) {
604
+ return withoutTransformations[withoutTransformations.length - 1];
605
+ } else {
606
+ console.warn("Not possible to retrieve the publicUrl from " + src + ", make sure it's a valid cloudinary image url.");
607
+ }
608
+ }
609
+
610
+ return src;
611
+ }
612
+ /**
613
+ * createPlaceholderUrl
614
+ */
615
+
616
+ function createPlaceholderUrl(_ref4) {
617
+ var src = _ref4.src,
618
+ _ref4$placeholder = _ref4.placeholder,
619
+ placeholder = _ref4$placeholder === void 0 ? true : _ref4$placeholder,
620
+ config = _ref4.config;
621
+ var rawTransformations = [];
622
+
623
+ if (placeholder === 'grayscale') {
624
+ rawTransformations.push('e_grayscale');
625
+ }
626
+
627
+ if (typeof placeholder === 'string' && placeholder.includes('color:')) {
628
+ var color = placeholder.split(':').splice(1).join(':');
629
+ rawTransformations.push('e_grayscale');
630
+ rawTransformations.push("e_colorize:60,co_" + color);
631
+ }
632
+
633
+ return constructCloudinaryUrl({
634
+ options: {
635
+ src: src,
636
+ width: 100,
637
+ quality: 1,
638
+ rawTransformations: rawTransformations
639
+ },
640
+ config: config
641
+ });
642
+ }
643
+
644
+ function cloudinaryLoader(defaultOptions, cldOptions, cldConfig) {
645
+ if (cldConfig === void 0) {
646
+ cldConfig = {};
647
+ }
648
+
649
+ return constructCloudinaryUrl({
650
+ options: _extends({}, defaultOptions, cldOptions),
651
+ config: cldConfig
652
+ });
586
653
  }
587
654
 
588
655
  var CldImage = function CldImage(props) {
@@ -641,5 +708,187 @@ var CldImage = function CldImage(props) {
641
708
  }));
642
709
  };
643
710
 
644
- export { CldImage, cloudinaryLoader, position, primary, text };
711
+ var _excluded$1 = ["excludeTags", "twitterTitle"];
712
+ var IMAGE_WIDTH = 2400;
713
+ var IMAGE_HEIGHT = 1200;
714
+ var TWITTER_CARD = 'summary_large_image';
715
+
716
+ var CldOgImage = function CldOgImage(_ref) {
717
+ var _ref$excludeTags = _ref.excludeTags,
718
+ excludeTags = _ref$excludeTags === void 0 ? [] : _ref$excludeTags,
719
+ twitterTitle = _ref.twitterTitle,
720
+ props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
721
+
722
+ var options = _extends({}, props, {
723
+ width: props.width || IMAGE_WIDTH,
724
+ height: props.height || IMAGE_HEIGHT,
725
+ crop: props.crop || 'fill',
726
+ gravity: props.gravity || 'center'
727
+ });
728
+
729
+ var ogImageUrl = constructCloudinaryUrl({
730
+ options: options
731
+ }); // We need to include the tags within the Next.js Head component rather than
732
+ // direcly adding them inside of the Head otherwise we get unexpected results
733
+
734
+ return /*#__PURE__*/jsxs(Head, {
735
+ children: [/*#__PURE__*/jsx("meta", {
736
+ property: "og:image",
737
+ content: ogImageUrl
738
+ }), /*#__PURE__*/jsx("meta", {
739
+ property: "og:image:secure_url",
740
+ content: ogImageUrl
741
+ }), /*#__PURE__*/jsx("meta", {
742
+ property: "og:image:width",
743
+ content: options.width
744
+ }), /*#__PURE__*/jsx("meta", {
745
+ property: "og:image:height",
746
+ content: options.height
747
+ }), options.alt && /*#__PURE__*/jsx("meta", {
748
+ property: "og:image:alt",
749
+ content: options.alt
750
+ }), !excludeTags.includes('twitter:title') && /*#__PURE__*/jsx("meta", {
751
+ property: "twitter:title",
752
+ content: twitterTitle || ' '
753
+ }), /*#__PURE__*/jsx("meta", {
754
+ property: "twitter:card",
755
+ content: TWITTER_CARD
756
+ }), /*#__PURE__*/jsx("meta", {
757
+ property: "twitter:image",
758
+ content: ogImageUrl
759
+ })]
760
+ });
761
+ };
762
+
763
+ var CldUploadWidget = function CldUploadWidget(_ref) {
764
+ var children = _ref.children,
765
+ onUpload = _ref.onUpload,
766
+ options = _ref.options,
767
+ signed = _ref.signed,
768
+ signatureEndpoint = _ref.signatureEndpoint;
769
+ var cloudinary = useRef();
770
+ var widget = useRef();
771
+ /**
772
+ * generateSignature
773
+ * @description Makes a request to an endpoint to sign Cloudinary parameters as part of widget creation
774
+ */
775
+
776
+ function generateSignature(callback, paramsToSign) {
777
+ fetch(signatureEndpoint, {
778
+ method: "POST",
779
+ body: JSON.stringify({
780
+ paramsToSign: paramsToSign
781
+ })
782
+ }).then(function (r) {
783
+ return r.json();
784
+ }).then(function (_ref2) {
785
+ var signature = _ref2.signature;
786
+ callback(signature);
787
+ });
788
+ }
789
+ /**
790
+ * createWidget
791
+ * @description Creates a new instance of the Cloudinary widget and stores in a ref
792
+ */
793
+
794
+
795
+ function createWidget() {
796
+ var _cloudinary$current;
797
+
798
+ // When creating a signed upload, you need to provide both your Cloudinary API Key
799
+ // as well as a signature generator function that will sign any paramters
800
+ // either on page load or during the upload process. Read more about signed uploads at:
801
+ // https://cloudinary.com/documentation/upload_widget#signed_uploads
802
+ var totalOptions = _extends({}, options, !!signed && {
803
+ uploadSignature: generateSignature
804
+ }); // no need for apiSecret because of api/sign-cloudinary-params
805
+
806
+
807
+ if (signed && !totalOptions.apiKey) {
808
+ return new Error("Signed Upload needs apiKey!");
809
+ }
810
+
811
+ return (_cloudinary$current = cloudinary.current) == null ? void 0 : _cloudinary$current.createUploadWidget(totalOptions, function (error, result) {
812
+ // The callback is a bit more chatty than failed or success so
813
+ // only trigger when one of those are the case. You can additionally
814
+ // create a separate handler such as onEvent and trigger it on
815
+ // ever occurance
816
+ if (error || result.event === "success") {
817
+ onUpload(error, result, widget == null ? void 0 : widget.current);
818
+ }
819
+ });
820
+ }
821
+ /**
822
+ * open
823
+ * @description When triggered, uses the current widget instance to open the upload modal
824
+ */
825
+
826
+
827
+ function open() {
828
+ if (!(widget != null && widget.current)) {
829
+ widget.current = createWidget();
830
+ }
831
+
832
+ (widget == null ? void 0 : widget.current) && widget.current.open();
833
+ }
834
+ /**
835
+ * handleOnLoad
836
+ * @description Stores the Cloudinary window instance to a ref when the widget script loads
837
+ */
838
+
839
+
840
+ function handleOnLoad() {
841
+ cloudinary.current = window.cloudinary;
842
+ }
843
+
844
+ return /*#__PURE__*/jsxs(Fragment, {
845
+ children: [children({
846
+ cloudinary: cloudinary.current,
847
+ widget: widget.current,
848
+ open: open
849
+ }), /*#__PURE__*/jsx(Script, {
850
+ id: "cloudinary-" + Math.floor(Math.random() * 100),
851
+ src: "https://widget.cloudinary.com/v2.0/global/all.js",
852
+ onLoad: handleOnLoad,
853
+ onError: function onError(e) {
854
+ console.error("Script failed to load", e);
855
+ }
856
+ })]
857
+ });
858
+ };
859
+
860
+ var _excluded = ["onUpload", "options", "signed", "label", "children"];
861
+
862
+ var CldUploadButton = function CldUploadButton(_ref) {
863
+ var onUpload = _ref.onUpload,
864
+ options = _ref.options,
865
+ signed = _ref.signed,
866
+ _children = _ref.children,
867
+ props = _objectWithoutPropertiesLoose(_ref, _excluded);
868
+
869
+ return /*#__PURE__*/jsx(Fragment, {
870
+ children: /*#__PURE__*/jsx(CldUploadWidget, {
871
+ signed: signed,
872
+ options: options,
873
+ onUpload: onUpload,
874
+ signatureEndpoint: signed != null ? signed : props.signatureEndpoint,
875
+ children: function children(_ref2) {
876
+ var open = _ref2.open;
877
+
878
+ function handleOnClick(e) {
879
+ e.preventDefault();
880
+ open();
881
+ }
882
+
883
+ return /*#__PURE__*/jsx("button", _extends({
884
+ onClick: handleOnClick
885
+ }, props, {
886
+ children: _children
887
+ }));
888
+ }
889
+ })
890
+ });
891
+ };
892
+
893
+ export { CldImage, CldOgImage, CldUploadButton, CldUploadWidget, cloudinaryLoader, position, primary, text };
645
894
  //# sourceMappingURL=next-cloudinary.module.js.map