withPluginApi means you don't have to check for null.

This commit is contained in:
Robin Ward 2016-02-16 12:17:22 -05:00
parent 3ea1f88cdc
commit 3a78321c63
4 changed files with 22 additions and 14 deletions

View file

@ -1,13 +1,13 @@
import highlightSyntax from 'discourse/lib/highlight-syntax';
import lightbox from 'discourse/lib/lightbox';
import { getPluginApi } from 'discourse/lib/plugin-api';
import { withPluginApi } from 'discourse/lib/plugin-api';
export default {
name: "post-decorations",
initialize() {
const api = getPluginApi('0.1');
api.decorateCooked(highlightSyntax);
api.decorateCooked(lightbox);
withPluginApi('0.1', api => {
api.decorateCooked(highlightSyntax);
api.decorateCooked(lightbox);
});
}
};

View file

@ -67,13 +67,20 @@ class PluginApi {
}
let _pluginv01;
export function getPluginApi(version) {
function getPluginApi(version) {
if (version === "0.1") {
if (!_pluginv01) {
_pluginv01 = new PluginApi(version, Discourse.__container__);
}
return _pluginv01;
} else {
throw `Plugin API v${version} is not supported`;
console.warn(`Plugin API v${version} is not supported`);
}
}
export function withPluginApi(version, cb) {
const api = getPluginApi(version);
if (api) {
cb(api);
}
}

View file

@ -1,11 +1,11 @@
import { getPluginApi } from 'discourse/lib/plugin-api';
import { withPluginApi } from 'discourse/lib/plugin-api';
export default {
name: "apply-details",
initialize() {
const api = getPluginApi('0.1');
api.decorateCooked($elem => $("details", $elem).details());
withPluginApi('0.1', api => {
api.decorateCooked($elem => $("details", $elem).details());
});
}
};

View file

@ -1,9 +1,10 @@
import { getPluginApi } from 'discourse/lib/plugin-api';
import { withPluginApi } from 'discourse/lib/plugin-api';
export default {
name: "apply-lazyYT",
initialize() {
const api = getPluginApi('0.1');
api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT());
withPluginApi('0.1', api => {
api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT());
});
}
};