1
+ import { element } from 'prop-types' ;
1
2
import {
2
3
Component ,
3
4
State ,
@@ -54,7 +55,13 @@ const generateUnformattedCode = (
54
55
if (
55
56
referencedHTML . tag === 'div' ||
56
57
referencedHTML . tag === 'separator' ||
57
- referencedHTML . tag === 'form'
58
+ // Caret Start
59
+ referencedHTML . tag === 'form' ||
60
+ referencedHTML . tag === 'ul' ||
61
+ referencedHTML . tag === 'ol' ||
62
+ referencedHTML . tag === 'menu' ||
63
+ referencedHTML . tag === 'li'
64
+ // Caret End
58
65
) {
59
66
child . children = getEnrichedChildren ( child ) ;
60
67
}
@@ -63,70 +70,80 @@ const generateUnformattedCode = (
63
70
links = true ;
64
71
child . name = components . find (
65
72
( comp : Component ) => comp . id === child . typeId
66
- ) . name ;
67
- return child ;
68
- }
69
- } ) ;
70
- return enrichedChildren ;
73
+ ) . name ;
74
+ return child ;
75
+ }
76
+ } ) ;
77
+ return enrichedChildren ;
78
+ } ;
79
+ // Caret Start
80
+ // Raised formatStyles so that it is declared before it is referenced. It was backwards.
81
+ // format styles stored in object to match React inline style format
82
+ const formatStyles = ( styleObj : any ) => {
83
+ if ( Object . keys ( styleObj ) . length === 0 ) return `` ;
84
+ const formattedStyles = [ ] ;
85
+ for ( let i in styleObj ) {
86
+ const styleString = i + ': ' + "'" + styleObj [ i ] + "'" ;
87
+ formattedStyles . push ( styleString ) ;
88
+ }
89
+ return ' style={{' + formattedStyles . join ( ',' ) + '}}' ;
71
90
} ;
91
+
92
+ // function to dynamically add classes, ids, and styles to an element if it exists.
93
+ const elementTagDetails = ( childElement : object ) => {
94
+ let customizationDetails = "" ;
95
+ if ( childElement . childId ) customizationDetails += ( ' ' + `id=${ childElement . childId } ` ) ;
96
+ if ( childElement . attributes && childElement . attributes . cssClasses ) customizationDetails += ( ' ' + `className=${ childElement . attributes . cssClasses } ` ) ;
97
+ if ( childElement . style && Object . keys ( childElement . style ) . length > 0 ) customizationDetails += ( ' ' + formatStyles ( childElement ) ) ;
98
+ return customizationDetails ;
99
+ } ;
100
+
101
+ // function to dynamically generate a complete html (& also other library type) elements
102
+ const elementGenerator = ( childElement : object , level : number = 2 ) => {
103
+ let innerText = '' ;
104
+ if ( childElement . attributes && childElement . attributes . compText ) innerText = childElement . attributes . compText ;
105
+
106
+ const tabSpacer = ( level : number ) => {
107
+ let tabs = ''
108
+ for ( let i = 0 ; i < level ; i ++ ) tabs += ' ' ;
109
+ return tabs ;
110
+ }
111
+
112
+ const levelSpacer = ( level : number , spaces : number ) => {
113
+ if ( level === 2 ) return `\n${ tabSpacer ( spaces ) } ` ;
114
+ else return ''
115
+ }
116
+
117
+ const nestable = childElement . tag === 'div' ||
118
+ childElement . tag === 'form' ||
119
+ childElement . tag === 'ol' ||
120
+ childElement . tag === 'ul' ||
121
+ childElement . tag === 'menu' ||
122
+ childElement . tag === 'li' ;
123
+
124
+ if ( childElement . tag === 'img' ) {
125
+ return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } src="" ${ elementTagDetails ( childElement ) } />${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
126
+ } else if ( childElement . tag === 'a' ) {
127
+ return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } href=""${ elementTagDetails ( childElement ) } >${ innerText } </${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
128
+ } else if ( childElement . tag === 'input' ) {
129
+ return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } ></${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
130
+ } else if ( nestable ) {
131
+ return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText }
132
+ ${ tabSpacer ( level ) } ${ writeNestedElements ( childElement . children , level + 1 ) }
133
+ ${ tabSpacer ( level - 1 ) } </${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
134
+ } else if ( childElement . tag !== 'separator' ) {
135
+ return `${ levelSpacer ( level , 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText } </${ childElement . tag } >${ levelSpacer ( 2 , ( 3 + level ) ) } ` ;
136
+ }
137
+ }
138
+
72
139
// write all code that will be under the "return" of the component
73
- const writeNestedElements = ( enrichedChildren : any ) => {
140
+ const writeNestedElements = ( enrichedChildren : any , level : number = 2 ) => {
74
141
return `${ enrichedChildren
75
142
. map ( ( child : any ) => {
76
143
if ( child . type === 'Component' ) {
77
- return `<${ child . name } ${ formatStyles ( child . style ) } />` ;
144
+ return `<${ child . name } ${ elementTagDetails ( child ) } />` ;
78
145
} else if ( child . type === 'HTML Element' ) {
79
- if ( child . tag === 'img' ) {
80
- return `<${ child . tag } src=""${ formatStyles ( child . style ) } />` ;
81
- } else if ( child . tag === 'a' ) {
82
- return `<${ child . tag } href=""${ formatStyles ( child . style ) } >[LINK]</${
83
- child . tag
84
- } >`;
85
- } else if ( child . tag === 'div' ) {
86
- return `<${ child . tag } ${ formatStyles (
87
- child . style
88
- ) } >${ writeNestedElements ( child . children ) } </${ child . tag } >`;
89
- } else if ( child . tag === 'h1' ) {
90
- return `<${ child . tag } ${ formatStyles ( child . style ) } >HEADER 1</${
91
- child . tag
92
- } >`;
93
- } else if ( child . tag === 'h2' ) {
94
- return `<${ child . tag } ${ formatStyles ( child . style ) } >HEADER 2</${
95
- child . tag
96
- } >`;
97
- } else if ( child . tag === 'form' ) {
98
- return `<${ child . tag } ${ formatStyles (
99
- child . style
100
- ) } >${ writeNestedElements ( child . children ) } </${ child . tag } >`;
101
- }
102
- // Caret Start
103
- else if ( child . tag === 'input' ) {
104
- return `<${ child . tag } ${ formatStyles ( child . style ) } >Input</${
105
- child . tag
106
- } >`;
107
- } else if ( child . tag === 'label' ) {
108
- return `<${ child . tag } ${ formatStyles ( child . style ) } >Label</${
109
- child . tag
110
- } >`;
111
- }
112
- // Caret End
113
- else if ( child . tag === 'p' ) {
114
- return `<${ child . tag } ${ formatStyles (
115
- child . style
116
- ) } >Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</${
117
- child . tag
118
- } >`;
119
- } else if ( child . tag === 'li' ) {
120
- return `<ul${ formatStyles ( child . style ) } ><li>item 1</li>
121
- <li>item 2</li>
122
- <li>item 3</li></ul>` ;
123
- } else if ( child . tag === 'button' ) {
124
- return `<${ child . tag } ${ formatStyles ( child . style ) } >BUTTON</${
125
- child . tag
126
- } >`;
127
- } else if ( child . tag !== 'separator' ) {
128
- return `<${ child . tag } ${ formatStyles ( child . style ) } ></${ child . tag } >` ;
129
- }
146
+ return elementGenerator ( child , level ) ;
130
147
}
131
148
// 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
132
149
else if ( child . type === 'Route Link' ) {
@@ -141,19 +158,9 @@ const generateUnformattedCode = (
141
158
}
142
159
} )
143
160
. filter ( element => ! ! element )
144
- . join ( '\n' ) } `;
145
- } ;
146
-
147
- // format styles stored in object to match React inline style format
148
- const formatStyles = ( styleObj : any ) => {
149
- if ( Object . keys ( styleObj ) . length === 0 ) return `` ;
150
- const formattedStyles = [ ] ;
151
- for ( let i in styleObj ) {
152
- const styleString = i + ': ' + "'" + styleObj [ i ] + "'" ;
153
- formattedStyles . push ( styleString ) ;
154
- }
155
- return ' style={{' + formattedStyles . join ( ',' ) + '}}' ;
161
+ . join ( '' ) } `;
156
162
} ;
163
+ // Caret End
157
164
158
165
const enrichedChildren : any = getEnrichedChildren ( currentComponent ) ;
159
166
@@ -186,11 +193,11 @@ const generateUnformattedCode = (
186
193
${ classBased ? `import React, {Component} from 'react';` : '' }
187
194
${ ! stateful && ! classBased ? `import React from 'react';` : '' }
188
195
${ importsMapped }
189
- ${
190
- classBased
191
- ? `class ${ currentComponent . name } extends Component {`
192
- : `const ${ currentComponent . name } = (props): JSX.Element => {`
193
- }
196
+ ${
197
+ classBased
198
+ ? `class ${ currentComponent . name } extends Component {`
199
+ : `const ${ currentComponent . name } = (props): JSX.Element => {`
200
+ }
194
201
${
195
202
stateful && ! classBased
196
203
? `const [value, setValue] = useState<any | undefined>("INITIAL VALUE");`
@@ -201,20 +208,20 @@ const generateUnformattedCode = (
201
208
? `constructor(props) {
202
209
super(props);
203
210
this.state = {}
204
- }`
211
+ }`
205
212
: ``
206
213
}
207
214
208
215
${ classBased ? `render(): JSX.Element {` : `` }
209
216
210
217
return (
211
218
<div className="${ currentComponent . name } " style={props.style}>
212
- ${ writeNestedElements ( enrichedChildren ) }
219
+ ${ writeNestedElements ( enrichedChildren ) }
213
220
</div>
214
221
);
215
222
}
216
- ${ classBased ? `}` : `` }
217
- export default ${ currentComponent . name } ;
223
+ ${ classBased ? `}` : `` }
224
+ export default ${ currentComponent . name } ;
218
225
` ;
219
226
}
220
227
// next.js component code
@@ -225,27 +232,27 @@ const generateUnformattedCode = (
225
232
import Head from 'next/head'
226
233
${ links ? `import Link from 'next/link'` : `` }
227
234
228
- const ${ currentComponent . name } = (props): JSX.Element => {
235
+ const ${ currentComponent . name } = (props): JSX.Element => {
229
236
230
- const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
237
+ const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
231
238
232
239
return (
233
- <>
234
- ${
235
- isRoot
236
- ? `<Head>
237
- <title>${ currentComponent . name } </title>
238
- </Head>`
239
- : ``
240
- }
241
- <div className="${ currentComponent . name } " style={props.style}>
242
- ${ writeNestedElements ( enrichedChildren ) }
243
- </div>
244
- </>
245
- );
240
+ <>
241
+ ${
242
+ isRoot
243
+ ? `<Head>
244
+ <title>${ currentComponent . name } </title>
245
+ </Head>`
246
+ : ``
246
247
}
248
+ <div className="${ currentComponent . name } " style={props.style}>
249
+ ${ writeNestedElements ( enrichedChildren ) }
250
+ </div>
251
+ </>
252
+ );
253
+ }
247
254
248
- export default ${ currentComponent . name } ;
255
+ export default ${ currentComponent . name } ;
249
256
` ;
250
257
} else {
251
258
// gatsby component code
0 commit comments