Skip to content

Commit 9137d95

Browse files
authored
Merge pull request #23 from LOLDragoon/master
Refined undo/redo logic
2 parents 0f4c084 + 1f59877 commit 9137d95

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"localforage": "^1.7.3",
2626
"lodash": ">=4.17.13",
2727
"lodash.isequal": "^4.5.0",
28+
"lodash.throttle": "^4.1.1",
2829
"mousetrap": "^1.6.3",
2930
"prismjs": "^1.16.0",
3031
"quasar": "^1.0.0",

src/App.vue

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@
88
import { SET_ACTIVE_COMPONENT } from './store/types'
99
const deepEqual = require('lodash.isequal')
1010
const cloneDeep = require('lodash.clonedeep')
11+
const throttle = require('lodash.throttle')
1112
import { defaultState } from './store/state/index.js'
1213
14+
15+
//use this to make sure these actions don't count as things you "undo"
16+
const ignoredActions = new Set([
17+
"setActiveComponent",
18+
"setActiveLayer",
19+
"upOneLayer",
20+
"setActiveHTML"
21+
])
22+
23+
1324
let redoMixin = {
1425
data () {
1526
return {
@@ -47,19 +58,24 @@ let redoMixin = {
4758
},
4859
4960
mounted () {
61+
62+
const throttledUndo = throttle(this.undo,300)
63+
const throttledRedo = throttle(this.redo,300)
64+
5065
window.addEventListener('keydown', event => {
5166
if (event.ctrlKey && event.key === 'z') {
5267
event.preventDefault()
53-
this.undo()
68+
throttledUndo()
5469
}
5570
})
5671
window.addEventListener('keydown', event => {
5772
if (event.ctrlKey && event.key === 'y') {
5873
event.preventDefault()
59-
this.redo()
74+
throttledRedo()
6075
}
6176
})
6277
78+
// commented code for hotkeys for debuging///////////////////////
6379
// window.addEventListener('keydown', event => {
6480
// if (event.ctrlKey && event.key === 'a') {
6581
// event.preventDefault()
@@ -76,6 +92,7 @@ let redoMixin = {
7692
// }
7793
// }
7894
// });
95+
//////////////////////////////////////////////////////////////////////
7996
8097
// console.log("do we want this? or this.$store.state?", this.$store.state)
8198
this.initialState = defaultState(this.$store.state)
@@ -85,48 +102,62 @@ let redoMixin = {
85102
undo: function () {
86103
// do {
87104
// console.log("How far back?")
105+
if(this.doneAction.length ===0){
106+
return
107+
}
88108
89109
this.isTimetraveling = true
90110
91111
let undone = this.doneAction.pop()
92112
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.
93118
if (undone !== undefined) {
94119
this.undoneAction.push(undone)
95-
if (undone.type === 'setActiveComponent') {
120+
if (ignoredActions.has(undone.type)) {
96121
// console.log('We did something useless!')
97-
do {
122+
while (this.doneAction[this.doneAction.length - 1] &&
123+
ignoredActions.has(this.doneAction[this.doneAction.length - 1].type)){
98124
this.undoneAction.push(this.doneAction.pop())
99125
}
100-
while (this.doneAction[this.doneAction.length - 1] &&
101-
(this.doneAction[this.doneAction.length - 1].type === 'setActiveComponent'))
126+
let finalPop = this.doneAction.pop()
127+
if(finalPop !== undefined){
128+
this.undoneAction.push(finalPop)
129+
}
102130
}
131+
// if we get here, that means we have undone all "useless" actions
132+
// so we have to do one more final pop and push, have to make sure it isn't null though
133+
103134
}
104135
105-
// while (this.doneAction[this.doneAction.length-1] &&
106-
// (this.doneAction[this.doneAction.length - 1].type === "setActiveComponent" ||
107-
// this.doneAction[this.doneAction.length - 1].type === "updateComponentPosition" ))
108136
let payload = {
109137
initialState: this.initialState,
110138
store: this.$store
111139
}
112140
this.$store.commit('EMPTY_STATE', payload)
113141
this.doneAction.forEach(action => {
114-
// console.log('In the loop', this.$store)
142+
//console.log('In the loop', this.$store)
115143
// this.$store.commit(`${mutation.type}`, mutation.payload);
116144
this.$store.dispatch(action.type, cloneDeep(action.payload))
117145
this.doneAction.pop()
118146
})
119147
this.isTimetraveling = false
120148
},
149+
121150
redo: function () {
122151
let action = this.undoneAction.pop()
152+
153+
//we have to set timeTraveling to true to preserve the undoneAction array while we make changes
123154
this.isTimetraveling = true
124155
if (action) {
125156
this.$store.dispatch(action.type, cloneDeep(action.payload))
126157
}
127158
this.isTimetraveling = false
128-
if (action && (action.type === 'setActiveComponent')) {
129-
console.log('WE GOTTA DO MORE')
159+
if (action && ignoredActions.has(action.type)) {
160+
//console.log('WE GOTTA DO MORE')
130161
this.redo()
131162
}
132163
}

src/components/ComponentDisplay.vue

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,28 +177,6 @@ export default {
177177
return this.imagePath[this.activeRoute]
178178
? {
179179
background: `url("${this.userImage}") center/contain no-repeat rgba(223, 218, 218, 0.886)`,
180-
// 'background-color': 'rgba(223, 218, 218, 0.886)',
181-
// 'background-size': '100px 100px, 100px 100px, 20px 20px, 20px 20px',
182-
// 'background-position':' -2px -2px, -2px -2px, -1px -1px, -1px -1px',
183-
// 'background-image': `-webkit-linear-gradient(white 2px, transparent 2px),
184-
// -webkit-linear-gradient(0, white 2px, transparent 2px),
185-
// -webkit-linear-gradient(rgba(255, 255, 255, 0.3) 1px, transparent 1px),
186-
// -webkit-linear-gradient(0, rgba(255, 255, 255, 0.3) 1px, transparent 1px)`,
187-
// 'background-image': `-moz-linear-gradient(white 2px, transparent 2px),
188-
// -moz-linear-gradient(0, white 2px, transparent 2px),
189-
// -moz-linear-gradient(rgba(255, 255, 255, 0.3) 1px, transparent 1px),
190-
// -moz-linear-gradient(0, rgba(255, 255, 255, 0.3) 1px, transparent 1px)`,
191-
// 'background-image': `linear-gradient(white 2px, transparent 2px),
192-
// linear-gradient(90deg, white 2px, transparent 2px),
193-
// linear-gradient(rgba(255, 255, 255, 0.3) 1px, transparent 1px),
194-
// linear-gradient(90deg, rgba(255, 255, 255, 0.3) 1px, transparent 1px)`,
195-
// '-pie-background': `linear-gradient(white 2px, transparent 2px) -2px -2px / 100px,
196-
// linear-gradient(90deg, white 2px, transparent 2px) -2px -2px / 100px,
197-
// linear-gradient(rgba(255, 255, 255, 0.3) 1px, transparent 1px) -1px -1px /
198-
// 20px, linear-gradient(90deg, rgba(255, 255, 255, 0.3) 1px, transparent 1px) -1px -1px /
199-
// 20px, #269`,
200-
// 'background-image': `url(${this.userImage})`,
201-
// behavior: 'url(/pie/PIE.htc)',
202180
}
203181
: {}
204182
}

0 commit comments

Comments
 (0)