@sunggang/ui-lib 0.3.63 → 0.3.64

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.
Files changed (2) hide show
  1. package/index.esm2.js +129 -67
  2. package/package.json +1 -1
package/index.esm2.js CHANGED
@@ -18057,8 +18057,6 @@ var DropImage = function(param) {
18057
18057
  var currentImageCount = (previewFiles === null || previewFiles === void 0 ? void 0 : previewFiles.length) || 0;
18058
18058
  var remainingSlots = maxTotalImages - currentImageCount;
18059
18059
  var canUploadMore = remainingSlots > 0;
18060
- // 動態調整 maxFiles,不能超過剩餘空間
18061
- var dynamicMaxFiles = Math.min(maxFiles, remainingSlots);
18062
18060
  var _useDropzone = useDropzone({
18063
18061
  accept: {
18064
18062
  "image/png": [
@@ -18069,30 +18067,13 @@ var DropImage = function(param) {
18069
18067
  ".jpeg"
18070
18068
  ]
18071
18069
  },
18072
- maxFiles: dynamicMaxFiles,
18070
+ maxFiles: maxFiles,
18073
18071
  maxSize: DEFAULT_IMAGE_SIZE_LIMIT_IN_BYTES,
18074
18072
  disabled: !canUploadMore,
18075
18073
  onDrop: function(dropFiles) {
18076
- if (!canUploadMore) {
18077
- setUploadError(/*#__PURE__*/ jsx(ErrorContent, {
18078
- setModal: function() {
18079
- return setUploadError(null);
18080
- },
18081
- errmsg: /*#__PURE__*/ jsxs("div", {
18082
- className: "text-center text-lg text-gray-500",
18083
- children: [
18084
- "已達到最大圖片數量限制:",
18085
- maxTotalImages,
18086
- " 張"
18087
- ]
18088
- })
18089
- }));
18090
- return;
18091
- }
18092
18074
  // 檢查是否會超過總數限制
18093
18075
  var totalAfterUpload = currentImageCount + dropFiles.length;
18094
18076
  if (totalAfterUpload > maxTotalImages) {
18095
- var allowedFiles = dropFiles.slice(0, remainingSlots);
18096
18077
  setUploadError(/*#__PURE__*/ jsx(ErrorContent, {
18097
18078
  setModal: function() {
18098
18079
  return setUploadError(null);
@@ -18100,35 +18081,33 @@ var DropImage = function(param) {
18100
18081
  errmsg: /*#__PURE__*/ jsxs("div", {
18101
18082
  className: "text-center text-lg text-gray-500",
18102
18083
  children: [
18103
- "最多只能上傳 ",
18104
- remainingSlots,
18105
- " 張圖片,已自動限制為前",
18106
- " ",
18107
- allowedFiles.length,
18108
- " 張"
18084
+ "已超過最大上傳限制",
18085
+ /*#__PURE__*/ jsxs("div", {
18086
+ className: "text-sm text-gray-400 mt-2",
18087
+ children: [
18088
+ "目前已有 ",
18089
+ currentImageCount,
18090
+ " 張,最多可上傳 ",
18091
+ maxTotalImages,
18092
+ " ",
18093
+ "張"
18094
+ ]
18095
+ }),
18096
+ /*#__PURE__*/ jsxs("div", {
18097
+ className: "text-sm text-gray-400",
18098
+ children: [
18099
+ "還可上傳 ",
18100
+ remainingSlots,
18101
+ " 張圖片"
18102
+ ]
18103
+ })
18109
18104
  ]
18110
18105
  })
18111
18106
  }));
18112
- // 只處理允許的檔案數量
18113
- var newFiles = [];
18114
- setFiles(function(prevFiles) {
18115
- return _to_consumable_array$8(prevFiles).concat(_to_consumable_array$8(allowedFiles));
18116
- });
18117
- allowedFiles.forEach(function(dropFile, index) {
18118
- Object.assign(dropFile, {
18119
- preview: URL.createObjectURL(dropFile),
18120
- id: v4(),
18121
- index: (previewFiles === null || previewFiles === void 0 ? void 0 : previewFiles.length) + index + 1
18122
- });
18123
- newFiles.push(dropFile);
18124
- });
18125
- setPreviewFiles(function(prev) {
18126
- return _to_consumable_array$8(prev).concat(_to_consumable_array$8(newFiles));
18127
- });
18128
18107
  return;
18129
18108
  }
18130
- // 正常處理
18131
- var newFiles1 = [];
18109
+ // 正常處理檔案上傳
18110
+ var newFiles = [];
18132
18111
  setFiles(function(prevFiles) {
18133
18112
  return _to_consumable_array$8(prevFiles).concat(_to_consumable_array$8(dropFiles));
18134
18113
  });
@@ -18138,17 +18117,38 @@ var DropImage = function(param) {
18138
18117
  id: v4(),
18139
18118
  index: (previewFiles === null || previewFiles === void 0 ? void 0 : previewFiles.length) + index + 1
18140
18119
  });
18141
- newFiles1.push(dropFile);
18120
+ newFiles.push(dropFile);
18142
18121
  });
18143
18122
  setPreviewFiles(function(prev) {
18144
- return _to_consumable_array$8(prev).concat(_to_consumable_array$8(newFiles1));
18123
+ return _to_consumable_array$8(prev).concat(_to_consumable_array$8(newFiles));
18145
18124
  });
18146
18125
  },
18147
18126
  onDropRejected: function(rejectedFiles) {
18148
- var errorItems = rejectedFiles.map(function(param) {
18149
- var file = param.file; param.errors;
18150
- return /*#__PURE__*/ jsx("div", {
18151
- children: /*#__PURE__*/ jsxs("div", {
18127
+ var errorMessage;
18128
+ // 分析錯誤類型並顯示對應訊息
18129
+ var fileSizeErrors = rejectedFiles.filter(function(param) {
18130
+ var errors = param.errors;
18131
+ return errors.some(function(error) {
18132
+ return error.code === "file-too-large";
18133
+ });
18134
+ });
18135
+ var fileTypeErrors = rejectedFiles.filter(function(param) {
18136
+ var errors = param.errors;
18137
+ return errors.some(function(error) {
18138
+ return error.code === "file-invalid-type";
18139
+ });
18140
+ });
18141
+ var tooManyFilesErrors = rejectedFiles.filter(function(param) {
18142
+ var errors = param.errors;
18143
+ return errors.some(function(error) {
18144
+ return error.code === "too-many-files";
18145
+ });
18146
+ });
18147
+ // 優先處理檔案大小錯誤
18148
+ if (fileSizeErrors.length > 0) {
18149
+ var sizeErrorItems = fileSizeErrors.map(function(param) {
18150
+ var file = param.file;
18151
+ return /*#__PURE__*/ jsxs("div", {
18152
18152
  className: "text-center text-lg text-gray-500",
18153
18153
  children: [
18154
18154
  "檔案太大: ",
@@ -18157,27 +18157,65 @@ var DropImage = function(param) {
18157
18157
  (file.size / 1024 / 1024).toFixed(2),
18158
18158
  " MB"
18159
18159
  ]
18160
- })
18161
- }, file.path);
18162
- });
18163
- setUploadError(/*#__PURE__*/ jsx(ErrorContent, {
18164
- setModal: function() {
18165
- return setUploadError(null);
18166
- },
18167
- errmsg: /*#__PURE__*/ jsxs(Fragment$1, {
18160
+ }, file.path);
18161
+ });
18162
+ errorMessage = /*#__PURE__*/ jsxs(Fragment$1, {
18168
18163
  children: [
18169
- errorItems,
18164
+ sizeErrorItems,
18170
18165
  /*#__PURE__*/ jsxs("div", {
18171
- className: "pt-4",
18166
+ className: "pt-4 text-center",
18172
18167
  children: [
18173
- "上限:",
18168
+ "檔案大小上限:",
18174
18169
  (DEFAULT_IMAGE_SIZE_LIMIT_IN_BYTES / 1024 / 1024).toFixed(2),
18175
- " ",
18176
- "MB"
18170
+ " MB"
18177
18171
  ]
18178
18172
  })
18179
18173
  ]
18180
- })
18174
+ });
18175
+ } else if (fileTypeErrors.length > 0) {
18176
+ var typeErrorItems = fileTypeErrors.map(function(param) {
18177
+ var file = param.file;
18178
+ return /*#__PURE__*/ jsxs("div", {
18179
+ className: "text-center text-lg text-gray-500",
18180
+ children: [
18181
+ "不支援的檔案格式: ",
18182
+ file.path
18183
+ ]
18184
+ }, file.path);
18185
+ });
18186
+ errorMessage = /*#__PURE__*/ jsxs(Fragment$1, {
18187
+ children: [
18188
+ typeErrorItems,
18189
+ /*#__PURE__*/ jsx("div", {
18190
+ className: "pt-4 text-center text-sm text-gray-400",
18191
+ children: "僅支援 PNG、JPG、JPEG 格式"
18192
+ })
18193
+ ]
18194
+ });
18195
+ } else if (tooManyFilesErrors.length > 0) {
18196
+ errorMessage = /*#__PURE__*/ jsxs("div", {
18197
+ className: "text-center text-lg text-gray-500",
18198
+ children: [
18199
+ "一次最多只能上傳 ",
18200
+ maxFiles,
18201
+ " 張圖片",
18202
+ /*#__PURE__*/ jsx("div", {
18203
+ className: "text-sm text-gray-400 mt-2",
18204
+ children: "請減少選擇的檔案數量"
18205
+ })
18206
+ ]
18207
+ });
18208
+ } else {
18209
+ errorMessage = /*#__PURE__*/ jsx("div", {
18210
+ className: "text-center text-lg text-gray-500",
18211
+ children: "檔案上傳失敗,請重試"
18212
+ });
18213
+ }
18214
+ setUploadError(/*#__PURE__*/ jsx(ErrorContent, {
18215
+ setModal: function() {
18216
+ return setUploadError(null);
18217
+ },
18218
+ errmsg: errorMessage
18181
18219
  }));
18182
18220
  }
18183
18221
  }), getRootProps = _useDropzone.getRootProps, getInputProps = _useDropzone.getInputProps; _useDropzone.isDragActive; _useDropzone.isDragAccept; _useDropzone.isDragReject;
@@ -18195,9 +18233,33 @@ var DropImage = function(param) {
18195
18233
  setPreviewFiles(reSortItem);
18196
18234
  };
18197
18235
  var handleRemove = function(item) {
18198
- var currentItem = previewFiles === null || previewFiles === void 0 ? void 0 : previewFiles.indexOf(item);
18199
- previewFiles.splice(currentItem, 1);
18200
- setImageUrls(_to_consumable_array$8(previewFiles));
18236
+ var currentItemIndex = previewFiles === null || previewFiles === void 0 ? void 0 : previewFiles.indexOf(item);
18237
+ if (currentItemIndex !== -1) {
18238
+ // 創建新的 previewFiles 陣列(移除指定項目)
18239
+ var newPreviewFiles = _to_consumable_array$8(previewFiles);
18240
+ newPreviewFiles.splice(currentItemIndex, 1);
18241
+ // 更新預覽檔案狀態
18242
+ setPreviewFiles(newPreviewFiles);
18243
+ setImageUrls(newPreviewFiles);
18244
+ // 同時更新實際檔案狀態
18245
+ setFiles(function(prevFiles) {
18246
+ // 如果被移除的項目有 id 且在當前上傳的檔案中
18247
+ if (item.id) {
18248
+ // 透過 id 來找到對應的檔案並移除
18249
+ return prevFiles.filter(function(file) {
18250
+ var shouldRemove = file.id === item.id;
18251
+ if (shouldRemove && item.preview) {
18252
+ // 釋放 URL 記憶體
18253
+ URL.revokeObjectURL(item.preview);
18254
+ }
18255
+ return !shouldRemove;
18256
+ });
18257
+ } else {
18258
+ // 如果沒有 id,可能是舊有的圖片,保持原本的 files
18259
+ return prevFiles;
18260
+ }
18261
+ });
18262
+ }
18201
18263
  };
18202
18264
  return /*#__PURE__*/ jsxs("section", {
18203
18265
  className: "w-full",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sunggang/ui-lib",
3
- "version": "0.3.63",
3
+ "version": "0.3.64",
4
4
  "dependencies": {
5
5
  "@emotion/react": "^11.14.0",
6
6
  "@emotion/styled": "^11.14.0",