|
1 | | -import UppyUploadMixin from "discourse/mixins/uppy-upload"; |
| 1 | +import UppyUpload from "discourse/lib/uppy/uppy-upload"; |
2 | 2 | import Component from "@ember/component"; |
3 | | -import { computed } from "@ember/object"; |
| 3 | +import { getOwner } from "@ember/owner"; |
| 4 | +import { service } from "@ember/service"; |
| 5 | +import discourseComputed from "discourse-common/utils/decorators"; |
| 6 | +import I18n from "discourse-i18n"; |
| 7 | +import { action } from "@ember/object"; |
4 | 8 |
|
5 | | -export default Component.extend(UppyUploadMixin, { |
6 | | - classNames: ["wizard-field-upload"], |
7 | | - classNameBindings: ["isImage", "fieldClass"], |
8 | | - uploading: false, |
9 | | - type: computed(function () { |
10 | | - return `wizard_${this.field.id}`; |
11 | | - }), |
12 | | - id: computed(function () { |
13 | | - return `wizard_field_upload_${this.field.id}`; |
14 | | - }), |
15 | | - isImage: computed("field.value.extension", function () { |
16 | | - return ( |
17 | | - this.field.value && |
18 | | - this.siteSettings.wizard_recognised_image_upload_formats |
19 | | - .split("|") |
20 | | - .includes(this.field.value.extension) |
21 | | - ); |
22 | | - }), |
| 9 | +export default class CustomWizardFieldUpload extends Component { |
| 10 | + @service siteSettings; |
23 | 11 |
|
24 | | - uploadDone(upload) { |
25 | | - this.set("field.value", upload); |
26 | | - }, |
27 | | -}); |
| 12 | + @action |
| 13 | + setup() { |
| 14 | + this.uppyUpload = new UppyUpload(getOwner(this), { |
| 15 | + id: this.inputId, |
| 16 | + type: `wizard_${this.field.id}`, |
| 17 | + uploadDone: (upload) => { |
| 18 | + this.setProperties({ |
| 19 | + "field.value": upload, |
| 20 | + isImage: this.imageUploadFormats.includes(upload.extension), |
| 21 | + }); |
| 22 | + this.done(); |
| 23 | + }, |
| 24 | + }); |
| 25 | + this.uppyUpload.setup(document.getElementById(this.inputId)); |
| 26 | + } |
| 27 | + |
| 28 | + get imageUploadFormats() { |
| 29 | + return this.siteSettings.wizard_recognised_image_upload_formats.split("|"); |
| 30 | + } |
| 31 | + |
| 32 | + get inputId() { |
| 33 | + return `wizard_field_upload_${this.field?.id}`; |
| 34 | + } |
| 35 | + |
| 36 | + get wrapperClass() { |
| 37 | + let result = "wizard-field-upload"; |
| 38 | + if (this.isImage) { |
| 39 | + result += " is-image"; |
| 40 | + } |
| 41 | + if (this.fieldClass) { |
| 42 | + result += ` ${this.fieldClass}`; |
| 43 | + } |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + @discourseComputed("uppyUpload.uploading", "uppyUpload.uploadProgress") |
| 48 | + uploadLabel() { |
| 49 | + return this.uppyUpload?.uploading |
| 50 | + ? `${I18n.t("wizard.uploading")} ${this.uppyUpload.uploadProgress}%` |
| 51 | + : I18n.t("wizard.upload"); |
| 52 | + } |
| 53 | + |
| 54 | + @action |
| 55 | + chooseFiles() { |
| 56 | + this.uppyUpload?.openPicker(); |
| 57 | + } |
| 58 | +} |
0 commit comments