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

Fix determining if at start of block when at end of doc

Fixes #190
This commit is contained in:
Neil Jenkins 2016-05-26 11:39:40 +10:00
parent d225f384b7
commit 7ecf75f246
3 changed files with 35 additions and 17 deletions

View file

@ -1098,7 +1098,7 @@ var getEndBlockOfRange = function ( range, root ) {
block = container; block = container;
} else { } else {
block = getNodeAfter( container, range.endOffset ); block = getNodeAfter( container, range.endOffset );
if ( !block ) { if ( !block || !isOrContains( root, block ) ) {
block = root; block = root;
while ( child = block.lastChild ) { while ( child = block.lastChild ) {
block = child; block = child;
@ -1120,8 +1120,9 @@ var contentWalker = new TreeWalker( null,
); );
var rangeDoesStartAtBlockBoundary = function ( range, root ) { var rangeDoesStartAtBlockBoundary = function ( range, root ) {
var startContainer = range.startContainer, var startContainer = range.startContainer;
startOffset = range.startOffset; var startOffset = range.startOffset;
var nodeAfterCursor;
// If in the middle or end of a text node, we're not at the boundary. // If in the middle or end of a text node, we're not at the boundary.
contentWalker.root = null; contentWalker.root = null;
@ -1129,16 +1130,24 @@ var rangeDoesStartAtBlockBoundary = function ( range, root ) {
if ( startOffset ) { if ( startOffset ) {
return false; return false;
} }
contentWalker.currentNode = startContainer; nodeAfterCursor = startContainer;
} else { } else {
contentWalker.currentNode = getNodeAfter( startContainer, startOffset ); nodeAfterCursor = getNodeAfter( startContainer, startOffset );
if ( nodeAfterCursor && !isOrContains( root, nodeAfterCursor ) ) {
if ( !contentWalker.currentNode ) { nodeAfterCursor = null;
contentWalker.currentNode = startContainer; }
// The cursor was right at the end of the document
if ( !nodeAfterCursor ) {
nodeAfterCursor = getNodeBefore( startContainer, startOffset );
if ( nodeAfterCursor.nodeType === TEXT_NODE &&
nodeAfterCursor.length ) {
return false;
}
} }
} }
// Otherwise, look for any previous content in the same block. // Otherwise, look for any previous content in the same block.
contentWalker.currentNode = nodeAfterCursor;
contentWalker.root = getStartBlockOfRange( range, root ); contentWalker.root = getStartBlockOfRange( range, root );
return !contentWalker.previousNode(); return !contentWalker.previousNode();

File diff suppressed because one or more lines are too long

View file

@ -438,7 +438,7 @@ var getEndBlockOfRange = function ( range, root ) {
block = container; block = container;
} else { } else {
block = getNodeAfter( container, range.endOffset ); block = getNodeAfter( container, range.endOffset );
if ( !block ) { if ( !block || !isOrContains( root, block ) ) {
block = root; block = root;
while ( child = block.lastChild ) { while ( child = block.lastChild ) {
block = child; block = child;
@ -460,8 +460,9 @@ var contentWalker = new TreeWalker( null,
); );
var rangeDoesStartAtBlockBoundary = function ( range, root ) { var rangeDoesStartAtBlockBoundary = function ( range, root ) {
var startContainer = range.startContainer, var startContainer = range.startContainer;
startOffset = range.startOffset; var startOffset = range.startOffset;
var nodeAfterCursor;
// If in the middle or end of a text node, we're not at the boundary. // If in the middle or end of a text node, we're not at the boundary.
contentWalker.root = null; contentWalker.root = null;
@ -469,16 +470,24 @@ var rangeDoesStartAtBlockBoundary = function ( range, root ) {
if ( startOffset ) { if ( startOffset ) {
return false; return false;
} }
contentWalker.currentNode = startContainer; nodeAfterCursor = startContainer;
} else { } else {
contentWalker.currentNode = getNodeAfter( startContainer, startOffset ); nodeAfterCursor = getNodeAfter( startContainer, startOffset );
if ( nodeAfterCursor && !isOrContains( root, nodeAfterCursor ) ) {
if ( !contentWalker.currentNode ) { nodeAfterCursor = null;
contentWalker.currentNode = startContainer; }
// The cursor was right at the end of the document
if ( !nodeAfterCursor ) {
nodeAfterCursor = getNodeBefore( startContainer, startOffset );
if ( nodeAfterCursor.nodeType === TEXT_NODE &&
nodeAfterCursor.length ) {
return false;
}
} }
} }
// Otherwise, look for any previous content in the same block. // Otherwise, look for any previous content in the same block.
contentWalker.currentNode = nodeAfterCursor;
contentWalker.root = getStartBlockOfRange( range, root ); contentWalker.root = getStartBlockOfRange( range, root );
return !contentWalker.previousNode(); return !contentWalker.previousNode();