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