Skip to content

Commit 615d291

Browse files
committed
nested undo not working
1 parent d941fd1 commit 615d291

File tree

10 files changed

+335
-8
lines changed

10 files changed

+335
-8
lines changed

app/src/components/bottom/CodePreview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const CodePreview: React.FC<{
2727
const currentComponent = state.components.find(
2828
(elem: Component) => elem.id === state.canvasFocus.componentId
2929
);
30-
console.log('currentComp in CodePreview', currentComponent)
30+
// console.log('currentComp in CodePreview', currentComponent)
3131
const handleCodeSnipChange = val => {
3232
currentComponent.code = val;
3333
};

app/src/components/main/Canvas.tsx

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,81 @@
1-
import React, { useContext } from 'react';
1+
import React, { useState, useContext } from 'react';
22
import { useDrop, DropTargetMonitor } from 'react-dnd';
3+
import customHooks from '../helperFunctions/customHook';
4+
import _ from 'lodash';
35
import { ItemTypes } from '../../constants/ItemTypes';
46
import StateContext from '../../context/context';
57
import { Component, DragItem } from '../../interfaces/Interfaces';
68
import { combineStyles } from '../../helperFunctions/combineStyles';
79
import renderChildren from '../../helperFunctions/renderChildren';
810

11+
// const snapStateArr = [];
912
function Canvas() {
1013
const [state, dispatch] = useContext(StateContext);
14+
// const [ prevState, setPrevState ] = useState<Array<object>>([]); // NOT USING
1115
// find the current component to render on the canvas
1216
const currentComponent: Component = state.components.find(
1317
(elem: Component) => elem.id === state.canvasFocus.componentId
14-
);
18+
);
19+
1520
// console.log('currentComponent', currentComponent)
1621

1722
// changes focus of the canvas to a new component / child
1823
const changeFocus = (componentId: number, childId: number | null) => {
1924
dispatch({ type: 'CHANGE FOCUS', payload: { componentId, childId } });
25+
2026
};
2127
// onClickHandler is responsible for changing the focused component and child component
2228
function onClickHandler(event) {
2329
event.stopPropagation();
2430
// note: a null value for the child id means that we are focusing on the top-level component rather than any child
2531
changeFocus(state.canvasFocus.componentId, null);
2632
}
33+
function onChangeHandler(event) {
34+
// console.log('working', event.target)
35+
}
36+
37+
// stores a limited snapshot of previous state to use in the useDrop function, for UNDO functionality
38+
// const snapStateArr = [];
39+
const snapShotFunc = () => {
40+
// make a deep clone of state ( JSON.parse(JSON.stringify(<object>)) ? )
41+
// function inner() {
42+
const deepCopiedState = JSON.parse(JSON.stringify(state));
43+
// console.log('deepCopiedState', deepCopiedState);
44+
// stateSnapArr.push(deepCopiedState);
45+
state.past.push(deepCopiedState.components[0].children);
46+
// state.past.push(deepCopiedState);
47+
// console.log('state after push', state)
48+
console.log('state in canvas', state.past)
49+
// return;
50+
// snapStateArr.push(5);
51+
// return snapStateArr;
52+
// }
53+
// inner();
54+
55+
// return;
56+
// const prevCount = customHooks(state);
57+
// console.log('prevCount', prevCount);
58+
// console.log('state', state);
59+
60+
// setPrevState( previousState => {
61+
// return [...previousState].push(deepCopiedState);
62+
// })
63+
// console.log('prevState: ', prevState)
64+
}
65+
2766
// This hook will allow the user to drag items from the left panel on to the canvas
2867
const [{ isOver }, drop] = useDrop({
2968
accept: ItemTypes.INSTANCE,
3069
drop: (item: DragItem, monitor: DropTargetMonitor) => {
31-
const didDrop = monitor.didDrop(); // returns false for direct drop target
70+
const didDrop = monitor.didDrop();
71+
// returns false for direct drop target
72+
//code here
73+
// 6.0 didDrop is firing when HTML tags are moved up
74+
snapShotFunc(); // < ------ snapShotFunc here
3275
if (didDrop) {
3376
return;
3477
}
35-
console.log('item', item)
78+
// console.log('item', item)
3679
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
3780
if (item.newInstance) {
3881
dispatch({
@@ -75,10 +118,12 @@ function Canvas() {
75118
// Direct children are draggable/clickable
76119
const canvasStyle = combineStyles(defaultCanvasStyle, currentComponent.style);
77120
return (
78-
<div ref={drop} style={canvasStyle} onClick={onClickHandler}>
121+
<div ref={drop} style={canvasStyle} onClick={onClickHandler} onChange={onChangeHandler}>
79122
{/* currentComponent is the selected component on Left Panel (eg: App or Index with green dot to the left) */}
80123
{renderChildren(currentComponent.children)}
81124
</div>
82125
);
83126
}
127+
// export { snapStateArr };
84128
export default Canvas;
129+

app/src/components/main/CanvasContainer.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function CanvasContainer() {
99
border: '2px Solid grey',
1010
};
1111

12+
function onChangeHandler(event) {
13+
console.log('working', event.target);
14+
}
15+
1216
return (
1317
<div style={canvasContainerStyle}>
1418
<Canvas />

app/src/containers/RightContainer.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import React, {
33
useContext,
44
useEffect,
55
useMemo,
6+
useRef,
67
} from 'react';
8+
import customHooks from '../helperFunctions/customHook';
9+
import initialState from '../context/initialState';
710
import { makeStyles } from '@material-ui/core/styles';
811
import FormControl from '@material-ui/core/FormControl';
912
import Select from '@material-ui/core/Select';
@@ -41,6 +44,31 @@ const RightContainer = ({isThemeLight}): JSX.Element => {
4144
const [deleteComponentError, setDeleteComponentError] = useState(false);
4245
const { style } = useContext(styleContext);
4346
const [modal, setModal] = useState(null);
47+
const [prevState, setPrevState] = useState(state);
48+
// const [ref, setRef] = useRef();
49+
50+
// const prevCountRef = useRef();
51+
// useEffect(() => {
52+
// prevCountRef.current = prevState;
53+
// });
54+
// const prevCount = prevCountRef.current;
55+
// console.log('prevCount <-- before', prevCount);
56+
// console.log('prevState <-- now', prevState);
57+
// console.log('prevState', prevState);
58+
//state.components[0].children
59+
// function usePrevious(value) {
60+
// const ref = useRef();
61+
// useEffect(() => {
62+
// ref.current = value;
63+
// });
64+
// return ref.current;
65+
// }
66+
// const prevCount = customHooks(state);
67+
// console.log('prevCount <-- before', prevCount);
68+
// console.log('prevState <-- now', prevState)
69+
// console.log('state in rightContainer', state);
70+
71+
4472

4573
const resetFields = () => {
4674
const style = configTarget.child
@@ -193,6 +221,45 @@ const RightContainer = ({isThemeLight}): JSX.Element => {
193221
return styleObj;
194222
};
195223

224+
/****************************** UNDO AND REDO ****************************************** */
225+
// undo functionality
226+
// onClick this function will be invoked.
227+
// set current state components.children to previous state and store current state children array in another place holder to not lose current state before reassigning to previous position.
228+
const undoAction = () => {
229+
dispatch({ type: 'UNDO', payload: {} });
230+
// const undoAction = () => {
231+
// dispatch({ type: 'UNDO', payload: {value} })
232+
// const ref = useRef();
233+
// useEffect(() => {
234+
// ref.current = value;
235+
// });
236+
// console.log('ref.curr', ref.current)
237+
// return ref.current;
238+
// dispatch({ type: 'UNDO', payload: {} });
239+
// const childrenArr = state.components[0].children;
240+
// const result = [];
241+
// // console.log('state.components', state.components[0].children)
242+
// for(let i = 0; i < prevState.children.length - 3; i++) {
243+
// result.push(prevState.children[i]);
244+
// }
245+
// setPrevState(result);
246+
};
247+
// };
248+
249+
// const prevCount = undoAction(prevState);
250+
// console.log('prevCount', prevCount);
251+
252+
const handleClick = (e, data) => {
253+
console.log(data);
254+
}
255+
256+
const redoAction = () => {
257+
dispatch({ type: 'REDO', payload: {} });
258+
}
259+
260+
261+
262+
196263
// placeholder for handling deleting instance
197264
const handleDelete = () => {
198265
dispatch({ type: 'DELETE CHILD', payload: {} });
@@ -502,6 +569,28 @@ const RightContainer = ({isThemeLight}): JSX.Element => {
502569
</Button>
503570
</div>
504571
)}
572+
<div className = {classes.buttonRow}>
573+
<Button
574+
color="primary"
575+
className={classes.button}
576+
onClick={undoAction}
577+
// onClick={((e) => handleClick(e, prevCount))}
578+
>
579+
580+
{/* <i className="fas fa-backward"/> */}
581+
<i className="fas fa-undo"></i>
582+
</Button>
583+
<Button
584+
color="primary"
585+
className={classes.button}
586+
// onClick={clearComps}
587+
// onClick={redoAction}
588+
// onClick = {handleClick}
589+
>
590+
<i className="fas fa-redo"></i>
591+
{/* <i className="fas fa-forward"/> */}
592+
</Button>
593+
</div>
505594
</div>
506595

507596
</div>

app/src/context/initialState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const initialState: State = {
2121
nextComponentId: 2,
2222
nextChildId: 1,
2323
nextTopSeparatorId: 1000,
24-
HTMLTypes
24+
HTMLTypes,
25+
past: []
2526
};
2627

2728
export default initialState;

app/src/helperFunctions/customHook.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React, {useRef, useEffect} from 'react';
2+
3+
4+
function usePrevious(value) {
5+
const ref = useRef();
6+
// console.log('ref', ref)
7+
useEffect(() => {
8+
ref.current = value;
9+
// console.log('useEffect', ref.current);
10+
// return ref.current;
11+
});
12+
// console.log('ref.current', ref.current)
13+
return ref.current;
14+
}
15+
16+
export default usePrevious;

app/src/interfaces/Interfaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export interface State {
1313
nextBottomSeparatorId: number;
1414
nextChildId: number;
1515
HTMLTypes: HTMLType[];
16+
past: any[];
17+
}
18+
export interface PastElement {
19+
type: string;
20+
typeId: number;
21+
name: string;
22+
childId: number;
23+
style: object;
24+
attributes?: object;
25+
children?: PastElement[];
1626
}
1727

1828
export interface ChildElement {

app/src/public/index-prod.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<html lang="en-US">
33
<head>
44
<meta charset="UTF-8" />
5+
<!-- <script src = "lodash.js"></script> -->
6+
<script src="https://kit.fontawesome.com/7e1cebd082.js" crossorigin="anonymous"></script>
57
<!-- This file is .ejs rather than .html so that we can dynamically add a nonce into the header -->
68
<!-- the nonce is used in the Content security policy webpack plugin so that Material UI styles can be injected into the DOM -->
79
<!-- https://github.com/reZach/secure-electron-template/issues/14 -->

app/src/public/index.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<html lang="en-US">
33
<head>
44
<meta charset="UTF-8" />
5+
<!-- <script src = "lodash.js"></script> -->
6+
<script src="https://kit.fontawesome.com/7e1cebd082.js" crossorigin="anonymous"></script>
57
<!-- This file is .ejs rather than .html so that we can dynamically add a nonce into the header -->
68
<!-- the nonce is used in the Content security policy webpack plugin so that Material UI styles can be injected into the DOM -->
79
<!-- https://github.com/reZach/secure-electron-template/issues/14 -->

0 commit comments

Comments
 (0)