SUndoStack.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SObject } from "../SObject";
  27. import { SUndoCommand } from "./SUndoCommand";
  28. import { SCommandLog } from "./SCommandLog";
  29. /**
  30. * Undo操作堆栈
  31. *
  32. * @author 庞利祥(sybotan@126.com)
  33. */
  34. export class SUndoStack extends SObject {
  35. /** 命令栈 */
  36. private cmdStack = Array<SUndoCommand>();
  37. /** 当前命令索引 */
  38. private _index = -1;
  39. get index(): number {
  40. return this._index;
  41. } // Get index
  42. /** 命令栈是否为空 */
  43. get isEmpty(): boolean {
  44. return this.cmdStack.length <= 0;
  45. } // Get isEmpty
  46. /** 是否更改 */
  47. isChange: boolean = false;
  48. /**
  49. * 执行redo操作
  50. */
  51. redo(): void {
  52. if (!this.canRedo()) {
  53. return;
  54. }
  55. this._index++;
  56. this.cmdStack[this._index].redo();
  57. this.isChange = true;
  58. } // Function redo()
  59. /**
  60. * 执行undo操作
  61. */
  62. undo(): void {
  63. if (!this.canUndo()) {
  64. return;
  65. }
  66. this.cmdStack[this._index].undo();
  67. this._index--;
  68. this.isChange = true;
  69. } // Function undo()
  70. /**
  71. * 是否可以执行Redo操作
  72. *
  73. * @return 可以执行,返回true;否则返回false。
  74. */
  75. canRedo(): boolean {
  76. return this.index + 1 < this.cmdStack.length;
  77. } // Function canRedo()
  78. /**
  79. * 是否可以执行Undo操作
  80. *
  81. * @return 可以执行,返回true;否则返回false。
  82. */
  83. canUndo(): boolean {
  84. return this.index >= 0;
  85. } // Function canUndo()
  86. /**
  87. * 清空堆栈
  88. */
  89. clear(): void {
  90. this.cmdStack.length = 0;
  91. this._index = -1;
  92. this.isChange = true;
  93. } // Function clear()
  94. /**
  95. * 返回指定索引的命令
  96. *
  97. * @param index 命令在栈中的索引
  98. */
  99. command(index: number): SUndoCommand | null {
  100. // 如果索引越界,则返回null
  101. if (index < 0 || index >= this.cmdStack.length) {
  102. return null;
  103. }
  104. return this.cmdStack[index];
  105. } // Function command()
  106. /**
  107. * 统计命令栈中命令的数量
  108. */
  109. count(): number {
  110. return this.cmdStack.length;
  111. } // Function count()
  112. /**
  113. * 将命令添加到命令栈
  114. *
  115. * @param cmd 被添加的命令
  116. */
  117. push(cmd: SUndoCommand): void {
  118. this.cmdStack.length = this._index + 1;
  119. if (this._index >= 0 && cmd.mergeWith(this.cmdStack[this._index])) {
  120. return;
  121. }
  122. this.cmdStack.push(cmd);
  123. this._index = this.cmdStack.length - 1;
  124. this.isChange = true;
  125. } // Function push()
  126. /**
  127. * 将命令堆栈转为日志(命令数组)
  128. *
  129. * */
  130. toLog(): SCommandLog[] {
  131. let stackList: SCommandLog[] = [];
  132. for (let i = 0; i <= this.index; i++) {
  133. stackList.push({
  134. command: this.cmdStack[i].command,
  135. desc: this.cmdStack[i].desc,
  136. detail: this.cmdStack[i].toString()
  137. });
  138. }
  139. return stackList;
  140. } // Function toLog()
  141. } // Class SUndoStack