allaw-ui 5.1.4 → 5.1.6
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.
|
@@ -56,15 +56,29 @@ var FileUploader = function (_a) {
|
|
|
56
56
|
var progressIntervalRef = useRef(null);
|
|
57
57
|
var fileInputRef = useRef(null);
|
|
58
58
|
var _z = useState(originalFileName), currentFileName = _z[0], setCurrentFileName = _z[1];
|
|
59
|
+
// Garder une référence à l'image originale pour permettre les recrops
|
|
60
|
+
var originalImageUrlRef = useRef(null);
|
|
61
|
+
var originalFileRef = useRef(null);
|
|
59
62
|
useEffect(function () {
|
|
60
63
|
if (initialFile) {
|
|
61
64
|
setSelectedFile(initialFile);
|
|
65
|
+
originalFileRef.current = initialFile;
|
|
62
66
|
initialFile.arrayBuffer().then(function (content) {
|
|
63
67
|
setFileContent(content);
|
|
64
68
|
});
|
|
69
|
+
// Créer une URL pour l'image originale si c'est une image
|
|
70
|
+
if (isImageFile(initialFile) && !originalImageUrlRef.current) {
|
|
71
|
+
var url = URL.createObjectURL(initialFile);
|
|
72
|
+
originalImageUrlRef.current = url;
|
|
73
|
+
}
|
|
65
74
|
}
|
|
66
75
|
if (initialPreviewUrl) {
|
|
67
76
|
setPreviewUrl(initialPreviewUrl);
|
|
77
|
+
// Pour initialPreviewUrl, on ne peut pas créer de File depuis l'URL
|
|
78
|
+
// originalFileRef restera null, mais on peut utiliser l'URL pour le crop
|
|
79
|
+
if (!originalImageUrlRef.current) {
|
|
80
|
+
originalImageUrlRef.current = initialPreviewUrl;
|
|
81
|
+
}
|
|
68
82
|
}
|
|
69
83
|
if (initialCropMetadata) {
|
|
70
84
|
setCropMetadata(initialCropMetadata);
|
|
@@ -78,6 +92,10 @@ var FileUploader = function (_a) {
|
|
|
78
92
|
if (previewUrl && !initialPreviewUrl) {
|
|
79
93
|
URL.revokeObjectURL(previewUrl);
|
|
80
94
|
}
|
|
95
|
+
// Nettoyer aussi la référence à l'image originale
|
|
96
|
+
if (originalImageUrlRef.current && !initialPreviewUrl) {
|
|
97
|
+
URL.revokeObjectURL(originalImageUrlRef.current);
|
|
98
|
+
}
|
|
81
99
|
};
|
|
82
100
|
}, [previewUrl, initialPreviewUrl]);
|
|
83
101
|
var startProgressSimulation = function () {
|
|
@@ -169,6 +187,12 @@ var FileUploader = function (_a) {
|
|
|
169
187
|
if (isImageFile(file)) {
|
|
170
188
|
url = URL.createObjectURL(file);
|
|
171
189
|
setPreviewUrl(url);
|
|
190
|
+
// Conserver la référence à l'image originale pour les recrops
|
|
191
|
+
if (originalImageUrlRef.current && !initialPreviewUrl) {
|
|
192
|
+
URL.revokeObjectURL(originalImageUrlRef.current);
|
|
193
|
+
}
|
|
194
|
+
originalImageUrlRef.current = url;
|
|
195
|
+
originalFileRef.current = file;
|
|
172
196
|
if (!enableCropping) {
|
|
173
197
|
setCropMetadata({
|
|
174
198
|
zoom: 1,
|
|
@@ -181,6 +205,10 @@ var FileUploader = function (_a) {
|
|
|
181
205
|
else {
|
|
182
206
|
setPreviewUrl(null);
|
|
183
207
|
setCropMetadata(null);
|
|
208
|
+
if (originalImageUrlRef.current && !initialPreviewUrl) {
|
|
209
|
+
URL.revokeObjectURL(originalImageUrlRef.current);
|
|
210
|
+
}
|
|
211
|
+
originalImageUrlRef.current = null;
|
|
184
212
|
}
|
|
185
213
|
if (autoManageProgress && !(enableCropping && isImageFile(file))) {
|
|
186
214
|
startProgressSimulation();
|
|
@@ -207,9 +235,28 @@ var FileUploader = function (_a) {
|
|
|
207
235
|
});
|
|
208
236
|
}); };
|
|
209
237
|
var openCropModal = function () {
|
|
210
|
-
|
|
238
|
+
var _a, _b;
|
|
239
|
+
// Si on a un fichier original, ouvrir le modal de crop pour recropper
|
|
240
|
+
// Sinon, si on n'a pas de fichier original (image déjà croppée), ouvrir le sélecteur de fichier
|
|
241
|
+
if (originalFileRef.current && isImageFile(originalFileRef.current)) {
|
|
242
|
+
// S'assurer qu'on a une URL pour l'image originale
|
|
243
|
+
if (!originalImageUrlRef.current) {
|
|
244
|
+
var url = URL.createObjectURL(originalFileRef.current);
|
|
245
|
+
originalImageUrlRef.current = url;
|
|
246
|
+
}
|
|
247
|
+
// Utiliser le fichier original pour le modal de crop
|
|
248
|
+
setSelectedFile(originalFileRef.current);
|
|
211
249
|
setShowCropper(true);
|
|
212
250
|
}
|
|
251
|
+
else if (selectedFile && isImageFile(selectedFile)) {
|
|
252
|
+
// Si on a un selectedFile mais pas d'original, c'est probablement une image déjà croppée
|
|
253
|
+
// Ouvrir le sélecteur de fichier pour charger une nouvelle image
|
|
254
|
+
(_a = fileInputRef.current) === null || _a === void 0 ? void 0 : _a.click();
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
// Pas de fichier, ouvrir le sélecteur
|
|
258
|
+
(_b = fileInputRef.current) === null || _b === void 0 ? void 0 : _b.click();
|
|
259
|
+
}
|
|
213
260
|
};
|
|
214
261
|
/**
|
|
215
262
|
* Crop l'image côté client et retourne un Blob
|
|
@@ -217,10 +264,16 @@ var FileUploader = function (_a) {
|
|
|
217
264
|
*/
|
|
218
265
|
var cropImageToBlob = function (imageUrl, cropMetadata, outputWidth, outputHeight) { return __awaiter(void 0, void 0, void 0, function () {
|
|
219
266
|
return __generator(this, function (_a) {
|
|
267
|
+
console.log("🔪 [FileUploader] cropImageToBlob appelé");
|
|
268
|
+
console.log(" - imageUrl:", imageUrl);
|
|
269
|
+
console.log(" - cropMetadata:", cropMetadata);
|
|
270
|
+
console.log(" - outputWidth:", outputWidth, "outputHeight:", outputHeight);
|
|
220
271
|
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
221
272
|
var img = new window.Image();
|
|
222
273
|
img.crossOrigin = "anonymous";
|
|
223
274
|
img.onload = function () {
|
|
275
|
+
console.log("✅ [FileUploader] Image chargée dans cropImageToBlob");
|
|
276
|
+
console.log(" - img.width:", img.width, "img.height:", img.height);
|
|
224
277
|
try {
|
|
225
278
|
// Créer un canvas aux dimensions de sortie
|
|
226
279
|
var canvas = document.createElement("canvas");
|
|
@@ -301,22 +354,30 @@ var FileUploader = function (_a) {
|
|
|
301
354
|
ctx.putImageData(imageData, 0, 0);
|
|
302
355
|
}
|
|
303
356
|
// Convertir en Blob PNG
|
|
357
|
+
console.log("🖼️ [FileUploader] Conversion canvas vers Blob...");
|
|
304
358
|
canvas.toBlob(function (blob) {
|
|
305
359
|
if (blob) {
|
|
360
|
+
console.log("✅ [FileUploader] Blob créé avec succès, taille:", blob.size);
|
|
306
361
|
resolve(blob);
|
|
307
362
|
}
|
|
308
363
|
else {
|
|
364
|
+
console.error("❌ [FileUploader] Échec de la conversion canvas vers Blob");
|
|
309
365
|
reject(new Error("Échec de la conversion canvas vers Blob"));
|
|
310
366
|
}
|
|
311
367
|
}, "image/png", 0.95);
|
|
312
368
|
}
|
|
313
369
|
catch (error) {
|
|
370
|
+
console.error("❌ [FileUploader] Erreur dans img.onload:", error);
|
|
314
371
|
reject(error);
|
|
315
372
|
}
|
|
316
373
|
};
|
|
317
|
-
img.onerror = function () {
|
|
374
|
+
img.onerror = function (error) {
|
|
375
|
+
console.error("❌ [FileUploader] Erreur de chargement de l'image");
|
|
376
|
+
console.error(" - imageUrl:", imageUrl);
|
|
377
|
+
console.error(" - error:", error);
|
|
318
378
|
reject(new Error("Échec du chargement de l'image"));
|
|
319
379
|
};
|
|
380
|
+
console.log("📤 [FileUploader] Définition de img.src:", imageUrl);
|
|
320
381
|
img.src = imageUrl;
|
|
321
382
|
})];
|
|
322
383
|
});
|
|
@@ -325,53 +386,88 @@ var FileUploader = function (_a) {
|
|
|
325
386
|
setShowCropper(false);
|
|
326
387
|
};
|
|
327
388
|
var handleCropConfirm = function (cropMetadata) { return __awaiter(void 0, void 0, void 0, function () {
|
|
328
|
-
var dimensions, croppedBlob, croppedFile, croppedContent, croppedUrl, error_2;
|
|
329
|
-
|
|
330
|
-
|
|
389
|
+
var dimensions, fileToUse, imageToCropUrl, croppedBlob, croppedFile, croppedContent, croppedUrl, error_2;
|
|
390
|
+
var _a, _b;
|
|
391
|
+
return __generator(this, function (_c) {
|
|
392
|
+
switch (_c.label) {
|
|
331
393
|
case 0:
|
|
394
|
+
console.log("✂️ [FileUploader] handleCropConfirm appelé");
|
|
395
|
+
console.log(" - cropMetadata:", cropMetadata);
|
|
396
|
+
console.log(" - clientSideCrop:", clientSideCrop);
|
|
397
|
+
console.log(" - selectedFile:", selectedFile === null || selectedFile === void 0 ? void 0 : selectedFile.name, "size:", selectedFile === null || selectedFile === void 0 ? void 0 : selectedFile.size);
|
|
398
|
+
console.log(" - originalFileRef.current:", (_a = originalFileRef.current) === null || _a === void 0 ? void 0 : _a.name, "size:", (_b = originalFileRef.current) === null || _b === void 0 ? void 0 : _b.size);
|
|
399
|
+
console.log(" - originalImageUrlRef.current:", originalImageUrlRef.current);
|
|
400
|
+
console.log(" - previewUrl:", previewUrl);
|
|
332
401
|
setShowCropper(false);
|
|
333
402
|
setCropMetadata(cropMetadata);
|
|
334
403
|
if (autoManageProgress) {
|
|
335
404
|
startProgressSimulation();
|
|
336
405
|
}
|
|
337
|
-
if (!(selectedFile && fileContent
|
|
338
|
-
if (!clientSideCrop) return [3 /*break*/,
|
|
339
|
-
|
|
406
|
+
if (!(selectedFile && fileContent)) return [3 /*break*/, 10];
|
|
407
|
+
if (!clientSideCrop) return [3 /*break*/, 9];
|
|
408
|
+
_c.label = 1;
|
|
340
409
|
case 1:
|
|
341
|
-
|
|
410
|
+
_c.trys.push([1, 7, , 8]);
|
|
342
411
|
dimensions = cropOutputDimensions[cropMetadata.shape] ||
|
|
343
412
|
cropOutputDimensions.square || { width: 200, height: 200 };
|
|
344
|
-
|
|
413
|
+
console.log("📐 [FileUploader] Dimensions de sortie:", dimensions);
|
|
414
|
+
fileToUse = originalFileRef.current || selectedFile;
|
|
415
|
+
console.log("📁 [FileUploader] Fichier à utiliser pour le crop:", fileToUse.name, "size:", fileToUse.size, "type:", fileToUse.type);
|
|
416
|
+
imageToCropUrl = URL.createObjectURL(fileToUse);
|
|
417
|
+
console.log("🔗 [FileUploader] URL blob créée pour le crop:", imageToCropUrl);
|
|
418
|
+
_c.label = 2;
|
|
345
419
|
case 2:
|
|
346
|
-
|
|
347
|
-
|
|
420
|
+
_c.trys.push([2, , 5, 6]);
|
|
421
|
+
// Cropper l'image côté client
|
|
422
|
+
console.log("🔄 [FileUploader] Appel de cropImageToBlob...");
|
|
423
|
+
return [4 /*yield*/, cropImageToBlob(imageToCropUrl, cropMetadata, dimensions.width, dimensions.height)];
|
|
424
|
+
case 3:
|
|
425
|
+
croppedBlob = _c.sent();
|
|
426
|
+
console.log("✅ [FileUploader] cropImageToBlob terminé, blob size:", croppedBlob.size);
|
|
427
|
+
croppedFile = new File([croppedBlob], fileToUse.name.replace(/\.[^/.]+$/, ".png"), // Remplacer l'extension par .png
|
|
348
428
|
{ type: "image/png" });
|
|
429
|
+
console.log("📄 [FileUploader] File croppé créé:", croppedFile.name, "size:", croppedFile.size);
|
|
349
430
|
return [4 /*yield*/, croppedFile.arrayBuffer()];
|
|
350
|
-
case
|
|
351
|
-
croppedContent =
|
|
431
|
+
case 4:
|
|
432
|
+
croppedContent = _c.sent();
|
|
433
|
+
console.log("📦 [FileUploader] Contenu du fichier croppé lu, size:", croppedContent.byteLength);
|
|
352
434
|
croppedUrl = URL.createObjectURL(croppedBlob);
|
|
435
|
+
console.log("🖼️ [FileUploader] URL blob créée pour la preview:", croppedUrl);
|
|
353
436
|
if (previewUrl && !initialPreviewUrl) {
|
|
437
|
+
console.log("🗑️ [FileUploader] Révoquement de l'ancienne previewUrl:", previewUrl);
|
|
354
438
|
URL.revokeObjectURL(previewUrl);
|
|
355
439
|
}
|
|
356
440
|
setPreviewUrl(croppedUrl);
|
|
357
441
|
setSelectedFile(croppedFile);
|
|
358
442
|
setFileContent(croppedContent);
|
|
359
443
|
setCurrentFileName(croppedFile.name);
|
|
444
|
+
// Réinitialiser cropMetadata car l'image est déjà croppée
|
|
445
|
+
// Plus besoin d'appliquer les transformations CSS
|
|
446
|
+
setCropMetadata(null);
|
|
360
447
|
// Appeler onFileRead avec le fichier croppé (pas de cropMetadata car déjà croppé)
|
|
448
|
+
console.log("📞 [FileUploader] Appel de onFileRead avec le fichier croppé");
|
|
361
449
|
onFileRead === null || onFileRead === void 0 ? void 0 : onFileRead(croppedFile, croppedContent);
|
|
362
|
-
return [3 /*break*/,
|
|
363
|
-
case
|
|
364
|
-
|
|
365
|
-
console.
|
|
450
|
+
return [3 /*break*/, 6];
|
|
451
|
+
case 5:
|
|
452
|
+
// Toujours nettoyer l'URL blob temporaire créée pour le crop
|
|
453
|
+
console.log("🗑️ [FileUploader] Révoquement de l'URL blob temporaire:", imageToCropUrl);
|
|
454
|
+
URL.revokeObjectURL(imageToCropUrl);
|
|
455
|
+
return [7 /*endfinally*/];
|
|
456
|
+
case 6: return [3 /*break*/, 8];
|
|
457
|
+
case 7:
|
|
458
|
+
error_2 = _c.sent();
|
|
459
|
+
console.error("❌ [FileUploader] Erreur lors du crop de l'image:", error_2);
|
|
460
|
+
console.error(" - error details:", error_2);
|
|
366
461
|
// En cas d'erreur, fallback sur l'image originale avec métadonnées
|
|
462
|
+
console.log("🔄 [FileUploader] Fallback sur l'image originale");
|
|
367
463
|
onFileRead === null || onFileRead === void 0 ? void 0 : onFileRead(selectedFile, fileContent, cropMetadata);
|
|
368
|
-
return [3 /*break*/,
|
|
369
|
-
case
|
|
370
|
-
case
|
|
464
|
+
return [3 /*break*/, 8];
|
|
465
|
+
case 8: return [3 /*break*/, 10];
|
|
466
|
+
case 9:
|
|
371
467
|
// Passer l'image originale avec les métadonnées au serveur
|
|
372
468
|
onFileRead === null || onFileRead === void 0 ? void 0 : onFileRead(selectedFile, fileContent, cropMetadata);
|
|
373
|
-
|
|
374
|
-
case
|
|
469
|
+
_c.label = 10;
|
|
470
|
+
case 10: return [2 /*return*/];
|
|
375
471
|
}
|
|
376
472
|
});
|
|
377
473
|
}); };
|
|
@@ -379,6 +475,12 @@ var FileUploader = function (_a) {
|
|
|
379
475
|
if (previewUrl && !initialPreviewUrl) {
|
|
380
476
|
URL.revokeObjectURL(previewUrl);
|
|
381
477
|
}
|
|
478
|
+
// Nettoyer la référence à l'image originale
|
|
479
|
+
if (originalImageUrlRef.current && !initialPreviewUrl) {
|
|
480
|
+
URL.revokeObjectURL(originalImageUrlRef.current);
|
|
481
|
+
}
|
|
482
|
+
originalImageUrlRef.current = null;
|
|
483
|
+
originalFileRef.current = null;
|
|
382
484
|
if (autoManageProgress) {
|
|
383
485
|
stopProgressSimulation();
|
|
384
486
|
setInternalProgress(0);
|