UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

173 lines 6.34 kB
import { EventObject } from "@aurigma/design-atoms-model/EventObject"; var History = /** @class */ (function () { function History(handler, historySize) { if (historySize === void 0) { historySize = 10; } this._snapshots = []; this._current = -1; this._enabled = true; this._locked = false; this._pauseCount = 0; this._addSnapshotOnResume = false; this._overflowMaxUndoStepCount = false; this._changed = new EventObject(); this._undoPerformed = new EventObject(); this._redoPerformed = new EventObject(); this._snapshotHandler = handler; this.historySize = historySize; } Object.defineProperty(History.prototype, "historySize", { set: function (historySize) { if (historySize >= 0) this._maxUndoStepCount = historySize; else { console.warn("History: historySize value isn't correct, reset to 10"); this._maxUndoStepCount = 10; } }, enumerable: true, configurable: true }); Object.defineProperty(History.prototype, "canRedo", { get: function () { return (this._current < this._snapshots.length - 1); }, enumerable: true, configurable: true }); Object.defineProperty(History.prototype, "canUndo", { get: function () { return (this._current >= 1); }, enumerable: true, configurable: true }); Object.defineProperty(History.prototype, "overflowMaxUndoStepCount", { get: function () { return this._overflowMaxUndoStepCount; }, enumerable: true, configurable: true }); History.prototype.enable = function () { if (!this._enabled) this._enabled = true; }; History.prototype.disable = function () { if (this._enabled) { this._enabled = false; this.clear(); } }; History.prototype.isPaused = function () { return this._pauseCount > 0; }; History.prototype.pause = function () { this._pauseCount++; if (this._pauseCount === 1) this._addSnapshotOnResume = false; }; History.prototype.resume = function (addSnapshot, forceAddSnapshot, replaceLastSnapshot) { if (addSnapshot === void 0) { addSnapshot = true; } if (forceAddSnapshot === void 0) { forceAddSnapshot = false; } if (replaceLastSnapshot === void 0) { replaceLastSnapshot = false; } if (this._pauseCount <= 0) return; this._pauseCount--; if (this._pauseCount === 0) { if (addSnapshot && this._addSnapshotOnResume || forceAddSnapshot) this.addSnapshot(false, replaceLastSnapshot); } }; History.prototype.raiseChanged = function () { this._changed.notify(); }; History.prototype._clearRedo = function () { this._snapshots.splice(this._current + 1); }; History.prototype._clearUndo = function () { this._snapshots.splice(0, this._current + 1); this._current = -1; this._overflowMaxUndoStepCount = false; }; History.prototype.clear = function (suppressOnChanged) { if (suppressOnChanged === void 0) { suppressOnChanged = false; } this._snapshots = []; this._current = -1; this.addSnapshot(true); if (!suppressOnChanged) this.raiseChanged(); }; History.prototype.addSnapshot = function (suppressOnChanged, replaceLastSnapshot) { if (suppressOnChanged === void 0) { suppressOnChanged = false; } if (replaceLastSnapshot === void 0) { replaceLastSnapshot = false; } if (!this._enabled || this._locked) return; if (this._pauseCount > 0) { this._addSnapshotOnResume = true; return; } var snapshot = this._snapshotHandler.createSnapshot(); if (snapshot == null) throw "Snapshot can not be null"; this._locked = true; if (this.canRedo) this._clearRedo(); if (replaceLastSnapshot && this._snapshots.length > 0) { this._snapshots.pop(); this._current--; } this._snapshots.push(snapshot); this._current++; this._locked = false; if (this._current > this._maxUndoStepCount) { this._snapshots.splice(0, 1); this._current--; this._overflowMaxUndoStepCount = true; } if (!suppressOnChanged) this.raiseChanged(); }; History.prototype.redo = function () { if (!this._enabled || !this.canRedo) return; this._locked = true; this._current++; var snapshot = this._snapshots[this._current]; this._snapshotHandler.loadSnapshot(snapshot); this._locked = false; this.raiseChanged(); this._redoPerformed.notify(); }; History.prototype.undo = function () { if (!this._enabled || !this.canUndo) return; this._locked = true; this._current--; var snapshot = this._snapshots[this._current]; this._snapshotHandler.loadSnapshot(snapshot); this._locked = false; this.raiseChanged(); this._undoPerformed.notify(); }; History.prototype.addChanged = function (listener) { this._changed.add(listener); }; History.prototype.removeChanged = function (listener) { this._changed.remove(listener); }; History.prototype.addUndoPerformed = function (listener) { this._undoPerformed.add(listener); }; History.prototype.removeUndoPerformed = function (listener) { this._undoPerformed.remove(listener); }; History.prototype.addRedoPerformed = function (listener) { this._redoPerformed.add(listener); }; History.prototype.removeRedoPerformed = function (listener) { this._redoPerformed.remove(listener); }; return History; }()); export { History }; //# sourceMappingURL=History.js.map