@@ -12,6 +12,24 @@ declare global {
12
12
}
13
13
}
14
14
15
+ // generate code based on component hierarchy and then return the rendered code
16
+ const generateCode = (
17
+ components : Component [ ] ,
18
+ componentId : number ,
19
+ rootComponents : number [ ] ,
20
+ projectType : string ,
21
+ HTMLTypes : HTMLType [ ]
22
+ ) => {
23
+ const code = generateUnformattedCode (
24
+ components ,
25
+ componentId ,
26
+ rootComponents ,
27
+ projectType ,
28
+ HTMLTypes
29
+ ) ;
30
+ return formatCode ( code ) ;
31
+ } ;
32
+
15
33
// generate code based on the component hierarchy
16
34
const generateUnformattedCode = (
17
35
comps : Component [ ] ,
@@ -23,14 +41,15 @@ const generateUnformattedCode = (
23
41
const components = [ ...comps ] ;
24
42
25
43
// find the component that we're going to generate code for
26
- const currentComponent = components . find ( elem => elem . id === componentId ) ;
44
+ const currComponent = components . find ( elem => elem . id === componentId ) ;
27
45
// find the unique components that we need to import into this component file
28
46
let imports : any = [ ] ;
29
47
let providers : string = '' ;
30
48
let context : string = '' ;
31
49
let links : boolean = false ;
32
50
33
51
const isRoot = rootComponents . includes ( componentId ) ;
52
+ let importReactRouter = false ; ;
34
53
35
54
// returns an array of objects which may include components, html elements, and/or route links
36
55
const getEnrichedChildren = ( currentComponent : Component | ChildElement ) => {
@@ -111,7 +130,7 @@ const generateUnformattedCode = (
111
130
112
131
// function to fix the spacing of the ace editor for new lines of added content. This was breaking on nested components, leaving everything right justified.
113
132
const tabSpacer = ( level : number ) => {
114
- let tabs = ''
133
+ let tabs = ' '
115
134
for ( let i = 0 ; i < level ; i ++ ) tabs += ' ' ;
116
135
return tabs ;
117
136
}
@@ -188,14 +207,12 @@ const generateUnformattedCode = (
188
207
const writeStateProps = ( stateArray : any ) => {
189
208
let stateToRender = '' ;
190
209
for ( const element of stateArray ) {
191
- stateToRender += levelSpacer ( 2 , 3 ) + element + ';'
210
+ stateToRender += levelSpacer ( 2 , 2 ) + element + ';'
192
211
}
193
212
return stateToRender
194
213
}
195
214
196
- const enrichedChildren : any = getEnrichedChildren ( currentComponent ) ;
197
-
198
- const next = true ;
215
+ const enrichedChildren : any = getEnrichedChildren ( currComponent ) ;
199
216
200
217
// import statements differ between root (pages) and regular components (components)
201
218
const importsMapped =
@@ -213,9 +230,6 @@ const generateUnformattedCode = (
213
230
} )
214
231
. join ( '\n' ) ;
215
232
216
- const stateful = true ;
217
- const classBased = false ;
218
- let importReactRouter ;
219
233
220
234
const createState = ( stateProps ) => {
221
235
let state = '{' ;
@@ -229,10 +243,10 @@ const generateUnformattedCode = (
229
243
return state ;
230
244
}
231
245
// check for context
232
- if ( currentComponent . useContext ) {
246
+ if ( currComponent . useContext ) {
233
247
234
- for ( const providerId of Object . keys ( currentComponent . useContext ) ) {
235
- const attributesAndStateIds = currentComponent . useContext [ String ( providerId ) ] ; //currently just from App
248
+ for ( const providerId of Object . keys ( currComponent . useContext ) ) {
249
+ const attributesAndStateIds = currComponent . useContext [ String ( providerId ) ] ; //currently just from App
236
250
const providerComponent = components [ providerId - 1 ] ;
237
251
providers += 'const ' + providerComponent . name . toLowerCase ( ) + 'Context = useContext(' + providerComponent . name + 'Context);\n' ;
238
252
@@ -253,57 +267,37 @@ const generateUnformattedCode = (
253
267
// classic react code
254
268
if ( projectType === 'Classic React' ) {
255
269
return `
256
- ${ stateful && ! classBased ? `import React, { useState, createContext, useContext } from 'react';` : '' }
270
+ ${ `import React, { useState, createContext, useContext } from 'react';` }
257
271
${ importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';` : `` }
258
- ${ classBased ? `import React, { Component } from 'react';` : '' }
259
- ${ ! stateful && ! classBased ? `import React, { createContext, useContext } from 'react';` : '' }
260
272
${ importsMapped }
261
273
${ providers }
262
274
${ context }
275
+ ${ `const ${ currComponent . name } = (props: any): JSX.Element => {` }
276
+ ${ ` const [value, setValue] = useState<any | undefined>("INITIAL VALUE");${ writeStateProps ( currComponent . useStateCodes ) } ` }
263
277
${
264
- classBased
265
- ? `class ${ currentComponent . name } extends Component {`
266
- : `const ${ currentComponent . name } = (props: any): JSX.Element => {`
267
- }
268
- ${
269
- stateful && ! classBased
270
- ? `const [value, setValue] = useState<any | undefined>("INITIAL VALUE");${ writeStateProps ( currentComponent . useStateCodes ) }
271
- `
272
- : ``
273
- }
274
- ${
275
- isRoot && currentComponent . stateProps . length !== 0
276
- ? `const ${ currentComponent . name } Context = createContext(${ createState ( currentComponent . stateProps ) } );`
278
+ isRoot && currComponent . stateProps . length !== 0
279
+ ? ` const ${ currComponent . name } Context = createContext(${ createState ( currComponent . stateProps ) } );`
277
280
: ``
278
-
279
- }
280
- ${
281
- classBased && stateful
282
- ? `constructor(props) {
283
- super(props);
284
- this.state = {}
285
- }`
286
- : ``
287
281
}
288
- ${ classBased ? `render(): JSX.Element {` : `` }
289
- ${ ! importReactRouter ?
290
- `return (
291
- <${ currentComponent . name } Context.Provider value="">
292
- <div className="${ currentComponent . name } " ${ formatStyles ( currentComponent ) } >
282
+ ${ ! importReactRouter
283
+ ? ` return (
284
+ <${ currComponent . name } Context.Provider value="">
285
+ <div className="${ currComponent . name } " ${ formatStyles ( currComponent ) } >
293
286
${ writeNestedElements ( enrichedChildren ) }
294
- </div>
295
- </${ currentComponent . name } Context.Provider>
296
- );` : `return (
297
- <${ currentComponent . name } Context.Provider value="">
298
- <Router>
299
- <div className="${ currentComponent . name } " ${ formatStyles ( currentComponent ) } >
300
- ${ writeNestedElements ( enrichedChildren ) }
301
287
</div>
302
- </Router>
303
- </${ currentComponent . name } Context.Provider>
304
- );` }
305
- ${ `}` }
306
- export default ${ currentComponent . name } ;
288
+ </${ currComponent . name } Context.Provider>
289
+ );`
290
+ : ` return (
291
+ <${ currComponent . name } Context.Provider value="">
292
+ <Router>
293
+ <div className="${ currComponent . name } " ${ formatStyles ( currComponent ) } >
294
+ ${ ` ` + writeNestedElements ( enrichedChildren ) }
295
+ </div>
296
+ </Router>
297
+ </${ currComponent . name } Context.Provider>
298
+ );` }
299
+ ${ `}\n` }
300
+ export default ${ currComponent . name } ;
307
301
` ;
308
302
}
309
303
// next.js component code
@@ -314,7 +308,7 @@ const generateUnformattedCode = (
314
308
import Head from 'next/head'
315
309
${ links ? `import Link from 'next/link'` : `` }
316
310
317
- const ${ currentComponent . name } = (props): JSX.Element => {
311
+ const ${ currComponent . name } = (props): JSX.Element => {
318
312
319
313
const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
320
314
@@ -323,18 +317,18 @@ const generateUnformattedCode = (
323
317
${
324
318
isRoot
325
319
? `<Head>
326
- <title>${ currentComponent . name } </title>
320
+ <title>${ currComponent . name } </title>
327
321
</Head>`
328
322
: ``
329
323
}
330
- <div className="${ currentComponent . name } " style={props.style}>
324
+ <div className="${ currComponent . name } " style={props.style}>
331
325
${ writeNestedElements ( enrichedChildren ) }
332
326
</div>
333
327
</>
334
328
);
335
329
}
336
330
337
- export default ${ currentComponent . name } ;
331
+ export default ${ currComponent . name } ;
338
332
` ;
339
333
} else {
340
334
// gatsby component code
@@ -345,7 +339,7 @@ const generateUnformattedCode = (
345
339
${ links ? `import { Link } from 'gatsby'` : `` }
346
340
347
341
348
- const ${ currentComponent . name } = (props: any): JSX.Element => {
342
+ const ${ currComponent . name } = (props: any): JSX.Element => {
349
343
350
344
const[value, setValue] = useState<any | undefined>("INITIAL VALUE");
351
345
@@ -354,18 +348,18 @@ const generateUnformattedCode = (
354
348
${
355
349
isRoot
356
350
? `<head>
357
- <title>${ currentComponent . name } </title>
351
+ <title>${ currComponent . name } </title>
358
352
</head>`
359
353
: ``
360
354
}
361
- <div className="${ currentComponent . name } " style={props.style}>
355
+ <div className="${ currComponent . name } " style={props.style}>
362
356
${ writeNestedElements ( enrichedChildren ) }
363
357
</div>
364
358
</>
365
359
);
366
360
}
367
361
368
- export default ${ currentComponent . name } ;
362
+ export default ${ currComponent . name } ;
369
363
` ;
370
364
}
371
365
} ;
@@ -390,22 +384,6 @@ const formatCode = (code: string) => {
390
384
}
391
385
} ;
392
386
393
- // generate code based on component hierarchy and then return the rendered code
394
- const generateCode = (
395
- components : Component [ ] ,
396
- componentId : number ,
397
- rootComponents : number [ ] ,
398
- projectType : string ,
399
- HTMLTypes : HTMLType [ ]
400
- ) => {
401
- const code = generateUnformattedCode (
402
- components ,
403
- componentId ,
404
- rootComponents ,
405
- projectType ,
406
- HTMLTypes
407
- ) ;
408
- return formatCode ( code ) ;
409
- } ;
387
+
410
388
411
389
export default generateCode ;
0 commit comments