@@ -13,6 +13,8 @@ import {
13
13
deleteCompAction
14
14
} from "lowcoder-core" ;
15
15
import { getEditorComponentInfo } from "../utils" ;
16
+ import { getPromiseAfterDispatch } from "@lowcoder-ee/util/promiseUtils" ;
17
+ import { hookCompCategory , HookCompType } from "@lowcoder-ee/comps/hooks/hookCompTypes" ;
16
18
17
19
export const addComponentAction : ActionConfig = {
18
20
key : 'add-components' ,
@@ -21,14 +23,30 @@ export const addComponentAction: ActionConfig = {
21
23
requiresComponentSelection : true ,
22
24
requiresInput : false ,
23
25
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
+
26
29
if ( ! selectedComponent || ! editorState ) {
27
30
message . error ( 'Component and editor state are required' ) ;
28
31
return ;
29
32
}
30
33
31
34
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
+
32
50
const uiComp = editorState . getUIComp ( ) ;
33
51
const container = uiComp . getComp ( ) ;
34
52
@@ -43,31 +61,33 @@ export const addComponentAction: ActionConfig = {
43
61
return ;
44
62
}
45
63
64
+ let compName = name ;
46
65
const nameGenerator = editorState . getNameGenerator ( ) ;
47
66
const compInfo = parseCompType ( selectedComponent ) ;
48
- const compName = nameGenerator . genItemName ( compInfo . compName ) ;
67
+ if ( ! compName ) {
68
+ compName = nameGenerator . genItemName ( compInfo . compName ) ;
69
+ }
49
70
const key = genRandomKey ( ) ;
50
71
51
72
const manifest = uiCompRegistry [ selectedComponent ] ;
52
73
let defaultDataFn = undefined ;
53
74
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 ) {
61
76
defaultDataFn = manifest ?. defaultDataFn ;
62
77
}
63
78
79
+ let compDefaultValue = defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ;
80
+ const compInitialValue = {
81
+ ...( compDefaultValue as any || { } ) ,
82
+ ...action_parameters ,
83
+ }
64
84
const widgetValue : GridItemDataType = {
65
85
compType : selectedComponent ,
66
86
name : compName ,
67
- comp : defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ,
87
+ comp : compInitialValue ,
68
88
} ;
69
89
70
- const currentLayout = simpleContainer . children . layout . getView ( ) ;
90
+ const currentLayout = uiComp . children . comp . children . layout . getView ( ) ;
71
91
const layoutInfo = manifest ?. layoutInfo || defaultLayout ( selectedComponent as UICompType ) ;
72
92
73
93
let itemPos = 0 ;
@@ -83,9 +103,11 @@ export const addComponentAction: ActionConfig = {
83
103
h : layoutInfo . h || 5 ,
84
104
pos : itemPos ,
85
105
isDragging : false ,
106
+ ...( layout || { } ) ,
86
107
} ;
87
108
88
- simpleContainer . dispatch (
109
+ await getPromiseAfterDispatch (
110
+ uiComp . children . comp . dispatch ,
89
111
wrapActionExtraInfo (
90
112
multiChangeAction ( {
91
113
layout : changeValueAction ( {
@@ -95,8 +117,23 @@ export const addComponentAction: ActionConfig = {
95
117
items : addMapChildAction ( key , widgetValue ) ,
96
118
} ) ,
97
119
{ compInfos : [ { compName : compName , compType : selectedComponent , type : "add" } ] }
98
- )
120
+ ) ,
121
+ {
122
+ autoHandleAfterReduce : true ,
123
+ }
99
124
) ;
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
+ // );
100
137
101
138
editorState . setSelectedCompNames ( new Set ( [ compName ] ) , "addComp" ) ;
102
139
@@ -116,28 +153,32 @@ export const nestComponentAction: ActionConfig = {
116
153
requiresInput : false ,
117
154
isNested : true ,
118
155
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
+
121
161
if ( ! selectedEditorComponent || ! selectedNestComponent || ! editorState ) {
122
162
message . error ( 'Parent component, child component, and editor state are required' ) ;
123
163
return ;
124
164
}
125
165
126
- const parentComponentInfo = getEditorComponentInfo ( editorState , selectedEditorComponent ) ;
166
+ const [ editorComponent , ...childComponents ] = selectedEditorComponent . split ( '.' ) ;
167
+ const parentItem = editorState . getUICompByName ( editorComponent ) ; //getEditorComponentInfo(editorState, editorComponent);
127
168
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
+ // }
132
173
133
- const { componentKey : parentKey , items } = parentComponentInfo ;
174
+ // const { componentKey: parentKey, items } = parentComponentInfo;
134
175
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
+ // }
139
180
140
- const parentItem = items [ parentKey ] ;
181
+ // const parentItem = items[parentKey];
141
182
if ( ! parentItem ) {
142
183
message . error ( `Parent component "${ selectedEditorComponent } " not found in items` ) ;
143
184
return ;
@@ -153,10 +194,15 @@ export const nestComponentAction: ActionConfig = {
153
194
}
154
195
155
196
try {
156
-
197
+ let compName = name ;
157
198
const nameGenerator = editorState . getNameGenerator ( ) ;
158
199
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);
160
206
const key = genRandomKey ( ) ;
161
207
162
208
const manifest = uiCompRegistry [ selectedNestComponent ] ;
@@ -172,15 +218,33 @@ export const nestComponentAction: ActionConfig = {
172
218
defaultDataFn = manifest ?. defaultDataFn ;
173
219
}
174
220
221
+ let compDefaultValue = defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ;
222
+ const compInitialValue = {
223
+ ...( compDefaultValue as any || { } ) ,
224
+ ...action_parameters ,
225
+ }
226
+
175
227
const widgetValue : GridItemDataType = {
176
228
compType : selectedNestComponent ,
177
229
name : compName ,
178
- comp : defaultDataFn ? defaultDataFn ( compName , nameGenerator , editorState ) : undefined ,
230
+ comp : compInitialValue ,
179
231
} ;
180
232
181
233
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 ( ) ;
184
248
if ( ! realContainer ) {
185
249
message . error ( `Container "${ selectedEditorComponent } " cannot accept nested components` ) ;
186
250
return ;
@@ -202,6 +266,7 @@ export const nestComponentAction: ActionConfig = {
202
266
h : layoutInfo . h || 5 ,
203
267
pos : itemPos ,
204
268
isDragging : false ,
269
+ ...( layout || { } ) ,
205
270
} ;
206
271
207
272
realContainer . dispatch (
0 commit comments