DEV: Update chat-composer-uploads uppy usage (#29339)

This commit is contained in:
David Taylor 2024-10-23 10:07:54 +01:00 committed by GitHub
parent 51a32de45e
commit 52016e4596
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 68 additions and 63 deletions

View File

@ -200,12 +200,10 @@ export default class UppyUpload {
}, },
}); });
if (this.config.uploadDropTargetOptions) { const resolvedDropTargetOptions = this.#resolvedDropTargetOptions;
if (resolvedDropTargetOptions) {
// DropTarget is a UI plugin, only preprocessors must call _useUploadPlugin // DropTarget is a UI plugin, only preprocessors must call _useUploadPlugin
this.uppyWrapper.uppyInstance.use( this.uppyWrapper.uppyInstance.use(DropTarget, resolvedDropTargetOptions);
DropTarget,
this.config.uploadDropTargetOptions
);
} }
this.uppyWrapper.uppyInstance.on("progress", (progress) => { this.uppyWrapper.uppyInstance.on("progress", (progress) => {
@ -533,6 +531,14 @@ export default class UppyUpload {
} }
} }
get #resolvedDropTargetOptions() {
if (typeof this.config.uploadDropTargetOptions === "function") {
return this.config.uploadDropTargetOptions();
} else {
return this.config.uploadDropTargetOptions;
}
}
#reset() { #reset() {
this.uppyWrapper.uppyInstance?.cancelAll(); this.uppyWrapper.uppyInstance?.cancelAll();
Object.assign(this, { Object.assign(this, {

View File

@ -21,4 +21,5 @@
@allowMultiple={{true}} @allowMultiple={{true}}
@fileInputId={{this.fileUploadElementId}} @fileInputId={{this.fileUploadElementId}}
@fileInputClass="hidden-upload-field" @fileInputClass="hidden-upload-field"
@registerFileInput={{this.uppyUpload.setup}}
/> />

View File

@ -1,38 +1,74 @@
import Component from "@ember/component"; import Component from "@ember/component";
import { action } from "@ember/object"; import { action } from "@ember/object";
import { getOwner } from "@ember/owner";
import { service } from "@ember/service"; import { service } from "@ember/service";
import { classNames } from "@ember-decorators/component"; import { classNames } from "@ember-decorators/component";
import UppyUpload from "discourse/lib/uppy/uppy-upload";
import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin"; import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import { clipboardHelpers } from "discourse/lib/utilities"; import { clipboardHelpers } from "discourse/lib/utilities";
import UppyUploadMixin from "discourse/mixins/uppy-upload";
import { cloneJSON } from "discourse-common/lib/object"; import { cloneJSON } from "discourse-common/lib/object";
import discourseComputed, { bind } from "discourse-common/utils/decorators"; import { bind } from "discourse-common/utils/decorators";
@classNames("chat-composer-uploads") @classNames("chat-composer-uploads")
export default class ChatComposerUploads extends Component.extend( export default class ChatComposerUploads extends Component {
UppyUploadMixin
) {
@service mediaOptimizationWorker; @service mediaOptimizationWorker;
@service chatStateManager; @service chatStateManager;
id = "chat-composer-uploader"; uppyUpload = new UppyUpload(getOwner(this), {
type = "chat-composer"; id: "chat-composer-uploader",
type: "chat-composer",
useMultipartUploadsIfAvailable: true,
uppyReady: () => {
if (this.siteSettings.composer_media_optimization_image_enabled) {
this.uppyUpload.uppyWrapper.useUploadPlugin(UppyMediaOptimization, {
optimizeFn: (data, opts) =>
this.mediaOptimizationWorker.optimizeImage(data, opts),
runParallel: !this.site.isMobileDevice,
});
}
this.uppyUpload.uppyWrapper.onPreProcessProgress((file) => {
const inProgressUpload = this.inProgressUploads.findBy("id", file.id);
if (!inProgressUpload?.processing) {
inProgressUpload?.set("processing", true);
}
});
this.uppyUpload.uppyWrapper.onPreProcessComplete((file) => {
const inProgressUpload = this.inProgressUploads.findBy("id", file.id);
inProgressUpload?.set("processing", false);
});
},
uploadDone: (upload) => {
this.uploads.pushObject(upload);
this._triggerUploadsChanged();
},
uploadDropTargetOptions: () => ({
target: this.uploadDropZone || document.body,
}),
onProgressUploadsChanged: () => {
this._triggerUploadsChanged(this.uploads, {
inProgressUploadsCount: this.inProgressUploads?.length,
});
},
});
existingUploads = null; existingUploads = null;
uploads = null; uploads = null;
useMultipartUploadsIfAvailable = true;
uploadDropZone = null; uploadDropZone = null;
init() { get inProgressUploads() {
super.init(...arguments); return this.uppyUpload.inProgressUploads;
this.setProperties({
fileInputSelector: `#${this.fileUploadElementId}`,
});
} }
didReceiveAttrs() { didReceiveAttrs() {
super.didReceiveAttrs(...arguments); super.didReceiveAttrs(...arguments);
if (this.inProgressUploads?.length > 0) { if (this.inProgressUploads?.length > 0) {
this._uppyInstance?.cancelAll(); this.uppyUpload.uppyWrapper.uppyInstance?.cancelAll();
} }
this.set( this.set(
@ -56,19 +92,13 @@ export default class ChatComposerUploads extends Component.extend(
); );
} }
uploadDone(upload) { get showUploadsContainer() {
this.uploads.pushObject(upload); return this.get("uploads.length") > 0 || this.inProgressUploads.length > 0;
this._triggerUploadsChanged();
}
@discourseComputed("uploads.length", "inProgressUploads.length")
showUploadsContainer(uploadsCount, inProgressUploadsCount) {
return uploadsCount > 0 || inProgressUploadsCount > 0;
} }
@action @action
cancelUploading(upload) { cancelUploading(upload) {
this.appEvents.trigger(`upload-mixin:${this.id}:cancel-upload`, { this.uppyUpload.cancelSingleUpload({
fileId: upload.id, fileId: upload.id,
}); });
this.removeUpload(upload); this.removeUpload(upload);
@ -80,34 +110,6 @@ export default class ChatComposerUploads extends Component.extend(
this._triggerUploadsChanged(); this._triggerUploadsChanged();
} }
_uploadDropTargetOptions() {
return {
target: this.uploadDropZone || document.body,
};
}
_uppyReady() {
if (this.siteSettings.composer_media_optimization_image_enabled) {
this.uppyUpload.uppyWrapper.useUploadPlugin(UppyMediaOptimization, {
optimizeFn: (data, opts) =>
this.mediaOptimizationWorker.optimizeImage(data, opts),
runParallel: !this.site.isMobileDevice,
});
}
this.uppyUpload.uppyWrapper.onPreProcessProgress((file) => {
const inProgressUpload = this.inProgressUploads.findBy("id", file.id);
if (!inProgressUpload?.processing) {
inProgressUpload?.set("processing", true);
}
});
this.uppyUpload.uppyWrapper.onPreProcessComplete((file) => {
const inProgressUpload = this.inProgressUploads.findBy("id", file.id);
inProgressUpload?.set("processing", false);
});
}
@bind @bind
_pasteEventListener(event) { _pasteEventListener(event) {
if (document.activeElement !== this.composerInputEl) { if (document.activeElement !== this.composerInputEl) {
@ -124,16 +126,12 @@ export default class ChatComposerUploads extends Component.extend(
} }
if (event && event.clipboardData && event.clipboardData.files) { if (event && event.clipboardData && event.clipboardData.files) {
this._addFiles([...event.clipboardData.files], { pasted: true }); this.uppyUpload.addFiles([...event.clipboardData.files], {
pasted: true,
});
} }
} }
onProgressUploadsChanged() {
this._triggerUploadsChanged(this.uploads, {
inProgressUploadsCount: this.inProgressUploads?.length,
});
}
_triggerUploadsChanged() { _triggerUploadsChanged() {
this.onUploadChanged?.(this.uploads, { this.onUploadChanged?.(this.uploads, {
inProgressUploadsCount: this.inProgressUploads?.length, inProgressUploadsCount: this.inProgressUploads?.length,