mirror of
https://github.com/discourse/discourse.git
synced 2025-09-05 08:59:27 +08:00
FIX: better plugin emojis API
This commit is contained in:
parent
927c492290
commit
86f76e5b4d
6 changed files with 72 additions and 59 deletions
|
@ -122,19 +122,23 @@ class Plugin::Instance
|
|||
# will make sure all the assets this plugin needs are registered
|
||||
def generate_automatic_assets!
|
||||
paths = []
|
||||
assets = []
|
||||
|
||||
automatic_assets.each do |path, contents|
|
||||
unless File.exists? path
|
||||
ensure_directory path
|
||||
File.open(path,"w") do |f|
|
||||
f.write(contents)
|
||||
end
|
||||
end
|
||||
write_asset(path, contents)
|
||||
paths << path
|
||||
assets << [path]
|
||||
end
|
||||
|
||||
automatic_server_assets.each do |path, contents|
|
||||
write_asset(path, contents)
|
||||
paths << path
|
||||
assets << [path, :server_side]
|
||||
end
|
||||
|
||||
delete_extra_automatic_assets(paths)
|
||||
|
||||
paths
|
||||
assets
|
||||
end
|
||||
|
||||
def delete_extra_automatic_assets(good_paths)
|
||||
|
@ -239,25 +243,6 @@ class Plugin::Instance
|
|||
end
|
||||
end
|
||||
|
||||
unless emojis.blank?
|
||||
if @enabled_site_setting.present?
|
||||
js << "Discourse.initializer({" << "\n"
|
||||
js << "name: 'emojis'," << "\n"
|
||||
js << "initialize: function() {" << "\n"
|
||||
js << "if (Discourse.SiteSettings.#{@enabled_site_setting}) {" << "\n"
|
||||
end
|
||||
|
||||
emojis.each do |name, url|
|
||||
js << "Discourse.Dialect.registerEmoji('#{name}', '#{url}');" << "\n"
|
||||
end
|
||||
|
||||
if @enabled_site_setting.present?
|
||||
js << "}" << "\n"
|
||||
js << "}" << "\n"
|
||||
js << "});" << "\n"
|
||||
end
|
||||
end
|
||||
|
||||
# Generate an IIFE for the JS
|
||||
js = "(function(){#{js}})();" if js.present?
|
||||
|
||||
|
@ -269,9 +254,38 @@ class Plugin::Instance
|
|||
hash = Digest::SHA1.hexdigest asset
|
||||
["#{auto_generated_path}/plugin_#{hash}.#{extension}", asset]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def automatic_server_assets
|
||||
js = ""
|
||||
|
||||
unless emojis.blank?
|
||||
js << "Discourse.Emoji.addCustomEmojis(function() {" << "\n"
|
||||
|
||||
if @enabled_site_setting.present?
|
||||
js << "if (Discourse.SiteSettings.#{@enabled_site_setting}) {" << "\n"
|
||||
end
|
||||
|
||||
emojis.each do |name, url|
|
||||
js << "Discourse.Dialect.registerEmoji('#{name}', '#{url}');" << "\n"
|
||||
end
|
||||
|
||||
if @enabled_site_setting.present?
|
||||
js << "}" << "\n"
|
||||
end
|
||||
|
||||
js << "});" << "\n"
|
||||
end
|
||||
|
||||
if js.present?
|
||||
# Generate an IIFE for the JS
|
||||
asset = "(function(){#{js}})();"
|
||||
hash = Digest::SHA1.hexdigest(asset)
|
||||
["#{auto_generated_path}/plugin_#{hash}.js", asset]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
# note, we need to be able to parse seperately to activation.
|
||||
# this allows us to present information about a plugin in the UI
|
||||
|
@ -291,7 +305,7 @@ class Plugin::Instance
|
|||
|
||||
self.instance_eval File.read(path), path
|
||||
if auto_assets = generate_automatic_assets!
|
||||
assets.concat auto_assets.map{|a| [a]}
|
||||
assets.concat(auto_assets)
|
||||
end
|
||||
|
||||
register_assets! unless assets.blank?
|
||||
|
@ -300,10 +314,6 @@ class Plugin::Instance
|
|||
DiscoursePluginRegistry.register_seed_data(key, value)
|
||||
end
|
||||
|
||||
emojis.each do |name, url|
|
||||
DiscoursePluginRegistry.register_emoji(name, url)
|
||||
end
|
||||
|
||||
# TODO: possibly amend this to a rails engine
|
||||
|
||||
# Automatically include assets
|
||||
|
@ -386,4 +396,13 @@ class Plugin::Instance
|
|||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def write_asset(path, contents)
|
||||
unless File.exists?(path)
|
||||
ensure_directory(path)
|
||||
File.open(path,"w") { |f| f.write(contents) }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue