Skip to content

Commit 39e209b

Browse files
committed
✨ 增加树形结构一键转框嵌套结构的功能
1 parent 697feb1 commit 39e209b

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

app/src/core/stage/stageManager/concreteMethods/StageSectionPackManager.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export namespace StageSectionPackManager {
8686
*/
8787
export function textNodeTreeToSection(rootNode: TextNode): void {
8888
if (!GraphMethods.isTree(rootNode)) {
89+
Dialog.show({
90+
title: "非树状结构",
91+
content: "请选择一个树状结构的节点作为根节点",
92+
});
8993
return;
9094
}
9195
const dfs = (node: TextNode): Section | TextNode => {
@@ -97,21 +101,27 @@ export namespace StageSectionPackManager {
97101
for (const childNode of childNodes) {
98102
const transEntity = dfs(childNode);
99103
childEntityList.push(transEntity);
100-
// TODO: 断开主和子之间的连线
104+
105+
const edges = GraphMethods.getEdgesBetween(node, childNode);
106+
for (const edge of edges) {
107+
StageManager.deleteEdge(edge);
108+
}
101109
}
102-
const section = targetTextNodeToSection(node);
110+
const section = targetTextNodeToSection(node, true);
103111

104112
StageSectionInOutManager.goInSection(childEntityList, section);
105113
return section;
106114
};
107115
dfs(rootNode);
116+
StageHistoryManager.recordStep();
108117
}
109118

110119
/**
111120
* 将指定的文本节点转换成Section
112-
* @param textNode
121+
* @param textNode 要转换的节点
122+
* @param ignoreEdges 是否忽略边的影响
113123
*/
114-
export function targetTextNodeToSection(textNode: TextNode): Section {
124+
export function targetTextNodeToSection(textNode: TextNode, ignoreEdges: boolean = false): Section {
115125
// 获取这个节点的父级Section
116126
const fatherSections = SectionMethods.getFatherSections(textNode);
117127
const rect = textNode.collisionBox.getRectangle().expandFromCenter(50);
@@ -136,13 +146,15 @@ export namespace StageSectionPackManager {
136146
StageSectionInOutManager.goInSection([newSection], fatherSection);
137147
}
138148

139-
// 将所有连向它的东西连到新的Section
140-
for (const fatherConnection of fatherConnections) {
141-
StageManager.connectEntity(fatherConnection, newSection);
142-
}
143-
// 将所有连向新的Section的东西连到它
144-
for (const childConnection of childConnections) {
145-
StageManager.connectEntity(newSection, childConnection);
149+
if (!ignoreEdges) {
150+
// 将所有连向它的东西连到新的Section
151+
for (const fatherConnection of fatherConnections) {
152+
StageManager.connectEntity(fatherConnection, newSection);
153+
}
154+
// 将所有连向新的Section的东西连到它
155+
for (const childConnection of childConnections) {
156+
StageManager.connectEntity(newSection, childConnection);
157+
}
146158
}
147159
// 更新section的碰撞箱
148160
newSection.adjustLocationAndSize();

app/src/pages/_popup_panel/_align_panel.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
LayoutGrid,
1212
Magnet,
1313
Network,
14+
SquareSquare,
1415
} from "lucide-react";
1516
import { useEffect, useState } from "react";
1617
import { Dialog } from "../../components/dialog";
@@ -22,6 +23,8 @@ import { cn } from "../../utils/cn";
2223
import { ToolbarItem } from "../_toolbar";
2324
import { StageEntityMoveManager } from "../../core/stage/stageManager/concreteMethods/StageEntityMoveManager";
2425
import { StageAutoAlignManager } from "../../core/stage/stageManager/concreteMethods/StageAutoAlignManager";
26+
import { StageSectionPackManager } from "../../core/stage/stageManager/concreteMethods/StageSectionPackManager";
27+
import { TextNode } from "../../core/stage/stageObject/entity/TextNode";
2528
export default function AlignNodePanel() {
2629
const [isEnableDragAutoAlign, setEnableDragAutoAlign] = useState(false);
2730

@@ -198,6 +201,21 @@ export default function AlignNodePanel() {
198201
StageEntityMoveManager.layoutToTightSquare();
199202
}}
200203
/>
204+
<ToolbarItem
205+
description="将树形结构变成框嵌套结构"
206+
icon={<SquareSquare />}
207+
handleFunction={() => {
208+
const selectedNodes = StageManager.getSelectedEntities().filter((node) => node instanceof TextNode);
209+
if (selectedNodes.length !== 1) {
210+
Dialog.show({
211+
title: "选择节点数量不为1",
212+
content: "必须只选择一个根节点才可以进行树形结构变成框嵌套结构",
213+
});
214+
return;
215+
}
216+
StageSectionPackManager.textNodeTreeToSection(selectedNodes[0]);
217+
}}
218+
/>
201219
<ToolbarItem
202220
description={isEnableDragAutoAlign ? "拖动吸附对齐:开启" : "拖动吸附对齐:关闭"}
203221
icon={<Magnet className={cn(!isEnableDragAutoAlign && "text-panel-details-text", "transition-transform")} />}

0 commit comments

Comments
 (0)