DEV: Allow plugins to add extra icons to category-link (#9743)

This commit is contained in:
Mark VanLandingham 2020-05-12 09:41:16 -05:00 committed by GitHub
parent 6230f5c554
commit 9bce91f59f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View File

@ -18,6 +18,12 @@ function categoryStripe(color, classes) {
return "<span class='" + classes + "' " + style + "></span>";
}
let _extraIconRenderers = [];
export function addExtraIconRenderer(renderer) {
_extraIconRenderers.push(renderer);
}
/**
Generates category badge HTML
@ -149,12 +155,15 @@ function defaultCategoryLinkRenderer(category, opts) {
}
if (restricted) {
html += `${iconHTML(
"lock"
)}<span class="category-name" ${categoryDir}>${categoryName}</span>`;
} else {
html += `<span class="category-name" ${categoryDir}>${categoryName}</span>`;
html += iconHTML("lock");
}
_extraIconRenderers.forEach(renderer => {
const iconName = renderer(category);
if (iconName) {
html += iconHTML(iconName);
}
});
html += `<span class="category-name" ${categoryDir}>${categoryName}</span>`;
html += "</span>";
if (opts.topicCount && categoryStyle !== "box") {

View File

@ -48,6 +48,7 @@ import {
addComposerUploadMarkdownResolver
} from "discourse/components/composer-editor";
import { addCategorySortCriteria } from "discourse/components/edit-category-settings";
import { addExtraIconRenderer } from "discourse/helpers/category-link";
import { queryRegistry } from "discourse/widgets/widget";
import Composer from "discourse/models/composer";
import { on } from "@ember/object/evented";
@ -1065,6 +1066,22 @@ class PluginApi {
decorateTopicTitle(callback) {
addTopicTitleDecorator(callback);
}
/**
* Allows adding icons to the category-link html
*
* ```
* api.addCategoryLinkIcon((category) => {
* if (category.someProperty) {
return "eye"
}
* });
* ```
*
**/
addCategoryLinkIcon(renderer) {
addExtraIconRenderer(renderer);
}
}
let _pluginv01;