message-input-editor.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div
  3. :class="{
  4. 'message-input-container': true,
  5. 'message-input-container-h5': !isPC,
  6. }"
  7. >
  8. <div
  9. v-if="props.isMuted"
  10. class="message-input-mute"
  11. >
  12. {{ props.muteText }}
  13. </div>
  14. <input
  15. id="editor"
  16. ref="inputRef"
  17. v-model="inputText"
  18. :adjust-position="true"
  19. cursor-spacing="20"
  20. confirm-type="send"
  21. :confirm-hold="true"
  22. maxlength="140"
  23. type="text"
  24. placeholder-class="input-placeholder"
  25. class="message-input-area"
  26. :placeholder="props.placeholder"
  27. auto-blur
  28. @confirm="handleSendMessage"
  29. @input="onInput"
  30. @blur="onBlur"
  31. @focus="onFocus"
  32. >
  33. </div>
  34. </template>
  35. <script lang="ts" setup>
  36. import { ref, watch, onMounted, onUnmounted } from '../../../adapter-vue';
  37. import { TUIStore, StoreName, IConversationModel, IMessageModel } from '@tencentcloud/chat-uikit-engine';
  38. import { TUIGlobal } from '@tencentcloud/universal-api';
  39. import DraftManager from '../utils/conversationDraft';
  40. import { transformTextWithEmojiNamesToKeys } from '../emoji-config';
  41. import { isPC } from '../../../utils/env';
  42. import { sendMessages } from '../utils/sendMessage';
  43. import { ISendMessagePayload } from '../../../interface';
  44. const props = defineProps({
  45. placeholder: {
  46. type: String,
  47. default: 'this is placeholder',
  48. },
  49. replayOrReferenceMessage: {
  50. type: Object,
  51. default: () => ({}),
  52. required: false,
  53. },
  54. isMuted: {
  55. type: Boolean,
  56. default: true,
  57. },
  58. muteText: {
  59. type: String,
  60. default: '',
  61. },
  62. enableInput: {
  63. type: Boolean,
  64. default: true,
  65. },
  66. enableAt: {
  67. type: Boolean,
  68. default: true,
  69. },
  70. enableTyping: {
  71. type: Boolean,
  72. default: true,
  73. },
  74. isGroup: {
  75. type: Boolean,
  76. default: false,
  77. },
  78. });
  79. const emits = defineEmits(['onTyping', 'onFocus', 'onAt']);
  80. const inputText = ref('');
  81. const inputRef = ref();
  82. const inputBlur = ref(true);
  83. const inputContentEmpty = ref(true);
  84. const allInsertedAtInfo = new Map();
  85. const currentConversation = ref<IConversationModel>();
  86. const currentConversationID = ref<string>('');
  87. const currentQuoteMessage = ref<{ message: IMessageModel; type: string }>();
  88. onMounted(() => {
  89. TUIStore.watch(StoreName.CONV, {
  90. currentConversation: onCurrentConversationUpdated,
  91. });
  92. TUIStore.watch(StoreName.CHAT, {
  93. quoteMessage: onQuoteMessageUpdated,
  94. });
  95. uni.$on('insert-emoji', (data) => {
  96. inputText.value += data?.emoji?.name;
  97. });
  98. uni.$on('send-message-in-emoji-picker', () => {
  99. handleSendMessage();
  100. });
  101. });
  102. onUnmounted(() => {
  103. if (currentConversationID.value) {
  104. DraftManager.setStore(currentConversationID.value, inputText.value, inputText.value, currentQuoteMessage.value);
  105. }
  106. uni.$off('insertEmoji');
  107. uni.$off('send-message-in-emoji-picker');
  108. TUIStore.unwatch(StoreName.CONV, {
  109. currentConversation: onCurrentConversationUpdated,
  110. });
  111. TUIStore.unwatch(StoreName.CHAT, {
  112. quoteMessage: onQuoteMessageUpdated,
  113. });
  114. reset();
  115. });
  116. const handleSendMessage = () => {
  117. const messageList = getEditorContent();
  118. resetEditor();
  119. sendMessages(messageList as any, currentConversation.value!);
  120. };
  121. const insertAt = (atInfo: any) => {
  122. if (!allInsertedAtInfo?.has(atInfo?.id)) {
  123. allInsertedAtInfo?.set(atInfo?.id, atInfo?.label);
  124. }
  125. inputText.value += atInfo?.label;
  126. };
  127. const getEditorContent = () => {
  128. let text = inputText.value;
  129. text = transformTextWithEmojiNamesToKeys(text);
  130. const atUserList: string[] = [];
  131. allInsertedAtInfo?.forEach((value: string, key: string) => {
  132. if (text?.includes('@' + value)) {
  133. atUserList.push(key);
  134. }
  135. });
  136. const payload: ISendMessagePayload = {
  137. text,
  138. };
  139. if (atUserList?.length) {
  140. payload.atUserList = atUserList;
  141. }
  142. return [
  143. {
  144. type: 'text',
  145. payload,
  146. },
  147. ];
  148. };
  149. const resetEditor = () => {
  150. inputText.value = '';
  151. inputContentEmpty.value = true;
  152. allInsertedAtInfo?.clear();
  153. };
  154. const setEditorContent = (content: any) => {
  155. inputText.value = content;
  156. };
  157. const onBlur = () => {
  158. inputBlur.value = true;
  159. };
  160. const onFocus = (e: any) => {
  161. inputBlur.value = false;
  162. emits('onFocus', e?.detail?.height);
  163. };
  164. const isEditorContentEmpty = () => {
  165. inputContentEmpty.value = inputText?.value?.length ? false : true;
  166. };
  167. const onInput = (e: any) => {
  168. // uni-app recognizes mention messages
  169. const text = e?.detail?.value;
  170. isEditorContentEmpty();
  171. if (props.isGroup && (text.endsWith('@') || text.endsWith('@\n'))) {
  172. TUIGlobal?.hideKeyboard();
  173. emits('onAt', true);
  174. }
  175. };
  176. watch(
  177. () => [inputContentEmpty.value, inputBlur.value],
  178. (newVal: any, oldVal: any) => {
  179. if (newVal !== oldVal) {
  180. emits('onTyping', inputContentEmpty.value, inputBlur.value);
  181. }
  182. },
  183. {
  184. immediate: true,
  185. deep: true,
  186. },
  187. );
  188. function onCurrentConversationUpdated(conversation: IConversationModel) {
  189. const prevConversationID = currentConversationID.value;
  190. currentConversation.value = conversation;
  191. currentConversationID.value = conversation?.conversationID;
  192. if (prevConversationID !== currentConversationID.value) {
  193. if (prevConversationID) {
  194. DraftManager.setStore(
  195. prevConversationID,
  196. inputText.value,
  197. inputText.value,
  198. currentQuoteMessage.value,
  199. );
  200. }
  201. resetEditor();
  202. if (currentConversationID.value) {
  203. DraftManager.getStore(currentConversationID.value, setEditorContent);
  204. }
  205. }
  206. }
  207. function onQuoteMessageUpdated(options?: { message: IMessageModel; type: string }) {
  208. currentQuoteMessage.value = options;
  209. }
  210. function reset() {
  211. inputBlur.value = true;
  212. currentConversation.value = null;
  213. currentConversationID.value = '';
  214. currentQuoteMessage.value = null;
  215. resetEditor();
  216. }
  217. defineExpose({
  218. insertAt,
  219. resetEditor,
  220. setEditorContent,
  221. getEditorContent,
  222. });
  223. </script>
  224. <style lang="scss" scoped>
  225. @import "../../../assets/styles/common";
  226. .message-input-container {
  227. display: flex;
  228. flex-direction: column;
  229. flex: 1;
  230. padding: 3px 10px 10px;
  231. overflow: hidden;
  232. &-h5 {
  233. flex: 1;
  234. height: auto;
  235. background: #fff;
  236. border-radius: 10px;
  237. padding: 7px 0 7px 10px;
  238. font-size: 16px !important;
  239. max-height: 86px;
  240. }
  241. .message-input-mute {
  242. flex: 1;
  243. display: flex;
  244. color: #999;
  245. font-size: 14px;
  246. justify-content: center;
  247. align-items: center;
  248. }
  249. .message-input-area {
  250. flex: 1;
  251. overflow-y: scroll;
  252. min-height: 25px;
  253. }
  254. }
  255. </style>