mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
issue #374 limit undo/redo to the last 500 states
This commit is contained in:
parent
e3b363d757
commit
4b50dfdb5b
@ -24,6 +24,9 @@
|
|||||||
// Interval/buffer (in milliseconds) between two state load (ctrl+z/y spamming)
|
// Interval/buffer (in milliseconds) between two state load (ctrl+z/y spamming)
|
||||||
ns.HistoryService.LOAD_STATE_INTERVAL = 50;
|
ns.HistoryService.LOAD_STATE_INTERVAL = 50;
|
||||||
|
|
||||||
|
// Maximum number of states that can be recorded.
|
||||||
|
ns.HistoryService.MAX_SAVED_STATES = 500;
|
||||||
|
|
||||||
ns.HistoryService.prototype.init = function () {
|
ns.HistoryService.prototype.init = function () {
|
||||||
$.subscribe(Events.PISKEL_SAVE_STATE, this.onSaveStateEvent.bind(this));
|
$.subscribe(Events.PISKEL_SAVE_STATE, this.onSaveStateEvent.bind(this));
|
||||||
|
|
||||||
@ -41,7 +44,6 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
ns.HistoryService.prototype.saveState = function (action) {
|
ns.HistoryService.prototype.saveState = function (action) {
|
||||||
this.stateQueue = this.stateQueue.slice(0, this.currentIndex + 1);
|
|
||||||
this.currentIndex = this.currentIndex + 1;
|
this.currentIndex = this.currentIndex + 1;
|
||||||
|
|
||||||
var state = {
|
var state = {
|
||||||
@ -58,6 +60,13 @@
|
|||||||
state.piskel = this.serializer.serialize(piskel);
|
state.piskel = this.serializer.serialize(piskel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the new state pushes over MAX_SAVED_STATES, erase all states between the first and
|
||||||
|
// second snapshot states.
|
||||||
|
if (this.stateQueue.length > ns.HistoryService.MAX_SAVED_STATES) {
|
||||||
|
var firstSnapshotIndex = this.getNextSnapshotIndex_(1);
|
||||||
|
this.stateQueue.splice(0, firstSnapshotIndex);
|
||||||
|
this.currentIndex = this.currentIndex - firstSnapshotIndex;
|
||||||
|
}
|
||||||
this.stateQueue.push(state);
|
this.stateQueue.push(state);
|
||||||
$.publish(Events.HISTORY_STATE_SAVED);
|
$.publish(Events.HISTORY_STATE_SAVED);
|
||||||
};
|
};
|
||||||
@ -92,6 +101,13 @@
|
|||||||
return index;
|
return index;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ns.HistoryService.prototype.getNextSnapshotIndex_ = function (index) {
|
||||||
|
while (this.stateQueue[index] && !this.stateQueue[index].piskel) {
|
||||||
|
index = index + 1;
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
};
|
||||||
|
|
||||||
ns.HistoryService.prototype.loadState = function (index) {
|
ns.HistoryService.prototype.loadState = function (index) {
|
||||||
try {
|
try {
|
||||||
if (this.isLoadStateAllowed_(index)) {
|
if (this.isLoadStateAllowed_(index)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user