@@ -202,10 +202,17 @@ export default class Scene {
202202 }
203203
204204 handleKeyDown = e => {
205- if ( ( e . metaKey || e . ctrlKey ) && e . which === 37 || e . which === 39 ) e . preventDefault ( ) ;
205+ if ( ( e . metaKey || e . ctrlKey ) && ( e . which === 89 || e . which === 37 || e . which === 39 ) ) e . preventDefault ( ) ;
206+
207+ // e.preventDefault();
208+ // e.stopPropagation();
209+ // e.stopImmediatePropagation();
206210
207211 switch ( e . which ) {
212+ // cmd+z, undo
208213 case 90 : e . metaKey && this . undo ( ) ; break ;
214+ // cmd+y, redo
215+ case 89 : e . metaKey && this . redo ( ) ; break ;
209216 case 32 : this . play ( ) ; break ;
210217 case 39 :
211218 e . metaKey || e . ctrlKey ? this . layers [ this . active_layer ] . rotation += 0.1 : this . next ( )
@@ -229,7 +236,8 @@ export default class Scene {
229236 this . history_offset < this . history . length ? this . history . length - this . history_offset : 0 ,
230237 [
231238 this . layers [ this . active_layer ] . id ,
232- this . layers [ this . active_layer ] . currentFrame . id
239+ this . layers [ this . active_layer ] . currentFrame . id ,
240+ null
233241 ]
234242 ) ;
235243
@@ -262,6 +270,8 @@ export default class Scene {
262270 }
263271
264272 undo ( ) {
273+ if ( this . history_offset <= 0 ) return ;
274+
265275 this . history_offset = Math . max ( 0 , this . history_offset - 1 ) ;
266276 const [ layerId , frameId ] = this . history [ this . history_offset ] ;
267277
@@ -272,12 +282,38 @@ export default class Scene {
272282 if ( this . layers [ layerIndex ] . id !== this . layers [ this . active_layer ] . id ) this . active_layer = layerIndex ;
273283 // move all layers if frame is navigated to
274284 if ( this . layers [ layerIndex ] . frame !== frameIndex ) this . layers . forEach ( l => l . goTo ( l . frame + frameDelta ) ) ;
275- this . layers [ layerIndex ] . undo ( frameIndex ) ;
285+ // The layer action that was undone
286+ const layerPath = this . layers [ layerIndex ] . undo ( frameIndex ) ;
287+ // add a third value to the `history` tuple, for `redo` action
288+ this . history [ this . history_offset ] [ 2 ] = layerPath ;
289+
276290 this . onlayeractivate && this . onlayeractivate ( this . layers [ this . active_layer ] ) ;
277291 }
278292
279293 redo ( ) {
294+ if ( this . history_offset === this . history . length ) return ;
295+
296+ const historyItem = this . history [ this . history_offset ] ;
297+ const [ layerId , frameId , path ] = historyItem ;
298+
299+ this . history_offset = Math . min ( this . history . length , this . history_offset + 1 ) ;
300+
301+ const layerIndex = this . layers . findIndex ( l => l . id === layerId ) ;
302+ const frameIndex = this . layers [ layerIndex ] . frames . findIndex ( f => f . id === frameId ) ;
303+ const frameDelta = frameIndex - this . layers [ layerIndex ] . frame ;
304+
305+ if ( ! path ) throw new Error ( `${ layerIndex } :${ frameIndex } : No path found for redo action!` ) ;
280306
307+ if ( this . layers [ layerIndex ] . id !== this . layers [ this . active_layer ] . id ) this . active_layer = layerIndex ;
308+ // move all layers if frame is navigated to
309+ if ( this . layers [ layerIndex ] . frame !== frameIndex ) this . layers . forEach ( l => l . goTo ( l . frame + frameDelta ) ) ;
310+
311+ this . layers [ layerIndex ] . frames [ frameIndex ] . addPath ( path ) ;
312+ // unset saved path (as its now back in the frame)
313+ historyItem [ 2 ] = null ;
314+
315+ this . onlayeractivate && this . onlayeractivate ( this . layers [ this . active_layer ] ) ;
316+ this . layers [ layerIndex ] . render ( ) ;
281317 }
282318
283319 play ( ) {
0 commit comments