@@ -622,46 +622,16 @@ const reducer = (state: State, action: Action) => {
622
622
} ;
623
623
}
624
624
case 'UNDO' : {
625
- //iterate through past arr, grab the 1 before the end, reassign state to equal that grabbed obj
626
- // let past: object[] = state.past[state.past.length - 1];
627
- // console.log()
628
- console . log ( 'state before past' , state ) ;
629
- // const pastBy = state.past[state.past.length - 1];
630
- // console.log('pastBy', pastBy);
631
- // console.log('state.past - 1', state.past[state.past.length - 1])
632
- // state.past = [];
633
-
634
-
635
- //check if state.components[0].children is nested
636
- //if it is nested, go into that object and do something
637
- //else, have state.comp.children = state.past[state.past.length - 1]
638
- //pop from past
639
- // const child = state.components[0].children
640
- // for(let i = 0; i < child.length; i++) {
641
- // // console.log('each children obj', child[i])
642
- // if(child[i].children.length === 2) {
643
- // // child[i].children = state.past[state.past.length - 2]//[];
644
- // child[i].children = [];
645
- // console.log('children of div or form', child[i])
646
-
647
- // // child[i].children = state.past[state.past.length - 1]
648
- // }
649
- // // child[i] = state.past[state.past.length - 1]
650
- // }
651
- let nestedChildren : any [ ] = state . components [ 0 ] . children [ 0 ] . children ;
652
- // check if there are any tags in the children of the children (nested tags)
653
- if ( nestedChildren . length === 2 ) {
654
- // if there are, execute undo code specific to nested children, otherwise, execute code below for non-nested children
655
- nestedChildren = [ ] ;
656
- } else if ( nestedChildren . length > 2 ) {
657
- nestedChildren = nestedChildren [ nestedChildren . length - 2 ] ;
658
- } else {
659
- state . components [ 0 ] . children = state . past [ state . past . length - 1 ]
660
- state . past . pop ( ) ;
661
- }
662
- console . log ( 'state after past reassigned' , state ) ;
625
+ //if past is empty, return state
626
+ if ( state . past . length === 0 ) return { ...state } ;
627
+ //the children array of state.components[0] will equal the last element of the past array
628
+ state . components [ 0 ] . children = state . past [ state . past . length - 1 ] ;
629
+ //the last element of past array gets pushed into future;
630
+ // state.future.push(state.past[state.past.length - 1]);
631
+ //pop the last element off the past array
632
+ state . past . pop ( ) ;
633
+ //generate code for the Code Preview
663
634
state . components . forEach ( ( el , i ) => {
664
- console . log ( 'el in undo' , el )
665
635
el . code = generateCode (
666
636
state . components ,
667
637
state . components [ i ] . id ,
@@ -672,104 +642,33 @@ const reducer = (state: State, action: Action) => {
672
642
} ) ;
673
643
return {
674
644
...state
675
- }
645
+ } ;
676
646
}
677
-
678
-
679
-
680
- // console.log('state', state);
681
- // const childrenArr = state.components[0].children;
682
- // const children: Array<object | null> = [];
683
- // const children = [];
684
- // console.log('state.components', state.components[0].children)
685
-
686
- // // console.log('state', state);
687
- // // const childrenArr = state.components[0].children;
688
- // // const children: Array<object | null> = [];
689
- // const children = state.components[0].children;
690
- // const past = [];
691
-
692
- // // console.log('children', children)
693
- // // console.log('state.components', state.components)
694
- // children.forEach(htmlTag => past.push(htmlTag));
695
- // // console.log('past', past);
696
- // redoArr.push(past[past.length-2], past[past.length-1]);
697
-
698
-
699
- // if(state.past.length >= 2) {
700
- // // state.past.splice(0, state.past.length -1);
701
- // state.past.length = 2;
702
- // } else {
703
- // state.past.push(past[past.length-2], past[past.length-1])
704
- // }
705
-
706
-
707
- // // redoArrFunc(past[past.length - 2], past[past.length-1])
708
- // console.log('redoArr', redoArr);
709
- // state.components[0].children = past.slice(0, past.length - 2);
710
- // // console.log('state with past', state.components[0].children)
711
- // // console.log('entire state with new past', state)
712
- // state.components.forEach((el, i) => {
713
- // // console.log('el in undo', el)
714
- // el.code = generateCode(
715
- // state.components,
716
- // state.components[i].id,
717
- // state.rootComponents,
718
- // state.projectType,
719
- // state.HTMLTypes
720
- // );
721
- // });
722
- // // state.components[0].children.splice(0, state.components[0].children.length);
723
- // console.log('the final boss in UNDO', state)
724
- // return {
725
- // ...state,
726
- // }
727
-
728
- // console.log('state', state);
729
- // let stateSnap = [state];
730
- // stateSnap = stateSnap.slice(0, stateSnap.length +1);
731
- // console.log('stateSnap in UNDOOOO', stateSnap);
732
- // console.log('stateSnap in UNDO before push', stateSnap)
733
- // stateSnap.push(state);
734
- // // redoArr.push(stateSnap);
735
- // console.log('stateSnap in UNDO after push', stateSnap)
736
- // console.log('redoArr in undo', redoArr);
737
-
738
-
739
-
740
- // }
741
- ////////////Reference ADD ELEMENT case to see how id is being incremented
742
647
case 'REDO' : {
743
- // const children = state.components[0].children;
744
- // if(redoArr.length >= 2) redoArr.length = 2;
745
- // children.push(redoArr);
746
- let { past} = state ;
747
- const children = state . components [ 0 ] . children ;
748
- // while(past.length <= 2) {
749
- children . push ( ...past )
750
- // children.push(past[past.length -2]);
751
- // }
648
+ //nothing left to undo
649
+ if ( state . past . length === 0 ) return { ...state } ;
650
+ //nothing left to redo
651
+ else if ( state . future . length === 0 ) return { ...state } ;
652
+ //the children array of state.components[0] will equal the last element of the future array
653
+ state . components [ 0 ] . children = state . future [ state . future . length - 1 ] ;
654
+ //the last element of the future array gets pushed into the past array
655
+ state . past . push ( state . future [ state . future . length - 1 ] )
656
+ //the last element of the future array gets popped off
657
+ state . future . pop ( ) ;
658
+ //generate code for the Code Preview
752
659
state . components . forEach ( ( el , i ) => {
753
- // console.log('el in undo', el)
754
660
el . code = generateCode (
755
661
state . components ,
756
662
state . components [ i ] . id ,
757
663
state . rootComponents ,
758
664
state . projectType ,
759
665
state . HTMLTypes
760
- ) ;
761
- } ) ;
762
- past . splice ( 0 , past . length ) ;
763
- console . log ( 'the final boss in REDO' , state )
764
- // console.log('past destructured', past)
765
- // console.log('redoItems', redoItems)
766
- // console.log('state in redo', state)
767
- // console.log(redoArr())eeeeeeeeeeeeeeweeeee
768
- // console.log('children in Redo', children)
769
- return {
770
- ...state
666
+ ) ;
667
+ } ) ;
668
+ return {
669
+ ...state
670
+ } ;
771
671
}
772
- }
773
672
774
673
default :
775
674
return state ;
0 commit comments