1-
21<template >
32 <!-- Get passed down to MyLayout.vue
43 & listen for custom events emitted from MyLayout.vue -->
5- <router-view
6- v-on:undo =" undoTrigger"
4+ <router-view
5+ v-on:undo =" undoTrigger"
76 v-on:redo =" redoTrigger"
87 v-bind:done-action =" doneAction"
98 v-bind:undone-action =" undoneAction"
109 />
1110</template >
1211
1312<script >
14- import { defineComponent } from ' vue' ;
15- const deepEqual = require (' lodash.isequal' )
16- const cloneDeep = require (' lodash.clonedeep' )
17- const throttle = require (' lodash.throttle' )
18- import { defaultState } from ' ./store/state/index.js'
13+ import { defineComponent } from " vue" ;
14+ const deepEqual = require (" lodash.isequal" );
15+ const cloneDeep = require (" lodash.clonedeep" );
16+ const throttle = require (" lodash.throttle" );
17+ import { defaultState } from " ./store/state/index.js" ;
1918
2019// use this to make sure these actions don't count as things you "undo"
2120const ignoredActions = new Set ([
22- ' setActiveComponent' ,
23- ' setActiveLayer' ,
24- ' upOneLayer' ,
25- ' setActiveHTML' ,
26- ' updateComponentNameInputValue'
27- ])
21+ " setActiveComponent" ,
22+ " setActiveLayer" ,
23+ " upOneLayer" ,
24+ " setActiveHTML" ,
25+ " updateComponentNameInputValue" ,
26+ ]);
2827
2928let redoMixin = {
30- data () {
29+ data () {
3130 return {
3231 doneAction: [],
3332 undoneAction: [],
3433 isTimetraveling: false ,
35- initialState: {},
36- }
34+ initialState: {},
35+ };
3736 },
38- created () {
37+ created () {
3938 this .$store .subscribeAction ((action , state ) => {
40- // console.log("We are saving this action!", action)
41- if (typeof action .payload === ' object' ) {
42- // console.log("We saved the world with a deepclone!", action.payload === cloneDeep)
43- action .payload = cloneDeep (action .payload )
39+ if (typeof action .payload === " object" ) {
40+ action .payload = cloneDeep (action .payload );
4441 }
4542 this .doneAction .push (action);
46- // console.log(`From app.vue: ${action.payload}`)
47- // console.log('this is the action we are logging',action)
48- // console.log('this is in our redo queue', this.undoneAction[this.undoneAction.length-1])
49- // console.log("Are these equal to each other?", action == this.undoneAction[this.undoneAction.length-1])
5043 if (! this .isTimetraveling ) {
5144 if (this .undoneAction [this .undoneAction .length - 1 ]) {
5245 if (
@@ -57,115 +50,111 @@ let redoMixin = {
5750 this .undoneAction [this .undoneAction .length - 1 ].payload
5851 )
5952 ) {
60- this .undoneAction .pop ()
53+ this .undoneAction .pop ();
6154 } else {
62- this .undoneAction = []
55+ this .undoneAction = [];
6356 }
6457 }
6558 }
66- })
59+ });
6760 },
6861 // undo + redo function calling
6962 // metaKey accounts for Command Key on Mac
70- mounted () {
71- const throttledUndo = throttle (this .undo , 300 )
72- const throttledRedo = throttle (this .redo , 300 )
63+ mounted () {
64+ const throttledUndo = throttle (this .undo , 300 );
65+ const throttledRedo = throttle (this .redo , 300 );
7366 // undo function calling
74- window .addEventListener (' keydown' , event => {
75- if ((event .ctrlKey || event .metaKey ) && event .key === ' z ' ) {
76- event .preventDefault ()
77- throttledUndo ()
67+ window .addEventListener (" keydown" , ( event ) => {
68+ if ((event .ctrlKey || event .metaKey ) && event .key === " z " ) {
69+ event .preventDefault ();
70+ throttledUndo ();
7871 }
79- })
72+ });
8073 // redo function calling
81- window .addEventListener (' keydown' , event => {
82- if ((event .ctrlKey || event .metaKey ) && event .key === ' y ' ) {
83- event .preventDefault ()
84- throttledRedo ()
74+ window .addEventListener (" keydown" , ( event ) => {
75+ if ((event .ctrlKey || event .metaKey ) && event .key === " y " ) {
76+ event .preventDefault ();
77+ throttledRedo ();
8578 }
86- })
79+ });
8780
88- this .initialState = defaultState (this .$store .state )
81+ this .initialState = defaultState (this .$store .state );
8982 },
9083
9184 methods: {
9285 undo : function () {
9386 if (this .doneAction .length === 0 ) {
94- return
87+ return ;
9588 }
9689
97- this .isTimetraveling = true
98- let undone = this .doneAction .pop ()
90+ this .isTimetraveling = true ;
91+ let undone = this .doneAction .pop ();
9992
10093 /* assuming we have have something to undo, we check if we are undoing an action that has no feedback
10194 e.g.: changing active elements or active components or going up and down a layer since these can be obscured from immediate feedback.
10295 these will feel bad to undo (imagine someone clicking around out of boredom and then deciding to undo
10396 this will force them to have to undo the 30 highlights they just did instead of the last "Real" action)
10497 if this happens we keep popping the doneAction queue and building up the redo queue. */
10598 if (undone !== undefined ) {
106- this .undoneAction .push (undone)
99+ this .undoneAction .push (undone);
107100 if (ignoredActions .has (undone .type )) {
108- // console.log('We undid an ignored action!')
109101 while (
110102 this .doneAction [this .doneAction .length - 1 ] &&
111103 ignoredActions .has (this .doneAction [this .doneAction .length - 1 ].type )
112104 ) {
113- this .undoneAction .push (this .doneAction .pop ())
105+ this .undoneAction .push (this .doneAction .pop ());
114106 }
115107 /* if we get here, that means we have undone all "useless" actions
116108 so we have to do one more final pop and push, have to make sure it isn't null though */
117- let finalPop = this .doneAction .pop ()
109+ let finalPop = this .doneAction .pop ();
118110 if (finalPop !== undefined ) {
119- this .undoneAction .push (finalPop)
111+ this .undoneAction .push (finalPop);
120112 }
121113 }
122114 }
123115
124116 let payload = {
125117 initialState: this .initialState ,
126- store: this .$store
127- }
128- this .$store .commit (' EMPTY_STATE' , payload)
129- this .doneAction .forEach (action => {
130- // console.log('in the undo loop', this.$store)
131- this .$store .dispatch (action .type , cloneDeep (action .payload ))
132- this .doneAction .pop ()
133- })
134- this .isTimetraveling = false
118+ store: this .$store ,
119+ };
120+ this .$store .commit (" EMPTY_STATE" , payload);
121+ this .doneAction .forEach ((action ) => {
122+ this .$store .dispatch (action .type , cloneDeep (action .payload ));
123+ this .doneAction .pop ();
124+ });
125+ this .isTimetraveling = false ;
135126 },
136127
137128 redo : function () {
138- let action = this .undoneAction .pop ()
129+ let action = this .undoneAction .pop ();
139130
140131 // we have to set timeTraveling to true to preserve the undoneAction array while we make changes
141- this .isTimetraveling = true
132+ this .isTimetraveling = true ;
142133 if (action) {
143- this .$store .dispatch (action .type , cloneDeep (action .payload ))
134+ this .$store .dispatch (action .type , cloneDeep (action .payload ));
144135 }
145- this .isTimetraveling = false
136+ this .isTimetraveling = false ;
146137 if (action && ignoredActions .has (action .type )) {
147- // console.log('in the redo loop')
148- this .redo ()
138+ this .redo ();
149139 }
150- },
140+ },
151141
152142 // Undo triggered from MyLayout.vue
153- undoTrigger : function () {
143+ undoTrigger : function () {
154144 const throttledUndo = throttle (this .undo , 300 );
155145 throttledUndo ();
156146 },
157147
158148 // Redo triggered from MyLayout.vue
159- redoTrigger : function () {
149+ redoTrigger : function () {
160150 const throttledRedo = throttle (this .redo , 300 );
161151 throttledRedo ();
162- }
163- }
164- }
152+ },
153+ },
154+ };
165155
166156export default defineComponent ({
167- name: ' App' ,
168- mixins: [redoMixin]
169- })
157+ name: " App" ,
158+ mixins: [redoMixin],
159+ });
170160 </script >
171-
0 commit comments