|
35 | 35 | export let inputElement = undefined; |
36 | 36 | export let required = false; |
37 | 37 | export let dropText = "Drag and drop some files here, or click to select files"; |
| 38 | + export let fileLimit = 5; |
38 | 39 |
|
39 | 40 | let innerDropText = dropText; |
| 41 | + let innerDisabled = disabled; |
40 | 42 | let isLoading = false; |
41 | 43 | let isError = false; |
42 | 44 | let isSuccess = false; |
|
48 | 50 | $: { |
49 | 51 | if (isLoading) { |
50 | 52 | innerDropText = "Uploading..."; |
51 | | - disabled = true; |
| 53 | + innerDisabled = true; |
52 | 54 | } else if (isSuccess) { |
53 | 55 | innerDropText = defaultSuccessMesssage; |
54 | | - disabled = true; |
| 56 | + innerDisabled = true; |
55 | 57 | } else if (isError) { |
56 | 58 | innerDropText = reason; |
57 | | - disabled = true; |
| 59 | + innerDisabled = true; |
58 | 60 | } else { |
59 | 61 | innerDropText = dropText; |
60 | | - disabled = false; |
| 62 | + innerDisabled = disabled; |
61 | 63 | } |
62 | 64 | } |
63 | 65 |
|
|
225 | 227 | } |
226 | 228 |
|
227 | 229 | /** @type {any[]} */ |
228 | | - const acceptedFiles = []; |
| 230 | + let acceptedFiles = []; |
229 | 231 | /** @type {any[]} */ |
230 | | - const fileRejections = []; |
| 232 | + let fileRejections = []; |
231 | 233 |
|
232 | 234 | /** @type {Promise<any>[]} */ |
233 | 235 | const promises = []; |
|
236 | 238 | promises.push(new Promise((resolve, reject) => { |
237 | 239 | const [accepted, acceptError] = fileAccepted(file, accept); |
238 | 240 | const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize); |
239 | | - getBase64(file).then(res => { |
| 241 | + getBase64(file).then(data => { |
240 | 242 | if (accepted && sizeMatch) { |
241 | 243 | acceptedFiles.push({ |
242 | 244 | file_name: file.name, |
243 | 245 | file_path: file.path, |
244 | | - url: res |
| 246 | + file_data: data, |
| 247 | + content_type: file.type, |
| 248 | + file_size: file.size |
245 | 249 | }); |
246 | 250 | } else { |
247 | 251 | const errors = [acceptError, sizeError].filter(Boolean); |
|
254 | 258 |
|
255 | 259 | Promise.all(promises).then(() => { |
256 | 260 | isLoading = false; |
| 261 | + if (acceptedFiles.length >= fileLimit) { |
| 262 | + acceptedFiles = acceptedFiles.slice(0, Math.max(fileLimit, 0)); |
| 263 | + } |
257 | 264 |
|
258 | 265 | if (!multiple && acceptedFiles.length > 1) { |
259 | 266 | // Reject everything and empty accepted files |
|
310 | 317 | resetState(); |
311 | 318 | } |
312 | 319 | |
313 | | - $: composeHandler = (/** @type {any} */ fn) => (disabled ? null : fn); |
| 320 | + $: composeHandler = (/** @type {any} */ fn) => (innerDisabled ? null : fn); |
314 | 321 | |
315 | 322 | $: composeKeyboardHandler = (/** @type {any} */ fn) => (noKeyboard ? null : composeHandler(fn)); |
316 | 323 | |
|
0 commit comments