123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import { CompoundedComponent } from "@/types";
- import { register } from "@antv/x6-react-shape";
- import { Graph, Node } from "@antv/x6";
- import { ports, defaultData } from "../../config/data";
- import CustomInput from "../CustomInput";
- import { useSizeHook, useShapeProps } from "@/hooks";
- const component = ({ node }: { node: Node }) => {
- const { label, text, fill, stroke, opacity } = node.getData();
- const { size, ref } = useSizeHook();
- const {
- fillContent,
- defsContent,
- strokeColor,
- strokeWidth,
- strokeDasharray,
- } = useShapeProps(fill, size, stroke);
- const generatePath = (width: number, height: number, strokeWidth: number) => {
- let path = "";
- // 开始位置
- path += `M${width / 2} ${height - strokeWidth}`;
- path += `L${width - strokeWidth} ${height * 0.3}`;
- path += `C${width - strokeWidth},${height * 0.3} ${(width * 3) / 4},${strokeWidth} ${width / 2},${strokeWidth}`;
- path += `S${strokeWidth},${height * 0.3} ${strokeWidth},${height * 0.3}`;
- path += `L${width / 2} ${height - strokeWidth} Z`;
- return path;
- };
- return (
- <>
- <div
- className="relative text-0 w-full h-full"
- ref={ref}
- style={{ opacity: opacity / 100 }}
- >
- <CustomInput value={label} styles={text} node={node} />
- <svg
- className="w-full h-full"
- viewBox={`0 0 ${size?.width} ${size?.height}`}
- xmlns="http://www.w3.org/2000/svg"
- >
- <defs>{defsContent}</defs>
- <path
- d={generatePath(size?.width, size?.height, strokeWidth)}
- fill={fillContent}
- stroke={strokeColor}
- strokeDasharray={strokeDasharray}
- strokeWidth={strokeWidth}
- />
- </svg>
- </div>
- </>
- );
- };
- register({
- shape: "custom-react-sector",
- width: 80,
- height: 70,
- effect: ["data"],
- component: component,
- });
- // 左侧连接桩
- Graph.registerPortLayout("sectorLeft", (portsPositionArgs, elemBBox) => {
- return portsPositionArgs.map((item) => {
- return {
- ...item,
- position: {
- x: elemBBox.width / 4,
- y: elemBBox.height / 2,
- },
- };
- });
- });
- // 右侧连接桩
- Graph.registerPortLayout("sectorRight", (portsPositionArgs, elemBBox) => {
- return portsPositionArgs.map((item) => {
- return {
- ...item,
- position: {
- x: elemBBox.width * (3 / 4),
- y: elemBBox.height / 2,
- },
- };
- });
- });
- const Sector: CompoundedComponent = {
- name: "扇形",
- icon: require("./image/sector.png"),
- node: {
- shape: "custom-react-sector",
- data: {
- label: "",
- ...defaultData,
- },
- ports: {
- ...ports,
- groups: {
- ...ports.groups,
- sectorLeft: {
- position: "sectorLeft",
- attrs: {
- circle: {
- r: 4,
- magnet: true,
- stroke: "#5F95FF",
- strokeWidth: 1,
- fill: "#fff",
- style: {
- visibility: "hidden",
- },
- },
- },
- },
- sectorRight: {
- position: "sectorRight",
- attrs: {
- circle: {
- r: 4,
- magnet: true,
- stroke: "#5F95FF",
- strokeWidth: 1,
- fill: "#fff",
- style: {
- visibility: "hidden",
- },
- },
- },
- },
- },
- items: [
- {
- group: "top",
- args: {
- dy: 2,
- },
- },
- {
- group: "sectorRight",
- },
- {
- group: "bottom",
- args: {
- dy: -2,
- },
- },
- {
- group: "sectorLeft",
- },
- ],
- },
- },
- };
- export default Sector;
|