base.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { register } from "@antv/x6-react-shape";
  2. import { Node } from "@antv/x6";
  3. import { ports, defaultData } from "../config/data";
  4. import { useSizeHook, useShapeProps } from "@/hooks";
  5. const component = ({ node }: { node: Node }) => {
  6. const { fill, stroke, opacity } = node.getData();
  7. const { size, ref } = useSizeHook();
  8. const {
  9. fillContent,
  10. defsContent,
  11. strokeColor,
  12. strokeWidth,
  13. strokeDasharray,
  14. } = useShapeProps(fill, size, stroke);
  15. return (
  16. <>
  17. <div
  18. className="relative text-0 w-full h-full"
  19. ref={ref}
  20. style={{ opacity: opacity / 100 }}
  21. >
  22. <svg
  23. className="w-full h-full"
  24. viewBox={`0 0 ${size?.width} ${size?.height}`}
  25. xmlns="http://www.w3.org/2000/svg"
  26. >
  27. <defs>{defsContent}</defs>
  28. <rect
  29. x={strokeWidth}
  30. y={strokeWidth}
  31. width={size?.width - 4}
  32. height={size?.height - 4}
  33. fill={fillContent}
  34. stroke={strokeColor}
  35. strokeDasharray={strokeDasharray}
  36. strokeWidth={strokeWidth}
  37. />
  38. </svg>
  39. </div>
  40. </>
  41. );
  42. };
  43. register({
  44. shape: "custom-react-base",
  45. width: 100,
  46. height: 100,
  47. effect: ["data"],
  48. component: component,
  49. });
  50. const baseNode = {
  51. shape: "custom-react-base",
  52. data: {
  53. label: "",
  54. ...defaultData,
  55. },
  56. ports,
  57. };
  58. export default baseNode;