diff --git a/app/assets/javascripts/discourse/app/widgets/post-menu.js b/app/assets/javascripts/discourse/app/widgets/post-menu.js index c3e186caed1..aa7df8b2235 100644 --- a/app/assets/javascripts/discourse/app/widgets/post-menu.js +++ b/app/assets/javascripts/discourse/app/widgets/post-menu.js @@ -19,7 +19,7 @@ const VIBRATE_DURATION = 5; const _builders = {}; export let apiExtraButtons = {}; let _extraButtons = {}; -let _buttonsToRemove = {}; +let _buttonsToRemoveCallbacks = {}; export function addButton(name, builder) { _extraButtons[name] = builder; @@ -31,17 +31,13 @@ export function resetPostMenuExtraButtons() { } _extraButtons = {}; - _buttonsToRemove = {}; + _buttonsToRemoveCallbacks = {}; } export function removeButton(name, callback) { - if (callback) { - _buttonsToRemove[name] = callback; - } else { - _buttonsToRemove[name] = () => { - return true; - }; - } + // 🏌️ + _buttonsToRemoveCallbacks[name] ??= []; + _buttonsToRemoveCallbacks[name].push(callback || (() => true)); } function registerButton(name, builder) { @@ -53,13 +49,9 @@ export function buildButton(name, widget) { let shouldAddButton = true; - if (_buttonsToRemove[name]) { - shouldAddButton = !_buttonsToRemove[name]( - attrs, - state, - siteSettings, - settings, - currentUser + if (_buttonsToRemoveCallbacks[name]) { + shouldAddButton = !_buttonsToRemoveCallbacks[name].some((c) => + c(attrs, state, siteSettings, settings, currentUser) ); } @@ -555,13 +547,15 @@ export default createWidget("post-menu", { Object.values(_extraButtons).forEach((builder) => { let shouldAddButton = true; - if (_buttonsToRemove[name]) { - shouldAddButton = !_buttonsToRemove[name]( - attrs, - this.state, - this.siteSettings, - this.settings, - this.currentUser + if (_buttonsToRemoveCallbacks[name]) { + shouldAddButton = !_buttonsToRemoveCallbacks[name].some((c) => + c( + attrs, + this.state, + this.siteSettings, + this.settings, + this.currentUser + ) ); } diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-menu-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-menu-test.js index 776d942cb7c..60f26df3f64 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-menu-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-menu-test.js @@ -75,4 +75,17 @@ module("Integration | Component | Widget | post-menu", function (hooks) { assert.ok(!exists(".actions .reply"), "it removes reply button"); }); + + test("removes button when any callback evaluates to true", async function (assert) { + this.set("args", {}); + + withPluginApi("0.14.0", (api) => { + api.removePostMenuButton("reply", () => true); + api.removePostMenuButton("reply", () => false); + }); + + await render(hbs``); + + assert.ok(!exists(".actions .reply"), "it removes reply button"); + }); });