mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-16 02:51:50 +00:00
92f6688162
Objective: Generalize the shield and phantom magic, so we can use it for pretty much any node we like. Usually this will be used with generated content nodes, but also with aliens (of course) and possible other stuff in the future. Bonus: Also fixes a bug in DM that would crash VE when you selected to the end and hit backspace. Changes: *.php * Added links to files aliens.html * Added attributes to aliens to make them aliens again ve.ce.AlienNode.js * Moved shield and phantom functionality to ve.ce.ProtectedNode ve.ce.AlienNode.js, ve.ce.MWReferenceListNode.js, ve.ce.MWReferenceNode.js, ve.ce.MWTemplateNode.js * Mixed in ve.ce.ProtectedNode ve.ce.Node.css * Reorganized styles and updated class names * Added simple light blue hover with outline (using inset box shadow) for protected nodes, same style as before for aliens ve.ce.Surface.css * Moved phantom styles to ve.ce.Node.css ve.ce.BranchNode.js * Moved call to setLive(false) to happen before detach() so that the surface object is still available and events can be disconnected ve.ce.BranchNode.js, ve.ce.Document.js, ve.ce.js, ve.ce.Surface.js, ve.ce.SurfaceObserver.js * Adjusted CSS class names ve.ce.Node.js * Moved shield template to ve.ce.ProtectedNode ve.ce.ProtectedNode.js * New class, mix into another class to protect it from editing ve.ce.RelocatableNode.js * Renamed temporary surface property to relocatingSurface to avoid confusion when debugging ve.ce.Surface.js * Moved phantom template to ve.ce.ProtectedNode ve.dm.Transaction.js * Fixed bug where most of the internal list was being deleted when the end of the document was selected and the user pressed backspace Change-Id: I2468b16e1ba6785ad298e38190e33493135719c3
208 lines
4.9 KiB
JavaScript
208 lines
4.9 KiB
JavaScript
/*global rangy */
|
|
|
|
/*!
|
|
* VisualEditor ContentEditable Surface class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* ContentEditable surface observer.
|
|
*
|
|
* @class
|
|
* @mixins ve.EventEmitter
|
|
*
|
|
* @constructor
|
|
* @param {ve.ce.Document} documentView Document to observe
|
|
*/
|
|
ve.ce.SurfaceObserver = function VeCeSurfaceObserver( documentView ) {
|
|
// Mixin constructors
|
|
ve.EventEmitter.call( this );
|
|
|
|
// Properties
|
|
this.documentView = documentView;
|
|
this.polling = false;
|
|
this.timeoutId = null;
|
|
this.frequency = 250; //ms
|
|
|
|
// Initialization
|
|
this.clear();
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.mixinClass( ve.ce.SurfaceObserver, ve.EventEmitter );
|
|
|
|
/* Events */
|
|
|
|
/**
|
|
* When #poll sees a change this event is emitted (before the
|
|
* properties are updated).
|
|
*
|
|
* @event contentChange
|
|
* @param {HTMLElement} node DOM node the change occured in
|
|
* @param {Object} previous Old data
|
|
* @param {Object} previous.text Old plain text content
|
|
* @param {Object} previous.hash Old DOM hash
|
|
* @param {ve.Range} previous.range Old selection
|
|
* @param {Object} next New data
|
|
* @param {Object} next.text New plain text content
|
|
* @param {Object} next.hash New DOM hash
|
|
* @param {ve.Range} next.range New selection
|
|
*/
|
|
|
|
/**
|
|
* When #poll observes a change in the document and the new
|
|
* selection does not equal as the last known selection, this event
|
|
* is emitted (before the properties are updated).
|
|
*
|
|
* @event selectionChange
|
|
* @param {ve.Range} oldRange
|
|
* @param {ve.Range} newRange
|
|
*/
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Clear polling data.
|
|
*
|
|
* @method
|
|
* @param {ve.Range} range Initial range to use
|
|
*/
|
|
ve.ce.SurfaceObserver.prototype.clear = function ( range ) {
|
|
this.rangyRange = null;
|
|
this.range = range || null;
|
|
this.node = null;
|
|
this.text = null;
|
|
this.hash = null;
|
|
};
|
|
|
|
/**
|
|
* Start polling.
|
|
*
|
|
* If {async} is false or undefined the first poll cycle will occur immediately and synchronously.
|
|
*
|
|
* @method
|
|
* @param {boolean} async Poll the first time asynchronously
|
|
*/
|
|
ve.ce.SurfaceObserver.prototype.start = function ( async ) {
|
|
this.polling = true;
|
|
this.poll( async );
|
|
};
|
|
|
|
/**
|
|
* Stop polling.
|
|
*
|
|
* 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.
|
|
*
|
|
* @method
|
|
* @param {boolean} poll Poll one last time before stopping future polling
|
|
*/
|
|
ve.ce.SurfaceObserver.prototype.stop = function ( poll ) {
|
|
if ( this.polling === true ) {
|
|
if ( poll === true ) {
|
|
this.poll();
|
|
}
|
|
this.polling = false;
|
|
clearTimeout( this.timeoutId );
|
|
this.timeoutId = null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Poll for changes.
|
|
*
|
|
* If `async` is false or undefined then polling will occcur asynchronously.
|
|
*
|
|
* TODO: fixing selection in certain cases, handling selection across multiple nodes in Firefox
|
|
*
|
|
* FIXME: Does not work well (selectionChange is not emited) when cursor is placed inside a slug
|
|
* with a mouse.
|
|
*
|
|
* @method
|
|
* @param {boolean} async Poll asynchronously
|
|
* @emits contentChange
|
|
* @emits selectionChange
|
|
*/
|
|
ve.ce.SurfaceObserver.prototype.poll = function ( async ) {
|
|
var delayPoll, $nodeOrSlug, node, text, hash, range, rangyRange;
|
|
|
|
if ( this.polling === false ) {
|
|
return;
|
|
}
|
|
|
|
if ( this.timeoutId !== null ) {
|
|
clearTimeout( this.timeoutId );
|
|
this.timeoutId = null;
|
|
}
|
|
|
|
delayPoll = ve.bind( function ( async ) {
|
|
this.timeoutId = setTimeout(
|
|
ve.bind( this.poll, this ),
|
|
async === true ? 0 : this.frequency
|
|
);
|
|
}, this );
|
|
|
|
if ( async === true ) {
|
|
delayPoll( true );
|
|
return;
|
|
}
|
|
|
|
range = this.range;
|
|
node = this.node;
|
|
rangyRange = ve.ce.DomRange.newFromDomSelection( rangy.getSelection() );
|
|
|
|
if ( !rangyRange.equals( this.rangyRange ) ){
|
|
this.rangyRange = rangyRange;
|
|
node = null;
|
|
$nodeOrSlug = $( rangyRange.anchorNode ).closest( '.ve-ce-branchNode, .ve-ce-branchNode-slug' );
|
|
if ( $nodeOrSlug.length ) {
|
|
range = rangyRange.getRange();
|
|
if ( !$nodeOrSlug.hasClass( 've-ce-branchNode-slug' ) ) {
|
|
node = $nodeOrSlug.data( 'view' );
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Only emit selectionChange event if there's a meaningful range difference
|
|
if ( ( this.range && range ) ? !this.range.equals( range ) : ( this.range !== range ) ) {
|
|
this.emit(
|
|
'selectionChange',
|
|
this.range,
|
|
range
|
|
);
|
|
this.range = range;
|
|
}
|
|
|
|
delayPoll();
|
|
};
|