FIX: allows to have custom emoji translation without static file (#9893)

FIX: allows to have custom emoji translation without static file (#9893)

diff --git a/app/assets/javascripts/discourse/app/components/d-editor.js b/app/assets/javascripts/discourse/app/components/d-editor.js
index bfdf021..184b7f9 100644
--- a/app/assets/javascripts/discourse/app/components/d-editor.js
+++ b/app/assets/javascripts/discourse/app/components/d-editor.js
@@ -483,8 +483,15 @@ export default Component.extend({
             }
           }
 
-          if (translations[full]) {
-            return resolve([translations[full]]);
+          // note this will only work for emojis starting with :
+          // eg: :-)
+          const allTranslations = Object.assign(
+            {},
+            translations,
+            this.getWithDefault("site.custom_emoji_translation", {})
+          );
+          if (allTranslations[full]) {
+            return resolve([allTranslations[full]]);
           }
 
           const match = term.match(/^:?(.*?):t([2-6])?$/);
diff --git a/app/assets/javascripts/discourse/app/lib/text.js b/app/assets/javascripts/discourse/app/lib/text.js
index 692e75b..94d33b1 100644
--- a/app/assets/javascripts/discourse/app/lib/text.js
+++ b/app/assets/javascripts/discourse/app/lib/text.js
@@ -18,6 +18,7 @@ function getOpts(opts) {
       getURL: getURLWithCDN,
       currentUser: Discourse.__container__.lookup("current-user:main"),
       censoredRegexp: site.censored_regexp,
+      customEmojiTranslation: site.custom_emoji_translation,
       siteSettings,
       formatUsername
     },
diff --git a/app/assets/javascripts/pretty-text/addon/emoji.js b/app/assets/javascripts/pretty-text/addon/emoji.js
index 74af3b9..5d9fb76 100644
--- a/app/assets/javascripts/pretty-text/addon/emoji.js
+++ b/app/assets/javascripts/pretty-text/addon/emoji.js
@@ -98,13 +98,18 @@ export function performEmojiUnescape(string, opts) {
 
   const inlineEmoji = opts.inlineEmoji;
   const regexp = unicodeRegexp(inlineEmoji);
+  const allTranslations = Object.assign(
+    {},
+    translations,
+    opts.customEmojiTranslation || {}
+  );
 
   return string.replace(regexp, (m, index) => {
-    const isEmoticon = opts.enableEmojiShortcuts && !!translations[m];
+    const isEmoticon = opts.enableEmojiShortcuts && !!allTranslations[m];
     const isUnicodeEmoticon = !!replacements[m];
     let emojiVal;
     if (isEmoticon) {
-      emojiVal = translations[m];
+      emojiVal = allTranslations[m];
     } else if (isUnicodeEmoticon) {
       emojiVal = replacements[m];
     } else {
@@ -131,11 +136,16 @@ export function performEmojiUnescape(string, opts) {
 export function performEmojiEscape(string, opts) {
   const inlineEmoji = opts.inlineEmoji;
   const regexp = unicodeRegexp(inlineEmoji);
+  const allTranslations = Object.assign(
+    {},
+    translations,
+    opts.customEmojiTranslation || {}
+  );
 
   return string.replace(regexp, (m, index) => {
     if (isReplacableInlineEmoji(string, index, inlineEmoji)) {
-      if (!!translations[m]) {
-        return opts.emojiShortcuts ? `:${translations[m]}:` : m;
+      if (!!allTranslations[m]) {
+        return opts.emojiShortcuts ? `:${allTranslations[m]}:` : m;
       } else if (!!replacements[m]) {
         return `:${replacements[m]}:`;
       }
diff --git a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/emoji.js b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/emoji.js
index 7b4bee7..452bb50 100644
--- a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/emoji.js
+++ b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/emoji.js
@@ -5,15 +5,25 @@ const MAX_NAME_LENGTH = 60;
 
 let translationTree = null;
 
+export function resetTranslationTree() {
+  translationTree = null;
+}
+
 // This allows us to efficiently search for aliases
 // We build a data structure that allows us to quickly
 // search through our N next chars to see if any match
 // one of our alias emojis.
-function buildTranslationTree() {
+function buildTranslationTree(customEmojiTranslation) {
   let tree = [];
   let lastNode;
 
-  Object.keys(translations).forEach(key => {
+  const allTranslations = Object.assign(
+    {},
+    translations,
+    customEmojiTranslation || {}
+  );
+
+  Object.keys(allTranslations).forEach(key => {
     let node = tree;
 
     for (let i = 0; i < key.length; i++) {
@@ -37,7 +47,7 @@ function buildTranslationTree() {
       }
     }
 
-    lastNode[2] = translations[key];
+    lastNode[2] = allTranslations[key];
   });
 
   return tree;
@@ -114,8 +124,14 @@ function getEmojiTokenByName(name, state) {
   }
 }
 
-function getEmojiTokenByTranslation(content, pos, state) {
-  translationTree = translationTree || buildTranslationTree();
+function getEmojiTokenByTranslation(
+  content,
+  pos,
+  state,
+  customEmojiTranslation
+) {
+  translationTree =
+    translationTree || buildTranslationTree(customEmojiTranslation);
 
   let t = translationTree;
   let start = pos;
@@ -175,7 +191,8 @@ function applyEmoji(
   state,
   emojiUnicodeReplacer,
   enableShortcuts,
-  inlineEmoji
+  inlineEmoji,
+  customEmojiTranslation
 ) {
   let result = null;
   let start = 0;
@@ -201,7 +218,12 @@ function applyEmoji(
 
     if (enableShortcuts && !token) {
       // handle aliases (note: we can't do this in inline cause ; is not a split point)
-      const info = getEmojiTokenByTranslation(content, i, state);
+      const info = getEmojiTokenByTranslation(
+        content,
+        i,
+        state,
+        customEmojiTranslation
+      );
 
       if (info) {
         offset = info.pos - i;
@@ -310,7 +332,8 @@ export function setup(helper) {
           s,
           md.options.discourse.emojiUnicodeReplacer,
           md.options.discourse.features.emojiShortcuts,
-          md.options.discourse.features.inlineEmoji
+          md.options.discourse.features.inlineEmoji,
+          md.options.discourse.customEmojiTranslation
         )
       )
     );
diff --git a/app/assets/javascripts/pretty-text/addon/pretty-text.js b/app/assets/javascripts/pretty-text/addon/pretty-text.js
index 7a867d8..d24fb4a 100644
--- a/app/assets/javascripts/pretty-text/addon/pretty-text.js
+++ b/app/assets/javascripts/pretty-text/addon/pretty-text.js
@@ -30,7 +30,8 @@ export function buildOptions(state) {
     previewing,
     linkify,
     censoredRegexp,
-    disableEmojis
+    disableEmojis,
+    customEmojiTranslation
   } = state;
 
   let features = {
@@ -68,6 +69,7 @@ export function buildOptions(state) {
     emojiUnicodeReplacer,
     lookupUploadUrls,
     censoredRegexp,
+    customEmojiTranslation,
     allowedHrefSchemes: siteSettings.allowed_href_schemes
       ? siteSettings.allowed_href_schemes.split("|")
       : null,
diff --git a/app/models/emoji.rb b/app/models/emoji.rb
index cfd121b..7222caf 100644
--- a/app/models/emoji.rb
+++ b/app/models/emoji.rb
@@ -126,7 +126,7 @@ class Emoji
   end
 
   def self.load_translations
-    db["translations"].merge(Plugin::CustomEmoji.translations)
+    db["translations"]
   end
 
   def self.base_directory
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index 06acfc0..d5bb901 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -26,7 +26,8 @@ class SiteSerializer < ApplicationSerializer
     :topic_featured_link_allowed_category_ids,
     :user_themes,
     :censored_regexp,
-    :shared_drafts_category_id
+    :shared_drafts_category_id,
+    :custom_emoji_translation
   )
 
   has_many :categories, serializer: SiteCategorySerializer, embed: :objects
@@ -154,6 +155,10 @@ class SiteSerializer < ApplicationSerializer
     WordWatcher.word_matcher_regexp(:censor)&.source
   end
 
+  def custom_emoji_translation
+    Plugin::CustomEmoji.translations
+  end
+
   def shared_drafts_category_id
     SiteSetting.shared_drafts_category.to_i
   end
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 7d48dbb..11f866f 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb

[... diff too long, it was truncated ...]

GitHub sha: 77801aa9

1 Like

This commit appears in #9893 which was approved by eviltrout. It was merged by jjaffeux.