diff --git a/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 b/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 index 38a6186c0..df6e8d8bc 100644 --- a/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 +++ b/app/assets/javascripts/discourse/initializers/post-decorations.js.es6 @@ -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); + }); } }; diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 156f74a31..3f9d17c4a 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -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); } } diff --git a/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 b/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 index b7fdf4202..367607f8f 100644 --- a/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 +++ b/plugins/discourse-details/assets/javascripts/initializers/apply-details.js.es6 @@ -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()); + }); } - }; diff --git a/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 b/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 index 1f1fbe7fc..3453c425f 100644 --- a/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 +++ b/plugins/lazyYT/assets/javascripts/initializers/lazyYT.js.es6 @@ -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()); + }); } };