/* * ********************************************************************************************************************* * * !! * .F88X * X8888Y * .}888888N; * i888888N; .:! .I$WI: * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8& * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I * +888888N; .8888888Y "&&8Y.}8, * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$* * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~ * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi' * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/ * .:R888888I * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司 * ~8888' * .!88~ All rights reserved. * * ********************************************************************************************************************* */ import { SObject } from "../SObject"; import { SUndoCommand } from "./SUndoCommand"; import { SCommandLog } from "./SCommandLog"; /** * Undo操作堆栈 * * @author 庞利祥(sybotan@126.com) */ export class SUndoStack extends SObject { /** 命令栈 */ private cmdStack = Array(); /** 当前命令索引 */ private _index = -1; get index(): number { return this._index; } // Get index /** 命令栈是否为空 */ get isEmpty(): boolean { return this.cmdStack.length <= 0; } // Get isEmpty /** 是否更改 */ isChange: boolean = false; /** * 执行redo操作 */ redo(): void { if (!this.canRedo()) { return; } this._index++; this.cmdStack[this._index].redo(); this.isChange = true; } // Function redo() /** * 执行undo操作 */ undo(): void { if (!this.canUndo()) { return; } this.cmdStack[this._index].undo(); this._index--; this.isChange = true; } // Function undo() /** * 是否可以执行Redo操作 * * @return 可以执行,返回true;否则返回false。 */ canRedo(): boolean { return this.index + 1 < this.cmdStack.length; } // Function canRedo() /** * 是否可以执行Undo操作 * * @return 可以执行,返回true;否则返回false。 */ canUndo(): boolean { return this.index >= 0; } // Function canUndo() /** * 清空堆栈 */ clear(): void { this.cmdStack.length = 0; this._index = -1; this.isChange = true; } // Function clear() /** * 返回指定索引的命令 * * @param index 命令在栈中的索引 */ command(index: number): SUndoCommand | null { // 如果索引越界,则返回null if (index < 0 || index >= this.cmdStack.length) { return null; } return this.cmdStack[index]; } // Function command() /** * 统计命令栈中命令的数量 */ count(): number { return this.cmdStack.length; } // Function count() /** * 将命令添加到命令栈 * * @param cmd 被添加的命令 */ push(cmd: SUndoCommand): void { this.cmdStack.length = this._index + 1; if (this._index >= 0 && cmd.mergeWith(this.cmdStack[this._index])) { return; } this.cmdStack.push(cmd); this._index = this.cmdStack.length - 1; this.isChange = true; } // Function push() /** * 将命令堆栈转为日志(命令数组) * * */ toLog(): SCommandLog[] { let stackList: SCommandLog[] = []; for (let i = 0; i <= this.index; i++) { stackList.push({ command: this.cmdStack[i].command, desc: this.cmdStack[i].desc, detail: this.cmdStack[i].toString() }); } return stackList; } // Function toLog() } // Class SUndoStack