@@ -32,7 +32,7 @@ export const addComponent = (state, { title }) => {
32
32
. replace ( / [ - _ \s 0 - 9 \W ] + / gi, "" ) ;
33
33
34
34
if ( state . components . find ( comp => comp . title === strippedTitle ) ) {
35
- // alert the user that duplicate component names are not allowed
35
+ window . alert ( `a component with the name: " ${ strippedTitle } " already exists.\n Please think of another name.` ) ;
36
36
return {
37
37
...state
38
38
} ;
@@ -68,19 +68,20 @@ export const addComponent = (state, { title }) => {
68
68
// get the focus component (aka the component were adding the child to)
69
69
70
70
export const addChild = ( state , { title } ) => {
71
- const strippedTitle = title
72
- . replace ( / [ a - z ] + / gi, word => word [ 0 ] . toUpperCase ( ) + word . slice ( 1 ) )
73
- . replace ( / [ - _ \s 0 - 9 \W ] + / gi, "" ) ;
71
+ const strippedTitle = title ;
72
+ // .replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
73
+ // .replace(/[-_\s0-9\W]+/gi, "");
74
74
75
- // view represents the component that this child will live (and be rendered) in
75
+ // view represents the curretn FOCUSED COMPONENT - this is the component where the child is being added to
76
+ // we only add childrent (or do any action) to the focused omconent
76
77
const view = state . components . find (
77
78
comp => comp . title === state . focusComponent . title
78
79
) ;
79
80
80
81
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
81
82
const parentComponent = state . components . find ( comp => comp . title === title ) ;
82
83
83
- console . log ( "view from addChild: " , view ) ;
84
+ // console.log("view from addChild: ", view);
84
85
85
86
const newChild = {
86
87
childId : view . nextChildId . toString ( ) ,
@@ -119,6 +120,60 @@ export const addChild = (state, { title }) => {
119
120
} ;
120
121
} ;
121
122
123
+ export const deleteChild = ( state , { } ) => {
124
+ // The child to delete is the FOCUSED child
125
+ // THE parent is the FOCUSED component
126
+
127
+ console . log ( ( `Focused Component (parent) id:${ state . focusComponent . id } title:${ state . focusComponent . title }
128
+ Focused Child (the one 2 be deleted) ChildID:${ state . focusChild . childId } ` )
129
+ )
130
+
131
+ if ( ! state . focusComponent . id ) return window . alert ( `Cannot delete Child if Focused component id = ZERO` )
132
+ if ( ! state . focusChild . childId ) return window . alert ( `Cannot delete Child if Focused Child id = ZERO` )
133
+ // I'm not sure what the "focused component" is (a reference, a copy or ?
134
+ // So, let's get a reference to the focused component ..
135
+ const currFocusedComponent = state . components . find ( c => c . id == state . focusComponent . id ) ;
136
+ console . log ( 'currFocusedComponent: ' , currFocusedComponent )
137
+ // copy the Children Array of the foxused component
138
+
139
+ console . log ( 'Wtf ?' )
140
+ const copyOfChildrenArray = [ ...currFocusedComponent . childrenArray ]
141
+ console . log ( 'copyOfChildrenArray' , copyOfChildrenArray )
142
+ console . log ( 'after copy : state.focusChild.childId:' , state . focusChild . childId )
143
+ // delete the SELECTED CHILD from the copied array
144
+ const indexToDelete = copyOfChildrenArray . findIndex ( elem => elem . childId == state . focusChild . childId )
145
+ console . log ( `Index to delete : ${ indexToDelete } ` )
146
+
147
+ if ( indexToDelete < 0 ) return window . alert ( `DeleteChild speaking here. The chils u r trying to delete was not found` ) ;
148
+
149
+ copyOfChildrenArray . splice ( indexToDelete , 1 )
150
+
151
+ //Make a copy of the focused component - this is the one we will modify
152
+ const copyOfFocusedComponent = {
153
+ ...currFocusedComponent ,
154
+ childrenArray : copyOfChildrenArray
155
+ }
156
+
157
+ console . log ( `copyOfFocusedComponent:` )
158
+ console . log ( copyOfFocusedComponent )
159
+ // copy the entire compoenents array from state
160
+ const modifiedComponentArray = [
161
+ ...state . components . filter ( c => c . id !== currFocusedComponent . id ) , // all elements besides the one just changed
162
+ copyOfFocusedComponent
163
+ ]
164
+ console . log ( 'modifiedComponentArray' )
165
+ console . log ( modifiedComponentArray )
166
+
167
+ // RETURN - update state...
168
+ return {
169
+ ...state ,
170
+ components : modifiedComponentArray ,
171
+ focusComponent : copyOfFocusedComponent , // problem! the delete works on State.components, but not on FOcus component.. starnge..
172
+ focusChild : { } // reset to blank.
173
+
174
+ }
175
+ }
176
+
122
177
export const handleTransform = (
123
178
state ,
124
179
{ componentId, childId, x, y, width, height }
0 commit comments