Support for an HTML builder that can create dynamic HTML

This commit is contained in:
Robin Ward 2017-04-17 15:47:21 -04:00
parent 3ccd5eacb4
commit 1363988cd7
5 changed files with 35 additions and 1 deletions

View File

@ -434,6 +434,10 @@ class ApplicationController < ActionController::Base
data.merge! DiscoursePluginRegistry.custom_html
end
DiscoursePluginRegistry.html_builders.each do |name, blk|
data[name] = blk.call
end
MultiJson.dump(data)
end

View File

@ -316,6 +316,11 @@ module ApplicationHelper
end
end
def build_plugin_html(name)
return "" unless allow_plugins?
DiscoursePluginRegistry.build_html(name) || ""
end
def theme_lookup(name)
lookup = Theme.lookup_field(theme_key, mobile_view? ? :mobile : :desktop, name)
lookup.html_safe if lookup

View File

@ -13,7 +13,6 @@ class DiscoursePluginRegistry
attr_writer :handlebars
attr_writer :serialized_current_user_fields
attr_writer :seed_data
attr_accessor :custom_html
def plugins
@ -60,6 +59,10 @@ class DiscoursePluginRegistry
def seed_data
@seed_data ||= HashWithIndifferentAccess.new({})
end
def html_builders
@html_builders ||= {}
end
end
def register_js(filename, options={})
@ -127,6 +130,14 @@ class DiscoursePluginRegistry
self.seed_data[key] = value
end
def self.register_html_builder(name, &block)
html_builders[name] = block
end
def self.build_html(name)
html_builders[name]&.call
end
def javascripts
self.class.javascripts
end
@ -169,6 +180,7 @@ class DiscoursePluginRegistry
sass_variables.clear
serialized_current_user_fields
asset_globs.clear
html_builders.clear
end
def self.setup(plugin_class)

View File

@ -238,6 +238,10 @@ class Plugin::Instance
DiscoursePluginRegistry.custom_html.merge!(hash)
end
def register_html_builder(name, &block)
DiscoursePluginRegistry.register_html_builder(name, &block)
end
def register_asset(file, opts=nil)
full_path = File.dirname(path) << "/assets/" << file
assets << [full_path, opts]

View File

@ -44,6 +44,15 @@ describe DiscoursePluginRegistry do
end
end
context '.register_html_builder' do
it "can register and build html" do
DiscoursePluginRegistry.register_html_builder(:my_html) { "<b>my html</b>" }
expect(DiscoursePluginRegistry.build_html(:my_html)).to eq('<b>my html</b>')
DiscoursePluginRegistry.reset!
expect(DiscoursePluginRegistry.build_html(:my_html)).to be_blank
end
end
context '.register_css' do
before do
registry_instance.register_css('hello.css')