braces.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { CompoundedComponent } from "@/types";
  2. import { register } from "@antv/x6-react-shape";
  3. import { Node } from "@antv/x6";
  4. import { ports, defaultData } from "../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. const { width: w, height: h } = size;
  18. return (
  19. <>
  20. <div
  21. className="relative text-0 w-full h-full"
  22. ref={ref}
  23. style={{ opacity: opacity / 100 }}
  24. >
  25. <CustomInput value={label} styles={text} node={node} />
  26. <svg
  27. className="w-full h-full"
  28. viewBox={`0 0 ${size?.width} ${size?.height}`}
  29. xmlns="http://www.w3.org/2000/svg"
  30. >
  31. <defs>{defsContent}</defs>
  32. <path
  33. d={`
  34. M ${Math.min(w*0.2,18)},${strokeWidth}
  35. Q ${Math.min(w*0.1,9)},${strokeWidth} ${Math.min(w*0.1,9)},${Math.min(h*0.1,9)}
  36. L ${Math.min(w*0.1,9)},${h*0.5-Math.min(h*0.1,9)}
  37. Q ${Math.min(w*0.1,9)},${h/2} ${strokeWidth},${h/2}
  38. Q ${Math.min(w*0.1,9)},${h/2} ${Math.min(w*0.1,9)},${h*0.5+Math.min(h*0.1,9)}
  39. L ${Math.min(w*0.1,9)},${h-Math.min(h*0.1,9)}
  40. Q ${Math.min(w*0.1,9)},${h} ${Math.min(w*0.2,18)},${h - strokeWidth}
  41. M ${w-Math.min(w*0.2,18)},${strokeWidth}
  42. Q ${w-Math.min(w*0.1,9)},${strokeWidth} ${w-Math.min(w*0.1,9)},${Math.min(h*0.1,9)}
  43. L ${w-Math.min(w*0.1,9)},${h*0.5-Math.min(h*0.1,9)}
  44. Q ${w-Math.min(w*0.1,9)},${h/2} ${w-strokeWidth},${h/2}
  45. Q ${w-Math.min(w*0.1,9)},${h/2} ${w-Math.min(w*0.1,9)},${h*0.5+Math.min(h*0.1,9)}
  46. L ${w-Math.min(w*0.1,9)},${h-Math.min(h*0.1,9)}
  47. Q ${w-Math.min(w*0.1,9)},${h} ${w-Math.min(w*0.2,18)},${h - strokeWidth}
  48. `}
  49. fill={fillContent}
  50. stroke={strokeColor}
  51. strokeDasharray={strokeDasharray}
  52. strokeWidth={strokeWidth}
  53. />
  54. </svg>
  55. </div>
  56. </>
  57. );
  58. };
  59. register({
  60. shape: "custom-react-braces",
  61. width: 200,
  62. height: 140,
  63. effect: ["data"],
  64. component: component,
  65. });
  66. const braces: CompoundedComponent = {
  67. name: "大括号",
  68. icon: require("./image/braces.png"),
  69. node: {
  70. shape: "custom-react-braces",
  71. data: {
  72. label: "",
  73. ...defaultData,
  74. },
  75. ports,
  76. },
  77. };
  78. export default braces;