history-actions
Version:
Action manager framework with undo/redo
87 lines • 3.23 kB
JavaScript
import ChangeLog from '../model/ChangeLog';
var HistoryManager = /** @class */ (function () {
function HistoryManager() {
this._recording = new ChangeLog();
this._done = [];
this._undone = [];
this._maxLogs = 20;
}
Object.defineProperty(HistoryManager.prototype, "maxLogs", {
/** The max number of undo's allowed (default: 20) */
get: function () {
return this._maxLogs;
},
set: function (value) {
this._maxLogs = value;
},
enumerable: true,
configurable: true
});
/** Returns true if an Action has been pushed to the current MutationLog */
HistoryManager.prototype.isRecording = function () {
return this._recording.actions.length > 0;
};
/** Clear all mutation logs and reset */
HistoryManager.prototype.clear = function () {
this._recording = new ChangeLog();
this._done = [];
this._undone = [];
};
/** Pushes an <Action> to the current <MutationLog> being recorded */
HistoryManager.prototype.record = function (action) {
this._recording.actions.push(action);
};
/** Returns the current <MutationLog> being recorded. */
HistoryManager.prototype.getRecording = function () {
return this._recording;
};
/** Returns the last recorded <Action> in the current <MutationLog> being recorded. */
HistoryManager.prototype.getLastRecordedAction = function () {
return this._recording.actions[this._recording.actions.length - 1];
};
/** Saves the current <MutationLog> being recorded */
HistoryManager.prototype.save = function () {
this._undone = [];
this._done.push(this._recording);
if (this._done.length > this._maxLogs) {
this._done.shift();
console.warn("The last MutationLog was dropped. You are trying to push more mutation logs than allowed. Please fix your code, or set a higher 'historyManager.maxLogs'");
}
this._recording = new ChangeLog();
};
/** Undo the last saved <MutationLog> */
HistoryManager.prototype.undo = function () {
if (this._done.length === 0)
return;
// remove last log "done"
var log = this._done.pop();
// execute log's actions undo's
this.executeUndo(log);
// add to undone
this._undone.push(log);
};
/** Redo the last undone <MutationLog> */
HistoryManager.prototype.redo = function () {
if (this._undone.length === 0)
return;
// remove last log "undone"
var log = this._undone.pop();
// execute log's actions redo's
this.executeRedo(log);
// add to done
this._done.push(log);
};
HistoryManager.prototype.executeUndo = function (log) {
for (var i = log.actions.length - 1; i >= 0; i--) {
log.actions[i].undo();
}
};
HistoryManager.prototype.executeRedo = function (log) {
for (var i in log.actions) {
log.actions[i].redo();
}
};
return HistoryManager;
}());
export var historyManager = new HistoryManager();
//# sourceMappingURL=HistoryManager.js.map