emotion.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const emoTextList = ['憨笑', '媚眼', '开心', '坏笑', '可怜', '爱心', '笑哭', '拍手', '惊喜', '打气',
  2. '大哭', '流泪', '饥饿', '难受', '健身', '示爱', '色色', '眨眼', '暴怒', '惊恐',
  3. '思考', '头晕', '大吐', '酷笑', '翻滚', '享受', '鼻涕', '快乐', '雀跃', '微笑',
  4. '贪婪', '红心', '粉心', '星星', '大火', '眼睛', '音符', "叹号", "问号", "绿叶",
  5. "燃烧", "喇叭", "警告", "信封", "房子", "礼物", "点赞", "举手", "拍手", "点头",
  6. "摇头", "偷瞄", "庆祝", "疾跑", "打滚", "惊吓", "起跳"
  7. ];
  8. let transform = (content) => {
  9. return content.replace(/\#[\u4E00-\u9FA5]{1,3}\;/gi, textToImg);
  10. }
  11. // 将匹配结果替换表情图片
  12. let textToImg = (emoText) => {
  13. let word = emoText.replace(/\#|\;/gi, '');
  14. let idx = emoTextList.indexOf(word);
  15. if (idx == -1) {
  16. return emoText;
  17. }
  18. let path = textToPath(emoText);
  19. // #ifdef MP
  20. // 微信小程序不能有前面的'/'
  21. path = path.slice(1);
  22. // #endif
  23. let img = `<img src="${path}" style="with:35px;height:35px;
  24. margin: 0 -2px;vertical-align:bottom;"/>`;
  25. return img;
  26. }
  27. let textToPath = (emoText) => {
  28. let word = emoText.replace(/\#|\;/gi, '');
  29. let idx = emoTextList.indexOf(word);
  30. return `/static/emoji/${idx}.gif`;
  31. }
  32. export default {
  33. emoTextList,
  34. transform,
  35. textToImg,
  36. textToPath
  37. }