index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.stop="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.stop="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. setTimeout(() => {
  114. this.loadImage()
  115. }, 0)
  116. },
  117. parseEmoji (ul) {
  118. let emojiDefault = require('../../assets/icon-face.png')
  119. twemoji.parse(ul, {
  120. callback: function (icon, options) {
  121. return emojiDefault
  122. },
  123. attributes (icon, variant) {
  124. return {
  125. originurl: 'https://w2.meechat.me/emoji/' + variant + '.svg'
  126. }
  127. }
  128. })
  129. },
  130. loadImage () {
  131. lazyloadImage({
  132. wrap: this.emojiListSel,
  133. imageArr: this.emojiImageArrSel
  134. })
  135. },
  136. handleScroll () {
  137. let listScrollTop = this.emojiListSel.scrollTop
  138. let titleLen = this.emojiTitleArrSel.length
  139. this.loadImage()
  140. // 表情导航
  141. for (let i = 0; i < titleLen; i++) {
  142. let item = this.emojiTitleArrSel[i]
  143. let nextItem = this.emojiTitleArrSel[i + 1]
  144. if (
  145. (listScrollTop + 20 > item.offsetTop &&
  146. nextItem &&
  147. listScrollTop <= nextItem.offsetTop) ||
  148. (!nextItem && listScrollTop + 20 > item.offsetTop)
  149. ) {
  150. this.typeIndex = i
  151. }
  152. }
  153. },
  154. closeEmojiList () {
  155. this.$emit('closeEmojiList')
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss">
  161. .c-main {
  162. .emoji-wrap {
  163. position: absolute;
  164. top: -269px;
  165. left: 4px;
  166. width: 432px;
  167. padding-bottom: 44px;
  168. border-radius: 4px;
  169. border: 1px solid #d6d6d6;
  170. background-color: #ffffff;
  171. box-shadow: 0px 1px 5px 1px #cdcdcd;
  172. }
  173. }
  174. .emoji-wrap {
  175. .type-list {
  176. position: absolute;
  177. bottom: 0;
  178. width: 100%;
  179. height: 44px;
  180. display: flex;
  181. background-color: #eeeeee;
  182. li {
  183. flex: 1;
  184. &.active {
  185. background-color: #ffffff;
  186. }
  187. @for $i from 1 through 8 {
  188. &:nth-child(#{$i}) {
  189. i {
  190. display: block;
  191. height: 100%;
  192. // background: url(../../assets/h5/emoji-type#{$i}.png) center / auto px2rem(48) no-repeat;
  193. background: url(../../assets/h5/emoji-type#{$i}.png)
  194. center /
  195. auto
  196. 24px
  197. no-repeat;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. .emoji-list {
  204. height: 210px;
  205. overflow-x: hidden;
  206. overflow-y: auto;
  207. margin: 2px 2px 6px 2px;
  208. padding: 0 5px;
  209. .title {
  210. font-size: 12px;
  211. font-weight: 700;
  212. padding: 0 7.5px;
  213. height: 30px;
  214. line-height: 30px;
  215. color: #999999;
  216. }
  217. }
  218. .emoji-list * {
  219. -webkit-user-select: none;
  220. -moz-user-select: none;
  221. -ms-user-select: none;
  222. user-select: none;
  223. }
  224. .emoji-list li {
  225. font-size: 28px;
  226. border-radius: 4px;
  227. display: inline-block;
  228. padding: 5px;
  229. cursor: pointer;
  230. &:hover {
  231. background: #eeeeee;
  232. }
  233. img.emoji {
  234. width: 24px;
  235. height: 24px;
  236. }
  237. }
  238. }
  239. .mini-wrap,
  240. .h5-wrap {
  241. .emoji-wrap {
  242. padding-bottom: 40px;
  243. position: relative;
  244. width: auto;
  245. top: 0;
  246. border-radius: 0;
  247. left: 0;
  248. border: 0;
  249. background: transparent;
  250. }
  251. .emoji-list {
  252. position: relative;
  253. top: 0;
  254. left: 0;
  255. height: px2rem(379);
  256. background: transparent;
  257. width: auto;
  258. border: 0;
  259. margin: 0;
  260. padding: 0;
  261. .title {
  262. padding: 0 px2rem(24);
  263. }
  264. ul {
  265. padding-left: px2rem(12);
  266. }
  267. li {
  268. padding: px2rem(12) px2rem(21);
  269. }
  270. img.emoji {
  271. width: px2rem(48);
  272. height: px2rem(48);
  273. }
  274. }
  275. .type-list {
  276. background: #ffffff;
  277. height: 40px;
  278. li {
  279. &.active {
  280. background-color: #e2e1de;
  281. }
  282. @for $i from 1 through 8 {
  283. &:nth-child(#{$i}) {
  284. i {
  285. background: url(../../assets/h5/emoji-type#{$i}.png)
  286. center /
  287. auto
  288. px2rem(48)
  289. no-repeat;
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. </style>