5
5
</template >
6
6
7
7
<script >
8
- import { SET_ACTIVE_COMPONENT } from ' ./store/types'
9
8
const deepEqual = require (' lodash.isequal' )
10
9
const cloneDeep = require (' lodash.clonedeep' )
11
10
const throttle = require (' lodash.throttle' )
12
11
import { defaultState } from ' ./store/state/index.js'
13
12
14
-
15
- // use this to make sure these actions don't count as things you "undo"
13
+ // use this to make sure these actions don't count as things you "undo"
16
14
const ignoredActions = new Set ([
17
- " setActiveComponent" ,
18
- " setActiveLayer" ,
19
- " upOneLayer" ,
20
- " setActiveHTML"
21
- ])
22
-
15
+ ' setActiveComponent' ,
16
+ ' setActiveLayer' ,
17
+ ' upOneLayer' ,
18
+ ' setActiveHTML'
19
+ ])
23
20
24
21
let redoMixin = {
25
22
data () {
26
23
return {
27
- // banana:[],
28
24
doneAction: [],
29
25
undoneAction: [],
30
26
isTimetraveling: false ,
31
27
initialState: {}
32
28
}
33
29
},
34
-
35
30
created () {
36
31
this .$store .subscribeAction ((action , state ) => {
37
32
// console.log("We are saving this action!", action)
@@ -45,7 +40,7 @@ let redoMixin = {
45
40
// console.log("Are these equal to each other?", action == this.undoneAction[this.undoneAction.length-1])
46
41
if (! this .isTimetraveling ) {
47
42
if (this .undoneAction [this .undoneAction .length - 1 ]) {
48
- if (action .type == this .undoneAction [this .undoneAction .length - 1 ].type &&
43
+ if (action .type === this .undoneAction [this .undoneAction .length - 1 ].type &&
49
44
deepEqual (action .payload , this .undoneAction [this .undoneAction .length - 1 ].payload )) {
50
45
this .undoneAction .pop ()
51
46
} else {
@@ -54,13 +49,11 @@ let redoMixin = {
54
49
}
55
50
}
56
51
})
57
- // this.blankState = cloneDeep(this.$store)
58
52
},
59
53
60
54
mounted () {
61
-
62
- const throttledUndo = throttle (this .undo ,300 )
63
- const throttledRedo = throttle (this .redo ,300 )
55
+ const throttledUndo = throttle (this .undo , 300 )
56
+ const throttledRedo = throttle (this .redo , 300 )
64
57
65
58
window .addEventListener (' keydown' , event => {
66
59
if (event .ctrlKey && event .key === ' z' ) {
@@ -75,62 +68,39 @@ let redoMixin = {
75
68
}
76
69
})
77
70
78
- // commented code for hotkeys for debuging///////////////////////
79
- // window.addEventListener('keydown', event => {
80
- // if (event.ctrlKey && event.key === 'a') {
81
- // event.preventDefault()
82
- // if (this.$store.state.activeHTML !== '') {
83
- // this.$store.dispatch('setActiveLayer', {id:this.$store.state.activeHTML, text:"banana"})
84
- // }
85
- // }
86
- // });
87
- // window.addEventListener('keydown', event => {
88
- // if (event.ctrlKey && event.key === 'd') {
89
- // event.preventDefault()
90
- // if (this.$store.state.activeLayer.id !== '') {
91
- // this.$store.dispatch('upOneLayer', this.$store.state.activeLayer.id)
92
- // }
93
- // }
94
- // });
95
- // ////////////////////////////////////////////////////////////////////
96
-
97
- // console.log("do we want this? or this.$store.state?", this.$store.state)
98
71
this .initialState = defaultState (this .$store .state )
99
72
},
100
73
101
74
methods: {
102
75
undo : function () {
103
- // do {
104
- // console.log("How far back?")
105
- if (this .doneAction .length === 0 ){
76
+ if (this .doneAction .length === 0 ) {
106
77
return
107
78
}
108
79
109
80
this .isTimetraveling = true
110
81
111
82
let undone = this .doneAction .pop ()
112
83
113
- // assuming we have have something to undo, we check if we are undoing an action that has no feedback
114
- // like say changing active elements or active components or going up and down a layer since these can be obscured from immediate feedback
115
- // will just feel bad to undo (Imagine someone just clicking around out of boredom and then deciding to undo
116
- // will force them to have to undo the 30 highlights they just did instead of the last "Real" action)
117
- // if this happens we keep popping the doneAction queue and building up the redo queue.
84
+ /* assuming we have have something to undo, we check if we are undoing an action that has no feedback
85
+ e.g.: changing active elements or active components or going up and down a layer since these can be obscured from immediate feedback.
86
+ these will feel bad to undo (imagine someone clicking around out of boredom and then deciding to undo
87
+ this will force them to have to undo the 30 highlights they just did instead of the last "Real" action)
88
+ if this happens we keep popping the doneAction queue and building up the redo queue. */
118
89
if (undone !== undefined ) {
119
90
this .undoneAction .push (undone)
120
91
if (ignoredActions .has (undone .type )) {
121
- // console.log('We did something useless !')
92
+ // console.log('We undid an ignored action !')
122
93
while (this .doneAction [this .doneAction .length - 1 ] &&
123
- ignoredActions .has (this .doneAction [this .doneAction .length - 1 ].type )){
94
+ ignoredActions .has (this .doneAction [this .doneAction .length - 1 ].type )) {
124
95
this .undoneAction .push (this .doneAction .pop ())
125
96
}
126
- // if we get here, that means we have undone all "useless" actions
127
- // so we have to do one more final pop and push, have to make sure it isn't null though
97
+ /* if we get here, that means we have undone all "useless" actions
98
+ so we have to do one more final pop and push, have to make sure it isn't null though */
128
99
let finalPop = this .doneAction .pop ()
129
- if (finalPop !== undefined ){
100
+ if (finalPop !== undefined ) {
130
101
this .undoneAction .push (finalPop)
131
102
}
132
103
}
133
-
134
104
}
135
105
136
106
let payload = {
@@ -139,8 +109,7 @@ let redoMixin = {
139
109
}
140
110
this .$store .commit (' EMPTY_STATE' , payload)
141
111
this .doneAction .forEach (action => {
142
- // console.log('In the loop', this.$store)
143
- // this.$store.commit(`${mutation.type}`, mutation.payload);
112
+ // console.log('in the undo loop', this.$store)
144
113
this .$store .dispatch (action .type , cloneDeep (action .payload ))
145
114
this .doneAction .pop ()
146
115
})
@@ -150,14 +119,14 @@ let redoMixin = {
150
119
redo : function () {
151
120
let action = this .undoneAction .pop ()
152
121
153
- // we have to set timeTraveling to true to preserve the undoneAction array while we make changes
122
+ // we have to set timeTraveling to true to preserve the undoneAction array while we make changes
154
123
this .isTimetraveling = true
155
124
if (action) {
156
125
this .$store .dispatch (action .type , cloneDeep (action .payload ))
157
126
}
158
127
this .isTimetraveling = false
159
128
if (action && ignoredActions .has (action .type )) {
160
- // console.log('WE GOTTA DO MORE ')
129
+ // console.log('in the redo loop ')
161
130
this .redo ()
162
131
}
163
132
}
0 commit comments