import type { DisplayObjectConfig, RectStyleProps as GRectStyleProps, Group } from '@antv/g';
import { Rect as GRect } from '@antv/g';
import { ICON_SIZE_RATIO } from '../../constants/element';
import type { IconStyleProps } from '../shapes';
import type { BaseNodeStyleProps } from './base-node';
import { BaseNode } from './base-node';
/**
* 矩形节点样式配置项
*
* Rect node style props
*/
export interface RectStyleProps extends BaseNodeStyleProps {}
type ParsedRectStyleProps = Required;
/**
* 矩形节点
*
* Rect node
*/
export class Rect extends BaseNode {
constructor(options: DisplayObjectConfig) {
super(options);
}
protected getKeyStyle(attributes: ParsedRectStyleProps): GRectStyleProps {
const [width, height] = this.getSize(attributes);
return {
...super.getKeyStyle(attributes),
width,
height,
x: -width / 2,
y: -height / 2,
};
}
protected getIconStyle(attributes: ParsedRectStyleProps): false | IconStyleProps {
const style = super.getIconStyle(attributes);
const { width, height } = this.getShape('key').attributes;
return style
? ({
width: (width as number) * ICON_SIZE_RATIO,
height: (height as number) * ICON_SIZE_RATIO,
...style,
} as IconStyleProps)
: false;
}
protected drawKeyShape(attributes: ParsedRectStyleProps, container: Group): GRect | undefined {
return this.upsert('key', GRect, this.getKeyStyle(attributes), container);
}
}