1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <template> <div> <textarea id="textarea" v-model="content" /> <div class="face"> <Picker @select="showEmoji" :i18n="I18N" /> </div> </div> </template>
<script> import { Picker } from "emoji-mart-vue";
const I18N = { search: '搜索', notfound: 'No Emoji Found', categories: { search: '搜索结果', recent: '经常使用', smileys: '表情与情感', people: '人物与身体', nature: '动物与自然', foods: '食物与饮料', activity: '活动', places: '旅行与地理', objects: '物品', symbols: '符号标志', flags: '旗帜', custom: 'Custom', }, }
export default { name: 'App', components: { Picker }, data() { return { content: '', I18N: I18N
} }, methods: { // 选择表情 showEmoji(emoji) { const str = emoji.native var oText = document.getElementById('textarea') var index = this.getCursortPosition(oText) this.$nextTick( () => { this.insert(oText,str,index) }) },
// 插入表情 insert (oText,varstr,index) { var text = this.content let startStr = text.substring(0, index) let endStr = text.substring(index, text.length) this.content = startStr + varstr + endStr setTimeout(() => { this.setCaretPosition(oText, index+2) }, 10); },
// 获取光标位置 getCursortPosition (obj) { var cursorIndex = 0 if (document.selection) { obj.focus() var range = document.selection.createRange() range.moveStart('character', -obj.value.length) cursorIndex = range.text.length } else if (obj.selectionStart || obj.selectionStart == 0) { cursorIndex = obj.selectionStart } return cursorIndex },
// 设置光标位置 setCaretPosition(ctrl, pos){ if(ctrl.setSelectionRange) { ctrl.focus(); ctrl.setSelectionRange(pos,pos); } else if (ctrl.createTextRange) { var range = ctrl.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } }, } } </script>
<style> textarea{ width: 100%; height: 200px; padding: 10px; line-height: 24px; } </style>
|