mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 01:31:35 +08:00
DEV: Add remove button function to PluginAPI (#9627)
This commit is contained in:
@ -5,7 +5,7 @@ import { addPluginOutletDecorator } from "discourse/components/plugin-connector"
|
|||||||
import { addTopicTitleDecorator } from "discourse/components/topic-title";
|
import { addTopicTitleDecorator } from "discourse/components/topic-title";
|
||||||
import ComposerEditor from "discourse/components/composer-editor";
|
import ComposerEditor from "discourse/components/composer-editor";
|
||||||
import DiscourseBanner from "discourse/components/discourse-banner";
|
import DiscourseBanner from "discourse/components/discourse-banner";
|
||||||
import { addButton } from "discourse/widgets/post-menu";
|
import { addButton, removeButton } from "discourse/widgets/post-menu";
|
||||||
import { includeAttributes } from "discourse/lib/transform-post";
|
import { includeAttributes } from "discourse/lib/transform-post";
|
||||||
import { registerHighlightJSLanguage } from "discourse/lib/highlight-syntax";
|
import { registerHighlightJSLanguage } from "discourse/lib/highlight-syntax";
|
||||||
import { addToolbarCallback } from "discourse/components/d-editor";
|
import { addToolbarCallback } from "discourse/components/d-editor";
|
||||||
@ -399,11 +399,25 @@ class PluginApi {
|
|||||||
* position: 'first' // can be `first`, `last` or `second-last-hidden`
|
* position: 'first' // can be `first`, `last` or `second-last-hidden`
|
||||||
* };
|
* };
|
||||||
* });
|
* });
|
||||||
|
* ```
|
||||||
**/
|
**/
|
||||||
addPostMenuButton(name, callback) {
|
addPostMenuButton(name, callback) {
|
||||||
addButton(name, callback);
|
addButton(name, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove existing button below a post with your plugin.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* ```
|
||||||
|
* api.removePostMenuButton('like');
|
||||||
|
* ```
|
||||||
|
**/
|
||||||
|
removePostMenuButton(name) {
|
||||||
|
removeButton(name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hook that is called when the editor toolbar is created. You can
|
* A hook that is called when the editor toolbar is created. You can
|
||||||
* use this to add custom editor buttons.
|
* use this to add custom editor buttons.
|
||||||
|
@ -41,6 +41,11 @@ export function addButton(name, builder) {
|
|||||||
_extraButtons[name] = builder;
|
_extraButtons[name] = builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function removeButton(name) {
|
||||||
|
if (_extraButtons[name]) delete _extraButtons[name];
|
||||||
|
if (_builders[name]) delete _builders[name];
|
||||||
|
}
|
||||||
|
|
||||||
function registerButton(name, builder) {
|
function registerButton(name, builder) {
|
||||||
_builders[name] = builder;
|
_builders[name] = builder;
|
||||||
}
|
}
|
||||||
|
44
test/javascripts/widgets/post-menu-test.js
Normal file
44
test/javascripts/widgets/post-menu-test.js
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { moduleForWidget, widgetTest } from "helpers/widget-test";
|
||||||
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||||
|
|
||||||
|
moduleForWidget("post-menu");
|
||||||
|
|
||||||
|
widgetTest("add extra button", {
|
||||||
|
template: '{{mount-widget widget="post-menu" args=args}}',
|
||||||
|
beforeEach() {
|
||||||
|
this.set("args", {});
|
||||||
|
withPluginApi("0.8", api => {
|
||||||
|
api.addPostMenuButton("coffee", () => {
|
||||||
|
return {
|
||||||
|
action: "drinkCoffee",
|
||||||
|
icon: "coffee",
|
||||||
|
className: "hot-coffee",
|
||||||
|
title: "coffee.title",
|
||||||
|
position: "first"
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async test(assert) {
|
||||||
|
assert.ok(
|
||||||
|
find(".actions .extra-buttons .hot-coffee").length === 1,
|
||||||
|
"It renders extra button"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
widgetTest("remove extra button", {
|
||||||
|
template: '{{mount-widget widget="post-menu" args=args}}',
|
||||||
|
beforeEach() {
|
||||||
|
this.set("args", {});
|
||||||
|
withPluginApi("0.8", api => {
|
||||||
|
api.removePostMenuButton("coffee");
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async test(assert) {
|
||||||
|
assert.ok(
|
||||||
|
find(".actions .extra-buttons .hot-coffee").length === 0,
|
||||||
|
"It doesn't removes coffee button"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
Reference in New Issue
Block a user