FIX: Evaluate all callbacks rather than override them (#18788)

This commit is contained in:
Natalie Tay 2022-10-31 10:13:56 +08:00 committed by GitHub
parent cfefdf0832
commit 5e4bad0d8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 23 deletions

View File

@ -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
)
);
}

View File

@ -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");
});
});