FEATURE: plugin can now extend list of classes for topic-post

This commit is contained in:
Sam 2017-03-03 16:57:25 -05:00
parent 31a81d4eee
commit 3e3fdfc717
2 changed files with 28 additions and 1 deletions

View File

@ -16,9 +16,10 @@ import { addPostSmallActionIcon } from 'discourse/widgets/post-small-action';
import { addDiscoveryQueryParam } from 'discourse/controllers/discovery-sortable';
import { addTagsHtmlCallback } from 'discourse/lib/render-tags';
import { addUserMenuGlyph } from 'discourse/widgets/user-menu';
import { addPostClassesCallback } from 'discourse/widgets/post';
// If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = '0.8.3';
const PLUGIN_API_VERSION = '0.8.4';
class PluginApi {
constructor(version, container) {
@ -425,6 +426,18 @@ class PluginApi {
addUserMenuGlyph(glyph) {
addUserMenuGlyph(glyph);
};
/**
* Adds a callback to be called before rendering any post that
* that returns custom classes to add to the post
*
* Example:
*
* addPostClassesCallback((atts) => {if (atts.post_number == 1) return ["first"];})
**/
addPostClassesCallback(callback) {
addPostClassesCallback(callback);
}
}
let _pluginv01;

View File

@ -376,6 +376,12 @@ createWidget('post-article', {
});
let addPostClassesCallbacks = null;
export function addPostClassesCallback(callback) {
addPostClassesCallbacks = addPostClassesCallbacks || [];
addPostClassesCallbacks.push(callback);
}
export default createWidget('post', {
buildKey: attrs => `post-${attrs.id}`,
shadowTree: true,
@ -405,6 +411,14 @@ export default createWidget('post', {
} else {
classNames.push('regular');
}
if (addPostClassesCallbacks) {
for(let i=0; i<addPostClassesCallbacks.length; i++) {
let pluginClasses = addPostClassesCallbacks[i].call(this, attrs);
if (pluginClasses) {
classNames.push.apply(classNames, pluginClasses);
}
}
}
return classNames;
},