message-text.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <div :class="['message-text-container', isPC && 'text-select']">
  3. <span
  4. v-for="(item, index) in data.text"
  5. :key="index"
  6. >
  7. <span
  8. v-if="item.name === 'text'"
  9. class="text"
  10. >{{ item.text }}</span>
  11. <img
  12. v-else
  13. class="emoji"
  14. :src="item.src"
  15. :alt="item.emojiKey"
  16. >
  17. </span>
  18. </div>
  19. </template>
  20. <script lang="ts" setup>
  21. import { watchEffect, ref } from '../../../../adapter-vue';
  22. import { CUSTOM_BASIC_EMOJI_URL, CUSTOM_BASIC_EMOJI_URL_MAPPING } from '../../emoji-config';
  23. import { isPC } from '../../../../utils/env';
  24. interface IProps {
  25. content: Record<string, any>;
  26. }
  27. const props = withDefaults(defineProps<IProps>(), {
  28. content: () => ({}),
  29. });
  30. const data = ref();
  31. watchEffect(() => {
  32. data.value = props.content;
  33. data.value.text?.forEach((item: { name: string; text?: string; src?: string; type?: string; emojiKey?: string }) => {
  34. if (item.name === 'img' && item?.type === 'custom') {
  35. if (!CUSTOM_BASIC_EMOJI_URL) {
  36. console.warn('CUSTOM_BASIC_EMOJI_URL is required for custom emoji, please check your CUSTOM_BASIC_EMOJI_URL.');
  37. } else if (!item.emojiKey || !CUSTOM_BASIC_EMOJI_URL_MAPPING[item.emojiKey]) {
  38. console.warn('emojiKey is required for custom emoji, please check your emojiKey.');
  39. } else {
  40. item.src = CUSTOM_BASIC_EMOJI_URL + CUSTOM_BASIC_EMOJI_URL_MAPPING[item.emojiKey];
  41. }
  42. }
  43. });
  44. });
  45. </script>
  46. <style lang="scss" scoped>
  47. .message-text-container {
  48. display: inline;
  49. }
  50. .text-select {
  51. -webkit-user-select: text;
  52. -moz-user-select: text;
  53. -ms-user-select: text;
  54. user-select: text;
  55. }
  56. .emoji {
  57. vertical-align: bottom;
  58. width: 20px;
  59. height: 20px;
  60. }
  61. .text {
  62. white-space: pre-wrap;
  63. word-break: break-all;
  64. font-size: 14px;
  65. text-size-adjust: none;
  66. }
  67. </style>