0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Add saveUndoState method to public API

For #174
This commit is contained in:
Neil Jenkins 2016-03-11 15:56:34 +11:00
parent 6413034884
commit 62616ef4ec
6 changed files with 51 additions and 38 deletions

View file

@ -211,6 +211,15 @@ document.
Returns self (the Squire instance).
### saveUndoState
Saves an undo checkpoint with the current editor state. Methods that modify the
state (e.g. bold/setHighlightColour/modifyBlocks) will automatically save undo
checkpoints; you only need this method if you want to modify the DOM outside of
one of these methods, and you want to save an undo checkpoint first.
Returns self (the Squire instance).
### undo
Undoes the most recent change.

View file

@ -1212,8 +1212,7 @@ var onKey = function ( event ) {
this._keyHandlers[ key ]( this, event, range );
} else if ( key.length === 1 && !range.collapsed ) {
// Record undo checkpoint.
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
// Delete the selection
deleteContentsOfRange( range );
this._ensureBottomLine();
@ -1396,8 +1395,7 @@ var keyHandlers = {
backspace: function ( self, event, range ) {
self._removeZWS();
// Record undo checkpoint.
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
self.saveUndoState( range );
// If not collapsed, delete contents
if ( !range.collapsed ) {
event.preventDefault();
@ -1464,8 +1462,7 @@ var keyHandlers = {
cursorContainer, cursorOffset, nodeAfterCursor;
self._removeZWS();
// Record undo checkpoint.
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
self.saveUndoState( range );
// If not collapsed, delete contents
if ( !range.collapsed ) {
event.preventDefault();
@ -1989,7 +1986,7 @@ var onCut = function ( event ) {
var self = this;
// Save undo checkpoint
this._recordUndoState( range );
this.saveUndoState( range );
// Edge only seems to support setting plain text as of 2016-03-11.
if ( !isEdge && clipboardData ) {
@ -2008,7 +2005,6 @@ var onCut = function ( event ) {
}, 0 );
}
this._getRangeAndRemoveBookmark( range );
this.setSelection( range );
};
@ -2842,6 +2838,17 @@ proto._recordUndoState = function ( range ) {
}
};
proto.saveUndoState = function ( range ) {
if ( range === undefined ) {
range = this.getSelection();
}
if ( !this._isInUndoState ) {
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
}
return this;
};
proto.undo = function () {
// Sanity check: must not be at beginning of the history stack
if ( this._undoIndex !== 0 || !this._isInUndoState ) {
@ -3212,8 +3219,7 @@ proto.changeFormat = function ( add, remove, range, partial ) {
}
// Save undo checkpoint
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
if ( remove ) {
range = this._removeFormat( remove.tag.toUpperCase(),
@ -3275,8 +3281,7 @@ proto.forEachBlock = function ( fn, mutates, range ) {
// Save undo checkpoint
if ( mutates ) {
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
}
var start = getStartBlockOfRange( range ),
@ -3609,8 +3614,7 @@ proto.setHTML = function ( html ) {
// Record undo state
var range = this._getRangeAndRemoveBookmark() ||
this._createRange( body.firstChild, 0 );
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
// IE will also set focus when selecting text so don't use
// setSelection. Instead, just store it in lastSelection, so if
// anything calls getSelection before first focus, we have a range
@ -3716,8 +3720,7 @@ proto.insertHTML = function ( html, isPaste ) {
frag.appendChild( empty( div ) );
// Record undo checkpoint
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
try {
var node = frag;
@ -3962,8 +3965,7 @@ proto.removeAllFormatting = function ( range ) {
}
// Record undo point
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
// Avoid splitting where we're already at edges.
moveRangeBoundariesUpTree( range, stopNode );

File diff suppressed because one or more lines are too long

View file

@ -8,7 +8,7 @@ var onCut = function ( event ) {
var self = this;
// Save undo checkpoint
this._recordUndoState( range );
this.saveUndoState( range );
// Edge only seems to support setting plain text as of 2016-03-11.
if ( !isEdge && clipboardData ) {
@ -27,7 +27,6 @@ var onCut = function ( event ) {
}, 0 );
}
this._getRangeAndRemoveBookmark( range );
this.setSelection( range );
};

View file

@ -662,6 +662,17 @@ proto._recordUndoState = function ( range ) {
}
};
proto.saveUndoState = function ( range ) {
if ( range === undefined ) {
range = this.getSelection();
}
if ( !this._isInUndoState ) {
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
}
return this;
};
proto.undo = function () {
// Sanity check: must not be at beginning of the history stack
if ( this._undoIndex !== 0 || !this._isInUndoState ) {
@ -1032,8 +1043,7 @@ proto.changeFormat = function ( add, remove, range, partial ) {
}
// Save undo checkpoint
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
if ( remove ) {
range = this._removeFormat( remove.tag.toUpperCase(),
@ -1095,8 +1105,7 @@ proto.forEachBlock = function ( fn, mutates, range ) {
// Save undo checkpoint
if ( mutates ) {
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
}
var start = getStartBlockOfRange( range ),
@ -1429,8 +1438,7 @@ proto.setHTML = function ( html ) {
// Record undo state
var range = this._getRangeAndRemoveBookmark() ||
this._createRange( body.firstChild, 0 );
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
// IE will also set focus when selecting text so don't use
// setSelection. Instead, just store it in lastSelection, so if
// anything calls getSelection before first focus, we have a range
@ -1536,8 +1544,7 @@ proto.insertHTML = function ( html, isPaste ) {
frag.appendChild( empty( div ) );
// Record undo checkpoint
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
try {
var node = frag;
@ -1782,8 +1789,7 @@ proto.removeAllFormatting = function ( range ) {
}
// Record undo point
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
// Avoid splitting where we're already at edges.
moveRangeBoundariesUpTree( range, stopNode );

View file

@ -61,8 +61,7 @@ var onKey = function ( event ) {
this._keyHandlers[ key ]( this, event, range );
} else if ( key.length === 1 && !range.collapsed ) {
// Record undo checkpoint.
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
this.saveUndoState( range );
// Delete the selection
deleteContentsOfRange( range );
this._ensureBottomLine();
@ -245,8 +244,7 @@ var keyHandlers = {
backspace: function ( self, event, range ) {
self._removeZWS();
// Record undo checkpoint.
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
self.saveUndoState( range );
// If not collapsed, delete contents
if ( !range.collapsed ) {
event.preventDefault();
@ -313,8 +311,7 @@ var keyHandlers = {
cursorContainer, cursorOffset, nodeAfterCursor;
self._removeZWS();
// Record undo checkpoint.
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
self.saveUndoState( range );
// If not collapsed, delete contents
if ( !range.collapsed ) {
event.preventDefault();