@@ -257,6 +257,7 @@ const reducer = (state: State, action: Action) => {
257257
258258 const parentComponentId : number = state . canvasFocus . componentId ;
259259 const components = [ ...state . components ] ;
260+ updateAllIds ( components ) ;
260261
261262 // find component that we're adding a child to
262263 const parentComponent = findComponent ( components , parentComponentId ) ;
@@ -327,7 +328,11 @@ const reducer = (state: State, action: Action) => {
327328 componentId : state . canvasFocus . componentId ,
328329 childId : newChild . childId
329330 } ;
330- const nextChildId = state . nextChildId + 1 ;
331+
332+ let nextChildId : number = 1 ;
333+ for ( let i = 0 ; i < parentComponent . children . length ; i += 1 ) {
334+ nextChildId += 1 ;
335+ }
331336 return { ...state , components, nextChildId, canvasFocus } ;
332337 }
333338 // move an instance from one position in a component to another position in a component
@@ -371,17 +376,23 @@ const reducer = (state: State, action: Action) => {
371376 state . HTMLTypes
372377 ) ;
373378
374- return { ...state , components } ;
379+ let nextChildId : number = 1 ;
380+ for ( let i = 0 ; i < component . children . length ; i += 1 ) {
381+ nextChildId += 1 ;
382+ }
383+ updateAllIds ( components ) ;
384+ return { ...state , components, nextChildId } ;
375385 }
376386 // Change the focus component and child
377387 case 'CHANGE FOCUS' : {
378388 const {
379389 componentId,
380390 childId
381391 } : { componentId : number ; childId : number | null } = action . payload ;
382-
383- const canvasFocus = { ...state . canvasFocus , componentId, childId } ;
384- return { ...state , canvasFocus } ;
392+ const canvasFocus = { componentId, childId } ;
393+ const components = [ ...state . components ] ;
394+ updateAllIds ( components ) ;
395+ return { ...state , canvasFocus, components } ;
385396 }
386397 case 'UPDATE CSS' : {
387398 const { style } = action . payload ;
@@ -407,7 +418,7 @@ const reducer = (state: State, action: Action) => {
407418 case 'DELETE CHILD' : {
408419 // if in-focus instance is a top-level component and not a child, don't delete anything
409420
410- if ( ! state . canvasFocus . childId ) return state ;
421+ // if (!state.canvasFocus.childId) return state;
411422
412423 // find the current component in focus
413424 const components = [ ...state . components ] ;
@@ -432,15 +443,23 @@ const reducer = (state: State, action: Action) => {
432443 state . HTMLTypes
433444 ) ;
434445
435- const canvasFocus = { ...state . canvasFocus , childId : null } ;
436- return { ...state , components, canvasFocus } ;
446+ let nextChildId : number = 1 ;
447+ for ( let i = 0 ; i < component . children . length ; i += 1 ) {
448+ nextChildId += 1 ;
449+ }
450+
451+ let childId : null | number = ( ( state . canvasFocus . childId - 1 ) === 0 ) ? null : state . canvasFocus . childId - 1 ;
452+ const canvasFocus = { ...state . canvasFocus , childId } ;
453+ updateAllIds ( components ) ;
454+ return { ...state , components, canvasFocus, nextChildId } ;
437455 }
438456
439457 case 'DELETE PAGE' : {
440458 const id : number = state . canvasFocus . componentId ;
441459 const name : string = state . components [ id - 1 ] . name ;
442460
443461 const components : Component [ ] = deleteById ( id , name ) ;
462+
444463 // rebuild rootComponents with correct page IDs
445464 const rootComponents = updateRoots ( components ) ;
446465 const canvasFocus = { componentId : 1 , childId : null } ;
@@ -455,10 +474,10 @@ const reducer = (state: State, action: Action) => {
455474 const rootComponents : number [ ] = updateRoots ( components ) ;
456475
457476 // iterate over the length of the components array
458- for ( let i = 0 ; i < components . length ; i ++ ) {
477+ components . forEach ( ( el , i ) => {
459478 // for each components' code, run the generateCode function to
460479 // update the code preview on the app
461- components [ i ] . code = generateCode (
480+ el . code = generateCode (
462481 components ,
463482 components [ i ] . id ,
464483 rootComponents ,
@@ -467,6 +486,7 @@ const reducer = (state: State, action: Action) => {
467486 ) ;
468487 }
469488
489+
470490 const canvasFocus = { componentId : 1 , childId : null } ;
471491
472492 return {
@@ -554,13 +574,14 @@ const reducer = (state: State, action: Action) => {
554574
555575 case 'OPEN PROJECT' : {
556576 convertToJSX ( action . payload . HTMLTypes ) ;
577+ // action.payload.canvasFocus = { ...action.payload.canvasFocus, childId: null}
557578 return {
558579 ...action . payload
559580 } ;
560581 }
561582
562583 case 'ADD ELEMENT' : {
563- const HTMLTypes = [ ...state . HTMLTypes ] ;
584+ const HTMLTypes : HTMLType [ ] = [ ...state . HTMLTypes ] ;
564585 HTMLTypes . push ( action . payload ) ;
565586 return {
566587 ...state ,
@@ -589,7 +610,8 @@ const reducer = (state: State, action: Action) => {
589610 } )
590611 return {
591612 ...state ,
592- HTMLTypes
613+ HTMLTypes,
614+ components
593615 } ;
594616 }
595617
0 commit comments