materialNoTextMould.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { Space } from "antd"
  2. import React, { useEffect, useState } from "react"
  3. import { TextEditor } from "@/pages/weComTask/components/textEditor"
  4. import MaterialMould from "@/pages/weComTask/components/materialMould"
  5. interface Props {
  6. value?: TASK_CREATE.ContentDTOProps
  7. onChange?: (value?: TASK_CREATE.ContentDTOProps) => void
  8. }
  9. /**
  10. * 群发文本设置
  11. * @returns
  12. */
  13. const MaterialNoTextMould: React.FC<Props> = ({ onChange, value }) => {
  14. /***************************************/
  15. const [textContent, setTextContent] = useState<string>()
  16. const [attachmentList, setAttachmentList] = useState<any>()
  17. /***************************************/
  18. /** 回填 */
  19. useEffect(() => {
  20. if (value) {
  21. if (value?.text?.content && !textContent) {
  22. const newValue = value?.text?.content
  23. setTextContent(newValue)
  24. }
  25. if (value?.attachmentList && value?.attachmentList?.length > 0 && !attachmentList) {
  26. const newAttachmentList = value?.attachmentList.map(item => {
  27. switch (item.msgType) {
  28. case 'TASK_CONTENT_IMAGE':
  29. return { mediaType: 'image', imageUrl: item?.image?.picUrl }
  30. case 'TASK_CONTENT_LINK':
  31. return { mediaType: 'link', linkDesc: item?.link?.desc, linkPicurl: item?.link?.picUrl, linkTitle: item?.link?.title, linkUrl: item?.link?.url }
  32. case 'TASK_STATUS_FILE':
  33. return { mediaType: 'file', fileUrl: item?.file?.fileUrl }
  34. case 'TASK_STATUS_VIDEO':
  35. return { mediaType: 'video', videoUrl: item?.video?.videoUrl }
  36. case 'TASK_STATUS_MINIPROGRAM':
  37. return {
  38. mediaType: 'miniprogram',
  39. miniprogramAppid: item.miniprogram?.appId,
  40. miniprogramPage: item?.miniprogram?.page,
  41. miniprogramPicurl: item?.miniprogram?.picUrl,
  42. miniprogramTitle: item?.miniprogram?.title
  43. }
  44. }
  45. })
  46. setAttachmentList(newAttachmentList)
  47. }
  48. }
  49. }, [value, textContent, attachmentList])
  50. /** 返回 */
  51. const handleChange = (textContent: any, attachmentList?: any) => {
  52. let newAttachmentList: TASK_CREATE.Attachment[] = []
  53. let text: any = {}
  54. if (textContent) {
  55. text = {
  56. content: textContent
  57. }
  58. }
  59. if (attachmentList && attachmentList?.length > 0) {
  60. newAttachmentList = attachmentList.map((item: TASK_CREATE.MediaContentProps) => {
  61. switch (item.mediaType) {
  62. case 'image':
  63. return { image: { picUrl: item.imageUrl }, msgType: 'TASK_CONTENT_IMAGE' }
  64. case 'video':
  65. return { video: { videoUrl: item.videoUrl }, msgType: 'TASK_STATUS_VIDEO' }
  66. case 'file':
  67. return { file: { fileUrl: item.fileUrl }, msgType: 'TASK_STATUS_FILE' }
  68. case 'link':
  69. return { link: { desc: item.linkDesc, picUrl: item.linkPicurl, title: item.linkTitle, url: item.linkUrl }, msgType: 'TASK_CONTENT_LINK' }
  70. case 'miniprogram':
  71. return {
  72. miniprogram: {
  73. appId: item.miniprogramAppid,
  74. page: item.miniprogramPage,
  75. picUrl: item.miniprogramPicurl,
  76. title: item.miniprogramTitle
  77. },
  78. msgType: 'TASK_STATUS_MINIPROGRAM'
  79. }
  80. }
  81. })
  82. }
  83. if (text?.content) {
  84. text.content = text.content.replace(/\n$/, '')
  85. }
  86. onChange?.({
  87. attachmentList: newAttachmentList as any,
  88. text
  89. })
  90. }
  91. return <Space style={{ width: '100%' }} direction='vertical'>
  92. <TextEditor
  93. maxStr={2000}
  94. value={textContent || ''}
  95. onChange={(value) => {
  96. setTextContent(value)
  97. handleChange(value, attachmentList)
  98. }}
  99. />
  100. <MaterialMould
  101. posiType="QF"
  102. noShowType={['voice']}
  103. textCount={1}
  104. value={attachmentList}
  105. onChange={(value) => {
  106. if (value?.length > 0) {
  107. let newText: TASK_CREATE.MediaContentProps[] = []
  108. let newAttachmentList: TASK_CREATE.MediaContentProps[] = []
  109. value?.forEach((cur: TASK_CREATE.MediaContentProps) => {
  110. if (cur.mediaType === 'text') {
  111. newText.push(cur)
  112. } else {
  113. newAttachmentList.push(cur)
  114. }
  115. })
  116. if (newText.length > 0) {
  117. if (textContent) {
  118. setTextContent('')
  119. setAttachmentList(newAttachmentList)
  120. handleChange(newText[0].textContent, newAttachmentList)
  121. } else {
  122. setTextContent('')
  123. setAttachmentList(newAttachmentList)
  124. handleChange(newText[0].textContent, newAttachmentList)
  125. }
  126. } else {
  127. setAttachmentList(value)
  128. handleChange(textContent, value)
  129. }
  130. } else {
  131. setAttachmentList([])
  132. handleChange(textContent, [])
  133. }
  134. }} />
  135. </Space>
  136. }
  137. export default React.memo(MaterialNoTextMould)