@@ -21,6 +21,70 @@ import { Settings } from "../../Settings";
2121export class KeyboardOnlyTreeEngine {
2222 constructor ( private readonly project : Project ) { }
2323
24+ /**
25+ * 方向取反
26+ */
27+ private getOppositeDirection ( direction : Direction ) : Direction {
28+ switch ( direction ) {
29+ case Direction . Left :
30+ return Direction . Right ;
31+ case Direction . Right :
32+ return Direction . Left ;
33+ case Direction . Up :
34+ return Direction . Down ;
35+ case Direction . Down :
36+ return Direction . Up ;
37+ }
38+ }
39+
40+ /**
41+ * 将一条边整体设置为标准方向连线。
42+ * 例如向左时,源端点贴左侧,目标端点贴右侧。
43+ */
44+ private changeEdgeToDirection ( edge : Edge , direction : Direction ) {
45+ this . project . stageManager . changeEdgesConnectLocation ( [ edge ] , direction , true ) ;
46+ this . project . stageManager . changeEdgesConnectLocation ( [ edge ] , this . getOppositeDirection ( direction ) ) ;
47+ }
48+
49+ /**
50+ * 以指定节点为子树根,递归修改整棵子树的边方向,
51+ * 并额外调整指向该根节点的父边,最后触发一次树形自动布局。
52+ */
53+ private adjustSubtreeDirection ( root : ConnectableEntity , direction : Direction ) {
54+ const visitedNodeUUIDs = new Set < string > ( ) ;
55+
56+ const dfs = ( node : ConnectableEntity ) => {
57+ if ( visitedNodeUUIDs . has ( node . uuid ) ) return ;
58+ visitedNodeUUIDs . add ( node . uuid ) ;
59+
60+ const childEdges = this . project . graphMethods . edgeChildrenArray ( node ) ;
61+ for ( const edge of childEdges ) {
62+ this . changeEdgeToDirection ( edge , direction ) ;
63+ dfs ( edge . target ) ;
64+ }
65+ } ;
66+
67+ const parentEdges = this . project . graphMethods . edgeParentArray ( root ) ;
68+ for ( const edge of parentEdges ) {
69+ this . changeEdgeToDirection ( edge , direction ) ;
70+ }
71+
72+ dfs ( root ) ;
73+ this . project . autoAlign . autoLayoutSelectedFastTreeMode ( root ) ;
74+ }
75+
76+ /**
77+ * 对当前选中的所有可连接节点分别执行“以自身为根调整子树方向”。
78+ */
79+ public adjustSelectedSubtreesDirection ( direction : Direction ) {
80+ const selectedRoots = this . project . stageManager
81+ . getSelectedEntities ( )
82+ . filter ( ( entity ) => entity instanceof ConnectableEntity ) ;
83+ for ( const root of selectedRoots ) {
84+ this . adjustSubtreeDirection ( root , direction ) ;
85+ }
86+ }
87+
2488 /**
2589 * 获取节点的“预方向”
2690 * 如果有缓存,则拿缓存中的值,没有缓存,根据节点的入度线的方向,来判断“预方向”
0 commit comments