mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Site Customizations can use the plugin api
This commit is contained in:
@@ -6,6 +6,8 @@ define('ember', ['exports'], function(__exports__) {
|
||||
__exports__.default = Ember;
|
||||
});
|
||||
|
||||
var _pluginCallbacks = [];
|
||||
|
||||
window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
|
||||
rootElement: '#main',
|
||||
_docTitle: document.title,
|
||||
@@ -127,6 +129,18 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
|
||||
}
|
||||
});
|
||||
|
||||
// Plugins that are registered via `<script>` tags.
|
||||
var withPluginApi = require('discourse/lib/plugin-api').withPluginApi;
|
||||
var initCount = 0;
|
||||
_pluginCallbacks.forEach(function(cb) {
|
||||
Discourse.instanceInitializer({
|
||||
name: "_discourse_plugin_" + (++initCount),
|
||||
after: 'inject-objects',
|
||||
initialize: function() {
|
||||
withPluginApi(cb.version, cb.code);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
requiresRefresh: function(){
|
||||
@@ -134,6 +148,9 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
|
||||
return desired && Discourse.get("currentAssetVersion") !== desired;
|
||||
}.property("currentAssetVersion", "desiredAssetVersion"),
|
||||
|
||||
_registerPluginCode(version, code) {
|
||||
_pluginCallbacks.push({ version: version, code: code });
|
||||
},
|
||||
|
||||
assetVersion: Ember.computed({
|
||||
get: function() {
|
||||
|
||||
@@ -27,6 +27,17 @@ class SiteCustomization < ActiveRecord::Base
|
||||
raise e
|
||||
end
|
||||
|
||||
def transpile(es6_source, version)
|
||||
template = Tilt::ES6ModuleTranspilerTemplate.new {}
|
||||
wrapped = <<PLUGIN_API_JS
|
||||
Discourse._registerPluginCode('#{version}', api => {
|
||||
#{es6_source}
|
||||
});
|
||||
PLUGIN_API_JS
|
||||
|
||||
template.babel_transpile(wrapped)
|
||||
end
|
||||
|
||||
def process_html(html)
|
||||
doc = Nokogiri::HTML.fragment(html)
|
||||
doc.css('script[type="text/x-handlebars"]').each do |node|
|
||||
@@ -43,6 +54,17 @@ SCRIPT
|
||||
node.replace("<script>#{compiled}</script>")
|
||||
end
|
||||
|
||||
doc.css('script[type="text/discourse-plugin"]').each do |node|
|
||||
if node['version'].present?
|
||||
begin
|
||||
code = transpile(node.inner_html, node['version'])
|
||||
node.replace("<script>#{code}</script>")
|
||||
rescue Tilt::ES6ModuleTranspilerTemplate::JavaScriptError => ex
|
||||
node.replace("<script type='text/discourse-js-error'>#{ex.message}</script>")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
doc.to_s
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user