Skip to content

Commit 9ed0e78

Browse files
committed
Merge branch 'master' into githuboauth
2 parents f1d4074 + 5b7a761 commit 9ed0e78

14 files changed

+386
-230
lines changed

src/components/AppNew.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import '../public/styles/styleNew.css';
44
import { DndProvider } from 'react-dnd';
55
import { HTML5Backend } from 'react-dnd-html5-backend';
66
import AppContainer from '../containers/AppContainer';
7-
import { initialState, stateContext } from '../context/context';
7+
import { stateContext } from '../context/context';
8+
import initialState from '../context/initialState';
89
import reducer from '../reducers/componentReducerNew';
910
// import { Context, State } from '../interfaces/InterfacesNew';
1011

src/components/left/ComponentPanelItemNew.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import Grid from '@material-ui/core/Grid';
3-
3+
import { stateContext } from '../../context/context';
44
import { useDrag } from 'react-dnd';
55
import { ItemTypes } from '../../constants/ItemTypes';
66

@@ -10,19 +10,27 @@ const ComponentPanelItem: React.FC<{
1010
root: Boolean;
1111
focusClick: any;
1212
}> = ({ name, id, root, focusClick }) => {
13+
const [state, dispatch] = useContext(stateContext);
1314
// useDrag hook allows components in left panel to be drag source
1415
const [{ isDragging }, drag] = useDrag({
1516
item: {
1617
type: ItemTypes.INSTANCE,
1718
newInstance: true,
1819
instanceType: 'Component',
19-
instanceId: id
20+
instanceTypeId: id
2021
},
2122
canDrag: !root,
2223
collect: (monitor: any) => ({
2324
isDragging: !!monitor.isDragging()
2425
})
2526
});
27+
28+
const handleClick = () => {
29+
dispatch({
30+
type: 'CHANGE FOCUS',
31+
payload: { componentId: id, childId: null }
32+
});
33+
};
2634
return (
2735
<Grid
2836
item
@@ -41,7 +49,7 @@ const ComponentPanelItem: React.FC<{
4149
borderRadius: root ? '' : '8px'
4250
}}
4351
>
44-
<div className="compPanelItem" onClick={focusClick}>
52+
<div className="compPanelItem" onClick={handleClick}>
4553
<h3>
4654
{id} {name}
4755
</h3>

src/components/left/HTMLPanelNew.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ import Grid from '@material-ui/core/Grid';
1111
import Typography from '@material-ui/core/Typography';
1212
import { useDrag } from 'react-dnd';
1313
import { ItemTypes } from '../../constants/ItemTypes';
14-
import HTMLTypes from '../../HTMLTypes';
14+
import HTMLTypes from '../../context/HTMLTypes';
1515

1616
const HTMLPanel = (): JSX.Element => {
17-
console.log(HTMLTypes);
1817
const options = HTMLTypes.map(option => {
1918
const [{ isDragging }, drag] = useDrag({
2019
item: {
2120
type: ItemTypes.INSTANCE,
2221
newInstance: true,
2322
instanceType: 'HTML Element',
2423
name: option.name,
25-
instanceId: option.id
24+
instanceTypeId: option.id
2625
},
2726
collect: (monitor: any) => ({
2827
isDragging: !!monitor.isDragging()

src/components/main/CanvasNew.tsx

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ function Canvas() {
1818
);
1919

2020
// function to add a new child to the canvas
21-
const addChild = (instanceType: string, instanceTypeId: number) => {
21+
const addChild = (
22+
instanceType: string,
23+
instanceTypeId: number,
24+
childId: number
25+
) => {
2226
dispatch({
2327
type: 'ADD CHILD',
24-
payload: { type: instanceType, typeId: instanceTypeId }
28+
payload: { type: instanceType, typeId: instanceTypeId, childId: childId }
2529
});
2630
};
2731

@@ -30,14 +34,35 @@ function Canvas() {
3034
dispatch({ type: 'CHANGE FOCUS', payload: { componentId, childId } });
3135
};
3236

33-
// onClickHandler is responsible for changing the focus of the "focused" component to be the canvas when the canvas is clicked
34-
const onClickHandler = () => {
35-
console.log('You have clicked the CANVAS!');
36-
// keep the component focus the same as the current state, but update the child focus to null
37-
// a "null" value for the child component signifies that we're solely focusing on the parent component
38-
changeFocus(state.canvasFocus.componentId, null);
37+
// function to change position of element dragged inside div, so that the canvas is the new parent
38+
const changePosition = (
39+
instanceType: string,
40+
instanceTypeId: number,
41+
currentChildId: number,
42+
newParentChildId: number | null,
43+
style: object,
44+
attributes: object
45+
) => {
46+
dispatch({
47+
type: 'CHANGE POSITION',
48+
payload: {
49+
type: instanceType,
50+
typeId: instanceTypeId,
51+
currentChildId: currentChildId,
52+
newParentChildId: newParentChildId,
53+
style: style,
54+
attributes: attributes
55+
}
56+
});
3957
};
4058

59+
// onClickHandler is responsible for changing the focused component and child component
60+
function onClickHandler(event) {
61+
event.stopPropagation();
62+
// note: a null value for the child id means that we are focusing on the top-level component rather than any child
63+
changeFocus(state.canvasFocus.componentId, null);
64+
}
65+
4166
// This hook will allow the user to drag items from the left panel on to the canvas
4267
const [{ isOver }, drop] = useDrop({
4368
accept: ItemTypes.INSTANCE,
@@ -46,9 +71,21 @@ function Canvas() {
4671
if (didDrop) {
4772
return;
4873
}
49-
console.log('Item dropped on the canvas: ', item);
5074
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
51-
if (item.newInstance) addChild(item.instanceType, item.instanceId);
75+
if (item.newInstance) {
76+
addChild(item.instanceType, item.instanceTypeId, null);
77+
}
78+
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
79+
else {
80+
changePosition(
81+
item.instanceType,
82+
item.instanceTypeId,
83+
item.childId,
84+
null,
85+
item.style,
86+
item.attributes
87+
);
88+
}
5289
},
5390
collect: monitor => ({
5491
isOver: !!monitor.isOver()

src/components/main/DirectChildComponentNew.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useContext, useRef } from 'react';
1+
import React, { useMemo, useContext, useRef, Children } from 'react';
22
import {
33
State,
44
Component,
@@ -10,10 +10,17 @@ import { ItemTypes } from '../../constants/ItemTypes';
1010
import { stateContext } from '../../context/context';
1111
import { combineStyles } from '../../helperFunctions/combineStyles';
1212
import IndirectChild from './IndirectChildNew';
13-
import HTMLTypes from '../../HTMLTypes';
13+
import HTMLTypes from '../../context/HTMLTypes';
1414
import globalDefaultStyle from '../../globalDefaultStyles';
1515

16-
function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
16+
function DirectChildComponent({
17+
childId,
18+
type,
19+
typeId,
20+
style,
21+
attributes,
22+
children
23+
}: ChildElement) {
1724
const [state, dispatch] = useContext(stateContext);
1825
const ref = useRef(null);
1926

@@ -28,7 +35,12 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
2835
item: {
2936
type: ItemTypes.INSTANCE,
3037
newInstance: false,
31-
id: childId
38+
childId: childId,
39+
instanceType: type,
40+
instanceTypeId: typeId,
41+
style: style,
42+
attributes: attributes,
43+
children: []
3244
},
3345
// canDrag: !props.children.length,
3446
collect: (monitor: any) => ({
@@ -40,16 +52,10 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
4052
dispatch({ type: 'CHANGE FOCUS', payload: { componentId, childId } });
4153
};
4254

43-
// onClickHandler is responsible for changing the focus of the "focused" component to be the canvas when the canvas is clicked
44-
// TODO: stopPropogation is returning a CORS error
55+
// onClickHandler is responsible for changing the focused component and child component
4556
function onClickHandler(event) {
46-
// console.log(event);
47-
// event.stopPropogation();
48-
49-
console.log('You have clicked a DIRECT CHILD COMPONENT: ', childId);
50-
// keep the component focus the same as the current state, but update the child focus to null
51-
// a "null" value for the child component signifies that we're solely focusing on the parent component
52-
// changeFocus(state.canvasFocus.componentId, childId);
57+
event.stopPropagation();
58+
changeFocus(state.canvasFocus.componentId, childId);
5359
}
5460

5561
// combine all styles so that higher priority style specifications overrule lower priority style specifications
@@ -106,7 +112,7 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
106112
});
107113
};
108114
return (
109-
<div onClick={() => onClickHandler(event)} style={combinedStyle} ref={drag}>
115+
<div onClick={onClickHandler} style={combinedStyle} ref={drag}>
110116
{renderIndirectChildren(referencedComponent)}
111117
</div>
112118
);

src/components/main/DirectChildHTMLNestableNew.tsx

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useDrag, useDrop, DropTargetMonitor } from 'react-dnd';
99
import { ItemTypes } from '../../constants/ItemTypes';
1010
import { stateContext } from '../../context/context';
1111
import { combineStyles } from '../../helperFunctions/combineStyles';
12-
import HTMLTypes from '../../HTMLTypes';
12+
import HTMLTypes from '../../context/HTMLTypes';
1313
import globalDefaultStyle from '../../globalDefaultStyles';
1414
import renderChildren from '../../helperFunctions/renderChildren';
1515

@@ -36,7 +36,12 @@ function DirectChildHTMLNestable({
3636
item: {
3737
type: ItemTypes.INSTANCE,
3838
newInstance: false,
39-
id: childId
39+
childId: childId,
40+
instanceType: type,
41+
instanceTypeId: typeId,
42+
style: style,
43+
attributes: attributes,
44+
children: children
4045
},
4146
// canDrag: !props.children.length,
4247
collect: (monitor: any) => ({
@@ -55,27 +60,69 @@ function DirectChildHTMLNestable({
5560
}
5661
// const hoverId = id;
5762
// updates state with new instance
58-
console.log('Item was dropped here: ', item);
63+
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
64+
if (item.newInstance) {
65+
addChild(item.instanceType, item.instanceTypeId, childId);
66+
}
67+
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
68+
else {
69+
changePosition(
70+
item.instanceType,
71+
item.instanceTypeId,
72+
item.childId,
73+
childId,
74+
item.style,
75+
item.attributes
76+
);
77+
}
5978
},
6079
collect: (monitor: any) => ({
6180
isOver: !!monitor.isOver({ shallow: true })
6281
})
6382
});
6483

84+
// function to add a new child to the canvas
85+
const addChild = (
86+
instanceType: string,
87+
instanceTypeId: number,
88+
childId: number
89+
) => {
90+
dispatch({
91+
type: 'ADD CHILD',
92+
payload: { type: instanceType, typeId: instanceTypeId, childId: childId }
93+
});
94+
};
95+
96+
// function to change position of element dragged inside div, so that the div is the new parent
97+
const changePosition = (
98+
instanceType: string,
99+
instanceTypeId: number,
100+
currentChildId: number,
101+
newParentChildId: number | null,
102+
style: object,
103+
attributes: object
104+
) => {
105+
dispatch({
106+
type: 'CHANGE POSITION',
107+
payload: {
108+
type: instanceType,
109+
typeId: instanceTypeId,
110+
currentChildId: currentChildId,
111+
newParentChildId: newParentChildId,
112+
style: style,
113+
attributes: attributes
114+
}
115+
});
116+
};
117+
65118
const changeFocus = (componentId: number, childId: number | null) => {
66119
dispatch({ type: 'CHANGE FOCUS', payload: { componentId, childId } });
67120
};
68121

69-
// onClickHandler is responsible for changing the focus of the "focused" component to be the canvas when the canvas is clicked
70-
// TODO: stopPropogation is returning a CORS error
122+
// onClickHandler is responsible for changing the focused component and child component
71123
function onClickHandler(event) {
72-
// console.log(event);
73-
// event.stopPropogation();
74-
75-
console.log('You have clicked a DIRECT CHILD COMPONENT: ', childId);
76-
// keep the component focus the same as the current state, but update the child focus to null
77-
// a "null" value for the child component signifies that we're solely focusing on the parent component
78-
// changeFocus(state.canvasFocus.componentId, childId);
124+
event.stopPropagation();
125+
changeFocus(state.canvasFocus.componentId, childId);
79126
}
80127

81128
// combine all styles so that higher priority style specifications overrule lower priority style specifications
@@ -90,7 +137,7 @@ function DirectChildHTMLNestable({
90137

91138
drag(drop(ref));
92139
return (
93-
<div onClick={() => onClickHandler(event)} style={combinedStyle} ref={ref}>
140+
<div onClick={onClickHandler} style={combinedStyle} ref={ref}>
94141
{renderChildren(children)}
95142
</div>
96143
);

0 commit comments

Comments
 (0)