`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 highlightSyntax from 'discourse/lib/highlight-syntax';
import lightbox from 'discourse/lib/lightbox'; import lightbox from 'discourse/lib/lightbox';
import { getPluginApi } from 'discourse/lib/plugin-api'; import { withPluginApi } from 'discourse/lib/plugin-api';
export default { export default {
name: "post-decorations", name: "post-decorations",
initialize() { initialize() {
const api = getPluginApi('0.1'); withPluginApi('0.1', api => {
api.decorateCooked(highlightSyntax);
api.decorateCooked(highlightSyntax); api.decorateCooked(lightbox);
api.decorateCooked(lightbox); });
} }
}; };

View File

@ -67,13 +67,20 @@ class PluginApi {
} }
let _pluginv01; let _pluginv01;
export function getPluginApi(version) { function getPluginApi(version) {
if (version === "0.1") { if (version === "0.1") {
if (!_pluginv01) { if (!_pluginv01) {
_pluginv01 = new PluginApi(version, Discourse.__container__); _pluginv01 = new PluginApi(version, Discourse.__container__);
} }
return _pluginv01; return _pluginv01;
} else { } 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 { export default {
name: "apply-details", name: "apply-details",
initialize() { initialize() {
const api = getPluginApi('0.1'); withPluginApi('0.1', api => {
api.decorateCooked($elem => $("details", $elem).details()); 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 { export default {
name: "apply-lazyYT", name: "apply-lazyYT",
initialize() { initialize() {
const api = getPluginApi('0.1'); withPluginApi('0.1', api => {
api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT()); api.decorateCooked($elem => $('.lazyYT', $elem).lazyYT());
});
} }
}; };