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

Use selectionchange event if available.

Fixes #194.
This commit is contained in:
Neil Jenkins 2016-05-26 11:07:11 +10:00
parent 4354b46db9
commit d225f384b7
3 changed files with 62 additions and 38 deletions

View file

@ -2275,8 +2275,12 @@ function Squire ( root, config ) {
this._lastFocusNode = null; this._lastFocusNode = null;
this._path = ''; this._path = '';
this.addEventListener( 'keyup', this._updatePathOnEvent ); if ( 'onselectionchange' in doc ) {
this.addEventListener( 'mouseup', this._updatePathOnEvent ); this.addEventListener( 'selectionchange', this._updatePathOnEvent );
} else {
this.addEventListener( 'keyup', this._updatePathOnEvent );
this.addEventListener( 'mouseup', this._updatePathOnEvent );
}
this._undoIndex = -1; this._undoIndex = -1;
this._undoStack = []; this._undoStack = [];
@ -2474,18 +2478,15 @@ proto.fireEvent = function ( type, event ) {
}; };
proto.destroy = function () { proto.destroy = function () {
var root = this._root, var l = instances.length;
events = this._events, var events = this._events;
type; var type;
for ( type in events ) { for ( type in events ) {
if ( !customEvents[ type ] ) { this.removeEventListener( type );
root.removeEventListener( type, this, true );
}
} }
if ( this._mutation ) { if ( this._mutation ) {
this._mutation.disconnect(); this._mutation.disconnect();
} }
var l = instances.length;
while ( l-- ) { while ( l-- ) {
if ( instances[l] === this ) { if ( instances[l] === this ) {
instances.splice( l, 1 ); instances.splice( l, 1 );
@ -2499,6 +2500,7 @@ proto.handleEvent = function ( event ) {
proto.addEventListener = function ( type, fn ) { proto.addEventListener = function ( type, fn ) {
var handlers = this._events[ type ]; var handlers = this._events[ type ];
var target = this._root;
if ( !fn ) { if ( !fn ) {
this.didError({ this.didError({
name: 'Squire: addEventListener with null or undefined fn', name: 'Squire: addEventListener with null or undefined fn',
@ -2509,7 +2511,10 @@ proto.addEventListener = function ( type, fn ) {
if ( !handlers ) { if ( !handlers ) {
handlers = this._events[ type ] = []; handlers = this._events[ type ] = [];
if ( !customEvents[ type ] ) { if ( !customEvents[ type ] ) {
this._root.addEventListener( type, this, true ); if ( type === 'selectionchange' ) {
target = this._doc;
}
target.addEventListener( type, this, true );
} }
} }
handlers.push( fn ); handlers.push( fn );
@ -2517,19 +2522,27 @@ proto.addEventListener = function ( type, fn ) {
}; };
proto.removeEventListener = function ( type, fn ) { proto.removeEventListener = function ( type, fn ) {
var handlers = this._events[ type ], var handlers = this._events[ type ];
l; var target = this._root;
var l;
if ( handlers ) { if ( handlers ) {
l = handlers.length; if ( fn ) {
while ( l-- ) { l = handlers.length;
if ( handlers[l] === fn ) { while ( l-- ) {
handlers.splice( l, 1 ); if ( handlers[l] === fn ) {
handlers.splice( l, 1 );
}
} }
} else {
handlers.length = 0;
} }
if ( !handlers.length ) { if ( !handlers.length ) {
delete this._events[ type ]; delete this._events[ type ];
if ( !customEvents[ type ] ) { if ( !customEvents[ type ] ) {
this._root.removeEventListener( type, this, true ); if ( type === 'selectionchange' ) {
target = this._doc;
}
target.removeEventListener( type, this, true );
} }
} }
} }
@ -2647,7 +2660,6 @@ proto.getSelection = function () {
}; };
function enableRestoreSelection () { function enableRestoreSelection () {
this.getSelection();
this._restoreSelection = true; this._restoreSelection = true;
} }
function disableRestoreSelection () { function disableRestoreSelection () {

File diff suppressed because one or more lines are too long

View file

@ -56,8 +56,12 @@ function Squire ( root, config ) {
this._lastFocusNode = null; this._lastFocusNode = null;
this._path = ''; this._path = '';
this.addEventListener( 'keyup', this._updatePathOnEvent ); if ( 'onselectionchange' in doc ) {
this.addEventListener( 'mouseup', this._updatePathOnEvent ); this.addEventListener( 'selectionchange', this._updatePathOnEvent );
} else {
this.addEventListener( 'keyup', this._updatePathOnEvent );
this.addEventListener( 'mouseup', this._updatePathOnEvent );
}
this._undoIndex = -1; this._undoIndex = -1;
this._undoStack = []; this._undoStack = [];
@ -255,18 +259,15 @@ proto.fireEvent = function ( type, event ) {
}; };
proto.destroy = function () { proto.destroy = function () {
var root = this._root, var l = instances.length;
events = this._events, var events = this._events;
type; var type;
for ( type in events ) { for ( type in events ) {
if ( !customEvents[ type ] ) { this.removeEventListener( type );
root.removeEventListener( type, this, true );
}
} }
if ( this._mutation ) { if ( this._mutation ) {
this._mutation.disconnect(); this._mutation.disconnect();
} }
var l = instances.length;
while ( l-- ) { while ( l-- ) {
if ( instances[l] === this ) { if ( instances[l] === this ) {
instances.splice( l, 1 ); instances.splice( l, 1 );
@ -280,6 +281,7 @@ proto.handleEvent = function ( event ) {
proto.addEventListener = function ( type, fn ) { proto.addEventListener = function ( type, fn ) {
var handlers = this._events[ type ]; var handlers = this._events[ type ];
var target = this._root;
if ( !fn ) { if ( !fn ) {
this.didError({ this.didError({
name: 'Squire: addEventListener with null or undefined fn', name: 'Squire: addEventListener with null or undefined fn',
@ -290,7 +292,10 @@ proto.addEventListener = function ( type, fn ) {
if ( !handlers ) { if ( !handlers ) {
handlers = this._events[ type ] = []; handlers = this._events[ type ] = [];
if ( !customEvents[ type ] ) { if ( !customEvents[ type ] ) {
this._root.addEventListener( type, this, true ); if ( type === 'selectionchange' ) {
target = this._doc;
}
target.addEventListener( type, this, true );
} }
} }
handlers.push( fn ); handlers.push( fn );
@ -298,19 +303,27 @@ proto.addEventListener = function ( type, fn ) {
}; };
proto.removeEventListener = function ( type, fn ) { proto.removeEventListener = function ( type, fn ) {
var handlers = this._events[ type ], var handlers = this._events[ type ];
l; var target = this._root;
var l;
if ( handlers ) { if ( handlers ) {
l = handlers.length; if ( fn ) {
while ( l-- ) { l = handlers.length;
if ( handlers[l] === fn ) { while ( l-- ) {
handlers.splice( l, 1 ); if ( handlers[l] === fn ) {
handlers.splice( l, 1 );
}
} }
} else {
handlers.length = 0;
} }
if ( !handlers.length ) { if ( !handlers.length ) {
delete this._events[ type ]; delete this._events[ type ];
if ( !customEvents[ type ] ) { if ( !customEvents[ type ] ) {
this._root.removeEventListener( type, this, true ); if ( type === 'selectionchange' ) {
target = this._doc;
}
target.removeEventListener( type, this, true );
} }
} }
} }
@ -428,7 +441,6 @@ proto.getSelection = function () {
}; };
function enableRestoreSelection () { function enableRestoreSelection () {
this.getSelection();
this._restoreSelection = true; this._restoreSelection = true;
} }
function disableRestoreSelection () { function disableRestoreSelection () {