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

Scroll cursor into view after setting selection.

Fixes #162 and fixes #165.
This commit is contained in:
Neil Jenkins 2015-12-09 15:12:47 +11:00
parent df25d6d596
commit 9ccf765ba8
4 changed files with 41 additions and 21 deletions

View file

@ -1390,16 +1390,6 @@ var keyHandlers = {
range = self._createRange( nodeAfterSplit, 0 ); range = self._createRange( nodeAfterSplit, 0 );
self.setSelection( range ); self.setSelection( range );
self._updatePath( range, true ); self._updatePath( range, true );
// Scroll into view
if ( nodeAfterSplit.nodeType === TEXT_NODE ) {
nodeAfterSplit = nodeAfterSplit.parentNode;
}
// 16 ~ one standard line height in px.
if ( nodeAfterSplit.getBoundingClientRect().top + 16 >
self._doc.documentElement.clientHeight ) {
nodeAfterSplit.scrollIntoView( false );
}
}, },
backspace: function ( self, event, range ) { backspace: function ( self, event, range ) {
self._removeZWS(); self._removeZWS();
@ -2433,6 +2423,25 @@ proto._createRange =
return domRange; return domRange;
}; };
proto.scrollRangeIntoView = function ( range ) {
var win = this._win;
var top = range.getBoundingClientRect().top;
var height = win.innerHeight;
var node, parent;
if ( !top ) {
node = this._doc.createElement( 'SPAN' );
range = range.cloneRange();
insertNodeInRange( range, node );
top = node.getBoundingClientRect().top;
parent = node.parentNode;
parent.removeChild( node );
parent.normalize();
}
if ( top > height ) {
win.scrollBy( 0, top - height + 20 );
}
};
proto._moveCursorTo = function ( toStart ) { proto._moveCursorTo = function ( toStart ) {
var body = this._body, var body = this._body,
range = this._createRange( body, toStart ? 0 : body.childNodes.length ); range = this._createRange( body, toStart ? 0 : body.childNodes.length );
@ -2460,6 +2469,7 @@ proto.setSelection = function ( range ) {
if ( sel ) { if ( sel ) {
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange( range ); sel.addRange( range );
this.scrollRangeIntoView( range );
} }
} }
return this; return this;

File diff suppressed because one or more lines are too long

View file

@ -301,6 +301,25 @@ proto._createRange =
return domRange; return domRange;
}; };
proto.scrollRangeIntoView = function ( range ) {
var win = this._win;
var top = range.getBoundingClientRect().top;
var height = win.innerHeight;
var node, parent;
if ( !top ) {
node = this._doc.createElement( 'SPAN' );
range = range.cloneRange();
insertNodeInRange( range, node );
top = node.getBoundingClientRect().top;
parent = node.parentNode;
parent.removeChild( node );
parent.normalize();
}
if ( top > height ) {
win.scrollBy( 0, top - height + 20 );
}
};
proto._moveCursorTo = function ( toStart ) { proto._moveCursorTo = function ( toStart ) {
var body = this._body, var body = this._body,
range = this._createRange( body, toStart ? 0 : body.childNodes.length ); range = this._createRange( body, toStart ? 0 : body.childNodes.length );
@ -328,6 +347,7 @@ proto.setSelection = function ( range ) {
if ( sel ) { if ( sel ) {
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange( range ); sel.addRange( range );
this.scrollRangeIntoView( range );
} }
} }
return this; return this;

View file

@ -241,16 +241,6 @@ var keyHandlers = {
range = self._createRange( nodeAfterSplit, 0 ); range = self._createRange( nodeAfterSplit, 0 );
self.setSelection( range ); self.setSelection( range );
self._updatePath( range, true ); self._updatePath( range, true );
// Scroll into view
if ( nodeAfterSplit.nodeType === TEXT_NODE ) {
nodeAfterSplit = nodeAfterSplit.parentNode;
}
// 16 ~ one standard line height in px.
if ( nodeAfterSplit.getBoundingClientRect().top + 16 >
self._doc.documentElement.clientHeight ) {
nodeAfterSplit.scrollIntoView( false );
}
}, },
backspace: function ( self, event, range ) { backspace: function ( self, event, range ) {
self._removeZWS(); self._removeZWS();