From 3d642d8ad29b42adc6aebbe426287156b6581dfd Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Wed, 30 Aug 2017 14:48:43 +0200
Subject: [PATCH] Tweak emoji search to only select query on click and refocus
 it after clicking emoji

---
 Resources/Plugins/emoji-keyboard/browser.js | 26 +++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/Resources/Plugins/emoji-keyboard/browser.js b/Resources/Plugins/emoji-keyboard/browser.js
index eb18bc51..45ede4a2 100644
--- a/Resources/Plugins/emoji-keyboard/browser.js
+++ b/Resources/Plugins/emoji-keyboard/browser.js
@@ -72,6 +72,8 @@ enabled(){
   this.currentKeyboard = null;
   this.currentSpanner = null;
   
+  var wasSearchFocused = false;
+  
   var hideKeyboard = (refocus) => {
     $(this.currentKeyboard).remove();
     this.currentKeyboard = null;
@@ -193,16 +195,24 @@ enabled(){
     var searchInput = search.children[0];
     searchInput.focus();
     
+    wasSearchFocused = false;
+    
     searchInput.addEventListener("input", function(e){
       me.currentKeywords = e.target.value.split(" ").filter(kw => kw.length > 0).map(kw => kw.toLowerCase());
       updateFilters();
+      
+      wasSearchFocused = $(this).val().length > 0;
       e.stopPropagation();
     });
     
-    searchInput.addEventListener("focus", function(){
+    searchInput.addEventListener("click", function(){
       $(this).select();
     });
     
+    searchInput.addEventListener("focus", function(){
+      wasSearchFocused = true;
+    });
+    
     this.currentKeyboard = outer;
     selectSkinTone(this.selectedSkinTone);
     
@@ -227,10 +237,16 @@ enabled(){
 
     input.val(val.slice(0, posStart)+alt+val.slice(posEnd));
     input.trigger("change");
-    input.focus();
 
     input[0].selectionStart = posStart+alt.length;
     input[0].selectionEnd = posStart+alt.length;
+    
+    if (wasSearchFocused){
+      $(".emoji-keyboard-search").children("input").focus();
+    }
+    else{
+      input.focus();
+    }
   };
   
   // general event handlers
@@ -254,6 +270,10 @@ enabled(){
     }
   };
   
+  this.composeInputFocusEvent = function(e){
+    wasSearchFocused = false;
+  };
+  
   this.composerSendingEvent = function(e){
     hideKeyboard();
   };
@@ -285,6 +305,7 @@ ready(){
   this.composePanelScroller.on("scroll", this.composerScrollEvent);
   
   $(".emoji-keyboard-popup-btn").on("click", this.emojiKeyboardButtonClickEvent);
+  $(".js-compose-text", ".js-docked-compose").on("focus", this.composeInputFocusEvent);
   $(document).on("click", this.documentClickEvent);
   $(document).on("keydown", this.documentKeyEvent);
   $(document).on("uiComposeImageAdded", this.uploadFilesEvent);
@@ -408,6 +429,7 @@ disabled(){
   $(".emoji-keyboard-popup-btn").off("click", this.emojiKeyboardButtonClickEvent);
   $(".emoji-keyboard-popup-btn").remove();
   
+  $(".js-compose-text", ".js-docked-compose").off("focus", this.composeInputFocusEvent);
   $(document).off("click", this.documentClickEvent);
   $(document).off("keydown", this.documentKeyEvent);
   $(document).off("uiComposeImageAdded", this.uploadFilesEvent);