mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 12:43:54 -06:00
FIX: Evaluate all callbacks rather than override them (#18788)
This commit is contained in:
parent
cfefdf0832
commit
5e4bad0d8f
@ -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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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`<MountWidget @widget="post-menu" @args={{this.args}} />`);
|
||||
|
||||
assert.ok(!exists(".actions .reply"), "it removes reply button");
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user