import G6 from '@antv/g6' ; import registerInnerMarker from '../icons/marker' ; import { GraphinProps } from '../types' ; import compiler from '../shape/render/compiler' ; import SimplicityNode from '../shape/render/SimplicityNode' ; import RegisterSimplicityLineEdge from '../shape/g6/LineEdge' ; import RegisterLineEdge from '../shape/graph-studio/LineEdge' ; import RegisterPolyEdge from '../shape/graph-studio/PolyEdge' ; import RegisterCurveEdge from '../shape/graph-studio/CurveEdge' ; import RegisterLoopEdge from '../shape/graph-studio/LoopEdge' ; import RegisterCircleNode from '../shape/graph-studio/CircleNode' ; import RegisterRectNode from '../shape/graph-studio/RectNode' ; import RegisterHexagonNode from '../shape/graph-studio/HexagonNode' ; import RegisterPointNode from '../shape/graph-studio/PointNode' ; import RegisterStubNode from '../shape/graph-studio/StubNode' ; import RegisterCustomNode from '../shape/graph-studio/CustomNode' ; import graphinHighlight from '../behaviors/graphin-highlight' ; import { registerFontFamily } from '../icons/iconFont' ; import { BehaviorModeItem } from './init' ; const defaultRegister = { nodeShape : ( ) => { return [ { name : 'CircleNode' , register : ( ) => { RegisterCircleNode ( G6 ) ; } , } , { name : 'CustomNode' , register : ( ) => { RegisterCustomNode ( G6 ) ; } , } , { name : 'RectNode' , register : ( ) => { RegisterRectNode ( G6 ) ; } , } , { name : 'HexagonNode' , register : ( ) => { RegisterHexagonNode ( G6 ) ; } , } , { name : 'RegisterPointNode' , register : ( ) => { RegisterStubNode ( G6 ) ; } , } , { name : 'StubNode' , register : ( ) => { RegisterPointNode ( G6 ) ; } , } , ] ; } , edgeShape : ( ) => { return [ { name : 'SimplicityLineEdge' , register : ( ) => { RegisterSimplicityLineEdge ( G6 ) ; } , } , { name : 'LineEdge' , register : ( ) => { RegisterLineEdge ( G6 ) ; } , } , { name : 'LoopEdge' , register : ( ) => { RegisterLoopEdge ( G6 ) ; } , } , { name : 'PolyEdge' , register : ( ) => { RegisterPolyEdge ( G6 ) ; } , } , { name : 'CurveEdge' , register : ( ) => { RegisterCurveEdge ( G6 ) ; } , } , ] ; } , behavior : ( ) => { return [ { name : 'graphin-highlight' , mode : 'default' , options : { } , register : ( ) => { G6 . registerBehavior ( 'graphin-highlight' , graphinHighlight as any ) ; } , } , ] ; } , } ; const defaultExtend = { nodeShape : ( ) => { return [ { name : 'SimplicityNode' , render : SimplicityNode , } , ] ; } , marker : ( ) => { return [ ] ; } , icon : ( ) => { return [ ] ; } , } ; const dummyRegister = ( ) => { return [ ] ; } ; const dummyExtend = ( ) => { return [ ] ; } ; const dummyIcon = ( ) => { return [ ] ; } ; const toUpperCaseWithFirst = ( str ) : string => { return `${ str [ 0 ] . toUpperCase ( ) }${ str . slice ( 1 ) }` ; } ; interface Mode { mode : string ; type : string ; [ key ] : string | number | boolean | undefined ; } interface BehaviorMode { default : BehaviorModeItem [ ] ; [ key ] : BehaviorModeItem [ ] ; } const graphinRegister = ( props ) => { const { extend = { } , register = { } , options = { } } = props ; const defaultBehaviors = defaultRegister . behavior ( ) . filter ( ( behavior ) => { const behaviorName = behavior . name . split ( '-' ) [ 1 ] ; const disableName = `disable${ toUpperCaseWithFirst ( behaviorName ) }` ; return ! options [ disableName ] ; } ) ; const { nodeShape = dummyRegister , edgeShape = dummyRegister , behavior = dummyRegister , } = register ; const registerNodes = [ ... defaultRegister . nodeShape ( ) , ... nodeShape ( G6 ) ] ; const registerEdges = [ ... defaultRegister . edgeShape ( ) , ... edgeShape ( G6 ) ] ; const registerBehaviors = [ ... defaultBehaviors , ... behavior ( G6 ) ] ; registerNodes . forEach ( ( item ) => { item . register ( G6 ) ; } ) ; registerEdges . forEach ( ( item ) => { item . register ( G6 ) ; } ) ; const modes = [ ] ; registerBehaviors . forEach ( ( item ) => { item . register ( G6 ) ; const { name , mode } = item ; modes . push ( { mode , type : name , ... item . options , } ) ; } ) ; const initialValue = { default : [ ] , } ; const behaviorMode = modes . reduce ( ( acc , curr ) => { const { mode , ... others } = curr ; if ( ! acc [ mode ] ) { acc [ mode ] = [ ] ; } acc [ mode ] . push ( others ) ; return { ... acc } ; } , initialValue ) ; const { icon = dummyIcon , nodeShape : ExNodeShape = dummyExtend , marker : ExMarker = dummyExtend , } = extend ; const extendNodes = [ ... defaultExtend . nodeShape ! ( ) , ... ExNodeShape ( ) ] ; const extendMarker = [ ... defaultExtend . marker ! ( ) , ... ExMarker ( ) ] ; extendNodes . forEach ( ( item ) => { compiler ( item ) ; } ) ; registerInnerMarker ( extendMarker ) ; registerFontFamily ( icon ( ) ) ; return behaviorMode ; } ; export default graphinRegister ;