emoji.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import CustomPage from '../../../base/CustomPage'
  2. import {compareVersion} from '../../../../util/util'
  3. CustomPage({
  4. onShareAppMessage() {
  5. return {
  6. title: 'emoji',
  7. path: 'packageExtend/pages/extend/emoji/emoji'
  8. }
  9. },
  10. data: {
  11. lineHeight: 24,
  12. functionShow: false,
  13. emojiShow: false,
  14. comment: '',
  15. focus: false,
  16. cursor: 0,
  17. _keyboardShow: false,
  18. emojiSource: 'https://res.wx.qq.com/op_res/eROMsLpnNC10dC40vzF8qviz63ic7ATlbGg20lr5pYykOwHRbLZFUhgg23RtVorX',
  19. // parsedComment: []
  20. historyList: [],
  21. layoutHeight: '0px',
  22. safeHeight: 0,
  23. keyboardHeight: 0,
  24. isIOS: false,
  25. canIUse: true,
  26. },
  27. onLoad() {
  28. const system = wx.getSystemInfoSync()
  29. const isIOS = system.platform === 'ios'
  30. this.safeHeight = (system.screenHeight - system.safeArea.bottom)
  31. const layoutHeight = wx.getSystemInfoSync().windowHeight - (this.safeHeight / 2)
  32. this.setData({
  33. isIOS,
  34. safeHeight: this.safeHeight,
  35. layoutHeight,
  36. })
  37. const emojiInstance = this.selectComponent('.mp-emoji')
  38. this.emojiNames = emojiInstance.getEmojiNames()
  39. this.parseEmoji = emojiInstance.parseEmoji
  40. },
  41. onReady() {
  42. // 解决基础库小于 2.9.2 的兼容问题
  43. const {SDKVersion} = wx.getSystemInfoSync()
  44. if (compareVersion(SDKVersion, '2.9.1') < 0) {
  45. this.setData({
  46. canIUse: false,
  47. })
  48. }
  49. },
  50. onkeyboardHeightChange(e) {
  51. const {height} = e.detail
  52. if (height === 0) {
  53. this.data._keyboardShow = false
  54. this.setData({
  55. safeHeight: this.safeHeight,
  56. keyboardHeight: height
  57. })
  58. } else {
  59. this.data._keyboardShow = true
  60. this.setData({
  61. safeHeight: 0,
  62. functionShow: false,
  63. emojiShow: false,
  64. keyboardHeight: height
  65. })
  66. }
  67. },
  68. hideAllPanel() {
  69. this.setData({
  70. functionShow: false,
  71. emojiShow: false
  72. })
  73. },
  74. showEmoji() {
  75. this.setData({
  76. functionShow: false,
  77. emojiShow: this.data._keyboardShow || !this.data.emojiShow
  78. })
  79. },
  80. showFunction() {
  81. this.setData({
  82. functionShow: this.data._keyboardShow || !this.data.functionShow,
  83. emojiShow: false
  84. })
  85. },
  86. chooseImage() {},
  87. onFocus() {
  88. this.data._keyboardShow = true
  89. this.hideAllPanel()
  90. },
  91. onBlur(e) {
  92. this.data._keyboardShow = false
  93. this.data.cursor = e.detail.cursor || 0
  94. },
  95. onInput(e) {
  96. const value = e.detail.value
  97. this.data.comment = value
  98. },
  99. onConfirm() {
  100. this.onsend()
  101. },
  102. insertEmoji(evt) {
  103. const emotionName = evt.detail.emotionName
  104. const {cursor, comment} = this.data
  105. const newComment =
  106. comment.slice(0, cursor) + emotionName + comment.slice(cursor)
  107. this.setData({
  108. comment: newComment,
  109. cursor: cursor + emotionName.length
  110. })
  111. },
  112. onsend() {
  113. // const comment = this.data.comment
  114. const parsedComment = {
  115. emoji: this.parseEmoji(this.data.comment),
  116. id: `emoji_${this.data.historyList.length}`
  117. }
  118. this.setData({
  119. historyList: [...this.data.historyList, parsedComment],
  120. comment: '',
  121. emojiShow: false,
  122. })
  123. },
  124. deleteEmoji() {
  125. const pos = this.data.cursor
  126. const comment = this.data.comment
  127. let result = ''
  128. let cursor = 0
  129. let emojiLen = 6
  130. let startPos = pos - emojiLen
  131. if (startPos < 0) {
  132. startPos = 0
  133. emojiLen = pos
  134. }
  135. const str = comment.slice(startPos, pos)
  136. const matchs = str.match(/\[([\u4e00-\u9fa5\w]+)\]$/g)
  137. // 删除表情
  138. if (matchs) {
  139. const rawName = matchs[0]
  140. const left = emojiLen - rawName.length
  141. if (this.emojiNames.indexOf(rawName) >= 0) {
  142. const replace = str.replace(rawName, '')
  143. result = comment.slice(0, startPos) + replace + comment.slice(pos)
  144. cursor = startPos + left
  145. }
  146. // 删除字符
  147. } else {
  148. let endPos = pos - 1
  149. if (endPos < 0) endPos = 0
  150. const prefix = comment.slice(0, endPos)
  151. const suffix = comment.slice(pos)
  152. result = prefix + suffix
  153. cursor = endPos
  154. }
  155. this.setData({
  156. comment: result,
  157. cursor
  158. })
  159. }
  160. })