next-cloudinary 1.3.0 → 1.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.
@@ -1,8 +1,8 @@
1
+ import { useState, useRef } from 'react';
1
2
  import Image from 'next/image';
2
3
  import { Cloudinary } from '@cloudinary/url-gen';
3
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
5
  import Head from 'next/head';
5
- import { useRef } from 'react';
6
6
  import Script from 'next/script';
7
7
 
8
8
  function _extends() {
@@ -86,89 +86,6 @@ var croppingPlugin = {
86
86
  plugin: plugin$7
87
87
  };
88
88
 
89
- var params = ['art', {
90
- prop: 'autoBrightness',
91
- effect: 'auto_brightness'
92
- }, {
93
- prop: 'autoColor',
94
- effect: 'auto_color'
95
- }, {
96
- prop: 'autoContrast',
97
- effect: 'auto_contrast'
98
- }, {
99
- prop: 'assistColorblind',
100
- effect: 'assist_colorblind'
101
- }, 'blackwhite', 'blur', {
102
- prop: 'blurFaces',
103
- effect: 'blur_faces'
104
- }, {
105
- prop: 'blurRegion',
106
- effect: 'blur_region'
107
- }, 'brightness', {
108
- prop: 'brightnessHSB',
109
- effect: 'brightness_hsb'
110
- }, 'cartoonify', 'colorize', 'contrast', 'distort', {
111
- prop: 'fillLight',
112
- effect: 'fill_light'
113
- }, 'gamma', {
114
- prop: 'gradientFade',
115
- effect: 'gradient_fade'
116
- }, 'grayscale', 'improve', 'negate', {
117
- prop: 'oilPaint',
118
- effect: 'oil_paint'
119
- }, 'outline', 'pixelate', {
120
- prop: 'pixelateFaces',
121
- effect: 'pixelate_faces'
122
- }, {
123
- prop: 'pixelateRegion',
124
- effect: 'pixelate_region'
125
- }, 'redeye', {
126
- prop: 'replaceColor',
127
- effect: 'replace_color'
128
- }, 'saturation', 'sepia', 'shadow', 'sharpen', 'shear', {
129
- prop: 'simulateColorblind',
130
- effect: 'simulate_colorblind'
131
- }, 'tint', {
132
- prop: 'unsharpMask',
133
- effect: 'unsharp_mask'
134
- }, 'vectorize', 'vibrance', 'vignette'];
135
- var props$6 = params.map(function (param) {
136
- return param.prop || param;
137
- });
138
- function plugin$6(_temp) {
139
- var _ref = _temp === void 0 ? {} : _temp,
140
- cldImage = _ref.cldImage,
141
- options = _ref.options;
142
- params.forEach(function (key) {
143
- var prop = key.prop || key;
144
- var effect = key.effect || key;
145
- if (options[prop] === true) {
146
- cldImage.effect("e_" + effect);
147
- } else if (typeof options[prop] === 'string') {
148
- cldImage.effect("e_" + effect + ":" + options[prop]);
149
- }
150
- });
151
- }
152
-
153
- var effectsPlugin = {
154
- __proto__: null,
155
- props: props$6,
156
- plugin: plugin$6
157
- };
158
-
159
- /**
160
- * encodeBase64
161
- */
162
-
163
- function encodeBase64(value) {
164
- if (typeof btoa === 'function') {
165
- return btoa(value);
166
- }
167
- if (typeof Buffer !== 'undefined') {
168
- return Buffer.from(value).toString('base64');
169
- }
170
- }
171
-
172
89
  // aspectRatio
173
90
 
174
91
  var primary = {
@@ -236,6 +153,211 @@ var text = {
236
153
  qualifier: false
237
154
  }
238
155
  };
156
+ var effects = {
157
+ art: {
158
+ prefix: 'e',
159
+ qualifier: 'art'
160
+ },
161
+ autoBrightness: {
162
+ prefix: 'e',
163
+ qualifier: 'auto_brightness'
164
+ },
165
+ autoColor: {
166
+ prefix: 'e',
167
+ qualifier: 'auto_color'
168
+ },
169
+ autoContrast: {
170
+ prefix: 'e',
171
+ qualifier: 'auto_contrast'
172
+ },
173
+ assistColorblind: {
174
+ prefix: 'e',
175
+ qualifier: 'assist_colorblind'
176
+ },
177
+ blackwhite: {
178
+ prefix: 'e',
179
+ qualifier: 'blackwhite'
180
+ },
181
+ blur: {
182
+ prefix: 'e',
183
+ qualifier: 'blur'
184
+ },
185
+ blurFaces: {
186
+ prefix: 'e',
187
+ qualifier: 'blur_faces'
188
+ },
189
+ blurRegion: {
190
+ prefix: 'e',
191
+ qualifier: 'blur_region'
192
+ },
193
+ brightness: {
194
+ prefix: 'e',
195
+ qualifier: 'brightness'
196
+ },
197
+ brightnessHSB: {
198
+ prefix: 'e',
199
+ qualifier: 'brightness_hsb'
200
+ },
201
+ cartoonify: {
202
+ prefix: 'e',
203
+ qualifier: 'cartoonify'
204
+ },
205
+ colorize: {
206
+ prefix: 'e',
207
+ qualifier: 'colorize'
208
+ },
209
+ contrast: {
210
+ prefix: 'e',
211
+ qualifier: 'contrast'
212
+ },
213
+ distort: {
214
+ prefix: 'e',
215
+ qualifier: 'distort'
216
+ },
217
+ fillLight: {
218
+ prefix: 'e',
219
+ qualifier: 'fill_light'
220
+ },
221
+ gamma: {
222
+ prefix: 'e',
223
+ qualifier: 'gamma'
224
+ },
225
+ gradientFade: {
226
+ prefix: 'e',
227
+ qualifier: 'gradient_fade'
228
+ },
229
+ grayscale: {
230
+ prefix: 'e',
231
+ qualifier: 'grayscale'
232
+ },
233
+ improve: {
234
+ prefix: 'e',
235
+ qualifier: 'improve'
236
+ },
237
+ negate: {
238
+ prefix: 'e',
239
+ qualifier: 'negate'
240
+ },
241
+ oilPaint: {
242
+ prefix: 'e',
243
+ qualifier: 'oil_paint'
244
+ },
245
+ opacity: {
246
+ qualifier: 'o'
247
+ },
248
+ outline: {
249
+ prefix: 'e',
250
+ qualifier: 'outline'
251
+ },
252
+ pixelate: {
253
+ prefix: 'e',
254
+ qualifier: 'pixelate'
255
+ },
256
+ pixelateFaces: {
257
+ prefix: 'e',
258
+ qualifier: 'pixelate_faces'
259
+ },
260
+ pixelateRegion: {
261
+ prefix: 'e',
262
+ qualifier: 'pixelate_region'
263
+ },
264
+ redeye: {
265
+ prefix: 'e',
266
+ qualifier: 'redeye'
267
+ },
268
+ replaceColor: {
269
+ prefix: 'e',
270
+ qualifier: 'replace_color'
271
+ },
272
+ saturation: {
273
+ prefix: 'e',
274
+ qualifier: 'saturation'
275
+ },
276
+ sepia: {
277
+ prefix: 'e',
278
+ qualifier: 'sepia'
279
+ },
280
+ shadow: {
281
+ prefix: 'e',
282
+ qualifier: 'shadow'
283
+ },
284
+ sharpen: {
285
+ prefix: 'e',
286
+ qualifier: 'sharpen'
287
+ },
288
+ shear: {
289
+ prefix: 'e',
290
+ qualifier: 'shear'
291
+ },
292
+ simulateColorblind: {
293
+ prefix: 'e',
294
+ qualifier: 'simulate_colorblind'
295
+ },
296
+ tint: {
297
+ prefix: 'e',
298
+ qualifier: 'tint'
299
+ },
300
+ unsharpMask: {
301
+ prefix: 'e',
302
+ qualifier: 'unsharp_mask'
303
+ },
304
+ vectorize: {
305
+ prefix: 'e',
306
+ qualifier: 'vectorize'
307
+ },
308
+ vibrance: {
309
+ prefix: 'e',
310
+ qualifier: 'vibrance'
311
+ },
312
+ vignette: {
313
+ prefix: 'e',
314
+ qualifier: 'vignette'
315
+ }
316
+ };
317
+
318
+ var props$6 = Object.keys(effects);
319
+ function plugin$6(_temp) {
320
+ var _ref = _temp === void 0 ? {} : _temp,
321
+ cldImage = _ref.cldImage,
322
+ options = _ref.options;
323
+ Object.keys(effects).forEach(function (key) {
324
+ var _effects$key = effects[key],
325
+ prefix = _effects$key.prefix,
326
+ qualifier = _effects$key.qualifier;
327
+ var transformation = '';
328
+ if (prefix) {
329
+ transformation = prefix + "_";
330
+ }
331
+ if (options[key] === true) {
332
+ cldImage.effect("" + transformation + qualifier);
333
+ } else if (typeof options[key] === 'string') {
334
+ if (prefix) {
335
+ cldImage.effect("" + transformation + qualifier + ":" + options[key]);
336
+ } else {
337
+ cldImage.effect(qualifier + "_" + options[key]);
338
+ }
339
+ }
340
+ });
341
+ }
342
+
343
+ var effectsPlugin = {
344
+ __proto__: null,
345
+ props: props$6,
346
+ plugin: plugin$6
347
+ };
348
+
349
+ /**
350
+ * encodeBase64
351
+ */
352
+
353
+ function encodeBase64(value) {
354
+ if (typeof btoa === 'function') {
355
+ return btoa(value);
356
+ }
357
+ if (typeof Buffer !== 'undefined') {
358
+ return Buffer.from(value).toString('base64');
359
+ }
360
+ }
239
361
 
240
362
  var _excluded$3 = ["publicId", "url", "position", "text", "effects"];
241
363
  var props$5 = ['text', 'overlays'];
@@ -317,12 +439,33 @@ function plugin$5(_temp) {
317
439
  });
