DEV: Allow a simplier way to register a link under sidebar topics section (#16916)

This commit is contained in:
Alan Guo Xiang Tan 2022-05-26 09:01:37 +08:00 committed by GitHub
parent 641c4e0b7a
commit 037436047d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 13 deletions

View File

@ -1634,11 +1634,15 @@ class PluginApi {
* api.addTopicsSectionLink((baseSectionLink) => {
* return class CustomSectionLink extends baseSectionLink {
* get name() {
* returns "bookmarked"
* returns "bookmarked";
* }
*
* get route() {
* returns "userActivity.bookmarks"
* returns "userActivity.bookmarks";
* }
*
* get model() {
* return this.currentUser;
* }
*
* get title() {
@ -1652,14 +1656,29 @@ class PluginApi {
* })
* ```
*
* @callback addTopicsSectionLinkCallback
* @param {BaseSectionLink} baseSectionLink Factory class to inherit from.
* @returns {BaseSectionLink} A class that extends BaseSectionLink.
* or
*
* @param {addTopicsSectionLinkCallback} callback
* ```
* api.addTopicsSectionLink({
* name: "unread",
* route: "discovery.unread",
* title: I18n.t("some.unread.title"),
* text: I18n.t("some.unread.text")
* })
* ```
*
* @callback addTopicsSectionLinkCallback
* @param {BaseSectionLink} baseSectionLink - Factory class to inherit from.
* @returns {BaseSectionLink} - A class that extends BaseSectionLink.
*
* @param {(addTopicsSectionLinkCallback|Object)} arg - A callback function or an Object.
* @param {string} arg.name - The name of the link. Needs to be dasherized and lowercase.
* @param {string} arg.route - The Ember route of the link.
* @param {string} arg.title - The title attribute for the link.
* @param {string} arg.text - The text to display for the link.
*/
async addTopicsSectionLink(callback) {
addSectionLink(callback);
addTopicsSectionLink(arg) {
addSectionLink(arg);
}
}

View File

@ -8,10 +8,36 @@ export let customSectionLinks = [];
* @param {BaseSectionLink} baseSectionLink Factory class to inherit from.
* @returns {BaseSectionLink} A class that extends BaseSectionLink.
*
* @param {addTopicsSectionLinkCallback} callback
* @param {(addTopicsSectionLinkCallback|Object)} arg - A callback function or an Object.
* @param {string} arg.name - The name of the link. Needs to be dasherized and lowercase.
* @param {string} arg.route - The Ember route of the link.
* @param {string} arg.title - The title attribute for the link.
* @param {string} arg.text - The text to display for the link.
*/
export function addSectionLink(callback) {
customSectionLinks.push(callback.call(this, BaseSectionLink));
export function addSectionLink(arg) {
if (typeof arg === "function") {
customSectionLinks.push(arg.call(this, BaseSectionLink));
} else {
const klass = class extends BaseSectionLink {
get name() {
return arg.name;
}
get route() {
return arg.route;
}
get text() {
return arg.text;
}
get title() {
return arg.title;
}
};
customSectionLinks.push(klass);
}
}
export function resetDefaultSectionLinks() {

View File

@ -8,7 +8,7 @@ export default class BaseSectionLink {
}
/**
* @returns {string} The name of the section link
* @returns {string} The name of the section link. Needs to be dasherized and lowercase.
*/
get name() {
this._notImplemented();

View File

@ -410,7 +410,40 @@ acceptance("Sidebar - Topics Section", function (needs) {
);
conditionalTest(
"adding section link via plugin API",
"adding section link via plugin API with Object",
!isLegacyEmber(),
async function (assert) {
withPluginApi("1.2.0", (api) => {
api.addTopicsSectionLink({
name: "unread",
route: "discovery.unread",
text: "unread topics",
title: "List of unread topics",
});
});
await visit("/");
assert.strictEqual(
query(".sidebar-section-link-unread").textContent.trim(),
"unread topics",
"displays the right text for the link"
);
assert.strictEqual(
query(".sidebar-section-link-unread").title,
"List of unread topics",
"displays the right title for the link"
);
await click(".sidebar-section-link-unread");
assert.strictEqual(currentURL(), "/unread", "links to the right URL");
}
);
conditionalTest(
"adding section link via plugin API with callback function",
!isLegacyEmber(),
async function (assert) {
withPluginApi("1.2.0", (api) => {