Created
May 4, 2016 02:50
-
-
Save mmmpa/7db8a0a4e54b0019b21d69db8e7b5224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Stack{ | |
| up:()=>void, | |
| down:()=>void | |
| } | |
| export default class HistoryStack { | |
| private history:Stack[] = []; | |
| private position:number = 0; | |
| constructor(public length = 10){ | |
| this.length++; | |
| } | |
| stock(command):void { | |
| this.now = command; | |
| this.stepForward(); | |
| this.now && this.dispose(); | |
| //this.now = null; | |
| } | |
| undo():void { | |
| if (!this.isUndoable) { | |
| return; | |
| } | |
| this.stepBackward(); | |
| this.now.down() | |
| } | |
| redo():void { | |
| if (!this.isRedoable) { | |
| return; | |
| } | |
| this.now.up(); | |
| this.stepForward(); | |
| } | |
| stepForward(){ | |
| this.position++; | |
| if(this.position === this.length){ | |
| this.position = 0; | |
| } | |
| } | |
| stepBackward(){ | |
| if(this.position === 0){ | |
| this.position = this.length - 1; | |
| }else{ | |
| this.position--; | |
| } | |
| } | |
| get isUndoable():boolean { | |
| return !!this.previous; | |
| } | |
| get isRedoable():boolean { | |
| return !!this.now; | |
| } | |
| get previous():Stack { | |
| if(this.position === 0){ | |
| return this.history[this.length - 1]; | |
| }else{ | |
| return this.history[this.position - 1]; | |
| } | |
| } | |
| get now():Stack { | |
| return this.history[this.position]; | |
| } | |
| set now(v:Stack) { | |
| this.history[this.position] = v; | |
| } | |
| private dispose(){ | |
| delete this.history[this.position]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment