@@ -257,7 +257,6 @@ const reducer = (state: State, action: Action) => {
257
257
258
258
const parentComponentId : number = state . canvasFocus . componentId ;
259
259
const components = [ ...state . components ] ;
260
- updateAllIds ( components ) ;
261
260
262
261
// find component that we're adding a child to
263
262
const parentComponent = findComponent ( components , parentComponentId ) ;
@@ -328,11 +327,7 @@ const reducer = (state: State, action: Action) => {
328
327
componentId : state . canvasFocus . componentId ,
329
328
childId : newChild . childId
330
329
} ;
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 ;
336
331
return { ...state , components, nextChildId, canvasFocus } ;
337
332
}
338
333
// 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) => {
376
371
state . HTMLTypes
377
372
) ;
378
373
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 } ;
385
375
}
386
376
// Change the focus component and child
387
377
case 'CHANGE FOCUS' : {
388
378
const {
389
379
componentId,
390
380
childId
391
381
} : { 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 } ;
396
385
}
397
386
case 'UPDATE CSS' : {
398
387
const { style } = action . payload ;
@@ -418,7 +407,7 @@ const reducer = (state: State, action: Action) => {
418
407
case 'DELETE CHILD' : {
419
408
// if in-focus instance is a top-level component and not a child, don't delete anything
420
409
421
- // if (!state.canvasFocus.childId) return state;
410
+ if ( ! state . canvasFocus . childId ) return state ;
422
411
423
412
// find the current component in focus
424
413
const components = [ ...state . components ] ;
@@ -443,23 +432,15 @@ const reducer = (state: State, action: Action) => {
443
432
state . HTMLTypes
444
433
) ;
445
434
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 } ;
455
437
}
456
438
457
439
case 'DELETE PAGE' : {
458
440
const id : number = state . canvasFocus . componentId ;
459
441
const name : string = state . components [ id - 1 ] . name ;
460
442
461
443
const components : Component [ ] = deleteById ( id , name ) ;
462
-
463
444
// rebuild rootComponents with correct page IDs
464
445
const rootComponents = updateRoots ( components ) ;
465
446
const canvasFocus = { componentId : 1 , childId : null } ;
@@ -474,18 +455,17 @@ const reducer = (state: State, action: Action) => {
474
455
const rootComponents : number [ ] = updateRoots ( components ) ;
475
456
476
457
// iterate over the length of the components array
477
- components . forEach ( ( el , i ) => {
458
+ for ( let i = 0 ; i < components . length ; i ++ ) {
478
459
// for each components' code, run the generateCode function to
479
460
// update the code preview on the app
480
- el . code = generateCode (
461
+ components [ i ] . code = generateCode (
481
462
components ,
482
463
components [ i ] . id ,
483
464
rootComponents ,
484
465
state . projectType ,
485
466
state . HTMLTypes
486
467
) ;
487
- } ) ;
488
-
468
+ }
489
469
490
470
const canvasFocus = { componentId : 1 , childId : null } ;
491
471
@@ -574,14 +554,13 @@ const reducer = (state: State, action: Action) => {
574
554
575
555
case 'OPEN PROJECT' : {
576
556
convertToJSX ( action . payload . HTMLTypes ) ;
577
- // action.payload.canvasFocus = { ...action.payload.canvasFocus, childId: null}
578
557
return {
579
558
...action . payload
580
559
} ;
581
560
}
582
561
583
562
case 'ADD ELEMENT' : {
584
- const HTMLTypes : HTMLType [ ] = [ ...state . HTMLTypes ] ;
563
+ const HTMLTypes = [ ...state . HTMLTypes ] ;
585
564
HTMLTypes . push ( action . payload ) ;
586
565
return {
587
566
...state ,
@@ -610,8 +589,7 @@ const reducer = (state: State, action: Action) => {
610
589
} )
611
590
return {
612
591
...state ,
613
- HTMLTypes,
614
- components
592
+ HTMLTypes
615
593
} ;
616
594
}
617
595
0 commit comments