next-cloudinary 0.4.0 → 0.5.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,901 @@
1
+ var Image = require('next/image');
2
+ var urlGen = require('@cloudinary/url-gen');
3
+ var jsxRuntime = require('react/jsx-runtime');
4
+ var Head = require('next/head');
5
+ var react = require('react');
6
+ var Script = require('next/script');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var Image__default = /*#__PURE__*/_interopDefaultLegacy(Image);
11
+ var Head__default = /*#__PURE__*/_interopDefaultLegacy(Head);
12
+ var Script__default = /*#__PURE__*/_interopDefaultLegacy(Script);
13
+
14
+ function _extends() {
15
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
16
+ for (var i = 1; i < arguments.length; i++) {
17
+ var source = arguments[i];
18
+ for (var key in source) {
19
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
20
+ target[key] = source[key];
21
+ }
22
+ }
23
+ }
24
+ return target;
25
+ };
26
+ return _extends.apply(this, arguments);
27
+ }
28
+ function _objectWithoutPropertiesLoose(source, excluded) {
29
+ if (source == null) return {};
30
+ var target = {};
31
+ var sourceKeys = Object.keys(source);
32
+ var key, i;
33
+ for (i = 0; i < sourceKeys.length; i++) {
34
+ key = sourceKeys[i];
35
+ if (excluded.indexOf(key) >= 0) continue;
36
+ target[key] = source[key];
37
+ }
38
+ return target;
39
+ }
40
+
41
+ var cropsGravityAuto = ['crop', 'fill', 'lfill', 'fill_pad', 'thumb'];
42
+ var props$6 = ['crop', 'gravity'];
43
+ function plugin$6(_temp) {
44
+ var _ref = _temp === void 0 ? {} : _temp,
45
+ cldImage = _ref.cldImage,
46
+ options = _ref.options;
47
+ var width = options.width,
48
+ height = options.height,
49
+ _options$crop = options.crop,
50
+ crop = _options$crop === void 0 ? 'limit' : _options$crop;
51
+ var transformationString = "c_" + crop + ",w_" + width;
52
+ if (!options.gravity && cropsGravityAuto.includes(crop)) {
53
+ options.gravity = 'auto';
54
+ }
55
+ if (!['limit'].includes(crop)) {
56
+ transformationString = transformationString + ",h_" + height;
57
+ }
58
+ if (options.gravity) {
59
+ if (options.gravity === 'auto' && !cropsGravityAuto.includes(crop)) {
60
+ console.warn('Auto gravity can only be used with crop, fill, lfill, fill_pad or thumb. Not applying gravity.');
61
+ } else {
62
+ transformationString = transformationString + ",g_" + options.gravity;
63
+ }
64
+ }
65
+ cldImage.resize(transformationString);
66
+ }
67
+
68
+ var croppingPlugin = {
69
+ __proto__: null,
70
+ props: props$6,
71
+ plugin: plugin$6
72
+ };
73
+
74
+ var params = ['art', {
75
+ prop: 'autoBrightness',
76
+ effect: 'auto_brightness'
77
+ }, {
78
+ prop: 'autoColor',
79
+ effect: 'auto_color'
80
+ }, {
81
+ prop: 'autoContrast',
82
+ effect: 'auto_contrast'
83
+ }, {
84
+ prop: 'assistColorblind',
85
+ effect: 'assist_colorblind'
86
+ }, 'blackwhite', 'blur', {
87
+ prop: 'blurFaces',
88
+ effect: 'blur_faces'
89
+ }, {
90
+ prop: 'blurRegion',
91
+ effect: 'blur_region'
92
+ }, 'brightness', {
93
+ prop: 'brightnessHSB',
94
+ effect: 'brightness_hsb'
95
+ }, 'cartoonify', 'colorize', 'contrast', 'distort', {
96
+ prop: 'fillLight',
97
+ effect: 'fill_light'
98
+ }, 'gamma', {
99
+ prop: 'gradientFade',
100
+ effect: 'gradient_fade'
101
+ }, 'grayscale', 'improve', 'negate', {
102
+ prop: 'oilPaint',
103
+ effect: 'oil_paint'
104
+ }, 'outline', 'pixelate', {
105
+ prop: 'pixelateFaces',
106
+ effect: 'pixelate_faces'
107
+ }, {
108
+ prop: 'pixelateRegion',
109
+ effect: 'pixelate_region'
110
+ }, 'redeye', {
111
+ prop: 'replaceColor',
112
+ effect: 'replace_color'
113
+ }, 'saturation', 'sepia', 'shadow', 'sharpen', 'shear', {
114
+ prop: 'simulateColorblind',
115
+ effect: 'simulate_colorblind'
116
+ }, 'tint', {
117
+ prop: 'unsharpMask',
118
+ effect: 'unsharp_mask'
119
+ }, 'vectorize', 'vibrance', 'vignette'];
120
+ var props$5 = params.map(function (param) {
121
+ return param.prop || param;
122
+ });
123
+ function plugin$5(_temp) {
124
+ var _ref = _temp === void 0 ? {} : _temp,
125
+ cldImage = _ref.cldImage,
126
+ options = _ref.options;
127
+ params.forEach(function (key) {
128
+ var prop = key.prop || key;
129
+ var effect = key.effect || key;
130
+ if (options[prop] === true) {
131
+ cldImage.effect("e_" + effect);
132
+ } else if (typeof options[prop] === 'string') {
133
+ cldImage.effect("e_" + effect + ":" + options[prop]);
134
+ }
135
+ });
136
+ }
137
+
138
+ var effectsPlugin = {
139
+ __proto__: null,
140
+ props: props$5,
141
+ plugin: plugin$5
142
+ };
143
+
144
+ // aspectRatio
145
+
146
+ var primary = {
147
+ aspectRatio: {
148
+ qualifier: 'ar'
149
+ },
150
+ crop: {
151
+ qualifier: 'c'
152
+ },
153
+ gravity: {
154
+ qualifier: 'g'
155
+ },
156
+ height: {
157
+ qualifier: 'h'
158
+ },
159
+ width: {
160
+ qualifier: 'w'
161
+ }
162
+ };
163
+ var position = {
164
+ angle: {
165
+ qualifier: 'a'
166
+ },
167
+ gravity: {
168
+ qualifier: 'g'
169
+ },
170
+ x: {
171
+ qualifier: 'x'
172
+ },
173
+ y: {
174
+ qualifier: 'y'
175
+ }
176
+ };
177
+ var text = {
178
+ alignment: {
179
+ qualifier: 'alignment'
180
+ },
181
+ border: {
182
+ qualifier: 'bo',
183
+ location: 'primary'
184
+ },
185
+ color: {
186
+ qualifier: 'co',
187
+ location: 'primary'
188
+ },
189
+ fontFamily: {
190
+ qualifier: false
191
+ },
192
+ fontSize: {
193
+ qualifier: false
194
+ },
195
+ fontWeight: {
196
+ qualifier: false
197
+ },
198
+ letterSpacing: {
199
+ qualifier: 'letter_spacing'
200
+ },
201
+ lineSpacing: {
202
+ qualifier: 'line_spacing'
203
+ },
204
+ stroke: {
205
+ qualifier: 'self'
206
+ },
207
+ textDecoration: {
208
+ qualifier: false
209
+ }
210
+ };
211
+
212
+ var _excluded$3 = ["publicId", "position", "text", "effects"];
213
+ var props$4 = ['text', 'overlays'];
214
+ var DEFAULT_TEXT_OPTIONS = {
215
+ color: 'black',
216
+ fontFamily: 'Arial',
217
+ fontSize: 200,
218
+ fontWeight: 'bold'
219
+ };
220
+ function plugin$4(_temp) {
221
+ var _ref = _temp === void 0 ? {} : _temp,
222
+ cldImage = _ref.cldImage,
223
+ options = _ref.options;
224
+ var text$1 = options.text,
225
+ _options$overlays = options.overlays,
226
+ overlays = _options$overlays === void 0 ? [] : _options$overlays;
227
+ var type = 'overlay';
228
+ var typeQualifier = 'l';
229
+ if (Array.isArray(overlays)) {
230
+ overlays.forEach(applyOverlay);
231
+ }
232
+ if (typeof text$1 === 'string') {
233
+ applyOverlay({
234
+ text: _extends({}, DEFAULT_TEXT_OPTIONS, {
235
+ text: text$1
236
+ })
237
+ });
238
+ } else if (typeof text$1 === 'object') {
239
+ applyOverlay({
240
+ text: _extends({}, DEFAULT_TEXT_OPTIONS, text$1)
241
+ });
242
+ }
243
+
244
+ /**
245
+ * applyOverlay
246
+ */
247
+
248
+ function applyOverlay(_ref2) {
249
+ var publicId = _ref2.publicId,
250
+ position$1 = _ref2.position,
251
+ text$1 = _ref2.text,
252
+ _ref2$effects = _ref2.effects,
253
+ layerEffects = _ref2$effects === void 0 ? [] : _ref2$effects,
254
+ options = _objectWithoutPropertiesLoose(_ref2, _excluded$3);
255
+ var hasPublicId = typeof publicId === 'string';
256
+ var hasText = typeof text$1 === 'object' || typeof text$1 === 'string';
257
+ var hasPosition = typeof position$1 === 'object';
258
+ if (!hasPublicId && !hasText) {
259
+ console.warn("An " + type + " is missing Public ID or Text");
260
+ return;
261
+ }
262
+
263
+ // Start to construct the transformation string using text or the public ID
264
+ // if it's image-based
265
+
266
+ var layerTransformation;
267
+ if (hasText) {
268
+ layerTransformation = typeQualifier + "_text";
269
+ } else {
270
+ layerTransformation = typeQualifier + "_" + publicId.replace(/\//g, ':');
271
+ }
272
+
273
+ // Begin organizing transformations based on what it is and the location
274
+ // it needs to be placed in the URL
275
+
276
+ var primary$1 = [];
277
+ var applied = [];
278
+
279
+ // Gemeral options
280
+
281
+ Object.keys(options).forEach(function (key) {
282
+ if (!primary[key]) return;
283
+ var qualifier = primary[key].qualifier;
284
+ primary$1.push(qualifier + "_" + options[key]);
285
+ });
286
+
287
+ // Layer effects
288
+
289
+ layerEffects.forEach(function (effect) {
290
+ Object.keys(effect).forEach(function (key) {
291
+ if (!primary[key]) return;
292
+ var qualifier = primary[key].qualifier;
293
+ primary$1.push(qualifier + "_" + effect[key]);
294
+ });
295
+ });
296
+
297
+ // Text styling
298
+
299
+ if (hasText) {
300
+ if (typeof text$1 === 'string') {
301
+ text$1 = _extends({}, DEFAULT_TEXT_OPTIONS, {
302
+ text: text$1
303
+ });
304
+ }
305
+ var textTransformations = [];
306
+ Object.keys(text$1).forEach(function (key) {
307
+ if (!text[key]) return;
308
+ var _qualifiersText$key = text[key],
309
+ qualifier = _qualifiersText$key.qualifier,
310
+ location = _qualifiersText$key.location;
311
+ if (location === 'primary') {
312
+ primary$1.push(qualifier + "_" + text$1[key]);
313
+ } else if (qualifier === 'self') {
314
+ textTransformations.push(key);
315
+ } else if (qualifier) {
316
+ textTransformations.push(qualifier + "_" + text$1[key]);
317
+ } else {
318
+ textTransformations.push(text$1[key]);
319
+ }
320
+ });
321
+ layerTransformation = layerTransformation + ":" + textTransformations.join('_') + ":" + text$1.text;
322
+ }
323
+
324
+ // Positioning
325
+
326
+ if (hasPosition) {
327
+ Object.keys(position$1).forEach(function (key) {
328
+ if (!position[key]) return;
329
+ var qualifier = position[key].qualifier;
330
+ applied.push(qualifier + "_" + position$1[key]);
331
+ });
332
+ }
333
+
334
+ // Add all primary transformations
335
+
336
+ layerTransformation = layerTransformation + "," + primary$1.join(',');
337
+
338
+ // Add all applied transformations
339
+
340
+ layerTransformation = layerTransformation + "/fl_layer_apply,fl_no_overflow";
341
+ if (applied.length > 0) {
342
+ layerTransformation = layerTransformation + "," + applied.join(',');
343
+ }
344
+
345
+ // Finally add it to the image
346
+
347
+ cldImage.addTransformation(layerTransformation);
348
+ }
349
+ }
350
+
351
+ var overlaysPlugin = {
352
+ __proto__: null,
353
+ props: props$4,
354
+ DEFAULT_TEXT_OPTIONS: DEFAULT_TEXT_OPTIONS,
355
+ plugin: plugin$4
356
+ };
357
+
358
+ var props$3 = ['rawTransformations'];
359
+ function plugin$3(_temp) {
360
+ var _ref = _temp === void 0 ? {} : _temp,
361
+ cldImage = _ref.cldImage,
362
+ options = _ref.options;
363
+ var _options$rawTransform = options.rawTransformations,
364
+ rawTransformations = _options$rawTransform === void 0 ? [] : _options$rawTransform;
365
+ rawTransformations.forEach(function (transformation) {
366
+ cldImage.addTransformation(transformation);
367
+ });
368
+ }
369
+
370
+ var rawTransformationsPlugin = {
371
+ __proto__: null,
372
+ props: props$3,
373
+ plugin: plugin$3
374
+ };
375
+
376
+ var props$2 = ['removeBackground'];
377
+ function plugin$2(_temp) {
378
+ var _ref = _temp === void 0 ? {} : _temp,
379
+ cldImage = _ref.cldImage,
380
+ options = _ref.options;
381
+ var _options$removeBackgr = options.removeBackground,
382
+ removeBackground = _options$removeBackgr === void 0 ? false : _options$removeBackgr;
383
+ if (removeBackground) {
384
+ cldImage.effect('e_background_removal');
385
+ }
386
+ }
387
+
388
+ var removeBackgroundPlugin = {
389
+ __proto__: null,
390
+ props: props$2,
391
+ plugin: plugin$2
392
+ };
393
+
394
+ var _excluded$2 = ["publicId", "type", "position", "text", "effects"];
395
+ var props$1 = ['underlay', 'underlays'];
396
+ function plugin$1(_temp) {
397
+ var _ref = _temp === void 0 ? {} : _temp,
398
+ cldImage = _ref.cldImage,
399
+ options = _ref.options;
400
+ var underlay = options.underlay,
401
+ _options$underlays = options.underlays,
402
+ underlays = _options$underlays === void 0 ? [] : _options$underlays;
403
+ var typeQualifier = 'u';
404
+ if (Array.isArray(underlays)) {
405
+ underlays.forEach(applyUnderlay);
406
+ }
407
+ if (typeof underlay === 'string') {
408
+ var underlayOptions = {
409
+ publicId: underlay,
410
+ crop: 'fill'
411
+ };
412
+ if (options.width) {
413
+ underlayOptions.width = options.width;
414
+ }
415
+ if (options.height) {
416
+ underlayOptions.height = options.height;
417
+ }
418
+ applyUnderlay(underlayOptions);
419
+ }
420
+
421
+ /**
422
+ * applyUnderlay
423
+ */
424
+
425
+ function applyUnderlay(_ref2) {
426
+ var publicId = _ref2.publicId,
427
+ type = _ref2.type,
428
+ position$1 = _ref2.position,
429
+ _ref2$effects = _ref2.effects,
430
+ layerEffects = _ref2$effects === void 0 ? [] : _ref2$effects,
431
+ options = _objectWithoutPropertiesLoose(_ref2, _excluded$2);
432
+ var hasPublicId = typeof publicId === 'string';
433
+ var hasPosition = typeof position$1 === 'object';
434
+ if (!hasPublicId) {
435
+ console.warn("An " + type + " is missing a Public ID");
436
+ return;
437
+ }
438
+
439
+ // Start to construct the transformation string using text or the public ID
440
+ // if it's image-based
441
+
442
+ var layerTransformation = typeQualifier + "_" + publicId.replace(/\//g, ':');
443
+
444
+ // Begin organizing transformations based on what it is and the location
445
+ // it needs to be placed in the URL
446
+
447
+ var primary$1 = [];
448
+ var applied = [];
449
+
450
+ // Gemeral options
451
+
452
+ Object.keys(options).forEach(function (key) {
453
+ if (!primary[key]) return;
454
+ var qualifier = primary[key].qualifier;
455
+ primary$1.push(qualifier + "_" + options[key]);
456
+ });
457
+
458
+ // Layer effects
459
+
460
+ layerEffects.forEach(function (effect) {
461
+ Object.keys(effect).forEach(function (key) {
462
+ if (!primary[key]) return;
463
+ var qualifier = primary[key].qualifier;
464
+ primary$1.push(qualifier + "_" + effect[key]);
465
+ });
466
+ });
467
+
468
+ // Positioning
469
+
470
+ if (hasPosition) {
471
+ Object.keys(position$1).forEach(function (key) {
472
+ if (!position[key]) return;
473
+ var qualifier = position[key].qualifier;
474
+ applied.push(qualifier + "_" + position$1[key]);
475
+ });
476
+ }
477
+
478
+ // Add all primary transformations
479
+
480
+ layerTransformation = layerTransformation + "," + primary$1.join(',');
481
+
482
+ // Add all applied transformations
483
+
484
+ layerTransformation = layerTransformation + "/fl_layer_apply,fl_no_overflow";
485
+ if (applied.length > 0) {
486
+ layerTransformation = layerTransformation + "," + applied.join(',');
487
+ }
488
+
489
+ // Finally add it to the image
490
+
491
+ cldImage.addTransformation(layerTransformation);
492
+ }
493
+ }
494
+
495
+ var underlaysPlugin = {
496
+ __proto__: null,
497
+ props: props$1,
498
+ plugin: plugin$1
499
+ };
500
+
501
+ var props = ['zoompan'];
502
+ function plugin(_temp) {
503
+ var _ref = _temp === void 0 ? {} : _temp,
504
+ cldImage = _ref.cldImage,
505
+ options = _ref.options;
506
+ var _options$zoompan = options.zoompan,
507
+ zoompan = _options$zoompan === void 0 ? false : _options$zoompan;
508
+ var overrides = {};
509
+ if (zoompan === true) {
510
+ cldImage.effect('e_zoompan');
511
+ } else if (typeof zoompan === 'string') {
512
+ if (zoompan === 'loop') {
513
+ cldImage.effect('e_zoompan');
514
+ cldImage.effect('e_loop');
515
+ } else {
516
+ cldImage.effect("e_zoompan:" + zoompan);
517
+ }
518
+ } else if (typeof zoompan === 'object') {
519
+ var zoompanEffect = 'e_zoompan';
520
+ if (typeof zoompan.options === 'string') {
521
+ zoompanEffect = "" + zoompanEffect + zoompan.options;
522
+ }
523
+ cldImage.effect(zoompanEffect);
524
+ var loopEffect;
525
+ if (zoompan.loop === true) {
526
+ loopEffect = 'e_loop';
527
+ } else if (typeof zoompan.loop === 'string') {
528
+ loopEffect = "e_loop" + zoompan.loop;
529
+ }
530
+ if (loopEffect) {
531
+ cldImage.effect(loopEffect);
532
+ }
533
+ }
534
+ if (zoompan !== false) {
535
+ overrides.format = 'gif';
536
+ }
537
+ return {
538
+ options: overrides
539
+ };
540
+ }
541
+
542
+ var zoompanPlugin = {
543
+ __proto__: null,
544
+ props: props,
545
+ plugin: plugin
546
+ };
547
+
548
+ var transformationPlugins = [
549
+ // Background Removal must always come first
550
+ removeBackgroundPlugin, croppingPlugin, effectsPlugin, overlaysPlugin, underlaysPlugin, zoompanPlugin,
551
+ // Raw transformations needs to be last simply to make sure
552
+ // it's always expected to applied the same way
553
+
554
+ rawTransformationsPlugin];
555
+ var cld;
556
+
557
+ /**
558
+ * constructCloudinaryUrl
559
+ */
560
+
561
+ function constructCloudinaryUrl(_ref) {
562
+ var options = _ref.options,
563
+ config = _ref.config;
564
+ if (!cld) {
565
+ cld = new urlGen.Cloudinary(_extends({
566
+ cloud: {
567
+ cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
568
+ },
569
+ url: {
570
+ // Used to avoid issues with SSR particularly for the blurred placeholder
571
+ analytics: false
572
+ }
573
+ }, config));
574
+ }
575
+ var publicId = getPublicId(options.src);
576
+ var cldImage = cld.image(publicId);
577
+ transformationPlugins.forEach(function (_ref2) {
578
+ var plugin = _ref2.plugin;
579
+ var _ref3 = plugin({
580
+ cldImage: cldImage,
581
+ options: options
582
+ }) || {},
583
+ pluginOptions = _ref3.options;
584
+ if (pluginOptions != null && pluginOptions.format) {
585
+ options.format = pluginOptions.format;
586
+ }
587
+ });
588
+ return cldImage.setDeliveryType(options.deliveryType || 'upload').format(options.format || 'auto').delivery("q_" + (options.quality || 'auto')).toURL();
589
+ }
590
+
591
+ /**
592
+ * Retrieves the public id of a cloudiary image url. If no url is recognized it returns the parameter it self.
593
+ * If it's recognized that is a url and it's not possible to get the public id, it warns the user.
594
+ *
595
+ * @param {string} src The cloudiary url or public id.
596
+ *
597
+ * @return {string} The images public id
598
+ */
599
+
600
+ function getPublicId(src) {
601
+ if (typeof src !== 'string') {
602
+ throw new Error("Invalid src of type " + typeof src);
603
+ }
604
+ if (src.includes('res.cloudinary.com')) {
605
+ var regexWithTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(.*)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
606
+ var regexWithoutTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
607
+ var withTransformations = src.match(regexWithTransformations);
608
+ var withoutTransformations = src.match(regexWithoutTransformations);
609
+ if (withTransformations) {
610
+ return withTransformations[withTransformations.length - 1];
611
+ } else if (withoutTransformations) {
612
+ return withoutTransformations[withoutTransformations.length - 1];
613
+ } else {
614
+ console.warn("Not possible to retrieve the publicUrl from " + src + ", make sure it's a valid cloudinary image url.");
615
+ }
616
+ }
617
+ return src;
618
+ }
619
+
620
+ /**
621
+ * createPlaceholderUrl
622
+ */
623
+
624
+ function createPlaceholderUrl(_ref4) {
625
+ var src = _ref4.src,
626
+ _ref4$placeholder = _ref4.placeholder,
627
+ placeholder = _ref4$placeholder === void 0 ? true : _ref4$placeholder,
628
+ config = _ref4.config;
629
+ var rawTransformations = [];
630
+ if (placeholder === 'grayscale') {
631
+ rawTransformations.push('e_grayscale');
632
+ }
633
+ if (typeof placeholder === 'string' && placeholder.includes('color:')) {
634
+ var color = placeholder.split(':').splice(1).join(':');
635
+ rawTransformations.push('e_grayscale');
636
+ rawTransformations.push("e_colorize:60,co_" + color);
637
+ }
638
+ return constructCloudinaryUrl({
639
+ options: {
640
+ src: src,
641
+ width: 100,
642
+ quality: 1,
643
+ rawTransformations: rawTransformations
644
+ },
645
+ config: config
646
+ });
647
+ }
648
+
649
+ function cloudinaryLoader(defaultOptions, cldOptions, cldConfig) {
650
+ if (cldConfig === void 0) {
651
+ cldConfig = {};
652
+ }
653
+ return constructCloudinaryUrl({
654
+ options: _extends({}, defaultOptions, cldOptions),
655
+ config: cldConfig
656
+ });
657
+ }
658
+
659
+ var CldImage = function CldImage(props) {
660
+ var CLD_OPTIONS = [];
661
+ transformationPlugins.forEach(function (_ref) {
662
+ var _ref$props = _ref.props,
663
+ props = _ref$props === void 0 ? [] : _ref$props;
664
+ props.forEach(function (prop) {
665
+ if (CLD_OPTIONS.includes(prop)) {
666
+ throw new Error("Option " + prop + " already exists!");
667
+ }
668
+ CLD_OPTIONS.push(prop);
669
+ });
670
+ });
671
+
672
+ // Construct the base Image component props by filtering out Cloudinary-specific props
673
+
674
+ var imageProps = {};
675
+ Object.keys(props).filter(function (key) {
676
+ return !CLD_OPTIONS.includes(key);
677
+ }).forEach(function (key) {
678
+ return imageProps[key] = props[key];
679
+ });
680
+
681
+ // Construct Cloudinary-specific props by looking for values for any of the supported prop keys
682
+
683
+ var cldOptions = {};
684
+ CLD_OPTIONS.forEach(function (key) {
685
+ if (props[key]) {
686
+ cldOptions[key] = props[key];
687
+ }
688
+ });
689
+
690
+ // If we see a placeholder option, configure a Cloudinary-based URL.
691
+ // The resulting image will always be blurred per Next.js, so we're
692
+ // limited on options for placeholders.
693
+ //
694
+ // We need to do this logic here as we potentially need to mutate
695
+ // an Image component prop as opposed to simply the URL
696
+ //
697
+ // https://nextjs.org/docs/api-reference/next/image#blurdataurl
698
+
699
+ if (imageProps.placeholder) {
700
+ var publicId = getPublicId(props.src);
701
+ imageProps.blurDataURL = createPlaceholderUrl({
702
+ src: publicId,
703
+ placeholder: props.placeholder
704
+ });
705
+ if (imageProps.placeholder !== 'blur') {
706
+ imageProps.placeholder = 'blur';
707
+ }
708
+ }
709
+ return /*#__PURE__*/jsxRuntime.jsx(Image__default["default"], _extends({}, imageProps, {
710
+ loader: function loader(options) {
711
+ return cloudinaryLoader(_extends({}, imageProps, {
712
+ options: options
713
+ }), cldOptions);
714
+ }
715
+ }));
716
+ };
717
+
718
+ var _excluded$1 = ["excludeTags", "twitterTitle"];
719
+ var IMAGE_WIDTH = 2400;
720
+ var IMAGE_HEIGHT = 1200;
721
+ var TWITTER_CARD = 'summary_large_image';
722
+ var CldOgImage = function CldOgImage(_ref) {
723
+ var _ref$excludeTags = _ref.excludeTags,
724
+ excludeTags = _ref$excludeTags === void 0 ? [] : _ref$excludeTags,
725
+ twitterTitle = _ref.twitterTitle,
726
+ props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
727
+ var options = _extends({}, props, {
728
+ width: props.width || IMAGE_WIDTH,
729
+ height: props.height || IMAGE_HEIGHT,
730
+ crop: props.crop || 'fill',
731
+ gravity: props.gravity || 'center'
732
+ });
733
+ var ogImageUrl = constructCloudinaryUrl({
734
+ options: options
735
+ });
736
+
737
+ // We need to include the tags within the Next.js Head component rather than
738
+ // direcly adding them inside of the Head otherwise we get unexpected results
739
+
740
+ return /*#__PURE__*/jsxRuntime.jsxs(Head__default["default"], {
741
+ children: [/*#__PURE__*/jsxRuntime.jsx("meta", {
742
+ property: "og:image",
743
+ content: ogImageUrl
744
+ }), /*#__PURE__*/jsxRuntime.jsx("meta", {
745
+ property: "og:image:secure_url",
746
+ content: ogImageUrl
747
+ }), /*#__PURE__*/jsxRuntime.jsx("meta", {
748
+ property: "og:image:width",
749
+ content: options.width
750
+ }), /*#__PURE__*/jsxRuntime.jsx("meta", {
751
+ property: "og:image:height",
752
+ content: options.height
753
+ }), options.alt && /*#__PURE__*/jsxRuntime.jsx("meta", {
754
+ property: "og:image:alt",
755
+ content: options.alt
756
+ }), !excludeTags.includes('twitter:title') && /*#__PURE__*/jsxRuntime.jsx("meta", {
757
+ property: "twitter:title",
758
+ content: twitterTitle || ' '
759
+ }), /*#__PURE__*/jsxRuntime.jsx("meta", {
760
+ property: "twitter:card",
761
+ content: TWITTER_CARD
762
+ }), /*#__PURE__*/jsxRuntime.jsx("meta", {
763
+ property: "twitter:image",
764
+ content: ogImageUrl
765
+ })]
766
+ });
767
+ };
768
+
769
+ var CldUploadWidget = function CldUploadWidget(_ref) {
770
+ var children = _ref.children,
771
+ onUpload = _ref.onUpload,
772
+ options = _ref.options,
773
+ signed = _ref.signed,
774
+ signatureEndpoint = _ref.signatureEndpoint;
775
+ var cloudinary = react.useRef();
776
+ var widget = react.useRef();
777
+
778
+ /**
779
+ * generateSignature
780
+ * @description Makes a request to an endpoint to sign Cloudinary parameters as part of widget creation
781
+ */
782
+
783
+ function generateSignature(callback, paramsToSign) {
784
+ fetch(signatureEndpoint, {
785
+ method: "POST",
786
+ body: JSON.stringify({
787
+ paramsToSign: paramsToSign
788
+ })
789
+ }).then(function (r) {
790
+ return r.json();
791
+ }).then(function (_ref2) {
792
+ var signature = _ref2.signature;
793
+ callback(signature);
794
+ });
795
+ }
796
+
797
+ /**
798
+ * createWidget
799
+ * @description Creates a new instance of the Cloudinary widget and stores in a ref
800
+ */
801
+
802
+ function createWidget() {
803
+ var _cloudinary$current;
804
+ // When creating a signed upload, you need to provide both your Cloudinary API Key
805
+ // as well as a signature generator function that will sign any paramters
806
+ // either on page load or during the upload process. Read more about signed uploads at:
807
+ // https://cloudinary.com/documentation/upload_widget#signed_uploads
808
+
809
+ var totalOptions = _extends({}, options, !!signed && {
810
+ uploadSignature: generateSignature
811
+ });
812
+
813
+ // no need for apiSecret because of api/sign-cloudinary-params
814
+ if (signed && !totalOptions.apiKey) {
815
+ return new Error("Signed Upload needs apiKey!");
816
+ }
817
+ return (_cloudinary$current = cloudinary.current) == null ? void 0 : _cloudinary$current.createUploadWidget(totalOptions, function (error, result) {
818
+ // The callback is a bit more chatty than failed or success so
819
+ // only trigger when one of those are the case. You can additionally
820
+ // create a separate handler such as onEvent and trigger it on
821
+ // ever occurance
822
+ if (error || result.event === "success") {
823
+ onUpload(error, result, widget == null ? void 0 : widget.current);
824
+ }
825
+ });
826
+ }
827
+
828
+ /**
829
+ * open
830
+ * @description When triggered, uses the current widget instance to open the upload modal
831
+ */
832
+
833
+ function open() {
834
+ if (!(widget != null && widget.current)) {
835
+ widget.current = createWidget();
836
+ }
837
+ (widget == null ? void 0 : widget.current) && widget.current.open();
838
+ }
839
+
840
+ /**
841
+ * handleOnLoad
842
+ * @description Stores the Cloudinary window instance to a ref when the widget script loads
843
+ */
844
+
845
+ function handleOnLoad() {
846
+ cloudinary.current = window.cloudinary;
847
+ }
848
+ return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
849
+ children: [children({
850
+ cloudinary: cloudinary.current,
851
+ widget: widget.current,
852
+ open: open
853
+ }), /*#__PURE__*/jsxRuntime.jsx(Script__default["default"], {
854
+ id: "cloudinary-" + Math.floor(Math.random() * 100),
855
+ src: "https://widget.cloudinary.com/v2.0/global/all.js",
856
+ onLoad: handleOnLoad,
857
+ onError: function onError(e) {
858
+ console.error("Script failed to load", e);
859
+ }
860
+ })]
861
+ });
862
+ };
863
+
864
+ var _excluded = ["onUpload", "options", "signed", "label", "children"];
865
+ var CldUploadButton = function CldUploadButton(_ref) {
866
+ var onUpload = _ref.onUpload,
867
+ options = _ref.options,
868
+ signed = _ref.signed,
869
+ _children = _ref.children,
870
+ props = _objectWithoutPropertiesLoose(_ref, _excluded);
871
+ return /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, {
872
+ children: /*#__PURE__*/jsxRuntime.jsx(CldUploadWidget, {
873
+ signed: signed,
874
+ options: options,
875
+ onUpload: onUpload,
876
+ signatureEndpoint: signed != null ? signed : props.signatureEndpoint,
877
+ children: function children(_ref2) {
878
+ var open = _ref2.open;
879
+ function handleOnClick(e) {
880
+ e.preventDefault();
881
+ open();
882
+ }
883
+ return /*#__PURE__*/jsxRuntime.jsx("button", _extends({
884
+ onClick: handleOnClick
885
+ }, props, {
886
+ children: _children
887
+ }));
888
+ }
889
+ })
890
+ });
891
+ };
892
+
893
+ exports.CldImage = CldImage;
894
+ exports.CldOgImage = CldOgImage;
895
+ exports.CldUploadButton = CldUploadButton;
896
+ exports.CldUploadWidget = CldUploadWidget;
897
+ exports.cloudinaryLoader = cloudinaryLoader;
898
+ exports.position = position;
899
+ exports.primary = primary;
900
+ exports.text = text;
901
+ //# sourceMappingURL=next-cloudinary.js.map