SUndoStack.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.SUndoStack = void 0;
  17. var SObject_1 = require("../SObject");
  18. var SUndoStack = (function (_super) {
  19. __extends(SUndoStack, _super);
  20. function SUndoStack() {
  21. var _this = _super !== null && _super.apply(this, arguments) || this;
  22. _this.cmdStack = Array();
  23. _this._index = -1;
  24. _this.isChange = false;
  25. return _this;
  26. }
  27. Object.defineProperty(SUndoStack.prototype, "index", {
  28. get: function () {
  29. return this._index;
  30. },
  31. enumerable: false,
  32. configurable: true
  33. });
  34. Object.defineProperty(SUndoStack.prototype, "isEmpty", {
  35. get: function () {
  36. return this.cmdStack.length <= 0;
  37. },
  38. enumerable: false,
  39. configurable: true
  40. });
  41. SUndoStack.prototype.redo = function () {
  42. if (!this.canRedo()) {
  43. return;
  44. }
  45. this._index++;
  46. this.cmdStack[this._index].redo();
  47. this.isChange = true;
  48. };
  49. SUndoStack.prototype.undo = function () {
  50. if (!this.canUndo()) {
  51. return;
  52. }
  53. this.cmdStack[this._index].undo();
  54. this._index--;
  55. this.isChange = true;
  56. };
  57. SUndoStack.prototype.canRedo = function () {
  58. return this.index + 1 < this.cmdStack.length;
  59. };
  60. SUndoStack.prototype.canUndo = function () {
  61. return this.index >= 0;
  62. };
  63. SUndoStack.prototype.clear = function () {
  64. this.cmdStack.length = 0;
  65. this._index = -1;
  66. this.isChange = true;
  67. };
  68. SUndoStack.prototype.command = function (index) {
  69. if (index < 0 || index >= this.cmdStack.length) {
  70. return null;
  71. }
  72. return this.cmdStack[index];
  73. };
  74. SUndoStack.prototype.count = function () {
  75. return this.cmdStack.length;
  76. };
  77. SUndoStack.prototype.push = function (cmd) {
  78. this.cmdStack.length = this._index + 1;
  79. if (this._index >= 0 && cmd.mergeWith(this.cmdStack[this._index])) {
  80. return;
  81. }
  82. this.cmdStack.push(cmd);
  83. this._index = this.cmdStack.length - 1;
  84. this.isChange = true;
  85. };
  86. SUndoStack.prototype.toLog = function () {
  87. var stackList = [];
  88. for (var i = 0; i <= this.index; i++) {
  89. stackList.push({
  90. command: this.cmdStack[i].command,
  91. desc: this.cmdStack[i].desc,
  92. detail: this.cmdStack[i].toString()
  93. });
  94. }
  95. return stackList;
  96. };
  97. return SUndoStack;
  98. }(SObject_1.SObject));
  99. exports.SUndoStack = SUndoStack;