@@ -2,7 +2,6 @@ import { useLocation } from 'react-router-dom';
22import { useCallback , useMemo , useState } from 'react' ;
33import { pick , find , filter } from 'lodash' ;
44import { Message } from '@/shared/utils/message' ;
5- import { createGraphOperator } from '@fit-elsa/elsa-react' ;
65import { storage } from '../storage' ;
76import i18n from '@/locale/i18n' ;
87import { getAppCategories } from "@/shared/http/aipp" ;
@@ -450,3 +449,126 @@ export const getAppConfig = (appInfo) => {
450449export const generateUniqueName = ( ) => {
451450 return 'guest-' + nanoid ( 16 ) ;
452451}
452+
453+ // ==================== GraphOperator ====================
454+
455+ // 数据类型常量
456+ const DATA_TYPES = {
457+ STRING : 'String' ,
458+ INTEGER : 'Integer' ,
459+ BOOLEAN : 'Boolean' ,
460+ NUMBER : 'Number' ,
461+ OBJECT : 'Object' ,
462+ ARRAY : 'Array' ,
463+ } ;
464+
465+ // 来源类型常量
466+ const FROM_TYPE = {
467+ EXPAND : 'Expand' ,
468+ INPUT : 'Input' ,
469+ REFERENCE : 'Reference' ,
470+ } ;
471+
472+ /**
473+ * 将配置数据转换为结构体
474+ * @param config 配置数据
475+ * @return {{}|* } 结构体
476+ */
477+ const configToStruct = ( config : any ) => {
478+ if ( config . type === DATA_TYPES . ARRAY ) {
479+ return config . value . map ( v => configToStruct ( v ) ) ;
480+ } else if ( config . type === DATA_TYPES . OBJECT ) {
481+ const obj = { } ;
482+ config . value . forEach ( ( item : any ) => {
483+ obj [ item . name ] = configToStruct ( item ) ;
484+ } ) ;
485+ return obj ;
486+ } else {
487+ return Object . prototype . hasOwnProperty . call ( config , 'value' ) ? config . value : config ;
488+ }
489+ } ;
490+
491+ /**
492+ * 创建画布操纵器
493+ * @param graphString 画布字符串
494+ * @return {{} } 画布操纵器对象
495+ */
496+ export const createGraphOperator = ( graphString : string ) => {
497+ const graph = JSON . parse ( graphString ) ;
498+ const shapes = graph . pages [ 0 ] . shapes ;
499+
500+ const getInputParams = ( shape : any ) => {
501+ if ( shape . type === 'startNodeStart' ) {
502+ return shape . flowMeta . inputParams ;
503+ } else if ( shape . type === 'endNodeEnd' ) {
504+ return shape . flowMeta . callback . converter . entity . inputParams ;
505+ } else {
506+ return shape . flowMeta . jober . converter . entity . inputParams ;
507+ }
508+ } ;
509+
510+ const getConfigByKeys = ( keys : string [ ] ) => {
511+ if ( ! Array . isArray ( keys ) ) {
512+ throw new Error ( 'Expected keys to be an array' ) ;
513+ }
514+
515+ if ( keys . length === 0 ) {
516+ return null ;
517+ }
518+
519+ const tmpKeys = [ ...keys ] ;
520+ const shapeId = tmpKeys . shift ( ) ;
521+ const shape = getShapeById ( shapeId ) ;
522+ const inputParams = getInputParams ( shape ) ;
523+ if ( ! inputParams ) {
524+ throw new Error ( 'Expected inputParams exists' ) ;
525+ }
526+ let config = { type : DATA_TYPES . OBJECT , value : inputParams } ;
527+ while ( tmpKeys . length > 0 && config && config . value ) {
528+ const key = tmpKeys . shift ( ) ;
529+ config = config . value . find ( v => v . name === key ) ;
530+ }
531+ return config ;
532+ } ;
533+
534+ const getShapeById = ( shapeId : string ) => {
535+ return shapes . find ( ( shape ) => shape . id === shapeId ) ;
536+ } ;
537+
538+ return {
539+ /**
540+ * 获取配置信息
541+ * @param keys 键值数组
542+ * @return {{}|*|null } 配置信息
543+ */
544+ getConfig : ( keys : string [ ] ) => {
545+ const config = getConfigByKeys ( keys ) ;
546+ return config ? configToStruct ( config ) : null ;
547+ } ,
548+
549+ /**
550+ * 获取画布中开始节点的入参信息
551+ * @return {array } 开始节点入参信息
552+ */
553+ getStartNodeInputParams : ( ) : any [ ] => {
554+ return shapes . filter ( shape => shape . type === 'startNodeStart' ) . map ( startNode => startNode . flowMeta . inputParams ) ;
555+ } ,
556+
557+ /**
558+ * 根据节点类型获取对应节点id列表
559+ * @param type 节点类型
560+ * @returns {array } 对应节点id列表
561+ */
562+ getShapeIdsByType : ( type : string ) : string [ ] => {
563+ return shapes . filter ( ( shape ) => shape . type === type ) . map ( ( shape ) => shape . id ) ;
564+ } ,
565+
566+ /**
567+ * 获取画布数据
568+ * @return {string } 画布数据
569+ */
570+ getGraph : ( ) : string => {
571+ return JSON . stringify ( graph ) ;
572+ }
573+ } ;
574+ } ;
0 commit comments