@c2n/upload 0.0.9
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/README.md +27 -0
- package/dist/upload.js +482 -0
- package/package.json +77 -0
- package/react.d.ts +24 -0
- package/react.js +3 -0
- package/types/src/upload.d.ts +210 -0
- package/types/src/upload.d.ts.map +1 -0
- package/vue.d.ts +45 -0
- package/vue.js +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nguyen Thai Vinh
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Upload
|
|
2
|
+
|
|
3
|
+
`@c2n/upload` is a Lit web component for browsing or dropping files, validating them, uploading one or many files, and presenting the queue with `c2-attachment`.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @c2n/upload
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```html
|
|
10
|
+
<script type="module" src="/node_modules/@c2n/upload/dist/upload.js"></script>
|
|
11
|
+
|
|
12
|
+
<c2-upload id="documents" name="documents" multiple accept=".pdf,image/*" max-files="5" max-size="10485760"></c2-upload>
|
|
13
|
+
|
|
14
|
+
<script type="module">
|
|
15
|
+
const upload = document.querySelector('#documents')
|
|
16
|
+
upload.uploadHandler = async (file, { signal, onProgress }) => {
|
|
17
|
+
// Connect XMLHttpRequest, an SDK, or another transport here.
|
|
18
|
+
// Call onProgress(0–100), resolve with the server result, and honor signal for cancellation.
|
|
19
|
+
onProgress(50)
|
|
20
|
+
return { name: file.name }
|
|
21
|
+
}
|
|
22
|
+
</script>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Set `auto-upload="false"` to prepare the queue and call `upload()` later. Failed handlers render the attachment retry action. Removing an active item aborts its signal. The form-associated element contributes every selected file under `name` and supports `required` validation.
|
|
26
|
+
|
|
27
|
+
Single-file uploaders hide the picker once a file is accepted and restore it after removal. Multiple uploaders remain open until `max-files` is reached and report the remaining capacity. Use `variant="compact"` for a button-only picker without drag-and-drop.
|
package/dist/upload.js
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { LitElement, html, nothing, svg, unsafeCSS } from "lit";
|
|
2
|
+
import { property, query, state } from "lit/decorators.js";
|
|
3
|
+
import { classMap } from "lit/directives/class-map.js";
|
|
4
|
+
import { customElement } from "@c2n/core/element-helper.js";
|
|
5
|
+
import "@c2n/attachment";
|
|
6
|
+
import "@c2n/button";
|
|
7
|
+
//#region src/upload.scss?inline
|
|
8
|
+
var upload_default = "/* ex : var((width: 24px), width, c2-checkbox) returns var(--c2-checkbox-width, 24px) */\n:host {\n display: block;\n width: min(100%, var(--c2-upload--width,420px));\n}\n\n:host([hidden]) {\n display: none;\n}\n\n.dropzone {\n box-sizing: border-box;\n display: flex;\n width: 100%;\n padding: var(--c2-upload__dropzone--padding,28px 20px);\n flex-direction: column;\n align-items: center;\n justify-content: center;\n gap: var(--c2-upload__dropzone--gap,8px);\n border: var(--c2-upload__dropzone--border,1px dashed #bcbcc6);\n border-radius: var(--c2-upload__dropzone--border-radius,14px);\n background: var(--c2-upload__dropzone--background,#ffffff);\n text-align: center;\n cursor: pointer;\n transition: background 250ms cubic-bezier(0.2, 0, 0, 1), border 250ms cubic-bezier(0.2, 0, 0, 1);\n}\n.dropzone:hover:not([aria-disabled=true]) {\n border: var(--c2-upload__dropzone__hover--border,1px dashed #a1a1aa);\n background: var(--c2-upload__dropzone__hover--background,#fafafa);\n}\n.dropzone:focus-visible {\n outline: var(--c2-upload__dropzone__focus--outline,2px solid rgba(2, 101, 220, 0.4));\n outline-offset: var(--c2-upload__dropzone__focus--outline-offset,2px);\n}\n.dropzone[aria-disabled=true] {\n cursor: default;\n opacity: var(--c2-upload__dropzone__disabled--opacity,0.38);\n}\n\n.dropzone--dragging,\n.dropzone--dragging:hover {\n border: var(--c2-upload__dropzone__drag--border,1px dashed rgb(2, 101, 220));\n background: var(--c2-upload__dropzone__drag--background,#e8f2ff);\n}\n\ninput {\n position: absolute;\n width: 1px;\n height: 1px;\n margin: -1px;\n padding: 0;\n overflow: hidden;\n clip: rect(0, 0, 0, 0);\n white-space: nowrap;\n border: 0;\n}\n\n.upload-icon,\n::slotted([slot=drop-icon]) {\n display: block;\n width: var(--c2-upload__icon--size,32px);\n height: var(--c2-upload__icon--size,32px);\n color: var(--c2-upload__icon--color,rgb(2, 101, 220));\n --c2-feather-icon--size: var(--c2-upload__icon--size,32px);\n --c2-phosphor-icon--size: var(--c2-upload__icon--size,32px);\n --c2-mat-icon--font-size: var(--c2-upload__icon--size,32px);\n}\n\n.compact-button {\n vertical-align: top;\n}\n\n.compact-icon {\n display: inline-flex;\n width: 18px;\n height: 18px;\n flex-shrink: 0;\n align-items: center;\n justify-content: center;\n}\n.compact-icon .upload-icon,\n.compact-icon ::slotted([slot=drop-icon]) {\n width: 100%;\n height: 100%;\n}\n\n.prompt {\n color: var(--c2-upload__prompt--color,#18181b);\n font-size: var(--c2-upload__prompt--font-size,14px);\n font-weight: var(--c2-upload__prompt--font-weight,600);\n line-height: 1.4;\n}\n\n.action {\n color: var(--c2-upload__action--color,rgb(2, 101, 220));\n}\n\n.hint {\n color: var(--c2-upload__hint--color,#71717a);\n font-size: var(--c2-upload__hint--font-size,12px);\n line-height: 1.4;\n}\n\n.list {\n display: grid;\n gap: var(--c2-upload__list--gap,10px);\n margin-top: var(--c2-upload__list--margin-top,12px);\n}\n\n.list--standalone {\n margin-top: 0;\n}\n\n.errors {\n display: grid;\n gap: var(--c2-upload__error--gap,4px);\n margin-top: var(--c2-upload__list--margin-top,12px);\n color: var(--c2-upload__error--color,#dc2626);\n font-size: var(--c2-upload__error--font-size,12px);\n line-height: 1.4;\n}";
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/decorate.js
|
|
11
|
+
function __decorate(decorators, target, key, desc) {
|
|
12
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
13
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
14
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
15
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/upload.ts
|
|
19
|
+
var uploadItemId = 0;
|
|
20
|
+
var Upload = class Upload extends LitElement {
|
|
21
|
+
constructor(..._args) {
|
|
22
|
+
super(..._args);
|
|
23
|
+
this.internals = this.attachInternals();
|
|
24
|
+
this.controllers = /* @__PURE__ */ new Map();
|
|
25
|
+
this.dragDepth = 0;
|
|
26
|
+
this.customValidityMessage = "";
|
|
27
|
+
this.uploadItems = [];
|
|
28
|
+
this.rejections = [];
|
|
29
|
+
this.dragging = false;
|
|
30
|
+
this.disabledByForm = false;
|
|
31
|
+
this.multiple = false;
|
|
32
|
+
this.variant = "dropzone";
|
|
33
|
+
this.accept = "";
|
|
34
|
+
this.maxFiles = 0;
|
|
35
|
+
this.maxSize = 0;
|
|
36
|
+
this.autoUpload = true;
|
|
37
|
+
this.disabled = false;
|
|
38
|
+
this.required = false;
|
|
39
|
+
this.name = "";
|
|
40
|
+
this.ariaLabel = null;
|
|
41
|
+
}
|
|
42
|
+
static {
|
|
43
|
+
this.formAssociated = true;
|
|
44
|
+
}
|
|
45
|
+
static {
|
|
46
|
+
this.styles = unsafeCSS(upload_default);
|
|
47
|
+
}
|
|
48
|
+
/** Current queue, including ready, uploading, complete and failed items. */
|
|
49
|
+
get items() {
|
|
50
|
+
return this.uploadItems;
|
|
51
|
+
}
|
|
52
|
+
/** Selected files. Assign a new array to replace the queue. */
|
|
53
|
+
get value() {
|
|
54
|
+
return this.uploadItems.map((item) => item.file);
|
|
55
|
+
}
|
|
56
|
+
set value(files) {
|
|
57
|
+
this.resetQueue(false);
|
|
58
|
+
if (files.length) this.addFiles(files);
|
|
59
|
+
}
|
|
60
|
+
get form() {
|
|
61
|
+
return this.internals.form;
|
|
62
|
+
}
|
|
63
|
+
get labels() {
|
|
64
|
+
return this.internals.labels;
|
|
65
|
+
}
|
|
66
|
+
get validity() {
|
|
67
|
+
return this.internals.validity;
|
|
68
|
+
}
|
|
69
|
+
get validationMessage() {
|
|
70
|
+
return this.internals.validationMessage;
|
|
71
|
+
}
|
|
72
|
+
get willValidate() {
|
|
73
|
+
return this.internals.willValidate;
|
|
74
|
+
}
|
|
75
|
+
disconnectedCallback() {
|
|
76
|
+
super.disconnectedCallback();
|
|
77
|
+
for (const controller of this.controllers.values()) controller.abort();
|
|
78
|
+
for (const item of this.uploadItems) this.revokePreview(item);
|
|
79
|
+
}
|
|
80
|
+
formResetCallback() {
|
|
81
|
+
this.resetQueue(false);
|
|
82
|
+
}
|
|
83
|
+
formDisabledCallback(disabled) {
|
|
84
|
+
this.disabledByForm = disabled;
|
|
85
|
+
}
|
|
86
|
+
checkValidity() {
|
|
87
|
+
return this.internals.checkValidity();
|
|
88
|
+
}
|
|
89
|
+
reportValidity() {
|
|
90
|
+
return this.internals.reportValidity();
|
|
91
|
+
}
|
|
92
|
+
setCustomValidity(message) {
|
|
93
|
+
this.customValidityMessage = message;
|
|
94
|
+
this.syncFormState();
|
|
95
|
+
}
|
|
96
|
+
updated() {
|
|
97
|
+
this.syncFormState();
|
|
98
|
+
}
|
|
99
|
+
get effectiveDisabled() {
|
|
100
|
+
return this.disabled || this.disabledByForm;
|
|
101
|
+
}
|
|
102
|
+
get remainingCapacity() {
|
|
103
|
+
return this.maxFiles > 0 ? Math.max(0, this.maxFiles - this.uploadItems.length) : void 0;
|
|
104
|
+
}
|
|
105
|
+
get canAddMore() {
|
|
106
|
+
if (!this.multiple) return this.uploadItems.length === 0;
|
|
107
|
+
return this.remainingCapacity === void 0 || this.remainingCapacity > 0;
|
|
108
|
+
}
|
|
109
|
+
get pickerDisabled() {
|
|
110
|
+
return this.effectiveDisabled || !this.canAddMore;
|
|
111
|
+
}
|
|
112
|
+
/** Opens the native file browser. */
|
|
113
|
+
browse() {
|
|
114
|
+
if (!this.pickerDisabled) this.input.click();
|
|
115
|
+
}
|
|
116
|
+
/** Validates and adds files supplied by the browser, drag-and-drop or application code. */
|
|
117
|
+
addFiles(files) {
|
|
118
|
+
if (this.effectiveDisabled) return [];
|
|
119
|
+
const incoming = [...files];
|
|
120
|
+
const accepted = [];
|
|
121
|
+
const rejected = [];
|
|
122
|
+
const available = this.maxFiles > 0 ? Math.max(0, this.maxFiles - this.uploadItems.length) : Number.POSITIVE_INFINITY;
|
|
123
|
+
incoming.forEach((file, index) => {
|
|
124
|
+
let reason;
|
|
125
|
+
let message = "";
|
|
126
|
+
if (!this.multiple && (this.uploadItems.length > 0 || index > 0)) {
|
|
127
|
+
reason = "multiple";
|
|
128
|
+
message = "Only one file can be selected.";
|
|
129
|
+
} else if (accepted.length >= available) {
|
|
130
|
+
reason = "limit";
|
|
131
|
+
message = `A maximum of ${this.maxFiles} files is allowed.`;
|
|
132
|
+
} else if (!this.accepts(file)) {
|
|
133
|
+
reason = "type";
|
|
134
|
+
message = `${file.name} is not an accepted file type.`;
|
|
135
|
+
} else if (this.maxSize > 0 && file.size > this.maxSize) {
|
|
136
|
+
reason = "size";
|
|
137
|
+
message = `${file.name} exceeds the ${this.formatBytes(this.maxSize)} size limit.`;
|
|
138
|
+
}
|
|
139
|
+
if (reason) rejected.push({
|
|
140
|
+
file,
|
|
141
|
+
reason,
|
|
142
|
+
message
|
|
143
|
+
});
|
|
144
|
+
else accepted.push(this.createItem(file));
|
|
145
|
+
});
|
|
146
|
+
if (accepted.length) {
|
|
147
|
+
this.uploadItems = [...this.uploadItems, ...accepted];
|
|
148
|
+
this.emit("files-selected", {
|
|
149
|
+
files: accepted.map((item) => item.file),
|
|
150
|
+
items: accepted
|
|
151
|
+
});
|
|
152
|
+
this.notifyValueChange();
|
|
153
|
+
if (this.autoUpload && this.uploadHandler) Promise.all(accepted.map((item) => this.runUpload(item.id)));
|
|
154
|
+
}
|
|
155
|
+
this.rejections = rejected;
|
|
156
|
+
if (rejected.length) this.emit("file-reject", { rejections: rejected });
|
|
157
|
+
return accepted;
|
|
158
|
+
}
|
|
159
|
+
/** Upload every ready/error item, or one item by id. */
|
|
160
|
+
async upload(id) {
|
|
161
|
+
if (!this.uploadHandler || this.effectiveDisabled) return;
|
|
162
|
+
const items = this.uploadItems.filter((item) => (!id || item.id === id) && item.status !== "uploading" && item.status !== "complete");
|
|
163
|
+
await Promise.all(items.map((item) => this.runUpload(item.id)));
|
|
164
|
+
}
|
|
165
|
+
/** Retry a failed or ready item. */
|
|
166
|
+
async retry(id) {
|
|
167
|
+
await this.upload(id);
|
|
168
|
+
}
|
|
169
|
+
/** Cancels an active upload and removes its attachment. */
|
|
170
|
+
removeFile(id) {
|
|
171
|
+
const item = this.uploadItems.find((candidate) => candidate.id === id);
|
|
172
|
+
if (!item || this.effectiveDisabled) return;
|
|
173
|
+
this.controllers.get(id)?.abort();
|
|
174
|
+
this.controllers.delete(id);
|
|
175
|
+
this.revokePreview(item);
|
|
176
|
+
this.uploadItems = this.uploadItems.filter((candidate) => candidate.id !== id);
|
|
177
|
+
this.emit("upload-remove", { item });
|
|
178
|
+
this.notifyValueChange();
|
|
179
|
+
}
|
|
180
|
+
/** Cancels and removes every queued file. */
|
|
181
|
+
clear() {
|
|
182
|
+
this.resetQueue(true);
|
|
183
|
+
}
|
|
184
|
+
resetQueue(notify) {
|
|
185
|
+
const removedItems = this.uploadItems;
|
|
186
|
+
for (const controller of this.controllers.values()) controller.abort();
|
|
187
|
+
this.controllers.clear();
|
|
188
|
+
for (const item of this.uploadItems) this.revokePreview(item);
|
|
189
|
+
this.uploadItems = [];
|
|
190
|
+
this.rejections = [];
|
|
191
|
+
if (this.input) this.input.value = "";
|
|
192
|
+
if (notify && removedItems.length) {
|
|
193
|
+
for (const item of removedItems) this.emit("upload-remove", { item });
|
|
194
|
+
this.notifyValueChange();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
notifyValueChange() {
|
|
198
|
+
this.syncFormState();
|
|
199
|
+
this.dispatchEvent(new Event("input", {
|
|
200
|
+
bubbles: true,
|
|
201
|
+
composed: true
|
|
202
|
+
}));
|
|
203
|
+
this.dispatchEvent(new Event("change", {
|
|
204
|
+
bubbles: true,
|
|
205
|
+
composed: true
|
|
206
|
+
}));
|
|
207
|
+
}
|
|
208
|
+
emit(type, detail) {
|
|
209
|
+
const event = new CustomEvent(type, {
|
|
210
|
+
detail,
|
|
211
|
+
bubbles: true,
|
|
212
|
+
composed: true
|
|
213
|
+
});
|
|
214
|
+
this.dispatchEvent(event);
|
|
215
|
+
}
|
|
216
|
+
createItem(file) {
|
|
217
|
+
return {
|
|
218
|
+
id: `upload-${++uploadItemId}`,
|
|
219
|
+
file,
|
|
220
|
+
status: "ready",
|
|
221
|
+
progress: 0,
|
|
222
|
+
previewUrl: file.type.startsWith("image/") && typeof URL.createObjectURL === "function" ? URL.createObjectURL(file) : void 0
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
updateItem(id, update) {
|
|
226
|
+
let updated;
|
|
227
|
+
this.uploadItems = this.uploadItems.map((item) => {
|
|
228
|
+
if (item.id !== id) return item;
|
|
229
|
+
updated = {
|
|
230
|
+
...item,
|
|
231
|
+
...update
|
|
232
|
+
};
|
|
233
|
+
return updated;
|
|
234
|
+
});
|
|
235
|
+
return updated;
|
|
236
|
+
}
|
|
237
|
+
async runUpload(id) {
|
|
238
|
+
const current = this.uploadItems.find((item) => item.id === id);
|
|
239
|
+
if (!current || !this.uploadHandler || this.effectiveDisabled) return;
|
|
240
|
+
this.controllers.get(id)?.abort();
|
|
241
|
+
const controller = new AbortController();
|
|
242
|
+
this.controllers.set(id, controller);
|
|
243
|
+
const started = this.updateItem(id, {
|
|
244
|
+
status: "uploading",
|
|
245
|
+
progress: 0,
|
|
246
|
+
error: void 0
|
|
247
|
+
});
|
|
248
|
+
if (!started) return;
|
|
249
|
+
this.emit("upload-start", { item: started });
|
|
250
|
+
try {
|
|
251
|
+
const result = await this.uploadHandler(current.file, {
|
|
252
|
+
signal: controller.signal,
|
|
253
|
+
onProgress: (progress) => {
|
|
254
|
+
if (controller.signal.aborted) return;
|
|
255
|
+
const safeProgress = Math.min(100, Math.max(0, Number.isFinite(progress) ? progress : 0));
|
|
256
|
+
const item = this.updateItem(id, { progress: safeProgress });
|
|
257
|
+
if (item) this.emit("upload-progress", {
|
|
258
|
+
item,
|
|
259
|
+
progress: safeProgress
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
if (controller.signal.aborted || !this.uploadItems.some((item) => item.id === id)) return;
|
|
264
|
+
const completed = this.updateItem(id, {
|
|
265
|
+
status: "complete",
|
|
266
|
+
progress: 100,
|
|
267
|
+
result,
|
|
268
|
+
error: void 0
|
|
269
|
+
});
|
|
270
|
+
if (completed) this.emit("upload-success", {
|
|
271
|
+
item: completed,
|
|
272
|
+
result
|
|
273
|
+
});
|
|
274
|
+
} catch (error) {
|
|
275
|
+
if (controller.signal.aborted || !this.uploadItems.some((item) => item.id === id)) return;
|
|
276
|
+
const failed = this.updateItem(id, {
|
|
277
|
+
status: "error",
|
|
278
|
+
error
|
|
279
|
+
});
|
|
280
|
+
if (failed) this.emit("upload-error", {
|
|
281
|
+
item: failed,
|
|
282
|
+
error
|
|
283
|
+
});
|
|
284
|
+
} finally {
|
|
285
|
+
if (this.controllers.get(id) === controller) this.controllers.delete(id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
accepts(file) {
|
|
289
|
+
const rules = this.accept.split(",").map((rule) => rule.trim().toLowerCase()).filter(Boolean);
|
|
290
|
+
if (!rules.length) return true;
|
|
291
|
+
const name = file.name.toLowerCase();
|
|
292
|
+
const type = file.type.toLowerCase();
|
|
293
|
+
return rules.some((rule) => {
|
|
294
|
+
if (rule.startsWith(".")) return name.endsWith(rule);
|
|
295
|
+
if (rule.endsWith("/*")) return type.startsWith(rule.slice(0, -1));
|
|
296
|
+
return type === rule;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
formatBytes(bytes) {
|
|
300
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
301
|
+
if (bytes < 1048576) return `${Math.round(bytes / 1024)} KB`;
|
|
302
|
+
return `${(bytes / 1048576).toFixed(bytes < 10485760 ? 1 : 0)} MB`;
|
|
303
|
+
}
|
|
304
|
+
revokePreview(item) {
|
|
305
|
+
if (item.previewUrl) URL.revokeObjectURL(item.previewUrl);
|
|
306
|
+
}
|
|
307
|
+
handleInputChange(event) {
|
|
308
|
+
event.stopPropagation();
|
|
309
|
+
const input = event.currentTarget;
|
|
310
|
+
if (input.files) this.addFiles(input.files);
|
|
311
|
+
input.value = "";
|
|
312
|
+
}
|
|
313
|
+
stopNativeInput(event) {
|
|
314
|
+
event.stopPropagation();
|
|
315
|
+
}
|
|
316
|
+
handleKeydown(event) {
|
|
317
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
318
|
+
event.preventDefault();
|
|
319
|
+
this.browse();
|
|
320
|
+
}
|
|
321
|
+
handleDragEnter(event) {
|
|
322
|
+
event.preventDefault();
|
|
323
|
+
if (this.pickerDisabled || !event.dataTransfer?.types.includes("Files")) return;
|
|
324
|
+
this.dragDepth += 1;
|
|
325
|
+
this.dragging = true;
|
|
326
|
+
}
|
|
327
|
+
handleDragOver(event) {
|
|
328
|
+
if (this.pickerDisabled) return;
|
|
329
|
+
event.preventDefault();
|
|
330
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = "copy";
|
|
331
|
+
}
|
|
332
|
+
handleDragLeave(event) {
|
|
333
|
+
event.preventDefault();
|
|
334
|
+
this.dragDepth = Math.max(0, this.dragDepth - 1);
|
|
335
|
+
if (this.dragDepth === 0) this.dragging = false;
|
|
336
|
+
}
|
|
337
|
+
handleDrop(event) {
|
|
338
|
+
event.preventDefault();
|
|
339
|
+
this.dragDepth = 0;
|
|
340
|
+
this.dragging = false;
|
|
341
|
+
if (this.pickerDisabled || !event.dataTransfer?.files) return;
|
|
342
|
+
this.addFiles(event.dataTransfer.files);
|
|
343
|
+
}
|
|
344
|
+
syncFormState() {
|
|
345
|
+
if (this.effectiveDisabled || !this.name || this.uploadItems.length === 0) this.internals.setFormValue(null);
|
|
346
|
+
else {
|
|
347
|
+
const data = new FormData();
|
|
348
|
+
for (const item of this.uploadItems) data.append(this.name, item.file, item.file.name);
|
|
349
|
+
this.internals.setFormValue(data);
|
|
350
|
+
}
|
|
351
|
+
const missing = this.required && this.uploadItems.length === 0;
|
|
352
|
+
const flags = this.customValidityMessage ? { customError: true } : missing ? { valueMissing: true } : {};
|
|
353
|
+
const message = this.customValidityMessage || (missing ? "Please select a file." : "");
|
|
354
|
+
const anchor = this.renderRoot.querySelector(".picker") ?? void 0;
|
|
355
|
+
this.internals.setValidity(flags, message, anchor);
|
|
356
|
+
}
|
|
357
|
+
renderIcon() {
|
|
358
|
+
return svg`<svg class="upload-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M16 16l-4-4-4 4M12 12v9"></path><path d="M20.4 17.5A5 5 0 0 0 18 8.2 7 7 0 0 0 4.3 10.8 4.5 4.5 0 0 0 5.5 19H7"></path></svg>`;
|
|
359
|
+
}
|
|
360
|
+
get defaultHint() {
|
|
361
|
+
const parts = [];
|
|
362
|
+
if (this.accept) parts.push(this.accept);
|
|
363
|
+
if (this.maxSize > 0) parts.push(`up to ${this.formatBytes(this.maxSize)}`);
|
|
364
|
+
if (this.multiple && this.uploadItems.length > 0 && this.remainingCapacity !== void 0) parts.push(`${this.remainingCapacity} file${this.remainingCapacity === 1 ? "" : "s"} remaining`);
|
|
365
|
+
else if (this.maxFiles > 0) parts.push(`${this.maxFiles} file${this.maxFiles === 1 ? "" : "s"} maximum`);
|
|
366
|
+
return parts.join(" · ") || (this.multiple ? "Select one or more files" : "Select one file");
|
|
367
|
+
}
|
|
368
|
+
get defaultActionLabel() {
|
|
369
|
+
if (!this.multiple || this.uploadItems.length === 0) return this.multiple ? "Upload files" : "Upload file";
|
|
370
|
+
return this.remainingCapacity === void 0 ? "Upload more" : `Upload more (${this.remainingCapacity} remaining)`;
|
|
371
|
+
}
|
|
372
|
+
renderPicker() {
|
|
373
|
+
if (!this.canAddMore) return nothing;
|
|
374
|
+
if (this.variant === "compact") return html`
|
|
375
|
+
<c2-button class="picker compact-button" ?disabled=${this.effectiveDisabled} @click=${this.browse}>
|
|
376
|
+
<span class="compact-icon" slot="prefix-icon"><slot name="drop-icon">${this.renderIcon()}</slot></span>
|
|
377
|
+
<slot name="action">${this.defaultActionLabel}</slot>
|
|
378
|
+
</c2-button>
|
|
379
|
+
`;
|
|
380
|
+
return html`
|
|
381
|
+
<div
|
|
382
|
+
class=${classMap({
|
|
383
|
+
picker: true,
|
|
384
|
+
dropzone: true,
|
|
385
|
+
"dropzone--dragging": this.dragging
|
|
386
|
+
})}
|
|
387
|
+
role="button"
|
|
388
|
+
tabindex=${this.effectiveDisabled ? -1 : 0}
|
|
389
|
+
aria-label=${this.ariaLabel || "Upload files"}
|
|
390
|
+
aria-disabled=${this.effectiveDisabled ? "true" : "false"}
|
|
391
|
+
@click=${this.browse}
|
|
392
|
+
@keydown=${this.handleKeydown}
|
|
393
|
+
@dragenter=${this.handleDragEnter}
|
|
394
|
+
@dragover=${this.handleDragOver}
|
|
395
|
+
@dragleave=${this.handleDragLeave}
|
|
396
|
+
@drop=${this.handleDrop}
|
|
397
|
+
>
|
|
398
|
+
<slot name="drop-icon">${this.renderIcon()}</slot>
|
|
399
|
+
<div class="prompt">
|
|
400
|
+
<slot name="prompt">
|
|
401
|
+
${this.multiple && this.uploadItems.length ? html`Drop more files here or <span class="action">upload more</span>` : html`Drop files here or <span class="action">browse</span>`}
|
|
402
|
+
</slot>
|
|
403
|
+
</div>
|
|
404
|
+
<div class="hint"><slot name="hint">${this.defaultHint}</slot></div>
|
|
405
|
+
</div>
|
|
406
|
+
`;
|
|
407
|
+
}
|
|
408
|
+
render() {
|
|
409
|
+
return html`
|
|
410
|
+
<input
|
|
411
|
+
type="file"
|
|
412
|
+
tabindex="-1"
|
|
413
|
+
aria-label=${this.ariaLabel || "Upload files"}
|
|
414
|
+
.accept=${this.accept}
|
|
415
|
+
?multiple=${this.multiple}
|
|
416
|
+
?disabled=${this.pickerDisabled}
|
|
417
|
+
@input=${this.stopNativeInput}
|
|
418
|
+
@change=${this.handleInputChange}
|
|
419
|
+
/>
|
|
420
|
+
${this.renderPicker()}
|
|
421
|
+
${this.rejections.length ? html`<div class="errors" role="status">${this.rejections.map((rejection) => html`<div>${rejection.message}</div>`)}</div>` : nothing}
|
|
422
|
+
${this.uploadItems.length ? html`<div class=${classMap({
|
|
423
|
+
list: true,
|
|
424
|
+
"list--standalone": !this.canAddMore
|
|
425
|
+
})} role="list" aria-label="Selected files">
|
|
426
|
+
${this.uploadItems.map((item) => html`
|
|
427
|
+
<c2-attachment
|
|
428
|
+
role="listitem"
|
|
429
|
+
layout="row"
|
|
430
|
+
.name=${item.file.name}
|
|
431
|
+
.type=${item.file.type || void 0}
|
|
432
|
+
.size=${this.formatBytes(item.file.size)}
|
|
433
|
+
.src=${item.previewUrl}
|
|
434
|
+
.status=${item.status}
|
|
435
|
+
.progress=${item.progress}
|
|
436
|
+
removable
|
|
437
|
+
?disabled=${this.effectiveDisabled}
|
|
438
|
+
@attachment-remove=${() => this.removeFile(item.id)}
|
|
439
|
+
@attachment-retry=${() => void this.retry(item.id)}
|
|
440
|
+
></c2-attachment>
|
|
441
|
+
`)}
|
|
442
|
+
</div>` : html`<slot name="empty"></slot>`}
|
|
443
|
+
`;
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
__decorate([state()], Upload.prototype, "uploadItems", void 0);
|
|
447
|
+
__decorate([state()], Upload.prototype, "rejections", void 0);
|
|
448
|
+
__decorate([state()], Upload.prototype, "dragging", void 0);
|
|
449
|
+
__decorate([state()], Upload.prototype, "disabledByForm", void 0);
|
|
450
|
+
__decorate([property({
|
|
451
|
+
type: Boolean,
|
|
452
|
+
reflect: true
|
|
453
|
+
})], Upload.prototype, "multiple", void 0);
|
|
454
|
+
__decorate([property({ reflect: true })], Upload.prototype, "variant", void 0);
|
|
455
|
+
__decorate([property({ type: String })], Upload.prototype, "accept", void 0);
|
|
456
|
+
__decorate([property({
|
|
457
|
+
type: Number,
|
|
458
|
+
attribute: "max-files"
|
|
459
|
+
})], Upload.prototype, "maxFiles", void 0);
|
|
460
|
+
__decorate([property({
|
|
461
|
+
type: Number,
|
|
462
|
+
attribute: "max-size"
|
|
463
|
+
})], Upload.prototype, "maxSize", void 0);
|
|
464
|
+
__decorate([property({
|
|
465
|
+
type: Boolean,
|
|
466
|
+
attribute: "auto-upload"
|
|
467
|
+
})], Upload.prototype, "autoUpload", void 0);
|
|
468
|
+
__decorate([property({
|
|
469
|
+
type: Boolean,
|
|
470
|
+
reflect: true
|
|
471
|
+
})], Upload.prototype, "disabled", void 0);
|
|
472
|
+
__decorate([property({
|
|
473
|
+
type: Boolean,
|
|
474
|
+
reflect: true
|
|
475
|
+
})], Upload.prototype, "required", void 0);
|
|
476
|
+
__decorate([property({ type: String })], Upload.prototype, "name", void 0);
|
|
477
|
+
__decorate([property({ attribute: "aria-label" })], Upload.prototype, "ariaLabel", void 0);
|
|
478
|
+
__decorate([property({ attribute: false })], Upload.prototype, "uploadHandler", void 0);
|
|
479
|
+
__decorate([query("input", true)], Upload.prototype, "input", void 0);
|
|
480
|
+
Upload = __decorate([customElement("c2-upload")], Upload);
|
|
481
|
+
//#endregion
|
|
482
|
+
export { Upload };
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@c2n/upload",
|
|
3
|
+
"version": "0.0.9",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/upload.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./types/src/upload.d.ts",
|
|
9
|
+
"default": "./dist/upload.js"
|
|
10
|
+
},
|
|
11
|
+
"./react": {
|
|
12
|
+
"types": "./react.d.ts",
|
|
13
|
+
"default": "./react.js"
|
|
14
|
+
},
|
|
15
|
+
"./vue": {
|
|
16
|
+
"types": "./vue.d.ts",
|
|
17
|
+
"default": "./vue.js"
|
|
18
|
+
},
|
|
19
|
+
"./custom-elements.json": "./custom-elements.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"types",
|
|
24
|
+
"react.d.ts",
|
|
25
|
+
"react.js",
|
|
26
|
+
"vue.d.ts",
|
|
27
|
+
"vue.js"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"upload",
|
|
31
|
+
"web component",
|
|
32
|
+
"lit"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"author": "code2nguyen@gmail.com",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"registry": "https://registry.npmjs.org",
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/code2nguyen/web-components.git"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"dev": "vite",
|
|
46
|
+
"build": "wireit",
|
|
47
|
+
"build:only": "vite build",
|
|
48
|
+
"type-check": "wireit"
|
|
49
|
+
},
|
|
50
|
+
"wireit": {
|
|
51
|
+
"type-check": {
|
|
52
|
+
"dependencies": [
|
|
53
|
+
"../../core:build",
|
|
54
|
+
"../attachment:build",
|
|
55
|
+
"../button:build"
|
|
56
|
+
],
|
|
57
|
+
"command": "tsc -p tsconfig.lib.json --composite false"
|
|
58
|
+
},
|
|
59
|
+
"build": {
|
|
60
|
+
"dependencies": [
|
|
61
|
+
"type-check"
|
|
62
|
+
],
|
|
63
|
+
"command": "vite build"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@c2n/attachment": "0.0.9",
|
|
68
|
+
"@c2n/button": "0.0.9",
|
|
69
|
+
"@c2n/core": "0.0.9",
|
|
70
|
+
"lit": "3.3.3"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@c2n/config": "*"
|
|
74
|
+
},
|
|
75
|
+
"customElements": "custom-elements.json",
|
|
76
|
+
"gitHead": "e4768cce5e0fbd5bc3165cd493a1e8d57cf0fff4"
|
|
77
|
+
}
|
package/react.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// GENERATED by @c2n/framework-types. Do not edit by hand.
|
|
2
|
+
//
|
|
3
|
+
// JSX types for the custom elements of @c2n/upload. Import once, anywhere in the program:
|
|
4
|
+
//
|
|
5
|
+
// import '@c2n/upload/react'
|
|
6
|
+
//
|
|
7
|
+
// Register the elements at module scope before React renders, or React writes an object prop as an
|
|
8
|
+
// attribute and it stringifies.
|
|
9
|
+
|
|
10
|
+
import type { DetailedHTMLProps, HTMLAttributes } from 'react'
|
|
11
|
+
import type { Upload } from '@c2n/upload'
|
|
12
|
+
|
|
13
|
+
/** Standard React host-element attributes plus the element's own public properties. */
|
|
14
|
+
type C2Props<T> = DetailedHTMLProps<HTMLAttributes<T>, T> & Partial<Omit<T, keyof HTMLElement>>
|
|
15
|
+
|
|
16
|
+
declare module 'react' {
|
|
17
|
+
namespace JSX {
|
|
18
|
+
interface IntrinsicElements {
|
|
19
|
+
'c2-upload': C2Props<Upload>
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export {}
|
package/react.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { LitElement } from 'lit';
|
|
2
|
+
import type { TypedAddEventListener, TypedRemoveEventListener } from '@c2n/core/event-helper.js';
|
|
3
|
+
import type { AttachmentStatus } from '@c2n/attachment';
|
|
4
|
+
import '@c2n/attachment';
|
|
5
|
+
import '@c2n/button';
|
|
6
|
+
export type UploadVariant = 'dropzone' | 'compact';
|
|
7
|
+
export interface UploadItem {
|
|
8
|
+
id: string;
|
|
9
|
+
file: File;
|
|
10
|
+
status: AttachmentStatus;
|
|
11
|
+
progress: number;
|
|
12
|
+
result?: unknown;
|
|
13
|
+
error?: unknown;
|
|
14
|
+
previewUrl?: string;
|
|
15
|
+
}
|
|
16
|
+
export type UploadRejectionReason = 'type' | 'size' | 'limit' | 'multiple';
|
|
17
|
+
export interface UploadRejection {
|
|
18
|
+
file: File;
|
|
19
|
+
reason: UploadRejectionReason;
|
|
20
|
+
message: string;
|
|
21
|
+
}
|
|
22
|
+
export interface UploadHandlerContext {
|
|
23
|
+
signal: AbortSignal;
|
|
24
|
+
onProgress: (progress: number) => void;
|
|
25
|
+
}
|
|
26
|
+
/** Application upload function. Resolve with server data; throw to show the attachment error/retry state. */
|
|
27
|
+
export type UploadHandler = (file: File, context: UploadHandlerContext) => unknown | Promise<unknown>;
|
|
28
|
+
export interface UploadFilesEventDetail {
|
|
29
|
+
files: File[];
|
|
30
|
+
items: UploadItem[];
|
|
31
|
+
}
|
|
32
|
+
export interface UploadProgressEventDetail {
|
|
33
|
+
item: UploadItem;
|
|
34
|
+
progress: number;
|
|
35
|
+
}
|
|
36
|
+
export interface UploadResultEventDetail {
|
|
37
|
+
item: UploadItem;
|
|
38
|
+
result?: unknown;
|
|
39
|
+
error?: unknown;
|
|
40
|
+
}
|
|
41
|
+
/** Events fired by {@link Upload}, keyed for `addEventListener`. */
|
|
42
|
+
export interface UploadEventMap {
|
|
43
|
+
'files-selected': CustomEvent<UploadFilesEventDetail>;
|
|
44
|
+
'file-reject': CustomEvent<{
|
|
45
|
+
rejections: UploadRejection[];
|
|
46
|
+
}>;
|
|
47
|
+
'upload-start': CustomEvent<{
|
|
48
|
+
item: UploadItem;
|
|
49
|
+
}>;
|
|
50
|
+
'upload-progress': CustomEvent<UploadProgressEventDetail>;
|
|
51
|
+
'upload-success': CustomEvent<UploadResultEventDetail>;
|
|
52
|
+
'upload-error': CustomEvent<UploadResultEventDetail>;
|
|
53
|
+
'upload-remove': CustomEvent<{
|
|
54
|
+
item: UploadItem;
|
|
55
|
+
}>;
|
|
56
|
+
input: Event;
|
|
57
|
+
change: Event;
|
|
58
|
+
}
|
|
59
|
+
export interface Upload {
|
|
60
|
+
addEventListener: TypedAddEventListener<Upload, UploadEventMap>;
|
|
61
|
+
removeEventListener: TypedRemoveEventListener<Upload, UploadEventMap>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Drag-and-drop file uploader with multiple selection, validation and an attachment-based upload queue. Set an async
|
|
65
|
+
* `uploadHandler(file, { signal, onProgress })` property to connect transport; the component owns progress, success,
|
|
66
|
+
* error, retry, cancel and removal UI while leaving network policy to the application.
|
|
67
|
+
*
|
|
68
|
+
* @tag c2-upload
|
|
69
|
+
*
|
|
70
|
+
* @slot drop-icon - Replaces the upload-cloud icon in the drop zone.
|
|
71
|
+
* @slot prompt - Replaces the main drop-zone instruction.
|
|
72
|
+
* @slot hint - Replaces the accepted-type and size hint.
|
|
73
|
+
* @slot action - Replaces the dynamic label of the compact upload button.
|
|
74
|
+
* @slot empty - Optional content shown below the drop zone while no files are selected.
|
|
75
|
+
*
|
|
76
|
+
* @event {CustomEvent<UploadFilesEventDetail>} files-selected - Fired after valid files enter the queue through browse, drop or `addFiles()`.
|
|
77
|
+
* @event {CustomEvent<{ rejections: UploadRejection[] }>} file-reject - Fired when type, size, count or single-file rules reject files.
|
|
78
|
+
* @event {CustomEvent<{ item: UploadItem }>} upload-start - Fired when an item begins uploading.
|
|
79
|
+
* @event {CustomEvent<UploadProgressEventDetail>} upload-progress - Fired when the upload handler reports progress.
|
|
80
|
+
* @event {CustomEvent<UploadResultEventDetail>} upload-success - Fired after an upload handler resolves; `item.result` contains its result.
|
|
81
|
+
* @event {CustomEvent<UploadResultEventDetail>} upload-error - Fired after an upload handler rejects; `item.error` contains the error.
|
|
82
|
+
* @event {CustomEvent<{ item: UploadItem }>} upload-remove - Fired after an item is removed or an active upload is cancelled.
|
|
83
|
+
* @event {Event} input - Fired whenever the file value changes, for form and framework bindings.
|
|
84
|
+
* @event {Event} change - Fired whenever files are added or removed, for form and framework bindings.
|
|
85
|
+
*
|
|
86
|
+
* @cssproperty {pixel} [--c2-upload--width=420px]
|
|
87
|
+
* @cssproperty {padding} [--c2-upload__dropzone--padding=28px 20px]
|
|
88
|
+
* @cssproperty {pixel} [--c2-upload__dropzone--gap=8px]
|
|
89
|
+
* @cssproperty {color} [--c2-upload__dropzone--background=#ffffff]
|
|
90
|
+
* @cssproperty {border} [--c2-upload__dropzone--border=1px dashed #bcbcc6]
|
|
91
|
+
* @cssproperty {border-radius} [--c2-upload__dropzone--border-radius=14px]
|
|
92
|
+
* @cssproperty {color} [--c2-upload__dropzone__hover--background=#fafafa]
|
|
93
|
+
* @cssproperty {border} [--c2-upload__dropzone__hover--border=1px dashed #a1a1aa]
|
|
94
|
+
* @cssproperty {color} [--c2-upload__dropzone__drag--background=#e8f2ff]
|
|
95
|
+
* @cssproperty {border} [--c2-upload__dropzone__drag--border=1px dashed rgb(2, 101, 220)]
|
|
96
|
+
* @cssproperty {outline} [--c2-upload__dropzone__focus--outline=2px solid rgba(2, 101, 220, 0.4)]
|
|
97
|
+
* @cssproperty {pixel} [--c2-upload__dropzone__focus--outline-offset=2px]
|
|
98
|
+
* @cssproperty {opacity} [--c2-upload__dropzone__disabled--opacity=0.38]
|
|
99
|
+
* @cssproperty {pixel} [--c2-upload__icon--size=32px]
|
|
100
|
+
* @cssproperty {color} [--c2-upload__icon--color=rgb(2, 101, 220)]
|
|
101
|
+
* @cssproperty {color} [--c2-upload__prompt--color=#18181b]
|
|
102
|
+
* @cssproperty {font-size} [--c2-upload__prompt--font-size=14px]
|
|
103
|
+
* @cssproperty {font-weight} [--c2-upload__prompt--font-weight=600]
|
|
104
|
+
* @cssproperty {color} [--c2-upload__action--color=rgb(2, 101, 220)]
|
|
105
|
+
* @cssproperty {color} [--c2-upload__hint--color=#71717a]
|
|
106
|
+
* @cssproperty {font-size} [--c2-upload__hint--font-size=12px]
|
|
107
|
+
* @cssproperty {pixel} [--c2-upload__list--gap=10px]
|
|
108
|
+
* @cssproperty {pixel} [--c2-upload__list--margin-top=12px]
|
|
109
|
+
* @cssproperty {color} [--c2-upload__error--color=#dc2626]
|
|
110
|
+
* @cssproperty {font-size} [--c2-upload__error--font-size=12px]
|
|
111
|
+
* @cssproperty {pixel} [--c2-upload__error--gap=4px]
|
|
112
|
+
*
|
|
113
|
+
* @internalcomponent c2-attachment, c2-button
|
|
114
|
+
*/
|
|
115
|
+
export declare class Upload extends LitElement {
|
|
116
|
+
static formAssociated: boolean;
|
|
117
|
+
static styles: import("lit").CSSResult;
|
|
118
|
+
private readonly internals;
|
|
119
|
+
private readonly controllers;
|
|
120
|
+
private dragDepth;
|
|
121
|
+
private customValidityMessage;
|
|
122
|
+
private uploadItems;
|
|
123
|
+
private rejections;
|
|
124
|
+
private dragging;
|
|
125
|
+
private disabledByForm;
|
|
126
|
+
/** Accept several files in one browse or drop operation. */
|
|
127
|
+
multiple: boolean;
|
|
128
|
+
/** Dropzone provides drag-and-drop; compact renders only an upload button. */
|
|
129
|
+
variant: UploadVariant;
|
|
130
|
+
/** Comma-separated MIME types or file extensions, using the native file-input syntax. */
|
|
131
|
+
accept: string;
|
|
132
|
+
/** Maximum number of queued files. Zero means unlimited. */
|
|
133
|
+
maxFiles: number;
|
|
134
|
+
/** Maximum size of each file in bytes. Zero means unlimited. */
|
|
135
|
+
maxSize: number;
|
|
136
|
+
/** Run `uploadHandler` immediately after files are accepted. */
|
|
137
|
+
autoUpload: boolean;
|
|
138
|
+
/** Disables browsing, dropping and attachment actions. */
|
|
139
|
+
disabled: boolean;
|
|
140
|
+
/** Requires at least one selected file for native form validation. */
|
|
141
|
+
required: boolean;
|
|
142
|
+
/** Form field name. Each queued file is appended with this name. */
|
|
143
|
+
name: string;
|
|
144
|
+
/** Accessible name for the drop zone. */
|
|
145
|
+
ariaLabel: string | null;
|
|
146
|
+
/** Application-provided transport. Report percentages through `onProgress`; respect `signal` to support cancel. */
|
|
147
|
+
uploadHandler: UploadHandler | undefined;
|
|
148
|
+
private input;
|
|
149
|
+
/** Current queue, including ready, uploading, complete and failed items. */
|
|
150
|
+
get items(): readonly UploadItem[];
|
|
151
|
+
/** Selected files. Assign a new array to replace the queue. */
|
|
152
|
+
get value(): File[];
|
|
153
|
+
set value(files: File[]);
|
|
154
|
+
get form(): HTMLFormElement | null;
|
|
155
|
+
get labels(): NodeList;
|
|
156
|
+
get validity(): ValidityState;
|
|
157
|
+
get validationMessage(): string;
|
|
158
|
+
get willValidate(): boolean;
|
|
159
|
+
disconnectedCallback(): void;
|
|
160
|
+
formResetCallback(): void;
|
|
161
|
+
formDisabledCallback(disabled: boolean): void;
|
|
162
|
+
checkValidity(): boolean;
|
|
163
|
+
reportValidity(): boolean;
|
|
164
|
+
setCustomValidity(message: string): void;
|
|
165
|
+
protected updated(): void;
|
|
166
|
+
private get effectiveDisabled();
|
|
167
|
+
private get remainingCapacity();
|
|
168
|
+
private get canAddMore();
|
|
169
|
+
private get pickerDisabled();
|
|
170
|
+
/** Opens the native file browser. */
|
|
171
|
+
browse(): void;
|
|
172
|
+
/** Validates and adds files supplied by the browser, drag-and-drop or application code. */
|
|
173
|
+
addFiles(files: Iterable<File>): UploadItem[];
|
|
174
|
+
/** Upload every ready/error item, or one item by id. */
|
|
175
|
+
upload(id?: string): Promise<void>;
|
|
176
|
+
/** Retry a failed or ready item. */
|
|
177
|
+
retry(id: string): Promise<void>;
|
|
178
|
+
/** Cancels an active upload and removes its attachment. */
|
|
179
|
+
removeFile(id: string): void;
|
|
180
|
+
/** Cancels and removes every queued file. */
|
|
181
|
+
clear(): void;
|
|
182
|
+
private resetQueue;
|
|
183
|
+
private notifyValueChange;
|
|
184
|
+
private emit;
|
|
185
|
+
private createItem;
|
|
186
|
+
private updateItem;
|
|
187
|
+
private runUpload;
|
|
188
|
+
private accepts;
|
|
189
|
+
private formatBytes;
|
|
190
|
+
private revokePreview;
|
|
191
|
+
private handleInputChange;
|
|
192
|
+
private stopNativeInput;
|
|
193
|
+
private handleKeydown;
|
|
194
|
+
private handleDragEnter;
|
|
195
|
+
private handleDragOver;
|
|
196
|
+
private handleDragLeave;
|
|
197
|
+
private handleDrop;
|
|
198
|
+
private syncFormState;
|
|
199
|
+
private renderIcon;
|
|
200
|
+
private get defaultHint();
|
|
201
|
+
private get defaultActionLabel();
|
|
202
|
+
private renderPicker;
|
|
203
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
204
|
+
}
|
|
205
|
+
declare global {
|
|
206
|
+
interface HTMLElementTagNameMap {
|
|
207
|
+
'c2-upload': Upload;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=upload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../../src/upload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAiC,MAAM,KAAK,CAAA;AAI/D,OAAO,KAAK,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAA;AAChG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAGvD,OAAO,iBAAiB,CAAA;AACxB,OAAO,aAAa,CAAA;AAEpB,MAAM,MAAM,aAAa,GAAG,UAAU,GAAG,SAAS,CAAA;AAElD,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,gBAAgB,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;AAE1E,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,qBAAqB,CAAA;IAC7B,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,WAAW,CAAA;IACnB,UAAU,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;CACvC;AAED,6GAA6G;AAC7G,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,OA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AAE,MAAM,CAAA;KACpB;CACF"}
|
package/vue.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// GENERATED by @c2n/framework-types. Do not edit by hand.
|
|
2
|
+
//
|
|
3
|
+
// Vue template types for the custom elements of @c2n/upload. Import once, anywhere in the program:
|
|
4
|
+
//
|
|
5
|
+
// import '@c2n/upload/vue'
|
|
6
|
+
//
|
|
7
|
+
// Tell the compiler about the tags as well, or every c2-* tag is resolved as a Vue component and renders
|
|
8
|
+
// nothing: template.compilerOptions.isCustomElement = (tag) => tag.startsWith('c2-') in vite.config.ts.
|
|
9
|
+
|
|
10
|
+
import type { DefineComponent, HTMLAttributes } from 'vue'
|
|
11
|
+
import type { Upload, UploadEventMap } from '@c2n/upload'
|
|
12
|
+
|
|
13
|
+
/** The element's own public properties, plus every attribute Vue understands on a host element. */
|
|
14
|
+
type C2Props<T> = Partial<Omit<T, keyof HTMLElement>> & HTMLAttributes
|
|
15
|
+
|
|
16
|
+
declare module 'vue' {
|
|
17
|
+
interface GlobalComponents {
|
|
18
|
+
'c2-upload': DefineComponent<
|
|
19
|
+
C2Props<Upload> & {
|
|
20
|
+
'max-files'?: unknown
|
|
21
|
+
'max-size'?: unknown
|
|
22
|
+
'auto-upload'?: unknown
|
|
23
|
+
'aria-label'?: unknown
|
|
24
|
+
onInput?: (event: UploadEventMap['input']) => void
|
|
25
|
+
onChange?: (event: UploadEventMap['change']) => void
|
|
26
|
+
onFilesSelected?: (event: UploadEventMap['files-selected']) => void
|
|
27
|
+
onFileReject?: (event: UploadEventMap['file-reject']) => void
|
|
28
|
+
onUploadStart?: (event: UploadEventMap['upload-start']) => void
|
|
29
|
+
onUploadProgress?: (event: UploadEventMap['upload-progress']) => void
|
|
30
|
+
onUploadSuccess?: (event: UploadEventMap['upload-success']) => void
|
|
31
|
+
onUploadError?: (event: UploadEventMap['upload-error']) => void
|
|
32
|
+
onUploadRemove?: (event: UploadEventMap['upload-remove']) => void
|
|
33
|
+
}
|
|
34
|
+
>
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare module '@vue/runtime-dom' {
|
|
39
|
+
interface HTMLAttributes {
|
|
40
|
+
/** Vue's own definition omits it, and slotting a plain element into a component needs it. */
|
|
41
|
+
slot?: string
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export {}
|
package/vue.js
ADDED