conversationDraft.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {
  2. IMessageModel,
  3. SetConversationDraftParams,
  4. StoreName,
  5. TUIConversationService,
  6. TUIStore,
  7. TUITranslateService,
  8. } from '@tencentcloud/chat-uikit-engine';
  9. import { transformTextWithKeysToEmojiNames } from '../emoji-config';
  10. import { JSONToObject } from '../../../utils/index';
  11. class ConversationDraftManager {
  12. private static instance: ConversationDraftManager | null = null;
  13. private quoteMessageMap = new Map<string, IMessageModel>();
  14. private constructor() { }
  15. public static getInstance(): ConversationDraftManager {
  16. if (!ConversationDraftManager.instance) {
  17. ConversationDraftManager.instance = new ConversationDraftManager();
  18. }
  19. return ConversationDraftManager.instance;
  20. }
  21. public setStore(conversationID: string, draftContent: string, abstract: string, quoteMessage?: { type: 'quote' | 'reply'; message: IMessageModel }) {
  22. if (conversationID && (this.isEditorNotEmpty(draftContent) || quoteMessage?.message?.ID)) {
  23. let additionalDraftInfo = {};
  24. if (quoteMessage?.message?.ID) {
  25. this.quoteMessageMap.set(quoteMessage.message.ID, quoteMessage.message);
  26. additionalDraftInfo = { messageID: quoteMessage.message.ID, type: quoteMessage.type };
  27. }
  28. const draftParams: SetConversationDraftParams = {
  29. conversationID: conversationID,
  30. draftInfo: {
  31. html: draftContent,
  32. abstract: abstract,
  33. ...additionalDraftInfo,
  34. },
  35. };
  36. TUIConversationService.setConversationDraft(draftParams);
  37. TUIStore.update(StoreName.CHAT, 'quoteMessage', { message: undefined, type: 'quote' });
  38. }
  39. }
  40. public getStore(conversationID: string, setEditorContentCallback: (...args: any[]) => void) {
  41. const conversation = TUIStore.getConversationModel(conversationID);
  42. if (!conversation) {
  43. return;
  44. }
  45. if (conversation.conversationID && conversation.draftText) {
  46. const draftObject = JSONToObject(conversation.draftText);
  47. TUIStore.update(StoreName.CHAT, 'quoteMessage', { message: this.quoteMessageMap.get(draftObject.messageID) || undefined, type: draftObject.type });
  48. setEditorContentCallback(draftObject.html);
  49. }
  50. TUIConversationService.setConversationDraft({ conversationID: conversation.conversationID });
  51. }
  52. public generateAbstract(editorContent: Array<{ type: string; payload: { text?: string; file?: File } }>): string {
  53. let abstract = '';
  54. editorContent?.forEach((item: { type: string; payload: { text?: string; file?: File } }) => {
  55. switch (item.type) {
  56. case 'text':
  57. abstract += transformTextWithKeysToEmojiNames(item.payload.text || '');
  58. break;
  59. case 'image':
  60. abstract += TUITranslateService.t('TUIChat.图片');
  61. break;
  62. case 'video':
  63. abstract += TUITranslateService.t('TUIChat.视频');
  64. break;
  65. case 'file':
  66. abstract += TUITranslateService.t('TUIChat.文件');
  67. break;
  68. default:
  69. break;
  70. }
  71. });
  72. return abstract;
  73. }
  74. private isEditorNotEmpty(editorHTML: string) {
  75. return editorHTML && !editorHTML.includes('is-empty') && editorHTML !== '<p></p>';
  76. }
  77. }
  78. export default ConversationDraftManager.getInstance();