原生的textarea已经把选择表情的按钮丢弃了,因此本身就模拟了一个表情选择器
表情能够在qq表情地带中查看函数
经过 escape() 和 unescape() 两个函数来对表情进行解析和重构this
escape("😃") // %uD83D%uDE04 unescape("%uD83D%uDE04") // 😃
这里返回的表情是十六进制的,为了计算出后续的表情,将十六进制转换成10进制spa
parseInt("D83D",16) // 55357 parseInt("DE04",16) // 56836
经过加减来获得后续表情数据并转换成16进制用 unescape() 重构出表情3d
const high = parseInt("D83D",16) const low = parseInt("DE04",16) const U_high = "%u" + high.toString(16) const U_low = "%u" + (low+1).toString(16) unescape(U_high + U_low)
// components/emoji/index.js Component({ /** * 组件的属性列表 */ properties: { value: String, // 输入框的值 }, /** * 组件的初始数据 */ data: { emojis: [], }, /** * 组件的方法列表 */ methods: { // 点击表情 onTapEmoji: function(e) { const { currentTarget: { dataset: { emoji } } } = e; const { value } = this.properties; this.triggerEvent('clickEmoji', { emoji: emoji, value: value + emoji }) }, }, lifetimes: { attached: function() { const _high = 55357; const _low = 56832; const _nums = 18; const emojis = []; for (let i = 0; i < _nums; i++) { const U_high = "%u" + _high.toString(16) const U_low = "%u" + (_low + i).toString(16) emojis.push(unescape(U_high + U_low)) } this.setData({ emojis }) }, } })
index.wmxlcode
<textarea value="{{textareaValue}}" bindinput="onInputTextarea"></textarea> <emoji bind:clickEmoji="clickEmoji" value="{{textareaValue}}" />
index.jscomponent
Page({ /** * 页面的初始数据 */ data: { textareaValue: "", }, /** * 生命周期函数--监听页面加载 */ clickEmoji: function(e) { const { detail: { value } } = e; this.setData({ textareaValue: value }) }, onInputTextarea: function(e) { const { detail: { value } } = e; this.setData({ textareaValue: value }) } })