import { Document , FileResponse , Style , ComponentMetadata , Node , } from "figma-js" ; import { groupNodes } from "./utils" ; import { ProcessedFile } from "./types" ; export function processFile ( data , id ? ) { const { name , lastModified , thumbnailUrl , version , document , styles , components , } = data ; const [ processedNodes , processedShortcuts ] = processNodes ( document , styles , components , id ) ; return { fileId : id , name , lastModified , thumbnailUrl , version , children : processedNodes [ 0 ] . children , shortcuts : groupNodes ( processedShortcuts ) , } ; } export function processNodes ( nodes , documentStyles , components , fileId ? ) { const parsedStyles = new Map ( Object . entries ( documentStyles ) ) ; const parsedComponents = new Map ( Object . entries ( components ) ) ; const traverseChildren = ( node , parentId ) => { const { id , styles , children , ... rest } = node ; let nodeStyles = [ ] ; if ( styles != null ) { nodeStyles = Object . entries ( styles ) . map ( ( [ key , styleId ] ) => { const documentStyle = parsedStyles . get ( styleId as string ) ; return { id : styleId , ... documentStyle , styles : node [ `${ key }s` ] , textStyles : node . style , type : "STYLE" , } ; } ) ; } if ( children == null || children . length === 0 ) { return [ [ { id , parentId , fileId , ... rest } ] , nodeStyles ] ; } const [ parsedChildren , shortcuts ] = children . reduce ( ( acc , child ) => { const [ accChildren , accShortcuts ] = acc ; const [ tChildren , tShortcuts ] = traverseChildren ( child , id ) ; return [ [ ... accChildren , ... tChildren ] , [ ... accShortcuts , ... tChildren , ... tShortcuts ] , ] ; } , [ [ ] , [ ] ] ) ; const componentInfo = parsedComponents . get ( id ) ; const parsedNode = { id , parentId , fileId , ... rest , ... ( componentInfo && componentInfo ) , children : parsedChildren , shortcuts : groupNodes ( shortcuts ) , } ; return [ [ parsedNode ] , shortcuts ] ; } ; return traverseChildren ( nodes , "0:0" ) ; } export * from "./types" ;