1
+ import { element } from 'prop-types' ;
1
2
import {
2
3
Component ,
3
4
State ,
@@ -63,58 +64,72 @@ const generateUnformattedCode = (
63
64
links = true ;
64
65
child . name = components . find (
65
66
( comp : Component ) => comp . id === child . typeId
66
- ) . name ;
67
- return child ;
68
- }
69
- } ) ;
70
- return enrichedChildren ;
67
+ ) . name ;
68
+ return child ;
69
+ }
70
+ } ) ;
71
+ return enrichedChildren ;
72
+ } ;
73
+ // Caret Start
74
+ // Raised formatStyles so that it is declared before it is referenced. It was backwards.
75
+ // format styles stored in object to match React inline style format
76
+ const formatStyles = ( styleObj : any ) => {
77
+ if ( Object . keys ( styleObj ) . length === 0 ) return `` ;
78
+ const formattedStyles = [ ] ;
79
+ for ( let i in styleObj ) {
80
+ const styleString = i + ': ' + "'" + styleObj [ i ] + "'" ;
81
+ formattedStyles . push ( styleString ) ;
82
+ }
83
+ return ' style={{' + formattedStyles . join ( ',' ) + '}}' ;
84
+ } ;
85
+
86
+ // function to dynamically add classes, ids, and styles to an element if it exists.
87
+ const elementTagDetails = ( childElement : object ) => {
88
+ let customizationDetails = "" ;
89
+ if ( childElement . childId ) customizationDetails += ( ' ' + `id=${ childElement . childId } ` ) ;
90
+ if ( childElement . attributes && childElement . attributes . cssClasses ) customizationDetails += ( ' ' + `className=${ childElement . attributes . cssClasses } ` ) ;
91
+ if ( childElement . style && Object . keys ( childElement . style ) . length > 0 ) customizationDetails += ( ' ' + formatStyles ( childElement ) ) ;
92
+ return customizationDetails ;
71
93
} ;
94
+
72
95
// write all code that will be under the "return" of the component
73
96
const writeNestedElements = ( enrichedChildren : any ) => {
74
97
return `${ enrichedChildren
75
98
. map ( ( child : any ) => {
76
99
console . log ( "What is in this child" , child ) ;
77
- const childId = child . childId ;
78
- const childStyle = child . style ;
79
- const innerText = child . attributes . compText ;
80
- const classRender = child . attributes . cssClasses ;
100
+ let innerText = '' ;
101
+ if ( child . attributes && child . attributes . compText ) innerText = child . attributes . compText ;
81
102
if ( child . type === 'Component' ) {
82
- return `<${ child . name } ${ formatStyles ( child . style ) } />` ;
103
+ return `<${ child . name } ${ elementTagDetails ( child ) } />` ;
83
104
} else if ( child . type === 'HTML Element' ) {
84
105
if ( child . tag === 'img' ) {
85
- return `<${ child . tag } src=""${ formatStyles ( child . style ) } />` ;
106
+ return `<${ child . tag } src="" ${ elementTagDetails ( child ) } />` ;
86
107
} else if ( child . tag === 'a' ) {
87
- return `<${ child . tag } href=""${ formatStyles ( child . style ) } >[LINK] </${ child . tag } >` ;
108
+ return `<${ child . tag } href=""${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
88
109
} else if ( child . tag === 'div' ) {
89
- return `<${ child . tag } className=${ classRender } id=${ childId } style=${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } ${ writeNestedElements ( child . children ) }
90
- </${ child . tag } >` ;
110
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
91
111
} else if ( child . tag === 'h1' ) {
92
- return `<${ child . tag } className= ${ classRender } id= ${ childId } style= ${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } </${ child . tag } >` ;
112
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
93
113
} else if ( child . tag === 'h2' ) {
94
- return `<${ child . tag } className= ${ classRender } id= ${ childId } style= ${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } </${ child . tag } >` ;
114
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
95
115
} else if ( child . tag === 'form' ) {
96
- return `<${ child . tag } className=${ classRender } id=${ childId } style=${ childStyle } ${ formatStyles ( child . style ) } >
97
- ${ innerText }
98
- ${ writeNestedElements ( child . children ) }
99
- </${ child . tag } >` ;
100
- }
101
- // Caret Start
102
- else if ( child . tag === 'input' ) {
103
- return `<${ child . tag } className=${ classRender } id=${ childId } style=${ childStyle } ${ formatStyles ( child . style ) } ></${ child . tag } >` ;
116
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
117
+ } else if ( child . tag === 'input' ) {
118
+ return `<${ child . tag } ${ elementTagDetails ( child ) } ></${ child . tag } >` ;
104
119
} else if ( child . tag === 'label' ) {
105
- return `<${ child . tag } className=${ classRender } id=${ childId } style=${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } </${ child . tag } >` ;
106
- }
107
- // Caret End
108
- else if ( child . tag === 'p' ) {
109
- return `<${ child . tag } className=${ classRender } id=${ childId } style=${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } </${ child . tag } >` ;
120
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
121
+ } else if ( child . tag === 'p' ) {
122
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
123
+ } else if ( child . tag === 'ol' ) {
124
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
125
+ } else if ( child . tag === 'ul' ) {
126
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
110
127
} else if ( child . tag === 'li' ) {
111
- return `<ul${ formatStyles ( child . style ) } ><li>item 1</li>
112
- <li>item 2</li>
113
- <li>item 3</li></ul>` ;
128
+ return `<${ child . tag } ${ elementTagDetails ( child ) } ></${ child . tag } >` ;
114
129
} else if ( child . tag === 'button' ) {
115
- return `<${ child . tag } className= ${ classRender } id= ${ childId } style= ${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } </${ child . tag } >` ;
130
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
116
131
} else if ( child . tag !== 'separator' ) {
117
- return `<${ child . tag } className= ${ classRender } id= ${ childId } style= ${ childStyle } ${ formatStyles ( child . style ) } >${ innerText } </${ child . tag } >` ;
132
+ return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
118
133
}
119
134
}
120
135
// 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
@@ -133,16 +148,6 @@ const generateUnformattedCode = (
133
148
. join ( '\n' ) } `;
134
149
} ;
135
150
136
- // format styles stored in object to match React inline style format
137
- const formatStyles = ( styleObj : any ) => {
138
- if ( Object . keys ( styleObj ) . length === 0 ) return `` ;
139
- const formattedStyles = [ ] ;
140
- for ( let i in styleObj ) {
141
- const styleString = i + ': ' + "'" + styleObj [ i ] + "'" ;
142
- formattedStyles . push ( styleString ) ;
143
- }
144
- return ' style={{' + formattedStyles . join ( ',' ) + '}}' ;
145
- } ;
146
151
147
152
const enrichedChildren : any = getEnrichedChildren ( currentComponent ) ;
148
153
0 commit comments