1
1
import React , { useState , useEffect , useRef , useContext } from 'react' ;
2
- import { BrowserRouter as Router , Switch , Route , Link } from " react-router-dom" ;
2
+ import { BrowserRouter as Router , Switch , Route , Link } from ' react-router-dom' ;
3
3
import Box from '@material-ui/core/Box' ;
4
4
import cssRefresher from '../../helperFunctions/cssRefresh' ;
5
5
import { useSelector } from 'react-redux' ;
@@ -17,12 +17,12 @@ const DemoRender = (): JSX.Element => {
17
17
( elem : Component ) => elem . id === state . canvasFocus . componentId
18
18
) ;
19
19
20
- // Create React ref to inject transpiled code in inframe
20
+ // Create React ref to inject transpiled code in inframe
21
21
const iframe = useRef < any > ( ) ;
22
22
const demoContainerStyle = {
23
23
width : '100%' ,
24
24
backgroundColor : '#FBFBFB' ,
25
- border : '2px Solid grey' ,
25
+ border : '2px Solid grey'
26
26
} ;
27
27
28
28
const html = `
@@ -55,14 +55,19 @@ const DemoRender = (): JSX.Element => {
55
55
` ;
56
56
57
57
//Switch between components when clicking on a link in the live render
58
- window . onmessage = ( event ) => {
59
- if ( event . data === undefined ) return ;
60
- const component :string = event . data ?. split ( '/' ) . at ( - 1 ) ;
61
- const componentId = component && state . components ?. find ( ( el ) => {
62
- return el . name . toLowerCase ( ) === component . toLowerCase ( ) ;
63
- } ) . id ;
64
- componentId && dispatch ( { type : 'CHANGE FOCUS' , payload : { componentId, childId : null } } )
65
-
58
+ window . onmessage = event => {
59
+ if ( event . data === undefined ) return ;
60
+ const component : string = event . data ?. split ( '/' ) . at ( - 1 ) ;
61
+ const componentId =
62
+ component &&
63
+ state . components ?. find ( el => {
64
+ return el . name . toLowerCase ( ) === component . toLowerCase ( ) ;
65
+ } ) . id ;
66
+ componentId &&
67
+ dispatch ( {
68
+ type : 'CHANGE FOCUS' ,
69
+ payload : { componentId, childId : null }
70
+ } ) ;
66
71
} ;
67
72
68
73
// This function is the heart of DemoRender it will take the array of components stored in state and dynamically construct the desired React component for the live demo
@@ -78,28 +83,94 @@ const DemoRender = (): JSX.Element => {
78
83
const classRender = element . attributes . cssClasses ;
79
84
const activeLink = element . attributes . compLink ;
80
85
let renderedChildren ;
81
- if ( elementType !== 'input' && elementType !== 'img' && elementType !== 'Image' && element . children . length > 0 ) {
86
+ if (
87
+ elementType !== 'input' &&
88
+ elementType !== 'img' &&
89
+ elementType !== 'Image' &&
90
+ element . children . length > 0
91
+ ) {
82
92
renderedChildren = componentBuilder ( element . children ) ;
83
93
}
84
- if ( elementType === 'input' ) componentsToRender . push ( < Box component = { elementType } className = { classRender } style = { elementStyle } key = { key } id = { `rend${ childId } ` } > </ Box > ) ;
85
- else if ( elementType === 'img' ) componentsToRender . push ( < Box component = { elementType } src = { activeLink } className = { classRender } style = { elementStyle } key = { key } id = { `rend${ childId } ` } > </ Box > ) ;
86
- else if ( elementType === 'Image' ) componentsToRender . push ( < Box component = 'img' src = { activeLink } className = { classRender } style = { elementStyle } key = { key } id = { `rend${ childId } ` } > </ Box > ) ;
87
- else if ( elementType === 'a' || elementType === 'Link' ) componentsToRender . push ( < Box component = 'a' href = { activeLink } className = { classRender } style = { elementStyle } key = { key } id = { `rend${ childId } ` } > { innerText } { renderedChildren } </ Box > ) ;
88
- else if ( elementType === 'Switch' ) componentsToRender . push ( < Switch > { renderedChildren } </ Switch > ) ;
89
- else if ( elementType === 'Route' ) componentsToRender . push ( < Route exact path = { activeLink } > { renderedChildren } </ Route > ) ;
90
- else componentsToRender . push ( < Box component = { elementType } className = { classRender } style = { elementStyle } key = { key } id = { `rend${ childId } ` } > { innerText } { renderedChildren } </ Box >
91
- ) ;
94
+ if ( elementType === 'input' )
95
+ componentsToRender . push (
96
+ < Box
97
+ component = { elementType }
98
+ className = { classRender }
99
+ style = { elementStyle }
100
+ key = { key }
101
+ id = { `rend${ childId } ` }
102
+ > </ Box >
103
+ ) ;
104
+ else if ( elementType === 'img' )
105
+ componentsToRender . push (
106
+ < Box
107
+ component = { elementType }
108
+ src = { activeLink }
109
+ className = { classRender }
110
+ style = { elementStyle }
111
+ key = { key }
112
+ id = { `rend${ childId } ` }
113
+ > </ Box >
114
+ ) ;
115
+ else if ( elementType === 'Image' )
116
+ componentsToRender . push (
117
+ < Box
118
+ component = "img"
119
+ src = { activeLink }
120
+ className = { classRender }
121
+ style = { elementStyle }
122
+ key = { key }
123
+ id = { `rend${ childId } ` }
124
+ > </ Box >
125
+ ) ;
126
+ else if ( elementType === 'a' || elementType === 'Link' )
127
+ componentsToRender . push (
128
+ < Box
129
+ component = "a"
130
+ href = { activeLink }
131
+ className = { classRender }
132
+ style = { elementStyle }
133
+ key = { key }
134
+ id = { `rend${ childId } ` }
135
+ >
136
+ { innerText }
137
+ { renderedChildren }
138
+ </ Box >
139
+ ) ;
140
+ else if ( elementType === 'Switch' )
141
+ componentsToRender . push ( < Switch > { renderedChildren } </ Switch > ) ;
142
+ else if ( elementType === 'Route' )
143
+ componentsToRender . push (
144
+ < Route exact path = { activeLink } >
145
+ { renderedChildren }
146
+ </ Route >
147
+ ) ;
148
+ else
149
+ componentsToRender . push (
150
+ < Box
151
+ component = { elementType }
152
+ className = { classRender }
153
+ style = { elementStyle }
154
+ key = { key }
155
+ id = { `rend${ childId } ` }
156
+ >
157
+ { innerText }
158
+ { renderedChildren }
159
+ </ Box >
160
+ ) ;
92
161
key += 1 ;
93
162
}
94
163
}
95
164
return componentsToRender ;
96
165
} ;
97
166
98
167
let code = '' ;
99
- const currComponent = state . components . find ( element => element . id === state . canvasFocus . componentId ) ;
168
+ const currComponent = state . components . find (
169
+ element => element . id === state . canvasFocus . componentId
170
+ ) ;
100
171
101
172
componentBuilder ( currComponent . children ) . forEach ( element => {
102
- try {
173
+ try {
103
174
code += ReactDOMServer . renderToString ( element ) ;
104
175
} catch {
105
176
return ;
@@ -108,17 +179,23 @@ const DemoRender = (): JSX.Element => {
108
179
109
180
useEffect ( ( ) => {
110
181
cssRefresher ( ) ;
111
- } , [ ] )
182
+ } , [ ] ) ;
112
183
113
184
useEffect ( ( ) => {
114
185
iframe . current . contentWindow . postMessage ( code , '*' ) ;
115
- } , [ code ] )
186
+ } , [ code ] ) ;
116
187
117
188
return (
118
189
< div id = { 'renderFocus' } style = { demoContainerStyle } >
119
- < iframe ref = { iframe } sandbox = 'allow-scripts' srcDoc = { html } width = '100%' height = '100%' />
190
+ < iframe
191
+ ref = { iframe }
192
+ sandbox = "allow-scripts"
193
+ srcDoc = { html }
194
+ width = "100%"
195
+ height = "100%"
196
+ />
120
197
</ div >
121
198
) ;
122
199
} ;
123
200
124
- export default DemoRender ;
201
+ export default DemoRender ;
0 commit comments