|
5 | 5 | </template>
|
6 | 6 |
|
7 | 7 | <script>
|
| 8 | +import { SET_ACTIVE_COMPONENT } from './store/types' |
| 9 | +const deepEqual = require('lodash.isequal') |
| 10 | +const cloneDeep = require('lodash.clonedeep') |
| 11 | +import { defaultState } from './store/state/index.js' |
| 12 | +
|
| 13 | +let redoMixin = { |
| 14 | + data () { |
| 15 | + return { |
| 16 | + // banana:[], |
| 17 | + doneAction: [], |
| 18 | + undoneAction: [], |
| 19 | + isTimetraveling: false, |
| 20 | + initialState: {} |
| 21 | + } |
| 22 | + }, |
| 23 | +
|
| 24 | + created () { |
| 25 | + this.$store.subscribeAction((action, state) => { |
| 26 | + // console.log("We are saving this action!", action) |
| 27 | + if (typeof action.payload === 'object') { |
| 28 | + // console.log("We saved the world with a deepclone!", action.payload === cloneDeep) |
| 29 | + action.payload = cloneDeep(action.payload) |
| 30 | + } |
| 31 | + this.doneAction.push(action) |
| 32 | + // console.log('this is the action we are logging',action) |
| 33 | + // console.log('this is in our redo queue', this.undoneAction[this.undoneAction.length-1]) |
| 34 | + // console.log("Are these equal to each other?", action == this.undoneAction[this.undoneAction.length-1]) |
| 35 | + if (!this.isTimetraveling) { |
| 36 | + if (this.undoneAction[this.undoneAction.length - 1]) { |
| 37 | + if (action.type == this.undoneAction[this.undoneAction.length - 1].type && |
| 38 | + deepEqual(action.payload, this.undoneAction[this.undoneAction.length - 1].payload)) { |
| 39 | + this.undoneAction.pop() |
| 40 | + } else { |
| 41 | + this.undoneAction = [] |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }) |
| 46 | + // this.blankState = cloneDeep(this.$store) |
| 47 | + }, |
| 48 | +
|
| 49 | + mounted () { |
| 50 | + window.addEventListener('keydown', event => { |
| 51 | + if (event.ctrlKey && event.key === 'z') { |
| 52 | + event.preventDefault() |
| 53 | + this.undo() |
| 54 | + } |
| 55 | + }) |
| 56 | + window.addEventListener('keydown', event => { |
| 57 | + if (event.ctrlKey && event.key === 'y') { |
| 58 | + event.preventDefault() |
| 59 | + this.redo() |
| 60 | + } |
| 61 | + }) |
| 62 | +
|
| 63 | + // window.addEventListener('keydown', event => { |
| 64 | + // if (event.ctrlKey && event.key === 'a') { |
| 65 | + // event.preventDefault() |
| 66 | + // if (this.$store.state.activeHTML !== '') { |
| 67 | + // this.$store.dispatch('setActiveLayer', {id:this.$store.state.activeHTML, text:"banana"}) |
| 68 | + // } |
| 69 | + // } |
| 70 | + // }); |
| 71 | + // window.addEventListener('keydown', event => { |
| 72 | + // if (event.ctrlKey && event.key === 'd') { |
| 73 | + // event.preventDefault() |
| 74 | + // if (this.$store.state.activeLayer.id !== '') { |
| 75 | + // this.$store.dispatch('upOneLayer', this.$store.state.activeLayer.id) |
| 76 | + // } |
| 77 | + // } |
| 78 | + // }); |
| 79 | +
|
| 80 | + // console.log("do we want this? or this.$store.state?", this.$store.state) |
| 81 | + this.initialState = defaultState(this.$store.state) |
| 82 | + }, |
| 83 | +
|
| 84 | + methods: { |
| 85 | + undo: function () { |
| 86 | + // do { |
| 87 | + // console.log("How far back?") |
| 88 | +
|
| 89 | + this.isTimetraveling = true |
| 90 | +
|
| 91 | + let undone = this.doneAction.pop() |
| 92 | +
|
| 93 | + if (undone !== undefined) { |
| 94 | + this.undoneAction.push(undone) |
| 95 | + if (undone.type === 'setActiveComponent') { |
| 96 | + console.log('We did something useless!') |
| 97 | + do { |
| 98 | + this.undoneAction.push(this.doneAction.pop()) |
| 99 | + } |
| 100 | + while (this.doneAction[this.doneAction.length - 1] && |
| 101 | + (this.doneAction[this.doneAction.length - 1].type === 'setActiveComponent')) |
| 102 | + } |
| 103 | + } |
| 104 | +
|
| 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" )) |
| 108 | + let payload = { |
| 109 | + initialState: this.initialState, |
| 110 | + store: this.$store |
| 111 | + } |
| 112 | + this.$store.commit('EMPTY_STATE', payload) |
| 113 | + console.log(this.$store) |
| 114 | + this.doneAction.forEach(action => { |
| 115 | + console.log('In the loop', this.$store) |
| 116 | + // this.$store.commit(`${mutation.type}`, mutation.payload); |
| 117 | + this.$store.dispatch(action.type, cloneDeep(action.payload)) |
| 118 | + this.doneAction.pop() |
| 119 | + }) |
| 120 | + this.isTimetraveling = false |
| 121 | + }, |
| 122 | + redo: function () { |
| 123 | + let action = this.undoneAction.pop() |
| 124 | + this.isTimetraveling = true |
| 125 | + if (action) { |
| 126 | + this.$store.dispatch(action.type, cloneDeep(action.payload)) |
| 127 | + } |
| 128 | + this.isTimetraveling = false |
| 129 | + if (action && (action.type === 'setActiveComponent')) { |
| 130 | + console.log('WE GOTTA DO MORE') |
| 131 | + this.redo() |
| 132 | + } |
| 133 | + } |
| 134 | +
|
| 135 | + } |
| 136 | +} |
| 137 | +
|
8 | 138 | export default {
|
9 |
| - name: 'App' |
| 139 | + name: 'App', |
| 140 | + mixins: [redoMixin] |
10 | 141 | }
|
11 | 142 | </script>
|
12 | 143 |
|
|
0 commit comments