@destyler/file-upload 0.2.10 → 0.2.12
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/dist/index.d.mts +8 -11
- package/dist/index.mjs +52 -59
- package/package.json +8 -8
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import * as _destyler_anatomy0 from "@destyler/anatomy";
|
|
2
1
|
import { FileError, FileMimeType } from "@destyler/file";
|
|
3
|
-
import * as _destyler_xstate0 from "@destyler/xstate";
|
|
4
2
|
import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
|
|
5
3
|
import { CommonProperties, LocaleProperties, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
|
|
6
|
-
|
|
7
4
|
//#region src/anatomy.d.ts
|
|
8
|
-
declare const anatomy:
|
|
5
|
+
export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"root" | "dropzone" | "item" | "itemDeleteTrigger" | "itemGroup" | "itemName" | "itemPreview" | "itemPreviewImage" | "itemSizeText" | "label" | "trigger" | "clearTrigger">;
|
|
9
6
|
//#endregion
|
|
10
7
|
//#region src/types.d.ts
|
|
11
8
|
interface FileRejection {
|
|
@@ -238,15 +235,15 @@ interface MachineApi<T extends PropTypes> {
|
|
|
238
235
|
}
|
|
239
236
|
//#endregion
|
|
240
237
|
//#region src/connect.d.ts
|
|
241
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
238
|
+
export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
242
239
|
//#endregion
|
|
243
240
|
//#region src/machine.d.ts
|
|
244
|
-
declare function machine(userContext: UserDefinedContext):
|
|
241
|
+
export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
|
|
245
242
|
//#endregion
|
|
246
243
|
//#region src/props.d.ts
|
|
247
|
-
declare const props: ("name" | "ids" | "translations" | "accept" | "disabled" | "required" | "allowDrop" | "maxFileSize" | "minFileSize" | "maxFiles" | "preventDocumentDrop" | "validate" | "onFileChange" | "onFileAccept" | "onFileReject" | "capture" | "directory" | "invalid" | "locale" | "dir" | "id" | "getRootNode")[];
|
|
248
|
-
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "name" | "ids" | "translations" | "accept" | "disabled" | "required" | "allowDrop" | "maxFileSize" | "minFileSize" | "maxFiles" | "preventDocumentDrop" | "validate" | "onFileChange" | "onFileAccept" | "onFileReject" | "capture" | "directory" | "invalid" | "locale" | "dir" | "id" | "getRootNode">];
|
|
249
|
-
declare const itemProps: "file"[];
|
|
250
|
-
declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, "file">];
|
|
244
|
+
export declare const props: ("name" | "ids" | "translations" | "accept" | "disabled" | "required" | "allowDrop" | "maxFileSize" | "minFileSize" | "maxFiles" | "preventDocumentDrop" | "validate" | "onFileChange" | "onFileAccept" | "onFileReject" | "capture" | "directory" | "invalid" | "locale" | "dir" | "id" | "getRootNode")[];
|
|
245
|
+
export declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "name" | "ids" | "translations" | "accept" | "disabled" | "required" | "allowDrop" | "maxFileSize" | "minFileSize" | "maxFiles" | "preventDocumentDrop" | "validate" | "onFileChange" | "onFileAccept" | "onFileReject" | "capture" | "directory" | "invalid" | "locale" | "dir" | "id" | "getRootNode">];
|
|
246
|
+
export declare const itemProps: "file"[];
|
|
247
|
+
export declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, "file">];
|
|
251
248
|
//#endregion
|
|
252
|
-
export {
|
|
249
|
+
export type { MachineApi as Api, UserDefinedContext as Context, DropzoneProps, ElementIds, FileAcceptDetails, FileChangeDetails, FileError, FileMimeType, FileRejectDetails, FileRejection, FileValidateDetails, IntlTranslations, ItemPreviewImageProps, ItemProps, Service };
|
package/dist/index.mjs
CHANGED
|
@@ -5,11 +5,9 @@ import { getAcceptAttrString, isFileEqual, isValidFileSize, isValidFileType } fr
|
|
|
5
5
|
import { callAll, compact, createSplitProps } from "@destyler/utils";
|
|
6
6
|
import { createMachine, ref } from "@destyler/xstate";
|
|
7
7
|
import { createProps } from "@destyler/types";
|
|
8
|
-
|
|
9
8
|
//#region src/anatomy.ts
|
|
10
9
|
const anatomy = createAnatomy("file-upload").parts("root", "dropzone", "item", "itemDeleteTrigger", "itemGroup", "itemName", "itemPreview", "itemPreviewImage", "itemSizeText", "label", "trigger", "clearTrigger");
|
|
11
10
|
const parts = anatomy.build();
|
|
12
|
-
|
|
13
11
|
//#endregion
|
|
14
12
|
//#region src/dom.ts
|
|
15
13
|
const dom = createScope({
|
|
@@ -26,7 +24,6 @@ const dom = createScope({
|
|
|
26
24
|
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx)),
|
|
27
25
|
getDropzoneEl: (ctx) => dom.getById(ctx, dom.getDropzoneId(ctx))
|
|
28
26
|
});
|
|
29
|
-
|
|
30
27
|
//#endregion
|
|
31
28
|
//#region src/utils.ts
|
|
32
29
|
function isEventWithFiles(event) {
|
|
@@ -77,7 +74,6 @@ function getFilesFromEvent(ctx, files) {
|
|
|
77
74
|
rejectedFiles
|
|
78
75
|
};
|
|
79
76
|
}
|
|
80
|
-
|
|
81
77
|
//#endregion
|
|
82
78
|
//#region src/connect.ts
|
|
83
79
|
function connect(state, send, normalize) {
|
|
@@ -148,13 +144,13 @@ function connect(state, send, normalize) {
|
|
|
148
144
|
"data-dragging": dataAttr(dragging)
|
|
149
145
|
});
|
|
150
146
|
},
|
|
151
|
-
getDropzoneProps(props
|
|
147
|
+
getDropzoneProps(props = {}) {
|
|
152
148
|
return normalize.element({
|
|
153
149
|
...parts.dropzone.attrs,
|
|
154
150
|
"dir": state.context.dir,
|
|
155
151
|
"id": dom.getDropzoneId(state.context),
|
|
156
|
-
"tabIndex": disabled || props
|
|
157
|
-
"role": props
|
|
152
|
+
"tabIndex": disabled || props.disableClick ? void 0 : 0,
|
|
153
|
+
"role": props.disableClick ? "application" : "button",
|
|
158
154
|
"aria-label": translations.dropzone,
|
|
159
155
|
"aria-disabled": disabled,
|
|
160
156
|
"data-invalid": dataAttr(state.context.invalid),
|
|
@@ -164,7 +160,7 @@ function connect(state, send, normalize) {
|
|
|
164
160
|
if (disabled) return;
|
|
165
161
|
if (event.defaultPrevented) return;
|
|
166
162
|
if (!isSelfTarget(event)) return;
|
|
167
|
-
if (props
|
|
163
|
+
if (props.disableClick) return;
|
|
168
164
|
if (event.key !== "Enter" && event.key !== " ") return;
|
|
169
165
|
send({
|
|
170
166
|
type: "DROPZONE.CLICK",
|
|
@@ -174,7 +170,7 @@ function connect(state, send, normalize) {
|
|
|
174
170
|
onClick(event) {
|
|
175
171
|
if (disabled) return;
|
|
176
172
|
if (event.defaultPrevented) return;
|
|
177
|
-
if (props
|
|
173
|
+
if (props.disableClick) return;
|
|
178
174
|
if (!isSelfTarget(event)) return;
|
|
179
175
|
if (event.currentTarget.localName === "label") event.preventDefault();
|
|
180
176
|
send("DROPZONE.CLICK");
|
|
@@ -273,8 +269,8 @@ function connect(state, send, normalize) {
|
|
|
273
269
|
"data-disabled": dataAttr(disabled)
|
|
274
270
|
});
|
|
275
271
|
},
|
|
276
|
-
getItemProps(props
|
|
277
|
-
const { file } = props
|
|
272
|
+
getItemProps(props) {
|
|
273
|
+
const { file } = props;
|
|
278
274
|
return normalize.element({
|
|
279
275
|
...parts.item.attrs,
|
|
280
276
|
"dir": state.context.dir,
|
|
@@ -282,8 +278,8 @@ function connect(state, send, normalize) {
|
|
|
282
278
|
"data-disabled": dataAttr(disabled)
|
|
283
279
|
});
|
|
284
280
|
},
|
|
285
|
-
getItemNameProps(props
|
|
286
|
-
const { file } = props
|
|
281
|
+
getItemNameProps(props) {
|
|
282
|
+
const { file } = props;
|
|
287
283
|
return normalize.element({
|
|
288
284
|
...parts.itemName.attrs,
|
|
289
285
|
"dir": state.context.dir,
|
|
@@ -291,8 +287,8 @@ function connect(state, send, normalize) {
|
|
|
291
287
|
"data-disabled": dataAttr(disabled)
|
|
292
288
|
});
|
|
293
289
|
},
|
|
294
|
-
getItemSizeTextProps(props
|
|
295
|
-
const { file } = props
|
|
290
|
+
getItemSizeTextProps(props) {
|
|
291
|
+
const { file } = props;
|
|
296
292
|
return normalize.element({
|
|
297
293
|
...parts.itemSizeText.attrs,
|
|
298
294
|
"dir": state.context.dir,
|
|
@@ -300,8 +296,8 @@ function connect(state, send, normalize) {
|
|
|
300
296
|
"data-disabled": dataAttr(disabled)
|
|
301
297
|
});
|
|
302
298
|
},
|
|
303
|
-
getItemPreviewProps(props
|
|
304
|
-
const { file } = props
|
|
299
|
+
getItemPreviewProps(props) {
|
|
300
|
+
const { file } = props;
|
|
305
301
|
return normalize.element({
|
|
306
302
|
...parts.itemPreview.attrs,
|
|
307
303
|
"dir": state.context.dir,
|
|
@@ -309,8 +305,8 @@ function connect(state, send, normalize) {
|
|
|
309
305
|
"data-disabled": dataAttr(disabled)
|
|
310
306
|
});
|
|
311
307
|
},
|
|
312
|
-
getItemPreviewImageProps(props
|
|
313
|
-
const { file, url } = props
|
|
308
|
+
getItemPreviewImageProps(props) {
|
|
309
|
+
const { file, url } = props;
|
|
314
310
|
if (!file.type.startsWith("image/")) throw new Error("Preview Image is only supported for image files");
|
|
315
311
|
return normalize.img({
|
|
316
312
|
...parts.itemPreviewImage.attrs,
|
|
@@ -319,8 +315,8 @@ function connect(state, send, normalize) {
|
|
|
319
315
|
"data-disabled": dataAttr(disabled)
|
|
320
316
|
});
|
|
321
317
|
},
|
|
322
|
-
getItemDeleteTriggerProps(props
|
|
323
|
-
const { file } = props
|
|
318
|
+
getItemDeleteTriggerProps(props) {
|
|
319
|
+
const { file } = props;
|
|
324
320
|
return normalize.button({
|
|
325
321
|
...parts.itemDeleteTrigger.attrs,
|
|
326
322
|
"dir": state.context.dir,
|
|
@@ -363,7 +359,6 @@ function connect(state, send, normalize) {
|
|
|
363
359
|
}
|
|
364
360
|
};
|
|
365
361
|
}
|
|
366
|
-
|
|
367
362
|
//#endregion
|
|
368
363
|
//#region src/machine.ts
|
|
369
364
|
const invoke = {
|
|
@@ -412,8 +407,8 @@ function machine(userContext) {
|
|
|
412
407
|
}
|
|
413
408
|
},
|
|
414
409
|
computed: {
|
|
415
|
-
acceptAttr: (ctx
|
|
416
|
-
multiple: (ctx
|
|
410
|
+
acceptAttr: (ctx) => getAcceptAttrString(ctx.accept),
|
|
411
|
+
multiple: (ctx) => ctx.maxFiles > 1
|
|
417
412
|
},
|
|
418
413
|
watch: { acceptedFiles: ["syncInputElement"] },
|
|
419
414
|
on: {
|
|
@@ -445,72 +440,71 @@ function machine(userContext) {
|
|
|
445
440
|
} }
|
|
446
441
|
}
|
|
447
442
|
}, {
|
|
448
|
-
activities: { preventDocumentDrop(ctx
|
|
449
|
-
if (!ctx
|
|
450
|
-
if (!ctx
|
|
451
|
-
if (ctx
|
|
452
|
-
const doc = dom.getDoc(ctx
|
|
443
|
+
activities: { preventDocumentDrop(ctx) {
|
|
444
|
+
if (!ctx.preventDocumentDrop) return;
|
|
445
|
+
if (!ctx.allowDrop) return;
|
|
446
|
+
if (ctx.disabled) return;
|
|
447
|
+
const doc = dom.getDoc(ctx);
|
|
453
448
|
const onDragOver = (event) => {
|
|
454
449
|
event?.preventDefault();
|
|
455
450
|
};
|
|
456
451
|
const onDrop = (event) => {
|
|
457
|
-
if (contains(dom.getRootEl(ctx
|
|
452
|
+
if (contains(dom.getRootEl(ctx), getEventTarget(event))) return;
|
|
458
453
|
event.preventDefault();
|
|
459
454
|
};
|
|
460
455
|
return callAll(addDomEvent(doc, "dragover", onDragOver, false), addDomEvent(doc, "drop", onDrop, false));
|
|
461
456
|
} },
|
|
462
457
|
actions: {
|
|
463
|
-
syncInputElement(ctx
|
|
458
|
+
syncInputElement(ctx) {
|
|
464
459
|
queueMicrotask(() => {
|
|
465
|
-
const inputEl = dom.getHiddenInputEl(ctx
|
|
460
|
+
const inputEl = dom.getHiddenInputEl(ctx);
|
|
466
461
|
if (!inputEl) return;
|
|
467
|
-
const win = dom.getWin(ctx
|
|
462
|
+
const win = dom.getWin(ctx);
|
|
468
463
|
const dataTransfer = new win.DataTransfer();
|
|
469
|
-
ctx
|
|
464
|
+
ctx.acceptedFiles.forEach((v) => {
|
|
470
465
|
dataTransfer.items.add(v);
|
|
471
466
|
});
|
|
472
467
|
inputEl.files = dataTransfer.files;
|
|
473
468
|
inputEl.dispatchEvent(new win.Event("change", { bubbles: true }));
|
|
474
469
|
});
|
|
475
470
|
},
|
|
476
|
-
openFilePicker(ctx
|
|
471
|
+
openFilePicker(ctx) {
|
|
477
472
|
raf(() => {
|
|
478
|
-
dom.getHiddenInputEl(ctx
|
|
473
|
+
dom.getHiddenInputEl(ctx)?.click();
|
|
479
474
|
});
|
|
480
475
|
},
|
|
481
|
-
setFilesFromEvent(ctx
|
|
482
|
-
const { acceptedFiles, rejectedFiles } = getFilesFromEvent(ctx
|
|
483
|
-
if (ctx
|
|
484
|
-
const files = ref([...ctx
|
|
485
|
-
set.files(ctx
|
|
476
|
+
setFilesFromEvent(ctx, evt) {
|
|
477
|
+
const { acceptedFiles, rejectedFiles } = getFilesFromEvent(ctx, evt.files);
|
|
478
|
+
if (ctx.multiple) {
|
|
479
|
+
const files = ref([...ctx.acceptedFiles, ...acceptedFiles]);
|
|
480
|
+
set.files(ctx, files, rejectedFiles);
|
|
486
481
|
return;
|
|
487
482
|
}
|
|
488
483
|
if (acceptedFiles.length) {
|
|
489
484
|
const files = ref([acceptedFiles[0]]);
|
|
490
|
-
set.files(ctx
|
|
491
|
-
} else if (rejectedFiles.length) set.files(ctx
|
|
485
|
+
set.files(ctx, files, rejectedFiles);
|
|
486
|
+
} else if (rejectedFiles.length) set.files(ctx, ctx.acceptedFiles, rejectedFiles);
|
|
492
487
|
},
|
|
493
|
-
removeFile(ctx
|
|
494
|
-
const files = Array.from(ctx
|
|
495
|
-
const rejectedFiles = Array.from(ctx
|
|
496
|
-
ctx
|
|
497
|
-
ctx
|
|
498
|
-
invoke.change(ctx
|
|
488
|
+
removeFile(ctx, evt) {
|
|
489
|
+
const files = Array.from(ctx.acceptedFiles.filter((file) => file !== evt.file));
|
|
490
|
+
const rejectedFiles = Array.from(ctx.rejectedFiles.filter((item) => item.file !== evt.file));
|
|
491
|
+
ctx.acceptedFiles = ref(files);
|
|
492
|
+
ctx.rejectedFiles = ref(rejectedFiles);
|
|
493
|
+
invoke.change(ctx);
|
|
499
494
|
},
|
|
500
|
-
clearRejectedFiles(ctx
|
|
501
|
-
ctx
|
|
502
|
-
invoke.change(ctx
|
|
495
|
+
clearRejectedFiles(ctx) {
|
|
496
|
+
ctx.rejectedFiles = ref([]);
|
|
497
|
+
invoke.change(ctx);
|
|
503
498
|
},
|
|
504
|
-
clearFiles(ctx
|
|
505
|
-
ctx
|
|
506
|
-
ctx
|
|
507
|
-
invoke.change(ctx
|
|
499
|
+
clearFiles(ctx) {
|
|
500
|
+
ctx.acceptedFiles = ref([]);
|
|
501
|
+
ctx.rejectedFiles = ref([]);
|
|
502
|
+
invoke.change(ctx);
|
|
508
503
|
}
|
|
509
504
|
},
|
|
510
505
|
compareFns: { acceptedFiles: (a, b) => a.length === b.length && a.every((file, i) => isFileEqual(file, b[i])) }
|
|
511
506
|
});
|
|
512
507
|
}
|
|
513
|
-
|
|
514
508
|
//#endregion
|
|
515
509
|
//#region src/props.ts
|
|
516
510
|
const props = createProps()([
|
|
@@ -540,6 +534,5 @@ const props = createProps()([
|
|
|
540
534
|
const splitProps = createSplitProps(props);
|
|
541
535
|
const itemProps = createProps()(["file"]);
|
|
542
536
|
const splitItemProps = createSplitProps(itemProps);
|
|
543
|
-
|
|
544
537
|
//#endregion
|
|
545
|
-
export { anatomy, connect, itemProps, machine, props, splitItemProps, splitProps };
|
|
538
|
+
export { anatomy, connect, itemProps, machine, props, splitItemProps, splitProps };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@destyler/file-upload",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.12",
|
|
5
5
|
"description": "a component that is used to upload multiple files.",
|
|
6
6
|
"author": "Elone Hoo <elonehoo@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@destyler/anatomy": "0.2.
|
|
26
|
-
"@destyler/dom": "0.2.
|
|
27
|
-
"@destyler/file": "0.2.
|
|
28
|
-
"@destyler/
|
|
29
|
-
"@destyler/
|
|
30
|
-
"@destyler/
|
|
31
|
-
"@destyler/
|
|
25
|
+
"@destyler/anatomy": "0.2.12",
|
|
26
|
+
"@destyler/dom": "0.2.12",
|
|
27
|
+
"@destyler/file": "0.2.12",
|
|
28
|
+
"@destyler/i18n": "0.2.12",
|
|
29
|
+
"@destyler/types": "0.2.12",
|
|
30
|
+
"@destyler/utils": "0.2.12",
|
|
31
|
+
"@destyler/xstate": "0.2.12"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|