flowEvent.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Graph, Node, EventArgs } from "@antv/x6";
  2. import { AddFlowchartElement, BatchEditFlowchartElement, BatchDeleteFlowchartElement } from "@/api/systemDesigner";
  3. export const handleGraphEvent = (graph: Graph) => {
  4. // 边开始拖拽点
  5. const sourceArrowhead = {
  6. name: "source-arrowhead",
  7. args: {
  8. attrs: {
  9. d: 'M -5,-5 5,-5 5,5 -5,5 Z',
  10. fill: '#fff',
  11. stroke: '#239edd',
  12. 'stroke-width': 1,
  13. },
  14. }
  15. }
  16. // 边结束拖拽点
  17. const targetArrowhead = {
  18. name: "target-arrowhead",
  19. args: {
  20. attrs: {
  21. d: 'M -5,-5 5,-5 5,5 -5,5 Z',
  22. fill: '#fff',
  23. stroke: '#239edd',
  24. 'stroke-width': 1,
  25. },
  26. }
  27. }
  28. // 边选中
  29. graph.on("edge:selected", (args) => {
  30. args.edge.addTools(['edge-editor', sourceArrowhead, targetArrowhead]);
  31. });
  32. // 边取消选中
  33. graph.on("edge:unselected", (args) => {
  34. setTimeout(() => {
  35. args.edge.removeTools(['edge-editor', sourceArrowhead, targetArrowhead]);
  36. }, 100);
  37. });
  38. // 控制连接桩显示/隐藏
  39. const showPorts = (ports: NodeListOf<SVGElement>, show: boolean) => {
  40. for (let i = 0, len = ports.length; i < len; i += 1) {
  41. ports[i].style.visibility = show ? "visible" : "hidden";
  42. }
  43. };
  44. graph.on(
  45. "node:mouseenter",
  46. ({
  47. _e,
  48. node,
  49. }: {
  50. _e: React.MouseEvent<HTMLDivElement, MouseEvent>;
  51. node: Node.Metadata;
  52. }) => {
  53. if (node.data?.isPage) return;
  54. const container = document.querySelector(`[data-cell-id="${node.id}"]`)!;
  55. if (!container) return;
  56. const ports = container.querySelectorAll(
  57. ".x6-port-body"
  58. ) as NodeListOf<SVGElement>;
  59. showPorts(ports, true);
  60. }
  61. );
  62. graph.on(
  63. "node:mouseleave",
  64. ({
  65. _e,
  66. node,
  67. }: {
  68. _e: React.MouseEvent<HTMLDivElement, MouseEvent>;
  69. node: Node.Metadata;
  70. }) => {
  71. if (node.data?.isPage) return;
  72. const container = document.querySelector("#graph-container")!;
  73. if (!container) return;
  74. const ports = container.querySelectorAll(
  75. ".x6-port-body"
  76. ) as NodeListOf<SVGElement>;
  77. showPorts(ports, false);
  78. }
  79. );
  80. // 处理添加节点事件 noCreate为不用创建节点
  81. graph.on("node:added", ({ node }) => {
  82. if(node.getData()?.noCreate) {
  83. setTimeout(() => {
  84. graph.removeNode(node.id);
  85. }, 50);
  86. }
  87. });
  88. };
  89. export const handleGraphApiEvent = (graph: Graph) => {
  90. graph.on("cell:added", (args) => {
  91. if(args.cell?.data?.isPage) return;
  92. const graphId = sessionStorage.getItem("projectId");
  93. const data = args.cell.toJSON();
  94. graphId && AddFlowchartElement({
  95. ...data,
  96. graphId,
  97. tools: ''
  98. });
  99. });
  100. // 批量处理节点更新
  101. let timer1: any;
  102. let map: Record<string, any> = {};
  103. graph.on("cell:change:*", (args: EventArgs["cell:change:*"]) => {
  104. if(args.cell?.data?.isPage || args.key === 'tools') return;
  105. const setData = () => {
  106. const id = args.cell.id;
  107. const data = args.cell.toJSON();
  108. delete data.tools;
  109. map[id] = data;
  110. }
  111. if(timer1) {
  112. setData();
  113. return;
  114. }
  115. setData();
  116. timer1 = setTimeout(() => {
  117. const graphId = sessionStorage.getItem("projectId");
  118. const list = Object.values(map);
  119. if(graphId && list.length > 0) {
  120. BatchEditFlowchartElement(list.map(item => {
  121. return {
  122. ...item,
  123. graphId
  124. }
  125. }));
  126. }
  127. timer1 = null;
  128. map = {};
  129. }, 1000);
  130. });
  131. let timer: any
  132. const ids: string[] = [];
  133. graph.on("cell:removed", (args) => {
  134. // 加入防抖 如果连续多个删除调用批量删除
  135. if(timer) {
  136. ids.push(args.cell.id);
  137. return;
  138. }
  139. ids.push(args.cell.id);
  140. timer = setTimeout(() => {
  141. timer = null;
  142. if(ids.length > 0) {
  143. BatchDeleteFlowchartElement({ids});
  144. }
  145. ids.splice(0, ids.length);
  146. }, 500);
  147. });
  148. };