1
- import { ComponentInt , ChildInt , ChildrenInt , PropInt } from "./Interfaces" ;
1
+ import { ComponentInt , ComponentsInt , ChildInt , ChildrenInt , PropInt } from './Interfaces' ;
2
+ import cloneDeep from './cloneDeep' ;
2
3
3
- const componentRender = ( component : ComponentInt , data : any ) => {
4
+ const componentRender = ( component : ComponentInt , components : ComponentsInt ) => {
4
5
const {
5
6
// stateful,
6
7
// id,
7
8
// position,
8
9
childrenArray,
9
10
title,
10
- props
11
+ props,
11
12
} : {
12
13
childrenArray : ChildrenInt ;
13
14
title : string ;
@@ -16,62 +17,66 @@ const componentRender = (component: ComponentInt, data: any) => {
16
17
17
18
function typeSwitcher ( type : string ) {
18
19
switch ( type ) {
19
- case " string" :
20
- return " string" ;
21
- case " number" :
22
- return " number" ;
23
- case " object" :
24
- return " object" ;
25
- case " array" :
26
- return " any[]" ;
27
- case " bool" :
28
- return " boolean" ;
29
- case " function" :
30
- return " () => any" ;
20
+ case ' string' :
21
+ return ' string' ;
22
+ case ' number' :
23
+ return ' number' ;
24
+ case ' object' :
25
+ return ' object' ;
26
+ case ' array' :
27
+ return ' any[]' ;
28
+ case ' bool' :
29
+ return ' boolean' ;
30
+ case ' function' :
31
+ return ' () => any' ;
31
32
// case 'symbol':
32
33
// return 'string';
33
- case " node" :
34
- return " string" ;
35
- case " element" :
36
- return " string" ;
37
- case " tuple" :
38
- return " [any]" ;
39
- case " enum" :
40
- return "{}" ;
41
- case " any" :
42
- return " any" ;
34
+ case ' node' :
35
+ return ' string' ;
36
+ case ' element' :
37
+ return ' string' ;
38
+ case ' tuple' :
39
+ return ' [any]' ;
40
+ case ' enum' :
41
+ return '{}' ;
42
+ case ' any' :
43
+ return ' any' ;
43
44
default :
44
- return " any" ;
45
+ return ' any' ;
45
46
}
46
47
}
47
48
48
49
function propDrillTextGenerator ( child : ChildInt ) {
49
- if ( child . childType === " COMP" ) {
50
- return data
50
+ if ( child . childType === ' COMP' ) {
51
+ return components
51
52
. find ( ( c : any ) => c . id === child . childComponentId )
52
53
. props . map ( ( prop : PropInt ) => `${ prop . key } ={${ prop . value } }` )
53
- . join ( " " ) ;
54
+ . join ( ' ' ) ;
54
55
}
55
- return "" ;
56
+ if ( child . childType === 'HTML' ) {
57
+ const keys : string [ ] = Object . keys ( child . HTMLInfo ) ;
58
+ return keys . map ( key => `${ key } ={${ child . HTMLInfo [ key ] } }` ) . join ( ' ' ) ;
59
+ }
60
+ return '' ;
56
61
}
57
62
58
63
function componentNameGenerator ( child : ChildInt ) {
59
- if ( child . childType === " HTML" ) {
64
+ if ( child . childType === ' HTML' ) {
60
65
switch ( child . componentName ) {
61
- case " Image" :
62
- return " img" ;
63
- case " Form" :
64
- return " form" ;
65
- case " Button" :
66
- return " button" ;
67
- case " Link" :
66
+ case ' Image' :
67
+ return ' img' ;
68
+ case ' Form' :
69
+ return ' form' ;
70
+ case ' Button' :
71
+ return ' button' ;
72
+ case ' Link' :
68
73
return 'a href=""' ;
69
- case " List" :
70
- return "ul" ;
71
- case " Paragraph" :
72
- return "p" ;
74
+ case ' List' :
75
+ return 'ul' ;
76
+ case ' Paragraph' :
77
+ return 'p' ;
73
78
default :
74
- return " div" ;
79
+ return ' div' ;
75
80
}
76
81
} else {
77
82
return child . componentName ;
@@ -119,37 +124,30 @@ const componentRender = (component: ComponentInt, data: any) => {
119
124
return `
120
125
import React from 'react';
121
126
${ childrenArray
122
- . filter ( child => child . childType !== "HTML" )
123
- . map (
124
- child =>
125
- `import ${ child . componentName } from './${ child . componentName } .tsx'`
126
- )
127
+ . filter ( child => child . childType !== 'HTML' )
128
+ . map ( child => `import ${ child . componentName } from './${ child . componentName } .tsx'` )
127
129
. reduce ( ( acc : Array < string > , child ) => {
128
130
if ( ! acc . includes ( child ) ) {
129
131
acc . push ( child ) ;
130
132
return acc ;
131
133
}
132
134
return acc ;
133
135
} , [ ] )
134
- . join ( "\n" ) }
136
+ . join ( '\n' ) }
135
137
136
138
type Props = {
137
- ${ props . map ( prop => `${ prop . key } : ${ typeSwitcher ( prop . type ) } ` ) . join ( "\n" ) }
139
+ ${ props . map ( prop => `${ prop . key } : ${ typeSwitcher ( prop . type ) } ` ) . join ( '\n' ) }
138
140
}
139
141
140
142
const ${ title } = (props: Props) => {
141
- const {${ props . map ( el => el . key ) . join ( " ,\n" ) } } = props
143
+ const {${ props . map ( el => el . key ) . join ( ' ,\n' ) } } = props
142
144
143
145
return (
144
146
<div>
145
- ${ childrenArray
146
- . map (
147
- child =>
148
- `<${ componentNameGenerator ( child ) } ${ propDrillTextGenerator (
149
- child
150
- ) } />`
151
- )
152
- . join ( "\n" ) }
147
+ ${ cloneDeep ( childrenArray )
148
+ . sort ( ( a , b ) => a . childSort - b . childSort )
149
+ . map ( child => `<${ componentNameGenerator ( child ) } ${ propDrillTextGenerator ( child ) } />` )
150
+ . join ( '\n' ) }
153
151
</div>
154
152
);
155
153
}
0 commit comments