From d2d2bc65074c515afabc743f5ceff060d7cb4ba2 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 26 Feb 2024 16:56:43 +0800 Subject: [PATCH] refactor(elements): optimize elements types (#5472) --- packages/g6/src/elements/edges/base-edge.ts | 36 ++++---- packages/g6/src/elements/nodes/base-node.ts | 88 ++++++++----------- packages/g6/src/elements/shapes/base-shape.ts | 14 +-- packages/g6/src/runtime/element.ts | 2 +- 4 files changed, 61 insertions(+), 79 deletions(-) diff --git a/packages/g6/src/elements/edges/base-edge.ts b/packages/g6/src/elements/edges/base-edge.ts index 71ea64fd01d..62ba22f4819 100644 --- a/packages/g6/src/elements/edges/base-edge.ts +++ b/packages/g6/src/elements/edges/base-edge.ts @@ -29,23 +29,17 @@ import type { LabelStyleProps } from '../shapes'; import { Label } from '../shapes'; import { BaseShape } from '../shapes/base-shape'; -export type BaseEdgeStyleProps = BaseEdgeProps & +export type BaseEdgeStyleProps = BaseEdgeProps & ShapeSwitch & - Pick & PrefixObject & PrefixObject & PrefixObject & PrefixObject & PrefixObject; -type ParsedBaseEdgeStyleProps = Required< - BaseEdgeStyleProps ->; -type BaseEdgeKeyStyleProps = PathStyleProps; +type ParsedBaseEdgeStyleProps = Required; -export abstract class BaseEdge extends BaseShape< - BaseEdgeStyleProps -> { +export abstract class BaseEdge extends BaseShape { static defaultStyleProps: Partial = { isBillboard: true, label: true, @@ -85,11 +79,11 @@ export abstract class BaseEdge>) { + constructor(options: DisplayObjectConfig) { super(deepMix({}, { style: BaseEdge.defaultStyleProps }, options)); } - protected getKeyStyle(attributes: ParsedBaseEdgeStyleProps): PathStyleProps { + protected getKeyStyle(attributes: ParsedBaseEdgeStyleProps): PathStyleProps { const { sourceNode, targetNode, color, stroke, ...style } = this.getGraphicStyle(attributes); const path = isSameNode(sourceNode, targetNode) ? this.getLoopPath(attributes) : this.getKeyPath(attributes); @@ -100,9 +94,9 @@ export abstract class BaseEdge): PathArray; + protected abstract getKeyPath(attributes: ParsedBaseEdgeStyleProps): PathArray; - protected getLoopPath(attributes: ParsedBaseEdgeStyleProps): PathArray { + protected getLoopPath(attributes: ParsedBaseEdgeStyleProps): PathArray { const { sourceNode: node, sourcePort, targetPort } = attributes; const bbox = getNodeBBox(node); @@ -117,7 +111,7 @@ export abstract class BaseEdge): [Point, Point] { + protected getEndpoints(attributes: ParsedBaseEdgeStyleProps): [Point, Point] { const { sourceNode, targetNode, sourcePort: sourcePortKey, targetPort: targetPortKey } = attributes; const [sourcePort, targetPort] = findPorts(sourceNode, targetNode, sourcePortKey, targetPortKey); @@ -128,7 +122,7 @@ export abstract class BaseEdge): false | PathStyleProps { + protected getHaloStyle(attributes: ParsedBaseEdgeStyleProps): false | PathStyleProps { if (attributes.halo === false) return false; const keyStyle = this.getKeyStyle(attributes); @@ -137,7 +131,7 @@ export abstract class BaseEdge): false | LabelStyleProps { + protected getLabelStyle(attributes: ParsedBaseEdgeStyleProps): false | LabelStyleProps { if (attributes.label === false || isEmpty(attributes.labelText)) return false; const labelStyle = subStyleProps>(this.getGraphicStyle(attributes), 'label'); @@ -156,7 +150,7 @@ export abstract class BaseEdge, isStart: boolean) { + protected drawArrow(attributes: ParsedBaseEdgeStyleProps, isStart: boolean) { const arrowType = isStart ? 'startArrow' : 'endArrow'; const arrowPresence = attributes[arrowType]; @@ -172,7 +166,7 @@ export abstract class BaseEdge, isStart: boolean) { + private getArrowStyle(attributes: ParsedBaseEdgeStyleProps, isStart: boolean) { const keyStyle = this.getKeyStyle(attributes) as BaseStyleProps; const arrowType = isStart ? 'startArrow' : 'endArrow'; const { width, height, type, ctor, ...arrowStyle } = subStyleProps>( @@ -194,15 +188,15 @@ export abstract class BaseEdge, container: Group) { + protected drawLabelShape(attributes: ParsedBaseEdgeStyleProps, container: Group) { this.upsert('label', Label, this.getLabelStyle(attributes), container); } - protected drawHaloShape(attributes: ParsedBaseEdgeStyleProps, container: Group) { + protected drawHaloShape(attributes: ParsedBaseEdgeStyleProps, container: Group) { this.upsert('halo', Path, this.getHaloStyle(attributes), container); } - protected drawKeyShape(attributes: ParsedBaseEdgeStyleProps, container: Group): Path | undefined { + protected drawKeyShape(attributes: ParsedBaseEdgeStyleProps, container: Group): Path | undefined { return this.upsert('key', Path, this.getKeyStyle(attributes), container); } diff --git a/packages/g6/src/elements/nodes/base-node.ts b/packages/g6/src/elements/nodes/base-node.ts index bbfed6c80d5..c18aa767559 100644 --- a/packages/g6/src/elements/nodes/base-node.ts +++ b/packages/g6/src/elements/nodes/base-node.ts @@ -22,24 +22,32 @@ import { getWordWrapWidthByBox } from '../../utils/text'; import type { BadgeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes'; import { Badge, BaseShape, Icon, Label } from '../shapes'; -export type BaseNodeStyleProps< - KeyStyleProps extends BaseStyleProps = BaseNodeProps, - StyleLifting extends keyof KeyStyleProps = never, -> = BaseNodeProps & - ShapeSwitch & - // 将 KeyStyleProps 的属性提升到 BaseNodeStyleProps - // Lift the properties of KeyStyleProps to BaseNodeStyleProps - Pick & - PrefixObject & +export type BaseNodeStyleProps = BaseNodeProps & + ShapeSwitch & { + /** + * 连接桩 + * Port + */ + ports?: NodePortStyleProps[]; + /** + * 徽标 + * Badge + */ + badges?: NodeBadgeStyleProps[]; + /** + * 背景色板 + * Background color palette + */ + badgePalette?: string[] | CategoricalPalette; + } & PrefixObject & PrefixObject & PrefixObject & - NodeBadgesStyleProps & - NodePortsStyleProps; + PrefixObject & + PrefixObject; -export type ParsedBaseNodeStyleProps< - KeyStyleProps extends BaseStyleProps, - StyleLifting extends keyof KeyStyleProps = never, -> = Required>; +export type ParsedBaseNodeStyleProps = Required< + BaseNodeStyleProps +>; /** * Design document: https://www.yuque.com/antv/g6/gl1iof1xpzg6ed98 @@ -47,11 +55,9 @@ export type ParsedBaseNodeStyleProps< * The P is the StyleProps of Key Shape. * The KeyShape is the type of the key shape. */ -export abstract class BaseNode< - KeyShape extends DisplayObject, - KeyStyleProps extends BaseStyleProps, - StyleLifting extends keyof KeyStyleProps = never, -> extends BaseShape> { +export abstract class BaseNode extends BaseShape< + BaseNodeStyleProps +> { static defaultStyleProps: BaseNodeStyleProps = { x: 0, y: 0, @@ -79,7 +85,7 @@ export abstract class BaseNode< labelZIndex: 0, }; - constructor(options: DisplayObjectConfig>) { + constructor(options: DisplayObjectConfig>) { super(deepMix({}, { style: BaseNode.defaultStyleProps }, options)); } @@ -88,7 +94,7 @@ export abstract class BaseNode< return parseSize(size); } - protected getKeyStyle(attributes: ParsedBaseNodeStyleProps): KeyStyleProps { + protected getKeyStyle(attributes: ParsedBaseNodeStyleProps): KeyStyleProps { const { color, fill, ...style } = this.getGraphicStyle(attributes); return Object.assign( @@ -97,7 +103,7 @@ export abstract class BaseNode< ) as unknown as KeyStyleProps; } - protected getLabelStyle(attributes: ParsedBaseNodeStyleProps): false | LabelStyleProps { + protected getLabelStyle(attributes: ParsedBaseNodeStyleProps): false | LabelStyleProps { if (attributes.label === false || isEmpty(attributes.labelText)) return false; const { position, maxWidth, ...labelStyle } = subStyleProps>( @@ -114,7 +120,7 @@ export abstract class BaseNode< ); } - protected getHaloStyle(attributes: ParsedBaseNodeStyleProps): false | KeyStyleProps { + protected getHaloStyle(attributes: ParsedBaseNodeStyleProps): false | KeyStyleProps { if (attributes.halo === false) return false; const { fill, ...keyStyle } = this.getKeyStyle(attributes); @@ -123,7 +129,7 @@ export abstract class BaseNode< return { ...keyStyle, stroke: fill, ...haloStyle }; } - protected getIconStyle(attributes: ParsedBaseNodeStyleProps): false | IconStyleProps { + protected getIconStyle(attributes: ParsedBaseNodeStyleProps): false | IconStyleProps { if (attributes.icon === false || isEmpty(attributes.iconText || attributes.iconSrc)) return false; const iconStyle = subStyleProps(this.getGraphicStyle(attributes), 'icon'); @@ -134,7 +140,7 @@ export abstract class BaseNode< } protected getBadgesStyle( - attributes: ParsedBaseNodeStyleProps, + attributes: ParsedBaseNodeStyleProps, ): Record { const badges = subObject(this.shapeMap, 'badge-'); const badgesShapeStyle: Record = {}; @@ -166,9 +172,7 @@ export abstract class BaseNode< return { ...textStyle, ...restStyle }; } - protected getPortsStyle( - attributes: ParsedBaseNodeStyleProps, - ): Record { + protected getPortsStyle(attributes: ParsedBaseNodeStyleProps): Record { const ports = this.getPorts(); const portsShapeStyle: Record = {}; @@ -188,10 +192,7 @@ export abstract class BaseNode< return portsShapeStyle; } - protected getPortXY( - attributes: ParsedBaseNodeStyleProps, - style: NodePortStyleProps, - ): Point { + protected getPortXY(attributes: ParsedBaseNodeStyleProps, style: NodePortStyleProps): Point { const { position = 'left' } = style; const bounds = this.getKey().getLocalBounds(); return getPortPosition(bounds, position as PortPosition); @@ -231,7 +232,7 @@ export abstract class BaseNode< return getRectIntersectPoint(point, keyShapeBounds); } - protected drawHaloShape(attributes: ParsedBaseNodeStyleProps, container: Group): void { + protected drawHaloShape(attributes: ParsedBaseNodeStyleProps, container: Group): void { const keyShape = this.getKey(); this.upsert( 'halo', @@ -241,14 +242,14 @@ export abstract class BaseNode< ); } - protected drawBadgeShapes(attributes: ParsedBaseNodeStyleProps, container: Group): void { + protected drawBadgeShapes(attributes: ParsedBaseNodeStyleProps, container: Group): void { const badgesStyle = this.getBadgesStyle(attributes); Object.keys(badgesStyle).forEach((key) => { this.upsert(`badge-${key}`, Badge, badgesStyle[key], container); }); } - protected drawPortShapes(attributes: ParsedBaseNodeStyleProps, container: Group): void { + protected drawPortShapes(attributes: ParsedBaseNodeStyleProps, container: Group): void { const portsStyle = this.getPortsStyle(attributes); Object.keys(portsStyle).forEach((key) => { this.upsert(`port-${key}`, GCircle, portsStyle[key], container); @@ -256,7 +257,7 @@ export abstract class BaseNode< } protected abstract drawKeyShape( - attributes: ParsedBaseNodeStyleProps, + attributes: ParsedBaseNodeStyleProps, container: Group, ): KeyShape | undefined; @@ -314,15 +315,6 @@ type NodeBadgeStyleProps = BadgeStyleProps & { position?: BadgePosition; }; -type NodeBadgesStyleProps = { - badges?: NodeBadgeStyleProps[]; - /** - * 背景色板 - * Background color palette - */ - badgePalette?: string[] | CategoricalPalette; -} & PrefixObject; - export type NodePortStyleProps = Partial & { /** * The key of the port. Default is the index of the port. @@ -336,10 +328,6 @@ export type NodePortStyleProps = Partial & { position: string | [number, number]; }; -type NodePortsStyleProps = { - ports?: NodePortStyleProps[]; -} & PrefixObject; - type ShapeSwitch = { label?: boolean; halo?: boolean; diff --git a/packages/g6/src/elements/shapes/base-shape.ts b/packages/g6/src/elements/shapes/base-shape.ts index 32da0db5a2e..f19e70d81f8 100644 --- a/packages/g6/src/elements/shapes/base-shape.ts +++ b/packages/g6/src/elements/shapes/base-shape.ts @@ -10,16 +10,16 @@ export interface BaseShapeStyleProps extends BaseStyleProps { y?: number | string; } -export abstract class BaseShape extends CustomElement { - constructor(options: DisplayObjectConfig) { +export abstract class BaseShape extends CustomElement { + constructor(options: DisplayObjectConfig) { super(options); - this.render(this.attributes as Required, this); + this.render(this.attributes as Required, this); this.bindEvents(); } get parsedAttributes() { - return this.attributes as Required; + return this.attributes as Required; } /** @@ -94,9 +94,9 @@ export abstract class BaseShape extends CustomEle return createAnimationsProxy(result, Object.values(this.animateMap)); } - public update(attr: Partial = {}): void { + public update(attr: Partial = {}): void { this.attr(deepMix({}, this.attributes, attr)); - return this.render(this.attributes as Required, this); + return this.render(this.attributes as Required, this); } /** @@ -106,7 +106,7 @@ export abstract class BaseShape extends CustomEle * @param attributes * @param container */ - public abstract render(attributes: Required, container: Group): void; + public abstract render(attributes: Required, container: Group): void; public bindEvents() {} diff --git a/packages/g6/src/runtime/element.ts b/packages/g6/src/runtime/element.ts index 002c4975d75..9fe4f8be670 100644 --- a/packages/g6/src/runtime/element.ts +++ b/packages/g6/src/runtime/element.ts @@ -321,7 +321,7 @@ export class ElementController { } public getEdges() { - return this.container.edge.children as BaseEdge[]; + return this.container.edge.children as BaseEdge[]; } public getCombos() {