summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/emojis_mixin.js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/mail/static/src/js/emojis_mixin.js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/mail/static/src/js/emojis_mixin.js')
-rw-r--r--addons/mail/static/src/js/emojis_mixin.js91
1 files changed, 91 insertions, 0 deletions
diff --git a/addons/mail/static/src/js/emojis_mixin.js b/addons/mail/static/src/js/emojis_mixin.js
new file mode 100644
index 00000000..aa535d1d
--- /dev/null
+++ b/addons/mail/static/src/js/emojis_mixin.js
@@ -0,0 +1,91 @@
+odoo.define('mail.emoji_mixin', function (require) {
+"use strict";
+
+var emojis = require('mail.emojis');
+
+/**
+ * This mixin gathers a few methods that are used to handle emojis.
+ *
+ * It's used to:
+ *
+ * - handle the click on an emoji from a dropdown panel and add it to the related textarea/input
+ * - format text and wrap the emojis around <span class="o_mail_emoji"> to make them look nicer
+ *
+ * Methods are based on the collections of emojis available in mail.emojis
+ *
+ */
+return {
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * This method should be bound to a click event on an emoji.
+ * (used in text element's emojis dropdown list)
+ *
+ * It assumes that a ``_getTargetTextElement`` method is defined that will return the related
+ * textarea/input element in which the emoji will be inserted.
+ *
+ * @param {MouseEvent} ev
+ */
+ _onEmojiClick: function (ev) {
+ var unicode = ev.currentTarget.textContent.trim();
+ var textInput = this._getTargetTextElement($(ev.currentTarget))[0];
+ var selectionStart = textInput.selectionStart;
+
+ textInput.value = textInput.value.slice(0, selectionStart) + unicode + textInput.value.slice(selectionStart);
+ textInput.focus();
+ textInput.setSelectionRange(selectionStart + unicode.length, selectionStart + unicode.length);
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * This method is used to wrap emojis in a text message with <span class="o_mail_emoji">
+ * As this returns html to be used in a 't-raw' argument, it first makes sure that the
+ * passed text message is html escaped for safety reasons.
+ *
+ * @param {String} message a text message to format
+ */
+ _formatText: function (message) {
+ message = this._htmlEscape(message);
+ message = this._wrapEmojis(message);
+ message = message.replace(/(?:\r\n|\r|\n)/g, '<br>');
+
+ return message;
+ },
+
+ /**
+ * Adapted from qweb2.js#html_escape to avoid formatting '&'
+ *
+ * @param {String} s
+ * @private
+ */
+ _htmlEscape: function (s) {
+ if (s == null) {
+ return '';
+ }
+ return String(s).replace(/</g, '&lt;').replace(/>/g, '&gt;');
+ },
+
+ /**
+ * Will use the mail.emojis library to wrap emojis unicode around a span with a special font
+ * that will make them look nicer (colored, ...).
+ *
+ * @param {String} message
+ */
+ _wrapEmojis: function (message) {
+ emojis.forEach(function (emoji) {
+ message = message.replace(
+ new RegExp(emoji.unicode.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'),
+ '<span class="o_mail_emoji">' + emoji.unicode + '</span>'
+ );
+ });
+
+ return message;
+ }
+};
+
+});