Skip to content

Commit 32d68e1

Browse files
committed
♻️ 减小StageManager体积
1 parent 42cd5ea commit 32d68e1

File tree

10 files changed

+66
-70
lines changed

10 files changed

+66
-70
lines changed

src/core/service/controlService/autoLayoutEngine/autoLayoutFastTreeMode.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { Vector } from "../../../dataStruct/Vector";
8-
import { StageManager } from "../../../stage/stageManager/StageManager";
8+
import { GraphMethods } from "../../../stage/stageManager/basicMethods/GraphMethods";
99
import { ConnectableEntity } from "../../../stage/stageObject/abstract/ConnectableEntity";
1010

1111
/**
@@ -17,11 +17,11 @@ export function autoLayoutFastTreeMode(rootNode: ConnectableEntity) {
1717
const spaceX = 20;
1818
const spaceY = 150;
1919
// 子节点所占空间的宽度
20-
let width = Math.max(0, StageManager.nodeChildrenArray(node).length - 1) * spaceX;
20+
let width = Math.max(0, GraphMethods.nodeChildrenArray(node).length - 1) * spaceX;
2121
const widths: number[] = [];
2222
const paddings: number[] = [];
2323
let sumWidths = -width; // widths元素之和
24-
for (const child of StageManager.nodeChildrenArray(node)) {
24+
for (const child of GraphMethods.nodeChildrenArray(node)) {
2525
const childrenWidth = dfs(child);
2626
const wd = child.collisionBox.getRectangle().size.x;
2727
widths.push(Math.max(wd, childrenWidth));
@@ -31,7 +31,7 @@ export function autoLayoutFastTreeMode(rootNode: ConnectableEntity) {
3131
sumWidths += width;
3232
let currentX = node.geometryCenter.x - (sumWidths - paddings[0] - paddings[paddings.length - 1]) / 2 - paddings[0];
3333
for (let i = 0; i < widths.length; i++) {
34-
const child = StageManager.nodeChildrenArray(node)[i];
34+
const child = GraphMethods.nodeChildrenArray(node)[i];
3535
child.moveTo(new Vector(currentX + paddings[i], node.collisionBox.getRectangle().top + spaceY));
3636
currentX += widths[i] + spaceX;
3737
}

src/core/service/controlService/autoLayoutEngine/mainTick.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GraphMethods } from "../../../stage/stageManager/basicMethods/GraphMethods";
12
import { StageManager } from "../../../stage/stageManager/StageManager";
23
import { ConnectableEntity } from "../../../stage/stageObject/abstract/ConnectableEntity";
34
import { Controller } from "../controller/Controller";
@@ -17,7 +18,7 @@ export function autoLayoutMainTick(): void {
1718
// 遍历所有选中的节点,将他们的直接孩子节点拉向自己
1819
selectedConnectableEntities.forEach((entity) => {
1920
// 计算父向子的关系
20-
const children = StageManager.nodeChildrenArray(entity);
21+
const children = GraphMethods.nodeChildrenArray(entity);
2122
children.forEach((child) => {
2223
// 计算子节点到父节点的向量
2324
const fatherToChildVector = child.collisionBox

src/core/service/dataGenerateService/autoComputeEngine/AutoComputeUtils.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ConnectableEntity } from "../../../stage/stageObject/abstract/Connectab
44
import { Section } from "../../../stage/stageObject/entity/Section";
55
import { TextNode } from "../../../stage/stageObject/entity/TextNode";
66
import { ProgramFunctions } from "./functions/programLogic";
7+
import { GraphMethods } from "../../../stage/stageManager/basicMethods/GraphMethods";
78

89
/**
910
* 一些在自动计算引擎中
@@ -16,7 +17,7 @@ export namespace AutoComputeUtils {
1617
* @returns
1718
*/
1819
export function getParentTextNodes(node: TextNode): TextNode[] {
19-
const parents = StageManager.nodeParentArray(node).filter((node) => node instanceof TextNode);
20+
const parents = GraphMethods.nodeParentArray(node).filter((node) => node instanceof TextNode);
2021
// 将parents按x的坐标排序,小的在前面
2122
parents.sort((a, b) => {
2223
return a.collisionBox.getRectangle().location.x - b.collisionBox.getRectangle().location.x;
@@ -30,7 +31,7 @@ export namespace AutoComputeUtils {
3031
* @returns
3132
*/
3233
export function getChildTextNodes(node: TextNode): TextNode[] {
33-
return StageManager.nodeChildrenArray(node)
34+
return GraphMethods.nodeChildrenArray(node)
3435
.filter((node) => node instanceof TextNode)
3536
.sort((a, b) => a.collisionBox.getRectangle().location.x - b.collisionBox.getRectangle().location.x);
3637
}
@@ -41,7 +42,7 @@ export namespace AutoComputeUtils {
4142
* @param resultText
4243
*/
4344
export function getNodeOneResult(node: TextNode, resultText: string) {
44-
const childrenList = StageManager.nodeChildrenArray(node).filter((node) => node instanceof TextNode);
45+
const childrenList = GraphMethods.nodeChildrenArray(node).filter((node) => node instanceof TextNode);
4546
if (childrenList.length > 0) {
4647
for (const child of childrenList) {
4748
child.rename(resultText);
@@ -66,7 +67,7 @@ export namespace AutoComputeUtils {
6667
* @param resultText
6768
*/
6869
export function getSectionOneResult(section: Section, resultText: string) {
69-
const childrenList = StageManager.nodeChildrenArray(section).filter((node) => node instanceof TextNode);
70+
const childrenList = GraphMethods.nodeChildrenArray(section).filter((node) => node instanceof TextNode);
7071
if (childrenList.length > 0) {
7172
for (const child of childrenList) {
7273
child.rename(resultText);
@@ -86,7 +87,7 @@ export namespace AutoComputeUtils {
8687
}
8788

8889
export function getSectionMultiResult(section: Section, resultTextList: string[]) {
89-
let childrenList = StageManager.nodeChildrenArray(section).filter((node) => node instanceof TextNode);
90+
let childrenList = GraphMethods.nodeChildrenArray(section).filter((node) => node instanceof TextNode);
9091
if (childrenList.length < resultTextList.length) {
9192
// 子节点数量不够,需要新建节点
9293
const needCount = resultTextList.length - childrenList.length;
@@ -106,7 +107,7 @@ export namespace AutoComputeUtils {
106107
}
107108
}
108109
// 子节点数量够了,直接修改,顺序是从上到下
109-
childrenList = StageManager.nodeChildrenArray(section)
110+
childrenList = GraphMethods.nodeChildrenArray(section)
110111
.filter((node) => node instanceof TextNode)
111112
.sort(
112113
(node1, node2) => node1.collisionBox.getRectangle().location.y - node2.collisionBox.getRectangle().location.y,
@@ -126,7 +127,7 @@ export namespace AutoComputeUtils {
126127
*/
127128
export function getMultiResult(node: TextNode, resultTextList: string[]) {
128129
// 先把子节点数量凑够
129-
let childrenList = StageManager.nodeChildrenArray(node).filter((node) => node instanceof TextNode);
130+
let childrenList = GraphMethods.nodeChildrenArray(node).filter((node) => node instanceof TextNode);
130131
if (childrenList.length < resultTextList.length) {
131132
// 子节点数量不够,需要新建节点
132133
const needCount = resultTextList.length - childrenList.length;
@@ -146,7 +147,7 @@ export namespace AutoComputeUtils {
146147
}
147148
}
148149
// 子节点数量够了,直接修改,顺序是从上到下
149-
childrenList = StageManager.nodeChildrenArray(node)
150+
childrenList = GraphMethods.nodeChildrenArray(node)
150151
.filter((node) => node instanceof TextNode)
151152
.sort(
152153
(node1, node2) => node1.collisionBox.getRectangle().location.y - node2.collisionBox.getRectangle().location.y,
@@ -176,12 +177,12 @@ export namespace AutoComputeUtils {
176177
* @param node
177178
*/
178179
export function isNodeConnectedWithLogicNode(node: ConnectableEntity): boolean {
179-
for (const fatherNode of StageManager.nodeParentArray(node)) {
180+
for (const fatherNode of GraphMethods.nodeParentArray(node)) {
180181
if (fatherNode instanceof TextNode && isNameIsLogicNode(fatherNode.text)) {
181182
return true;
182183
}
183184
}
184-
for (const childNode of StageManager.nodeChildrenArray(node)) {
185+
for (const childNode of GraphMethods.nodeChildrenArray(node)) {
185186
if (childNode instanceof TextNode && isNameIsLogicNode(childNode.text)) {
186187
return true;
187188
}

src/core/service/dataGenerateService/stageExportEngine/stageExportEngine.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { writeTextFile } from "../../../../utils/fs";
2-
import { StageManager } from "../../../stage/stageManager/StageManager";
2+
import { GraphMethods } from "../../../stage/stageManager/basicMethods/GraphMethods";
33
import { ConnectableEntity } from "../../../stage/stageObject/abstract/ConnectableEntity";
44
import { Entity } from "../../../stage/stageObject/abstract/StageEntity";
55
import { TextNode } from "../../../stage/stageObject/entity/TextNode";
@@ -52,7 +52,7 @@ export class StageExportEngine {
5252
if (node.details.trim()) {
5353
nodesContent += "\t" + node.details + "\n";
5454
}
55-
StageManager.nodeChildrenArray(node)
55+
GraphMethods.nodeChildrenArray(node)
5656
.filter((node) => node instanceof TextNode)
5757
.filter((node) => nodes.includes(node))
5858
.forEach((child) => {
@@ -101,7 +101,7 @@ export class StageExportEngine {
101101
* @param node
102102
*/
103103
private getNodeChildrenArray(node: TextNode): ConnectableEntity[] {
104-
const result = StageManager.nodeChildrenArray(node);
104+
const result = GraphMethods.nodeChildrenArray(node);
105105
// 如果全都在右侧或者左侧
106106
if (
107107
result.every((v) => v.geometryCenter.x > node.geometryCenter.x) ||

src/core/stage/stageManager/StageManager.tsx

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { PenStroke } from "../stageObject/entity/PenStroke";
2121
import { Section } from "../stageObject/entity/Section";
2222
import { TextNode } from "../stageObject/entity/TextNode";
2323
import { UrlNode } from "../stageObject/entity/UrlNode";
24+
import { GraphMethods } from "./basicMethods/GraphMethods";
2425
import { SectionMethods } from "./basicMethods/SectionMethods";
2526
import { StageAutoAlignManager } from "./concreteMethods/StageAutoAlignManager";
2627
import { StageDeleteManager } from "./concreteMethods/StageDeleteManager";
@@ -214,41 +215,6 @@ export namespace StageManager {
214215
// 用于UI层监测
215216
export let selectedNodeCount = 0;
216217
export let selectedEdgeCount = 0;
217-
218-
/** 获取节点连接的子节点数组,未排除自环 */
219-
export function nodeChildrenArray(node: ConnectableEntity): ConnectableEntity[] {
220-
const res: ConnectableEntity[] = [];
221-
for (const edge of getLineEdges()) {
222-
if (edge.source.uuid === node.uuid) {
223-
res.push(edge.target);
224-
}
225-
}
226-
return res;
227-
}
228-
229-
/**
230-
* 获取一个节点的所有父亲节点,未排除自环
231-
* 性能有待优化!!
232-
*/
233-
export function nodeParentArray(node: ConnectableEntity): ConnectableEntity[] {
234-
const res: ConnectableEntity[] = [];
235-
for (const edge of getLineEdges()) {
236-
if (edge.target.uuid === node.uuid) {
237-
res.push(edge.source);
238-
}
239-
}
240-
return res;
241-
}
242-
243-
export function isConnected(node: ConnectableEntity, target: ConnectableEntity): boolean {
244-
for (const edge of getLineEdges()) {
245-
if (edge.source === node && edge.target === target) {
246-
return true;
247-
}
248-
}
249-
return false;
250-
}
251-
252218
/**
253219
* 更新节点的引用,将unknown的节点替换为真实的节点,保证对象在内存中的唯一性
254220
* 节点什么情况下会是unknown的?
@@ -354,16 +320,6 @@ export namespace StageManager {
354320
if (entities.length === 0) {
355321
return new Vector(Renderer.w, Renderer.h);
356322
}
357-
// const size = Vector.getZero();
358-
// for (const node of getEntities()) {
359-
// if (node.collisionBox.getRectangle().size.x > size.x) {
360-
// size.x = node.collisionBox.getRectangle().size.x;
361-
// }
362-
// if (node.collisionBox.getRectangle().size.y > size.y) {
363-
// size.y = node.collisionBox.getRectangle().size.y;
364-
// }
365-
// }
366-
// return size;
367323
const size = Rectangle.getBoundingRectangle(
368324
Array.from(entities.valuesToArray()).map((node) => node.collisionBox.getRectangle()),
369325
).size;
@@ -691,7 +647,7 @@ export namespace StageManager {
691647
}
692648
StageNodeConnector.connectConnectableEntity(fromNode, toNode);
693649
StageHistoryManager.recordStep();
694-
return isConnected(fromNode, toNode);
650+
return GraphMethods.isConnected(fromNode, toNode);
695651
}
696652

697653
export function reverseEdges(edges: LineEdge[]) {

src/core/stage/stageManager/basicMethods/GraphMethods.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export namespace GraphMethods {
88
return false;
99
}
1010
visited.push(node);
11-
for (const child of StageManager.nodeChildrenArray(node)) {
11+
for (const child of nodeChildrenArray(node)) {
1212
if (!dfs(child, visited)) {
1313
return false;
1414
}
@@ -18,4 +18,38 @@ export namespace GraphMethods {
1818

1919
return dfs(node, []);
2020
}
21+
22+
/** 获取节点连接的子节点数组,未排除自环 */
23+
export function nodeChildrenArray(node: ConnectableEntity): ConnectableEntity[] {
24+
const res: ConnectableEntity[] = [];
25+
for (const edge of StageManager.getLineEdges()) {
26+
if (edge.source.uuid === node.uuid) {
27+
res.push(edge.target);
28+
}
29+
}
30+
return res;
31+
}
32+
33+
/**
34+
* 获取一个节点的所有父亲节点,未排除自环
35+
* 性能有待优化!!
36+
*/
37+
export function nodeParentArray(node: ConnectableEntity): ConnectableEntity[] {
38+
const res: ConnectableEntity[] = [];
39+
for (const edge of StageManager.getLineEdges()) {
40+
if (edge.target.uuid === node.uuid) {
41+
res.push(edge.source);
42+
}
43+
}
44+
return res;
45+
}
46+
47+
export function isConnected(node: ConnectableEntity, target: ConnectableEntity): boolean {
48+
for (const edge of StageManager.getLineEdges()) {
49+
if (edge.source === node && edge.target === target) {
50+
return true;
51+
}
52+
}
53+
return false;
54+
}
2155
}

src/core/stage/stageManager/concreteMethods/StageEntityMoveManager.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Vector } from "../../../dataStruct/Vector";
22
import { ConnectableEntity } from "../../stageObject/abstract/ConnectableEntity";
33
import { Entity } from "../../stageObject/abstract/StageEntity";
4+
import { GraphMethods } from "../basicMethods/GraphMethods";
45
import { StageManager } from "../StageManager";
56

67
/**
@@ -106,7 +107,7 @@ export namespace StageEntityMoveManager {
106107

107108
function moveWithChildrenDfs(node: ConnectableEntity, delta: Vector, visitedUUIDs: string[]) {
108109
moveEntityUtils(node, delta);
109-
for (const child of StageManager.nodeChildrenArray(node)) {
110+
for (const child of GraphMethods.nodeChildrenArray(node)) {
110111
if (visitedUUIDs.includes(child.uuid)) {
111112
continue;
112113
}

src/core/stage/stageManager/concreteMethods/StageNodeConnector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LineEdge } from "../../stageObject/association/LineEdge";
55
import { ConnectPoint } from "../../stageObject/entity/ConnectPoint";
66
import { StageManager } from "../StageManager";
77
import { StageDeleteManager } from "./StageDeleteManager";
8+
import { GraphMethods } from "../basicMethods/GraphMethods";
89

910
/**
1011
* 集成所有连线相关的功能
@@ -20,7 +21,7 @@ export namespace StageNodeConnector {
2021
if (fromNode.uuid === toNode.uuid && fromNode instanceof ConnectPoint) {
2122
return false;
2223
}
23-
if (StageManager.isConnected(fromNode, toNode)) {
24+
if (GraphMethods.isConnected(fromNode, toNode)) {
2425
// 已经连接过了,不需要再次连接
2526
return false;
2627
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Section } from "../../stageObject/entity/Section";
55
import { SectionMethods } from "../basicMethods/SectionMethods";
66
import { StageManager } from "../StageManager";
77
import { StageSectionInOutManager } from "./StageSectionInOutManager";
8+
import { GraphMethods } from "../basicMethods/GraphMethods";
89

910
/**
1011
* 管理所有东西进出StageSection的逻辑
@@ -81,8 +82,8 @@ export namespace StageSectionPackManager {
8182
details: textNode.details,
8283
});
8384
// 获取所有连向它的和它连向的东西
84-
const fatherConnections = StageManager.nodeParentArray(textNode);
85-
const childConnections = StageManager.nodeChildrenArray(textNode);
85+
const fatherConnections = GraphMethods.nodeParentArray(textNode);
86+
const childConnections = GraphMethods.nodeChildrenArray(textNode);
8687

8788
// 删除原来的textNode
8889
StageManager.deleteEntities([textNode]);

src/core/stage/stageManager/concreteMethods/stageNodeRotate.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Vector } from "../../../dataStruct/Vector";
44
import { LineEffect } from "../../../service/feedbackService/effectEngine/concrete/LineEffect";
55
import { Stage } from "../../Stage";
66
import { ConnectableEntity } from "../../stageObject/abstract/ConnectableEntity";
7+
import { GraphMethods } from "../basicMethods/GraphMethods";
78
import { StageManager } from "../StageManager";
89
import { StageEntityMoveManager } from "./StageEntityMoveManager";
910

@@ -69,7 +70,7 @@ export namespace StageNodeRotate {
6970

7071
StageEntityMoveManager.moveEntityUtils(currentNode, centerToChildVectorRotated.subtract(centerToChildVector));
7172
// 再旋转子节点
72-
for (const child of StageManager.nodeChildrenArray(currentNode)) {
73+
for (const child of GraphMethods.nodeChildrenArray(currentNode)) {
7374
if (visitedUUIDs.includes(child.uuid)) {
7475
continue;
7576
}

0 commit comments

Comments
 (0)