Skip to content

Commit ecb6aad

Browse files
committed
trying to figure out undo/redo
2 parents 66aac20 + 5558178 commit ecb6aad

File tree

11 files changed

+438
-38
lines changed

11 files changed

+438
-38
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
"aws-amplify": "^1.1.30",
2222
"aws-amplify-vue": "^0.2.13",
2323
"aws-appsync": "^1.8.1",
24-
"lodash": ">=4.17.13",
2524
"fs-extra": "^8.1.0",
2625
"localforage": "^1.7.3",
26+
"lodash": ">=4.17.13",
27+
"lodash.isequal": "^4.5.0",
2728
"mousetrap": "^1.6.3",
2829
"prismjs": "^1.16.0",
2930
"quasar": "^1.0.0",

src/App.vue

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,103 @@
55
</template>
66

77
<script>
8+
import { SET_ACTIVE_COMPONENT } from './store/types'
9+
const deepEqual = require('lodash.isequal')
10+
11+
12+
let redoMixin = {
13+
data() {
14+
return {
15+
// banana:[],
16+
doneAction:[],
17+
undoneAction:[],
18+
isTimetraveling: false
19+
}
20+
},
21+
22+
created(){
23+
// this.$store.subscribe(mutation => {
24+
// console.log("What is this?",this)
25+
// this.banana.push(mutation);
26+
// })
27+
28+
this.$store.subscribeAction((action,state)=>{
29+
this.doneAction.push(action)
30+
// console.log('this is the action we are logging',action)
31+
// console.log('this is in our redo queue', this.undoneAction[this.undoneAction.length-1])
32+
// console.log("Are these equal to each other?", action == this.undoneAction[this.undoneAction.length-1])
33+
if(!this.isTimetraveling){
34+
if (this.undoneAction[this.undoneAction.length-1]){
35+
if(action.type == this.undoneAction[this.undoneAction.length-1].type && deepEqual(action.payload,this.undoneAction[this.undoneAction.length-1].payload)){
36+
this.undoneAction.pop()
37+
}
38+
else{
39+
this.undoneAction = []
40+
}
41+
}
42+
}
43+
})
44+
// this.blankState = cloneDeep(this.$store)
45+
},
46+
47+
mounted(){
48+
window.addEventListener("keyup", event => {
49+
if (event.key === "z") {
50+
51+
this.undo()
52+
}
53+
});
54+
window.addEventListener("keyup", event => {
55+
if (event.key === "y") {
56+
this.redo()
57+
}
58+
});
59+
},
60+
61+
methods: {
62+
undo: function() {
63+
// do {
64+
// console.log("How far back?")
65+
let undone = this.doneAction.pop()
66+
this.isTimetraveling = true;
67+
if(undone !== undefined){
68+
this.undoneAction.push(undone)
69+
}
70+
// } while (this.doneAction[this.doneAction.length-1] &&
71+
// (this.doneAction[this.doneAction.length - 1].type === "setActiveComponent" ||
72+
// this.doneAction[this.doneAction.length - 1].type === "updateComponentPosition" ))
73+
this.$store.commit("EMPTY_STATE",this.$store)
74+
console.log(this.$store)
75+
this.doneAction.forEach(action => {
76+
console.log("In the loop",this.$store)
77+
//this.$store.commit(`${mutation.type}`, mutation.payload);
78+
this.$store.dispatch(action.type, action.payload);
79+
this.doneAction.pop();
80+
});
81+
this.isTimetraveling = false;
82+
83+
},
84+
redo: function() {
85+
86+
let action = this.undoneAction.pop()
87+
this.isTimetraveling = true;
88+
if(action){
89+
this.$store.dispatch(action.type, action.payload)
90+
}
91+
this.isTimetraveling = false;
92+
// if(action && (action.type === "setActiveComponent" || action.type === "updateStartingPosition")){
93+
// console.log("WE GOTTA DO MORE")
94+
// this.redo();
95+
// }
96+
}
97+
98+
}
99+
}
100+
101+
8102
export default {
9-
name: 'App'
103+
name: 'App',
104+
mixins:[redoMixin]
10105
}
11106
</script>
12107

