From eb5830f3b03c323e8a5debe2a712ce2bf5bdc8a8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 29 Aug 2013 13:11:12 -0400 Subject: [PATCH] FIX: Make `getURL` available to plugins while they are starting up in a similar load order to the client app. --- .../javascripts/discourse/dialects/dialect.js | 8 ++++---- lib/pretty_text.rb | 17 +++++++++++------ .../assets/javascripts/discourse_emoji.js | 8 +++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js index ba208fac901..5202c4910a6 100644 --- a/app/assets/javascripts/discourse/dialects/dialect.js +++ b/app/assets/javascripts/discourse/dialects/dialect.js @@ -96,17 +96,17 @@ Discourse.Dialect = { For example to replace all occurrances of :) with a smile image: - ```javascript + ```javascript Discourse.Dialect.inlineReplace(':)', ['img', {src: '/images/smile.gif'}]) ``` @method inlineReplace @param {String} token The token we want to replace - @param {Array} jsonml The JsonML to replace it with. + @param {Function} emitter A function that emits the JsonML for the replacement. **/ - inlineReplace: function(token, jsonml) { + inlineReplace: function(token, emitter) { dialect.inline[token] = function(text, match, prev) { - return [token.length, jsonml]; + return [token.length, emitter.call(this, token)]; }; }, diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index eecb5ca7eda..8a9603ae20c 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -104,6 +104,8 @@ module PrettyText ctx.eval("var window = {}; window.devicePixelRatio = 2;") # hack to make code think stuff is retina ctx.eval("var I18n = {}; I18n.t = function(a,b){ return helpers.t(a,b); }"); + decorate_context(ctx) + ctx_load(ctx, "app/assets/javascripts/external/better_markdown.js", "app/assets/javascripts/discourse/dialects/dialect.js", @@ -145,6 +147,13 @@ module PrettyText @ctx end + def self.decorate_context(context) + context.eval("Discourse.SiteSettings = #{SiteSetting.client_settings_json};") + context.eval("Discourse.CDN = '#{Rails.configuration.action_controller.asset_host}';") + context.eval("Discourse.BaseUrl = 'http://#{RailsMultisite::ConnectionManagement.current_hostname}';") + context.eval("Discourse.getURL = function(url) {return '#{Discourse::base_uri}' + url};") + end + def self.markdown(text, opts=nil) # we use the exact same markdown converter as the client # TODO: use the same extensions on both client and server (in particular the template for mentions) @@ -154,9 +163,7 @@ module PrettyText @mutex.synchronize do context = v8 # we need to do this to work in a multi site environment, many sites, many settings - context.eval("Discourse.SiteSettings = #{SiteSetting.client_settings_json};") - context.eval("Discourse.BaseUrl = 'http://#{RailsMultisite::ConnectionManagement.current_hostname}';") - context.eval("Discourse.getURL = function(url) {return '#{Discourse::base_uri}' + url};") + decorate_context(context) context['opts'] = opts || {} context['raw'] = text context.eval('opts["mentionLookup"] = function(u){return helpers.is_username_valid(u);}') @@ -175,9 +182,7 @@ module PrettyText @mutex.synchronize do v8['avatarTemplate'] = avatar_template v8['size'] = size - v8.eval("Discourse.SiteSettings = #{SiteSetting.client_settings_json};") - v8.eval("Discourse.CDN = '#{Rails.configuration.action_controller.asset_host}';") - v8.eval("Discourse.BaseUrl = '#{RailsMultisite::ConnectionManagement.current_hostname}';") + decorate_context(v8) r = v8.eval("Discourse.Utilities.avatarImg({ avatarTemplate: avatarTemplate, size: size });") end r diff --git a/vendor/gems/discourse_emoji/vendor/assets/javascripts/discourse_emoji.js b/vendor/gems/discourse_emoji/vendor/assets/javascripts/discourse_emoji.js index 2f5aced8cbc..de90a3d8c45 100644 --- a/vendor/gems/discourse_emoji/vendor/assets/javascripts/discourse_emoji.js +++ b/vendor/gems/discourse_emoji/vendor/assets/javascripts/discourse_emoji.js @@ -24,9 +24,11 @@ ":$" : 'blush' }; - Discourse.Dialect.on('register', function() { - Object.keys(translations).forEach(function (code) { - Discourse.Dialect.inlineReplace(code, imageFor(translations[code])); + Object.keys(translations).forEach(function (code) { + + var replacement = translations[code]; + Discourse.Dialect.inlineReplace(code, function (code) { + return imageFor(replacement); }); });