Skip to content

Commit 9e1dfd9

Browse files
committed
FIX: simplify wizard composer event handling
1 parent 7386be3 commit 9e1dfd9

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

assets/javascripts/discourse/components/custom-wizard-composer-editor.js.es6

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ComposerEditor from "discourse/components/composer-editor";
22
import {
3+
bind,
34
default as discourseComputed,
45
on,
56
} from "discourse-common/utils/decorators";
@@ -12,6 +13,9 @@ import Site from "discourse/models/site";
1213
import { uploadIcon } from "discourse/lib/uploads";
1314
import { dasherize } from "@ember/string";
1415

16+
const IMAGE_MARKDOWN_REGEX =
17+
/!\[(.*?)\|(\d{1,4}x\d{1,4})(,\s*\d{1,3}%)?(.*?)\]\((upload:\/\/.*?)\)(?!(.*`))/g;
18+
1519
export default ComposerEditor.extend({
1620
classNameBindings: ["fieldClass"],
1721
allowUpload: true,
@@ -25,7 +29,7 @@ export default ComposerEditor.extend({
2529
popupMenuOptions: [],
2630
draftStatus: "null",
2731
replyPlaceholder: alias("field.translatedPlaceholder"),
28-
uploadingFieldId: null,
32+
wizardEventFieldId: null,
2933

3034
@on("didInsertElement")
3135
_composerEditorInit() {
@@ -76,7 +80,6 @@ export default ComposerEditor.extend({
7680

7781
const wizardEventNames = ["insert-text", "replace-text"];
7882
const eventPrefix = this.eventPrefix;
79-
const session = this.get("session");
8083
this.appEvents.reopen({
8184
trigger(name, ...args) {
8285
let eventParts = name.split(":");
@@ -87,26 +90,8 @@ export default ComposerEditor.extend({
8790
currentEventPrefix !== "wizard-editor" &&
8891
wizardEventNames.some((wen) => wen === currentEventName)
8992
) {
90-
let wizardName = name.replace(eventPrefix, "wizard-editor");
91-
if (currentEventName === "insert-text") {
92-
args = {
93-
text: args[0],
94-
};
95-
}
96-
if (currentEventName === "replace-text") {
97-
args = {
98-
oldVal: args[0],
99-
newVal: args[1],
100-
};
101-
}
102-
let wizardArgs = Object.assign(
103-
{},
104-
{
105-
fieldId: session.get("uploadingFieldId"),
106-
},
107-
args
108-
);
109-
return this._super(wizardName, wizardArgs);
93+
let wizardEventName = name.replace(eventPrefix, "wizard-editor");
94+
return this._super(wizardEventName, ...args);
11095
} else {
11196
return this._super(name, ...args);
11297
}
@@ -138,6 +123,28 @@ export default ComposerEditor.extend({
138123
}
139124
},
140125

126+
@bind
127+
_handleImageDeleteButtonClick(event) {
128+
if (!event.target.classList.contains("delete-image-button")) {
129+
return;
130+
}
131+
132+
const index = parseInt(
133+
event.target.closest(".button-wrapper").dataset.imageIndex,
134+
10
135+
);
136+
const matchingPlaceholder =
137+
this.get("composer.reply").match(IMAGE_MARKDOWN_REGEX);
138+
139+
this.session.set("wizardEventFieldId", this.field.id);
140+
this.appEvents.trigger(
141+
"composer:replace-text",
142+
matchingPlaceholder[index],
143+
"",
144+
{ regex: IMAGE_MARKDOWN_REGEX, index }
145+
);
146+
},
147+
141148
actions: {
142149
extraButtons(toolbar) {
143150
const component = this;
@@ -213,7 +220,7 @@ export default ComposerEditor.extend({
213220
},
214221

215222
showUploadModal() {
216-
this.session.set("uploadingFieldId", this.field.id);
223+
this.session.set("wizardEventFieldId", this.field.id);
217224
document.getElementById(this.fileUploadElementId).click();
218225
},
219226
},

assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@ export default {
4747
}
4848
},
4949
});
50+
51+
api.modifyClass("component:d-editor", {
52+
pluginId: "custom-wizard",
53+
54+
didInsertElement() {
55+
this._super(...arguments);
56+
57+
if (this.wizardComposer) {
58+
this.appEvents.on(
59+
`wizard-editor:insert-text`,
60+
this,
61+
"_wizardInsertText"
62+
);
63+
this.appEvents.on(
64+
"wizard-editor:replace-text",
65+
this,
66+
"_wizardReplaceText"
67+
);
68+
}
69+
},
70+
71+
_wizardInsertText(text, options) {
72+
if (this.session.wizardEventFieldId === this.fieldId) {
73+
this.insertText(text, options);
74+
}
75+
},
76+
77+
_wizardReplaceText(oldVal, newVal, opts = {}) {
78+
if (this.session.wizardEventFieldId === this.fieldId) {
79+
this.replaceText(oldVal, newVal, opts);
80+
}
81+
},
82+
});
5083
});
5184
},
5285
};

assets/javascripts/discourse/templates/components/custom-wizard-composer-editor.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
validation=validation
1212
loading=composer.loading
1313
showLink=showLink
14-
wizardComposerEvents=true
14+
wizardComposer=true
1515
fieldId=field.id
1616
disabled=disableTextarea
1717
outletArgs=(hash composer=composer editorType="composer")}}

0 commit comments

Comments
 (0)