import { Graph } from '@antv/g6' ; import { NodeData , Node , Data } from '../types' ; interface Property { value ? : string ; } const search = ( graph ) => ( words = '' ) => { const wordsArray = words . trim ( ) . split ( ' ' ) ; const { nodes } = ( graph . save ( ) as any ) as Data ; const matchWord = ( node , word ) => { const { id , label = '' , properties } = node ; if ( word && String ( id ) . indexOf ( word ) !== - 1 ) { return { isMatch : true , searchType : 'id' } ; } if ( word && String ( label ) . indexOf ( word ) !== - 1 ) { return { isMatch : true , searchType : 'label' } ; } let matchProperty = [ ] ; if ( properties ) { matchProperty = properties . filter ( ( property ) => { return property . value && property . value === word ; } ) ; } if ( matchProperty . length > 0 ) { return { isMatch : true , searchType : 'property' } ; } return { isMatch : false , searchType : '' } ; } ; const result = [ ] ; nodes . forEach ( ( node ) => { const matchResult = wordsArray . map ( ( word ) => { return matchWord ( node . data , word ) ; } ) ; const searchTypes = matchResult . reduce < string [ ] > ( ( acc , curr ) => { const { searchType } = curr ; if ( searchType && acc . indexOf ( searchType ) === - 1 ) { acc . push ( searchType ) ; } return acc ; } , [ ] ) ; if ( searchTypes . length > 0 ) { result . push ( { ... node , searchTypes , } ) ; } } ) ; return result ; } ; export default search ;