import { CornerType } from "bp/content/json" ; import type { JConfiguration , JCorner , JDesign , JGadget , JOverlap , JPartition , JPattern , JStretch , Strategy } from "bp/content/json" ; /** rc1 版本中在資料結構上插入了  Partition 和  Device 的層次  export default function $process ( design ) { let st = design . layout ?. stretches as Pseudo < JStretch > [ ] ; if ( st ) { for ( let s of st . concat ( ) ) { let cf = s . configuration as Pseudo < JConfiguration > ; if ( cf ) pattern ( cf , s ) ; } } return false ; } function pattern ( cf , s ) { s . configuration = { partitions : partition ( cf . overlaps as JOverlap [ ] , cf . strategy as Strategy ) , } ; let pt = s . pattern as Pseudo < JPattern > ; if ( pt ) { let offsets = pt . offsets as number [ ] | undefined ; if ( s . configuration . partitions ! . length == 1 ) { s . pattern = { devices : [ { gadgets : pt . gadgets as JGadget [ ] , offset : offsets ?. [ 0 ] , } ] , } ; } else { s . pattern = { devices : ( pt . gadgets as JGadget [ ] ) . map ( ( g , i ) => ( { gadgets : [ g ] , offset : offsets ?. [ i ] , } ) ) , } ; } } } function cornerFilter ( c ) { return c . type == CornerType . $coincide ; } function partition ( overlaps , strategy ? ) { let partitions = [ ] ; let partitionMap = new Map < number , number > ( ) ; for ( let [ i , o ] of overlaps . entries ( ) ) { if ( partitionMap . has ( i ) ) continue ; let coins = o . c . filter ( cornerFilter ) ; let coin = coins . find ( c => partitionMap . has ( - c . e ! - 1 ) ) ; let j = partitions . length ; if ( coin ) { let k = partitionMap . get ( - coin . e ! - 1 ) ! ; partitionMap . set ( i , k ) ; partitions [ k ] . push ( o ) ; } else { partitionMap . set ( i , j ) ; partitions . push ( [ o ] ) ; } coins . forEach ( c => { i = - c . e ! - 1 ; if ( ! partitionMap . has ( i ) ) { partitionMap . set ( i , j ) ; partitions [ j ] . push ( overlaps [ i ] ) ; } } ) ; } return partitions . map < JPartition > ( p => ( { overlaps : p , strategy } ) ) ; }