import { IGroup , IShape } from '@antv/g-base' ; import { upperFirst } from '@antv/util' ; import { ShapeOptions , ShapeDefine } from '../interface/shape' ; import { IPoint , Item , ModelConfig , NodeConfig , EdgeConfig , UpdateType } from '../types' ; import { createNodeFromXML } from './xml' ; const cache = { } ; function ucfirst ( str ) { if ( ! cache [ str ] ) { cache [ str ] = upperFirst ( str ) ; } return cache [ str ] ; } type Shape . FactoryBase export const ShapeFactoryBase = { type { String } defaultShapeType : 'defaultType' , type { String } className : null , param { String } type return { Shape } getShape ( type ? ) { const self = this as any ; const shape = self [ type ! ] || self [ self . defaultShapeType ] || self [ 'simple-circle' ] ; return shape ; } , param { String } type param { Object } cfg param { G . Group } group return { IShape } draw ( type , cfg , group ) { const shape = this . getShape ( type ) ; group [ 'shapeMap' ] = { } ; const rst = shape . draw ! ( cfg , group ) ; if ( shape . afterDraw ) { shape . afterDraw ( cfg , group , rst ) ; } return rst ; } , param { String } type param { Object } cfg param { G6 . Item } item baseUpdate ( type , cfg , item , updateType ? ) { const shape = this . getShape ( type ) ; if ( shape . update ) { shape . mergeStyle = shape . getOptions ?. ( cfg , updateType ) ; shape . update ?. ( cfg , item , updateType ) ; } if ( shape . afterUpdate ) { shape . afterUpdate ( cfg , item ) ; } } , param { String } type param { String } name param { String | Boolean } value param { G6 . Item } item setState ( type , name , value , item ) { const shape = this . getShape ( type ) ; shape . setState ! ( name , value , item ) ; } , param { String } type return { Boolean } shouldUpdate ( type ) { const shape = this . getShape ( type ) ; return ! ! shape . update ; } , getControlPoints ( type , cfg ) { const shape = this . getShape ( type ) ; return shape . getControlPoints ! ( cfg ) ; } , param { String } type param { Object } cfg return { Array | null } getAnchorPoints ( type , cfg ) { const shape = this . getShape ( type ) ; return shape . getAnchorPoints ! ( cfg ) ; } , } ; /**
 * 元素的框架
 */ const ShapeFramework = { options : { } , /**
     * 绘制
     */ draw ( cfg , group ) { return this . drawShape ( cfg , group ) ; } , /**
     * 绘制
     */ drawShape ( ) { } , /**
     * 绘制完成后的操作，便于用户继承现有的节点、边
     */ afterDraw ( ) { } , afterUpdate ( ) { } , /**
     * 设置节点、边状态
     */ setState ( ) { } , param { Object } cfg return { Array | null } getControlPoints ( cfg ) { return cfg . controlPoints ; } , param { Object } cfg return { Array | null } getAnchorPoints ( cfg ) { const { anchorPoints : defaultAnchorPoints } = this . options as any ; const anchorPoints = cfg . anchorPoints || defaultAnchorPoints ; return anchorPoints ; } , } ; export default class Shape { public static Node : any ; public static Edge : any ; public static Combo : any ; public static registerFactory ( factoryType , cfg ) { const className = ucfirst ( factoryType ) ; const factoryBase = ShapeFactoryBase ; const shapeFactory = { ... factoryBase , ... cfg } as any ; ( Shape as any ) [ className ] = shapeFactory ; shapeFactory . className = className ; return shapeFactory ; } public static getFactory ( factoryType ) { const className = ucfirst ( factoryType ) ; return ( Shape as any ) [ className ] ; } public static registerNode ( shapeType , nodeDefinition , extendShapeType ? ) { const shapeFactory = Shape . Node ; let shapeObj ; if ( typeof nodeDefinition === 'string' || typeof nodeDefinition === 'function' ) { const autoNodeDefinition = createNodeFromXML ( nodeDefinition ) ; shapeObj = { ... shapeFactory . getShape ( 'single-node' ) , ... autoNodeDefinition } ; } else if ( nodeDefinition . jsx ) { const { jsx } = nodeDefinition ; const autoNodeDefinition = createNodeFromXML ( jsx ) ; shapeObj = { ... shapeFactory . getShape ( 'single-node' ) , ... autoNodeDefinition , ... nodeDefinition , } ; } else { shapeFactory . getShape ( extendShapeType ) ; const extendShape = extendShapeType ? shapeFactory . getShape ( extendShapeType ) : ShapeFramework ; shapeObj = { ... extendShape , ... nodeDefinition } ; } shapeObj . type = shapeType ; shapeObj . itemType = 'node' ; shapeFactory [ shapeType ] = shapeObj ; return shapeObj ; } public static registerEdge ( shapeType , edgeDefinition , extendShapeType ? ) { const shapeFactory = Shape . Edge ; const extendShape = extendShapeType ? shapeFactory . getShape ( extendShapeType ) : ShapeFramework ; const shapeObj = { ... extendShape , ... edgeDefinition } ; shapeObj . type = shapeType ; shapeObj . itemType = 'edge' ; shapeFactory [ shapeType ] = shapeObj ; return shapeObj ; } public static registerCombo ( shapeType , comboDefinition , extendShapeType ? ) { const shapeFactory = Shape . Combo ; const extendShape = extendShapeType ? shapeFactory . getShape ( extendShapeType ) : ShapeFramework ; const shapeObj = { ... extendShape , ... comboDefinition } ; shapeObj . type = shapeType ; shapeObj . itemType = 'combo' ; shapeFactory [ shapeType ] = shapeObj ; return shapeObj ; } } Shape . registerFactory ( 'node' , { defaultShapeType : 'circle' , } ) ; Shape . registerFactory ( 'edge' , { defaultShapeType : 'line' , } ) ; Shape . registerFactory ( 'combo' , { defaultShapeType : 'circle' , } ) ;