miniBox.tsx 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { useMemo, useState } from 'react';
  2. import styles from './index.less'
  3. import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
  4. import { useModel } from '@umijs/max';
  5. import { Config } from "@/models/appPageConifg";
  6. import { Tooltip } from 'antd';
  7. let noDrag = ["banners", "guess_like"]
  8. function MiniBox() {
  9. const { state, dispatch } = useModel("appPageConifg")
  10. const list = useMemo(() => {
  11. let pageConfig = state.pageConfigList?.find(page => page.pageUrl === state.activePage)
  12. let list: {
  13. appComponentId: any;
  14. componentType: string;
  15. showRightButton?: boolean;
  16. componentName?: string;
  17. remark?: string;
  18. configs?: Config[];
  19. }[] = []
  20. if (state.isWorkDirection) {
  21. let thePage = pageConfig?.workDirectionList?.find(page => page.workDirection == state.tabs)
  22. list = thePage?.componentConfigList || []
  23. } else {
  24. list = pageConfig?.workDirectionList?.[0].componentConfigList || []
  25. }
  26. return list
  27. }, [state])
  28. // 处理拖动结束
  29. const onDragEnd = (result: any) => {
  30. const { destination, source } = result;
  31. // 如果没有目标位置或目标位置与起始位置相同,则返回
  32. if (!destination || destination.index === source.index || destination.index === 0 || destination.index === list.length - 1) return;
  33. // 调整列表顺序
  34. const reorderedItems = Array.from(list);
  35. const [removed] = reorderedItems.splice(source.index, 1);
  36. reorderedItems.splice(destination.index, 0, removed);
  37. let { tabs, pageConfigList, activePage, isWorkDirection } = state
  38. let pageConfig = pageConfigList?.find(page => page.pageUrl === state.activePage)
  39. // 存在男女频页面
  40. if (isWorkDirection && pageConfig?.workDirectionList) {
  41. for (let page of pageConfig?.workDirectionList) {
  42. if (page.workDirection == tabs) {
  43. page.componentConfigList = reorderedItems
  44. }
  45. }
  46. }
  47. dispatch({
  48. type: 'setAll', params: { pageConfigList, index: state.index + 1 }
  49. })
  50. console.log(reorderedItems)
  51. };
  52. return list?.length > 0 && <DragDropContext onDragEnd={onDragEnd}>
  53. <Droppable droppableId="droppable">
  54. {(provided) => (
  55. <Tooltip title={<>
  56. 1.拖动下方模块改变页面组件排序<br />
  57. 2.点击下方模块可快捷选中组件<br />
  58. 3.顶部组件和底部组件不可改变顺便
  59. </>}
  60. color={"#2db7f5"} open={true}>
  61. <div
  62. {...provided.droppableProps}
  63. ref={provided.innerRef}
  64. className={styles.miniBox}
  65. >
  66. {list?.map((item, index) => (
  67. <Draggable
  68. key={item.appComponentId}
  69. draggableId={String(item.appComponentId)}
  70. index={index}
  71. isDragDisabled={noDrag?.includes(item.componentType)} // 使用isDragDisabled来控制拖动权限
  72. >
  73. {(provided) => (
  74. <div
  75. ref={provided.innerRef}
  76. {...provided.draggableProps}
  77. {...provided.dragHandleProps} // 始终传递dragHandleProps
  78. className={`${noDrag?.includes(item.componentType) ? styles.nodraggableItem : styles.draggableItem} ${state.compAc === item.componentType ? styles.compAc : ""}`} // 使用 CSS 样式而非内联样式
  79. onClick={() => { dispatch({ type: "setAll", params: { compAc: item.componentType } }) }}
  80. >
  81. {item.componentName || item.componentType}
  82. </div>
  83. )}
  84. </Draggable>
  85. ))}
  86. {provided.placeholder}
  87. </div>
  88. </Tooltip>
  89. )}
  90. </Droppable>
  91. </DragDropContext>
  92. }
  93. export default MiniBox