mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Merge pull request #163 from SyncrowIOT/fix/duplication-flatten
Fix/duplication-flatten
This commit is contained in:
@ -407,21 +407,31 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
|
||||
List<SpaceModel> flattenSpaces(List<SpaceModel> spaces) {
|
||||
List<SpaceModel> result = [];
|
||||
Map<String, SpaceModel> idToSpace = {};
|
||||
|
||||
void flatten(SpaceModel space) {
|
||||
if (space.status == SpaceStatus.deleted || space.status == SpaceStatus.parentDeleted) {
|
||||
return;
|
||||
}
|
||||
result.add(space);
|
||||
idToSpace[space.internalId] = space;
|
||||
|
||||
for (var child in space.children) {
|
||||
flatten(child);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (var space in spaces) {
|
||||
flatten(space);
|
||||
}
|
||||
|
||||
for (var space in result) {
|
||||
if (space.parent != null) {
|
||||
space.parent = idToSpace[space.parent!.internalId];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -698,7 +708,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
final double verticalGap = 180.0;
|
||||
final double nodeWidth = 200;
|
||||
final double nodeHeight = 100;
|
||||
final double breathingSpace = 300.0; // extra gap after original tree
|
||||
final double breathingSpace = 300.0;
|
||||
|
||||
/// Helper to recursively duplicate a node and its children
|
||||
SpaceModel duplicateRecursive(SpaceModel original, SpaceModel? duplicatedParent) {
|
||||
@ -706,7 +716,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
final duplicated = SpaceModel(
|
||||
name: duplicatedName,
|
||||
icon: original.icon,
|
||||
position: Offset.zero,
|
||||
position: original.position,
|
||||
isPrivate: original.isPrivate,
|
||||
children: [],
|
||||
status: SpaceStatus.newSpace,
|
||||
@ -725,11 +735,13 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
direction: "down",
|
||||
);
|
||||
connections.add(newConnection);
|
||||
|
||||
duplicated.incomingConnection = newConnection;
|
||||
duplicatedParent.addOutgoingConnection(newConnection);
|
||||
duplicatedParent.children.add(duplicated);
|
||||
}
|
||||
|
||||
// Duplicate its children recursively
|
||||
for (var child in original.children) {
|
||||
duplicateRecursive(child, duplicated);
|
||||
}
|
||||
@ -737,48 +749,29 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
return duplicated;
|
||||
}
|
||||
|
||||
/// Layout a subtree rooted at node
|
||||
void layoutSubtree(SpaceModel node, double startX, double startY) {
|
||||
double calculateSubtreeWidth(SpaceModel n) {
|
||||
if (n.children.isEmpty) return nodeWidth;
|
||||
double width = 0;
|
||||
for (var child in n.children) {
|
||||
width += calculateSubtreeWidth(child) + horizontalGap;
|
||||
}
|
||||
return width - horizontalGap;
|
||||
}
|
||||
|
||||
void assignPositions(SpaceModel n, double x, double y) {
|
||||
double subtreeWidth = calculateSubtreeWidth(n);
|
||||
double centerX = x + subtreeWidth / 2 - nodeWidth / 2;
|
||||
n.position = Offset(centerX, y);
|
||||
|
||||
if (n.children.length == 1) {
|
||||
assignPositions(n.children.first, centerX, y + verticalGap);
|
||||
} else {
|
||||
double childX = x;
|
||||
for (var child in n.children) {
|
||||
double childWidth = calculateSubtreeWidth(child);
|
||||
assignPositions(child, childX, y + verticalGap);
|
||||
childX += childWidth + horizontalGap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double totalSubtreeWidth = calculateSubtreeWidth(node);
|
||||
assignPositions(node, startX, startY);
|
||||
}
|
||||
|
||||
/// Actual duplication process
|
||||
setState(() {
|
||||
if (space.parent == null) {
|
||||
// Duplicating a ROOT node
|
||||
SpaceModel duplicatedRoot = duplicateRecursive(space, null);
|
||||
duplicateRecursive(space, null);
|
||||
connections = createConnections(spaces);
|
||||
realignTree();
|
||||
} else {
|
||||
// Duplicating a CHILD node inside its parent
|
||||
SpaceModel duplicated = duplicateRecursive(space, space.parent);
|
||||
final parent = space.parent!;
|
||||
|
||||
final duplicated = duplicateRecursive(space, parent);
|
||||
final newConnection = Connection(
|
||||
startSpace: parent,
|
||||
endSpace: duplicated,
|
||||
direction: "down",
|
||||
);
|
||||
connections.add(newConnection);
|
||||
duplicated.incomingConnection = newConnection;
|
||||
parent.addOutgoingConnection(newConnection);
|
||||
parent.children.add(duplicated);
|
||||
connections = createConnections(spaces);
|
||||
realignTree();
|
||||
//_realignSubtree(space.parent!);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user