@@ -107,6 +107,107 @@ export const closeExpanded = (components: ComponentState[], id? : number): void
107107 } ) ;
108108}
109109
110+ << < < < << HEAD
111+ // This action adds a child to the current focusComponent
112+ // (The component currently selected in LeftContainer)
113+ export const addChild = (
114+ state : ApplicationState ,
115+ { title, childType = '' , HTMLInfo = { } } : { title : string ; childType : string ; HTMLInfo : object } ,
116+ ) => {
117+ const strippedTitle = title ;
118+
119+ if ( ! childType ) {
120+ window . alert ( 'addChild Error! no type specified' ) ;
121+ }
122+
123+ const htmlElement = childType !== 'COMP' ? childType : null ;
124+ if ( childType !== 'COMP' ) {
125+ childType = 'HTML' ;
126+ }
127+
128+ // view represents the current FOCUSED COMPONENT - this is the component where the child is being added to
129+ // we only add children (perform any action) to the focused component
130+ const view : ComponentState = state . components . find ( comp => comp . title === state . focusComponent . title ) ;
131+
132+ // parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
133+ let parentComponent ;
134+
135+ // conditional if adding an HTML component
136+ if ( childType === 'COMP' ) {
137+ parentComponent = state . components . find ( ( comp ) => comp . title === title ) ;
138+ }
139+
140+ interface htmlElemPositionInt {
141+ width : number ;
142+ height : number ;
143+ }
144+
145+ let htmlElemPosition : htmlElemPositionInt = { width : null , height : null } ;
146+ if ( childType === 'HTML' ) {
147+ htmlElemPosition = getSize ( htmlElement ) ;
148+ // if above function doesn't return anything, it means html element is not yet implemented
149+ if ( ! htmlElemPosition . width ) {
150+ console . log (
151+ `Did not add html child: ${ htmlElement } the GetSize function indicated that it isnt in our DB`
152+ ) ;
153+ return ;
154+ }
155+ }
156+ // The postion of the new child is dependent on its parent, the focusComponent (called view here)
157+ const newPosition =
158+ childType === 'COMP'
159+ ? {
160+ //In order to avoid the overlap of newly added children, this adds an x and y offset
161+ x : view . position . x + ( ( view . nextChildId * 16 ) % 150 ) , // new children are offset by some amount, map of 150px
162+ y : view . position . y + ( ( view . nextChildId * 16 ) % 150 ) ,
163+ width : parentComponent . position . width - 1 , // the size of new children is based on that of parent component
164+ height : parentComponent . position . height - 1
165+ }
166+ : {
167+ x : view . position . x + view . nextChildId * 16 ,
168+ y : view . position . y + view . nextChildId * 16 ,
169+ width : htmlElemPosition . width ,
170+ height : htmlElemPosition . height
171+ } ;
172+ // Creation of the actual new child object with all data necessary to render a Rectangle
173+ const newChild : ChildState = {
174+ childId : view . nextChildId ,
175+ childSort : view . nextChildId ,
176+ childType,
177+ childComponentId : childType === 'COMP' ? parentComponent . id : null , // only relevant fot children of type COMPONENT
178+ componentName : strippedTitle ,
179+ position : newPosition ,
180+ color : null , // parentComponent.color, // only relevant for children of type COMPONENT
181+ htmlElement, // only relevant fot children of type HTML
182+ HTMLInfo
183+ } ;
184+
185+ const compsChildrenArr = [ ...view . children , newChild ] ;
186+
187+ const component = {
188+ ...view ,
189+ children : compsChildrenArr ,
190+ focusChildId : newChild . childId ,
191+ nextChildId : view . nextChildId + 1
192+ } ;
193+
194+ const components = [
195+ ...state . components . filter ( ( comp ) => {
196+ if ( comp . title !== view . title ) return comp ;
197+ } ) ,
198+ component
199+ ] ;
200+
201+ return {
202+ ...state ,
203+ components,
204+ focusChild : newChild ,
205+ focusComponent : component // refresh the focus component so we have the new child
206+ } ;
207+ } ;
208+
209+ = === ===
210+ >>> >>> > dev
110211export const deleteChild = (
111212 state : ApplicationState ,
112213 { parentId = state . focusComponent . id , childId = state . focusChild . childId , calledFromDeleteComponent = false } ,
0 commit comments