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

View File

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

View File

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