Преглед на файлове

New loadEmojis hook, to customize emojis at runtime.

John Livingston преди 1 година
родител
ревизия
a0153bd707
променени са 3 файла, в които са добавени 25 реда и са изтрити 1 реда
  1. 1 0
      CHANGES.md
  2. 1 0
      docs/source/configuration.rst
  3. 23 1
      src/headless/plugins/emoji/plugin.js

+ 1 - 0
CHANGES.md

@@ -20,6 +20,7 @@
 - Add an occupants filter to the MUC sidebar
 - Change contacts filter to rename the anachronistic `Online` state to `Available`.
 - Enable [reuse_scram_keys](https://conversejs.org/docs/html/configuration.html#reuse-scram-keys) by default.
+- New loadEmojis hook, to customize emojis at runtime.
 
 ### Breaking changes:
 

+ 1 - 0
docs/source/configuration.rst

@@ -848,6 +848,7 @@ settings, it's not possible to add new emoji categories. There is however a
 To add custom emojis, you need to edit ``src/headless/emojis.json`` to add new
 entries to the map under the  ``custom`` key.
 
+You can also listen for the ``loadEmojis`` hook, that allows you to modify emojis.json at runtime.
 
 emoji_categories_label
 ----------------------

+ 23 - 1
src/headless/plugins/emoji/plugin.js

@@ -75,8 +75,30 @@ converse.plugins.add('converse-emoji', {
                 async initialize () {
                     if (!converse.emojis.initialized) {
                         converse.emojis.initialized = true;
+
                         const module = await import(/*webpackChunkName: "emojis" */ './emoji.json');
-                        const json = (converse.emojis.json = module.default);
+                        /**
+                         * *Hook* which allows plugins to modify emojis definition.
+                         *
+                         * Note: This hook is only fired one time, when ConverseJS initiliazed.
+                         *
+                         * @event _converse#loadEmojis
+                         * @param json
+                         *      See src/headless/emojis.json for more information about the content of this parameter.
+                         * @example
+                         *  api.listen.on('loadEmojis', (json) => {
+                         *      json.custom??= {};
+                         *      json.custom[":my_emoji"] = {
+                         *          "sn":":my_emoji:","url":"https://example.com/my_emoji.png","c":"custom"
+                         *      };
+                         *      delete json.custom[":converse:"];
+                         *      return json;
+                         *  });
+                         */
+                        const json = await api.hook('loadEmojis', module.default);
+                        converse.emojis.json = json;
+
+                        
                         converse.emojis.by_sn = Object.keys(json).reduce(
                             (result, cat) => Object.assign(result, json[cat]),
                             {}