From 691a8e3e78a08cde9f23d88f1f1c5fab435c4ebb Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 6 Nov 2024 10:06:09 +0100 Subject: [PATCH] Convert upload component to new type with improved uppy support --- .../custom-wizard-field-upload.js.es6 | 79 +++++++++++++------ .../components/custom-wizard-field-upload.hbs | 41 +++++----- 2 files changed, 75 insertions(+), 45 deletions(-) diff --git a/assets/javascripts/discourse/components/custom-wizard-field-upload.js.es6 b/assets/javascripts/discourse/components/custom-wizard-field-upload.js.es6 index 990d7daa..6a58de0c 100644 --- a/assets/javascripts/discourse/components/custom-wizard-field-upload.js.es6 +++ b/assets/javascripts/discourse/components/custom-wizard-field-upload.js.es6 @@ -1,27 +1,58 @@ -import UppyUploadMixin from "discourse/mixins/uppy-upload"; +import UppyUpload from "discourse/lib/uppy/uppy-upload"; import Component from "@ember/component"; -import { computed } from "@ember/object"; +import { getOwner } from "@ember/owner"; +import { service } from "@ember/service"; +import discourseComputed from "discourse-common/utils/decorators"; +import I18n from "discourse-i18n"; +import { action } from "@ember/object"; -export default Component.extend(UppyUploadMixin, { - classNames: ["wizard-field-upload"], - classNameBindings: ["isImage", "fieldClass"], - uploading: false, - type: computed(function () { - return `wizard_${this.field.id}`; - }), - id: computed(function () { - return `wizard_field_upload_${this.field.id}`; - }), - isImage: computed("field.value.extension", function () { - return ( - this.field.value && - this.siteSettings.wizard_recognised_image_upload_formats - .split("|") - .includes(this.field.value.extension) - ); - }), +export default class CustomWizardFieldUpload extends Component { + @service siteSettings; - uploadDone(upload) { - this.set("field.value", upload); - }, -}); + @action + setup() { + this.uppyUpload = new UppyUpload(getOwner(this), { + id: this.inputId, + type: `wizard_${this.field.id}`, + uploadDone: (upload) => { + this.setProperties({ + "field.value": upload, + isImage: this.imageUploadFormats.includes(upload.extension), + }); + this.done(); + }, + }); + this.uppyUpload.setup(document.getElementById(this.inputId)); + } + + get imageUploadFormats() { + return this.siteSettings.wizard_recognised_image_upload_formats.split("|"); + } + + get inputId() { + return `wizard_field_upload_${this.field?.id}`; + } + + get wrapperClass() { + let result = "wizard-field-upload"; + if (this.isImage) { + result += " is-image"; + } + if (this.fieldClass) { + result += ` ${this.fieldClass}`; + } + return result; + } + + @discourseComputed("uppyUpload.uploading", "uppyUpload.uploadProgress") + uploadLabel() { + return this.uppyUpload?.uploading + ? `${I18n.t("wizard.uploading")} ${this.uppyUpload.uploadProgress}%` + : I18n.t("wizard.upload"); + } + + @action + chooseFiles() { + this.uppyUpload?.openPicker(); + } +} diff --git a/assets/javascripts/discourse/templates/components/custom-wizard-field-upload.hbs b/assets/javascripts/discourse/templates/components/custom-wizard-field-upload.hbs index f899f580..cbb80708 100644 --- a/assets/javascripts/discourse/templates/components/custom-wizard-field-upload.hbs +++ b/assets/javascripts/discourse/templates/components/custom-wizard-field-upload.hbs @@ -1,27 +1,26 @@ -