Skip to content

Commit 5b44858

Browse files
atvanekrachelk585
andcommitted
CSS editor fully functional
Co-authored-by: rachelk585 <[email protected]>
1 parent 1cd16fd commit 5b44858

File tree

7 files changed

+53
-96
lines changed

7 files changed

+53
-96
lines changed

app/src/components/bottom/StylesEditor.tsx

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useRef, useEffect } from 'react';
1+
import React, { useState, useRef } from 'react';
22
import AceEditor from 'react-ace';
33
import 'ace-builds/src-noconflict/mode-css';
44
import 'ace-builds/src-noconflict/theme-monokai';
@@ -10,65 +10,32 @@ import 'ace-builds/src-noconflict/theme-monokai';
1010
import 'ace-builds/src-min-noconflict/ext-searchbox';
1111
import Fab from '@mui/material/Fab';
1212
import SaveIcon from '@mui/icons-material/Save';
13-
import cssRefresher from '../../helperFunctions/cssRefresh';
1413
import { updateStylesheet } from '../../redux/reducers/slice/appStateSlice';
15-
import { useDispatch } from 'react-redux';
16-
17-
//This was being used for the demo
18-
// const serverURL = 'https://reactype-caret.herokuapp.com';
14+
import { useDispatch, useSelector } from 'react-redux';
15+
import { RootState } from '../../redux/store';
1916

