From 98d4bd961300ae4339c40ad982afebba60a6ec62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Tue, 18 Apr 2017 16:07:07 +0200 Subject: [PATCH] Emojification: only replace sequences of emoji surrounded by whitespace Fixes #903, #930 --- js/inputbar.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/js/inputbar.js b/js/inputbar.js index c77bcf4..d884de7 100644 --- a/js/inputbar.js +++ b/js/inputbar.js @@ -28,9 +28,40 @@ weechat.directive('inputBar', function() { // Expose utils to be able to check if we're on a mobile UI $scope.utils = utils; - // E.g. Turn :smile: into the unicode equivalent + // Emojify input. E.g. Turn :smile: into the unicode equivalent, but + // don't do replacements in the middle of a word (e.g. std::io::foo) $scope.inputChanged = function() { - $scope.command = emojione.shortnameToUnicode($scope.command); + var emojiRegex = /^(?:[\uD800-\uDBFF][\uDC00-\uDFFF])+$/, // *only* emoji + changed = false, // whether a segment was modified + inputNode = $scope.getInputNode(), + caretPos = inputNode.selectionStart, + position = 0; // current position in text + + // use capturing group in regex to include whitespace in output array + var segments = $scope.command.split(/(\s+)/); + for (var i = 0; i < segments.length; i ++) { + if (/\s+/.test(segments[i]) || emojiRegex.test(segments[i])) { + // ignore whitespace and emoji-only segments + position += segments[i].length; + continue; + } + // emojify segment + var emojified = emojione.shortnameToUnicode(segments[i]); + if (emojiRegex.test(emojified)) { + // If result consists *only* of emoji, adjust caret + // position and replace segment with emojified version + caretPos = caretPos - segments[i].length + emojified.length; + segments[i] = emojified; + changed = true; + } + position += segments[i].length; + } + if (changed) { // Only re-assemble if something changed + $scope.command = segments.join(''); + setTimeout(function() { + inputNode.setSelectionRange(caretPos, caretPos); + }); + } }; /*