flowEvent.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Graph, Node } from "@antv/x6";
  2. export const handleGraphEvent = (graph: Graph) => {
  3. const sourceArrowhead = {
  4. name: "source-arrowhead",
  5. args: {
  6. attrs: {
  7. d: 'M -5,-5 5,-5 5,5 -5,5 Z',
  8. fill: '#fff',
  9. stroke: '#239edd',
  10. 'stroke-width': 1,
  11. },
  12. }
  13. }
  14. const targetArrowhead = {
  15. name: "target-arrowhead",
  16. args: {
  17. attrs: {
  18. d: 'M -5,-5 5,-5 5,5 -5,5 Z',
  19. fill: '#fff',
  20. stroke: '#239edd',
  21. 'stroke-width': 1,
  22. },
  23. }
  24. }
  25. // 边选中
  26. graph.on("edge:selected", (args) => {
  27. args.edge.addTools(['edge-editor', sourceArrowhead, targetArrowhead]);
  28. });
  29. // 边取消选中
  30. graph.on("edge:unselected", (args) => {
  31. args.edge.removeTools(['edge-editor', sourceArrowhead, targetArrowhead]);
  32. });
  33. // 控制连接桩显示/隐藏
  34. const showPorts = (ports: NodeListOf<SVGElement>, show: boolean) => {
  35. for (let i = 0, len = ports.length; i < len; i += 1) {
  36. ports[i].style.visibility = show ? "visible" : "hidden";
  37. }
  38. };
  39. graph.on(
  40. "node:mouseenter",
  41. ({
  42. _e,
  43. node,
  44. }: {
  45. _e: React.MouseEvent<HTMLDivElement, MouseEvent>;
  46. node: Node.Metadata;
  47. }) => {
  48. if (node.data?.isPage) return;
  49. const container = document.querySelector(`[data-cell-id="${node.id}"]`)!;
  50. if (!container) return;
  51. const ports = container.querySelectorAll(
  52. ".x6-port-body"
  53. ) as NodeListOf<SVGElement>;
  54. showPorts(ports, true);
  55. }
  56. );
  57. graph.on(
  58. "node:mouseleave",
  59. ({
  60. _e,
  61. node,
  62. }: {
  63. _e: React.MouseEvent<HTMLDivElement, MouseEvent>;
  64. node: Node.Metadata;
  65. }) => {
  66. if (node.data?.isPage) return;
  67. const container = document.querySelector("#graph-container")!;
  68. if (!container) return;
  69. const ports = container.querySelectorAll(
  70. ".x6-port-body"
  71. ) as NodeListOf<SVGElement>;
  72. showPorts(ports, false);
  73. }
  74. );
  75. // 处理添加节点事件 noCreate为不用创建节点
  76. graph.on("node:added", ({ node }) => {
  77. if(node.getData()?.noCreate) {
  78. setTimeout(() => {
  79. graph.removeNode(node.id);
  80. }, 50);
  81. }
  82. })
  83. };