src/components/ComponentDisplay.vue

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
@dragging="onDrag"
2020
@resizing="onResize"
2121
@dblclick.native="onDoubleClick(componentData)"
22+
@dragstop="finishedDrag"
23+
:onDragStart="recordInitialPosition"
2224
>
2325
<div class="component-title">
2426
<p>{{ componentData.componentName }}</p>
@@ -85,7 +87,8 @@ export default {
8587
testOptions: ["parent", "child", "grandchild"],
8688
testModel: [],
8789
mockImg: false,
88-
counter: 0
90+
counter: 0,
91+
initialPosition:{x:0, y:0,},
8992
};
9093
},
9194
mounted() {
@@ -179,14 +182,16 @@ export default {
179182
console.log("updated")
180183
if(this.activeComponent === '')
181184
{
185+
if(this.$refs.boxes){
182186
this.$refs.boxes.forEach((element)=> {
183187
element.enabled = false;
184188
element.$emit('deactivated')
185189
element.$emit('update:active', false)
186190
187-
})
191+
})}
188192
}
189193
else{
194+
<<<<<<< HEAD
190195
this.$refs.boxes.forEach((element)=>{
191196
// added "element.enabled === false to stop it from emitting a change every frame the box moves
192197
//may need to re-enable to track box movement and resizing since that stuff isn't part of a single source of truth.
@@ -199,12 +204,28 @@ export default {
199204
})
200205
}
201206
},
207+
=======
208+
this.$refs.boxes.forEach((element)=>{
209+
// added "element.enabled === false to stop it from emitting a change every frame the box moves
210+
//may need to re-enable to track box movement and resizing since that stuff isn't part of a single source of truth.
211+
if(this.activeComponent === element.$attrs.id && element.enabled === false)
212+
{
213+
element.enabled = true
214+
element.$emit('activated')
215+
element.$emit('update:active', true)
216+
}
217+
})
218+
}
219+
},
220+
>>>>>>> 55581781ee9af1d82fd02398e554577cdc7b71d4
202221
203222
methods: {
204223
...mapActions([
205224
"setActiveComponent",
206225
"updateComponentChildrenMultiselectValue",
207-
"updateActiveComponentChildrenValue"
226+
"updateActiveComponentChildrenValue",
227+
"updateComponentPosition",
228+
"updateStartingPosition",
208229
]),
209230
onResize: function(x, y, width, height) {
210231
this.activeComponentData.x = x;
@@ -217,6 +238,26 @@ export default {
217238
this.componentMap[this.activeComponent].w = width;
218239
this.componentMap[this.activeComponent].h = height;
219240
},
241+
242+
recordInitialPosition: function(e) {
243+
console.log("we started a drag")
244+
console.log("this.intialPosition",this.initialPosition)
245+
console.log("WHAT IS THIS", this)
246+
if(this.activeComponent !== e.target.id){
247+
this.setActiveComponent(e.target.id)
248+
}
249+
this.initialPosition.x = this.componentMap[this.activeComponent].x
250+
this.initialPosition.y = this.componentMap[this.activeComponent].y
251+
let payload = {
252+
x: this.initialPosition.x,
253+
y: this.initialPosition.y,
254+
activeComponent: this.activeComponent,
255+
routeArray: this.routes[this.activeRoute],
256+
activeComponentData: this.activeComponentData
257+
}
258+
this.updateStartingPosition(payload);
259+
},
260+
220261
onDrag: function(x, y) {
221262
this.activeComponentData.x = x;
222263
this.activeComponentData.y = y;
@@ -225,19 +266,45 @@ export default {
225266
this.componentMap[this.activeComponent].y = y;
226267
this.userImage;
227268
},
228-
onLayer: function(z) {
229-
this.activeComponentData.z = z;
269+
// onLayer: function(z) {
270+
// this.activeComponentData.z = z;
271+
// // Want to change the "Z" of the component found in Routes[activeRoute][whatever the component is]
272+
// //have to do this via an action or it won't be preserved in our undo/redo
273+
// },
274+
275+
finishedDrag: function(x,y){
276+
console.log("FINISHED DRAGGING")
277+
let payload = {
278+
x: x,
279+
y: y,
280+
activeComponent: this.activeComponent,
281+
routeArray: this.routes[this.activeRoute],
282+
activeComponentData: this.activeComponentData
283+
}
284+
console.log("Payload.x = ", payload.x, "this.initialPosition.x", this.initialPosition.x)
285+
console.log("Payload.y = ", payload.y, "this.initialPosition.y", this.initialPosition.y)
286+
if(payload.x !== this.initialPosition.x || payload.y !== this.initialPosition.y){
287+
this.updateComponentPosition(payload);
288+
}
230289
},
290+
231291
onActivated(componentData) {
292+
console.log("I RAN!")
232293
this.$refs.boxes.forEach((element)=> {
233294
if (element.$attrs.id !== componentData.componentName){
234295
element.enabled = false;
235296
element.$emit('deactivated')
236297
element.$emit('update:active', false)
237298
}
238299
})
239-
this.setActiveComponent(componentData.componentName);
300+
// console.log("this is what is currently active",this.activeComponent)
301+
// console.log("this is this", this)
302+
// console.log('!(componentData.componentName === this.activeComponent)?',!(componentData.componentName === this.activeComponent))
303+
if(!(componentData.componentName === this.activeComponent)){
304+
this.setActiveComponent(componentData.componentName);
305+
}
240306
this.activeComponentData.isActive = true;
307+
241308
242309
},
243310
@@ -255,7 +322,9 @@ export default {
255322
// }
256323
},
257324
onDoubleClick(compData) {
258-
this.setActiveComponent(compData.componentName);
325+
if(!(componentData.componentName === this.activeComponent)){
326+
this.setActiveComponent(componentData.componentName);
327+
}
259328
this.activeComponentData.isActive = true;
260329
},
261330
handleAddChild() {
@@ -293,7 +362,9 @@ export default {
293362
handleClick(event){
294363
if(event.target.className === "component-display grid-bg")
295364
{
296-
this.setActiveComponent('')
365+
if(!('' === this.activeComponent)){
366+
this.setActiveComponent('');
367+
}
297368
}
298369
}
299370
}

src/components/CreateComponent.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export default {
8484
8585
},
8686
resetActiveComponent () {
87+
if(!this.activeComponent === ''){
8788
this.setActiveComponent('')
89+
}
8890
8991
},
9092
handleIconClick () {

src/store/actions.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ const actions = {
5050
commit(types.SET_SELECTED_ELEMENT_LIST, payload)
5151
}
5252
},
53+
54+
[types.updateComponentPosition]: ({ commit }, payload) =>{
55+
/* let payload = {
56+
x = x,
57+
y = y,
58+
activeComponent = this.activeComponent,
59+
routeArray = this.routes[this.activeRoute],
60+
activeComponentData = this.activeComponentData
61+
}*/
62+
63+
commit(types.UPDATE_COMPONENT_POSITION, payload)
64+
65+
},
66+
67+
//does the same as update component position, but needed to record the initial spot of the draggable resizeable in component display
68+
// or else undo/redo won't work
69+
[types.updateStartingPosition]: ({ commit }, payload) => {
70+
commit(types.UPDATE_COMPONENT_POSITION, payload)
71+
},
72+
73+
5374
// adds component to the homeQueue
5475
[types.addToSelectedElementList]: ({ commit }, payload) => {
5576
commit(types.ADD_TO_SELECTED_ELEMENT_LIST, payload)

src/store/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3+
import undoRedo from './plugins.js'
34

45
import state from './state'
56
import actions from './actions.js'
67
import mutations from './mutations.js'
78

89
Vue.use(Vuex)
10+
// Vue.use(undoRedo)
911

1012
export default new Vuex.Store({
1113
state,

0 commit comments

Comments
 (0)