Insert character at Cursor position in VUE JS

后端 未结 3 1511
小蘑菇
小蘑菇 2021-02-03 10:42

I have been trying to insert emoji in textarea exactly where the cursor is at. I looked around how tos in the web could not find anything specific in VUE JS. Most o

3条回答
  •  长情又很酷
    2021-02-03 11:06

    
    
    

    // methods:
    insertSomething: function(insert) {
      const self = this;
      var tArea = this.$refs.yourTextarea;
      // filter:
      if (0 == insert) {
        return;
      }
      if (0 == cursorPos) {
        return;
      }
    
      // get cursor's position:
      var startPos = tArea.selectionStart,
        endPos = tArea.selectionEnd,
        cursorPos = startPos,
        tmpStr = tArea.value;
    
      // insert:
      self.txtContent = tmpStr.substring(0, startPos) + insert + tmpStr.substring(endPos, tmpStr.length);
    
      // move cursor:
      setTimeout(() => {
        cursorPos += insert.length;
        tArea.selectionStart = tArea.selectionEnd = cursorPos;
      }, 10);
    }

提交回复
热议问题