@v-c/upload 0.0.1
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/LICENSE +21 -0
- package/dist/AjaxUploader.cjs +415 -0
- package/dist/AjaxUploader.d.ts +3 -0
- package/dist/AjaxUploader.js +408 -0
- package/dist/Upload.cjs +196 -0
- package/dist/Upload.d.ts +3 -0
- package/dist/Upload.js +190 -0
- package/dist/_virtual/rolldown_runtime.cjs +21 -0
- package/dist/attrAccept.cjs +34 -0
- package/dist/attrAccept.d.ts +2 -0
- package/dist/attrAccept.js +29 -0
- package/dist/index.cjs +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/interface.cjs +1 -0
- package/dist/interface.d.ts +82 -0
- package/dist/interface.js +0 -0
- package/dist/request.cjs +60 -0
- package/dist/request.d.ts +4 -0
- package/dist/request.js +56 -0
- package/dist/traverseFileTree.cjs +55 -0
- package/dist/traverseFileTree.d.ts +17 -0
- package/dist/traverseFileTree.js +51 -0
- package/dist/uid.cjs +10 -0
- package/dist/uid.d.ts +1 -0
- package/dist/uid.js +6 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 antdv-community
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
const require_rolldown_runtime = require("./_virtual/rolldown_runtime.cjs");
|
|
6
|
+
const require_attrAccept = require("./attrAccept.cjs");
|
|
7
|
+
const require_request = require("./request.cjs");
|
|
8
|
+
const require_traverseFileTree = require("./traverseFileTree.cjs");
|
|
9
|
+
const require_uid = require("./uid.cjs");
|
|
10
|
+
let vue = require("vue");
|
|
11
|
+
let _v_c_util = require("@v-c/util");
|
|
12
|
+
let _v_c_util_dist_pickAttrs = require("@v-c/util/dist/pickAttrs");
|
|
13
|
+
_v_c_util_dist_pickAttrs = require_rolldown_runtime.__toESM(_v_c_util_dist_pickAttrs);
|
|
14
|
+
function noop() {}
|
|
15
|
+
var AjaxUploader = /* @__PURE__ */ (0, vue.defineComponent)((props, { attrs, expose, slots }) => {
|
|
16
|
+
const uid$1 = (0, vue.ref)(require_uid.default());
|
|
17
|
+
let _isMounted = false;
|
|
18
|
+
const reqs = {};
|
|
19
|
+
const fileInputRef = (0, vue.ref)();
|
|
20
|
+
const cls = (0, vue.computed)(() => {
|
|
21
|
+
const { prefixCls, disabled, className } = props;
|
|
22
|
+
return (0, _v_c_util.clsx)({
|
|
23
|
+
[prefixCls]: true,
|
|
24
|
+
[`${prefixCls}-disabled`]: disabled,
|
|
25
|
+
[className]: className
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
const filterFile = (file, force = false) => {
|
|
29
|
+
const { accept, directory } = props;
|
|
30
|
+
let filterFn;
|
|
31
|
+
let acceptFormat;
|
|
32
|
+
if (typeof accept === "string") acceptFormat = accept;
|
|
33
|
+
else {
|
|
34
|
+
const { filter, format } = accept || {};
|
|
35
|
+
acceptFormat = format;
|
|
36
|
+
if (filter === "native") filterFn = () => true;
|
|
37
|
+
else filterFn = filter;
|
|
38
|
+
}
|
|
39
|
+
return (filterFn || (directory || force ? (currentFile) => require_attrAccept.default(currentFile, acceptFormat) : () => true))(file);
|
|
40
|
+
};
|
|
41
|
+
const onClick = (event) => {
|
|
42
|
+
if (!fileInputRef.value) return;
|
|
43
|
+
const target = event.target;
|
|
44
|
+
if (target && target.tagName === "BUTTON") {
|
|
45
|
+
fileInputRef.value.parentNode.focus();
|
|
46
|
+
target.blur();
|
|
47
|
+
}
|
|
48
|
+
fileInputRef.value.click();
|
|
49
|
+
if (props.onClick) props.onClick(event);
|
|
50
|
+
};
|
|
51
|
+
const onKeyDown = (e) => {
|
|
52
|
+
if (e.key === "Enter") onClick(e);
|
|
53
|
+
};
|
|
54
|
+
const post = ({ data, origin, action, parsedFile }) => {
|
|
55
|
+
if (!_isMounted) return;
|
|
56
|
+
const { onStart, customRequest, name, headers, withCredentials, method } = props;
|
|
57
|
+
const { uid: uid$2 } = origin;
|
|
58
|
+
const request = customRequest || require_request.default;
|
|
59
|
+
const requestOption = {
|
|
60
|
+
action,
|
|
61
|
+
filename: name,
|
|
62
|
+
data,
|
|
63
|
+
file: parsedFile,
|
|
64
|
+
headers,
|
|
65
|
+
withCredentials,
|
|
66
|
+
method: method || "post",
|
|
67
|
+
onProgress: (e) => {
|
|
68
|
+
const { onProgress } = props;
|
|
69
|
+
onProgress?.(e, parsedFile);
|
|
70
|
+
},
|
|
71
|
+
onSuccess: (ret, xhr) => {
|
|
72
|
+
const { onSuccess } = props;
|
|
73
|
+
onSuccess?.(ret, parsedFile, xhr);
|
|
74
|
+
delete reqs[uid$2];
|
|
75
|
+
},
|
|
76
|
+
onError: (err, ret) => {
|
|
77
|
+
const { onError } = props;
|
|
78
|
+
onError?.(err, ret, parsedFile);
|
|
79
|
+
delete reqs[uid$2];
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
onStart?.(origin);
|
|
83
|
+
reqs[uid$2] = request(requestOption, { defaultRequest: require_request.default });
|
|
84
|
+
};
|
|
85
|
+
const processFile = async (file, fileList) => {
|
|
86
|
+
const { beforeUpload } = props;
|
|
87
|
+
let transformedFile = file;
|
|
88
|
+
if (beforeUpload) {
|
|
89
|
+
try {
|
|
90
|
+
transformedFile = await beforeUpload(file, fileList);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
transformedFile = false;
|
|
93
|
+
}
|
|
94
|
+
if (transformedFile === false) return {
|
|
95
|
+
origin: file,
|
|
96
|
+
parsedFile: null,
|
|
97
|
+
action: null,
|
|
98
|
+
data: null
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const { action } = props;
|
|
102
|
+
let mergedAction;
|
|
103
|
+
if (typeof action === "function") mergedAction = await action(file);
|
|
104
|
+
else mergedAction = action;
|
|
105
|
+
const { data } = props;
|
|
106
|
+
let mergedData;
|
|
107
|
+
if (typeof data === "function") mergedData = await data(file);
|
|
108
|
+
else mergedData = data;
|
|
109
|
+
const parsedData = (typeof transformedFile === "object" || typeof transformedFile === "string") && transformedFile ? transformedFile : file;
|
|
110
|
+
let parsedFile;
|
|
111
|
+
if (parsedData instanceof File) parsedFile = parsedData;
|
|
112
|
+
else parsedFile = new File([parsedData], file.name, { type: file.type });
|
|
113
|
+
const mergedParsedFile = parsedFile;
|
|
114
|
+
mergedParsedFile.uid = file.uid;
|
|
115
|
+
return {
|
|
116
|
+
origin: file,
|
|
117
|
+
data: mergedData,
|
|
118
|
+
parsedFile: mergedParsedFile,
|
|
119
|
+
action: mergedAction
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
const uploadFiles = (files) => {
|
|
123
|
+
const originFiles = [...files];
|
|
124
|
+
const postFiles = originFiles.map((file) => {
|
|
125
|
+
file.uid = require_uid.default();
|
|
126
|
+
return processFile(file, originFiles);
|
|
127
|
+
});
|
|
128
|
+
Promise.all(postFiles).then((fileList) => {
|
|
129
|
+
const { onBatchStart } = props;
|
|
130
|
+
onBatchStart?.(fileList.map(({ origin, parsedFile }) => ({
|
|
131
|
+
file: origin,
|
|
132
|
+
parsedFile
|
|
133
|
+
})));
|
|
134
|
+
fileList.filter((file) => file.parsedFile !== null).forEach((file) => {
|
|
135
|
+
post(file);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
const onDataTransferFiles = async (dataTransfer, existFileCallback) => {
|
|
140
|
+
if (!dataTransfer) return;
|
|
141
|
+
const { multiple, directory } = props;
|
|
142
|
+
const items = [...dataTransfer.items || []];
|
|
143
|
+
let files = [...dataTransfer.files || []];
|
|
144
|
+
if (files.length > 0 || items.some((item) => item.kind === "file")) existFileCallback?.();
|
|
145
|
+
if (directory) {
|
|
146
|
+
files = await require_traverseFileTree.default(Array.prototype.slice.call(items), (currentFile) => filterFile(currentFile));
|
|
147
|
+
uploadFiles(files);
|
|
148
|
+
} else {
|
|
149
|
+
let acceptFiles = [...files].filter((file) => filterFile(file, true));
|
|
150
|
+
if (multiple === false) acceptFiles = files.slice(0, 1);
|
|
151
|
+
uploadFiles(acceptFiles);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const onFileDrop = (e) => {
|
|
155
|
+
e.preventDefault();
|
|
156
|
+
if (e.type === "drop") {
|
|
157
|
+
const dataTransfer = e.dataTransfer;
|
|
158
|
+
return onDataTransferFiles(dataTransfer);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const onFileDragOver = (e) => {
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
};
|
|
164
|
+
const reset = () => {
|
|
165
|
+
uid$1.value = require_uid.default();
|
|
166
|
+
};
|
|
167
|
+
const onChange = (e) => {
|
|
168
|
+
const { files } = e.target;
|
|
169
|
+
uploadFiles([...files || []].filter((file) => filterFile(file)));
|
|
170
|
+
reset();
|
|
171
|
+
};
|
|
172
|
+
const dirProps = (0, vue.computed)(() => {
|
|
173
|
+
return props.directory ? {
|
|
174
|
+
directory: "directory",
|
|
175
|
+
webkitdirectory: "webkitdirectory"
|
|
176
|
+
} : {};
|
|
177
|
+
});
|
|
178
|
+
const abort = (file) => {
|
|
179
|
+
if (file) {
|
|
180
|
+
const uid$2 = file.uid ? file.uid : file;
|
|
181
|
+
if (reqs[uid$2] && reqs[uid$2].abort) reqs[uid$2].abort();
|
|
182
|
+
delete reqs[uid$2];
|
|
183
|
+
} else Object.keys(reqs).forEach((uid$2) => {
|
|
184
|
+
if (reqs[uid$2] && reqs[uid$2].abort) reqs[uid$2].abort();
|
|
185
|
+
delete reqs[uid$2];
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
const events = (0, vue.computed)(() => {
|
|
189
|
+
return props.disabled ? {} : {
|
|
190
|
+
onClick: props.openFileDialogOnClick ? onClick : noop,
|
|
191
|
+
onKeyDown: props.openFileDialogOnClick ? onKeyDown : noop,
|
|
192
|
+
onMouseEnter: props.onMouseEnter,
|
|
193
|
+
onMouseLeave: props.onMouseLeave,
|
|
194
|
+
onDrop: onFileDrop,
|
|
195
|
+
onDragover: onFileDragOver,
|
|
196
|
+
tabIndex: props.hasControlInside ? void 0 : "0"
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
const onFilePaste = async (e) => {
|
|
200
|
+
const { pastable } = props;
|
|
201
|
+
if (!pastable) return;
|
|
202
|
+
if (e.type === "paste") {
|
|
203
|
+
const clipboardData = e.clipboardData;
|
|
204
|
+
return onDataTransferFiles(clipboardData, () => {
|
|
205
|
+
e.preventDefault();
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
let prevPastable;
|
|
210
|
+
(0, vue.onMounted)(() => {
|
|
211
|
+
_isMounted = true;
|
|
212
|
+
if (props.pastable) document.addEventListener("paste", onFilePaste);
|
|
213
|
+
prevPastable = props.pastable;
|
|
214
|
+
});
|
|
215
|
+
(0, vue.onUpdated)(() => {
|
|
216
|
+
const pastable = props.pastable;
|
|
217
|
+
if (pastable && !prevPastable) document.addEventListener("paste", onFilePaste);
|
|
218
|
+
else if (!pastable && prevPastable) document.removeEventListener("paste", onFilePaste);
|
|
219
|
+
prevPastable = pastable;
|
|
220
|
+
});
|
|
221
|
+
(0, vue.onUnmounted)(() => {
|
|
222
|
+
_isMounted = false;
|
|
223
|
+
abort();
|
|
224
|
+
document.removeEventListener("paste", onFilePaste);
|
|
225
|
+
});
|
|
226
|
+
expose({ abort });
|
|
227
|
+
return () => {
|
|
228
|
+
const { component, prefixCls, className, classNames = {}, disabled, id, name, style, styles = {}, multiple, accept, capture, directory, openFileDialogOnClick, onMouseEnter, onMouseLeave, hasControlInside, ...otherProps } = {
|
|
229
|
+
...props,
|
|
230
|
+
...attrs
|
|
231
|
+
};
|
|
232
|
+
const acceptFormat = typeof accept === "string" ? accept : accept?.format;
|
|
233
|
+
return (0, vue.createVNode)(component, (0, vue.mergeProps)({ "class": cls.value }, events.value, {
|
|
234
|
+
"role": hasControlInside ? void 0 : "button",
|
|
235
|
+
"style": style
|
|
236
|
+
}), { default: () => [(0, vue.createVNode)("input", (0, vue.mergeProps)((0, _v_c_util_dist_pickAttrs.default)(otherProps, {
|
|
237
|
+
aria: true,
|
|
238
|
+
data: true
|
|
239
|
+
}), {
|
|
240
|
+
"id": id,
|
|
241
|
+
"name": name,
|
|
242
|
+
"disabled": disabled,
|
|
243
|
+
"type": "file",
|
|
244
|
+
"ref": fileInputRef,
|
|
245
|
+
"onClick": (e) => e.stopPropagation(),
|
|
246
|
+
"key": uid$1.value,
|
|
247
|
+
"style": {
|
|
248
|
+
display: "none",
|
|
249
|
+
...styles.input
|
|
250
|
+
},
|
|
251
|
+
"class": classNames.input
|
|
252
|
+
}, dirProps.value, {
|
|
253
|
+
"multiple": multiple,
|
|
254
|
+
"accept": acceptFormat,
|
|
255
|
+
"onChange": onChange
|
|
256
|
+
}, capture != null ? { capture } : {}), null), slots.default?.()] });
|
|
257
|
+
};
|
|
258
|
+
}, { props: {
|
|
259
|
+
name: {
|
|
260
|
+
type: String,
|
|
261
|
+
required: false,
|
|
262
|
+
default: void 0
|
|
263
|
+
},
|
|
264
|
+
style: {
|
|
265
|
+
type: Object,
|
|
266
|
+
required: false,
|
|
267
|
+
default: void 0
|
|
268
|
+
},
|
|
269
|
+
className: {
|
|
270
|
+
type: String,
|
|
271
|
+
required: false,
|
|
272
|
+
default: void 0
|
|
273
|
+
},
|
|
274
|
+
disabled: {
|
|
275
|
+
type: Boolean,
|
|
276
|
+
required: false,
|
|
277
|
+
default: void 0
|
|
278
|
+
},
|
|
279
|
+
component: {
|
|
280
|
+
type: [
|
|
281
|
+
Object,
|
|
282
|
+
Function,
|
|
283
|
+
String
|
|
284
|
+
],
|
|
285
|
+
required: false,
|
|
286
|
+
default: void 0
|
|
287
|
+
},
|
|
288
|
+
action: {
|
|
289
|
+
type: [String, Function],
|
|
290
|
+
required: false,
|
|
291
|
+
default: void 0
|
|
292
|
+
},
|
|
293
|
+
method: {
|
|
294
|
+
type: String,
|
|
295
|
+
required: false,
|
|
296
|
+
default: void 0
|
|
297
|
+
},
|
|
298
|
+
directory: {
|
|
299
|
+
type: Boolean,
|
|
300
|
+
required: false,
|
|
301
|
+
default: void 0
|
|
302
|
+
},
|
|
303
|
+
data: {
|
|
304
|
+
type: [Object, Function],
|
|
305
|
+
required: false,
|
|
306
|
+
default: void 0
|
|
307
|
+
},
|
|
308
|
+
headers: {
|
|
309
|
+
type: Object,
|
|
310
|
+
required: false,
|
|
311
|
+
default: void 0
|
|
312
|
+
},
|
|
313
|
+
accept: {
|
|
314
|
+
type: [String, Object],
|
|
315
|
+
required: false,
|
|
316
|
+
default: void 0
|
|
317
|
+
},
|
|
318
|
+
multiple: {
|
|
319
|
+
type: Boolean,
|
|
320
|
+
required: false,
|
|
321
|
+
default: void 0
|
|
322
|
+
},
|
|
323
|
+
onBatchStart: {
|
|
324
|
+
type: Function,
|
|
325
|
+
required: false,
|
|
326
|
+
default: void 0
|
|
327
|
+
},
|
|
328
|
+
onStart: {
|
|
329
|
+
type: Function,
|
|
330
|
+
required: false,
|
|
331
|
+
default: void 0
|
|
332
|
+
},
|
|
333
|
+
onError: {
|
|
334
|
+
type: Function,
|
|
335
|
+
required: false,
|
|
336
|
+
default: void 0
|
|
337
|
+
},
|
|
338
|
+
onSuccess: {
|
|
339
|
+
type: Function,
|
|
340
|
+
required: false,
|
|
341
|
+
default: void 0
|
|
342
|
+
},
|
|
343
|
+
onProgress: {
|
|
344
|
+
type: Function,
|
|
345
|
+
required: false,
|
|
346
|
+
default: void 0
|
|
347
|
+
},
|
|
348
|
+
beforeUpload: {
|
|
349
|
+
type: Function,
|
|
350
|
+
required: false,
|
|
351
|
+
default: void 0
|
|
352
|
+
},
|
|
353
|
+
customRequest: {
|
|
354
|
+
type: Function,
|
|
355
|
+
required: false,
|
|
356
|
+
default: void 0
|
|
357
|
+
},
|
|
358
|
+
withCredentials: {
|
|
359
|
+
type: Boolean,
|
|
360
|
+
required: false,
|
|
361
|
+
default: void 0
|
|
362
|
+
},
|
|
363
|
+
openFileDialogOnClick: {
|
|
364
|
+
type: Boolean,
|
|
365
|
+
required: false,
|
|
366
|
+
default: void 0
|
|
367
|
+
},
|
|
368
|
+
prefixCls: {
|
|
369
|
+
type: String,
|
|
370
|
+
required: false,
|
|
371
|
+
default: void 0
|
|
372
|
+
},
|
|
373
|
+
id: {
|
|
374
|
+
type: String,
|
|
375
|
+
required: false,
|
|
376
|
+
default: void 0
|
|
377
|
+
},
|
|
378
|
+
onMouseEnter: {
|
|
379
|
+
type: Function,
|
|
380
|
+
required: false,
|
|
381
|
+
default: void 0
|
|
382
|
+
},
|
|
383
|
+
onMouseLeave: {
|
|
384
|
+
type: Function,
|
|
385
|
+
required: false,
|
|
386
|
+
default: void 0
|
|
387
|
+
},
|
|
388
|
+
onClick: {
|
|
389
|
+
type: Function,
|
|
390
|
+
required: false,
|
|
391
|
+
default: void 0
|
|
392
|
+
},
|
|
393
|
+
classNames: {
|
|
394
|
+
type: Object,
|
|
395
|
+
required: false,
|
|
396
|
+
default: void 0
|
|
397
|
+
},
|
|
398
|
+
styles: {
|
|
399
|
+
type: Object,
|
|
400
|
+
required: false,
|
|
401
|
+
default: void 0
|
|
402
|
+
},
|
|
403
|
+
hasControlInside: {
|
|
404
|
+
type: Boolean,
|
|
405
|
+
required: false,
|
|
406
|
+
default: void 0
|
|
407
|
+
},
|
|
408
|
+
pastable: {
|
|
409
|
+
type: Boolean,
|
|
410
|
+
required: false,
|
|
411
|
+
default: void 0
|
|
412
|
+
}
|
|
413
|
+
} });
|
|
414
|
+
var AjaxUploader_default = AjaxUploader;
|
|
415
|
+
exports.default = AjaxUploader_default;
|