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