index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <div class="emoji-wrap">
  3. <div id="emojiList" class="emoji-list pub-scroll-box" @scroll.passive="handleScroll">
  4. <div v-show="recentList.length">
  5. <p class="title">{{$t('emoji.lastestUse')}}</p>
  6. <ul id="recent-emoji">
  7. <li
  8. v-for="(item, key) in recentList"
  9. :key="key"
  10. v-html="item.surrogates"
  11. @click="handleClick(item, $event)"
  12. ></li>
  13. </ul>
  14. </div>
  15. <div v-for="(arr, key) in emojiList" :key="key">
  16. <p class="title">{{$t(`emoji.${key}`)}}</p>
  17. <ul>
  18. <li
  19. v-for="(item, ind) in arr"
  20. :key="ind"
  21. v-html="item.surrogates"
  22. @click="handleClick(item, $event)"
  23. ></li>
  24. </ul>
  25. </div>
  26. </div>
  27. <ul class="type-list">
  28. <li
  29. v-show="recentList.length"
  30. :class="[{'active' : typeIndex==0}]"
  31. @click.stop="handleType(0)"
  32. >
  33. <i></i>
  34. </li>
  35. <li
  36. v-for="(arr, key,index) in emojiList"
  37. :key="key"
  38. :class="[{'active' : typeIndex==index+1}]"
  39. @click.stop="handleType(index+1)"
  40. >
  41. <i></i>
  42. </li>
  43. </ul>
  44. </div>
  45. </template>
  46. <script>
  47. import { getMeechatType, lazyloadImage } from '@/util/util'
  48. import { emojiList } from '@/util/emoji'
  49. import twemoji from 'twemoji'
  50. export default {
  51. name: 'emojiList',
  52. props: {
  53. emojiShow: Boolean // 识别emoji显示
  54. },
  55. data () {
  56. return {
  57. emojiList,
  58. recentList: [],
  59. index: 0,
  60. typeIndex: 0,
  61. emojiListSel: null,
  62. emojiTitleArrSel: [], // 标题组
  63. meechatType: getMeechatType() // meechat版本
  64. }
  65. },
  66. watch: {
  67. emojiShow (isShow) {
  68. if (isShow) {
  69. this.$root.$el.addEventListener('click', this.closeEmojiList)
  70. } else {
  71. this.$root.$el.removeEventListener('click', this.closeEmojiList)
  72. }
  73. setTimeout(() => {
  74. this.handleScroll()
  75. }, 10)
  76. }
  77. },
  78. computed: {},
  79. mounted () {
  80. this.recentList = JSON.parse(localStorage.getItem('recentEmoji')) || []
  81. this.emojiListSel = document.getElementById('emojiList')
  82. this.emojiTitleArrSel = this.emojiListSel.getElementsByClassName('title')
  83. this.emojiImageArrSel = this.emojiListSel.getElementsByTagName('img')
  84. this.$nextTick(() => {
  85. this.parseEmoji(this.emojiListSel)
  86. })
  87. },
  88. methods: {
  89. handleType (index) {
  90. if (!this.emojiTitleArrSel[index]) return
  91. this.typeIndex = index
  92. this.emojiListSel.scrollTop = this.emojiTitleArrSel[index].offsetTop
  93. },
  94. handleClick (item, event) {
  95. let name = item.names
  96. this.$emit('addEmoji', JSON.stringify(name))
  97. // 最近使用
  98. if (this.recentList.length > 9) {
  99. this.recentList.pop()
  100. }
  101. let ind
  102. let isInRecent = this.recentList.some((val, index) => {
  103. ind = index
  104. return name[0] == val.names[0]
  105. })
  106. if (isInRecent) this.recentList.splice(ind, 1)
  107. this.recentList.unshift(item)
  108. localStorage.setItem('recentEmoji', JSON.stringify(this.recentList))
  109. let ul = document.getElementById('recent-emoji')
  110. this.$nextTick(() => {
  111. this.parseEmoji(ul)
  112. })
  113. },
  114. parseEmoji (ul) {
  115. let emojiDefault = require('../../assets/icon-face.png')
  116. twemoji.parse(ul, {
  117. callback: function (icon, options) {
  118. return emojiDefault
  119. },
  120. attributes (icon, variant) {
  121. return {
  122. originurl: 'https://w2.meechat.me/emoji/' + variant + '.svg'
  123. }
  124. }
  125. })
  126. },
  127. handleScroll () {
  128. let listScrollTop = this.emojiListSel.scrollTop
  129. let titleLen = this.emojiTitleArrSel.length
  130. lazyloadImage({
  131. wrap: this.emojiListSel,
  132. imageArr: this.emojiImageArrSel
  133. })
  134. // 表情导航
  135. for (let i = 0; i < titleLen; i++) {
  136. let item = this.emojiTitleArrSel[i]
  137. let nextItem = this.emojiTitleArrSel[i + 1]
  138. if (
  139. (listScrollTop + 20 > item.offsetTop &&
  140. nextItem &&
  141. listScrollTop <= nextItem.offsetTop) ||
  142. (!nextItem && listScrollTop + 20 > item.offsetTop)
  143. ) {
  144. this.typeIndex = i
  145. }
  146. }
  147. },
  148. closeEmojiList () {
  149. this.$emit('closeEmojiList')
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss">
  155. .c-main {
  156. .emoji-wrap {
  157. position: absolute;
  158. top: -269px;
  159. left: 4px;
  160. width: 432px;
  161. padding-bottom: 44px;
  162. border-radius: 4px;
  163. border: 1px solid #d6d6d6;
  164. background-color: #ffffff;
  165. box-shadow: 0px 1px 5px 1px #cdcdcd;
  166. }
  167. }
  168. .emoji-wrap {
  169. .type-list {
  170. position: absolute;
  171. bottom: 0;
  172. width: 100%;
  173. height: 44px;
  174. display: flex;
  175. background-color: #eeeeee;
  176. li {
  177. flex: 1;
  178. &.active {
  179. background-color: #ffffff;
  180. }
  181. @for $i from 1 through 8 {
  182. &:nth-child(#{$i}) {
  183. i {
  184. display: block;
  185. height: 100%;
  186. // background: url(../../assets/h5/emoji-type#{$i}.png) center / auto px2rem(48) no-repeat;
  187. background: url(../../assets/h5/emoji-type#{$i}.png)
  188. center /
  189. auto
  190. 24px
  191. no-repeat;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. .emoji-list {
  198. height: 210px;
  199. overflow-x: hidden;
  200. overflow-y: auto;
  201. margin: 2px 2px 6px 2px;
  202. padding: 0 5px;
  203. .title {
  204. font-size: 12px;
  205. font-weight: 700;
  206. padding: 0 7.5px;
  207. height: 30px;
  208. line-height: 30px;
  209. color: #999999;
  210. }
  211. }
  212. .emoji-list * {
  213. -webkit-user-select: none;
  214. -moz-user-select: none;
  215. -ms-user-select: none;
  216. user-select: none;
  217. }
  218. .emoji-list li {
  219. font-size: 28px;
  220. border-radius: 4px;
  221. display: inline-block;
  222. padding: 5px;
  223. cursor: pointer;
  224. &:hover {
  225. background: #eeeeee;
  226. }
  227. img.emoji {
  228. width: 24px;
  229. height: 24px;
  230. }
  231. }
  232. }
  233. .mini-wrap,
  234. .h5-wrap {
  235. .emoji-wrap {
  236. padding-bottom: 40px;
  237. position: relative;
  238. width: auto;
  239. top: 0;
  240. border-radius: 0;
  241. left: 0;
  242. border: 0;
  243. background: transparent;
  244. }
  245. .emoji-list {
  246. position: relative;
  247. top: 0;
  248. left: 0;
  249. height: px2rem(379);
  250. background: transparent;
  251. width: auto;
  252. border: 0;
  253. margin: 0;
  254. padding: 0;
  255. .title {
  256. padding: 0 px2rem(24);
  257. }
  258. ul {
  259. padding-left: px2rem(12);
  260. }
  261. li {
  262. padding: px2rem(12) px2rem(21);
  263. }
  264. img.emoji {
  265. width: px2rem(48);
  266. height: px2rem(48);
  267. }
  268. }
  269. .type-list {
  270. background: #ffffff;
  271. height: 40px;
  272. li {
  273. &.active {
  274. background-color: #e2e1de;
  275. }
  276. @for $i from 1 through 8 {
  277. &:nth-child(#{$i}) {
  278. i {
  279. background: url(../../assets/h5/emoji-type#{$i}.png)
  280. center /
  281. auto
  282. px2rem(48)
  283. no-repeat;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. </style>