refactor(elements): adjust elements type define

This commit is contained in:
Aaron 2024-06-13 10:57:05 +08:00
parent 6d4fd9f9b3
commit ea5a4605be
7 changed files with 135 additions and 44 deletions

View File

@ -4,7 +4,7 @@ import type { BaseShapeStyleProps } from './shapes';
import { BaseShape } from './shapes';
export abstract class BaseElement<T extends BaseShapeStyleProps> extends BaseShape<T> {
public get parsedAttributes() {
protected get parsedAttributes() {
return this.attributes as Required<T>;
}
@ -25,28 +25,4 @@ export abstract class BaseElement<T extends BaseShapeStyleProps> extends BaseSha
return animation;
}
/**
* <zh/>
*
* <en/> Called after the element is created and the entrance animation is completed
* @override
*/
public onCreate() {}
/**
* <zh/>
*
* <en/> Called after the element is updated and the transition animation is completed
* @override
*/
public onUpdate() {}
/**
* <zh/> 退
*
* <en/> Called after the element completes the exit animation and is destroyed
* @override
*/
public onDestroy() {}
}

View File

@ -1,7 +1,17 @@
import { AABB, BaseStyleProps, DisplayObject, DisplayObjectConfig, Group } from '@antv/g';
import { isFunction } from '@antv/util';
import { COMBO_KEY } from '../../constants';
import type { CollapsedMarkerStyleProps, ID, NodeLikeData, Padding, Point, Prefix, STDSize, Size } from '../../types';
import type {
CollapsedMarkerStyleProps,
Combo,
ID,
NodeLikeData,
Padding,
Point,
Prefix,
STDSize,
Size,
} from '../../types';
import { getBBoxHeight, getBBoxWidth, getCombinedBBox, getExpandedBBox } from '../../utils/bbox';
import { idOf } from '../../utils/id';
import { parsePadding } from '../../utils/padding';
@ -74,7 +84,10 @@ export interface BaseComboStyleProps
*
* <en/> When customizing a combo, it is recommended to use this class as the base class. In this way, users only need to focus on the logic of drawing keyShape
*/
export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStyleProps> extends BaseNode<S> {
export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStyleProps>
extends BaseNode<S>
implements Combo
{
public type = 'combo';
static defaultStyleProps: Partial<BaseComboStyleProps> = {

View File

@ -4,6 +4,7 @@ import type { PathArray } from '@antv/util';
import { isFunction, pick } from '@antv/util';
import type {
BaseElementStyleProps,
Edge,
EdgeArrowStyleProps,
EdgeBadgeStyleProps,
EdgeKey,
@ -170,7 +171,7 @@ type ParsedBaseEdgeStyleProps = Required<BaseEdgeStyleProps>;
*
* <en/> Base class of the edge
*/
export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> {
export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> implements Edge {
public type = 'edge';
static defaultStyleProps: Partial<BaseEdgeStyleProps> = {

View File

@ -5,6 +5,7 @@ import type { NodeData } from '../../spec';
import type {
BaseElementStyleProps,
ID,
Node,
NodeBadgeStyleProps,
NodeLabelStyleProps,
NodePortStyleProps,
@ -185,7 +186,10 @@ export interface BaseNodeStyleProps
*
* <en/> Design document: https://www.yuque.com/antv/g6/gl1iof1xpzg6ed98
*/
export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps> extends BaseElement<S> {
export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps>
extends BaseElement<S>
implements Node
{
public type = 'node';
static defaultStyleProps: Partial<BaseNodeStyleProps> = {

View File

@ -201,6 +201,8 @@ export type {
EdgeLabelStyleProps,
Element,
ElementDatum,
ElementHooks,
ElementMethods,
ElementType,
FitViewOptions,
IAnimateEvent,

View File

@ -389,7 +389,7 @@ export class ElementController {
{
after: () => {
this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_CREATE, elementType, datum), context);
element.onCreate();
element.onCreate?.();
},
},
);
@ -457,7 +457,7 @@ export class ElementController {
if (stage === 'collapse') updateStyle(element, style);
if (exactStage === 'hide') updateStyle(element, { visibility: getCachedStyle(element, 'visibility') });
this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_UPDATE, elementType, datum), context);
element.onUpdate();
element.onUpdate?.();
},
},
);
@ -496,7 +496,7 @@ export class ElementController {
after: () => {
this.clearElement(id);
element.destroy();
element.onDestroy();
element.onDestroy?.();
this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_DESTROY, elementType, datum), context);
},
},

View File

@ -1,25 +1,120 @@
import type { BaseStyleProps } from '@antv/g';
import type { BaseCombo } from '../elements/combos';
import type { BaseEdge } from '../elements/edges';
import type { BaseNode } from '../elements/nodes';
import type { BaseStyleProps, DisplayObject } from '@antv/g';
import type { RuntimeContext } from '../runtime/types';
import type { ComboOptions, EdgeOptions, NodeOptions } from '../spec';
import type { Point, Port } from '../types';
/**
* <zh/>
*
* <en/> Node type
*/
export interface Node extends DisplayObject, ElementHooks, ElementMethods {
/**
* <zh/>
*
* <en/> Get the ports
*/
getPorts(): Record<string, Port>;
/**
* <zh/>
*
* <en/> Get the center position of the node
*/
getCenter(): Point;
/**
* <zh/>
*
* <en/> Get the intersection point
* @param point - <zh/> | <en/> external position
* @returns <zh/> | <en/> intersection point
* @description
* <zh/>
*
* <en/> Given an external position, return the intersection point of the edge between the current node and the position and the node
*/
getIntersectPoint(point: Point): Point;
}
/**
* <zh/>
*
* <en/> Edge type
*/
export interface Edge extends DisplayObject, ElementHooks, ElementMethods {}
/**
* <zh/>
*
* <en/> Combo type
*/
export interface Combo extends Node {
/**
* <zh/>
*
* <en/> Get the position of the combo
* @param attributes - <zh/> | <en/> combo attributes
*/
getComboPosition(attributes: Record<string, unknown>): Point;
}
export type Element = Node | Edge | Combo;
export type ElementType = 'node' | 'edge' | 'combo';
export type ElementOptions = NodeOptions | EdgeOptions | ComboOptions;
export type Node = BaseNode<any>;
export type Edge = BaseEdge;
export type Combo = BaseCombo<any>;
export type Element = Node | Edge | Combo;
export interface BaseElementStyleProps extends BaseStyleProps {
/**
* @internal
*/
context?: RuntimeContext;
}
/**
* <zh/>
*
* <en/> Element methods
*/
export interface ElementMethods {
/**
* <zh/>
*
* <en/> Get the subgraph in the current element
* @param shapeID - <zh/> ID | <en/> Subgraph ID
* @returns <zh/> | <en/> Subgraph
*/
getShape<T extends DisplayObject = DisplayObject>(shapeID: string): T;
}
/**
* <zh/>
*
* <en/> Element hooks
*/
export interface ElementHooks {
/**
* <zh/>
*
* <en/> Called after the element is created and the entrance animation is completed
* @override
*/
onCreate?: () => void;
/**
* <zh/>
*
* <en/> Called after the element is updated and the transition animation is completed
* @override
*/
onUpdate?: () => void;
/**
* <zh/> 退
*
* <en/> Called after the element completes the exit animation and is destroyed
* @override
*/
onDestroy?: () => void;
}