Skip to content

Commit

Permalink
Merge pull request #9 from MadinventorZero/bugbusting
Browse files Browse the repository at this point in the history
Fixed several bugs. Details in PR.
  • Loading branch information
jonocr authored Apr 26, 2021
2 parents 22c66be + e9cbe2f commit 7bbdd47
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
27 changes: 16 additions & 11 deletions app/src/components/main/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,22 @@ function Canvas() {
}
});
}
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
else {
dispatch({
type: 'CHANGE POSITION',
payload: {
// name: item.name,
currentChildId: item.childId,
newParentChildId: null
}
});
}
// Caret Start - This code is never used. Adding a new child element to an existing element
// or moving existing element to nest in another element is handled by DirectChildHTMLNestable
//
//if item is not a new instance, change position of element dragged inside div so that the div is the new parent
// else {
// console.log("Changed Position, this is not doing shit");
// dispatch({
// type: 'CHANGE POSITION',
// payload: {
// // name: item.name,
// currentChildId: item.childId,
// newParentChildId: null
// }
// });
// }
// Caret End
},
collect: monitor => ({
isOver: !!monitor.isOver()
Expand Down
7 changes: 4 additions & 3 deletions app/src/components/main/DemoRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const DemoRender = (props): JSX.Element => {
if (elementType !== 'input' && elementType !== 'img' && element.children.length > 0) {
renderedChildren = componentBuilder(element.children);
}
if (elementType === 'input' || elementType === 'img') componentsToRender.push(<Box component={elementType} className={classRender} style={elementStyle} key={key} id={childId}>{innerText}</Box>);
if (elementType === 'input' || elementType === 'img') componentsToRender.push(<Box component={elementType} className={classRender} style={elementStyle} key={key} id={childId}></Box>);
else componentsToRender.push(<Box component={elementType} className={classRender} style={elementStyle} key={key} id={childId}>{innerText}{renderedChildren}</Box>);
key += 1;
}
Expand All @@ -40,11 +40,12 @@ const DemoRender = (props): JSX.Element => {
};

useEffect(() => {
const childrenArray = state.components[0].children;
const focusIndex = state.canvasFocus.componentId - 1;
const childrenArray = state.components[focusIndex].children;
console.log('Refrenced Children in State!!!', childrenArray);
const renderedComponents = componentBuilder(childrenArray);
setComponents(renderedComponents);
}, [state.components]);
}, [state.components, state.canvasFocus]);

return (
<div id={'renderFocus'} style={demoContainerStyle}>
Expand Down
19 changes: 12 additions & 7 deletions app/src/components/main/DirectChildHTMLNestable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Modal from '@material-ui/core/Modal';
import Annotation from './Annotation'
// Caret
import { makeStyles } from '@material-ui/core';
import validateNewParent from '../../helperFunctions/changePositionValidation'
// Caret
import TextField from '@material-ui/core/TextField';
import uniqid from 'uniqid';
Expand Down Expand Up @@ -73,6 +74,7 @@ const snapShotFunc = () => {
// updates state with new instance
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
if (item.newInstance) {
console.log("Child added directly to an existing element")
dispatch({
type: 'ADD CHILD',
payload: {
Expand All @@ -84,13 +86,16 @@ const snapShotFunc = () => {
}
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
else {
dispatch({
type: 'CHANGE POSITION',
payload: {
currentChildId: item.childId,
newParentChildId: childId,
}
});
// Caret check to see if the selected child is trying to nest within itself
if (validateNewParent(state, item.childId, childId) === true) {
dispatch({
type: 'CHANGE POSITION',
payload: {
currentChildId: item.childId,
newParentChildId: childId,
}
});
}
}
},

Expand Down
18 changes: 11 additions & 7 deletions app/src/components/main/SeparatorChild.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import StateContext from '../../context/context';
import { combineStyles } from '../../helperFunctions/combineStyles';
import globalDefaultStyle from '../../public/styles/globalDefaultStyles';
import renderChildren from '../../helperFunctions/renderChildren';
import validateNewParent from '../../helperFunctions/changePositionValidation'

function DirectChildHTMLNestable({
childId,
Expand Down Expand Up @@ -64,13 +65,16 @@ function DirectChildHTMLNestable({
}
// if item is not a new instance, change position of element dragged inside separator so that separator is new parent (until replacement)
else {
dispatch({
type: 'CHANGE POSITION',
payload: {
currentChildId: item.childId,
newParentChildId: childId
}
});
// Caret check to see if the selected child is trying to nest within itself
if (validateNewParent(state, item.childId, childId) === true) {
dispatch({
type: 'CHANGE POSITION',
payload: {
currentChildId: item.childId,
newParentChildId: childId
}
});
}
}
},

Expand Down
23 changes: 23 additions & 0 deletions app/src/helperFunctions/changePositionValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 100% Caret
// This function will evaluate the target destination when moving an element on the canvas
// If the target destination is actually a nested component within its own children array
// the new target parent is not a valid parent to change position
const validateNewParent = (state: object, currentChildId: number, toTargetParentId: number) => {
const focusIndex = state.canvasFocus.componentId - 1;
const childrenArray = state.components[focusIndex].children;
// Checks to see if a Parent is trying to nest inside one of its children
const selfNestingCheck = (array, nestedChild = false, nestedParent = false) => {
for (const element of array) {
if (element.childId === toTargetParentId && nestedChild === true) return nestedParent = true;
else if (element.childId === currentChildId && element.children.length > 0 && nestedChild === false) nestedParent = selfNestingCheck(element.children, nestedChild = true, nestedParent);
else if (element.children.length > 0 && nestedChild === false) nestedParent = selfNestingCheck(element.children, nestedChild, nestedParent);
}
return nestedParent;
}
const parentNestingIntoChild = selfNestingCheck(childrenArray);
console.log("Is a Parent trying to nest inside one of it's children? True fails, False passes,", parentNestingIntoChild);
if (parentNestingIntoChild === true) return false;
return true;
};

export default validateNewParent;
2 changes: 1 addition & 1 deletion app/src/helperFunctions/manageSeparators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
// this function replaces separators onto which an element is dropped with the element itself
manageSeparators.mergeSeparator = (arr: object[], index: number) => {
return arr.map((child) => {
if (child.name === 'div' || child.name === 'form' && child.children.length) {
if ((child.name === 'div' || child.name === 'form') && child.children.length) {
const divContents = manageSeparators.mergeSeparator(child.children, index);
return { ...child, children: divContents }
}
Expand Down

0 comments on commit 7bbdd47

Please sign in to comment.