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._path = '';
this.addEventListener( 'keyup', this._updatePathOnEvent );
this.addEventListener( 'mouseup', this._updatePathOnEvent );
if ( 'onselectionchange' in doc ) {
this.addEventListener( 'selectionchange', this._updatePathOnEvent );
} else {
this.addEventListener( 'keyup', this._updatePathOnEvent );
this.addEventListener( 'mouseup', this._updatePathOnEvent );
}
this._undoIndex = -1;
this._undoStack = [];
@ -2474,18 +2478,15 @@ proto.fireEvent = function ( type, event ) {
};
proto.destroy = function () {
var root = this._root,
events = this._events,
type;
var l = instances.length;
var events = this._events;
var type;
for ( type in events ) {
if ( !customEvents[ type ] ) {
root.removeEventListener( type, this, true );
}
this.removeEventListener( type );
}
if ( this._mutation ) {
this._mutation.disconnect();
}
var l = instances.length;
while ( l-- ) {
if ( instances[l] === this ) {
instances.splice( l, 1 );
@ -2499,6 +2500,7 @@ proto.handleEvent = function ( event ) {
proto.addEventListener = function ( type, fn ) {
var handlers = this._events[ type ];
var target = this._root;
if ( !fn ) {
this.didError({
name: 'Squire: addEventListener with null or undefined fn',
@ -2509,7 +2511,10 @@ proto.addEventListener = function ( type, fn ) {
if ( !handlers ) {
handlers = this._events[ type ] = [];
if ( !customEvents[ type ] ) {
this._root.addEventListener( type, this, true );
if ( type === 'selectionchange' ) {
target = this._doc;
}
target.addEventListener( type, this, true );
}
}
handlers.push( fn );
@ -2517,19 +2522,27 @@ proto.addEventListener = function ( type, fn ) {
};
proto.removeEventListener = function ( type, fn ) {
var handlers = this._events[ type ],
l;
var handlers = this._events[ type ];
var target = this._root;
var l;
if ( handlers ) {
l = handlers.length;
while ( l-- ) {
if ( handlers[l] === fn ) {
handlers.splice( l, 1 );
if ( fn ) {
l = handlers.length;
while ( l-- ) {
if ( handlers[l] === fn ) {
handlers.splice( l, 1 );
}
}
} else {
handlers.length = 0;
}
if ( !handlers.length ) {
delete this._events[ 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 () {
this.getSelection();
this._restoreSelection = true;
}
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._path = '';
this.addEventListener( 'keyup', this._updatePathOnEvent );
this.addEventListener( 'mouseup', this._updatePathOnEvent );
if ( 'onselectionchange' in doc ) {
this.addEventListener( 'selectionchange', this._updatePathOnEvent );
} else {
this.addEventListener( 'keyup', this._updatePathOnEvent );
this.addEventListener( 'mouseup', this._updatePathOnEvent );
}
this._undoIndex = -1;
this._undoStack = [];
@ -255,18 +259,15 @@ proto.fireEvent = function ( type, event ) {
};
proto.destroy = function () {
var root = this._root,
events = this._events,
type;
var l = instances.length;
var events = this._events;
var type;
for ( type in events ) {
if ( !customEvents[ type ] ) {
root.removeEventListener( type, this, true );
}
this.removeEventListener( type );
}
if ( this._mutation ) {
this._mutation.disconnect();
}
var l = instances.length;
while ( l-- ) {
if ( instances[l] === this ) {
instances.splice( l, 1 );
@ -280,6 +281,7 @@ proto.handleEvent = function ( event ) {
proto.addEventListener = function ( type, fn ) {
var handlers = this._events[ type ];
var target = this._root;
if ( !fn ) {
this.didError({
name: 'Squire: addEventListener with null or undefined fn',
@ -290,7 +292,10 @@ proto.addEventListener = function ( type, fn ) {
if ( !handlers ) {
handlers = this._events[ type ] = [];
if ( !customEvents[ type ] ) {
this._root.addEventListener( type, this, true );
if ( type === 'selectionchange' ) {
target = this._doc;
}
target.addEventListener( type, this, true );
}
}
handlers.push( fn );
@ -298,19 +303,27 @@ proto.addEventListener = function ( type, fn ) {
};
proto.removeEventListener = function ( type, fn ) {
var handlers = this._events[ type ],
l;
var handlers = this._events[ type ];
var target = this._root;
var l;
if ( handlers ) {
l = handlers.length;
while ( l-- ) {
if ( handlers[l] === fn ) {
handlers.splice( l, 1 );
if ( fn ) {
l = handlers.length;
while ( l-- ) {
if ( handlers[l] === fn ) {
handlers.splice( l, 1 );
}
}
} else {
handlers.length = 0;
}
if ( !handlers.length ) {
delete this._events[ 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 () {
this.getSelection();
this._restoreSelection = true;
}
function disableRestoreSelection () {