Skip to content

Commit 9c5d3b9

Browse files
committed
live rendering working
1 parent 5d9727e commit 9c5d3b9

File tree

12 files changed

+63
-83
lines changed

12 files changed

+63
-83
lines changed

app/electron/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ async function createWindow() {
7676
// enable devtools when in development mode
7777
devTools: true,
7878
// crucial security feature - blocks rendering process from having access to node modules
79-
nodeIntegration: true,
79+
nodeIntegration: false,
8080
// web workers will not have access to node
81-
nodeIntegrationInWorker: true,
81+
nodeIntegrationInWorker: false,
8282
// disallow experimental feature to allow node.js support in sub-frames (i-frames/child windows)
8383
nodeIntegrationInSubFrames: true,
8484
// runs electron apis and preload script in a separate JS context

app/src/components/bottom/CodePreview.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const CodePreview: React.FC<{
2929
const ref = useRef<any>();
3030
const iframe = useRef<any>();
3131
const [input, setInput] = useState('');
32-
const [code, setCode] = useState('');
32+
33+
3334

3435
const startService = async () => {
3536
ref.current = await esbuild.startService({
@@ -49,32 +50,32 @@ const CodePreview: React.FC<{
4950
(elem: Component) => elem.id === state.canvasFocus.componentId
5051
);
5152

53+
5254
const handleCodeSnipChange = (val) => {
5355
currentComponent.code = val;
54-
5556
};
5657

5758
useEffect(() => {
5859
startService();
5960
}, []);
6061

61-
console.log('saved code', store.getState().code)
6262
useEffect(() => {
6363
setDivHeight(height);
6464
}, [height])
6565

66-
useEffect(() =>
67-
{
68-
66+
useEffect(() => {
67+
console.log('hello')
68+
}, [currentComponent.code])
6969

70-
}, [code])
7170

72-
const handleChange = (event) => {
73-
setInput(event)
74-
// iframe.current.contentWindow.postMessage(result.outputFiles[0].text, '*');
71+
const handleChange = (data) => {
72+
setInput(data)
73+
console.log('line 69',input)
7574
};
7675

7776
const handleClick = async () => {
77+
// setInput(currentComponent.code)
78+
// console.log('currentComponent.code', currentComponent.code)
7879
if(!ref.current) {
7980
return;
8081
}
@@ -87,19 +88,15 @@ useEffect(() =>
8788
fetchPlugin(input)
8889
],
8990
define: {
90-
'process.env.NODE_ENV': '"production"',
91+
'process.env.NODE_ENV': '"development"',
9192
global: 'window'
9293
}
9394
})
95+
console.log('this is input',input)
9496
store.dispatch({type: "SAVE", payload: result.outputFiles[0].text});
95-
//setCode();
96-
97-
98-
// dispatch({type: 'code-preview'})
9997
}
10098

10199

102-
103100
return (
104101
<div
105102
ref={wrapper}
@@ -112,10 +109,11 @@ useEffect(() =>
112109
<AceEditor
113110
mode="javascript"
114111
theme="monokai"
115-
width="90%"
116-
height="50%"
112+
width="100%"
113+
height="70%"
117114
onChange={handleChange}
118115
// value={currentComponent.code}
116+
value={input}
119117
name="Code_div"
120118
readOnly={false}
121119
fontSize={16}
@@ -129,7 +127,4 @@ useEffect(() =>
129127
export default CodePreview;
130128

131129

132-
// const App = () => {
133-
// return <h1>hello</h1>
134-
// }
135130

app/src/components/form/Selector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const FormSelector = (props): JSX.Element => {
1313
})
1414
return (
1515
<div className={props.classes.configRow}>
16+
1617
<div className={props.isThemeLight ? `${props.classes.configType} ${props.classes.lightThemeFontColor}` : `${props.classes.configType} ${props.classes.darkThemeFontColor}`}>
1718
<h3>{props.title}</h3>
1819
</div>

app/src/components/left/HTMLItem.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const HTMLItem : React.FC<{
122122
// updated the id's to reflect the new element types input and label
123123
return ( // HTML Elements
124124
<Grid item xs={5} key={`html-g${name}`}>
125+
125126
{ id <= 18 &&
126127
<div ref={drag} className={isThemeLight ? `${classes.HTMLPanelItem} ${classes.lightThemeFontColor}` : `${classes.HTMLPanelItem} ${classes.darkThemeFontColor}`} id="HTMLItem">
127128
<h3>{name}</h3>

app/src/components/main/Canvas.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ function Canvas() {
8080
const canvasStyle = combineStyles(defaultCanvasStyle, currentComponent.style);
8181
return (
8282
<div ref={drop} style={canvasStyle} onClick={onClickHandler}>
83-
{/* currentComponent is the selected component on Left Panel (eg: App or Index with green dot to the left) */}
84-
{renderChildren(currentComponent.children)}
83+
84+
{renderChildren(currentComponent.children)}
8585
</div>
8686
);
8787
}

app/src/components/main/CanvasContainer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function CanvasContainer() {
1111

1212
return (
1313
<div style={canvasContainerStyle}>
14+
1415
<Canvas />
1516
</div>
1617
);

app/src/components/main/DemoRender.tsx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,47 @@
22
import React, {
33
useState, useCallback, useContext, useEffect, useRef
44
} from 'react';
5+
import ReactDOM from 'react-dom';
56
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
67
import Button from '@material-ui/core/Button';
78
import Box from '@material-ui/core/Box';
89
import StateContext from '../../context/context';
910
import cssRefresher from '../../helperFunctions/cssRefresh';
1011
import {store} from '../../../src/index';
12+
import { useSelector } from 'react-redux';
1113
// DemoRender is the full sandbox demo of our user's custom built React components. DemoRender references the design specifications stored in state to construct
1214
// real react components that utilize hot module reloading to depict the user's prototype application.
1315
const DemoRender = (props): JSX.Element => {
14-
// const ref = useRef<any>();
16+
1517
const iframe = useRef<any>();
1618
const [components, setComponents] = useState([]);
1719
const [state, dispatch] = useContext(StateContext);
20+
const [codeRender, setCodeRender] = useState('')
1821
const demoContainerStyle = {
1922
width: '100%',
2023
backgroundColor: '#FBFBFB',
2124
border: '2px Solid grey',
2225
};
2326

27+
let code = useSelector((state) => state.code);
28+
2429
const html = `
2530
<html>
2631
<head></head>
2732
<body>
28-
<div id='root'></div>
33+
<div id='app'></div>
2934
<script>
3035
window.addEventListener('message', (e) => {
31-
eval(event.data);
36+
console.log(e)
37+
eval(e.data);
3238
}, false);
3339
</script>
3440
</body>
3541
</html>
3642
`;
3743

38-
39-
// 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
40-
// Material UI is utilized to incorporate the apporpriate tags with specific configuration designs as necessitated by HTML standards.
44+
// 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
45+
// Material UI is utilized to incorporate the apporpriate tags with specific configuration designs as necessitated by HTML standards.
4146
const componentBuilder = (array: object, key: number = 0) => {
4247
const componentsToRender = [];
4348
for (const element of array) {
@@ -77,19 +82,24 @@ const DemoRender = (props): JSX.Element => {
7782
cssRefresher();
7883
}, [])
7984

80-
// iframe.current.contentWindow.postMessage(store.getState(), '*');
81-
console.log('in store line 81',store.getState())
8285

86+
useEffect(() => {
87+
88+
console.log("code changed", code);
89+
setCodeRender(code)
90+
iframe.current.contentWindow.postMessage(code, '*');
91+
92+
}, [code])
93+
8394
return (
84-
<Router>
85-
8695
<div id={'renderFocus'} style={demoContainerStyle}>
87-
<iframe ref={iframe} sandbox='allow-scripts' srcDoc={html}/>
88-
{/* {components.map((component, index) => component)} */}
89-
</div>
90-
</Router>
96+
97+
98+
<iframe ref={iframe} sandbox='allow-scripts' srcDoc={html} width='500px' height='500px'/>
99+
91100

101+
</div>
92102
);
93103
};
94104

95-
export default DemoRender;
105+
export default DemoRender;

app/src/helperFunctions/generateCode.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const generateUnformattedCode = (
4949
let links: boolean = false;
5050

5151
const isRoot = rootComponents.includes(componentId);
52-
let importReactRouter = false;;
52+
let importReactRouter = false;
5353

5454
// returns an array of objects which may include components, html elements, and/or route links
5555
const getEnrichedChildren = (currentComponent: Component | ChildElement) => {
@@ -287,38 +287,31 @@ const generateUnformattedCode = (
287287
// classic react code
288288
if (projectType === 'Classic React') {
289289
return `
290-
${`import React, { useState, createContext, useContext } from 'react';`}
290+
${`import React, { useState, useEffect} from 'react';`}
291+
${`import ReactDOM from 'react-dom';`}
291292
${importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';`: ``}
292293
${importsMapped}
293294
${importContext}
294295
${providers}
295296
${context}
296-
${`const ${currComponent.name} = (props: any): JSX.Element => {`}
297-
${` const [value, setValue] = useState<any | undefined>("INITIAL VALUE");${writeStateProps(currComponent.useStateCodes)}`}
298-
${
299-
isRoot && currComponent.stateProps.length !== 0
300-
? ` const ${currComponent.name}Context = createContext(${createState(currComponent.stateProps)});`
301-
: ``
302-
}
297+
${`const ${currComponent.name} = (props) => {`}
298+
${` const [value, setValue] = useState("");${writeStateProps(currComponent.useStateCodes)}`}
299+
303300
${!importReactRouter
304301
? ` return (
305-
<${currComponent.name}Context.Provider value="">
306302
<div className="${currComponent.name}" ${formatStyles(currComponent)}>
307303
\t${writeNestedElements(enrichedChildren)}
308304
</div>
309-
</${currComponent.name}Context.Provider>
310305
);`
311306
: ` return (
312-
<${currComponent.name}Context.Provider value="">
313307
<Router>
314308
<div className="${currComponent.name}" ${formatStyles(currComponent)}>
315309
\t${writeNestedElements(enrichedChildren)}
316310
</div>
317311
</Router>
318-
</${currComponent.name}Context.Provider>
319312
);`}
320313
${`}\n`}
321-
export default ${currComponent.name};
314+
ReactDOM.render(<${currComponent.name} />, document.querySelector('#app'));
322315
`;
323316
}
324317
// next.js component code

app/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ const client = new ApolloClient({
2424
uri: 'https://reactype-caret.herokuapp.com/graphql',
2525
cache: new InMemoryCache()
2626
});
27-
const initialState = {code: "initialState"}
27+
const initialState = {code: `
28+
import React from 'react';
29+
import ReactDOM from 'react-dom';
30+
const App = () => <h1>Hello World!</h1>
31+
32+
ReactDOM.render(<App />, document.querySelector('#app'));`}
2833
const rootReducer = (state = initialState, action) => {
2934
console.log('action', action);
3035
switch (action.type) {

app/src/public/index.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en-US">
33
<head>
44
<meta charset="UTF-8" />
5+
<!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self'"> -->
56
<script src="https://kit.fontawesome.com/7e1cebd082.js" crossorigin="anonymous"></script>
67
<!-- This file is .ejs rather than .html so that we can dynamically add a nonce into the header -->
78
<!-- the nonce is used in the Content security policy webpack plugin so that Material UI styles can be injected into the DOM -->

app/src/reducers/componentReducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ const reducer = (state: State, action: Action) => {
266266
typeId,
267267
childId
268268
}: { type: string; typeId: number; childId: any } = action.payload;
269-
269+
console.log("action.payload", action.payload);
270270
const parentComponentId: number = state.canvasFocus.componentId;
271271
const components = [...state.components];
272272

app/src/utils/createTestSuiteClassic.util.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,6 @@ async function createComponentTests(path: string, appName: string, components: C
105105
return string.charAt(0).toUpperCase() + string.slice(1);
106106
}
107107

108-
// //////////////////////////
109-
// // DELETE BELOW AFTERWARDS
110-
// import { shallow } from 'enzyme'
111-
// import React from 'react';
112-
113-
// import * as Enzyme from 'enzyme'
114-
// import Adapter from 'enzyme-adapter-react-16'
115-
116-
// Enzyme.configure({
117-
// adapter: new Adapter(),
118-
// })
119-
120-
// describe('Hello, Enzyme!', () => {
121-
// it('renders', () => {
122-
// const wrapper = shallow(<div>
123-
// <h1>Hello, Enzyme!</h1>
124-
// </div>)
125-
// expect(wrapper.find('h1').html()).toMatch(/Hello, Enzyme/)
126-
// })
127-
128-
// it('renders snapshots, too', () => {
129-
// const wrapper = shallow(< />)
130-
// expect(wrapper).toMatchSnapshot()
131-
// })
132-
// })
133-
// // DELETE ABOVE AFTERWARDS
134-
// //////////////////////////
135108
async function createTestSuiteClassic({
136109
path,
137110
appName,

0 commit comments

Comments
 (0)