mirror of
https://github.com/discourse/discourse.git
synced 2025-02-23 22:04:27 +08:00
Support for an HTML builder that can create dynamic HTML
This commit is contained in:
parent
3ccd5eacb4
commit
1363988cd7
@ -434,6 +434,10 @@ class ApplicationController < ActionController::Base
|
|||||||
data.merge! DiscoursePluginRegistry.custom_html
|
data.merge! DiscoursePluginRegistry.custom_html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
DiscoursePluginRegistry.html_builders.each do |name, blk|
|
||||||
|
data[name] = blk.call
|
||||||
|
end
|
||||||
|
|
||||||
MultiJson.dump(data)
|
MultiJson.dump(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -316,6 +316,11 @@ module ApplicationHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_plugin_html(name)
|
||||||
|
return "" unless allow_plugins?
|
||||||
|
DiscoursePluginRegistry.build_html(name) || ""
|
||||||
|
end
|
||||||
|
|
||||||
def theme_lookup(name)
|
def theme_lookup(name)
|
||||||
lookup = Theme.lookup_field(theme_key, mobile_view? ? :mobile : :desktop, name)
|
lookup = Theme.lookup_field(theme_key, mobile_view? ? :mobile : :desktop, name)
|
||||||
lookup.html_safe if lookup
|
lookup.html_safe if lookup
|
||||||
|
@ -13,7 +13,6 @@ class DiscoursePluginRegistry
|
|||||||
attr_writer :handlebars
|
attr_writer :handlebars
|
||||||
attr_writer :serialized_current_user_fields
|
attr_writer :serialized_current_user_fields
|
||||||
attr_writer :seed_data
|
attr_writer :seed_data
|
||||||
|
|
||||||
attr_accessor :custom_html
|
attr_accessor :custom_html
|
||||||
|
|
||||||
def plugins
|
def plugins
|
||||||
@ -60,6 +59,10 @@ class DiscoursePluginRegistry
|
|||||||
def seed_data
|
def seed_data
|
||||||
@seed_data ||= HashWithIndifferentAccess.new({})
|
@seed_data ||= HashWithIndifferentAccess.new({})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_builders
|
||||||
|
@html_builders ||= {}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_js(filename, options={})
|
def register_js(filename, options={})
|
||||||
@ -127,6 +130,14 @@ class DiscoursePluginRegistry
|
|||||||
self.seed_data[key] = value
|
self.seed_data[key] = value
|
||||||
end
|
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
|
def javascripts
|
||||||
self.class.javascripts
|
self.class.javascripts
|
||||||
end
|
end
|
||||||
@ -169,6 +180,7 @@ class DiscoursePluginRegistry
|
|||||||
sass_variables.clear
|
sass_variables.clear
|
||||||
serialized_current_user_fields
|
serialized_current_user_fields
|
||||||
asset_globs.clear
|
asset_globs.clear
|
||||||
|
html_builders.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.setup(plugin_class)
|
def self.setup(plugin_class)
|
||||||
|
@ -238,6 +238,10 @@ class Plugin::Instance
|
|||||||
DiscoursePluginRegistry.custom_html.merge!(hash)
|
DiscoursePluginRegistry.custom_html.merge!(hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def register_html_builder(name, &block)
|
||||||
|
DiscoursePluginRegistry.register_html_builder(name, &block)
|
||||||
|
end
|
||||||
|
|
||||||
def register_asset(file, opts=nil)
|
def register_asset(file, opts=nil)
|
||||||
full_path = File.dirname(path) << "/assets/" << file
|
full_path = File.dirname(path) << "/assets/" << file
|
||||||
assets << [full_path, opts]
|
assets << [full_path, opts]
|
||||||
|
@ -44,6 +44,15 @@ describe DiscoursePluginRegistry do
|
|||||||
end
|
end
|
||||||
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
|
context '.register_css' do
|
||||||
before do
|
before do
|
||||||
registry_instance.register_css('hello.css')
|
registry_instance.register_css('hello.css')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user