API for extra post menu buttons and support for solved button
This commit is contained in:
parent
98f4b3f8a9
commit
c0a6e34610
|
@ -1,6 +1,8 @@
|
||||||
import { addDecorator } from 'discourse/widgets/post-cooked';
|
import { addDecorator } from 'discourse/widgets/post-cooked';
|
||||||
import ComposerEditor from 'discourse/components/composer-editor';
|
import ComposerEditor from 'discourse/components/composer-editor';
|
||||||
import { addPosterIcon } from 'discourse/widgets/poster-name';
|
import { addPosterIcon } from 'discourse/widgets/poster-name';
|
||||||
|
import { addButton } from 'discourse/widgets/post-menu';
|
||||||
|
import { includeAttributes } from 'discourse/lib/transform-post';
|
||||||
|
|
||||||
let _decorateId = 0;
|
let _decorateId = 0;
|
||||||
function decorate(klass, evt, cb) {
|
function decorate(klass, evt, cb) {
|
||||||
|
@ -17,6 +19,12 @@ class PluginApi {
|
||||||
constructor(version, container) {
|
constructor(version, container) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.container = container;
|
this.container = container;
|
||||||
|
|
||||||
|
this._currentUser = container.lookup('current-user:main');
|
||||||
|
}
|
||||||
|
|
||||||
|
getCurrentUser() {
|
||||||
|
return this._currentUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,6 +72,20 @@ class PluginApi {
|
||||||
addPosterIcon(cb) {
|
addPosterIcon(cb) {
|
||||||
addPosterIcon(cb);
|
addPosterIcon(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attachWidgetAction(widget, actionName, fn) {
|
||||||
|
const widgetClass = this.container.lookupFactory(`widget:${widget}`);
|
||||||
|
widgetClass.prototype[actionName] = fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
includePostAttributes(...attributes) {
|
||||||
|
includeAttributes(...attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
addPostMenuButton(name, callback) {
|
||||||
|
addButton(name, callback);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let _pluginv01;
|
let _pluginv01;
|
||||||
|
|
|
@ -10,6 +10,12 @@ function actionDescription(action, acted, count) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const _additionalAttributes = [];
|
||||||
|
|
||||||
|
export function includeAttributes(...attributes) {
|
||||||
|
attributes.forEach(a => _additionalAttributes.push(a));
|
||||||
|
}
|
||||||
|
|
||||||
export function transformBasicPost(post) {
|
export function transformBasicPost(post) {
|
||||||
// Note: it can be dangerous to not use `get` in Ember code, but this is significantly
|
// Note: it can be dangerous to not use `get` in Ember code, but this is significantly
|
||||||
// faster and has tests to confirm it works. We only call `get` when the property is a CP
|
// faster and has tests to confirm it works. We only call `get` when the property is a CP
|
||||||
|
@ -189,5 +195,7 @@ export default function transformPost(currentUser, site, post, prevPost, nextPos
|
||||||
postAtts.canDelete = !postAtts.isDeleted && postAtts.canDelete;
|
postAtts.canDelete = !postAtts.isDeleted && postAtts.canDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_additionalAttributes.forEach(a => postAtts[a] = post[a]);
|
||||||
|
|
||||||
return postAtts;
|
return postAtts;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,11 @@ function animateHeart($elem, start, end, complete) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const _builders = {};
|
const _builders = {};
|
||||||
|
const _extraButtons = {};
|
||||||
|
|
||||||
|
export function addButton(name, builder) {
|
||||||
|
_extraButtons[name] = builder;
|
||||||
|
}
|
||||||
|
|
||||||
function registerButton(name, builder) {
|
function registerButton(name, builder) {
|
||||||
_builders[name] = builder;
|
_builders[name] = builder;
|
||||||
|
@ -246,6 +251,39 @@ export default createWidget('post-menu', {
|
||||||
visibleButtons.splice(visibleButtons.length - 1, 0, showMore);
|
visibleButtons.splice(visibleButtons.length - 1, 0, showMore);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.keys(_extraButtons).forEach(k => {
|
||||||
|
const builder = _extraButtons[k];
|
||||||
|
if (builder) {
|
||||||
|
const buttonAtts = builder(attrs, this.state, this.siteSettings);
|
||||||
|
if (buttonAtts) {
|
||||||
|
const { position, beforeButton } = buttonAtts;
|
||||||
|
delete buttonAtts.position;
|
||||||
|
|
||||||
|
let button = this.attach('button', buttonAtts);
|
||||||
|
|
||||||
|
if (beforeButton) {
|
||||||
|
button = h('span', [beforeButton(h), button]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (button) {
|
||||||
|
switch(position) {
|
||||||
|
case 'first':
|
||||||
|
visibleButtons.unshift(button);
|
||||||
|
break;
|
||||||
|
case 'second-last-hidden':
|
||||||
|
if (!state.collapsed) {
|
||||||
|
visibleButtons.splice(visibleButtons.length-2, 0, button);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
visibleButtons.push(button);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const postControls = [];
|
const postControls = [];
|
||||||
|
|
||||||
const repliesButton = this.attachButton('replies', attrs);
|
const repliesButton = this.attachButton('replies', attrs);
|
||||||
|
|
|
@ -379,7 +379,6 @@ export default createWidget('post', {
|
||||||
const likeAction = post.get('likeAction');
|
const likeAction = post.get('likeAction');
|
||||||
|
|
||||||
if (likeAction && likeAction.get('canToggle')) {
|
if (likeAction && likeAction.get('canToggle')) {
|
||||||
|
|
||||||
const promise = likeAction.togglePromise(post);
|
const promise = likeAction.togglePromise(post);
|
||||||
this.scheduleRerender();
|
this.scheduleRerender();
|
||||||
return promise;
|
return promise;
|
||||||
|
|
|
@ -75,6 +75,7 @@ export default class Widget {
|
||||||
this.siteSettings = container.lookup('site-settings:main');
|
this.siteSettings = container.lookup('site-settings:main');
|
||||||
this.currentUser = container.lookup('current-user:main');
|
this.currentUser = container.lookup('current-user:main');
|
||||||
this.store = container.lookup('store:main');
|
this.store = container.lookup('store:main');
|
||||||
|
this.appEvents = container.lookup('app-events:main');
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultState() {
|
defaultState() {
|
||||||
|
|
Loading…
Reference in New Issue