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