Skip to content

Commit 86a12f0

Browse files
kamalqureshiraheeliftikhar5
authored andcommitted
fixed: Layout components issue, children components depth issues
1 parent d3142c1 commit 86a12f0

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
import { getPromiseAfterDispatch } from "@lowcoder-ee/util/promiseUtils";
1717
import { hookCompCategory, HookCompType } from "@lowcoder-ee/comps/hooks/hookCompTypes";
1818

@@ -314,17 +314,22 @@ export const deleteComponentAction: ActionConfig = {
314314
return;
315315
}
316316

317-
const { componentKey, currentLayout, simpleContainer, componentType } = componentInfo;
318-
319-
if (!componentKey || !currentLayout[componentKey]) {
320-
message.error(`Component "${selectedEditorComponent}" not found in layout`);
317+
const { allAppComponents } = componentInfo;
318+
const targetComponent = allAppComponents.find(comp => comp.name === selectedEditorComponent);
319+
320+
if (!targetComponent) {
321+
message.error(`Component "${selectedEditorComponent}" not found in application`);
321322
return;
322323
}
323324

324-
const newLayout = { ...currentLayout };
325-
delete newLayout[componentKey];
325+
const targetInfo = findTargetComponent(editorState, selectedEditorComponent);
326326

327-
simpleContainer.dispatch(
327+
if (targetInfo) {
328+
const { container, layout, componentKey } = targetInfo;
329+
const newLayout = { ...layout };
330+
delete newLayout[componentKey];
331+
332+
container.dispatch(
328333
wrapActionExtraInfo(
329334
multiChangeAction({
330335
layout: changeValueAction(newLayout, true),
@@ -333,7 +338,7 @@ export const deleteComponentAction: ActionConfig = {
333338
{
334339
compInfos: [{
335340
compName: selectedEditorComponent,
336-
compType: componentType || 'unknown',
341+
compType: targetComponent.compType || 'unknown',
337342
type: "delete"
338343
}]
339344
}
@@ -343,6 +348,9 @@ export const deleteComponentAction: ActionConfig = {
343348
editorState.setSelectedCompNames(new Set(), "deleteComp");
344349

345350
message.success(`Component "${selectedEditorComponent}" deleted successfully`);
351+
} else {
352+
message.error(`Component "${selectedEditorComponent}" not found in any container`);
353+
}
346354
} catch (error) {
347355
console.error('Error deleting component:', error);
348356
message.error('Failed to delete component. Please try again.');
@@ -401,22 +409,29 @@ export const moveComponentAction: ActionConfig = {
401409
return;
402410
}
403411

412+
const targetInfo = findTargetComponent(editorState, selectedEditorComponent);
413+
414+
if (!targetInfo) {
415+
message.error(`Component "${selectedEditorComponent}" not found in any container`);
416+
return;
417+
}
418+
419+
const { container, layout, componentKey } = targetInfo;
420+
404421
const componentInfo = getEditorComponentInfo(editorState, selectedEditorComponent);
405422

406423
if (!componentInfo) {
407424
message.error(`Component "${selectedEditorComponent}" not found`);
408425
return;
409426
}
410427

411-
const { componentKey, currentLayout, simpleContainer } = componentInfo;
412-
413-
if (!componentKey || !currentLayout[componentKey]) {
428+
if (!componentKey || !layout[componentKey]) {
414429
message.error(`Component "${selectedEditorComponent}" not found in layout`);
415430
return;
416431
}
417432

418-
const currentLayoutItem = currentLayout[componentKey];
419-
const items = simpleContainer.children.items.children;
433+
const currentLayoutItem = layout[componentKey];
434+
const items = container.children.items.children;
420435

421436
const newLayoutItem = {
422437
...currentLayoutItem,
@@ -425,11 +440,11 @@ export const moveComponentAction: ActionConfig = {
425440
};
426441

427442
const newLayout = {
428-
...currentLayout,
443+
...layout,
429444
[componentKey]: newLayoutItem,
430445
};
431446

432-
simpleContainer.dispatch(
447+
container.dispatch(
433448
wrapActionExtraInfo(
434449
multiChangeAction({
435450
layout: changeValueAction(newLayout, true),
@@ -568,21 +583,22 @@ export const resizeComponentAction: ActionConfig = {
568583
return;
569584
}
570585

571-
const componentInfo = getEditorComponentInfo(editorState, selectedEditorComponent);
572-
573-
if (!componentInfo) {
574-
message.error(`Component "${selectedEditorComponent}" not found`);
586+
const targetInfo = findTargetComponent(editorState, selectedEditorComponent);
587+
588+
if (!targetInfo) {
589+
message.error(`Component "${selectedEditorComponent}" not found in any container`);
575590
return;
576591
}
577592

578-
const { componentKey, currentLayout, simpleContainer, items } = componentInfo;
593+
const { container, layout, componentKey } = targetInfo;
579594

580-
if (!componentKey || !currentLayout[componentKey]) {
595+
if (!componentKey || !layout[componentKey]) {
581596
message.error(`Component "${selectedEditorComponent}" not found in layout`);
582597
return;
583598
}
584599

585-
const currentLayoutItem = currentLayout[componentKey];
600+
const currentLayoutItem = layout[componentKey];
601+
const items = container.children.items.children;
586602

587603
const newLayoutItem = {
588604
...currentLayoutItem,
@@ -591,11 +607,11 @@ export const resizeComponentAction: ActionConfig = {
591607
};
592608

593609
const newLayout = {
594-
...currentLayout,
610+
...layout,
595611
[componentKey]: newLayoutItem,
596612
};
597613

598-
simpleContainer.dispatch(
614+
container.dispatch(
599615
wrapActionExtraInfo(
600616
multiChangeAction({
601617
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)