11import React , { useEffect , useRef } from 'react' ;
22import { BrowserRouter as Router , Switch , Route , Link } from 'react-router-dom' ;
33import Box from '@mui/material/Box' ;
4- import cssRefresher from '../../helperFunctions/cssRefresh' ;
54import { Component } from '../../interfaces/Interfaces' ;
65import ReactDOMServer from 'react-dom/server' ;
76import { useDispatch , useSelector } from 'react-redux' ;
@@ -11,11 +10,11 @@ import { RootState } from '../../redux/store';
1110// DemoRender is the full sandbox demo of our user's custom built React components. DemoRender references the design specifications stored in state to construct
1211// real react components that utilize hot module reloading to depict the user's prototype application.
1312const DemoRender = ( ) : JSX . Element => {
14- const state = useSelector ( ( store :RootState ) => store . appState ) ;
15- const dispatch = useDispatch ( ) ;
16- let currentComponent = state . components . find (
17- ( elem : Component ) => elem . id === state . canvasFocus . componentId
13+ const state = useSelector ( ( store : RootState ) => store . appState ) ;
14+ const stylesheet = useSelector (
15+ ( store : RootState ) => store . appState . stylesheet
1816 ) ;
17+ const dispatch = useDispatch ( ) ;
1918
2019 // Create React ref to inject transpiled code in inframe
2120 const iframe = useRef < any > ( ) ;
@@ -32,7 +31,6 @@ const DemoRender = (): JSX.Element => {
3231 </head>
3332 <body>
3433 <div id="app">
35-
3634 </div>
3735 <script>
3836 window.addEventListener('message', (event) => {
@@ -42,7 +40,6 @@ const DemoRender = (): JSX.Element => {
4240 element.addEventListener('click', (event) => {
4341 event.preventDefault();
4442 window.top.postMessage(event.currentTarget.href, '*');
45- //document.body.style.backgroundColor = 'orange';
4643 }, true)
4744 });
4845 } catch (err) {
@@ -64,9 +61,7 @@ const DemoRender = (): JSX.Element => {
6461 state . components ?. find ( ( el ) => {
6562 return el . name . toLowerCase ( ) === component . toLowerCase ( ) ;
6663 } ) . id ;
67- componentId &&
68- dispatch ( changeFocus ( { componentId, childId : null } ) ) ;
69-
64+ componentId && dispatch ( changeFocus ( { componentId, childId : null } ) ) ;
7065 } ;
7166
7267 // 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
@@ -163,11 +158,15 @@ const DemoRender = (): JSX.Element => {
163158 return componentsToRender ;
164159 } ;
165160
161+ //initializes our 'code' which will be whats actually in the iframe in the demo render
162+ //this will reset every time we make a change
166163 let code = '' ;
164+
167165 const currComponent = state . components . find (
168166 ( element ) => element . id === state . canvasFocus . componentId
169167 ) ;
170168
169+ //writes each component to the code
171170 componentBuilder ( currComponent . children ) . forEach ( ( element ) => {
172171 try {
173172 code += ReactDOMServer . renderToString ( element ) ;
@@ -176,24 +175,26 @@ const DemoRender = (): JSX.Element => {
176175 }
177176 } ) ;
178177
179- // useEffect(() => {
180- // cssRefresher();
181- // }, []);
178+ //writes our stylesheet from state to the code
179+ code += `<style>${ stylesheet } </style>` ;
182180
181+ //adds the code into the iframe
183182 useEffect ( ( ) => {
184183 iframe . current . contentWindow . postMessage ( code , '*' ) ;
185184 } , [ code ] ) ;
186185
187186 return (
188- < div id = { 'renderFocus' } style = { demoContainerStyle } >
189- < iframe
190- ref = { iframe }
191- sandbox = "allow-scripts"
192- srcDoc = { html }
193- width = "100%"
194- height = "100%"
195- />
196- </ div >
187+ < >
188+ < div id = { 'renderFocus' } style = { demoContainerStyle } >
189+ < iframe
190+ ref = { iframe }
191+ sandbox = "allow-scripts"
192+ srcDoc = { html }
193+ width = "100%"
194+ height = "100%"
195+ />
196+ </ div >
197+ </ >
197198 ) ;
198199} ;
199200
0 commit comments