0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Save undo step on delete.

* But only if it's deleting whitespace or destroying a block or deleting a
  selection. This makes it undo deletion word-by-word rather than
  character-by-character.
This commit is contained in:
Neil Jenkins 2013-02-25 13:52:22 +11:00
parent e3ad6c6b18
commit 327b48b9db
2 changed files with 27 additions and 6 deletions

File diff suppressed because one or more lines are too long

View file

@ -29,6 +29,8 @@
var losesSelectionOnBlur = UA.losesSelectionOnBlur; var losesSelectionOnBlur = UA.losesSelectionOnBlur;
var useTextFixer = UA.useTextFixer; var useTextFixer = UA.useTextFixer;
var notWS = /\S/;
// --- DOM Sugar --- // --- DOM Sugar ---
var createElement = function ( tag, props, children ) { var createElement = function ( tag, props, children ) {
@ -1000,7 +1002,7 @@
var spanToSemantic = { var spanToSemantic = {
color: { color: {
regexp: /\S/, regexp: notWS,
replace: function ( color ) { replace: function ( color ) {
return createElement( 'SPAN', { return createElement( 'SPAN', {
'class': 'colour', 'class': 'colour',
@ -1021,7 +1023,7 @@
} }
}, },
fontFamily: { fontFamily: {
regexp: /\S/, regexp: notWS,
replace: function ( family ) { replace: function ( family ) {
return createElement( 'SPAN', { return createElement( 'SPAN', {
'class': 'font', 'class': 'font',
@ -1030,7 +1032,7 @@
} }
}, },
fontSize: { fontSize: {
regexp: /\S/, regexp: notWS,
replace: function ( size ) { replace: function ( size ) {
return createElement( 'SPAN', { return createElement( 'SPAN', {
'class': 'size', 'class': 'size',
@ -1164,7 +1166,7 @@
cleanTree( child, allowStyles ); cleanTree( child, allowStyles );
} }
} else if ( nodeType !== TEXT_NODE || ( } else if ( nodeType !== TEXT_NODE || (
!( /\S/.test( child.data ) ) && !( notWS.test( child.data ) ) &&
!( i > 0 && children[ i - 1 ].isInline() ) && !( i > 0 && children[ i - 1 ].isInline() ) &&
!( i + 1 < l && children[ i + 1 ].isInline() ) !( i + 1 < l && children[ i + 1 ].isInline() )
) ) { ) ) {
@ -1230,7 +1232,7 @@
// placeholder to stop the block from collapsing, so we must leave // placeholder to stop the block from collapsing, so we must leave
// it. // it.
else if ( tagAfterSplit[ block.nodeName ] && else if ( tagAfterSplit[ block.nodeName ] &&
/\S/.test( block.textContent ) ) { notWS.test( block.textContent ) ) {
splitBlock( block, br.parentNode, br ); splitBlock( block, br.parentNode, br );
br.detach(); br.detach();
} }
@ -1588,6 +1590,8 @@
var range = getSelection(); var range = getSelection();
// If not collapsed, delete contents // If not collapsed, delete contents
if ( !range.collapsed ) { if ( !range.collapsed ) {
recordUndoState( range );
getRangeAndRemoveBookmark( range );
event.preventDefault(); event.preventDefault();
range._deleteContents(); range._deleteContents();
setSelection( range ); setSelection( range );
@ -1595,6 +1599,8 @@
} }
// If at beginning of block, merge with previous // If at beginning of block, merge with previous
else if ( range.startsAtBlockBoundary() ) { else if ( range.startsAtBlockBoundary() ) {
recordUndoState( range );
getRangeAndRemoveBookmark( range );
event.preventDefault(); event.preventDefault();
var current = range.getStartBlock(), var current = range.getStartBlock(),
previous = current.getPreviousBlock(); previous = current.getPreviousBlock();
@ -1636,6 +1642,11 @@
// Otherwise, leave to browser but check afterwards whether it has // Otherwise, leave to browser but check afterwards whether it has
// left behind an empty inline tag. // left behind an empty inline tag.
else { else {
var text = range.startContainer.data || '';
if ( !notWS.test( text.charAt( range.startOffset - 1 ) ) ) {
recordUndoState( range );
getRangeAndRemoveBookmark( range );
}
setTimeout( afterDelete, 0 ); setTimeout( afterDelete, 0 );
} }
}, },
@ -1643,6 +1654,8 @@
var range = getSelection(); var range = getSelection();
// If not collapsed, delete contents // If not collapsed, delete contents
if ( !range.collapsed ) { if ( !range.collapsed ) {
recordUndoState( range );
getRangeAndRemoveBookmark( range );
event.preventDefault(); event.preventDefault();
range._deleteContents(); range._deleteContents();
setSelection( range ); setSelection( range );
@ -1650,6 +1663,8 @@
} }
// If at end of block, merge next into this block // If at end of block, merge next into this block
else if ( range.endsAtBlockBoundary() ) { else if ( range.endsAtBlockBoundary() ) {
recordUndoState( range );
getRangeAndRemoveBookmark( range );
event.preventDefault(); event.preventDefault();
var current = range.getStartBlock(), var current = range.getStartBlock(),
next = current.getNextBlock(); next = current.getNextBlock();
@ -1678,6 +1693,12 @@
// Otherwise, leave to browser but check afterwards whether it has // Otherwise, leave to browser but check afterwards whether it has
// left behind an empty inline tag. // left behind an empty inline tag.
else { else {
// Record undo point if deleting whitespace
var text = range.startContainer.data || '';
if ( !notWS.test( text.charAt( range.startOffset ) ) ) {
recordUndoState( range );
getRangeAndRemoveBookmark( range );
}
setTimeout( afterDelete, 0 ); setTimeout( afterDelete, 0 );
} }
}, },