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

Take over selection deletion in more cases.

Never really want to let the browser delete a selection; it always makes a mess
of it. This now covers the case when an English letter key or number key is
pressed when a selection exists. Handling this completely (to include all keys
that produce a symbol) is tricky due to cross-browser key event compatibility
issues.
This commit is contained in:
Neil Jenkins 2014-12-26 14:27:19 +07:00
parent 4a264a52c7
commit a46dc2ef02
3 changed files with 35 additions and 37 deletions

View file

@ -2726,15 +2726,12 @@ var afterDelete = function ( self, range ) {
};
var keyHandlers = {
enter: function ( self, event ) {
enter: function ( self, event, range ) {
var block, parent, tag, splitTag, nodeAfterSplit;
// We handle this ourselves
event.preventDefault();
// Must have some form of selection
var range = self.getSelection(),
block, parent, tag, splitTag, nodeAfterSplit;
if ( !range ) { return; }
// Save undo checkpoint and add any links in the preceding section.
// Remove any zws so we don't think there's content in an empty
// block.
@ -2879,10 +2876,9 @@ var keyHandlers = {
// We're not still in an undo state
self._docWasChanged();
},
backspace: function ( self, event ) {
backspace: function ( self, event, range ) {
self._removeZWS();
// Record undo checkpoint.
var range = self.getSelection();
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
// If not collapsed, delete contents
@ -2939,10 +2935,9 @@ var keyHandlers = {
setTimeout( function () { afterDelete( self ); }, 0 );
}
},
'delete': function ( self, event ) {
'delete': function ( self, event, range ) {
self._removeZWS();
// Record undo checkpoint.
var range = self.getSelection();
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
// If not collapsed, delete contents
@ -2985,10 +2980,9 @@ var keyHandlers = {
setTimeout( function () { afterDelete( self ); }, 0 );
}
},
tab: function ( self, event ) {
tab: function ( self, event, range ) {
var node, parent;
self._removeZWS();
var range = self.getSelection(),
node, parent;
// If no selection and in an empty block
if ( range.collapsed &&
rangeDoesStartAtBlockBoundary( range ) &&
@ -3011,9 +3005,8 @@ var keyHandlers = {
event.preventDefault();
}
},
space: function ( self ) {
var range = self.getSelection(),
node, parent;
space: function ( self, _, range ) {
var node, parent;
self._recordUndoState( range );
addLinks( range.startContainer );
self._getRangeAndRemoveBookmark( range );
@ -3071,7 +3064,8 @@ keyHandlers[ ctrlKey + 'shift-z' ] = mapKeyTo( 'redo' );
proto._onKey = function ( event ) {
var code = event.keyCode,
key = keys[ code ],
modifiers = '';
modifiers = '',
range = this.getSelection();
if ( !key ) {
key = String.fromCharCode( code ).toLowerCase();
@ -3106,7 +3100,12 @@ proto._onKey = function ( event ) {
key = modifiers + key;
if ( keyHandlers[ key ] ) {
keyHandlers[ key ]( this, event );
keyHandlers[ key ]( this, event, range );
} else if ( key.length === 1 && !range.collapsed ) {
deleteContentsOfRange( range );
this._ensureBottomLine();
this.setSelection( range );
this._updatePath( range, true );
}
};

File diff suppressed because one or more lines are too long

View file

@ -1647,15 +1647,12 @@ var afterDelete = function ( self, range ) {
};
var keyHandlers = {
enter: function ( self, event ) {
enter: function ( self, event, range ) {
var block, parent, tag, splitTag, nodeAfterSplit;
// We handle this ourselves
event.preventDefault();
// Must have some form of selection
var range = self.getSelection(),
block, parent, tag, splitTag, nodeAfterSplit;
if ( !range ) { return; }
// Save undo checkpoint and add any links in the preceding section.
// Remove any zws so we don't think there's content in an empty
// block.
@ -1800,10 +1797,9 @@ var keyHandlers = {
// We're not still in an undo state
self._docWasChanged();
},
backspace: function ( self, event ) {
backspace: function ( self, event, range ) {
self._removeZWS();
// Record undo checkpoint.
var range = self.getSelection();
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
// If not collapsed, delete contents
@ -1860,10 +1856,9 @@ var keyHandlers = {
setTimeout( function () { afterDelete( self ); }, 0 );
}
},
'delete': function ( self, event ) {
'delete': function ( self, event, range ) {
self._removeZWS();
// Record undo checkpoint.
var range = self.getSelection();
self._recordUndoState( range );
self._getRangeAndRemoveBookmark( range );
// If not collapsed, delete contents
@ -1906,10 +1901,9 @@ var keyHandlers = {
setTimeout( function () { afterDelete( self ); }, 0 );
}
},
tab: function ( self, event ) {
tab: function ( self, event, range ) {
var node, parent;
self._removeZWS();
var range = self.getSelection(),
node, parent;
// If no selection and in an empty block
if ( range.collapsed &&
rangeDoesStartAtBlockBoundary( range ) &&
@ -1932,9 +1926,8 @@ var keyHandlers = {
event.preventDefault();
}
},
space: function ( self ) {
var range = self.getSelection(),
node, parent;
space: function ( self, _, range ) {
var node, parent;
self._recordUndoState( range );
addLinks( range.startContainer );
self._getRangeAndRemoveBookmark( range );
@ -1992,7 +1985,8 @@ keyHandlers[ ctrlKey + 'shift-z' ] = mapKeyTo( 'redo' );
proto._onKey = function ( event ) {
var code = event.keyCode,
key = keys[ code ],
modifiers = '';
modifiers = '',
range = this.getSelection();
if ( !key ) {
key = String.fromCharCode( code ).toLowerCase();
@ -2027,7 +2021,12 @@ proto._onKey = function ( event ) {
key = modifiers + key;
if ( keyHandlers[ key ] ) {
keyHandlers[ key ]( this, event );
keyHandlers[ key ]( this, event, range );
} else if ( key.length === 1 && !range.collapsed ) {
deleteContentsOfRange( range );
this._ensureBottomLine();
this.setSelection( range );
this._updatePath( range, true );
}
};