fiveStart.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { CompoundedComponent } from "@/types";
  2. import { register } from "@antv/x6-react-shape";
  3. import { Graph, Node } from "@antv/x6";
  4. import { ports, defaultData } from "../../config/data";
  5. import CustomInput from "../CustomInput";
  6. import { useSizeHook, useShapeProps } from "@/hooks";
  7. const component = ({ node }: { node: Node }) => {
  8. const { label, text, fill, stroke, opacity } = node.getData();
  9. const { size, ref } = useSizeHook();
  10. const {
  11. fillContent,
  12. defsContent,
  13. strokeColor,
  14. strokeWidth,
  15. strokeDasharray,
  16. } = useShapeProps(fill, size, stroke);
  17. // 计算正五边形的顶点坐标
  18. const getD = (width: number, height: number) => {
  19. let path = `
  20. M ${width / 2} ${strokeWidth}
  21. L ${width * 0.62},${height * 0.38}
  22. L ${width - strokeWidth},${ height * 0.38}
  23. L ${width * 0.7},${height * 0.62}
  24. L ${width * 0.82},${height - strokeWidth}
  25. L ${width / 2},${ height * 0.76}
  26. L ${width * 0.18},${height - strokeWidth}
  27. L ${width * 0.3},${height * 0.62}
  28. L ${strokeWidth},${height * 0.38}
  29. L ${width * 0.38},${ height * 0.38}
  30. L ${width / 2},${strokeWidth} Z
  31. `;
  32. return path;
  33. };
  34. return (
  35. <>
  36. <div
  37. className="relative text-0 w-full h-full"
  38. ref={ref}
  39. style={{ opacity: opacity / 100 }}
  40. >
  41. <CustomInput value={label} styles={text} node={node} />
  42. <svg
  43. className="w-full h-full"
  44. viewBox={`0 0 ${size?.width} ${size?.height}`}
  45. xmlns="http://www.w3.org/2000/svg"
  46. >
  47. <defs>{defsContent}</defs>
  48. <path
  49. d={getD(size?.width, size?.height)}
  50. fill={fillContent}
  51. stroke={strokeColor}
  52. strokeDasharray={strokeDasharray}
  53. strokeWidth={strokeWidth}
  54. />
  55. </svg>
  56. </div>
  57. </>
  58. );
  59. };
  60. register({
  61. shape: "custom-react-fiveStar",
  62. width: 90,
  63. height: 90,
  64. effect: ["data"],
  65. component: component,
  66. });
  67. // 左侧连接桩
  68. Graph.registerPortLayout("fiveStarLeft", (portsPositionArgs, elemBBox) => {
  69. return portsPositionArgs.map((item) => {
  70. return {
  71. ...item,
  72. position: {
  73. x: 0,
  74. y: elemBBox.height * 0.4,
  75. },
  76. };
  77. });
  78. });
  79. // 右侧连接桩
  80. Graph.registerPortLayout("fiveStarRight", (portsPositionArgs, elemBBox) => {
  81. return portsPositionArgs.map((item) => {
  82. return {
  83. ...item,
  84. position: {
  85. x: elemBBox.width,
  86. y: elemBBox.height * 0.4,
  87. },
  88. };
  89. });
  90. });
  91. const FiveStar: CompoundedComponent = {
  92. name: "五角星",
  93. icon: require("./image/pentagon.png"),
  94. node: {
  95. shape: "custom-react-fiveStar",
  96. data: {
  97. label: "",
  98. ...defaultData,
  99. },
  100. ports: {
  101. ...ports,
  102. groups: {
  103. ...ports.groups,
  104. fiveStarLeft: {
  105. position: "fiveStarLeft",
  106. attrs: {
  107. circle: {
  108. r: 4,
  109. magnet: true,
  110. stroke: "#5F95FF",
  111. strokeWidth: 1,
  112. fill: "#fff",
  113. style: {
  114. visibility: "hidden",
  115. },
  116. },
  117. },
  118. },
  119. fiveStarRight: {
  120. position: "fiveStarRight",
  121. attrs: {
  122. circle: {
  123. r: 4,
  124. magnet: true,
  125. stroke: "#5F95FF",
  126. strokeWidth: 1,
  127. fill: "#fff",
  128. style: {
  129. visibility: "hidden",
  130. },
  131. },
  132. },
  133. },
  134. },
  135. items: [
  136. {
  137. group: "top",
  138. args: {
  139. dy: 2,
  140. },
  141. },
  142. {
  143. group: "fiveStarLeft",
  144. },
  145. {
  146. group: "bottom",
  147. args: {
  148. dy: -2,
  149. },
  150. },
  151. {
  152. group: "fiveStarRight",
  153. },
  154. ],
  155. },
  156. },
  157. };
  158. export default FiveStar;