import _ from './util' ; import cache from './cache' ; import { RendererInterface , Renderer } from './store/render' ; import { GetterInterface , Getters } from './store/getters' ; import { MutationInterface , Mutations } from './store/mutations' ; import LDParse from './ld_parse' ; import LDRender from './ld_render' ; import defaultTemplate from './template' ; import Storage from './storage' ; import packageInfo from '../package.json' ; const emptyState = { annotations : [ ] , books : [ ] , calloutArrows : [ ] , callouts : [ ] , csis : [ ] , dividers : [ ] , licFilename : null , numberLabels : [ ] , pages : [ ] , pliItems : [ ] , pliTransforms : { } , plis : [ ] , plisVisible : true , points : [ ] , quantityLabels : [ ] , rotateIcons : [ ] , steps : [ ] , submodelImages : [ ] , template : _ . cloneDeep ( defaultTemplate ) , } ; export interface Store { version : string | null ; model : Model | null ; setModel ( model ) : void ; state : StateInterface ; replaceState ( state ) : void ; resetState ( ) : void ; load ( content ) : void ; saveLocal ( ) : void ; saveToFile ( filename ? , jsonIndent ? ) : void ; saveTemplate ( filename ? , jsonIndent ? ) : void ; render : RendererInterface ; get : GetterInterface ; mutations : MutationInterface ; } const store = { version : null , model : null , setModel ( model ) { store . model = model ; LDRender . setModel ( model ) ; store . state . licFilename = store . get . modelFilenameBase ( ) ; } , state : _ . cloneDeep ( emptyState ) , replaceState ( state ) { store . state = state ; cache . reset ( ) ; } , resetState ( ) { if ( store . model ) { delete LDParse . partDictionary [ store . model . filename ] ; } store . model = null ; store . state = _ . cloneDeep ( emptyState ) ; cache . reset ( ) ; } , load ( content ) { LDParse . setPartDictionary ( content . partDictionary ) ; LDParse . setColorTable ( content . colorTable ) ; store . model = LDParse . partDictionary [ content . modelFilename ] ; LDRender . setModel ( LDParse . partDictionary [ content . modelFilename ] ) ; LDRender . setRenderState ( content . state . template . sceneRendering ) ; store . replaceState ( content . state ) ; } , saveLocal ( ) { const content = getSaveContent ( ) ; console . log ( 'Updating local storage' ) ; Storage . replace . model ( content ) ; } , saveToFile ( filename ? , jsonIndent ? ) { const content = getSaveContent ( ) ; filename = ( filename || store . state . licFilename || 'filename' ) + '.lic' ; saveJSON ( content , filename , jsonIndent ) ; } , saveTemplate ( filename ? , jsonIndent ? ) { const content = { version : packageInfo . version , template : store . state . template , } ; filename = ( filename || store . state . licFilename || 'filename' ) + '.lit' ; saveJSON ( content , filename , jsonIndent ) ; } , render : Renderer , get : Getters , mutations : Mutations , } ; function getSaveContent ( ) { return { version : packageInfo . version , partDictionary : LDParse . partDictionary , colorTable : LDParse . colorTable , modelFilename : store ?. model ?. filename ?? '' , state : store . state , } ; } function saveJSON ( json , filename , jsonIndent ? ) { const content = JSON . stringify ( json , null , jsonIndent ) ; const blob = new Blob ( [ content ] , { type : 'text/plain;charset=utf-8' } ) ; saveAs ( blob , filename ) ; } export default store ;