Skip to content

Commit df3850e

Browse files
authored
feat: markdown导入后自动调用右向树布局 (#666)
## 实现方案 在 Markdown 导入链路的末端(节点和边全部创建完成后、记录历史之前),对根节点调用已有的 `autoLayoutSelectedFastTreeMode` 方法。 ### 调用链路 ``` GenerateNodeWindow (UI) → StageManager.generateNodeByMarkdown(text, location, autoLayout) → NodeAdder.addNodeByMarkdown(markdownText, diffLocation, autoLayout) → StageImport.addNodeByMarkdown(markdownText, diffLocation, autoLayout) → MarkdownImporter.import(markdownText, diffLocation, autoLayout) → autoAlign.autoLayoutSelectedFastTreeMode(rootNode) // ← 新增 ``` ### 关键设计决策 - **仅对根节点调用布局**:`autoLayoutSelectedFastTreeMode` 会递归处理整棵子树,只需传入 root 即可 - **默认启用,可选关闭**:`autoLayout` 参数默认为 `true`,保持向后兼容,调用方可传 `false` 跳过自动布局 - **布局在历史记录之前执行**:确保 undo 时能一步撤销「导入+布局」的完整操作 - **复用已有算法**:直接使用 `StageAutoAlignManager` 中的快速树布局,与右键菜单「自动排列 → 向右树形」完全一致 ## 效果 https://github.com/user-attachments/assets/437834cb-3a1a-4883-8b75-2e2ff298f231
2 parents 1e44520 + f0c1600 commit df3850e

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

app/src/core/service/dataGenerateService/stageImportEngine/MarkdownImporter.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ export class MarkdownImporter extends BaseImporter {
2323
* 导入 Markdown 文本并生成节点树
2424
* @param markdownText Markdown 格式文本
2525
* @param diffLocation 偏移位置
26+
* @param autoLayout 是否自动应用树形布局(默认为 true,自动整理为向右的树状结构)
2627
*/
27-
public import(markdownText: string, diffLocation: Vector = Vector.getZero()): void {
28+
public import(markdownText: string, diffLocation: Vector = Vector.getZero(), autoLayout = true): void {
2829
const markdownJson = parseMarkdownToJSON(markdownText);
2930

3031
const monoStack = new MonoStack<TextNode>();
@@ -77,6 +78,11 @@ export class MarkdownImporter extends BaseImporter {
7778
dfsMarkdownNode(markdownNode, 0);
7879
}
7980

81+
// 自动应用树形布局(如果启用)
82+
if (autoLayout) {
83+
this.project.autoAlign.autoLayoutSelectedFastTreeMode(rootNode);
84+
}
85+
8086
// 记录历史
8187
this.project.historyManager.recordStep();
8288
}

app/src/core/service/dataGenerateService/stageImportEngine/stageImportEngine.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ export class StageImport {
6666
* 支持 Markdown 标题层级(#, ##, ###)
6767
* @param markdownText Markdown 格式文本
6868
* @param diffLocation 偏移位置
69+
* @param autoLayout 是否自动应用树形布局(默认为 true)
6970
* @example
7071
* # 标题1
7172
* ## 子标题1.1
7273
* ## 子标题1.2
7374
* # 标题2
7475
*/
75-
public addNodeByMarkdown(markdownText: string, diffLocation: Vector = Vector.getZero()) {
76-
return this.markdownImporter.import(markdownText, diffLocation);
76+
public addNodeByMarkdown(markdownText: string, diffLocation: Vector = Vector.getZero(), autoLayout = true) {
77+
return this.markdownImporter.import(markdownText, diffLocation, autoLayout);
7778
}
7879
}

app/src/core/stage/stageManager/StageManager.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ export class StageManager {
641641
}
642642
}
643643

644-
generateNodeByMarkdown(text: string, location = this.project.camera.location) {
645-
this.project.nodeAdder.addNodeByMarkdown(text, location);
644+
generateNodeByMarkdown(text: string, location = this.project.camera.location, autoLayout = true) {
645+
this.project.nodeAdder.addNodeByMarkdown(text, location, autoLayout);
646646
this.project.historyManager.recordStep();
647647
}
648648

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,14 @@ export class NodeAdder {
208208
this.project.stageImport.addNodeMermaidByText(text, diffLocation);
209209
}
210210

211-
public addNodeByMarkdown(markdownText: string, diffLocation: Vector = Vector.getZero()) {
212-
this.project.stageImport.addNodeByMarkdown(markdownText, diffLocation);
211+
/**
212+
* 根据 Markdown 文本生成节点树结构
213+
* @param markdownText Markdown 格式文本
214+
* @param diffLocation 偏移位置
215+
* @param autoLayout 是否自动应用树形布局(默认为 true)
216+
*/
217+
public addNodeByMarkdown(markdownText: string, diffLocation: Vector = Vector.getZero(), autoLayout = true) {
218+
this.project.stageImport.addNodeByMarkdown(markdownText, diffLocation, autoLayout);
213219
}
214220

215221
/***

0 commit comments

Comments
 (0)