next-cloudinary 0.1.1 → 0.3.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.
- package/README.md +23 -3
- package/dist/next-cloudinary.js +384 -130
- package/dist/next-cloudinary.js.map +1 -1
- package/dist/next-cloudinary.modern.mjs +371 -121
- package/dist/next-cloudinary.modern.mjs.map +1 -1
- package/dist/next-cloudinary.module.js +381 -132
- package/dist/next-cloudinary.module.js.map +1 -1
- package/dist/next-cloudinary.umd.js +385 -134
- package/dist/next-cloudinary.umd.js.map +1 -1
- package/package.json +1 -1
- package/tests/lib/cloudinary.spec.js +110 -9
- package/tests/loaders/cloudinary-loader.spec.js +23 -7
- package/tests/plugins/cropping.spec.js +27 -0
- package/tests/plugins/overlays.spec.js +112 -0
- package/tests/plugins/raw-transformations.spec.js +6 -6
- package/tests/plugins/underlays.spec.js +62 -0
package/dist/next-cloudinary.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
var Image = require('next/image');
|
|
2
2
|
var urlGen = require('@cloudinary/url-gen');
|
|
3
3
|
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var Head = require('next/head');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var Script = require('next/script');
|
|
4
7
|
|
|
5
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
6
9
|
|
|
7
10
|
var Image__default = /*#__PURE__*/_interopDefaultLegacy(Image);
|
|
11
|
+
var Head__default = /*#__PURE__*/_interopDefaultLegacy(Head);
|
|
12
|
+
var Script__default = /*#__PURE__*/_interopDefaultLegacy(Script);
|
|
8
13
|
|
|
9
14
|
function _extends() {
|
|
10
15
|
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
@@ -38,96 +43,32 @@ function _objectWithoutPropertiesLoose(source, excluded) {
|
|
|
38
43
|
return target;
|
|
39
44
|
}
|
|
40
45
|
|
|
41
|
-
function _readOnlyError(name) {
|
|
42
|
-
throw new TypeError("\"" + name + "\" is read-only");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
var cld$1 = new urlGen.Cloudinary({
|
|
46
|
-
cloud: {
|
|
47
|
-
cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
|
|
48
|
-
},
|
|
49
|
-
url: {
|
|
50
|
-
// Used to avoid issues with SSR particularly for the blurred placeholder
|
|
51
|
-
analytics: false
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
/**
|
|
55
|
-
* Retrieves the public id of a cloudiary image url. If no url is recognized it returns the parameter it self.
|
|
56
|
-
* If it's recognized that is a url and it's not possible to get the public id, it warns the user.
|
|
57
|
-
*
|
|
58
|
-
* @param {string} src The cloudiary url or public id.
|
|
59
|
-
*
|
|
60
|
-
* @return {string} The images public id
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
function getPublicId(src) {
|
|
64
|
-
if (src.includes('res.cloudinary.com')) {
|
|
65
|
-
var regexWithTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(.*)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
|
|
66
|
-
var regexWithoutTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
|
|
67
|
-
var withTransformations = src.match(regexWithTransformations);
|
|
68
|
-
var withoutTransformations = src.match(regexWithoutTransformations);
|
|
69
|
-
|
|
70
|
-
if (withTransformations) {
|
|
71
|
-
return withTransformations[withTransformations.length - 1];
|
|
72
|
-
} else if (withoutTransformations) {
|
|
73
|
-
return withoutTransformations[withoutTransformations.length - 1];
|
|
74
|
-
} else {
|
|
75
|
-
console.warn("Not possible to retrieve the publicUrl from " + src + ", make sure it's a valid cloudinary image url.");
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return src;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* createPlaceholderUrl
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
function createPlaceholderUrl(_ref) {
|
|
86
|
-
var src = _ref.src,
|
|
87
|
-
placeholder = _ref.placeholder;
|
|
88
|
-
var cldImage = cld$1.image(src).resize('c_limit,w_100').delivery('q_1').format('auto');
|
|
89
|
-
|
|
90
|
-
if (placeholder === 'grayscale') {
|
|
91
|
-
cldImage.effect('e_grayscale');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (placeholder.includes('color:')) {
|
|
95
|
-
var color = placeholder.split(':').splice(1).join(':');
|
|
96
|
-
cldImage.effect('e_grayscale');
|
|
97
|
-
cldImage.effect("e_colorize:60,co_" + color);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return cldImage.toURL();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
46
|
var cropsGravityAuto = ['crop', 'fill', 'lfill', 'fill_pad', 'thumb'];
|
|
104
47
|
var props$6 = ['crop', 'gravity'];
|
|
105
48
|
function plugin$6(_temp) {
|
|
106
49
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
107
50
|
cldImage = _ref.cldImage,
|
|
108
|
-
options = _ref.options
|
|
109
|
-
cldOptions = _ref.cldOptions;
|
|
51
|
+
options = _ref.options;
|
|
110
52
|
|
|
111
53
|
var width = options.width,
|
|
112
|
-
height = options.height
|
|
113
|
-
|
|
114
|
-
crop =
|
|
115
|
-
gravity = cldOptions.gravity;
|
|
54
|
+
height = options.height,
|
|
55
|
+
_options$crop = options.crop,
|
|
56
|
+
crop = _options$crop === void 0 ? 'limit' : _options$crop;
|
|
116
57
|
var transformationString = "c_" + crop + ",w_" + width;
|
|
117
58
|
|
|
118
|
-
if (!gravity && cropsGravityAuto.includes(crop)) {
|
|
119
|
-
|
|
59
|
+
if (!options.gravity && cropsGravityAuto.includes(crop)) {
|
|
60
|
+
options.gravity = 'auto';
|
|
120
61
|
}
|
|
121
62
|
|
|
122
63
|
if (!['limit'].includes(crop)) {
|
|
123
64
|
transformationString = transformationString + ",h_" + height;
|
|
124
65
|
}
|
|
125
66
|
|
|
126
|
-
if (gravity) {
|
|
127
|
-
if (gravity === 'auto' && !cropsGravityAuto.includes(crop)) {
|
|
67
|
+
if (options.gravity) {
|
|
68
|
+
if (options.gravity === 'auto' && !cropsGravityAuto.includes(crop)) {
|
|
128
69
|
console.warn('Auto gravity can only be used with crop, fill, lfill, fill_pad or thumb. Not applying gravity.');
|
|
129
70
|
} else {
|
|
130
|
-
transformationString = transformationString + ",g_" + gravity;
|
|
71
|
+
transformationString = transformationString + ",g_" + options.gravity;
|
|
131
72
|
}
|
|
132
73
|
}
|
|
133
74
|
|
|
@@ -192,20 +133,16 @@ var props$5 = params.map(function (param) {
|
|
|
192
133
|
function plugin$5(_temp) {
|
|
193
134
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
194
135
|
cldImage = _ref.cldImage,
|
|
195
|
-
|
|
136
|
+
options = _ref.options;
|
|
196
137
|
|
|
197
138
|
params.forEach(function (key) {
|
|
198
139
|
var prop = key.prop || key;
|
|
199
140
|
var effect = key.effect || key;
|
|
200
141
|
|
|
201
|
-
if (prop ===
|
|
202
|
-
console.log('cldOptions[prop]', cldOptions[prop]);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (cldOptions[prop] === true) {
|
|
142
|
+
if (options[prop] === true) {
|
|
206
143
|
cldImage.effect("e_" + effect);
|
|
207
|
-
} else if (typeof
|
|
208
|
-
cldImage.effect("e_" + effect + ":" +
|
|
144
|
+
} else if (typeof options[prop] === 'string') {
|
|
145
|
+
cldImage.effect("e_" + effect + ":" + options[prop]);
|
|
209
146
|
}
|
|
210
147
|
});
|
|
211
148
|
}
|
|
@@ -270,27 +207,55 @@ var text = {
|
|
|
270
207
|
}
|
|
271
208
|
};
|
|
272
209
|
|
|
273
|
-
var _excluded$
|
|
274
|
-
var props$4 = ['overlays'];
|
|
210
|
+
var _excluded$3 = ["publicId", "position", "text", "effects"];
|
|
211
|
+
var props$4 = ['text', 'overlays'];
|
|
212
|
+
var DEFAULT_TEXT_OPTIONS = {
|
|
213
|
+
color: 'black',
|
|
214
|
+
fontFamily: 'Arial',
|
|
215
|
+
fontSize: 200,
|
|
216
|
+
fontWeight: 'bold'
|
|
217
|
+
};
|
|
275
218
|
function plugin$4(_temp) {
|
|
276
219
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
277
220
|
cldImage = _ref.cldImage,
|
|
278
|
-
|
|
221
|
+
options = _ref.options;
|
|
279
222
|
|
|
280
|
-
var
|
|
281
|
-
overlays =
|
|
223
|
+
var text$1 = options.text,
|
|
224
|
+
_options$overlays = options.overlays,
|
|
225
|
+
overlays = _options$overlays === void 0 ? [] : _options$overlays;
|
|
282
226
|
var type = 'overlay';
|
|
283
227
|
var typeQualifier = 'l';
|
|
284
|
-
|
|
228
|
+
|
|
229
|
+
if (Array.isArray(overlays)) {
|
|
230
|
+
overlays.forEach(applyOverlay);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (typeof text$1 === 'string') {
|
|
234
|
+
applyOverlay({
|
|
235
|
+
text: _extends({}, DEFAULT_TEXT_OPTIONS, {
|
|
236
|
+
text: text$1
|
|
237
|
+
})
|
|
238
|
+
});
|
|
239
|
+
} else if (typeof text$1 === 'object') {
|
|
240
|
+
applyOverlay({
|
|
241
|
+
text: _extends({}, DEFAULT_TEXT_OPTIONS, text$1)
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* applyOverlay
|
|
246
|
+
*/
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
function applyOverlay(_ref2) {
|
|
285
250
|
var publicId = _ref2.publicId,
|
|
286
251
|
position$1 = _ref2.position,
|
|
287
252
|
text$1 = _ref2.text,
|
|
288
253
|
_ref2$effects = _ref2.effects,
|
|
289
254
|
layerEffects = _ref2$effects === void 0 ? [] : _ref2$effects,
|
|
290
|
-
options = _objectWithoutPropertiesLoose(_ref2, _excluded$
|
|
255
|
+
options = _objectWithoutPropertiesLoose(_ref2, _excluded$3);
|
|
291
256
|
|
|
292
257
|
var hasPublicId = typeof publicId === 'string';
|
|
293
|
-
var hasText = typeof text$1 === 'object';
|
|
258
|
+
var hasText = typeof text$1 === 'object' || typeof text$1 === 'string';
|
|
294
259
|
var hasPosition = typeof position$1 === 'object';
|
|
295
260
|
|
|
296
261
|
if (!hasPublicId && !hasText) {
|
|
@@ -328,6 +293,12 @@ function plugin$4(_temp) {
|
|
|
328
293
|
}); // Text styling
|
|
329
294
|
|
|
330
295
|
if (hasText) {
|
|
296
|
+
if (typeof text$1 === 'string') {
|
|
297
|
+
text$1 = _extends({}, DEFAULT_TEXT_OPTIONS, {
|
|
298
|
+
text: text$1
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
331
302
|
var textTransformations = [];
|
|
332
303
|
Object.keys(text$1).forEach(function (key) {
|
|
333
304
|
if (!text[key]) return;
|
|
@@ -356,7 +327,7 @@ function plugin$4(_temp) {
|
|
|
356
327
|
|
|
357
328
|
layerTransformation = layerTransformation + "," + primary$1.join(','); // Add all applied transformations
|
|
358
329
|
|
|
359
|
-
layerTransformation = layerTransformation + "/fl_layer_apply";
|
|
330
|
+
layerTransformation = layerTransformation + "/fl_layer_apply,fl_no_overflow";
|
|
360
331
|
|
|
361
332
|
if (applied.length > 0) {
|
|
362
333
|
layerTransformation = layerTransformation + "," + applied.join(',');
|
|
@@ -364,12 +335,13 @@ function plugin$4(_temp) {
|
|
|
364
335
|
|
|
365
336
|
|
|
366
337
|
cldImage.addTransformation(layerTransformation);
|
|
367
|
-
}
|
|
338
|
+
}
|
|
368
339
|
}
|
|
369
340
|
|
|
370
341
|
var overlaysPlugin = {
|
|
371
342
|
__proto__: null,
|
|
372
343
|
props: props$4,
|
|
344
|
+
DEFAULT_TEXT_OPTIONS: DEFAULT_TEXT_OPTIONS,
|
|
373
345
|
plugin: plugin$4
|
|
374
346
|
};
|
|
375
347
|
|
|
@@ -377,10 +349,10 @@ var props$3 = ['rawTransformations'];
|
|
|
377
349
|
function plugin$3(_temp) {
|
|
378
350
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
379
351
|
cldImage = _ref.cldImage,
|
|
380
|
-
|
|
352
|
+
options = _ref.options;
|
|
381
353
|
|
|
382
|
-
var
|
|
383
|
-
rawTransformations =
|
|
354
|
+
var _options$rawTransform = options.rawTransformations,
|
|
355
|
+
rawTransformations = _options$rawTransform === void 0 ? [] : _options$rawTransform;
|
|
384
356
|
rawTransformations.forEach(function (transformation) {
|
|
385
357
|
cldImage.addTransformation(transformation);
|
|
386
358
|
});
|
|
@@ -396,10 +368,10 @@ var props$2 = ['removeBackground'];
|
|
|
396
368
|
function plugin$2(_temp) {
|
|
397
369
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
398
370
|
cldImage = _ref.cldImage,
|
|
399
|
-
|
|
371
|
+
options = _ref.options;
|
|
400
372
|
|
|
401
|
-
var
|
|
402
|
-
removeBackground =
|
|
373
|
+
var _options$removeBackgr = options.removeBackground,
|
|
374
|
+
removeBackground = _options$removeBackgr === void 0 ? false : _options$removeBackgr;
|
|
403
375
|
|
|
404
376
|
if (removeBackground) {
|
|
405
377
|
cldImage.effect('e_background_removal');
|
|
@@ -412,23 +384,50 @@ var removeBackgroundPlugin = {
|
|
|
412
384
|
plugin: plugin$2
|
|
413
385
|
};
|
|
414
386
|
|
|
415
|
-
var _excluded = ["publicId", "type", "position", "text", "effects"];
|
|
416
|
-
var props$1 = ['underlays'];
|
|
387
|
+
var _excluded$2 = ["publicId", "type", "position", "text", "effects"];
|
|
388
|
+
var props$1 = ['underlay', 'underlays'];
|
|
417
389
|
function plugin$1(_temp) {
|
|
418
390
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
419
391
|
cldImage = _ref.cldImage,
|
|
420
|
-
|
|
392
|
+
options = _ref.options;
|
|
421
393
|
|
|
422
|
-
var
|
|
423
|
-
underlays =
|
|
394
|
+
var underlay = options.underlay,
|
|
395
|
+
_options$underlays = options.underlays,
|
|
396
|
+
underlays = _options$underlays === void 0 ? [] : _options$underlays;
|
|
424
397
|
var typeQualifier = 'u';
|
|
425
|
-
|
|
398
|
+
|
|
399
|
+
if (Array.isArray(underlays)) {
|
|
400
|
+
underlays.forEach(applyUnderlay);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (typeof underlay === 'string') {
|
|
404
|
+
var underlayOptions = {
|
|
405
|
+
publicId: underlay,
|
|
406
|
+
crop: 'fill'
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
if (options.width) {
|
|
410
|
+
underlayOptions.width = options.width;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (options.height) {
|
|
414
|
+
underlayOptions.height = options.height;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
applyUnderlay(underlayOptions);
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* applyUnderlay
|
|
421
|
+
*/
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
function applyUnderlay(_ref2) {
|
|
426
425
|
var publicId = _ref2.publicId,
|
|
427
426
|
type = _ref2.type,
|
|
428
427
|
position$1 = _ref2.position,
|
|
429
428
|
_ref2$effects = _ref2.effects,
|
|
430
429
|
layerEffects = _ref2$effects === void 0 ? [] : _ref2$effects,
|
|
431
|
-
options = _objectWithoutPropertiesLoose(_ref2, _excluded);
|
|
430
|
+
options = _objectWithoutPropertiesLoose(_ref2, _excluded$2);
|
|
432
431
|
|
|
433
432
|
var hasPublicId = typeof publicId === 'string';
|
|
434
433
|
var hasPosition = typeof position$1 === 'object';
|
|
@@ -471,7 +470,7 @@ function plugin$1(_temp) {
|
|
|
471
470
|
|
|
472
471
|
layerTransformation = layerTransformation + "," + primary$1.join(','); // Add all applied transformations
|
|
473
472
|
|
|
474
|
-
layerTransformation = layerTransformation + "/fl_layer_apply";
|
|
473
|
+
layerTransformation = layerTransformation + "/fl_layer_apply,fl_no_overflow";
|
|
475
474
|
|
|
476
475
|
if (applied.length > 0) {
|
|
477
476
|
layerTransformation = layerTransformation + "," + applied.join(',');
|
|
@@ -479,7 +478,7 @@ function plugin$1(_temp) {
|
|
|
479
478
|
|
|
480
479
|
|
|
481
480
|
cldImage.addTransformation(layerTransformation);
|
|
482
|
-
}
|
|
481
|
+
}
|
|
483
482
|
}
|
|
484
483
|
|
|
485
484
|
var underlaysPlugin = {
|
|
@@ -492,11 +491,11 @@ var props = ['zoompan'];
|
|
|
492
491
|
function plugin(_temp) {
|
|
493
492
|
var _ref = _temp === void 0 ? {} : _temp,
|
|
494
493
|
cldImage = _ref.cldImage,
|
|
495
|
-
|
|
494
|
+
options = _ref.options;
|
|
496
495
|
|
|
497
|
-
var
|
|
498
|
-
zoompan =
|
|
499
|
-
var
|
|
496
|
+
var _options$zoompan = options.zoompan,
|
|
497
|
+
zoompan = _options$zoompan === void 0 ? false : _options$zoompan;
|
|
498
|
+
var overrides = {};
|
|
500
499
|
|
|
501
500
|
if (zoompan === true) {
|
|
502
501
|
cldImage.effect('e_zoompan');
|
|
@@ -529,11 +528,11 @@ function plugin(_temp) {
|
|
|
529
528
|
}
|
|
530
529
|
|
|
531
530
|
if (zoompan !== false) {
|
|
532
|
-
|
|
531
|
+
overrides.format = 'gif';
|
|
533
532
|
}
|
|
534
533
|
|
|
535
534
|
return {
|
|
536
|
-
options:
|
|
535
|
+
options: overrides
|
|
537
536
|
};
|
|
538
537
|
}
|
|
539
538
|
|
|
@@ -543,15 +542,18 @@ var zoompanPlugin = {
|
|
|
543
542
|
plugin: plugin
|
|
544
543
|
};
|
|
545
544
|
|
|
546
|
-
var cld;
|
|
547
545
|
var transformationPlugins = [// Background Removal must always come first
|
|
548
546
|
removeBackgroundPlugin, croppingPlugin, effectsPlugin, overlaysPlugin, underlaysPlugin, zoompanPlugin, // Raw transformations needs to be last simply to make sure
|
|
549
547
|
// it's always expected to applied the same way
|
|
550
548
|
rawTransformationsPlugin];
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
549
|
+
var cld;
|
|
550
|
+
/**
|
|
551
|
+
* constructCloudinaryUrl
|
|
552
|
+
*/
|
|
553
|
+
|
|
554
|
+
function constructCloudinaryUrl(_ref) {
|
|
555
|
+
var options = _ref.options,
|
|
556
|
+
config = _ref.config;
|
|
555
557
|
|
|
556
558
|
if (!cld) {
|
|
557
559
|
cld = new urlGen.Cloudinary(_extends({
|
|
@@ -562,31 +564,98 @@ function cloudinaryLoader(defaultOptions, cldOptions, cldConfig) {
|
|
|
562
564
|
// Used to avoid issues with SSR particularly for the blurred placeholder
|
|
563
565
|
analytics: false
|
|
564
566
|
}
|
|
565
|
-
},
|
|
567
|
+
}, config));
|
|
566
568
|
}
|
|
567
569
|
|
|
568
|
-
var options = _extends({
|
|
569
|
-
format: 'auto',
|
|
570
|
-
quality: 'auto'
|
|
571
|
-
}, defaultOptions);
|
|
572
|
-
|
|
573
570
|
var publicId = getPublicId(options.src);
|
|
574
571
|
var cldImage = cld.image(publicId);
|
|
575
|
-
transformationPlugins.forEach(function (
|
|
576
|
-
var plugin =
|
|
572
|
+
transformationPlugins.forEach(function (_ref2) {
|
|
573
|
+
var plugin = _ref2.plugin;
|
|
577
574
|
|
|
578
|
-
var
|
|
575
|
+
var _ref3 = plugin({
|
|
579
576
|
cldImage: cldImage,
|
|
580
|
-
options: options
|
|
581
|
-
cldOptions: cldOptions
|
|
577
|
+
options: options
|
|
582
578
|
}) || {},
|
|
583
|
-
pluginOptions =
|
|
579
|
+
pluginOptions = _ref3.options;
|
|
584
580
|
|
|
585
581
|
if (pluginOptions != null && pluginOptions.format) {
|
|
586
582
|
options.format = pluginOptions.format;
|
|
587
583
|
}
|
|
588
584
|
});
|
|
589
|
-
return cldImage.format(options.format).delivery("q_" + options.quality).toURL();
|
|
585
|
+
return cldImage.setDeliveryType(options.deliveryType || 'upload').format(options.format || 'auto').delivery("q_" + (options.quality || 'auto')).toURL();
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Retrieves the public id of a cloudiary image url. If no url is recognized it returns the parameter it self.
|
|
589
|
+
* If it's recognized that is a url and it's not possible to get the public id, it warns the user.
|
|
590
|
+
*
|
|
591
|
+
* @param {string} src The cloudiary url or public id.
|
|
592
|
+
*
|
|
593
|
+
* @return {string} The images public id
|
|
594
|
+
*/
|
|
595
|
+
|
|
596
|
+
function getPublicId(src) {
|
|
597
|
+
if (typeof src !== 'string') {
|
|
598
|
+
throw new Error("Invalid src of type " + typeof src);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (src.includes('res.cloudinary.com')) {
|
|
602
|
+
var regexWithTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(.*)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
|
|
603
|
+
var regexWithoutTransformations = /(https?)\:\/\/(res.cloudinary.com)\/([^\/]+)\/(image|video|raw)\/(upload|authenticated)\/(v[0-9]+)\/(.+)(?:\.[a-z]{3})?/;
|
|
604
|
+
var withTransformations = src.match(regexWithTransformations);
|
|
605
|
+
var withoutTransformations = src.match(regexWithoutTransformations);
|
|
606
|
+
|
|
607
|
+
if (withTransformations) {
|
|
608
|
+
return withTransformations[withTransformations.length - 1];
|
|
609
|
+
} else if (withoutTransformations) {
|
|
610
|
+
return withoutTransformations[withoutTransformations.length - 1];
|
|
611
|
+
} else {
|
|
612
|
+
console.warn("Not possible to retrieve the publicUrl from " + src + ", make sure it's a valid cloudinary image url.");
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return src;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* createPlaceholderUrl
|
|
620
|
+
*/
|
|
621
|
+
|
|
622
|
+
function createPlaceholderUrl(_ref4) {
|
|
623
|
+
var src = _ref4.src,
|
|
624
|
+
_ref4$placeholder = _ref4.placeholder,
|
|
625
|
+
placeholder = _ref4$placeholder === void 0 ? true : _ref4$placeholder,
|
|
626
|
+
config = _ref4.config;
|
|
627
|
+
var rawTransformations = [];
|
|
628
|
+
|
|
629
|
+
if (placeholder === 'grayscale') {
|
|
630
|
+
rawTransformations.push('e_grayscale');
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
if (typeof placeholder === 'string' && placeholder.includes('color:')) {
|
|
634
|
+
var color = placeholder.split(':').splice(1).join(':');
|
|
635
|
+
rawTransformations.push('e_grayscale');
|
|
636
|
+
rawTransformations.push("e_colorize:60,co_" + color);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
return constructCloudinaryUrl({
|
|
640
|
+
options: {
|
|
641
|
+
src: src,
|
|
642
|
+
width: 100,
|
|
643
|
+
quality: 1,
|
|
644
|
+
rawTransformations: rawTransformations
|
|
645
|
+
},
|
|
646
|
+
config: config
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function cloudinaryLoader(defaultOptions, cldOptions, cldConfig) {
|
|
651
|
+
if (cldConfig === void 0) {
|
|
652
|
+
cldConfig = {};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
return constructCloudinaryUrl({
|
|
656
|
+
options: _extends({}, defaultOptions, cldOptions),
|
|
657
|
+
config: cldConfig
|
|
658
|
+
});
|
|
590
659
|
}
|
|
591
660
|
|
|
592
661
|
var CldImage = function CldImage(props) {
|
|
@@ -645,7 +714,192 @@ var CldImage = function CldImage(props) {
|
|
|
645
714
|
}));
|
|
646
715
|
};
|
|
647
716
|
|
|
717
|
+
var _excluded$1 = ["excludeTags", "twitterTitle"];
|
|
718
|
+
var IMAGE_WIDTH = 2400;
|
|
719
|
+
var IMAGE_HEIGHT = 1200;
|
|
720
|
+
var TWITTER_CARD = 'summary_large_image';
|
|
721
|
+
|
|
722
|
+
var CldOgImage = function CldOgImage(_ref) {
|
|
723
|
+
var _ref$excludeTags = _ref.excludeTags,
|
|
724
|
+
excludeTags = _ref$excludeTags === void 0 ? [] : _ref$excludeTags,
|
|
725
|
+
twitterTitle = _ref.twitterTitle,
|
|
726
|
+
props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
727
|
+
|
|
728
|
+
var options = _extends({}, props, {
|
|
729
|
+
width: props.width || IMAGE_WIDTH,
|
|
730
|
+
height: props.height || IMAGE_HEIGHT,
|
|
731
|
+
crop: props.crop || 'fill',
|
|
732
|
+
gravity: props.gravity || 'center'
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
var ogImageUrl = constructCloudinaryUrl({
|
|
736
|
+
options: options
|
|
737
|
+
}); // We need to include the tags within the Next.js Head component rather than
|
|
738
|
+
// direcly adding them inside of the Head otherwise we get unexpected results
|
|
739
|
+
|
|
740
|
+
return /*#__PURE__*/jsxRuntime.jsxs(Head__default["default"], {
|
|
741
|
+
children: [/*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
742
|
+
property: "og:image",
|
|
743
|
+
content: ogImageUrl
|
|
744
|
+
}), /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
745
|
+
property: "og:image:secure_url",
|
|
746
|
+
content: ogImageUrl
|
|
747
|
+
}), /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
748
|
+
property: "og:image:width",
|
|
749
|
+
content: options.width
|
|
750
|
+
}), /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
751
|
+
property: "og:image:height",
|
|
752
|
+
content: options.height
|
|
753
|
+
}), options.alt && /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
754
|
+
property: "og:image:alt",
|
|
755
|
+
content: options.alt
|
|
756
|
+
}), !excludeTags.includes('twitter:title') && /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
757
|
+
property: "twitter:title",
|
|
758
|
+
content: twitterTitle || ' '
|
|
759
|
+
}), /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
760
|
+
property: "twitter:card",
|
|
761
|
+
content: TWITTER_CARD
|
|
762
|
+
}), /*#__PURE__*/jsxRuntime.jsx("meta", {
|
|
763
|
+
property: "twitter:image",
|
|
764
|
+
content: ogImageUrl
|
|
765
|
+
})]
|
|
766
|
+
});
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
var CldUploadWidget = function CldUploadWidget(_ref) {
|
|
770
|
+
var children = _ref.children,
|
|
771
|
+
onUpload = _ref.onUpload,
|
|
772
|
+
options = _ref.options,
|
|
773
|
+
signed = _ref.signed,
|
|
774
|
+
signatureEndpoint = _ref.signatureEndpoint;
|
|
775
|
+
var cloudinary = react.useRef();
|
|
776
|
+
var widget = react.useRef();
|
|
777
|
+
/**
|
|
778
|
+
* generateSignature
|
|
779
|
+
* @description Makes a request to an endpoint to sign Cloudinary parameters as part of widget creation
|
|
780
|
+
*/
|
|
781
|
+
|
|
782
|
+
function generateSignature(callback, paramsToSign) {
|
|
783
|
+
fetch(signatureEndpoint, {
|
|
784
|
+
method: "POST",
|
|
785
|
+
body: JSON.stringify({
|
|
786
|
+
paramsToSign: paramsToSign
|
|
787
|
+
})
|
|
788
|
+
}).then(function (r) {
|
|
789
|
+
return r.json();
|
|
790
|
+
}).then(function (_ref2) {
|
|
791
|
+
var signature = _ref2.signature;
|
|
792
|
+
callback(signature);
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* createWidget
|
|
797
|
+
* @description Creates a new instance of the Cloudinary widget and stores in a ref
|
|
798
|
+
*/
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
function createWidget() {
|
|
802
|
+
var _cloudinary$current;
|
|
803
|
+
|
|
804
|
+
// When creating a signed upload, you need to provide both your Cloudinary API Key
|
|
805
|
+
// as well as a signature generator function that will sign any paramters
|
|
806
|
+
// either on page load or during the upload process. Read more about signed uploads at:
|
|
807
|
+
// https://cloudinary.com/documentation/upload_widget#signed_uploads
|
|
808
|
+
var totalOptions = _extends({}, options, !!signed && {
|
|
809
|
+
uploadSignature: generateSignature
|
|
810
|
+
}); // no need for apiSecret because of api/sign-cloudinary-params
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
if (signed && !totalOptions.apiKey) {
|
|
814
|
+
return new Error("Signed Upload needs apiKey!");
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
return (_cloudinary$current = cloudinary.current) == null ? void 0 : _cloudinary$current.createUploadWidget(totalOptions, function (error, result) {
|
|
818
|
+
// The callback is a bit more chatty than failed or success so
|
|
819
|
+
// only trigger when one of those are the case. You can additionally
|
|
820
|
+
// create a separate handler such as onEvent and trigger it on
|
|
821
|
+
// ever occurance
|
|
822
|
+
if (error || result.event === "success") {
|
|
823
|
+
onUpload(error, result, widget == null ? void 0 : widget.current);
|
|
824
|
+
}
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* open
|
|
829
|
+
* @description When triggered, uses the current widget instance to open the upload modal
|
|
830
|
+
*/
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
function open() {
|
|
834
|
+
if (!(widget != null && widget.current)) {
|
|
835
|
+
widget.current = createWidget();
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
(widget == null ? void 0 : widget.current) && widget.current.open();
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* handleOnLoad
|
|
842
|
+
* @description Stores the Cloudinary window instance to a ref when the widget script loads
|
|
843
|
+
*/
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
function handleOnLoad() {
|
|
847
|
+
cloudinary.current = window.cloudinary;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, {
|
|
851
|
+
children: [children({
|
|
852
|
+
cloudinary: cloudinary.current,
|
|
853
|
+
widget: widget.current,
|
|
854
|
+
open: open
|
|
855
|
+
}), /*#__PURE__*/jsxRuntime.jsx(Script__default["default"], {
|
|
856
|
+
id: "cloudinary-" + Math.floor(Math.random() * 100),
|
|
857
|
+
src: "https://widget.cloudinary.com/v2.0/global/all.js",
|
|
858
|
+
onLoad: handleOnLoad,
|
|
859
|
+
onError: function onError(e) {
|
|
860
|
+
console.error("Script failed to load", e);
|
|
861
|
+
}
|
|
862
|
+
})]
|
|
863
|
+
});
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
var _excluded = ["onUpload", "options", "signed", "label", "children"];
|
|
867
|
+
|
|
868
|
+
var CldUploadButton = function CldUploadButton(_ref) {
|
|
869
|
+
var onUpload = _ref.onUpload,
|
|
870
|
+
options = _ref.options,
|
|
871
|
+
signed = _ref.signed,
|
|
872
|
+
_children = _ref.children,
|
|
873
|
+
props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
874
|
+
|
|
875
|
+
return /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, {
|
|
876
|
+
children: /*#__PURE__*/jsxRuntime.jsx(CldUploadWidget, {
|
|
877
|
+
signed: signed,
|
|
878
|
+
options: options,
|
|
879
|
+
onUpload: onUpload,
|
|
880
|
+
signatureEndpoint: signed != null ? signed : props.signatureEndpoint,
|
|
881
|
+
children: function children(_ref2) {
|
|
882
|
+
var open = _ref2.open;
|
|
883
|
+
|
|
884
|
+
function handleOnClick(e) {
|
|
885
|
+
e.preventDefault();
|
|
886
|
+
open();
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
return /*#__PURE__*/jsxRuntime.jsx("button", _extends({
|
|
890
|
+
onClick: handleOnClick
|
|
891
|
+
}, props, {
|
|
892
|
+
children: _children
|
|
893
|
+
}));
|
|
894
|
+
}
|
|
895
|
+
})
|
|
896
|
+
});
|
|
897
|
+
};
|
|
898
|
+
|
|
648
899
|
exports.CldImage = CldImage;
|
|
900
|
+
exports.CldOgImage = CldOgImage;
|
|
901
|
+
exports.CldUploadButton = CldUploadButton;
|
|
902
|
+
exports.CldUploadWidget = CldUploadWidget;
|
|
649
903
|
exports.cloudinaryLoader = cloudinaryLoader;
|
|
650
904
|
exports.position = position;
|
|
651
905
|
exports.primary = primary;
|