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

Restore selection on focus, unless explicit click

This commit is contained in:
Neil Jenkins 2016-03-28 09:43:14 +11:00
parent 3ff9a46ea9
commit bac7c5d2cc
3 changed files with 54 additions and 2 deletions

View file

@ -2288,6 +2288,17 @@ function Squire ( root, config ) {
this.addEventListener( 'keyup', this._keyUpDetectChange );
}
// On blur, restore focus except if there is any change to the content, or
// the user taps or clicks to focus a specific point. Can't actually use
// click event because focus happens before click, so use
// mousedown/touchstart
this._restoreSelection = false;
this.addEventListener( 'blur', enableRestoreSelection );
this.addEventListener( 'input', disableRestoreSelection );
this.addEventListener( 'mousedown', disableRestoreSelection );
this.addEventListener( 'touchstart', disableRestoreSelection );
this.addEventListener( 'focus', restoreSelection );
// IE sometimes fires the beforepaste event twice; make sure it is not run
// again before our after paste function is called.
this._awaitingPaste = false;
@ -2555,6 +2566,9 @@ var getWindowSelection = function ( self ) {
proto.setSelection = function ( range ) {
if ( range ) {
// If we're setting selection, that automatically, and synchronously, // triggers a focus event. Don't want a reentrant call to setSelection.
this._restoreSelection = false;
this._lastSelection = range;
// iOS bug: if you don't focus the iframe before setting the
// selection, you can end up in a state where you type but the input
// doesn't get directed into the contenteditable area but is instead
@ -2599,6 +2613,18 @@ proto.getSelection = function () {
return selection;
};
function enableRestoreSelection () {
this._restoreSelection = true;
}
function disableRestoreSelection () {
this._restoreSelection = false;
}
function restoreSelection () {
if ( this._restoreSelection ) {
this.setSelection( this._lastSelection );
}
}
proto.getSelectedText = function () {
var range = this.getSelection(),
walker = new TreeWalker(

File diff suppressed because one or more lines are too long

View file

@ -78,6 +78,17 @@ function Squire ( root, config ) {
this.addEventListener( 'keyup', this._keyUpDetectChange );
}
// On blur, restore focus except if there is any change to the content, or
// the user taps or clicks to focus a specific point. Can't actually use
// click event because focus happens before click, so use
// mousedown/touchstart
this._restoreSelection = false;
this.addEventListener( 'blur', enableRestoreSelection );
this.addEventListener( 'input', disableRestoreSelection );
this.addEventListener( 'mousedown', disableRestoreSelection );
this.addEventListener( 'touchstart', disableRestoreSelection );
this.addEventListener( 'focus', restoreSelection );
// IE sometimes fires the beforepaste event twice; make sure it is not run
// again before our after paste function is called.
this._awaitingPaste = false;
@ -345,6 +356,9 @@ var getWindowSelection = function ( self ) {
proto.setSelection = function ( range ) {
if ( range ) {
// If we're setting selection, that automatically, and synchronously, // triggers a focus event. Don't want a reentrant call to setSelection.
this._restoreSelection = false;
this._lastSelection = range;
// iOS bug: if you don't focus the iframe before setting the
// selection, you can end up in a state where you type but the input
// doesn't get directed into the contenteditable area but is instead
@ -389,6 +403,18 @@ proto.getSelection = function () {
return selection;
};
function enableRestoreSelection () {
this._restoreSelection = true;
}
function disableRestoreSelection () {
this._restoreSelection = false;
}
function restoreSelection () {
if ( this._restoreSelection ) {
this.setSelection( this._lastSelection );
}
}
proto.getSelectedText = function () {
var range = this.getSelection(),
walker = new TreeWalker(