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