Skip to content

Commit b29680f

Browse files
committed
fixed: Layout components issue, children components depth issues
1 parent 09f315c commit b29680f

File tree

2 files changed

+138
-45
lines changed

2 files changed

+138
-45
lines changed

client/packages/lowcoder/src/comps/comps/preLoadComp/actions/componentManagement.ts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
wrapChildAction,
1313
deleteCompAction
1414
} from "lowcoder-core";
15-
import { getEditorComponentInfo } from "../utils";
15+
import { getEditorComponentInfo, findTargetComponent } from "../utils";
1616

1717
export const addComponentAction: ActionConfig = {
1818
key: 'add-components',
@@ -249,17 +249,22 @@ export const deleteComponentAction: ActionConfig = {
249249
return;
250250
}
251251

252-
const { componentKey, currentLayout, simpleContainer, componentType } = componentInfo;
253-
254-
if (!componentKey || !currentLayout[componentKey]) {
255-
message.error(`Component "${selectedEditorComponent}" not found in layout`);
252+
const { allAppComponents } = componentInfo;
253+
const targetComponent = allAppComponents.find(comp => comp.name === selectedEditorComponent);
254+
255+
if (!targetComponent) {
256+
message.error(`Component "${selectedEditorComponent}" not found in application`);
256257
return;
257258
}
258259

259-
const newLayout = { ...currentLayout };
260-
delete newLayout[componentKey];
260+
const targetInfo = findTargetComponent(editorState, selectedEditorComponent);
261261

262-
simpleContainer.dispatch(
262+
if (targetInfo) {
263+
const { container, layout, componentKey } = targetInfo;
264+
const newLayout = { ...layout };
265+
delete newLayout[componentKey];
266+
267+
container.dispatch(
263268
wrapActionExtraInfo(
264269
multiChangeAction({
265270
layout: changeValueAction(newLayout, true),
@@ -268,7 +273,7 @@ export const deleteComponentAction: ActionConfig = {
268273
{
269274
compInfos: [{
270275
compName: selectedEditorComponent,
271-
compType: componentType || 'unknown',
276+
compType: targetComponent.compType || 'unknown',
272277
type: "delete"
273278
}]
274279
}
@@ -278,6 +283,9 @@ export const deleteComponentAction: ActionConfig = {
278283
editorState.setSelectedCompNames(new Set(), "deleteComp");
279284

280285
message.success(`Component "${selectedEditorComponent}" deleted successfully`);
286+
} else {
287+
message.error(`Component "${selectedEditorComponent}" not found in any container`);
288+
}
281289
} catch (error) {
282290
console.error('Error deleting component:', error);
283291
message.error('Failed to delete component. Please try again.');
@@ -336,22 +344,29 @@ export const moveComponentAction: ActionConfig = {
336344
return;
337345
}
338346

347+
const targetInfo = findTargetComponent(editorState, selectedEditorComponent);
348+
349+
if (!targetInfo) {
350+
message.error(`Component "${selectedEditorComponent}" not found in any container`);
351+
return;
352+
}
353+
354+
const { container, layout, componentKey } = targetInfo;
355+
339356
const componentInfo = getEditorComponentInfo(editorState, selectedEditorComponent);
340357

341358
if (!componentInfo) {
342359
message.error(`Component "${selectedEditorComponent}" not found`);
343360
return;
344361
}
345362

346-
const { componentKey, currentLayout, simpleContainer } = componentInfo;
347-
348-
if (!componentKey || !currentLayout[componentKey]) {
363+
if (!componentKey || !layout[componentKey]) {
349364
message.error(`Component "${selectedEditorComponent}" not found in layout`);
350365
return;
351366
}
352367

353-
const currentLayoutItem = currentLayout[componentKey];
354-
const items = simpleContainer.children.items.children;
368+
const currentLayoutItem = layout[componentKey];
369+
const items = container.children.items.children;
355370

356371
const newLayoutItem = {
357372
...currentLayoutItem,
@@ -360,11 +375,11 @@ export const moveComponentAction: ActionConfig = {
360375
};
361376

362377
const newLayout = {
363-
...currentLayout,
378+
...layout,
364379
[componentKey]: newLayoutItem,
365380
};
366381

367-
simpleContainer.dispatch(
382+
container.dispatch(
368383
wrapActionExtraInfo(
369384
multiChangeAction({
370385
layout: changeValueAction(newLayout, true),
@@ -503,21 +518,22 @@ export const resizeComponentAction: ActionConfig = {
503518
return;
504519
}
505520

506-
const componentInfo = getEditorComponentInfo(editorState, selectedEditorComponent);
507-
508-
if (!componentInfo) {
509-
message.error(`Component "${selectedEditorComponent}" not found`);
521+
const targetInfo = findTargetComponent(editorState, selectedEditorComponent);
522+
523+
if (!targetInfo) {
524+
message.error(`Component "${selectedEditorComponent}" not found in any container`);
510525
return;
511526
}
512527

513-
const { componentKey, currentLayout, simpleContainer, items } = componentInfo;
528+
const { container, layout, componentKey } = targetInfo;
514529

515-
if (!componentKey || !currentLayout[componentKey]) {
530+
if (!componentKey || !layout[componentKey]) {
516531
message.error(`Component "${selectedEditorComponent}" not found in layout`);
517532
return;
518533
}
519534

520-
const currentLayoutItem = currentLayout[componentKey];
535+
const currentLayoutItem = layout[componentKey];
536+
const items = container.children.items.children;
521537

522538
const newLayoutItem = {
523539
...currentLayoutItem,
@@ -526,11 +542,11 @@ export const resizeComponentAction: ActionConfig = {
526542
};
527543

528544
const newLayout = {
529-
...currentLayout,
545+
...layout,
530546
[componentKey]: newLayoutItem,
531547
};
532548

533-
simpleContainer.dispatch(
549+
container.dispatch(
534550
wrapActionExtraInfo(
535551
multiChangeAction({
536552
layout: changeValueAction(newLayout, true),

client/packages/lowcoder/src/comps/comps/preLoadComp/utils.ts

Lines changed: 97 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { UICompCategory, UICompManifest, uiCompCategoryNames, uiCompRegistry } f
55
import { MenuProps } from "antd/es/menu";
66
import React from "react";
77
import { EditorState } from "@lowcoder-ee/comps/editorState";
8+
import { getAllCompItems } from "comps/comps/containerBase/utils";
89

910
export function runScript(code: string, inHost?: boolean) {
1011
if (inHost) {
@@ -60,6 +61,7 @@ export function getEditorComponentInfo(editorState: EditorState, componentName?:
6061
simpleContainer: any;
6162
componentType?: string | null;
6263
items: any;
64+
allAppComponents: any[];
6365
} | null {
6466
try {
6567
// Get the UI component container
@@ -83,7 +85,9 @@ export function getEditorComponentInfo(editorState: EditorState, componentName?:
8385

8486
// Get current layout and items
8587
const currentLayout = simpleContainer.children.layout.getView();
88+
8689
const items = getCombinedItems(uiCompTree);
90+
const allAppComponents = getAllLayoutComponentsFromTree(uiCompTree);
8791

8892
// If no componentName is provided, return all items
8993
if (!componentName) {
@@ -92,6 +96,7 @@ export function getEditorComponentInfo(editorState: EditorState, componentName?:
9296
currentLayout,
9397
simpleContainer,
9498
items,
99+
allAppComponents,
95100
};
96101
}
97102

@@ -113,38 +118,35 @@ export function getEditorComponentInfo(editorState: EditorState, componentName?:
113118
simpleContainer,
114119
componentType,
115120
items,
121+
allAppComponents,
116122
};
117123
} catch(error) {
118124
console.error('Error getting editor component key:', error);
119125
return null;
120126
}
121127
}
122128

123-
interface Container {
124-
items?: Record<string, any>;
125-
}
126-
127-
function getCombinedItems(uiCompTree: any) {
129+
function getCombinedItems(uiCompTree: any, parentPath: string[] = []): Record<string, any> {
128130
const combined: Record<string, any> = {};
129131

130-
if (uiCompTree.items) {
131-
Object.entries(uiCompTree.items).forEach(([itemKey, itemValue]) => {
132-
combined[itemKey] = itemValue;
133-
});
134-
}
132+
function processContainer(container: any, currentPath: string[]) {
133+
if (container.items) {
134+
Object.entries(container.items).forEach(([itemKey, itemValue]) => {
135+
(itemValue as any).parentPath = [...currentPath];
136+
combined[itemKey] = itemValue;
137+
});
138+
}
135139

136-
if (uiCompTree.children) {
137-
Object.entries(uiCompTree.children).forEach(([parentKey, container]) => {
138-
const typedContainer = container as Container;
139-
if (typedContainer.items) {
140-
Object.entries(typedContainer.items).forEach(([itemKey, itemValue]) => {
141-
itemValue.parentContainer = parentKey;
142-
combined[itemKey] = itemValue;
143-
});
144-
}
145-
});
140+
if (container.children) {
141+
Object.entries(container.children).forEach(([childKey, childContainer]) => {
142+
const newPath = [...currentPath, childKey];
143+
processContainer(childContainer, newPath);
144+
});
145+
}
146146
}
147147

148+
processContainer(uiCompTree, parentPath);
149+
148150
return combined;
149151
}
150152

@@ -156,3 +158,78 @@ export function getLayoutItemsOrder(layoutItems: any[]){
156158
value: index.toString()
157159
}));
158160
}
161+
162+
function getAllLayoutComponentsFromTree(compTree: any): any[] {
163+
try {
164+
const allCompItems = getAllCompItems(compTree);
165+
166+
return Object.entries(allCompItems).map(([itemKey, item]) => {
167+
const compItem = item as any;
168+
if (compItem && compItem.children) {
169+
return {
170+
id: itemKey,
171+
compType: compItem.children.compType?.getView(),
172+
name: compItem.children.name?.getView(),
173+
key: itemKey,
174+
comp: compItem.children.comp,
175+
autoHeight: compItem.autoHeight?.(),
176+
hidden: compItem.children.comp?.children?.hidden?.getView(),
177+
parentPath: compItem.parentPath || []
178+
};
179+
}
180+
});
181+
} catch (error) {
182+
console.error('Error getting all app components from tree:', error);
183+
return [];
184+
}
185+
}
186+
187+
export function getAllContainers(editorState: any) {
188+
const containers: Array<{container: any, path: string[]}> = [];
189+
190+
function findContainers(comp: any, path: string[] = []) {
191+
if (!comp) return;
192+
193+
if (comp.realSimpleContainer && typeof comp.realSimpleContainer === 'function') {
194+
const simpleContainer = comp.realSimpleContainer();
195+
if (simpleContainer) {
196+
containers.push({ container: simpleContainer, path });
197+
}
198+
}
199+
200+
if (comp.children) {
201+
Object.entries(comp.children).forEach(([key, child]) => {
202+
findContainers(child, [...path, key]);
203+
});
204+
}
205+
}
206+
207+
const uiComp = editorState.getUIComp();
208+
const container = uiComp.getComp();
209+
if (container) {
210+
findContainers(container);
211+
}
212+
213+
return containers;
214+
}
215+
216+
export function findTargetComponent(editorState: any, selectedEditorComponent: string) {
217+
const allContainers = getAllContainers(editorState);
218+
219+
for (const containerInfo of allContainers) {
220+
const containerLayout = containerInfo.container.children.layout.getView();
221+
const containerItems = containerInfo.container.children.items.children;
222+
223+
for (const [key, item] of Object.entries(containerItems)) {
224+
if ((item as any).children.name.getView() === selectedEditorComponent) {
225+
return {
226+
container: containerInfo.container,
227+
layout: containerLayout,
228+
componentKey: key
229+
};
230+
}
231+
}
232+
}
233+
234+
return null;
235+
}

0 commit comments

Comments
 (0)