Skip to content

Commit e9cbe2f

Browse files
MadinventorZerojonocrbuddhajjigaexkevinparkwilliamdyoon
committed
Fixed several bugs. Details in PR.
Co-authored-by: jonocr <[email protected]> Co-authored-by: buddhajjigae <[email protected]> Co-authored-by: xkevinpark <[email protected]> Co-authored-by: williamdyoon <[email protected]>
1 parent 22c66be commit e9cbe2f

File tree

6 files changed

+67
-29
lines changed

6 files changed

+67
-29
lines changed

app/src/components/main/Canvas.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,22 @@ function Canvas() {
5656
}
5757
});
5858
}
59-
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
60-
else {
61-
dispatch({
62-
type: 'CHANGE POSITION',
63-
payload: {
64-
// name: item.name,
65-
currentChildId: item.childId,
66-
newParentChildId: null
67-
}
68-
});
69-
}
59+
// Caret Start - This code is never used. Adding a new child element to an existing element
60+
// or moving existing element to nest in another element is handled by DirectChildHTMLNestable
61+
//
62+
//if item is not a new instance, change position of element dragged inside div so that the div is the new parent
63+
// else {
64+
// console.log("Changed Position, this is not doing shit");
65+
// dispatch({
66+
// type: 'CHANGE POSITION',
67+
// payload: {
68+
// // name: item.name,
69+
// currentChildId: item.childId,
70+
// newParentChildId: null
71+
// }
72+
// });
73+
// }
74+
// Caret End
7075
},
7176
collect: monitor => ({
7277
isOver: !!monitor.isOver()

app/src/components/main/DemoRender.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const DemoRender = (props): JSX.Element => {
3131
if (elementType !== 'input' && elementType !== 'img' && element.children.length > 0) {
3232
renderedChildren = componentBuilder(element.children);
3333
}
34-
if (elementType === 'input' || elementType === 'img') componentsToRender.push(<Box component={elementType} className={classRender} style={elementStyle} key={key} id={childId}>{innerText}</Box>);
34+
if (elementType === 'input' || elementType === 'img') componentsToRender.push(<Box component={elementType} className={classRender} style={elementStyle} key={key} id={childId}></Box>);
3535
else componentsToRender.push(<Box component={elementType} className={classRender} style={elementStyle} key={key} id={childId}>{innerText}{renderedChildren}</Box>);
3636
key += 1;
3737
}
@@ -40,11 +40,12 @@ const DemoRender = (props): JSX.Element => {
4040
};
4141

4242
useEffect(() => {
43-
const childrenArray = state.components[0].children;
43+
const focusIndex = state.canvasFocus.componentId - 1;
44+
const childrenArray = state.components[focusIndex].children;
4445
console.log('Refrenced Children in State!!!', childrenArray);
4546
const renderedComponents = componentBuilder(childrenArray);
4647
setComponents(renderedComponents);
47-
}, [state.components]);
48+
}, [state.components, state.canvasFocus]);
4849

4950
return (
5051
<div id={'renderFocus'} style={demoContainerStyle}>

app/src/components/main/DirectChildHTMLNestable.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Modal from '@material-ui/core/Modal';
1010
import Annotation from './Annotation'
1111
// Caret
1212
import { makeStyles } from '@material-ui/core';
13+
import validateNewParent from '../../helperFunctions/changePositionValidation'
1314
// Caret
1415
import TextField from '@material-ui/core/TextField';
1516
import uniqid from 'uniqid';
@@ -73,6 +74,7 @@ const snapShotFunc = () => {
7374
// updates state with new instance
7475
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
7576
if (item.newInstance) {
77+
console.log("Child added directly to an existing element")
7678
dispatch({
7779
type: 'ADD CHILD',
7880
payload: {
@@ -84,13 +86,16 @@ const snapShotFunc = () => {
8486
}
8587
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
8688
else {
87-
dispatch({
88-
type: 'CHANGE POSITION',
89-
payload: {
90-
currentChildId: item.childId,
91-
newParentChildId: childId,
92-
}
93-
});
89+
// Caret check to see if the selected child is trying to nest within itself
90+
if (validateNewParent(state, item.childId, childId) === true) {
91+
dispatch({
92+
type: 'CHANGE POSITION',
93+
payload: {
94+
currentChildId: item.childId,
95+
newParentChildId: childId,
96+
}
97+
});
98+
}
9499
}
95100
},
96101

app/src/components/main/SeparatorChild.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import StateContext from '../../context/context';
66
import { combineStyles } from '../../helperFunctions/combineStyles';
77
import globalDefaultStyle from '../../public/styles/globalDefaultStyles';
88
import renderChildren from '../../helperFunctions/renderChildren';
9+
import validateNewParent from '../../helperFunctions/changePositionValidation'
910

1011
function DirectChildHTMLNestable({
1112
childId,
@@ -64,13 +65,16 @@ function DirectChildHTMLNestable({
6465
}
6566
// if item is not a new instance, change position of element dragged inside separator so that separator is new parent (until replacement)
6667
else {
67-
dispatch({
68-
type: 'CHANGE POSITION',
69-
payload: {
70-
currentChildId: item.childId,
71-
newParentChildId: childId
72-
}
73-
});
68+
// Caret check to see if the selected child is trying to nest within itself
69+
if (validateNewParent(state, item.childId, childId) === true) {
70+
dispatch({
71+
type: 'CHANGE POSITION',
72+
payload: {
73+
currentChildId: item.childId,
74+
newParentChildId: childId
75+
}
76+
});
77+
}
7478
}
7579
},
7680

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 100% Caret
2+
// This function will evaluate the target destination when moving an element on the canvas
3+
// If the target destination is actually a nested component within its own children array
4+
// the new target parent is not a valid parent to change position
5+
const validateNewParent = (state: object, currentChildId: number, toTargetParentId: number) => {
6+
const focusIndex = state.canvasFocus.componentId - 1;
7+
const childrenArray = state.components[focusIndex].children;
8+
// Checks to see if a Parent is trying to nest inside one of its children
9+
const selfNestingCheck = (array, nestedChild = false, nestedParent = false) => {
10+
for (const element of array) {
11+
if (element.childId === toTargetParentId && nestedChild === true) return nestedParent = true;
12+
else if (element.childId === currentChildId && element.children.length > 0 && nestedChild === false) nestedParent = selfNestingCheck(element.children, nestedChild = true, nestedParent);
13+
else if (element.children.length > 0 && nestedChild === false) nestedParent = selfNestingCheck(element.children, nestedChild, nestedParent);
14+
}
15+
return nestedParent;
16+
}
17+
const parentNestingIntoChild = selfNestingCheck(childrenArray);
18+
console.log("Is a Parent trying to nest inside one of it's children? True fails, False passes,", parentNestingIntoChild);
19+
if (parentNestingIntoChild === true) return false;
20+
return true;
21+
};
22+
23+
export default validateNewParent;

app/src/helperFunctions/manageSeparators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
4949
// this function replaces separators onto which an element is dropped with the element itself
5050
manageSeparators.mergeSeparator = (arr: object[], index: number) => {
5151
return arr.map((child) => {
52-
if (child.name === 'div' || child.name === 'form' && child.children.length) {
52+
if ((child.name === 'div' || child.name === 'form') && child.children.length) {
5353
const divContents = manageSeparators.mergeSeparator(child.children, index);
5454
return { ...child, children: divContents }
5555
}

0 commit comments

Comments
 (0)