newText.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import TextAideInput from "@/pages/launchSystemV3/components/TextAideInput"
  2. import { extractAndFilterBracketsContent, txtLength } from "@/utils/utils"
  3. import { Button, Card, Form, Modal, Popconfirm, Radio, Space, message } from "antd"
  4. import React, { useEffect, useState } from "react"
  5. import { TextTypeList } from "../../const"
  6. import { DeleteOutlined, MinusCircleOutlined, PlusOutlined } from "@ant-design/icons"
  7. import AddTextS from "./addTextS"
  8. import style from '../index.less'
  9. import styles from '../Material/index.less'
  10. import VideoNews from "@/pages/launchSystemNew/components/newsModal/videoNews"
  11. interface Props {
  12. textData: any,
  13. dynamicMaterialDTos: any,
  14. mediaType: 0 | 1 | 2,
  15. deliveryMode?: string,
  16. value?: any,
  17. visible?: boolean
  18. onClose?: () => void
  19. onChange?: (value: any) => void
  20. }
  21. /**
  22. * 创意文案
  23. * @param param0
  24. * @returns
  25. */
  26. const NewText: React.FC<Props> = ({ visible, onClose, onChange, value, textData, dynamicMaterialDTos, mediaType, deliveryMode }) => {
  27. /*************************************/
  28. const [form] = Form.useForm();
  29. const type = Form.useWatch('type', form)
  30. const textDto = Form.useWatch('textDto', form)
  31. const [textList, setTextList] = useState<PULLIN.TextDtoProps[]>([])
  32. const [desVisible, setDesVisible] = useState<boolean>(false)
  33. const [addSTitle, setAddStitle] = useState<string>()
  34. const [newText, setNewText] = useState<{ description?: string[], title?: string[] }>({})
  35. /*************************************/
  36. const handleOk = (values: any) => {
  37. console.log(values)
  38. const { type, textDto } = values
  39. onChange?.({
  40. type,
  41. dynamicCreativesTextDetailDTOList: textDto
  42. })
  43. }
  44. useEffect(() => {
  45. if (value && Object.keys(value).length) {
  46. const { type, dynamicCreativesTextDetailDTOList } = value
  47. form.setFieldsValue({ type, textDto: dynamicCreativesTextDetailDTOList })
  48. }
  49. }, [value])
  50. useEffect(() => {
  51. if (textData && Object.keys(textData)) {
  52. let newText: { description?: string[], title?: string[] } = {}
  53. let data = Object.values(textData).map((item: any) => {
  54. let content = item.children.content
  55. if (item.name === 'description') {
  56. newText.description = ['']
  57. } else if (item.name === 'title') {
  58. newText.title = ['']
  59. }
  60. return { label: content.description, restriction: content.restriction, value: item.name, required: item.required, arrayProperty: item?.arrayProperty }
  61. })
  62. setNewText(newText)
  63. if (!(value && Object.keys(value).length)) { form.setFieldsValue({ textDto: [newText] }) }
  64. setTextList(data)
  65. }
  66. }, [textData, value])
  67. // 一一对应显示素材
  68. const showMaterial = (index: number) => {
  69. const dynamicGroup = dynamicMaterialDTos?.dynamicGroup
  70. if (dynamicGroup && Object.keys(dynamicGroup).length) {
  71. let dynamic = dynamicGroup[index]
  72. const keys = Object.keys(dynamic)
  73. if (deliveryMode === "DELIVERY_MODE_CUSTOMIZE") {
  74. return <div className={style.detail_body_m}>
  75. {(keys.includes('video_id') || keys.includes('short_video1')) ? <>
  76. <div className={style.video}>
  77. <VideoNews src={dynamic?.video_id?.url || dynamic?.short_video1?.url} style={{ width: 40, height: 30 }} />
  78. {dynamic?.cover_id && <div className={style.cover_image} style={{ marginLeft: 4, width: 40, height: 30, minWidth: 42 }}>
  79. <img src={dynamic?.cover_id?.url} style={{ maxWidth: '96%', maxHeight: '96%' }} />
  80. </div>}
  81. </div>
  82. </> : keys.includes('image_id') ? <>
  83. <div className={style.cover_image} style={{ width: 40, height: 30, minWidth: 42 }}>
  84. <img src={dynamic?.image_id?.url} />
  85. </div>
  86. </> : (keys.includes('image_list') || keys.includes('element_story')) ? <>
  87. {dynamic?.[keys.includes('image_list') ? 'image_list' : 'element_story']?.map((item: { url: string | undefined; }, index: undefined) => <div className={style.cover_image} key={index} style={{ width: 30, height: 24, minWidth: 32 }}>
  88. <img src={item?.url} />
  89. </div>)}
  90. </> : null}
  91. </div>
  92. } else {
  93. return <div style={{ display: 'flex', gap: 5, flexWrap: 'wrap' }}>
  94. {dynamic?.list?.map((item: any, index: number) => {
  95. if (Array.isArray(item)) {
  96. let length = item.length
  97. return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
  98. <div className={styles.content} style={{ width: 30, height: 30 }}>
  99. {item.map((l, i) => <img src={l?.url} key={i} style={{ width: length === 6 ? 9.999 : 14.999 }} />)}
  100. </div>
  101. </div>
  102. } else if (item?.url?.includes('mp4')) {
  103. return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
  104. <div className={styles.content} style={{ width: 30, height: 30 }}>
  105. <VideoNews src={item?.url} style={{ width: 30, height: 30 }} maskBodyStyle={{ backgroundColor: "rgba(242, 246, 254, 0.1)" }} />
  106. </div>
  107. </div>
  108. } else {
  109. return <div className={styles.boxList_body_item} key={index} style={{ width: 30, height: 30 }}>
  110. <div className={styles.content} style={{ width: 30, height: 30 }}><img src={item?.url} /></div>
  111. </div>
  112. }
  113. })}
  114. </div>
  115. }
  116. }
  117. return null
  118. }
  119. return <Modal
  120. title={<Space>
  121. <strong style={{ fontSize: 20 }}>创意文案</strong>
  122. {type !== 0 && <>
  123. {textList.some(item => item.value === "description") && <a style={{ fontSize: 12 }} onClick={() => { setDesVisible(true); setAddStitle(textList.find(item => item.value === "description")?.label) }}>批量添加{textList.find(item => item.value === "description")?.label}</a>}
  124. {textList.some(item => item.value === "title") && <a style={{ fontSize: 12 }} onClick={() => { setDesVisible(true); setAddStitle(textList.find(item => item.value === "description")?.label) }}>批量添加{textList.find(item => item.value === "description")?.label}</a>}
  125. </>}
  126. </Space>}
  127. open={visible}
  128. onCancel={onClose}
  129. footer={null}
  130. width={800}
  131. className={`modalResetCss`}
  132. bodyStyle={{ padding: '0 0 40px', position: 'relative', borderRadius: '0 0 8px 8px' }}
  133. maskClosable={false}
  134. >
  135. <Form
  136. form={form}
  137. name="newText"
  138. labelAlign='left'
  139. layout="vertical"
  140. colon={false}
  141. style={{ backgroundColor: '#f1f4fc', maxHeight: 650, overflow: 'hidden', overflowY: 'auto', padding: '0 10px 10px', borderRadius: '0 0 8px 8px' }}
  142. scrollToFirstError={{
  143. behavior: 'smooth',
  144. block: 'center'
  145. }}
  146. onFinishFailed={({ errorFields }) => {
  147. message.error(errorFields?.[0]?.errors?.[0])
  148. }}
  149. initialValues={{
  150. type: 0,
  151. textDto: [{ description: [''], title: [''] }]
  152. }}
  153. onFinish={handleOk}
  154. >
  155. <Card className="cardResetCss" style={{ marginTop: 10 }}>
  156. <Form.Item name="type" label={<strong>文案分配规则</strong>} style={{ marginBottom: 0 }} rules={[{ required: true, message: '请选择文案分配规则!' }]}>
  157. <Radio.Group onChange={(e) => {
  158. let value = e.target.value
  159. let count = dynamicMaterialDTos.dynamicGroup.length
  160. let oldtextDto: PULLIN.TextDtoProps[] = JSON.parse(JSON.stringify(textDto))
  161. let length = oldtextDto.length
  162. if (value === 0) {
  163. oldtextDto = [textDto[0] || {}]
  164. } else if (value === 1) {
  165. if (count > length) {
  166. oldtextDto = oldtextDto.concat(Array(count - length).fill(newText || { description: [''] }))
  167. } else {
  168. oldtextDto = oldtextDto.slice(0, count)
  169. }
  170. } else if (value === 2) {
  171. if (count < length) {
  172. oldtextDto = oldtextDto.slice(0, count)
  173. }
  174. }
  175. form.setFieldsValue({ textDto: oldtextDto })
  176. }}>
  177. {TextTypeList.filter(item => mediaType !== 1 ? item.value !== 4 : true).map(item => <Radio value={item.value} key={item.value}>{item.label}</Radio>)}
  178. </Radio.Group>
  179. </Form.Item>
  180. </Card>
  181. <Form.List name="textDto">
  182. {(fields, { add, remove }) => (<>
  183. {fields.map(({ key, name, ...restField }, num) => (
  184. <Card
  185. title={type === 0 ? null : <div style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
  186. {type === 1 ? <strong style={{ fontSize: 14 }}>创意组{num + 1}</strong> : type === 0 ? null : <strong style={{ fontSize: 14 }}>文案组{num + 1}</strong>}
  187. {type === 1 && showMaterial(num)}
  188. </div>}
  189. className="cardResetCss"
  190. style={{ marginTop: 10, width: '100%' }}
  191. key={key}
  192. extra={([3, 2, 4].includes(type) && textDto?.length > 1) && <Popconfirm
  193. title="你确定删除当前文案组?"
  194. onConfirm={() => remove(name)}
  195. >
  196. <DeleteOutlined style={{ color: 'red' }} />
  197. </Popconfirm>}
  198. >
  199. <div style={{ display: 'flex', flexWrap: 'wrap', gap: 10, width: '100%', flexDirection: 'column' }}>
  200. {textList.map(item => {
  201. return <Form.List
  202. {...restField}
  203. name={[name, item.value]}
  204. key={key}
  205. >
  206. {(fields, { add, remove }) => <>
  207. {fields.map((field, tIndex) => (
  208. <Form.Item
  209. {...field}
  210. label={<Space>
  211. <strong>{item.label}</strong>
  212. {textDto?.[num]?.[item.value]?.length > 1 && <a style={{ color: 'red', fontSize: 12 }} onClick={() => remove(field.name)}><MinusCircleOutlined /></a>}
  213. </Space>}
  214. style={{ marginBottom: 0, width: '100%' }}
  215. rules={[{
  216. required: item.required, validator: (rule, value) => {
  217. if (!value) {
  218. return Promise.reject('请输入正确的' + item.label)
  219. } else if (!value.match(RegExp(item.restriction.textRestriction.textPattern))) {
  220. return Promise.reject('请输入正确的' + item.label)
  221. } else if (txtLength(value) > item.restriction.textRestriction.maxLength) {
  222. return Promise.reject('请输入正确的' + item.label)
  223. }
  224. const result = extractAndFilterBracketsContent(value);
  225. if (result.extracted.length > 4) {
  226. return Promise.reject('表情数量不得超出: 4 个')
  227. }
  228. return Promise.resolve()
  229. }
  230. }]}
  231. >
  232. <TextAideInput placeholder={'请输入' + item.label} style={{ width: 580 }} maxTextLength={item.restriction.textRestriction.maxLength} />
  233. </Form.Item>
  234. ))}
  235. {deliveryMode === 'DELIVERY_MODE_COMPONENT' && item.arrayProperty?.maxNumber > 1 && textDto?.[num]?.[item.value]?.length < item.arrayProperty?.maxNumber && <Form.Item style={{ marginTop: 6, marginBottom: 0 }}>
  236. <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />} >
  237. 新增{item.label}
  238. </Button>
  239. </Form.Item>}
  240. </>}
  241. </Form.List>
  242. })}
  243. </div>
  244. </Card>
  245. ))}
  246. {[3, 2, 4].includes(type) && !(type === 2 && textDto.length >= dynamicMaterialDTos.dynamicGroup.length) && <Form.Item style={{ marginTop: 10, marginBottom: 0 }}>
  247. <Button type="primary" onClick={() => add(newText)} block icon={<PlusOutlined />} disabled={type === 3 && textDto.length >= 30}>
  248. 新增
  249. </Button>
  250. </Form.Item>}
  251. </>)}
  252. </Form.List>
  253. <Form.Item className="submit_pull">
  254. <Space>
  255. <Button onClick={onClose}>取消</Button>
  256. <Button type="primary" htmlType="submit" className="modalResetCss">
  257. 确定
  258. </Button>
  259. </Space>
  260. </Form.Item>
  261. </Form>
  262. {/* 批量添加 */}
  263. {desVisible && <AddTextS
  264. visible={desVisible}
  265. title={addSTitle}
  266. onClose={() => {
  267. setDesVisible(false)
  268. setAddStitle(undefined)
  269. }}
  270. onChange={(value) => {
  271. if (value) {
  272. let valList = value
  273. .split(/\r?\n/) // 按换行符分割字符串
  274. .map(line => line.trim()) // 去除每行的首尾空白
  275. .filter(line => line !== ''); // 过滤掉空行
  276. let fieldData = textList.find(item => item.label === addSTitle)
  277. let count = dynamicMaterialDTos.dynamicGroup.length
  278. if (type === 1) {
  279. let len = 0
  280. const newTextDto = textDto.map((item: { [x: string]: any }) => {
  281. if (fieldData?.value && (item?.[fieldData?.value]?.length === 0 || !item?.[fieldData?.value]?.every((t: string) => t)) && valList.length >= len + 1) {
  282. return {
  283. ...item,
  284. [fieldData.value]: (item?.[fieldData?.value]?.length === 0 ? [''] : item?.[fieldData?.value]).map((t: string) => {
  285. if (t) {
  286. return t
  287. } else {
  288. return valList[len++]
  289. }
  290. })
  291. }
  292. }
  293. return item
  294. })
  295. form.setFieldsValue({
  296. textDto: newTextDto
  297. })
  298. } else if (type === 2) {
  299. let len = 0
  300. const newTextDto = textDto.map((item: { [x: string]: any }) => {
  301. if (fieldData?.value && (item?.[fieldData?.value]?.length === 0 || !item?.[fieldData?.value]?.every((t: string) => t)) && valList.length >= len + 1) {
  302. return {
  303. ...item,
  304. [fieldData.value]: (item?.[fieldData?.value]?.length === 0 ? [''] : item?.[fieldData?.value]).map((t: string) => {
  305. if (t) {
  306. return t
  307. } else {
  308. return valList[len++]
  309. }
  310. })
  311. }
  312. }
  313. return item
  314. })
  315. let diffTextDto: any[] = []
  316. if (newTextDto.length < count && len < valList.length) {
  317. let diffCount = count - newTextDto.length
  318. let diffTextCount = valList.length - len
  319. let diff = 0
  320. if (diffCount >= diffTextCount) {
  321. diff = diffTextCount
  322. } else {
  323. diff = diffCount
  324. }
  325. diffTextDto = Array(diff).fill('').map((item: { [x: string]: any }) => {
  326. if (fieldData?.value) {
  327. return { ...item, [fieldData.value]: [valList[len++]] }
  328. }
  329. return item
  330. })
  331. }
  332. form.setFieldsValue({
  333. textDto: [...newTextDto, ...diffTextDto]
  334. })
  335. } else if ([3, 4].includes(type)) {
  336. let len = 0
  337. const newTextDto = textDto.map((item: { [x: string]: any }) => {
  338. if (fieldData?.value && (item?.[fieldData?.value]?.length === 0 || !item?.[fieldData?.value]?.every((t: string) => t)) && valList.length >= len + 1) {
  339. return {
  340. ...item, [fieldData.value]: (item?.[fieldData?.value]?.length === 0 ? [''] : item?.[fieldData?.value]).map((t: string) => {
  341. if (t) {
  342. return t
  343. } else {
  344. return valList[len++]
  345. }
  346. })
  347. }
  348. }
  349. return item
  350. })
  351. let diffTextDto: any[] = []
  352. if (len < valList.length) {
  353. let diff = valList.length - len
  354. diffTextDto = Array(diff).fill('').map((item: { [x: string]: any }) => {
  355. if (fieldData?.value) {
  356. return { ...item, [fieldData.value]: [valList[len++]] }
  357. }
  358. return item
  359. })
  360. }
  361. form.setFieldsValue({
  362. textDto: [...newTextDto, ...diffTextDto]
  363. })
  364. }
  365. }
  366. setDesVisible(false)
  367. setAddStitle(undefined)
  368. }}
  369. />}
  370. </Modal>
  371. }
  372. export default React.memo(NewText)