0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-09 08:20:08 -05:00

Fix undo does not always restore cursor position.

If the content hasn't changed before an undo point is requested we ignore the
request, but the cursor position may have changed and users expect undo to
restore the cursor position immediately before the requested change was made.

So in this instance we now still record an undo state, but replace the previous
one if the content is unchanged.
This commit is contained in:
Neil Jenkins 2017-07-16 12:43:17 +02:00
parent 48fabd491a
commit 6842cb94eb
3 changed files with 30 additions and 30 deletions

View file

@ -3218,17 +3218,21 @@ proto._docWasChanged = function () {
};
// Leaves bookmark
proto._recordUndoState = function ( range ) {
proto._recordUndoState = function ( range, replace ) {
// Don't record if we're already in an undo state
if ( !this._isInUndoState ) {
if ( !this._isInUndoState|| replace ) {
// Advance pointer to new position
var undoIndex = this._undoIndex += 1;
var undoIndex = this._undoIndex;
var undoStack = this._undoStack;
var undoConfig = this._config.undo;
var undoThreshold = undoConfig.documentSizeThreshold;
var undoLimit = undoConfig.undoLimit;
var html;
if ( !replace ) {
undoIndex += 1;
}
// Truncate stack if longer (i.e. if has been previously undone)
if ( undoIndex < this._undoStackLength ) {
undoStack.length = this._undoStackLength = undoIndex;
@ -3246,13 +3250,14 @@ proto._recordUndoState = function ( range ) {
if ( undoThreshold > -1 && html.length * 2 > undoThreshold ) {
if ( undoLimit > -1 && undoIndex > undoLimit ) {
undoStack.splice( 0, undoIndex - undoLimit );
undoIndex = this._undoIndex = undoLimit;
undoIndex = undoLimit;
this._undoStackLength = undoLimit;
}
}
// Save data
undoStack[ undoIndex ] = html;
this._undoIndex = undoIndex;
this._undoStackLength += 1;
this._isInUndoState = true;
}
@ -3262,10 +3267,9 @@ proto.saveUndoState = function ( range ) {
if ( range === undefined ) {
range = this.getSelection();
}
if ( !this._isInUndoState ) {
this._recordUndoState( range );
this._recordUndoState( range, this._isInUndoState );
this._getRangeAndRemoveBookmark( range );
}
return this;
};
@ -3273,7 +3277,7 @@ proto.undo = function () {
// Sanity check: must not be at beginning of the history stack
if ( this._undoIndex !== 0 || !this._isInUndoState ) {
// Make sure any changes since last checkpoint are saved.
this._recordUndoState( this.getSelection() );
this._recordUndoState( this.getSelection(), false );
this._undoIndex -= 1;
this._setHTML( this._undoStack[ this._undoIndex ] );
@ -3737,11 +3741,7 @@ proto.modifyBlocks = function ( modify, range ) {
}
// 1. Save undo checkpoint and bookmark selection
if ( this._isInUndoState ) {
this._saveRangeToBookmark( range );
} else {
this._recordUndoState( range );
}
this._recordUndoState( range, this._isInUndoState );
var root = this._root;
var frag;

File diff suppressed because one or more lines are too long

View file

@ -784,17 +784,21 @@ proto._docWasChanged = function () {
};
// Leaves bookmark
proto._recordUndoState = function ( range ) {
proto._recordUndoState = function ( range, replace ) {
// Don't record if we're already in an undo state
if ( !this._isInUndoState ) {
if ( !this._isInUndoState|| replace ) {
// Advance pointer to new position
var undoIndex = this._undoIndex += 1;
var undoIndex = this._undoIndex;
var undoStack = this._undoStack;
var undoConfig = this._config.undo;
var undoThreshold = undoConfig.documentSizeThreshold;
var undoLimit = undoConfig.undoLimit;
var html;
if ( !replace ) {
undoIndex += 1;
}
// Truncate stack if longer (i.e. if has been previously undone)
if ( undoIndex < this._undoStackLength ) {
undoStack.length = this._undoStackLength = undoIndex;
@ -812,13 +816,14 @@ proto._recordUndoState = function ( range ) {
if ( undoThreshold > -1 && html.length * 2 > undoThreshold ) {
if ( undoLimit > -1 && undoIndex > undoLimit ) {
undoStack.splice( 0, undoIndex - undoLimit );
undoIndex = this._undoIndex = undoLimit;
undoIndex = undoLimit;
this._undoStackLength = undoLimit;
}
}
// Save data
undoStack[ undoIndex ] = html;
this._undoIndex = undoIndex;
this._undoStackLength += 1;
this._isInUndoState = true;
}
@ -828,10 +833,9 @@ proto.saveUndoState = function ( range ) {
if ( range === undefined ) {
range = this.getSelection();
}
if ( !this._isInUndoState ) {
this._recordUndoState( range );
this._recordUndoState( range, this._isInUndoState );
this._getRangeAndRemoveBookmark( range );
}
return this;
};
@ -839,7 +843,7 @@ proto.undo = function () {
// Sanity check: must not be at beginning of the history stack
if ( this._undoIndex !== 0 || !this._isInUndoState ) {
// Make sure any changes since last checkpoint are saved.
this._recordUndoState( this.getSelection() );
this._recordUndoState( this.getSelection(), false );
this._undoIndex -= 1;
this._setHTML( this._undoStack[ this._undoIndex ] );
@ -1303,11 +1307,7 @@ proto.modifyBlocks = function ( modify, range ) {
}
// 1. Save undo checkpoint and bookmark selection
if ( this._isInUndoState ) {
this._saveRangeToBookmark( range );
} else {
this._recordUndoState( range );
}
this._recordUndoState( range, this._isInUndoState );
var root = this._root;
var frag;