Skip to content

Commit dc9cbbb

Browse files
authored
Merge pull request #11 from oslabs-beta/liam/codepreview
Added code preview functionality
2 parents 5f6cf74 + 57c82cb commit dc9cbbb

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

app/src/components/bottom/CodePreview.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const CodePreview: React.FC<{
8787
<div
8888
ref={wrapper}
8989
style={{
90+
top: '1vw',
9091
height: '100%',
9192
maxWidth: '100%',
9293
justifyContent: 'center'

app/src/components/main/Canvas.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { useDrop, DropTargetMonitor } from 'react-dnd';
33
import { ItemTypes } from '../../constants/ItemTypes';
44
import { Component, DragItem } from '../../interfaces/Interfaces';
@@ -9,7 +9,8 @@ import { useDispatch, useSelector } from 'react-redux';
99
import {
1010
changeFocus,
1111
addChild,
12-
snapShotAction
12+
snapShotAction,
13+
toggleCodePreview
1314
} from '../../redux/reducers/slice/appStateSlice';
1415
import { RootState } from '../../redux/store';
1516

@@ -18,7 +19,7 @@ function Canvas(props): JSX.Element {
1819
(store: RootState) => ({
1920
state: store.appState,
2021
contextParam: store.contextSlice,
21-
isDarkMode: store.darkMode.isDarkMode
22+
isDarkMode: store.darkMode.isDarkMode,
2223
})
2324
);
2425
const dispatch = useDispatch();
@@ -150,15 +151,15 @@ function Canvas(props): JSX.Element {
150151
currentComponent.style
151152
);
152153
return (
153-
<div
154-
className={'componentContainer'}
155-
ref={drop}
156-
data-testid="drop"
157-
style={!isDarkMode ? canvasStyle : darkCombinedCanvasStyle}
158-
onClick={onClickHandler}
159-
>
160-
{renderChildren(currentComponent.children)}
161-
</div>
154+
<div
155+
className={'componentContainer'}
156+
ref={drop}
157+
data-testid="drop"
158+
style={!isDarkMode ? canvasStyle : darkCombinedCanvasStyle}
159+
onClick={onClickHandler}
160+
>
161+
{renderChildren(currentComponent.children)}
162+
</div>
162163
);
163164
}
164165

app/src/components/main/CanvasContainer.tsx

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import Canvas from './Canvas';
3+
import { useDispatch, useSelector } from 'react-redux';
4+
import { RootState } from '../../redux/store';
5+
import CodePreview from '../bottom/CodePreview';
6+
import { toggleCodePreview } from '../../redux/reducers/slice/appStateSlice';
7+
import { Button } from '@mui/material';
38

49
// The CanvasContainer sets the boundaries on the width/height of the canvas
510
function CanvasContainer(props): JSX.Element {
11+
const [theme, setTheme] = useState('solarized_light'); // theme for ACE editor, taken from BottomTabs
12+
const { state } = useSelector(
13+
(store: RootState) => ({
14+
state: store.appState,
15+
})
16+
);
17+
const dispatch = useDispatch();
18+
19+
// onClickCodePreview swaps the rendered component from the canvas to the code preview editor
20+
const onClickCodePreview = () => {
21+
dispatch(toggleCodePreview());
22+
console.log(state.codePreview);
23+
}
24+
625
const canvasContainerStyle = {
726
width: '100%',
827
backgroundColor: 'lightgrey',
928
border: '2px Solid grey',
1029
overflow: 'auto',
1130
};
1231

32+
const codePreviewStyle = {
33+
position: 'absolute',
34+
width: 'max-content',
35+
height: 'max-content',
36+
bottom: '100px',
37+
right: '51vw',
38+
textAlign: 'center',
39+
color: '#ffffff',
40+
backgroundColor: '#151515'
41+
} as const;
42+
1343
return (
1444
<div style={canvasContainerStyle}>
15-
16-
<Canvas isThemeLight={props.isThemeLight}/>
45+
{state.codePreview && <CodePreview theme={theme} setTheme={setTheme}/>}
46+
{!state.codePreview && <Canvas isThemeLight={props.isThemeLight}/>}
47+
<Button
48+
style={codePreviewStyle}
49+
onClick={onClickCodePreview}
50+
>
51+
Code Preview
52+
</Button>
1753
</div>
1854
);
1955
}

app/src/interfaces/Interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export interface State {
1515
nextChildId: number;
1616
HTMLTypes: HTMLType[];
1717
tailwind: boolean;
18-
stylesheet: string
18+
stylesheet: string;
19+
codePreview: boolean;
1920
}
2021

2122
export interface ChildElement {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export const initialState: State = {
3939
nextTopSeparatorId: 1000,
4040
HTMLTypes: HTMLTypes, // left as is for now
4141
tailwind: false,
42-
stylesheet: ''
42+
stylesheet: '',
43+
codePreview: false,
4344
};
4445

4546
let separator = initialState.HTMLTypes[1];
@@ -1261,6 +1262,11 @@ const appStateSlice = createSlice({
12611262
},
12621263
updateStylesheet: (state, action) => {
12631264
state.stylesheet = action.payload;
1265+
},
1266+
1267+
// toggles the active code preview editor for conditional rendering
1268+
toggleCodePreview: (state) => {
1269+
state.codePreview = !state.codePreview;
12641270
}
12651271
}
12661272
});
@@ -1301,7 +1307,8 @@ export const {
13011307
//configToggle,
13021308
snapShotAction,
13031309
allCooperativeState,
1304-
updateStylesheet
1310+
updateStylesheet,
1311+
toggleCodePreview
13051312
} = appStateSlice.actions;
13061313

13071314
// Exports so we can combine in rootReducer

0 commit comments

Comments
 (0)