Skip to content

Commit 30375e6

Browse files
buddhajjigaejonocrxkevinparkwilliamdyoonMadinventorZero
committed
Created a useEffect to allow annotations to remain persistent between root components (i.e. changing between root components on the right panel)
Co-authored-by: jonocr <[email protected]> Co-authored-by: xkevinpark <[email protected]> Co-authored-by: williamdyoon <[email protected]> Co-authored-by: MadinventorZero <[email protected]>
1 parent 2b3dd68 commit 30375e6

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

app/src/components/main/Annotation.tsx

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,85 @@ import StateContext from '../../context/context';
99
function Annotation({
1010
id,
1111
name,
12+
annotations,
1213
}: Annotations) {
13-
const [annotations, setAnnotations] = useState('');
1414
const [state, dispatch] = useContext(StateContext);
15+
const [annotation, setAnnotations] = useState('');
1516
const ref = useRef(null);
16-
1717
// React hook setting the annotation button modal open/close state
1818
const [open, setOpen] = React.useState(false);
19-
19+
const focusIndex = state.canvasFocus.componentId - 1;
20+
const childrenArray = state.components[focusIndex].children;
21+
2022
// For showing the modal
2123
const handleOpen = (id) => {
24+
const childEle = handleFindAnno(childrenArray, id);
2225
setOpen(true);
2326
};
2427

2528
// For closing the modal
26-
const handleClose = () => {
29+
const handleClose = (id) => {
30+
const focusIndex = state.canvasFocus.componentId - 1;
31+
const childrenArray = state.components[focusIndex].children;
32+
const childEle = handleFindAnno(childrenArray, id);
33+
34+
if(childEle) {
35+
childEle.annotations = annotation;
36+
}
37+
2738
setOpen(false);
2839
};
2940

30-
// Handles when text exists in the textarea of the modal. If text exists/does not exist, corresponding button changes colors.
41+
/**
42+
* Handles when text exists in the textarea of the modal.
43+
* If text exists/does not exist, corresponding button changes colors.
44+
*/
3145
const handleAnnoChange = (event) => {
3246
const { value } = event.target;
33-
const focusIndex = state.canvasFocus.componentId - 1;
34-
const childrenArray = state.components[focusIndex].children;
3547

36-
if(value === '') {
48+
if(value === '' || value === undefined) {
3749
ref.current.style.background = '#3ec1ac';
3850
ref.current.id = 'btn' + event.target.id;
51+
setAnnotations(value);
3952
} else {
4053
ref.current.style.background = '#cc99ff';
4154
ref.current.id = 'btn' + event.target.id;
4255
setAnnotations(value);
43-
44-
let childEle = handleSaveAnno(childrenArray, event.target.id);
45-
46-
if(childEle) {
47-
childEle.annotations = annotations;
48-
setState(childEle.annotations);
49-
}
5056
}
5157
}
5258

53-
54-
const handleSaveAnno = (array, id) => {
59+
/**
60+
* This handler will find the specific anno for the corresponding component on the canvas in the childrenArray -
61+
* where the canvas components are placed
62+
*/
63+
const handleFindAnno = (array, id) => {
5564
for(let i = 0; i < array.length; i++) {
56-
if(array[i].childId === id) {
65+
if(array[i].childId === Number(id)) {
5766
return array[i];
5867
}
68+
5969
if(array[i].children.length > 0) {
60-
return handleSaveAnno(array[i], id);
70+
return handleFindAnno(array[i], id);
6171
}
6272
}
6373
}
6474

65-
/*
66-
<span className='annotate-textarea-footer'>
67-
<button className='annotate-textarea-savebutton'>Save Notes</button>
68-
</span>
69-
*/
75+
/**
76+
* This useEffect allows the annotations to remain persistent when changing between root level components on the right panel
77+
*/
78+
useEffect(() => {
79+
const event = {
80+
target : { value: handleFindAnno(childrenArray, id).annotations},
81+
id : id,
82+
}
83+
84+
handleAnnoChange(event);
85+
}, [state.canvasFocus])
86+
7087
const body = (
7188
<div className='annotate-position'>
7289
<span className='annotate-textarea-header'>Notes for: {name} ( {id} )</span>
73-
<textarea className='annotate-textarea' id={id.toString()} onChange={handleAnnoChange}></textarea>
90+
<textarea className='annotate-textarea' id={id.toString()} onChange={handleAnnoChange}>{handleFindAnno(childrenArray, id).annotations}</textarea>
7491
</div>
7592
)
7693

@@ -79,7 +96,7 @@ function Annotation({
7996
<button className='annotate-button-empty' id={"btn" + id} onClick={() => handleOpen(id)} ref={ref}>Notes</button>
8097
<Modal
8198
open={open}
82-
onClose={handleClose}
99+
onClose={() => handleClose(id)}
83100
keepMounted={true}
84101
>
85102
{body}

app/src/components/main/Canvas.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ function Canvas() {
5252
type: item.instanceType,
5353
typeId: item.instanceTypeId,
5454
childId: null
55-
5655
}
5756
});
5857
}

app/src/components/main/DirectChildHTML.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function DirectChildHTML({
1717
type,
1818
typeId,
1919
style,
20+
annotations,
2021
}: ChildElement) {
2122
const [state, dispatch] = useContext(StateContext);
2223
const ref = useRef(null);
@@ -75,6 +76,7 @@ function DirectChildHTML({
7576
<Annotation
7677
id={childId}
7778
name={name}
79+
annotations={annotations}
7880
/>
7981
{/* Caret end */}
8082
</div>

app/src/components/main/DirectChildHTMLNestable.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import globalDefaultStyle from '../../public/styles/globalDefaultStyles';
88
import renderChildren from '../../helperFunctions/renderChildren';
99
// Caret
1010
import Annotation from './Annotation'
11+
import validateNewParent from '../../helperFunctions/changePositionValidation'
1112

1213
function DirectChildHTMLNestable({
1314
childId,
@@ -16,6 +17,7 @@ function DirectChildHTMLNestable({
1617
style,
1718
children,
1819
name,
20+
annotations,
1921
}: ChildElement) {
2022
const [state, dispatch] = useContext(StateContext);
2123
const ref = useRef(null);
@@ -137,6 +139,7 @@ const snapShotFunc = () => {
137139
<Annotation
138140
id={childId}
139141
name={name}
142+
annotations={annotations}
140143
/>
141144
{renderChildren(children)}
142145
{/* Caret end */}

app/src/components/main/IndirectChild.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import StateContext from '../../context/context';
55
import { Component } from '../../interfaces/Interfaces';
66
import Annotation from './Annotation'
77

8-
function IndirectChild({ style, children, placeHolder, linkId, childId, name }) {
8+
function IndirectChild({ style, children, placeHolder, linkId, childId, name, annotations }) {
99
const [state, dispatch] = useContext(StateContext);
1010
let combinedStyle = combineStyles(globalDefaultStyle, style);
1111

@@ -39,6 +39,7 @@ function IndirectChild({ style, children, placeHolder, linkId, childId, name })
3939
<Annotation
4040
id={childId}
4141
name={name}
42+
annotations={annotations}
4243
/>
4344
{/* Caret end */}
4445
</div>

app/src/interfaces/Interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ export interface LoginInt {
9696
export interface Annotations {
9797
id: number;
9898
name: string;
99+
annotations: string;
99100
}
100101
// Caret end

0 commit comments

Comments
 (0)