FEATURE: add support for custom emojis via plugins

This commit is contained in:
Régis Hanol 2015-11-05 17:25:26 +01:00
parent 3bf6617ff6
commit 13e96ffd3b
3 changed files with 59 additions and 5 deletions

View File

@ -104,9 +104,18 @@ class Emoji
end
def self.load_custom
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
.sort
.map { |emoji| Emoji.create_from_path(emoji) }
uploaded = Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
.sort
.map { |emoji| Emoji.create_from_path(emoji) }
from_plugins = DiscoursePluginRegistry.emojis.map do |name, url|
Emoji.new.tap do |e|
e.name = name
e.url = url
end
end
uploaded + from_plugins
end
def self.base_directory

View File

@ -14,6 +14,7 @@ class DiscoursePluginRegistry
attr_writer :handlebars
attr_writer :serialized_current_user_fields
attr_writer :seed_data
attr_writer :emojis
attr_accessor :custom_html
@ -61,6 +62,10 @@ class DiscoursePluginRegistry
def seed_data
@seed_data ||= HashWithIndifferentAccess.new({})
end
def emojis
@emojis ||= HashWithIndifferentAccess.new({})
end
end
def register_js(filename, options={})
@ -131,6 +136,10 @@ class DiscoursePluginRegistry
self.seed_data[key] = value
end
def self.register_emoji(name, url)
self.emojis[name] = url
end
def javascripts
self.class.javascripts
end
@ -159,6 +168,10 @@ class DiscoursePluginRegistry
self.class.handlebars
end
def emojis
self.class.emojis
end
def self.clear
self.javascripts = nil
self.server_side_javascripts = nil

View File

@ -17,8 +17,13 @@ class Plugin::Instance
}
end
def seed_data
@seed_data ||= {}
# Memoized hash readers
[:seed_data, :emojis].each do |att|
class_eval %Q{
def #{att}
@#{att} ||= HashWithIndifferentAccess.new({})
end
}
end
def self.find_all(parent_path)
@ -213,6 +218,10 @@ class Plugin::Instance
seed_data[key] = value
end
def register_emoji(name, url)
emojis[name] = url
end
def automatic_assets
css = styles.join("\n")
js = javascripts.join("\n")
@ -230,6 +239,25 @@ 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?
@ -272,6 +300,10 @@ 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