Skip to content

Commit 691a8e3

Browse files
committed
Convert upload component to new type with improved uppy support
1 parent 8751dc2 commit 691a8e3

File tree

2 files changed

+75
-45
lines changed

2 files changed

+75
-45
lines changed
Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
1-
import UppyUploadMixin from "discourse/mixins/uppy-upload";
1+
import UppyUpload from "discourse/lib/uppy/uppy-upload";
22
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";
48

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;
2311

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+
}
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
<label
2-
class="wizard-btn wizard-btn-upload-file {{if uploading 'disabled'}}"
3-
tabindex={{field.tabindex}}
4-
>
5-
{{#if uploading}}
6-
{{i18n "wizard.uploading"}}
7-
{{else}}
8-
{{i18n "wizard.upload"}}
9-
{{d-icon "upload"}}
10-
{{/if}}
11-
1+
<div class={{this.wrapperClass}}>
122
<input
13-
disabled={{uploading}}
3+
{{did-insert this.setup}}
4+
disabled={{this.uppyUpload.uploading}}
5+
id={{this.inputId}}
146
class="hidden-upload-field"
157
type="file"
16-
accept={{field.file_types}}
8+
accept={{this.field.file_types}}
179
style="visibility: hidden; position: absolute;"
1810
/>
19-
</label>
20-
21-
{{#if field.value}}
22-
{{#if isImage}}
23-
<img src={{field.value.url}} class="wizard-image-preview" />
24-
{{else}}
25-
{{field.value.original_filename}}
11+
<DButton
12+
@translatedLabel={{this.uploadLabel}}
13+
@translatedTitle={{this.uploadLabel}}
14+
@icon="upload"
15+
@disabled={{this.uppyUpload.uploading}}
16+
@action={{this.chooseFiles}}
17+
class="wizard-btn wizard-btn-upload-file"
18+
/>
19+
{{#if this.field.value}}
20+
{{#if this.isImage}}
21+
<img src={{this.field.value.url}} class="wizard-image-preview" />
22+
{{else}}
23+
{{this.field.value.original_filename}}
24+
{{/if}}
2625
{{/if}}
27-
{{/if}}
26+
</div>

0 commit comments

Comments
 (0)