DEV: UppyUpload improvements (#29279)

These tweaks will help adoption of the non-mixin-based uppy patterns.

- Add `type:`  to default arguments list
- Update pick-files-button to support explicit element registration
- Make `cancelSingleUpload` a public API, and add `cancelAllUploads`
- Remove `isDestroyed` logic - it doesn't do anything outside a component
- Add `@bind` to `setup()`
- Allow `additionalParams` to be a function
- Fix `autoStart` mixin shim
This commit is contained in:
David Taylor 2024-10-21 13:00:54 +01:00 committed by GitHub
parent 4749d094a9
commit be5b35071b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 10 deletions

View File

@ -7,6 +7,7 @@
{{/if}}
{{#if this.acceptsAllFormats}}
<input
{{did-insert (or @registerFileInput (noop))}}
type="file"
id={{this.fileInputId}}
class={{this.fileInputClass}}
@ -15,6 +16,7 @@
/>
{{else}}
<input
{{did-insert (or @registerFileInput (noop))}}
type="file"
id={{this.fileInputId}}
class={{this.fileInputClass}}

View File

@ -54,6 +54,7 @@ const DEFAULT_CONFIG = {
useMultipartUploadsIfAvailable: false,
uppyReady: null,
onProgressUploadsChanged: null,
type: null,
};
// Merges incoming config with defaults, without actually evaluating
@ -125,11 +126,12 @@ export default class UppyUpload {
);
this.appEvents.off(
`upload-mixin:${this.config.id}:cancel-upload`,
this._cancelSingleUpload
this.cancelSingleUpload
);
this.uppyWrapper.uppyInstance?.close();
}
@bind
setup(fileInputEl) {
this._fileInputEl = fileInputEl;
@ -145,7 +147,7 @@ export default class UppyUpload {
// actual file type
meta: deepMerge(
{ upload_type: this.config.type },
this.config.additionalParams
this.#resolvedAdditionalParams
),
onBeforeFileAdded: (currentFile) => {
@ -359,7 +361,7 @@ export default class UppyUpload {
);
this.appEvents.on(
`upload-mixin:${this.config.id}:cancel-upload`,
this._cancelSingleUpload
this.cancelSingleUpload
);
this.config.uppyReady?.();
@ -372,6 +374,11 @@ export default class UppyUpload {
});
}
@bind
openPicker() {
this._fileInputEl.click();
}
#triggerInProgressUploadsEvent() {
this.config.onProgressUploadsChanged?.(this.inProgressUploads);
this.appEvents.trigger(
@ -469,11 +476,18 @@ export default class UppyUpload {
}
@bind
_cancelSingleUpload(data) {
cancelSingleUpload(data) {
this.uppyWrapper.uppyInstance.removeFile(data.fileId);
this.#removeInProgressUpload(data.fileId);
}
@bind
cancelAllUploads() {
this.uppyWrapper.uppyInstance?.cancelAll();
this.inProgressUploads.length = 0;
this.#triggerInProgressUploadsEvent();
}
@bind
async addFiles(files, opts = {}) {
if (!this.session.csrfToken) {
@ -506,11 +520,19 @@ export default class UppyUpload {
type: "POST",
data: deepMerge(
{ unique_identifier: file.meta.uniqueUploadIdentifier },
this.config.additionalParams
this.#resolvedAdditionalParams
),
});
}
get #resolvedAdditionalParams() {
if (typeof this.config.additionalParams === "function") {
return this.config.additionalParams();
} else {
return this.config.additionalParams;
}
}
#reset() {
this.uppyWrapper.uppyInstance?.cancelAll();
Object.assign(this, {
@ -524,10 +546,6 @@ export default class UppyUpload {
}
#removeInProgressUpload(fileId) {
if (this.isDestroyed || this.isDestroying) {
return;
}
const index = this.inProgressUploads.findIndex((upl) => upl.id === fileId);
if (index === -1) {
return;

View File

@ -74,7 +74,7 @@ export default Mixin.create({
function configShim(component) {
return {
get autoStartUploads() {
return component.autoStartUploads || true;
return component.autoStartUploads ?? true;
},
get id() {
return component.id;