mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 13:41:31 +08:00
FEATURE: Extend plugin API to add multiple poster icons (#15311)
This commit is contained in:
parent
6fe4c8ae58
commit
77781f9a11
|
@ -97,7 +97,7 @@ import { downloadCalendar } from "discourse/lib/download-calendar";
|
||||||
// based on Semantic Versioning 2.0.0. Please up the changelog at
|
// based on Semantic Versioning 2.0.0. Please up the changelog at
|
||||||
// docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md whenever you change the version
|
// docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md whenever you change the version
|
||||||
// using the format described at https://keepachangelog.com/en/1.0.0/.
|
// using the format described at https://keepachangelog.com/en/1.0.0/.
|
||||||
const PLUGIN_API_VERSION = "1.0.0";
|
const PLUGIN_API_VERSION = "1.1.0";
|
||||||
|
|
||||||
// This helper prevents us from applying the same `modifyClass` over and over in test mode.
|
// This helper prevents us from applying the same `modifyClass` over and over in test mode.
|
||||||
function canModify(klass, type, resolverName, changes) {
|
function canModify(klass, type, resolverName, changes) {
|
||||||
|
@ -331,12 +331,22 @@ class PluginApi {
|
||||||
/**
|
/**
|
||||||
* addPosterIcon(callback)
|
* addPosterIcon(callback)
|
||||||
*
|
*
|
||||||
* This function can be used to add an icon with a link that will be displayed
|
* This function is an alias of addPosterIcons, which the latter has the ability
|
||||||
* beside a poster's name. The `callback` is called with the post's user custom
|
* to add multiple icons at once. Please refer to `addPosterIcons` for usage examples.
|
||||||
* fields and post attributes. An icon will be rendered if the callback returns
|
**/
|
||||||
* an object with the appropriate attributes.
|
addPosterIcon(cb) {
|
||||||
|
this.addPosterIcons(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addPosterIcons(callback)
|
||||||
*
|
*
|
||||||
* The returned object can have the following attributes:
|
* This function can be used to add one, or multiple icons, with a link that will
|
||||||
|
* be displayed beside a poster's name. The `callback` is called with the post's
|
||||||
|
* user custom fields and post attributes. One or multiple icons may be rendered
|
||||||
|
* when the callback returns an array of objects with the appropriate attributes.
|
||||||
|
*
|
||||||
|
* The returned object(s) each can have the following attributes:
|
||||||
*
|
*
|
||||||
* icon the font awesome icon to render
|
* icon the font awesome icon to render
|
||||||
* emoji an emoji icon to render
|
* emoji an emoji icon to render
|
||||||
|
@ -346,49 +356,70 @@ class PluginApi {
|
||||||
* text (optional) text to display alongside the emoji or icon
|
* text (optional) text to display alongside the emoji or icon
|
||||||
*
|
*
|
||||||
* ```
|
* ```
|
||||||
* api.addPosterIcon((cfs, attrs) => {
|
* api.addPosterIcons((cfs, attrs) => {
|
||||||
* if (cfs.customer) {
|
* if (cfs.customer) {
|
||||||
* return { icon: 'user', className: 'customer', title: 'customer' };
|
* return { icon: 'user', className: 'customer', title: 'customer' };
|
||||||
* }
|
* }
|
||||||
* });
|
* });
|
||||||
* ```
|
* ```
|
||||||
|
* or
|
||||||
|
* * ```
|
||||||
|
* api.addPosterIcons((cfs, attrs) => {
|
||||||
|
* return attrs.customers.map(({name}) => {
|
||||||
|
* icon: 'user', className: 'customer', title: name
|
||||||
|
* })
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
**/
|
**/
|
||||||
addPosterIcon(cb) {
|
addPosterIcons(cb) {
|
||||||
const site = this._lookupContainer("site:main");
|
const site = this._lookupContainer("site:main");
|
||||||
const loc = site && site.mobileView ? "before" : "after";
|
const loc = site && site.mobileView ? "before" : "after";
|
||||||
|
|
||||||
decorateWidget(`poster-name:${loc}`, (dec) => {
|
decorateWidget(`poster-name:${loc}`, (dec) => {
|
||||||
const attrs = dec.attrs;
|
const attrs = dec.attrs;
|
||||||
const result = cb(attrs.userCustomFields || {}, attrs);
|
let results = cb(attrs.userCustomFields || {}, attrs);
|
||||||
|
|
||||||
if (result) {
|
if (results) {
|
||||||
let iconBody;
|
if (!Array.isArray(results)) {
|
||||||
|
results = [results];
|
||||||
if (result.icon) {
|
|
||||||
iconBody = iconNode(result.icon);
|
|
||||||
} else if (result.emoji) {
|
|
||||||
iconBody = result.emoji.split("|").map((name) => {
|
|
||||||
let widgetAttrs = { name };
|
|
||||||
if (result.emojiTitle) {
|
|
||||||
widgetAttrs.title = true;
|
|
||||||
}
|
|
||||||
return dec.attach("emoji", widgetAttrs);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.text) {
|
return results.map((result) => {
|
||||||
iconBody = [iconBody, result.text];
|
let iconBody;
|
||||||
}
|
|
||||||
|
|
||||||
if (result.url) {
|
if (result.icon) {
|
||||||
iconBody = dec.h("a", { attributes: { href: result.url } }, iconBody);
|
iconBody = iconNode(result.icon);
|
||||||
}
|
} else if (result.emoji) {
|
||||||
|
iconBody = result.emoji.split("|").map((name) => {
|
||||||
|
let widgetAttrs = { name };
|
||||||
|
if (result.emojiTitle) {
|
||||||
|
widgetAttrs.title = true;
|
||||||
|
}
|
||||||
|
return dec.attach("emoji", widgetAttrs);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return dec.h(
|
if (result.text) {
|
||||||
"span.poster-icon",
|
iconBody = [iconBody, result.text];
|
||||||
{ className: result.className, attributes: { title: result.title } },
|
}
|
||||||
iconBody
|
|
||||||
);
|
if (result.url) {
|
||||||
|
iconBody = dec.h(
|
||||||
|
"a",
|
||||||
|
{ attributes: { href: result.url } },
|
||||||
|
iconBody
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dec.h(
|
||||||
|
"span.poster-icon",
|
||||||
|
{
|
||||||
|
className: result.className,
|
||||||
|
attributes: { title: result.title },
|
||||||
|
},
|
||||||
|
iconBody
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,13 @@ in this file..
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [1.1.0] - 2021-12-15
|
||||||
|
### Added
|
||||||
|
- Adds `addPosterIcons`, which allows users to add multiple icons to a poster. The
|
||||||
|
addition of this function also makes the existing `addPosterIcon` now an alias to this
|
||||||
|
function. Users may now just use `addPosterIcons` for both one or many icons. This
|
||||||
|
function allows users to now return many icons depending on an `attrs`.
|
||||||
|
|
||||||
## [1.0.0] - 2021-11-25
|
## [1.0.0] - 2021-11-25
|
||||||
### Removed
|
### Removed
|
||||||
- Removes the `addComposerUploadProcessor` function, which is no longer used in
|
- Removes the `addComposerUploadProcessor` function, which is no longer used in
|
||||||
|
|
Loading…
Reference in New Issue
Block a user