@@ -92,8 +92,8 @@ const generateUnformattedCode = (
92
92
) . name ;
93
93
return child ;
94
94
}
95
- } ) ;
96
- return enrichedChildren ;
95
+ } ) ;
96
+ return enrichedChildren ;
97
97
} ;
98
98
// Raised formatStyles so that it is declared before it is referenced. It was backwards.
99
99
// format styles stored in object to match React inline style format
@@ -102,7 +102,7 @@ const generateUnformattedCode = (
102
102
const formattedStyles = [ ] ;
103
103
let styleString ;
104
104
for ( let i in styleObj ) {
105
- if ( i === 'style' ) {
105
+ if ( i === 'style' ) {
106
106
styleString = i + '=' + '{' + JSON . stringify ( styleObj [ i ] ) + '}' ;
107
107
formattedStyles . push ( styleString ) ;
108
108
}
@@ -116,7 +116,7 @@ const generateUnformattedCode = (
116
116
if ( childElement . attributes && childElement . attributes . cssClasses ) {
117
117
customizationDetails += ( ' ' + `className="${ childElement . attributes . cssClasses } "` ) ;
118
118
}
119
- if ( childElement . style && Object . keys ( childElement . style ) . length > 0 ) customizationDetails += ( ' ' + formatStyles ( childElement ) ) ;
119
+ if ( childElement . style && Object . keys ( childElement . style ) . length > 0 ) customizationDetails += ( ' ' + formatStyles ( childElement ) ) ;
120
120
return customizationDetails ;
121
121
} ;
122
122
// 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.
@@ -127,7 +127,7 @@ const generateUnformattedCode = (
127
127
}
128
128
// function to dynamically generate the appropriate levels for the code preview
129
129
const levelSpacer = ( level : number , spaces : number ) => {
130
- if ( level === 2 ) return `\n${ tabSpacer ( spaces ) } ` ;
130
+ if ( level === 2 ) return `\n${ tabSpacer ( spaces ) } ` ;
131
131
else return ''
132
132
}
133
133
// function to dynamically generate a complete html (& also other library type) elements
@@ -145,17 +145,17 @@ const generateUnformattedCode = (
145
145
if ( childElement . stateUsed && childElement . stateUsed . compLink ) {
146
146
activeLink = '{' + childElement . stateUsed . compLink + '}' ;
147
147
} else {
148
- activeLink = '"' + childElement . attributes . compLink + '"' ;
148
+ activeLink = '"' + childElement . attributes . compLink + '"' ;
149
149
}
150
150
}
151
151
const nestable = childElement . tag === 'div' ||
152
- childElement . tag === 'form' ||
153
- childElement . tag === 'ol' ||
154
- childElement . tag === 'ul' ||
155
- childElement . tag === 'menu' ||
156
- childElement . tag === 'li' ||
157
- childElement . tag === 'Switch' ||
158
- childElement . tag === 'Route' ;
152
+ childElement . tag === 'form' ||
153
+ childElement . tag === 'ol' ||
154
+ childElement . tag === 'ul' ||
155
+ childElement . tag === 'menu' ||
156
+ childElement . tag === 'li' ||
157
+ childElement . tag === 'Switch' ||
158
+ childElement . tag === 'Route' ;
159
159
160
160
if ( childElement . tag === 'img' ) {
161
161
return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } src=${ activeLink } ${ elementTagDetails ( childElement ) } />${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
@@ -172,34 +172,34 @@ const generateUnformattedCode = (
172
172
return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } ${ routePath } >${ innerText }
173
173
${ tabSpacer ( level ) } ${ writeNestedElements ( childElement . children , level + 1 ) }
174
174
${ tabSpacer ( level - 1 ) } </${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
175
- } else if ( childElement . tag !== 'separator' ) {
175
+ } else if ( childElement . tag !== 'separator' ) {
176
176
return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText } </${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
177
177
}
178
178
}
179
179
// write all code that will be under the "return" of the component
180
180
const writeNestedElements = ( enrichedChildren : any , level : number = 2 ) => {
181
181
return `${ enrichedChildren
182
- . map ( ( child : any ) => {
183
- if ( child . type === 'Component' ) {
184
- return `<${ child . name } ${ elementTagDetails ( child ) } />` ;
185
- } else if ( child . type === 'HTML Element' ) {
186
- return elementGenerator ( child , level ) ;
187
- }
188
- // route links are for gatsby.js and next.js feature. if the user creates a route link and then switches projects, generate code for a normal link instead
189
- else if ( child . type === 'Route Link' ) {
190
- if ( projectType === 'Next.js' ) {
191
- // if route link points to index, to go endpoint / rather than /index
192
- if ( child . name === 'index' ) return `<div><Link href="/"><a>${ child . name } </a></Link></div>` ;
193
- else return `<div><Link href="/${ child . name } "><a>${ child . name } </a></Link></div>` ;
194
- } else if ( projectType === 'Gatsby.js' ) {
195
- if ( child . name === 'index' ) return `<div><Link to="/">${ child . name } </Link></div>` ;
196
- else return `<div><Link to="/${ child . name } ">${ child . name } </Link></div>` ;
197
- } else return `<div><a>${ child . name } </a></div>`
198
- }
199
- } )
200
- . filter ( element => ! ! element )
201
- . join ( '' )
202
- } `;
182
+ . map ( ( child : any ) => {
183
+ if ( child . type === 'Component' ) {
184
+ return `<${ child . name } ${ elementTagDetails ( child ) } />` ;
185
+ } else if ( child . type === 'HTML Element' ) {
186
+ return elementGenerator ( child , level ) ;
187
+ }
188
+ // route links are for gatsby.js and next.js feature. if the user creates a route link and then switches projects, generate code for a normal link instead
189
+ else if ( child . type === 'Route Link' ) {
190
+ if ( projectType === 'Next.js' ) {
191
+ // if route link points to index, to go endpoint / rather than /index
192
+ if ( child . name === 'index' ) return `<div><Link href="/"><a>${ child . name } </a></Link></div>` ;
193
+ else return `<div><Link href="/${ child . name } "><a>${ child . name } </a></Link></div>` ;
194
+ } else if ( projectType === 'Gatsby.js' ) {
195
+ if ( child . name === 'index' ) return `<div><Link to="/">${ child . name } </Link></div>` ;
196
+ else return `<div><Link to="/${ child . name } ">${ child . name } </Link></div>` ;
197
+ } else return `<div><a>${ child . name } </a></div>`
198
+ }
199
+ } )
200
+ . filter ( element => ! ! element )
201
+ . join ( '' )
202
+ } `;
203
203
} ;
204
204
// function to properly incorporate the user created state that is stored in the application state
205
205
const writeStateProps = ( stateArray : any ) => {
@@ -214,17 +214,17 @@ const generateUnformattedCode = (
214
214
const importsMapped =
215
215
projectType === 'Next.js' || projectType === 'Gatsby.js'
216
216
? imports
217
- . map ( ( comp : string ) => {
218
- return isRoot
219
- ? `import ${ comp } from '../components/${ comp } '`
220
- : `import ${ comp } from './${ comp } '` ;
221
- } )
222
- . join ( '\n' )
217
+ . map ( ( comp : string ) => {
218
+ return isRoot
219
+ ? `import ${ comp } from '../components/${ comp } '`
220
+ : `import ${ comp } from './${ comp } '` ;
221
+ } )
222
+ . join ( '\n' )
223
223
: imports
224
- . map ( ( comp : string ) => {
225
- return `import ${ comp } from './${ comp } '` ;
226
- } )
227
- . join ( '\n' ) ;
224
+ . map ( ( comp : string ) => {
225
+ return `import ${ comp } from './${ comp } '` ;
226
+ } )
227
+ . join ( '\n' ) ;
228
228
const createState = ( stateProps ) => {
229
229
let state = '{' ;
230
230
stateProps . forEach ( ( ele ) => {
@@ -235,27 +235,27 @@ const generateUnformattedCode = (
235
235
}
236
236
// Generate import
237
237
let importContext = '' ;
238
- if ( currComponent . useContext ) {
238
+ if ( currComponent . useContext ) {
239
239
for ( const providerId of Object . keys ( currComponent . useContext ) ) {
240
240
const providerComponent = components [ parseInt ( providerId ) - 1 ] ;
241
- importContext += `import ${ providerComponent . name } Context from './${ providerComponent . name } .tsx'\n \t\t` ;
241
+ importContext += `import ${ providerComponent . name } Context from './${ providerComponent . name } .tsx'\n \t\t` ;
242
242
}
243
243
}
244
244
if ( currComponent . useContext ) {
245
245
for ( const providerId of Object . keys ( currComponent . useContext ) ) {
246
246
const statesFromProvider = currComponent . useContext [ parseInt ( providerId ) ] . statesFromProvider ; //{1: {Set, compLink, compText}, 2 : {}... }
247
247
const providerComponent = components [ parseInt ( providerId ) - 1 ] ;
248
- providers += 'const ' + providerComponent . name . toLowerCase ( ) + 'Context = useContext(' + providerComponent . name + 'Context);\n \t\t' ;
248
+ providers += 'const ' + providerComponent . name . toLowerCase ( ) + 'Context = useContext(' + providerComponent . name + 'Context);\n \t\t' ;
249
249
for ( let i = 0 ; i < providerComponent . stateProps . length ; i ++ ) {
250
- if ( statesFromProvider . has ( providerComponent . stateProps [ i ] . id ) ) {
250
+ if ( statesFromProvider . has ( providerComponent . stateProps [ i ] . id ) ) {
251
251
context +=
252
- 'const ' +
253
- providerComponent . stateProps [ i ] . key +
254
- ' = ' +
255
- providerComponent . name . toLowerCase ( ) +
256
- 'Context.' +
257
- providerComponent . stateProps [ i ] . key +
258
- '; \n \t\t' ;
252
+ 'const ' +
253
+ providerComponent . stateProps [ i ] . key +
254
+ ' = ' +
255
+ providerComponent . name . toLowerCase ( ) +
256
+ 'Context.' +
257
+ providerComponent . stateProps [ i ] . key +
258
+ '; \n \t\t' ;
259
259
}
260
260
}
261
261
}
@@ -266,17 +266,17 @@ const generateUnformattedCode = (
266
266
return `
267
267
${ `import React, { useState, useEffect} from 'react';` }
268
268
${ `import ReactDOM from 'react-dom';` }
269
- ${ importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';` : `` }
269
+ ${ importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';` : `` }
270
270
${ importsMapped }
271
271
${ `const ${ currComponent . name } = (props) => {` }
272
272
${ ` const [value, setValue] = useState("");${ writeStateProps ( currComponent . useStateCodes ) } ` }
273
273
${ ! importReactRouter
274
- ? ` return (
274
+ ? ` return (
275
275
<div className="${ currComponent . name } " ${ formatStyles ( currComponent ) } >
276
276
\t${ writeNestedElements ( enrichedChildren ) }
277
277
</div>
278
278
);`
279
- : ` return (
279
+ : ` return (
280
280
<Router>
281
281
<div className="${ currComponent . name } " ${ formatStyles ( currComponent ) } >
282
282
\t${ writeNestedElements ( enrichedChildren ) }
@@ -298,12 +298,11 @@ const generateUnformattedCode = (
298
298
const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
299
299
return (
300
300
<>
301
- ${
302
- isRoot
303
- ? `<Head>
301
+ ${ isRoot
302
+ ? `<Head>
304
303
<title>${ currComponent . name } </title>
305
304
</Head>`
306
- : ``
305
+ : ``
307
306
}
308
307
<div className="${ currComponent . name } " style={props.style}>
309
308
${ writeNestedElements ( enrichedChildren ) }
@@ -324,13 +323,12 @@ const generateUnformattedCode = (
324
323
const[value, setValue] = useState<any | undefined>("INITIAL VALUE");
325
324
return (
326
325
<>
327
- ${
328
- isRoot
329
- ? `<head>
326
+ ${ isRoot
327
+ ? `<head>
330
328
<title>${ currComponent . name } </title>
331
329
</head>`
332
- : ``
333
- }
330
+ : ``
331
+ }
334
332
<div className="${ currComponent . name } " style={props.style}>
335
333
${ writeNestedElements ( enrichedChildren ) }
336
334
</div>
0 commit comments