@@ -257,7 +257,6 @@ const reducer = (state: State, action: Action) => {
257257
258258 const parentComponentId : number = state . canvasFocus . componentId ;
259259 const components = [ ...state . components ] ;
260- updateAllIds ( components ) ;
261260
262261 // find component that we're adding a child to
263262 const parentComponent = findComponent ( components , parentComponentId ) ;
@@ -328,11 +327,7 @@ const reducer = (state: State, action: Action) => {
328327 componentId : state . canvasFocus . componentId ,
329328 childId : newChild . childId
330329 } ;
331-
332- let nextChildId : number = 1 ;
333- for ( let i = 0 ; i < parentComponent . children . length ; i += 1 ) {
334- nextChildId += 1 ;
335- }
330+ const nextChildId = state . nextChildId + 1 ;
336331 return { ...state , components, nextChildId, canvasFocus } ;
337332 }
338333 // move an instance from one position in a component to another position in a component
@@ -376,23 +371,17 @@ const reducer = (state: State, action: Action) => {
376371 state . HTMLTypes
377372 ) ;
378373
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 } ;
374+ return { ...state , components } ;
385375 }
386376 // Change the focus component and child
387377 case 'CHANGE FOCUS' : {
388378 const {
389379 componentId,
390380 childId
391381 } : { componentId : number ; childId : number | null } = action . payload ;
392- const canvasFocus = { componentId, childId } ;
393- const components = [ ...state . components ] ;
394- updateAllIds ( components ) ;
395- return { ...state , canvasFocus, components } ;
382+
383+ const canvasFocus = { ...state . canvasFocus , componentId, childId } ;
384+ return { ...state , canvasFocus } ;
396385 }
397386 case 'UPDATE CSS' : {
398387 const { style } = action . payload ;
@@ -418,7 +407,7 @@ const reducer = (state: State, action: Action) => {
418407 case 'DELETE CHILD' : {
419408 // if in-focus instance is a top-level component and not a child, don't delete anything
420409
421- // if (!state.canvasFocus.childId) return state;
410+ if ( ! state . canvasFocus . childId ) return state ;
422411
423412 // find the current component in focus
424413 const components = [ ...state . components ] ;
@@ -443,23 +432,15 @@ const reducer = (state: State, action: Action) => {
443432 state . HTMLTypes
444433 ) ;
445434
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 } ;
435+ const canvasFocus = { ...state . canvasFocus , childId : null } ;
436+ return { ...state , components, canvasFocus } ;
455437 }
456438
457439 case 'DELETE PAGE' : {
458440 const id : number = state . canvasFocus . componentId ;
459441 const name : string = state . components [ id - 1 ] . name ;
460442
461443 const components : Component [ ] = deleteById ( id , name ) ;
462-
463444 // rebuild rootComponents with correct page IDs
464445 const rootComponents = updateRoots ( components ) ;
465446 const canvasFocus = { componentId : 1 , childId : null } ;
@@ -474,18 +455,17 @@ const reducer = (state: State, action: Action) => {
474455 const rootComponents : number [ ] = updateRoots ( components ) ;
475456
476457 // iterate over the length of the components array
477- components . forEach ( ( el , i ) => {
458+ for ( let i = 0 ; i < components . length ; i ++ ) {
478459 // for each components' code, run the generateCode function to
479460 // update the code preview on the app
480- el . code = generateCode (
461+ components [ i ] . code = generateCode (
481462 components ,
482463 components [ i ] . id ,
483464 rootComponents ,
484465 state . projectType ,
485466 state . HTMLTypes
486467 ) ;
487- } ) ;
488-
468+ }
489469
490470 const canvasFocus = { componentId : 1 , childId : null } ;
491471
@@ -574,14 +554,13 @@ const reducer = (state: State, action: Action) => {
574554
575555 case 'OPEN PROJECT' : {
576556 convertToJSX ( action . payload . HTMLTypes ) ;
577- // action.payload.canvasFocus = { ...action.payload.canvasFocus, childId: null}
578557 return {
579558 ...action . payload
580559 } ;
581560 }
582561
583562 case 'ADD ELEMENT' : {
584- const HTMLTypes : HTMLType [ ] = [ ...state . HTMLTypes ] ;
563+ const HTMLTypes = [ ...state . HTMLTypes ] ;
585564 HTMLTypes . push ( action . payload ) ;
586565 return {
587566 ...state ,
@@ -610,8 +589,7 @@ const reducer = (state: State, action: Action) => {
610589 } )
611590 return {
612591 ...state ,
613- HTMLTypes,
614- components
592+ HTMLTypes
615593 } ;
616594 }
617595
0 commit comments