2012-10-04 19:54:39 +00:00
|
|
|
/*global rangy */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VisualEditor content editable Surface class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Surface observer.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.EventEmitter}
|
|
|
|
* @param {ve.ce.Document} documentView Document to observe
|
|
|
|
*/
|
|
|
|
ve.ce.SurfaceObserver = function VeCeSurfaceObserver( documentView ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.documentView = documentView;
|
|
|
|
this.polling = false;
|
2012-10-20 00:11:59 +00:00
|
|
|
this.timeoutId = null;
|
|
|
|
this.frequency = 250; //ms
|
2012-10-26 17:27:54 +00:00
|
|
|
|
|
|
|
// Initialization
|
2012-10-04 19:54:39 +00:00
|
|
|
this.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.ce.SurfaceObserver, ve.EventEmitter );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears polling data.
|
|
|
|
*
|
|
|
|
* @method
|
2012-11-21 21:22:29 +00:00
|
|
|
* @param {ve.Range} range Initial range to use
|
2012-10-04 19:54:39 +00:00
|
|
|
*/
|
2012-11-21 21:22:29 +00:00
|
|
|
ve.ce.SurfaceObserver.prototype.clear = function ( range ) {
|
2012-10-04 19:54:39 +00:00
|
|
|
this.rangySelection = {
|
|
|
|
anchorNode: null,
|
|
|
|
anchorOffset: null,
|
|
|
|
focusNode: null,
|
|
|
|
focusOffset: null
|
|
|
|
};
|
2012-11-21 21:22:29 +00:00
|
|
|
this.range = range || null;
|
2012-10-04 19:54:39 +00:00
|
|
|
this.node = null;
|
|
|
|
this.text = null;
|
|
|
|
this.hash = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts polling.
|
|
|
|
*
|
2012-10-26 17:27:54 +00:00
|
|
|
* If {async} is false or undefined the first poll cycle will occur immediately and synchronously.
|
|
|
|
*
|
2012-10-04 19:54:39 +00:00
|
|
|
* @method
|
2012-10-26 17:27:54 +00:00
|
|
|
* @param {Boolean} async Poll the first time asynchronously
|
2012-10-04 19:54:39 +00:00
|
|
|
*/
|
2012-10-22 22:52:19 +00:00
|
|
|
ve.ce.SurfaceObserver.prototype.start = function ( async ) {
|
2012-10-04 19:54:39 +00:00
|
|
|
this.polling = true;
|
|
|
|
this.poll( async );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops polling.
|
|
|
|
*
|
2012-10-26 17:27:54 +00:00
|
|
|
* If {poll} is false or undefined than no final poll cycle will occur and changes can be lost. If
|
|
|
|
* it's true then a final poll cycle will occur immediately and synchronously.
|
|
|
|
*
|
2012-10-04 19:54:39 +00:00
|
|
|
* @method
|
2012-10-26 17:27:54 +00:00
|
|
|
* @param {Boolean} poll Poll one last time before stopping future polling
|
2012-10-04 19:54:39 +00:00
|
|
|
*/
|
2012-10-22 22:52:19 +00:00
|
|
|
ve.ce.SurfaceObserver.prototype.stop = function ( poll ) {
|
2012-10-04 19:54:39 +00:00
|
|
|
if ( this.polling === true ) {
|
|
|
|
if ( poll === true ) {
|
|
|
|
this.poll();
|
|
|
|
}
|
|
|
|
this.polling = false;
|
2012-10-20 00:11:59 +00:00
|
|
|
clearTimeout( this.timeoutId );
|
|
|
|
this.timeoutId = null;
|
2012-10-04 19:54:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-10-26 17:27:54 +00:00
|
|
|
* Poll for changes.
|
|
|
|
*
|
|
|
|
* If {async} is false or undefined then polling will occcur asynchronously.
|
|
|
|
*
|
2012-10-04 19:54:39 +00:00
|
|
|
* TODO: fixing selection in certain cases, handling selection across multiple nodes in Firefox
|
|
|
|
*
|
|
|
|
* @method
|
2012-10-26 17:27:54 +00:00
|
|
|
* @param {Boolean} async Poll asynchronously
|
2012-10-04 19:54:39 +00:00
|
|
|
*/
|
2012-10-22 22:52:19 +00:00
|
|
|
ve.ce.SurfaceObserver.prototype.poll = function ( async ) {
|
2012-12-04 00:34:27 +00:00
|
|
|
var delayPoll, rangySelection, $branch, node, text, hash,
|
|
|
|
range = null;
|
2012-10-04 19:54:39 +00:00
|
|
|
|
2012-10-20 00:11:59 +00:00
|
|
|
if ( this.timeoutId !== null ) {
|
|
|
|
clearTimeout( this.timeoutId );
|
|
|
|
this.timeoutId = null;
|
2012-10-04 19:54:39 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 17:27:54 +00:00
|
|
|
delayPoll = ve.bind( function ( async ) {
|
2012-10-20 00:11:59 +00:00
|
|
|
this.timeoutId = setTimeout(
|
2012-10-04 19:54:39 +00:00
|
|
|
ve.bind( this.poll, this ),
|
|
|
|
async === true ? 0 : this.frequency
|
|
|
|
);
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
if ( async === true ) {
|
|
|
|
delayPoll( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rangySelection = rangy.getSelection();
|
|
|
|
node = this.node;
|
|
|
|
|
|
|
|
if (
|
|
|
|
rangySelection.anchorNode !== this.rangySelection.anchorNode ||
|
|
|
|
rangySelection.anchorOffset !== this.rangySelection.anchorOffset ||
|
|
|
|
rangySelection.focusNode !== this.rangySelection.focusNode ||
|
|
|
|
rangySelection.focusOffset !== this.rangySelection.focusOffset
|
|
|
|
) {
|
|
|
|
this.rangySelection.anchorNode = rangySelection.anchorNode;
|
|
|
|
this.rangySelection.anchorOffset = rangySelection.anchorOffset;
|
|
|
|
this.rangySelection.focusNode = rangySelection.focusNode;
|
|
|
|
this.rangySelection.focusOffset = rangySelection.focusOffset;
|
|
|
|
|
2012-12-04 00:34:27 +00:00
|
|
|
$branch = $( rangySelection.anchorNode ).closest( '.ve-ce-branchNode' );
|
|
|
|
if ( $branch.length ) {
|
|
|
|
node = $branch.data( 'node' );
|
|
|
|
if ( node.canHaveGrandchildren() ) {
|
2012-10-04 19:54:39 +00:00
|
|
|
node = null;
|
|
|
|
} else {
|
2012-12-04 00:34:27 +00:00
|
|
|
range = new ve.Range(
|
|
|
|
ve.ce.getOffset( rangySelection.anchorNode, rangySelection.anchorOffset ),
|
|
|
|
ve.ce.getOffset( rangySelection.focusNode, rangySelection.focusOffset )
|
|
|
|
);
|
2012-10-04 19:54:39 +00:00
|
|
|
}
|
2012-12-04 00:34:27 +00:00
|
|
|
}
|
2012-10-04 19:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.node !== node ) {
|
|
|
|
if ( node === null ) {
|
|
|
|
this.text = null;
|
|
|
|
this.hash = null;
|
|
|
|
this.node = null;
|
|
|
|
} else {
|
|
|
|
this.text = ve.ce.getDomText( node.$[0] );
|
|
|
|
this.hash = ve.ce.getDomHash( node.$[0] );
|
|
|
|
this.node = node;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( node !== null ) {
|
|
|
|
text = ve.ce.getDomText( node.$[0] );
|
|
|
|
hash = ve.ce.getDomHash( node.$[0] );
|
|
|
|
if ( this.text !== text || this.hash !== hash ) {
|
|
|
|
this.emit(
|
|
|
|
'contentChange',
|
|
|
|
node,
|
|
|
|
{ 'text': this.text, 'hash': this.hash, 'range': this.range },
|
|
|
|
{ 'text': text, 'hash': hash, 'range': range }
|
|
|
|
);
|
|
|
|
this.text = text;
|
|
|
|
this.hash = hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 21:22:29 +00:00
|
|
|
// Only emit selectionChange event if there's a meaningful range difference
|
|
|
|
if ( ( this.range && range ) ? !this.range.equals( range ) : ( this.range !== range ) ) {
|
2012-10-04 19:54:39 +00:00
|
|
|
this.emit(
|
|
|
|
'selectionChange',
|
|
|
|
this.range,
|
|
|
|
range
|
|
|
|
);
|
|
|
|
this.range = range;
|
|
|
|
}
|
|
|
|
|
|
|
|
delayPoll();
|
|
|
|
};
|