@@ -13,6 +13,8 @@ import {
1313 deleteCompAction
1414} from "lowcoder-core" ;
1515import { getEditorComponentInfo } from "../utils" ;
16+ import { getPromiseAfterDispatch } from "@lowcoder-ee/util/promiseUtils" ;
17+ import { hookCompCategory , HookCompType } from "@lowcoder-ee/comps/hooks/hookCompTypes" ;
1618
1719export const addComponentAction : ActionConfig = {
1820 key : 'add-components' ,
@@ -21,14 +23,30 @@ export const addComponentAction: ActionConfig = {
2123 requiresComponentSelection : true ,
2224 requiresInput : false ,
2325 execute : async ( params : ActionExecuteParams ) => {
24- const { selectedComponent, editorState } = params ;
25-
26+ const { selectedComponent, editorState, actionPayload } = params ;
27+ const { component_name : name , layout, action_parameters } = actionPayload ;
28+
2629 if ( ! selectedComponent || ! editorState ) {
2730 message . error ( 'Component and editor state are required' ) ;
2831 return ;
2932 }
3033
3134 try {
35+ if ( hookCompCategory ( selectedComponent ) === "ui" ) {
36+ const compName = Boolean ( name ) ? name : editorState . getNameGenerator ( ) . genItemName ( selectedComponent ) ;
37+ editorState
38+ . getHooksComp ( )
39+ . dispatch (
40+ wrapActionExtraInfo (
41+ editorState
42+ . getHooksComp ( )
43+ . pushAction ( { name : compName , compType : selectedComponent as HookCompType } ) ,
44+ { compInfos : [ { compName : compName , compType : selectedComponent , type : "add" } ] }
45+ )
46+ ) ;
47+ return ;
48+ }
49+
3250 const uiComp = editorState . getUIComp ( ) ;
3351 const container = uiComp . getComp ( ) ;
3452
@@ -43,31 +61,33 @@ export const addComponentAction: ActionConfig = {
4361 return ;
4462 }
4563
64+ let compName = name ;
4665 const nameGenerator = editorState . getNameGenerator ( ) ;
4766 const compInfo = parseCompType ( selectedComponent ) ;
48- const compName = nameGenerator . genItemName ( compInfo . compName ) ;
67+ if ( ! compName ) {
68+ compName = nameGenerator . genItemName ( compInfo . compName ) ;
69+ }
4970 const key = genRandomKey ( ) ;
5071
5172 const manifest = uiCompRegistry [ selectedComponent ] ;
5273 let defaultDataFn = undefined ;
5374
54- if ( manifest ?. lazyLoad ) {
55- const { defaultDataFnName, defaultDataFnPath } = manifest ;
56- if ( defaultDataFnName && defaultDataFnPath ) {
57- const module = await import ( `../../../${ defaultDataFnPath } .tsx` ) ;
58- defaultDataFn = module [ defaultDataFnName ] ;
59- }
60- } else if ( ! compInfo . isRemote ) {
75+ if ( ! compInfo . isRemote ) {
6176 defaultDataFn = manifest ?. defaultDataFn ;
6277 }
6378
79+ let compDefaultValue = defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ;
80+ const compInitialValue = {
81+ ...( compDefaultValue as any || { } ) ,
82+ ...action_parameters ,
83+ }
6484 const widgetValue : GridItemDataType = {
6585 compType : selectedComponent ,
6686 name : compName ,
67- comp : defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ,
87+ comp : compInitialValue ,
6888 } ;
6989
70- const currentLayout = simpleContainer . children . layout . getView ( ) ;
90+ const currentLayout = uiComp . children . comp . children . layout . getView ( ) ;
7191 const layoutInfo = manifest ?. layoutInfo || defaultLayout ( selectedComponent as UICompType ) ;
7292
7393 let itemPos = 0 ;
@@ -83,9 +103,11 @@ export const addComponentAction: ActionConfig = {
83103 h : layoutInfo . h || 5 ,
84104 pos : itemPos ,
85105 isDragging : false ,
106+ ...( layout || { } ) ,
86107 } ;
87108
88- simpleContainer . dispatch (
109+ await getPromiseAfterDispatch (
110+ uiComp . children . comp . dispatch ,
89111 wrapActionExtraInfo (
90112 multiChangeAction ( {
91113 layout : changeValueAction ( {
@@ -95,8 +117,23 @@ export const addComponentAction: ActionConfig = {
95117 items : addMapChildAction ( key , widgetValue ) ,
96118 } ) ,
97119 { compInfos : [ { compName : compName , compType : selectedComponent , type : "add" } ] }
98- )
120+ ) ,
121+ {
122+ autoHandleAfterReduce : true ,
123+ }
99124 ) ;
125+ // simpleContainer.dispatch(
126+ // wrapActionExtraInfo(
127+ // multiChangeAction({
128+ // layout: changeValueAction({
129+ // ...currentLayout,
130+ // [key]: layoutItem,
131+ // }, true),
132+ // items: addMapChildAction(key, widgetValue),
133+ // }),
134+ // { compInfos: [{ compName: compName, compType: selectedComponent, type: "add" }] }
135+ // )
136+ // );
100137
101138 editorState . setSelectedCompNames ( new Set ( [ compName ] ) , "addComp" ) ;
102139
@@ -116,28 +153,32 @@ export const nestComponentAction: ActionConfig = {
116153 requiresInput : false ,
117154 isNested : true ,
118155 execute : async ( params : ActionExecuteParams ) => {
119- const { selectedEditorComponent, selectedNestComponent, editorState } = params ;
120-
156+ // const { selectedEditorComponent, selectedNestComponent, editorState, actionPayload } = params;
157+ const { editorState, actionPayload, selectedComponent : selectedNestComponent } = params ;
158+ const { component_name : name , layout, parent_component_name : selectedEditorComponent , action_parameters } = actionPayload ;
159+ // const { name, layout, target: selectedEditorComponent, ...otherProps } = actionPayload;
160+
121161 if ( ! selectedEditorComponent || ! selectedNestComponent || ! editorState ) {
122162 message . error ( 'Parent component, child component, and editor state are required' ) ;
123163 return ;
124164 }
125165
126- const parentComponentInfo = getEditorComponentInfo ( editorState , selectedEditorComponent ) ;
166+ const [ editorComponent , ...childComponents ] = selectedEditorComponent . split ( '.' ) ;
167+ const parentItem = editorState . getUICompByName ( editorComponent ) ; //getEditorComponentInfo(editorState, editorComponent);
127168
128- if ( ! parentComponentInfo ) {
129- message . error ( `Parent component "${ selectedEditorComponent } " not found` ) ;
130- return ;
131- }
169+ // if (!parentComponentInfo) {
170+ // message.error(`Parent component "${selectedEditorComponent}" not found`);
171+ // return;
172+ // }
132173
133- const { componentKey : parentKey , items } = parentComponentInfo ;
174+ // const { componentKey: parentKey, items } = parentComponentInfo;
134175
135- if ( ! parentKey ) {
136- message . error ( `Parent component "${ selectedEditorComponent } " not found in layout` ) ;
137- return ;
138- }
176+ // if (!parentKey) {
177+ // message.error(`Parent component "${selectedEditorComponent}" not found in layout`);
178+ // return;
179+ // }
139180
140- const parentItem = items [ parentKey ] ;
181+ // const parentItem = items[parentKey];
141182 if ( ! parentItem ) {
142183 message . error ( `Parent component "${ selectedEditorComponent } " not found in items` ) ;
143184 return ;
@@ -153,10 +194,15 @@ export const nestComponentAction: ActionConfig = {
153194 }
154195
155196 try {
156-
197+ let compName = name ;
157198 const nameGenerator = editorState . getNameGenerator ( ) ;
158199 const compInfo = parseCompType ( selectedNestComponent ) ;
159- const compName = nameGenerator . genItemName ( compInfo . compName ) ;
200+ if ( ! compName ) {
201+ compName = nameGenerator . genItemName ( compInfo . compName ) ;
202+ }
203+ // const nameGenerator = editorState.getNameGenerator();
204+ // const compInfo = parseCompType(selectedNestComponent);
205+ // const compName = nameGenerator.genItemName(compInfo.compName);
160206 const key = genRandomKey ( ) ;
161207
162208 const manifest = uiCompRegistry [ selectedNestComponent ] ;
@@ -172,15 +218,33 @@ export const nestComponentAction: ActionConfig = {
172218 defaultDataFn = manifest ?. defaultDataFn ;
173219 }
174220
221+ let compDefaultValue = defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ;
222+ const compInitialValue = {
223+ ...( compDefaultValue as any || { } ) ,
224+ ...action_parameters ,
225+ }
226+
175227 const widgetValue : GridItemDataType = {
176228 compType : selectedNestComponent ,
177229 name : compName ,
178- comp : defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ,
230+ comp : compInitialValue ,
179231 } ;
180232
181233 const parentContainer = parentItem . children . comp ;
182-
183- const realContainer = parentContainer . realSimpleContainer ( ) ;
234+ let originalContainer = parentContainer ;
235+ for ( const childComponent of childComponents ) {
236+ originalContainer = originalContainer . children [ childComponent ] ;
237+ }
238+ if ( originalContainer ?. children ?. [ 0 ] ?. children ?. view ) {
239+ originalContainer = originalContainer ?. children ?. [ 0 ] ?. children ?. view ;
240+ }
241+
242+ if ( ! originalContainer ) {
243+ message . error ( `Container "${ selectedEditorComponent } " cannot accept nested components` ) ;
244+ return ;
245+ }
246+
247+ const realContainer = originalContainer . realSimpleContainer ( ) ;
184248 if ( ! realContainer ) {
185249 message . error ( `Container "${ selectedEditorComponent } " cannot accept nested components` ) ;
186250 return ;
@@ -202,6 +266,7 @@ export const nestComponentAction: ActionConfig = {
202266 h : layoutInfo . h || 5 ,
203267 pos : itemPos ,
204268 isDragging : false ,
269+ ...( layout || { } ) ,
205270 } ;
206271
207272 realContainer . dispatch (
0 commit comments