appModel.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Cell, EventArgs, Graph } from "@antv/x6";
  2. import { useEffect, useRef, useState } from "react";
  3. import { message } from "antd";
  4. import brushImg from "@/assets/image/brush.svg"
  5. import { cellStyle } from '@/types'
  6. import { EditGraph } from "@/api/systemDesigner";
  7. interface PageSettings {
  8. // 背景颜色
  9. backgroundColor: string;
  10. // 宽
  11. width: number;
  12. // 高
  13. height: number;
  14. // 显示网格
  15. grid: boolean;
  16. // 网格大小
  17. gridSize: number;
  18. // 显示跨线
  19. jumpOver: boolean;
  20. // 显示打印分割线
  21. printLine: boolean;
  22. // 显示水印
  23. showWatermark: boolean;
  24. // 水印文字
  25. watermarkText: string;
  26. // 主题风格
  27. theme: string;
  28. }
  29. export default function appModel() {
  30. // 隐藏/显示右侧面板
  31. const [showRightPanel, setShowRightPanel] = useState(true);
  32. // 格式刷启用
  33. const [enableFormatBrush, setEnableFormatBrush] = useState(false);
  34. // 格式刷样式
  35. const formatBrushStyle = useRef<cellStyle>();
  36. // 左侧面板激活
  37. const [leftPanelActiveKey, setLeftPanelActiveKey] = useState("1");
  38. // 历史记录
  39. const [showHistory, setShowHistory] = useState(false);
  40. // 激活AI对话 chat对话 creator创作
  41. const [activeAI, setActiveAI] = useState<'chat' | 'creator'>();
  42. // 右侧面板tab activeKey
  43. const [rightPanelTabActiveKey, setRightPanelTabActiveKey] = useState("1");
  44. // 流程图右侧面板宽度
  45. const [flowRightPanelWidth, setFlowRightPanelWidth] = useState(280);
  46. const [rightPanelOriginalWidth, setRightOriginalPanelWidth] = useState(280);
  47. // 思维导图右侧宽度
  48. const [mindmapRightPanelWidth, setMindMapRightPanelWidth] = useState(280);
  49. const graphRef = useRef<Graph>();
  50. const [pageState, setPageState] = useState<PageSettings>({
  51. backgroundColor: "transparent",
  52. width: 0,
  53. height: 0,
  54. grid: true,
  55. gridSize: 15,
  56. jumpOver: false,
  57. printLine: false,
  58. showWatermark: false,
  59. watermarkText: "沙鲁低码平台",
  60. theme: '1'
  61. });
  62. const [historyColor, setHistoryColor] = useState<string[]>([
  63. '#B0E38F',
  64. '#F19594',
  65. '#EC7270',
  66. '#996A99',
  67. '#4F374F',
  68. '#94E0E1',
  69. '#FBF1B8',
  70. '#E3F0E1',
  71. '#FDF8DC',
  72. '#569230',
  73. ]);
  74. // 收折右侧样式面板
  75. const toggleRightPanel = (show?: boolean) => {
  76. if(show !== undefined) setShowRightPanel(show);
  77. else setShowRightPanel(!showRightPanel);
  78. }
  79. // 处理页面设置
  80. const onChangePageSettings = (key: keyof PageSettings, value: any) => {
  81. setPageState((state) => {
  82. return {
  83. ...state,
  84. [key]: value
  85. }
  86. });
  87. if(sessionStorage.getItem("projectId")) {
  88. EditGraph({
  89. id: sessionStorage.getItem("projectId"),
  90. [key]: value
  91. });
  92. }
  93. }
  94. /**
  95. * 新增颜色记录
  96. * 添加时,将移除最早的颜色
  97. * @param color 颜色
  98. */
  99. const addHistoryCoolor = (color: string) => {
  100. if( historyColor.includes(color)) return;
  101. setHistoryColor((state) => {
  102. const newState = [...state];
  103. newState.unshift(color);
  104. newState.pop();
  105. return newState;
  106. })
  107. }
  108. // 主题风格存入session其他地方使用
  109. useEffect(() => {
  110. sessionStorage.setItem("flow-theme-key", pageState.theme);
  111. }, [pageState.theme]);
  112. useEffect(() => {
  113. const graphRoot = document.querySelector(
  114. "#graph-container"
  115. ) as HTMLDivElement;
  116. const pageRoot = document.querySelector(
  117. "#flow_canvas_container"
  118. ) as HTMLDivElement;
  119. if(enableFormatBrush) {
  120. graphRoot && (graphRoot.style.cursor = `url(${brushImg}), auto`);
  121. pageRoot && (pageRoot.style.cursor = `url(${brushImg}), auto`);
  122. } else {
  123. graphRoot && (graphRoot.style.cursor = "default");
  124. pageRoot && (pageRoot.style.cursor = "default");
  125. }
  126. }, [enableFormatBrush]);
  127. const handleClick = (args: EventArgs & { cell: Cell }) => {
  128. // 取消格式刷
  129. if(!args?.cell || args?.cell?.data?.isPage) {
  130. formatBrushStyle.current = undefined;
  131. setEnableFormatBrush(false);
  132. graphRef.current?.off("cell:click", handleClick);
  133. graphRef.current?.off("blank:click", handleClick);
  134. } else {
  135. if(args.cell.data?.lock) return;
  136. // 应用格式刷
  137. const data = args.cell.data;
  138. args.cell.setData({
  139. text: formatBrushStyle.current?.text || data?.text,
  140. fill: formatBrushStyle.current?.fill || data?.fill,
  141. stroke: formatBrushStyle.current?.stroke || data?.stroke,
  142. opacity: formatBrushStyle.current?.opacity || data?.opacity
  143. })
  144. }
  145. };
  146. // 开启格式刷
  147. const toggleFormatBrush = (graph: Graph) => {
  148. graphRef.current = graph;
  149. const cell = graph?.getSelectedCells()?.find(item => item.isNode());
  150. setEnableFormatBrush((state) => {
  151. if(!state) {
  152. const data = cell?.getData();
  153. formatBrushStyle.current = data;
  154. message.info('格式刷已开启');
  155. graph.on("cell:click", handleClick);
  156. graph.on("blank:click", handleClick);
  157. } else {
  158. formatBrushStyle.current = undefined;
  159. graph.off("cell:click", handleClick);
  160. graph.off("blank:click", handleClick);
  161. }
  162. return !state;
  163. });
  164. }
  165. document.addEventListener("keydown", (e) => {
  166. if (e.key === "Escape") {
  167. setEnableFormatBrush(false);
  168. }
  169. });
  170. return {
  171. showRightPanel,
  172. toggleRightPanel,
  173. pageState,
  174. setPageState,
  175. onChangePageSettings,
  176. historyColor,
  177. addHistoryCoolor,
  178. enableFormatBrush,
  179. toggleFormatBrush,
  180. rightPanelTabActiveKey,
  181. setRightPanelTabActiveKey,
  182. leftPanelActiveKey,
  183. setLeftPanelActiveKey,
  184. showHistory,
  185. setShowHistory,
  186. activeAI,
  187. setActiveAI,
  188. flowRightPanelWidth,
  189. setFlowRightPanelWidth,
  190. mindmapRightPanelWidth,
  191. setMindMapRightPanelWidth,
  192. rightPanelOriginalWidth,
  193. setRightOriginalPanelWidth
  194. }
  195. }