next-cloudinary 0.4.0 → 0.4.1

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