import { SGMark , SGNodeType } from '@chart-parts/interfaces' ; import { Mark , Item , GroupItem } from './elements' ; import { createItemType } from './registry' ; const KNOWN_KEYS = [ 'marktype' , 'name' , 'role' , 'interactive' , 'clip' , 'items' , 'zindex' , 'x' , 'y' , 'width' , 'height' , 'align' , 'baseline' , 'fill' , 'fillOpacity' , 'opacity' , 'stroke' , 'strokeOpacity' , 'strokeWidth' , 'strokeCap' , 'strokeDash' , 'strokeDashOffset' , 'startAngle' , 'endAngle' , 'innerRadius' , 'outerRadius' , 'cornerRadius' , 'padAngle' , 'interpolate' , 'tension' , 'orient' , 'defined' , 'url' , 'path' , 'x2' , 'y2' , 'size' , 'shape' , 'text' , 'angle' , 'theta' , 'radius' , 'dx' , 'dy' , 'font' , 'fontSize' , 'fontWeight' , 'fontStyle' , 'fontVariant' , ] ; export function sceneToJSON ( scene , indent ? ) { return JSON . stringify ( scene , KNOWN_KEYS , indent ) ; } export function parseScene ( json ) { const scene = typeof json === 'string' ? JSON . parse ( json ) : json ; return initializeMark ( scene ) as Mark < any > ; } param rawMark function initializeMark ( rawMark ) { const { marktype , role } = rawMark ; const result = new Mark ( ) ; result . marktype = marktype ; result . role = role ; result . items = ( rawMark . items || [ ] ) . map ( ( rawItem ) => { const parentType = marktype ? SGNodeType . Mark : SGNodeType . Item ; const item = parentType === SGNodeType . Mark ? initializeItem ( marktype , rawItem ) : initializeMark ( rawItem ) ; item . parent = result ; item . parentType = parentType ; return item ; } ) ; if ( Object . prototype . hasOwnProperty . call ( rawMark , 'clip' ) ) { result . clip = rawMark . clip ; } if ( Object . prototype . hasOwnProperty . call ( rawMark , 'interactive' ) ) { result . interactive = rawMark . interactive ; } return result ; } param marktype param rawItem function initializeItem ( marktype , rawItem ) { const result = createItemType ( marktype , rawItem ) ; if ( result . itemtype === GroupItem . ITEM_TYPE ) { const group = result as GroupItem ; const items = ( rawItem . items || [ ] ) . map ( ( item ) => initializeMark ( item ) ) ; group . items = items ; } return result ; }