Skip to content

Commit 77c6d95

Browse files
committed
Finished Undo/Redo Functionality.
> > Co-authored-by: eddypjr <[email protected]> Co-authored-by: Anthonytorrero <[email protected]>
1 parent 65f7f72 commit 77c6d95

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

app/src/context/initialState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const initialState: State = {
2222
nextChildId: 1,
2323
nextTopSeparatorId: 1000,
2424
HTMLTypes,
25-
past: []
25+
past: [],
26+
future: []
2627
};
2728

2829
export default initialState;

app/src/interfaces/Interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export interface State {
1414
nextChildId: number;
1515
HTMLTypes: HTMLType[];
1616
past: any[];
17+
future: any[];
1718
}
19+
1820
export interface PastElement {
1921
type: string;
2022
typeId: number;

app/src/reducers/componentReducer.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,11 @@ const reducer = (state: State, action: Action) => {
627627
//the children array of state.components[0] will equal the last element of the past array
628628
state.components[0].children = state.past[state.past.length-1];
629629
//the last element of past array gets pushed into future;
630-
// state.future.push(state.past[state.past.length - 1]);
630+
state.future.push(state.past.pop());
631+
// state.past[state.past.length - 1]);
631632
//pop the last element off the past array
632-
state.past.pop();
633+
// state.past.pop();
634+
console.log('state in UNDO', state);
633635
//generate code for the Code Preview
634636
state.components.forEach((el, i) => {
635637
el.code = generateCode(
@@ -646,15 +648,15 @@ const reducer = (state: State, action: Action) => {
646648
}
647649
case 'REDO': {
648650
//nothing left to undo
649-
if (state.past.length === 0) return {...state};
651+
// if (state.past.length === 0) return {...state};
650652
//nothing left to redo
651-
else if(state.future.length === 0) return {...state};
653+
if(state.future.length === 0) return {...state};
652654
//the children array of state.components[0] will equal the last element of the future array
653655
state.components[0].children = state.future[state.future.length - 1];
654656
//the last element of the future array gets pushed into the past array
655-
state.past.push(state.future[state.future.length - 1])
657+
state.past.push(state.future.pop());
656658
//the last element of the future array gets popped off
657-
state.future.pop();
659+
console.log('state in REDO', state);
658660
//generate code for the Code Preview
659661
state.components.forEach((el, i) => {
660662
el.code = generateCode(
@@ -664,11 +666,11 @@ const reducer = (state: State, action: Action) => {
664666
state.projectType,
665667
state.HTMLTypes
666668
);
667-
});
668-
return {
669-
...state
670-
};
671-
}
669+
});
670+
return {
671+
...state
672+
};
673+
}
672674

673675
default:
674676
return state;

0 commit comments

Comments
 (0)