content.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { useDrop } from "ahooks";
  2. import { Col, Row, Space } from "antd";
  3. import React, { useState, useImperativeHandle, forwardRef, useRef, useMemo } from "react";
  4. import styles from './index.less'
  5. import { Banners } from "../components/banners";
  6. import { useModel } from "@umijs/max";
  7. import { HotBooks } from "../components/hot_books";
  8. import { HotCategory } from "../components/hot_category";
  9. import { Config } from "@/models/appPageConifg";
  10. // 使用 forwardRef 以支持传递 ref
  11. const DragItem = () => {
  12. const { state, dispatch } = useModel("appPageConifg")
  13. const [isHovering, setIsHovering] = useState(false);
  14. const dropRef = useRef(null);
  15. const list = useMemo(() => {
  16. let pageConfig = state.pageConfigList?.find(page => page.pageUrl === state.activePage)
  17. let list: {
  18. appComponentId: number | string;
  19. componentType: string;
  20. showRightButton?: boolean;
  21. componentName?: string;
  22. remark?: string;
  23. configs?: Config[];
  24. }[] = []
  25. if (state.isWorkDirection) {
  26. let thePage = pageConfig?.workDirectionList?.find(page => page.workDirection == state.tabs)
  27. list = thePage?.componentConfigList || []
  28. } else {
  29. list = pageConfig?.workDirectionList?.[0].componentConfigList || []
  30. }
  31. return list
  32. }, [state])
  33. useDrop(dropRef, {
  34. // 接收到组件拖拽到手机内容内,添加进入数据
  35. onDom: (content: string, e) => {
  36. let arr = content?.split('-')
  37. let appComponentId = arr[0]
  38. let componentType = arr[1]
  39. let newConfig: {
  40. appComponentId: number | string,//组件ID
  41. componentType: string,//组件类型
  42. showRightButton?: boolean,//是否展示左侧按钮
  43. componentName?: string,//组件名称
  44. remark?: string,//备注
  45. configs?: Config[],//组件内的配置
  46. } = { appComponentId, componentType, configs: [] }
  47. switch (componentType) {
  48. case 'hot_books':
  49. newConfig.configs = [
  50. {}, {}, {}, {}, {}
  51. ]
  52. newConfig.componentName = "今日热书"
  53. newConfig.showRightButton = true
  54. break;
  55. case "hot_category":
  56. newConfig.configs = []
  57. newConfig.componentName = "热门分类"
  58. newConfig.showRightButton = true
  59. break;
  60. }
  61. let { tabs, pageConfigList, activePage, isWorkDirection } = state
  62. let pageConfig = pageConfigList.find(page => page.pageUrl === activePage)
  63. // 首次添加页面基础数据
  64. if (!pageConfig) {
  65. pageConfigList.push({
  66. pageUrl: activePage,
  67. workDirectionList: isWorkDirection ? [
  68. {
  69. workDirection: 0,
  70. componentConfigList: []
  71. },
  72. {
  73. workDirection: 1,
  74. componentConfigList: []
  75. },
  76. ] : [
  77. {
  78. componentConfigList: []
  79. }
  80. ]
  81. })
  82. pageConfig = pageConfigList.find(page => page.pageUrl === activePage)
  83. }
  84. // 存在男女频页面
  85. if (isWorkDirection && pageConfig?.workDirectionList) {
  86. for (let page of pageConfig?.workDirectionList) {
  87. if (page.workDirection == tabs) {
  88. page.componentConfigList = [...page.componentConfigList, newConfig]
  89. }
  90. }
  91. } else if (pageConfig?.workDirectionList) {// 不存在男女频页面
  92. pageConfig.workDirectionList[0].componentConfigList = [...pageConfig.workDirectionList[0].componentConfigList, newConfig]
  93. }
  94. dispatch({
  95. type: 'setAll', params: { pageConfigList, index: state.index + 1 }
  96. })
  97. },
  98. onDragOver: (e) => {
  99. // console.log("拖拽中",e)
  100. },
  101. onDragEnter: () => setIsHovering(true),
  102. onDragLeave: () => setIsHovering(false),
  103. });
  104. return <div ref={dropRef} style={{ minHeight: '100%' }} >
  105. {
  106. list?.map(item => {
  107. return <React.Fragment key={item.appComponentId}>
  108. <div
  109. className={`${styles.comp} ${state.compAc === item.componentType ? styles.ac : ""}`}
  110. onClick={() => { dispatch({ type: "setAll", params: { compAc: item.componentType } }) }}
  111. >
  112. {/* banners */}
  113. {item.componentType === 'banners' && <Banners data={item.configs || []} />}
  114. {/* 热门书籍*/}
  115. {item.componentType === "hot_books" && <HotBooks data={item} />}
  116. {/* 热门分类 */}
  117. {item.componentType === "hot_category" && <HotCategory data={item} />}
  118. </div>
  119. <div className={styles.compBoxM} />
  120. </React.Fragment>
  121. })
  122. }
  123. {
  124. list?.length === 0 && <div className={styles.initBox}>
  125. 拖动左侧组件至此处
  126. </div>
  127. }
  128. </div>
  129. };
  130. // 定义组件的 Props 类型
  131. interface Props {
  132. }
  133. const Content = forwardRef((props: Props, ref) => {
  134. const { state, dispatch } = useModel("appPageConifg")
  135. // 使用 useImperativeHandle 暴露方法给父组件
  136. useImperativeHandle(ref, () => ({
  137. }));
  138. return <div className={styles.phone}>
  139. <div className={styles.content} >
  140. {/* 头部男女tabs */}
  141. <Space className={styles.tabs} size={[20, 20]}>
  142. {
  143. ["男生", "女生"].map((s, i) => {
  144. return <strong
  145. key={i}
  146. className={`${styles.tabs_text} ${state?.tabs === i ? styles.tabs_ac : ""}`}
  147. onClick={() => {
  148. dispatch({ type: "setAll", params: { tabs: i, compAc: 0 } })
  149. }}
  150. >
  151. {s}
  152. </strong>
  153. })
  154. }
  155. </Space>
  156. <div className={styles.content_box}>
  157. {/* 内容 */}
  158. <DragItem />
  159. </div>
  160. </div>
  161. </div>
  162. })
  163. export default Content;