import { BaseContainer } from "./BaseContainer" ; import { Edge } from "../components" ; import type { TreeEdge } from "bp/content/context" ; import type { Design } from ".." ; import type { JEdge } from "bp/content/json" ; /**
 *  EdgeContainer 映射  TreeEdge 成  Edge 。
  @ shrewd export class EdgeContainer extends BaseContainer < TreeEdge , Edge > { constructor ( design ) { super ( design , ( ) => design . $tree . $edge . values ( ) , e => new Edge ( design . $TreeSheet , design . $vertices . get ( e . n1 ) ! , design . $vertices . get ( e . n2 ) ! , e ) ) ; } /**
     * 把  EdgeContainer . toJSON 產出的  JEdge [] 做一個排序，
     * 使得從第一條邊開始逐一加入邊都能維持連通性，且 parent 的方向正確。
      public $sort ( ) { let edges = this . toJSON ( ) ; let nodes = new Set < number > ( ) ; let result = [ ] ; while ( edges . length ) { let e = edges . shift ( ) ! ; if ( nodes . size == 0 || nodes . has ( e . n1 ) ) { result . push ( e ) ; nodes . add ( e . n1 ) ; nodes . add ( e . n2 ) ; } else if ( result [ 0 ] . n1 == e . n2 ) { result . unshift ( e ) ; nodes . add ( e . n1 ) ; } else { edges . push ( e ) ; } } return result ; } public $toRiver ( edge ) { this . _design . $LayoutSheet . $clearSelection ( ) ; let te = edge . $edge ; if ( te . $isRiver ) { let r = this . _design . $rivers . get ( te ) ; if ( r ) r . $selected = true ; } else { let n = te . n1 . $degree == 1 ? te . n1 : te . n2 ; let f = this . _design . $flaps . get ( n ) ; if ( f ) f . $selected = true ; } this . _design . mode = "layout" ; } }