import exampleCode from "components/ui/code-panel/example-code" ; import { ICanvasItems , IData } from "types" ; param data function moveGlobOptionsOntoGlob ( data ) { const { globIds , globs } = data ; for ( const globId of globIds ) { const glob = globs [ globId ] ; if ( glob . options !== undefined ) { Object . assign ( glob , glob . options ) ; delete glob . options ; } } } param data function createDocument ( data ) { data . version = "2" ; data . pageId = "0" ; data . pages = { 0 : { id : "0" , name : "Page 1" , type : ICanvasItems . Page , locked : false , childIndex : 0 , } , } ; data . code = { 0 : { id : "0" , childIndex : 0 , name : "My Code" , code : exampleCode , } , } ; data . groups = { } ; const { globIds , globs , nodeIds , nodes } = data ; let i = 0 ; for ( const nodeId of nodeIds ) { const node = nodes [ nodeId ] ; node . type = ICanvasItems . Node ; node . parentId = "0" ; node . childIndex = i ; delete node . zIndex ; i ++ ; } for ( const globId of globIds ) { const glob = globs [ globId ] ; glob . type = ICanvasItems . Glob ; glob . parentId = "0" ; glob . childIndex = i ; delete glob . zIndex ; i ++ ; } let saved ; saved = localStorage . getItem ( "globs_editor_theme" ) ; if ( saved !== null ) { localStorage . removeItem ( "globs_editor_theme" ) ; } saved = localStorage . getItem ( "__globs_code" ) ; if ( saved !== null ) { try { const json = JSON . parse ( saved ) ; data . code [ "0" ] . code = json . code ; data . codePanel . fontSize = json . style . fontSize ; } catch ( e ) { console . warn ( "Could not parse code." ) ; } localStorage . removeItem ( "__globs_code" ) ; } } function supportShareUrls ( data ) { if ( "shareUrl" in data ) { data . shareUrls = [ data . shareUrl ] ; delete data . shareUrl ; } } function addPreferences ( data ) { data . preferences = { theme : data . theme , nudgeDistanceSmall : 1 , nudgeDistanceLarge : 10 , } ; delete data . theme ; } export default function migrate ( data ) { if ( ! ( "version" in data ) || Number ( data . version ) < 1 ) { if ( data . globIds . length > 0 && "options" in data . globs [ data . globIds [ 0 ] ] ) { moveGlobOptionsOntoGlob ( data ) ; } data . version = "1" ; } if ( Number ( data . version ) < 2 ) { createDocument ( data ) ; data . version = "2" ; } if ( Number ( data . version ) < 3 ) { supportShareUrls ( data ) ; data . version = "3" ; } if ( Number ( data . version ) < 4 ) { addPreferences ( data ) ; data . version = "4" ; } return data ; }