Skip to content

Insert emoji at caret location #365

Closed
@muskangupta3

Description

@muskangupta3

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Explanation

  • What is the expected behavior? I should be able to insert my emoji at the provided location

  • What is happening instead? I have a custom emoji picker in my code , which inserts the emoji at the cursor position . So when tagify is initialized , the emojis are not inserted on the correct position even after selecting a specific position for insertion . It is mostly inserted after whatever text i have written and if there is no text it starts inserting it inside the first tag value . This is happening in chrome , whereas in safari , there is already cursor issue after adding tag as mentioned in this issue(Mix Mode - Safari caret position after new tag #328 ) , but i am able to insert my emoji at the correct position .

    I am using an array of emoji objects like these where i pick the emoji to insert into my input

         {
          "emoji": "😀"
          , "description": "grinning face"
          , "category": "Smileys & Emotion"
          , "aliases": [
              "grinning"
          ]
          , "tags": [
              "smile"
              , "happy"
          ]
          , "unicode_version": "6.1"
          , "ios_version": "6.0"
      }
    

    This is the code i am using to insert emoji at a particular location in contenteditable elements.

          var sel,range;
          sel = window.getSelection();
          if (sel.getRangeAt && sel.rangeCount) {
                   range = sel.getRangeAt(0);
                    range.deleteContents();
                    // Range.createContextualFragment() would be useful here but is
                    // only relatively recently standardized and is not supported in
                    // some browsers (IE9, for one)
                    var el = document.createElement("div");
                    el.innerHTML = value.emoji;
                    var frag = document.createDocumentFragment(), node, lastNode;
                     while ((node = el.firstChild)) {
                         lastNode = frag.appendChild(node);
                     }
                    range.insertNode(frag);
    
                    // Preserve the selection
                    if (lastNode) {
                        range = range.cloneRange();
                        range.setStartAfter(lastNode);
                        range.collapse(true);
                        sel.removeAllRanges();
                        sel.addRange(range);
                     }
                 }
    

I have attached the screenshot of the emoji being inserted inside the tag:

Screenshot 2019-12-16 at 5 52 16 PM

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions