import type { JDesign } from "bp/content/json" ; interface IMigration { ( design ) : boolean ; } /**
 *  Migration 負責處理不同版本的檔案格式的遷移，以求向下相容。
  export namespace Migration { const migrations = [ ] ; export function $add ( migration , version ) { migrations . push ( [ migration , version ] ) ; } export function $getCurrentVersion ( ) { return migrations [ migrations . length - 1 ] [ 1 ] ; } export function $getSample ( ) { return { title : "" , version : $getCurrentVersion ( ) , mode : "layout" , layout : { sheet : { width : 16 , height : 16 } , flaps : [ ] , stretches : [ ] , } , tree : { sheet : { width : 20 , height : 20 } , nodes : [ ] , edges : [ ] , } , } ; } export function $process ( design ) { let i = 0 ; if ( 'version' in design ) { i = migrations . findIndex ( m => m [ 1 ] == design . version ) + 1 ; if ( i == 0 ) throw new Error ( "Unrecognized version" ) ; } let deprecate = false ; while ( i < migrations . length ) { deprecate ||= migrations [ i ++ ] [ 0 ] ( design ) ; } design . version = $getCurrentVersion ( ) ; if ( deprecate ) $onDeprecate ?. ( design . title ) ; return design as JDesign ; } /** 偵測到棄用格式的事件 callback */ export let $onDeprecate ; }