318
440
 
319
441
  // Layer effects
442
+ // For layer effects we allow both the standard effects to be applied as well
443
+ // as the primary ones like width and height as those can be used to modify
444
+ // the layer after it's created
320
445
 
321
446
  layerEffects.forEach(function (effect) {
322
447
  Object.keys(effect).forEach(function (key) {
323
- if (!primary[key]) return;
324
- var qualifier = primary[key].qualifier;
325
- primary$1.push(qualifier + "_" + effect[key]);
448
+ if (primary[key]) {
449
+ var qualifier = primary[key].qualifier;
450
+ primary$1.push(qualifier + "_" + effect[key]);
451
+ } else if (effects[key]) {
452
+ var _qualifiersEffects$ke = effects[key],
453
+ _qualifier = _qualifiersEffects$ke.qualifier,
454
+ prefix = _qualifiersEffects$ke.prefix;
455
+ var transformation = '';
456
+ if (prefix) {
457
+ transformation = prefix + "_";
458
+ }
459
+ if (effect[key] === true) {
460
+ primary$1.push("" + transformation + _qualifier);
461
+ } else if (typeof effect[key] === 'string' || typeof effect[key] === 'number') {
462
+ if (prefix) {
463
+ primary$1.push("" + transformation + _qualifier + ":" + effect[key]);
464
+ } else {
465
+ primary$1.push(_qualifier + "_" + effect[key]);
466
+ }
467
+ }
468
+ }
326
469
  });
327
470
  });
