Skip to content

A platform for community discussion. Free, open, simple.

FIX: Ensure custom composer button 'condition' is run correctly (#26245)

David Taylor5098338a9649
diff --git a/app/assets/javascripts/discourse/app/services/composer.js b/app/assets/javascripts/discourse/app/services/composer.js
index d52dc30b38b..55861ebcba2 100644
--- a/app/assets/javascripts/discourse/app/services/composer.js
+++ b/app/assets/javascripts/discourse/app/services/composer.js
@@ -435,7 +435,7 @@ export default class ComposerService extends Service {
return options.concat(
customPopupMenuOptions
- .map((option) => this._setupPopupMenuOption(option))
+ .map((option) => this._setupPopupMenuOption({ ...option }))
.filter((o) => o)
);
}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
index 19e185f3015..3f81b4b619c 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js
@@ -1394,3 +1394,48 @@ acceptance("Composer - current time", function (needs) {
);
});
});
+
+acceptance("composer buttons API", function (needs) {
+ needs.user();
+ needs.settings({
+ allow_uncategorized_topics: true,
+ });
+
+ test("buttons can be added conditionally", async function (assert) {
+ withPluginApi("0", (api) => {
+ api.addComposerToolbarPopupMenuOption({
+ action: (toolbarEvent) => {
+ toolbarEvent.applySurround("**", "**");
+ },
+ icon: "far-bold",
+ label: "some_label",
+ condition: (composer) => {
+ return composer.model.creatingTopic;
+ },
+ });
+ });
+
+ await visit("/t/internationalization-localization/280");
+
+ await click(".post-controls button.reply");
+ assert.dom(".d-editor-input").exists("the composer input is visible");
+
+ const expectedName = "[en.some_label]";
+ const dropdown = selectKit(".toolbar-popup-menu-options");
+ await dropdown.expand();
+
+ assert.false(
+ dropdown.rowByName(expectedName).exists(),
+ "custom button is not displayed for reply"
+ );
+
+ await visit("/latest");
+ await click("#create-topic");
+
+ await dropdown.expand();
+ assert.true(
+ dropdown.rowByName(expectedName).exists(),
+ "custom button is displayed for new topic"
+ );
+ });
+});