refactor(elements): remove getKey method on base-node (#5811)

This commit is contained in:
Aaron 2024-06-04 13:56:03 +08:00 committed by GitHub
parent eca59ed3ea
commit 8934714cce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 20 additions and 34 deletions

View File

@ -77,7 +77,7 @@ export function createGraphCanvas(
*/
class CenterConnectCircle extends Circle {
public getIntersectPoint(): Point {
const bounds = this.getKey().getBounds();
const bounds = this.getShape('key').getBounds();
return bounds.center;
}
}

View File

@ -151,7 +151,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
this.getGraphicStyle(attributes),
'collapsedMarker',
);
const keyShape = this.getKey();
const keyShape = this.getShape('key');
const [x, y] = getXYByPlacement(keyShape.getLocalBounds(), 'center');
const style = { ...collapsedMarkerStyle, x, y };

View File

@ -55,7 +55,7 @@ export class CircleCombo extends BaseCombo<CircleComboStyleProps> {
}
public getIntersectPoint(point: Point): Point {
const keyShapeBounds = this.getKey().getBounds();
const keyShapeBounds = this.getShape('key').getBounds();
return getEllipseIntersectPoint(point, keyShapeBounds);
}
}

View File

@ -239,7 +239,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
this.getGraphicStyle(attributes),
'label',
);
const keyShape = this.getKey();
const keyShape = this.getShape('key');
const keyBounds = keyShape.getLocalBounds();
return Object.assign(
@ -262,7 +262,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
if (attributes.icon === false || (!attributes.iconText && !attributes.iconSrc)) return false;
const iconStyle = subStyleProps(this.getGraphicStyle(attributes), 'icon');
const keyShape = this.getKey();
const keyShape = this.getShape('key');
const [x, y] = getXYByPlacement(keyShape.getLocalBounds(), 'center');
return { x, y, ...iconStyle };
@ -294,7 +294,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
}
protected getBadgeStyle(style: NodeBadgeStyleProps): NodeBadgeStyleProps {
const keyShape = this.getKey();
const keyShape = this.getShape('key');
const { placement = 'top', offsetX, offsetY, ...restStyle } = style;
const textStyle = getTextStyleByPlacement(keyShape.getLocalBounds(), placement, offsetX, offsetY, true);
return { ...textStyle, ...restStyle };
@ -327,18 +327,10 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
protected getPortXY(attributes: Required<S>, style: NodePortStyleProps): Point {
const { placement = 'left' } = style;
const bounds = this.getKey().getLocalBounds();
const bounds = this.getShape('key').getLocalBounds();
return getPortXYByPlacement(bounds, placement as PortPlacement);
}
/**
* Get the key shape for the node.
* @returns Key shape.
*/
public getKey<T extends DisplayObject>(): T {
return this.shapeMap.key as T;
}
/**
* Get the ports for the node.
* @returns Ports shape map.
@ -352,7 +344,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
* @returns The center point of the node.
*/
public getCenter(): Point {
return this.getKey().getBounds().center;
return this.getShape('key').getBounds().center;
}
/**
@ -361,12 +353,12 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
* @returns The intersection point.
*/
public getIntersectPoint(point: Point): Point {
const keyShapeBounds = this.getKey().getBounds();
const keyShapeBounds = this.getShape('key').getBounds();
return getRectIntersectPoint(point, keyShapeBounds);
}
protected drawHaloShape(attributes: Required<S>, container: Group): void {
const keyShape = this.getKey();
const keyShape = this.getShape('key');
this.upsert(
'halo',
keyShape.constructor as new (...args: unknown[]) => DisplayObject,

View File

@ -46,7 +46,7 @@ export class Circle extends BaseNode {
}
public getIntersectPoint(point: Point): Point {
const keyShapeBounds = this.getKey().getBounds();
const keyShapeBounds = this.getShape('key').getBounds();
return getEllipseIntersectPoint(point, keyShapeBounds);
}
}

View File

@ -52,7 +52,7 @@ export class Ellipse extends BaseNode {
}
public getIntersectPoint(point: Point): Point {
const keyShapeBounds = this.getKey().getBounds();
const keyShapeBounds = this.getShape('key').getBounds();
return getEllipseIntersectPoint(point, keyShapeBounds);
}
}

View File

@ -65,7 +65,7 @@ export class HTML extends BaseNode<HTMLStyleProps> {
}
protected getDomElement() {
return this.getKey<GHTML>().getDomElement();
return this.getShape<GHTML>('key').getDomElement();
}
protected getKeyStyle(attributes: Required<HTMLStyleProps>): GHTMLStyleProps {

View File

@ -49,7 +49,7 @@ export class Star extends Polygon<StarStyleProps> {
protected getPortXY(attributes: Required<StarStyleProps>, style: NodePortStyleProps): Point {
const { placement = 'top' } = style;
const bbox = this.getKey().getLocalBounds();
const bbox = this.getShape('key').getLocalBounds();
const ports = getStarPorts(this.getOuterR(attributes), this.getInnerR(attributes));
return getPortXYByPlacement(bbox, placement as StarPortPlacement, ports, false);
}

View File

@ -49,7 +49,7 @@ export class Triangle extends Polygon<TriangleStyleProps> {
protected getPortXY(attributes: Required<TriangleStyleProps>, style: NodePortStyleProps): Point {
const { direction } = attributes;
const { placement = 'top' } = style;
const bbox = this.getKey().getLocalBounds();
const bbox = this.getShape('key').getLocalBounds();
const [width, height] = this.getSize(attributes);
const ports = getTrianglePorts(width, height, direction);
return getPortXYByPlacement(bbox, placement as TrianglePortPlacement, ports, false);
@ -63,7 +63,7 @@ export class Triangle extends Polygon<TriangleStyleProps> {
if (icon === false || isEmpty(iconText || iconSrc)) return false;
const iconStyle = subStyleProps<IconStyleProps>(this.getGraphicStyle(attributes), 'icon');
const bbox = this.getKey().getLocalBounds();
const bbox = this.getShape('key').getLocalBounds();
const [x, y] = getTriangleCenter(bbox, direction);
const size = getIncircleRadius(bbox, direction) * 2 * ICON_SIZE_RATIO;

View File

@ -183,8 +183,8 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
return createAnimationsProxy(animationMap);
}
public getShape(key: string): DisplayObject | undefined {
return this.shapeMap[key];
public getShape<T extends DisplayObject>(name: string): T {
return this.shapeMap[name] as T;
}
private setVisibility() {

View File

@ -45,7 +45,7 @@ export function getBBoxSize(bbox: AABB): [number, number] {
* @returns <zh/> | <en/> bounding box
*/
export function getNodeBBox(node: Point | Node, padding?: Padding): AABB {
const bbox = isPoint(node) ? getPointBBox(node) : node.getKey().getBounds();
const bbox = isPoint(node) ? getPointBBox(node) : node.getShape('key').getBounds();
return padding ? getExpandedBBox(bbox, padding) : bbox;
}

View File

@ -289,8 +289,6 @@ export const getRadians = (bbox: AABB): Record<LoopPlacement, [number, number]>
* @param clockwise - <zh/> | <en/> Whether to draw the loop clockwise
* @param sourcePort - <zh/> | <en/> Source port
* @param targetPort - <zh/> | <en/> Target port
* @param rawSourcePoint - <zh/> | <en/> Source point
* @param rawTargetPoint - <zh/> | <en/> Target point
* @returns <zh/> | <en/> Start and end points
*/
export function getLoopEndpoints(
@ -336,8 +334,6 @@ export function getLoopEndpoints(
* @param dist - <zh/> keyShape | <en/> The distance from the edge of the node keyShape to the top of the self-loop
* @param sourcePortKey - <zh/> key | <en/> Source port key
* @param targetPortKey - <zh/> key | <en/> Target port key
* @param rawSourcePoint - <zh/> | <en/> Source point
* @param rawTargetPoint - <zh/> | <en/> Target point
* @returns <zh/> | <en/> Returns the path of the loop edge
*/
export function getCubicLoopPath(
@ -409,8 +405,6 @@ export function getCubicLoopControlPoints(
* @param dist - <zh/> keyShape | <en/> The distance from the edge of the node keyShape to the top of the self-loop
* @param sourcePortKey - <zh/> key | <en/> Source port key
* @param targetPortKey - <zh/> key | <en/> Target port key
* @param rawSourcePoint - <zh/> | <en/> Source point
* @param rawTargetPoint - <zh/> | <en/> Target point
* @returns <zh/> 线 | <en/> Returns the path of the loop polyline edge
*/
export function getPolylineLoopPath(

View File

@ -122,7 +122,7 @@ export function getAllPorts(node: Node): Record<string, Port> {
portsStyle.forEach((portStyle: NodePortStyleProps, i: number) => {
const { key, placement } = portStyle;
if (isSimplePort(portStyle)) {
ports[key || i] ||= getXYByPlacement(node.getKey().getBounds(), placement);
ports[key || i] ||= getXYByPlacement(node.getShape('key').getBounds(), placement);
}
});
return ports;