2017
const StylesEditor: React.FC<{
2118
theme: string | null;
2219
setTheme: any | null;
2320
}> = ({ theme, setTheme }) => {
2421
const wrapper = useRef();
25-
const [css, setCss] = useState();
26-
//now using variable and storing CSS in localStorage to retain CSS upon dismount of the component
27-
let currentCss = localStorage.getItem('css');
28-
const dispatch = useDispatch();
29-
30-
//This was being used for the demo
31-
32-
// useEffect(() => {
33-
// loadFile();
34-
// }, []);
35-
36-
// const loadFile = () => {
37-
// const myHeaders = new Headers({
38-
// 'Content-Type': 'text/css',
39-
// Accept: 'text/css',
40-
// });
41-
// fetch(`${serverURL}/demoRender`, {
42-
// headers: myHeaders,
43-
// })
44-
// .then(response => response.text())
45-
// .then((data) => {
46-
// setCss(data);
47-
// });
48-
// }
22+
const stylesheet = useSelector(
23+
(state: RootState) => state.appState.stylesheet
24+
);
25+
//sets state for what text is currently in the csseditor
26+
const [css, setCss] = useState(stylesheet);
4927

50-
// const saveFile = () => {
51-
// fetch(`${serverURL}/user-styles/save`, {
52-
// method: 'POST',
53-
// headers: { 'Content-Type': 'application/json' },
54-
// body: JSON.stringify({ data: css }),
55-
// })
56-
// .then(response => response.text())
57-
// .then((data) => {
58-
// // Removes old link to css and creates a new stylesheet link on demo render
59-
// cssRefresher();
60-
// });
61-
// }
28+
const dispatch = useDispatch();
6229

63-
//refactored this function to store the css on local storage rather than invoke saveFile()
30+
//on save, updates the state based on above hook and rerenders the demo
6431
const saveCss = (e) => {
6532
e.preventDefault();
66-
dispatch(updateStylesheet(currentCss));
67-
localStorage.setItem('css', currentCss);
33+
dispatch(updateStylesheet(css));
6834
};
6935

36+
//handles changes in the ace editor
7037
const handleChange = (text) => {
71-
currentCss = text;
38+
setCss(text);
7239
};
7340

7441
return (
@@ -87,7 +54,7 @@ const StylesEditor: React.FC<{
8754
width="100%"
8855
height="100%"
8956
onChange={handleChange}
90-
value={currentCss}
57+
value={css}
9158
name="Css_div"
9259
fontSize={16}
9360
tabSize={2}

app/src/components/main/DemoRender.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { useEffect, useRef } from 'react';
22
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
33
import Box from '@mui/material/Box';
4-
import cssRefresher from '../../helperFunctions/cssRefresh';
54
import { Component } from '../../interfaces/Interfaces';
65
import ReactDOMServer from 'react-dom/server';
76
import { 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.
1312
const 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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Removes old link to css and creates a new stylesheet link on demo render
22
// this is not currently being used for the website version
33
const cssRefresher = () => {
4-
const oldStylesheet = document.getElementById('Render Stylesheet')
4+
const oldStylesheet = document.getElementById('stylesheet');
55
console.log(oldStylesheet);
66
if (oldStylesheet !== null) oldStylesheet.remove();
7-
const rando = Math.random() * 100000;
8-
const newStylesheet = document.createElement("LINK");
9-
newStylesheet.setAttribute("rel", "stylesheet")
10-
newStylesheet.setAttribute("type", "text/css");
11-
newStylesheet.setAttribute("href", `https://reactype-caret.herokuapp.com/demoRender?${rando}`);
12-
newStylesheet.setAttribute("id", 'Render Stylesheet');
7+
// const rando = Math.random() * 100000;
8+
const newStylesheet = document.createElement('LINK');
9+
newStylesheet.setAttribute('rel', 'stylesheet');
10+
newStylesheet.setAttribute('type', 'text/css');
11+
newStylesheet.setAttribute('href', 'fake.css');
12+
newStylesheet.setAttribute('id', 'stylesheet');
1313
document.getElementById('renderFocus').append(newStylesheet);
14-
console.log(newStylesheet)
15-
}
16-
export default cssRefresher;
14+
console.log(newStylesheet);
15+
};
16+
export default cssRefresher;

app/src/helperFunctions/zipFiles.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ const zipFiles = (state) => {
1414
for (let i in state.components){
1515
componentFolder.file(`${state.components[i].name}.jsx`, state.components[i].code);
1616
}
17-
//writes our css file if we have a css file stored in local storage
18-
if(localStorage.getItem('css')){
19-
reacTypeApp.file('style.css', localStorage.getItem('css'));
20-
}
17+
//writes our css file
18+
reacTypeApp.file('style.css', state.stylesheet);
2119
//zips the file and saves to local machine
2220
zip.generateAsync({type:"blob"})
2321
.then(function(content) {

app/src/public/styles/style.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ LEFT COLUMN
107107

108108
.left-container {
109109
display: flex;
110-
z-index: 0;
110+
z-index: 1;
111111
}
112112

113113
.left-indicator {
@@ -470,6 +470,8 @@ BOTTOM PANEL
470470
width: 100%;
471471
overflow-y: scroll;
472472
overflow-x: auto;
473+
position: relative;
474+
z-index: 0;
473475
}
474476

475477
#resize-drag {

app/src/redux/reducers/slice/appStateSlice.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,13 +1248,7 @@ const appStateSlice = createSlice({
12481248
toggleLoggedIn: (state) => {
12491249
state.isLoggedIn = !state.isLoggedIn;
12501250
},
1251-
// configToggle: (state) => {
1252-
// state.config = {
1253-
// ...state.config,
1254-
// saveFlag: !state.config.saveFlag,
1255-
// saveTimer: !state.config.saveTimer
1256-
// };
1257-
// },
1251+
12581252
snapShotAction: (state, action) => {
12591253
state.components[action.payload.focusIndex].past.push(
12601254
action.payload.deepCopiedState.components[action.payload.focusIndex]
@@ -1265,7 +1259,6 @@ const appStateSlice = createSlice({
12651259
return Object.assign({}, state, action.payload);
12661260
},
12671261
updateStylesheet: (state, action) => {
1268-
console.log('stylesheet', state.stylesheet, 'action', action);
12691262
state.stylesheet = action.payload;
12701263
}
12711264
}

server/server.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ import { dirname } from 'path';
3131
import dotenv from 'dotenv';
3232
dotenv.config();
3333

34-
// const __filename = fileURLToPath(import.meta.url);
35-
// const __dirname = dirname(__filename);
36-
3734
const app = express();
3835

3936
const PORT = process.env.PORT || DEV_PORT;
@@ -43,7 +40,6 @@ const isTest = process.env.NODE_ENV === 'test';
4340

4441
app.use(express.json({ limit: '100mb' }));
4542
app.use(express.urlencoded({ limit: '100mb', extended: true }));
46-
// app.use(cookieParser());
4743

4844
// Routes
4945
// const stylesRouter = require('./routers/stylesRouter');

0 commit comments

Comments
 (0)