0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 23:40:35 -05:00

Fix deletion of range when start or end is at edge of block.

If the selection starts and ends in the middle of blocks, we need to merge
these after extracting the range. But we need to be careful not to merge if
the selection ends at a block, as we end up merging into a block that wasn't
selected.
This commit is contained in:
Neil Jenkins 2015-07-25 18:03:51 -07:00
parent 715166b95d
commit c190fab9fd
3 changed files with 32 additions and 8 deletions

View file

@ -766,8 +766,16 @@ var extractContentsOfRange = function ( range, common ) {
var deleteContentsOfRange = function ( range ) { var deleteContentsOfRange = function ( range ) {
// Move boundaries up as much as possible to reduce need to split. // Move boundaries up as much as possible to reduce need to split.
// But we need to check whether we've moved the boundary outside of a
// block. If so, the entire block will be removed, so we shouldn't merge
// later.
moveRangeBoundariesUpTree( range ); moveRangeBoundariesUpTree( range );
var startBlock = range.startContainer,
endBlock = range.endContainer,
needsMerge = ( isInline( startBlock ) || isBlock( startBlock ) ) &&
( isInline( endBlock ) || isBlock( endBlock ) );
// Remove selected range // Remove selected range
extractContentsOfRange( range ); extractContentsOfRange( range );
@ -777,10 +785,12 @@ var deleteContentsOfRange = function ( range ) {
moveRangeBoundariesDownTree( range ); moveRangeBoundariesDownTree( range );
// If we split into two different blocks, merge the blocks. // If we split into two different blocks, merge the blocks.
var startBlock = getStartBlockOfRange( range ), if ( needsMerge ) {
startBlock = getStartBlockOfRange( range );
endBlock = getEndBlockOfRange( range ); endBlock = getEndBlockOfRange( range );
if ( startBlock && endBlock && startBlock !== endBlock ) { if ( startBlock && endBlock && startBlock !== endBlock ) {
mergeWithBlock( startBlock, endBlock, range ); mergeWithBlock( startBlock, endBlock, range );
}
} }
// Ensure block has necessary children // Ensure block has necessary children
@ -794,6 +804,8 @@ var deleteContentsOfRange = function ( range ) {
if ( !child || child.nodeName === 'BR' ) { if ( !child || child.nodeName === 'BR' ) {
fixCursor( body ); fixCursor( body );
range.selectNodeContents( body.firstChild ); range.selectNodeContents( body.firstChild );
} else {
range.collapse( false );
} }
}; };

File diff suppressed because one or more lines are too long

View file

@ -134,8 +134,16 @@ var extractContentsOfRange = function ( range, common ) {
var deleteContentsOfRange = function ( range ) { var deleteContentsOfRange = function ( range ) {
// Move boundaries up as much as possible to reduce need to split. // Move boundaries up as much as possible to reduce need to split.
// But we need to check whether we've moved the boundary outside of a
// block. If so, the entire block will be removed, so we shouldn't merge
// later.
moveRangeBoundariesUpTree( range ); moveRangeBoundariesUpTree( range );
var startBlock = range.startContainer,
endBlock = range.endContainer,
needsMerge = ( isInline( startBlock ) || isBlock( startBlock ) ) &&
( isInline( endBlock ) || isBlock( endBlock ) );
// Remove selected range // Remove selected range
extractContentsOfRange( range ); extractContentsOfRange( range );
@ -145,10 +153,12 @@ var deleteContentsOfRange = function ( range ) {
moveRangeBoundariesDownTree( range ); moveRangeBoundariesDownTree( range );
// If we split into two different blocks, merge the blocks. // If we split into two different blocks, merge the blocks.
var startBlock = getStartBlockOfRange( range ), if ( needsMerge ) {
startBlock = getStartBlockOfRange( range );
endBlock = getEndBlockOfRange( range ); endBlock = getEndBlockOfRange( range );
if ( startBlock && endBlock && startBlock !== endBlock ) { if ( startBlock && endBlock && startBlock !== endBlock ) {
mergeWithBlock( startBlock, endBlock, range ); mergeWithBlock( startBlock, endBlock, range );
}
} }
// Ensure block has necessary children // Ensure block has necessary children
@ -162,6 +172,8 @@ var deleteContentsOfRange = function ( range ) {
if ( !child || child.nodeName === 'BR' ) { if ( !child || child.nodeName === 'BR' ) {
fixCursor( body ); fixCursor( body );
range.selectNodeContents( body.firstChild ); range.selectNodeContents( body.firstChild );
} else {
range.collapse( false );
} }
}; };