@@ -52,120 +52,31 @@ export class Codegen {
5252 )
5353 }
5454
55- async addComponent ( node : ComponentNode ) {
56- const childrenCodes =
57- 'children' in node
58- ? node . children . map ( async ( child ) => {
59- if ( child . type === 'INSTANCE' ) {
60- const mainComponent = await child . getMainComponentAsync ( )
61- if ( mainComponent ) await this . addComponent ( mainComponent )
62- }
55+ /**
56+ * Run the codegen process: build tree and render to JSX string.
57+ */
58+ async run ( node : SceneNode = this . node , depth : number = 0 ) : Promise < string > {
59+ // Build the tree first
60+ const tree = await this . buildTree ( node )
6361
64- return await this . run ( child , 0 )
65- } )
66- : [ ]
67- const props = await getProps ( node )
68- const selectorProps = await getSelectorProps ( node )
69- const variants = { }
62+ // Render the tree to JSX string
63+ const ret = Codegen . renderTree ( tree , depth )
7064
71- if ( selectorProps ) {
72- Object . assign ( props , selectorProps . props )
73- Object . assign ( variants , selectorProps . variants )
65+ if ( node === this . node ) {
66+ this . code = ret
67+ this . tree = tree
7468 }
7569
76- this . components . set ( node , {
77- code : renderNode (
78- getDevupComponentByProps ( props ) ,
79- props ,
80- 2 ,
81- await Promise . all ( childrenCodes ) ,
82- ) ,
83- variants,
84- } )
85- }
86-
87- async run ( node : SceneNode = this . node , dep : number = 0 ) : Promise < string > {
88- const assetNode = checkAssetNode ( node )
89- if ( assetNode ) {
90- const props = await getProps ( node )
91- props . src = `/${ assetNode === 'svg' ? 'icons' : 'images' } /${ node . name } .${ assetNode } `
92- if ( assetNode === 'svg' ) {
93- const maskColor = await checkSameColor ( node )
94- if ( maskColor ) {
95- // support mask image icon
96- props . maskImage = buildCssUrl ( props . src as string )
97- props . maskRepeat = 'no-repeat'
98- props . maskSize = 'contain'
99- props . bg = maskColor
100- delete props . src
101- }
70+ // Sync componentTrees to components
71+ for ( const [ compNode , compTree ] of this . componentTrees ) {
72+ if ( ! this . components . has ( compNode ) ) {
73+ this . components . set ( compNode , {
74+ code : Codegen . renderTree ( compTree . tree , 2 ) ,
75+ variants : compTree . variants ,
76+ } )
10277 }
103- const ret = renderNode ( 'src' in props ? 'Image' : 'Box' , props , dep , [ ] )
104- if ( node === this . node ) this . code = ret
105- return ret
10678 }
10779
108- const props = await getProps ( node )
109- if (
110- ( node . type === 'COMPONENT_SET' || node . type === 'COMPONENT' ) &&
111- ( ( this . node . type === 'COMPONENT_SET' &&
112- node === this . node . defaultVariant ) ||
113- this . node . type === 'COMPONENT' )
114- ) {
115- await this . addComponent (
116- node . type === 'COMPONENT_SET' ? node . defaultVariant : node ,
117- )
118- }
119- if ( node . type === 'INSTANCE' ) {
120- const mainComponent = await node . getMainComponentAsync ( )
121- if ( mainComponent ) await this . addComponent ( mainComponent )
122- let ret = renderNode ( getComponentName ( mainComponent || node ) , { } , dep , [ ] )
123- if ( props . pos ) {
124- ret = renderNode (
125- 'Box' ,
126- {
127- pos : props . pos ,
128- top : props . top ,
129- left : props . left ,
130- right : props . right ,
131- bottom : props . bottom ,
132- w :
133- // if the node is a page root, set the width to 100%
134- ( getPageNode ( node as BaseNode & ChildrenMixin ) as SceneNode )
135- ?. width === node . width
136- ? '100%'
137- : undefined ,
138- } ,
139- dep ,
140- [ ret ] ,
141- )
142- }
143- if ( node === this . node ) this . code = ret
144- return ret
145- }
146- const childrenCodes = await Promise . all (
147- 'children' in node
148- ? node . children . map ( async ( child ) => {
149- if ( child . type === 'INSTANCE' ) {
150- const mainComponent = await child . getMainComponentAsync ( )
151- if ( mainComponent ) await this . addComponent ( mainComponent )
152- }
153- return await this . run ( child )
154- } )
155- : [ ] ,
156- )
157- if ( node . type === 'TEXT' ) {
158- const { children, props : _props } = await renderText ( node )
159- childrenCodes . push ( ...children )
160- Object . assign ( props , _props )
161- }
162- const ret = renderNode (
163- getDevupComponentByNode ( node , props ) ,
164- props ,
165- dep ,
166- childrenCodes ,
167- )
168- if ( node === this . node ) this . code = ret
16980 return ret
17081 }
17182
@@ -188,7 +99,6 @@ export class Codegen {
18899 props . bg = maskColor
189100 delete props . src
190101 }
191- props . display = 'initial'
192102 }
193103 return {
194104 component : 'src' in props ? 'Image' : 'Box' ,
@@ -372,8 +282,9 @@ export class Codegen {
372282 return renderNode ( tree . component , tree . props , depth , tree . textChildren )
373283 }
374284
285+ // Children are rendered with depth 0 because renderNode handles indentation internally
375286 const childrenCodes = tree . children . map ( ( child ) =>
376- Codegen . renderTree ( child , depth + 1 ) ,
287+ Codegen . renderTree ( child , 0 ) ,
377288 )
378289 return renderNode ( tree . component , tree . props , depth , childrenCodes )
379290 }
0 commit comments