mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -05:00
Limit the undo stack size
Add an undo configuration. If the document is larger than the configured threshold, then limit the number of undo states that can be saved to the configured amount. Defaults to no limit.
This commit is contained in:
parent
9dcc4fb79f
commit
bb8cbe109d
3 changed files with 62 additions and 2 deletions
|
@ -2435,6 +2435,10 @@ proto.setConfig = function ( config ) {
|
|||
ol: null,
|
||||
li: null,
|
||||
a: null
|
||||
},
|
||||
undo: {
|
||||
documentSizeThreshold: -1, // -1 means no threshold
|
||||
undoLimit: -1 // -1 means no limit
|
||||
}
|
||||
}, config );
|
||||
|
||||
|
@ -2531,6 +2535,7 @@ proto.destroy = function () {
|
|||
var l = instances.length;
|
||||
var events = this._events;
|
||||
var type;
|
||||
|
||||
for ( type in events ) {
|
||||
this.removeEventListener( type );
|
||||
}
|
||||
|
@ -2542,6 +2547,11 @@ proto.destroy = function () {
|
|||
instances.splice( l, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
// Destroy undo stack
|
||||
this._undoIndex = -1;
|
||||
this._undoStack = [];
|
||||
this._undoStackLength = 0;
|
||||
};
|
||||
|
||||
proto.handleEvent = function ( event ) {
|
||||
|
@ -2969,6 +2979,12 @@ proto._docWasChanged = function () {
|
|||
this.fireEvent( 'input' );
|
||||
};
|
||||
|
||||
// Gets the size of the document in bytes.
|
||||
var documentSizeInBytes = function () {
|
||||
// Javascript uses 2 bytes per character
|
||||
return this._getHTML().length * 2;
|
||||
};
|
||||
|
||||
// Leaves bookmark
|
||||
proto._recordUndoState = function ( range ) {
|
||||
// Don't record if we're already in an undo state
|
||||
|
@ -2982,6 +2998,20 @@ proto._recordUndoState = function ( range ) {
|
|||
undoStack.length = this._undoStackLength = undoIndex;
|
||||
}
|
||||
|
||||
var undoThreshold = this._config.undo.documentSizeThreshold,
|
||||
undoLimit = this._config.undo.undoLimit;
|
||||
|
||||
// If this document is above the configured size threshold, limit the number
|
||||
// of saved undo states.
|
||||
if( undoThreshold > -1 && undoThreshold <= documentSizeInBytes.call( this ) ) {
|
||||
if( undoLimit > -1 && undoLimit < undoIndex ) {
|
||||
undoStack.splice( 0, undoIndex - undoLimit );
|
||||
|
||||
undoIndex = this._undoIndex = undoLimit;
|
||||
this._undoStackLength = undoStack.length;
|
||||
}
|
||||
}
|
||||
|
||||
// Write out data
|
||||
if ( range ) {
|
||||
this._saveRangeToBookmark( range );
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -167,6 +167,10 @@ proto.setConfig = function ( config ) {
|
|||
ol: null,
|
||||
li: null,
|
||||
a: null
|
||||
},
|
||||
undo: {
|
||||
documentSizeThreshold: -1, // -1 means no threshold
|
||||
undoLimit: -1 // -1 means no limit
|
||||
}
|
||||
}, config );
|
||||
|
||||
|
@ -263,6 +267,7 @@ proto.destroy = function () {
|
|||
var l = instances.length;
|
||||
var events = this._events;
|
||||
var type;
|
||||
|
||||
for ( type in events ) {
|
||||
this.removeEventListener( type );
|
||||
}
|
||||
|
@ -274,6 +279,11 @@ proto.destroy = function () {
|
|||
instances.splice( l, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
// Destroy undo stack
|
||||
this._undoIndex = -1;
|
||||
this._undoStack = [];
|
||||
this._undoStackLength = 0;
|
||||
};
|
||||
|
||||
proto.handleEvent = function ( event ) {
|
||||
|
@ -701,6 +711,12 @@ proto._docWasChanged = function () {
|
|||
this.fireEvent( 'input' );
|
||||
};
|
||||
|
||||
// Gets the size of the document in bytes.
|
||||
var documentSizeInBytes = function () {
|
||||
// Javascript uses 2 bytes per character
|
||||
return this._getHTML().length * 2;
|
||||
};
|
||||
|
||||
// Leaves bookmark
|
||||
proto._recordUndoState = function ( range ) {
|
||||
// Don't record if we're already in an undo state
|
||||
|
@ -714,6 +730,20 @@ proto._recordUndoState = function ( range ) {
|
|||
undoStack.length = this._undoStackLength = undoIndex;
|
||||
}
|
||||
|
||||
var undoThreshold = this._config.undo.documentSizeThreshold,
|
||||
undoLimit = this._config.undo.undoLimit;
|
||||
|
||||
// If this document is above the configured size threshold, limit the number
|
||||
// of saved undo states.
|
||||
if( undoThreshold > -1 && undoThreshold <= documentSizeInBytes.call( this ) ) {
|
||||
if( undoLimit > -1 && undoLimit < undoIndex ) {
|
||||
undoStack.splice( 0, undoIndex - undoLimit );
|
||||
|
||||
undoIndex = this._undoIndex = undoLimit;
|
||||
this._undoStackLength = undoStack.length;
|
||||
}
|
||||
}
|
||||
|
||||
// Write out data
|
||||
if ( range ) {
|
||||
this._saveRangeToBookmark( range );
|
||||
|
|
Loading…
Reference in a new issue