328
471
 
@@ -598,6 +741,58 @@ var zoompanPlugin = {
598
741
  plugin: plugin
599
742
  };
600
743
 
744
+ /**
745
+ * pollForProcessingImage
746
+ */
747
+
748
+ function _catch(body, recover) {
749
+ try {
750
+ var result = body();
751
+ } catch (e) {
752
+ return recover(e);
753
+ }
754
+ if (result && result.then) {
755
+ return result.then(void 0, recover);
756
+ }
757
+ return result;
758
+ }
759
+ var pollForProcessingImage = function pollForProcessingImage(options) {
760
+ try {
761
+ var _exit2;
762
+ var src = options.src;
763
+ var _temp4 = _catch(function () {
764
+ return Promise.resolve(new Promise(function (resolve, reject) {
765
+ fetch(src).then(function (res) {
766
+ if (!res.ok) {
767
+ reject(res);
768
+ return;
769
+ }
770
+ resolve(res);
771
+ });
772
+ })).then(function () {});
773
+ }, function (e) {
774
+ function _temp2(_result2) {
775
+ if (_exit2) return _result2;
776
+ _exit2 = 1;
777
+ return false;
778
+ }
779
+ var _temp = function () {
780
+ if (e.status === 423) {
781
+ return Promise.resolve(pollForProcessingImage(options)).then(function (_await$pollForProcess) {
782
+ _exit2 = 1;
783
+ return _await$pollForProcess;
784
+ });
785
+ }
786
+ }();
787
+ return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
788
+ });
789
+ return Promise.resolve(_temp4 && _temp4.then ? _temp4.then(function (_result) {
790
+ return _exit2 ? _result : true;
791
+ }) : _exit2 ? _temp4 : true);
792
+ } catch (e) {
793
+ return Promise.reject(e);
794
+ }
795
+ };
601
796
  var transformationPlugins = [
602
797
  // Background Removal must always come first
603
798
  removeBackgroundPlugin, croppingPlugin, effectsPlugin, overlaysPlugin, namedTransformationsPlugin, underlaysPlugin, zoompanPlugin,
@@ -765,6 +960,22 @@ function cloudinaryLoader(_ref) {
765
960
  }
766
961
 
767
962
  var CldImage = function CldImage(props) {
963
+ /**
964
+ * handleOnError
965
+ */
966
+ var handleOnError = function handleOnError(options) {
967
+ try {
968
+ return Promise.resolve(pollForProcessingImage({
969
+ src: options.target.src
970
+ })).then(function (result) {
971
+ if (result) {
972
+ setImgKey(defaultImgKey + ";" + Date.now());
973
+ }
974
+ });
975
+ } catch (e) {
976
+ return Promise.reject(e);
977
+ }
978
+ };
768
979
  var CLD_OPTIONS = ['deliveryType'];
769
980
  transformationPlugins.forEach(function (_ref) {
770
981
  var _ref$props = _ref.props,
@@ -785,6 +996,12 @@ var CldImage = function CldImage(props) {
785
996
  }).forEach(function (key) {
786
997
  return imageProps[key] = props[key];
787
998
  });
999
+ var defaultImgKey = Object.keys(imageProps).map(function (key) {
1000
+ return key + ":" + imageProps[key];
1001
+ }).join(';');
1002
+ var _useState = useState(defaultImgKey),
1003
+ imgKey = _useState[0],
1004
+ setImgKey = _useState[1];
788
1005
 
789
1006
  // Construct Cloudinary-specific props by looking for values for any of the supported prop keys
790
1007
 
@@ -825,8 +1042,9 @@ var CldImage = function CldImage(props) {
825
1042
  imageProps: imageProps,
826
1043
  cldOptions: cldOptions
827
1044
  });
828
- }
829
- }));
1045
+ },
1046
+ onError: handleOnError
1047
+ }), imgKey);
830
1048
  };
831
1049
 
832
1050
  var _excluded$1 = ["excludeTags", "twitterTitle"];