44 * Licensed under the MIT License. See License.txt in the project root for license information.
55 *--------------------------------------------------------------------------------------------*/
66
7+ import { v4 as uuidv4 } from 'uuid' ;
78import { configToStruct , toConfig } from '@/components/util/JadeConfigUtils.js' ;
89import { DATA_TYPES , FROM_TYPE } from '@/common/Consts.js' ;
910import { ShapeDataValidationProcessor } from '@/data/ShapeDataValidationProcessor.js' ;
@@ -75,10 +76,49 @@ const graphOperator = (graphString) => {
7576 * @param updates 待修改的值.
7677 */
7778 self . update = ( keys , updates ) => {
78- const config = getConfigByKeys ( keys ) ;
79- updateConfig ( config , updates ) ;
79+ if ( ! keys || keys . length === 0 ) {
80+ throw new Error ( 'Keys cannot be empty' ) ;
81+ }
82+ // 获取除最后一层外的所有路径
83+ const parentKeys = keys . slice ( 0 , - 1 ) ;
84+ const lastKey = keys [ keys . length - 1 ] ;
85+ // 检查父路径是否存在
86+ const parentConfig = getConfigByKeys ( parentKeys ) ;
87+ if ( ! parentConfig || ! parentConfig . value ) {
88+ throw new Error ( `Parent path does not exist: ${ parentKeys . join ( '.' ) } ` ) ;
89+ }
90+ // 检查最后一层是否存在
91+ let targetConfig = parentConfig . value . find ( v => v . name === lastKey ) ;
92+ // 如果最后一层不存在,则创建
93+ if ( ! targetConfig ) {
94+ targetConfig = {
95+ id : uuidv4 ( ) ,
96+ name : lastKey ,
97+ from : FROM_TYPE . INPUT ,
98+ type : getTypeFromUpdates ( updates ) ,
99+ value : updates
100+ } ;
101+ parentConfig . value . push ( targetConfig ) ;
102+ }
103+ updateConfig ( targetConfig , updates ) ;
80104 } ;
81105
106+ // 根据updates的类型返回对应的DATA_TYPES
107+ const getTypeFromUpdates = ( updates ) => {
108+ if ( Array . isArray ( updates ) ) {
109+ return DATA_TYPES . ARRAY ;
110+ } else if ( updates && typeof updates === 'object' ) {
111+ return DATA_TYPES . OBJECT ;
112+ } else if ( typeof updates === 'string' ) {
113+ return DATA_TYPES . STRING ;
114+ } else if ( typeof updates === 'number' ) {
115+ return DATA_TYPES . NUMBER ;
116+ } else if ( typeof updates === 'boolean' ) {
117+ return DATA_TYPES . BOOLEAN ;
118+ }
119+ return DATA_TYPES . STRING ; // 或者其他默认类型
120+ }
121+
82122 const updateConfig = ( config , updates ) => {
83123 if ( Array . isArray ( updates ) ) {
84124 config . value = updates . map ( update => {
@@ -97,19 +137,22 @@ const graphOperator = (graphString) => {
97137 const update = updates [ k ] ;
98138 const correspondingConfig = config . value ?. find ( v => v . name === k ) ;
99139 if ( ! correspondingConfig ) {
100- return ;
101- }
102-
103- // 处理对象
104- if ( typeof update === 'object' ) {
105- updateConfig ( correspondingConfig , update ) ;
140+ config . value = config . value || [ ] ; // 确保 config.value 是数组
141+ config . value . push ( {
142+ id : uuidv4 ( ) ,
143+ name : k ,
144+ from : FROM_TYPE . INPUT ,
145+ type : getTypeFromUpdates ( update ) ,
146+ value : update ,
147+ } ) ;
106148 return ;
107149 }
108150
109151 // 如果类型是reference,则不进行修改。
110- if ( correspondingConfig . from === FROM_TYPE . REFERENCE ) {
152+ if ( String ( correspondingConfig . from ?? "" ) . toLowerCase ( ) === FROM_TYPE . REFERENCE . toLowerCase ( ) ) {
111153 return ;
112154 }
155+
113156 updateConfig ( correspondingConfig , update ) ;
114157 }
115158 } ) ;
0 commit comments