Skip to content

Commit cfb848d

Browse files
buddhajjigaejonocrxkevinparkwilliamdyoonMadinventorZero
committed
Began working on saving annotations to state to keep them persistent between root level components and for readme export purposes. Changed styling of annotation textarea and header. Added requirements for state in Interfaces
Co-authored-by: jonocr <[email protected]> Co-authored-by: xkevinpark <[email protected]> Co-authored-by: williamdyoon <[email protected]> Co-authored-by: MadinventorZero <[email protected]>
2 parents 5ca085e + 7bbdd47 commit cfb848d

File tree

9 files changed

+128
-41
lines changed

9 files changed

+128
-41
lines changed

app/src/components/main/Annotation.tsx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// Caret Start
2-
import React, { useRef } from 'react';
2+
import React, {
3+
useRef, useState, useContext, useEffect,
4+
} from 'react';
35
import { Annotations } from '../../interfaces/Interfaces';
46
import Modal from '@material-ui/core/Modal';
7+
import StateContext from '../../context/context';
58

69
function Annotation({
710
id,
811
name,
912
}: Annotations) {
13+
const [annotations, setAnnotations] = useState('');
14+
const [state, dispatch] = useContext(StateContext);
1015
const ref = useRef(null);
1116

1217
// React hook setting the annotation button modal open/close state
@@ -25,20 +30,45 @@ function Annotation({
2530
// Handles when text exists in the textarea of the modal. If text exists/does not exist, corresponding button changes colors.
2631
const handleAnnoChange = (event) => {
2732
const { value } = event.target;
33+
const focusIndex = state.canvasFocus.componentId - 1;
34+
const childrenArray = state.components[focusIndex].children;
2835

2936
if(value === '') {
3037
ref.current.style.background = '#3ec1ac';
3138
ref.current.id = 'btn' + event.target.id;
3239
} else {
3340
ref.current.style.background = '#cc99ff';
3441
ref.current.id = 'btn' + event.target.id;
42+
setAnnotations(value);
43+
44+
let childEle = handleSaveAnno(childrenArray, event.target.id);
45+
46+
if(childEle) {
47+
childEle.annotations = annotations;
48+
setState(childEle.annotations);
49+
}
50+
}
51+
}
52+
53+
54+
const handleSaveAnno = (array, id) => {
55+
for(let i = 0; i < array.length; i++) {
56+
if(array[i].childId === id) {
57+
return array[i];
58+
}
59+
if(array[i].children.length > 0) {
60+
return handleSaveAnno(array[i], id);
61+
}
3562
}
3663
}
3764

3865
const body = (
39-
<div>
40-
<p className='annotate-textarea-header'>Notes for: {name} ( {id} )</p>
66+
<div className='annotate-position'>
67+
<span className='annotate-textarea-header'>Notes for: {name} ( {id} )</span>
4168
<textarea className='annotate-textarea' id={id.toString()} onChange={handleAnnoChange}></textarea>
69+
<span className='annotate-textarea-footer'>
70+
<button className='annotate-textarea-savebutton'>Save Notes</button>
71+
</span>
4272
</div>
4373
)
4474

@@ -51,7 +81,6 @@ function Annotation({
5181
keepMounted={true}
5282
>
5383
{body}
54-
5584
</Modal>
5685
</div>
5786
);

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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const snapShotFunc = () => {
6868
// updates state with new instance
6969
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
7070
if (item.newInstance) {
71+
console.log("Child added directly to an existing element")
7172
dispatch({
7273
type: 'ADD CHILD',
7374
payload: {
@@ -79,13 +80,16 @@ const snapShotFunc = () => {
7980
}
8081
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
8182
else {
82-
dispatch({
83-
type: 'CHANGE POSITION',
84-
payload: {
85-
currentChildId: item.childId,
86-
newParentChildId: childId,
87-
}
88-
});
83+
// Caret check to see if the selected child is trying to nest within itself
84+
if (validateNewParent(state, item.childId, childId) === true) {
85+
dispatch({
86+
type: 'CHANGE POSITION',
87+
payload: {
88+
currentChildId: item.childId,
89+
newParentChildId: childId,
90+
}
91+
});
92+
}
8993
}
9094
},
9195

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
}

app/src/interfaces/Interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface ChildElement {
2323
style: object;
2424
attributes?: object;
2525
children?: ChildElement[];
26+
// Caret
27+
annotations?: string;
2628
}
2729
export interface Component {
2830
id: number;
@@ -36,6 +38,8 @@ export interface Component {
3638
past: any[];
3739
future: any[];
3840
stateProps: StateProp[]; // state: [ { key: value, type }, {key: value, type}, {key: value, type} ]
41+
// Caret
42+
annotations?: string;
3943
}
4044

4145
export interface StateProp {

app/src/public/styles/style.css

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
box-sizing: inherit;
55
}
66

7+
@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');
78
@import url('https://fonts.googleapis.com/css2?family=Raleway&display=swap');
89

910
html {
@@ -418,27 +419,43 @@ ANNOTATIONS
418419
float: right;
419420
}
420421

421-
.annotate-textarea {
422-
width: 600px;
423-
height: 300px;
422+
.annotate-position {
423+
display: flex;
424+
flex-direction: column;
424425
position: fixed;
425-
top: 40%;
426+
align-items:flex-start;
427+
top: 30%;
426428
left: 30%;
429+
}
427430

431+
.annotate-textarea {
432+
width: 600px;
433+
height: 300px;
428434
resize: none;
429435
white-space: pre-line;
436+
font-size: 18px;
430437
}
431438

432439
.annotate-textarea-header {
433440
font-size: 35px;
434-
position: fixed;
435-
padding-left: 8px;
436441
background-color: #4772c7;
437442
color: rgb(241, 240, 240);
438443
width: 600px;
439-
top: 33%;
440-
left: 30%;
444+
font-family: 'Open Sans', sans-serif;
445+
}
446+
447+
.annotate-textarea-footer {
448+
background-color: #4772c7;
449+
color: rgb(107, 93, 93);
450+
width: 600px;
451+
}
441452

453+
.annotate-textarea-savebutton {
454+
float: right;
455+
width: 150px;
456+
height: 30px;
457+
font-size: 20px;
458+
margin: 2.5px 2.5px;
442459
}
443460
/* Caret End
444461

0 commit comments

Comments
 (0)