FIX: better plugin emojis API
This commit is contained in:
parent
927c492290
commit
86f76e5b4d
|
@ -6,6 +6,7 @@ export default {
|
|||
|
||||
initialize(container) {
|
||||
const siteSettings = container.lookup('site-settings:main');
|
||||
|
||||
if (siteSettings.enable_emoji) {
|
||||
|
||||
onToolbarCreate(toolbar => {
|
||||
|
@ -32,6 +33,9 @@ export default {
|
|||
});
|
||||
}
|
||||
});
|
||||
|
||||
// enable plugin emojis
|
||||
Discourse.Emoji.applyCustomEmojis();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -13,10 +13,20 @@ Discourse.Dialect.registerEmoji = function(code, url) {
|
|||
};
|
||||
|
||||
// This method is used by PrettyText to reset custom emojis in multisites
|
||||
Discourse.Dialect.resetEmoji = function() {
|
||||
Discourse.Dialect.resetEmojis = function() {
|
||||
extendedEmoji = {};
|
||||
};
|
||||
|
||||
var customEmojiCallbacks = [];
|
||||
Discourse.Emoji.addCustomEmojis = function(cb) {
|
||||
customEmojiCallbacks.push(cb);
|
||||
};
|
||||
|
||||
Discourse.Emoji.applyCustomEmojis = function() {
|
||||
var self = this;
|
||||
_.each(customEmojiCallbacks, function(cb) { cb.apply(self); });
|
||||
};
|
||||
|
||||
Discourse.Emoji.list = function(){
|
||||
var list = emoji.slice(0);
|
||||
_.each(extendedEmoji, function(v,k){ list.push(k); });
|
||||
|
|
|
@ -104,18 +104,9 @@ class Emoji
|
|||
end
|
||||
|
||||
def self.load_custom
|
||||
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
|
||||
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
|
||||
.sort
|
||||
.map { |emoji| Emoji.create_from_path(emoji) }
|
||||
end
|
||||
|
||||
def self.base_directory
|
||||
|
|
|
@ -14,7 +14,6 @@ class DiscoursePluginRegistry
|
|||
attr_writer :handlebars
|
||||
attr_writer :serialized_current_user_fields
|
||||
attr_writer :seed_data
|
||||
attr_writer :emojis
|
||||
|
||||
attr_accessor :custom_html
|
||||
|
||||
|
@ -62,10 +61,6 @@ class DiscoursePluginRegistry
|
|||
def seed_data
|
||||
@seed_data ||= HashWithIndifferentAccess.new({})
|
||||
end
|
||||
|
||||
def emojis
|
||||
@emojis ||= HashWithIndifferentAccess.new({})
|
||||
end
|
||||
end
|
||||
|
||||
def register_js(filename, options={})
|
||||
|
@ -136,10 +131,6 @@ 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
|
||||
|
@ -168,10 +159,6 @@ class DiscoursePluginRegistry
|
|||
self.class.handlebars
|
||||
end
|
||||
|
||||
def emojis
|
||||
self.class.emojis
|
||||
end
|
||||
|
||||
def self.clear
|
||||
self.javascripts = nil
|
||||
self.server_side_javascripts = nil
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -190,11 +190,13 @@ module PrettyText
|
|||
end
|
||||
|
||||
# reset emojis (v8 context is shared amongst multisites)
|
||||
context.eval("Discourse.Dialect.resetEmoji();")
|
||||
context.eval("Discourse.Dialect.resetEmojis();")
|
||||
# custom emojis
|
||||
Emoji.custom.each do |emoji|
|
||||
context.eval("Discourse.Dialect.registerEmoji('#{emoji.name}', '#{emoji.url}');")
|
||||
end
|
||||
# plugin emojis
|
||||
context.eval("Discourse.Emoji.applyCustomEmojis();")
|
||||
|
||||
context.eval('opts["mentionLookup"] = function(u){return helpers.is_username_valid(u);}')
|
||||
context.eval('opts["lookupAvatar"] = function(p){return Discourse.Utilities.avatarImg({size: "tiny", avatarTemplate: helpers.avatar_template(p)});}')
|
||||
|
|
Loading…
Reference in New Issue