2012-05-04 22:17:55 +00:00
|
|
|
/**
|
|
|
|
* Creates an ve.dm.DocumentSynchronizer object.
|
|
|
|
*
|
|
|
|
* This object is a utility for collecting actions to be performed on the model tree
|
|
|
|
* in multiple steps and then processing those actions in a single step.
|
|
|
|
*
|
|
|
|
* NOTE: An 'update' event is emitted for every node that is touched by the synchronizer in any
|
|
|
|
* way, but only once for every node. Other events are emitted as we go.
|
|
|
|
*
|
|
|
|
* IMPORTANT NOTE: It is assumed that:
|
|
|
|
* The linear model has already been updated for the pushed actions
|
|
|
|
* Actions are pushed in increasing offset order
|
|
|
|
* Actions are non-overlapping
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-05-08 02:43:34 +00:00
|
|
|
* @param {ve.dm.Document} doc Document to synchronize
|
2012-05-04 22:17:55 +00:00
|
|
|
*/
|
|
|
|
ve.dm.DocumentSynchronizer = function( doc ) {
|
|
|
|
// Properties
|
|
|
|
this.document = doc;
|
|
|
|
this.actions = [];
|
2012-05-04 22:42:23 +00:00
|
|
|
this.eventQueue = [];
|
2012-05-04 22:17:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.getDocument = function() {
|
|
|
|
return this.document;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an annotation action to the queue. This finds all leaf nodes covered wholly or partially
|
|
|
|
* by the given range, and emits annotation events for all of them.
|
|
|
|
* @param {ve.Range} range Range that was annotated
|
|
|
|
*/
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.pushAnnotation = function( range ) {
|
2012-05-04 23:13:54 +00:00
|
|
|
this.actions.push( {
|
2012-05-04 22:17:55 +00:00
|
|
|
'type': 'annotation',
|
|
|
|
'range': range
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an attribute change to the queue. This emits an attributeChange event for the given node
|
|
|
|
* with the provided metadata.
|
|
|
|
* @param {ve.dm.Node} node Node whose attribute changed
|
|
|
|
* @param {String} key Key of the attribute that changed
|
|
|
|
* @param from Old value of the attribute
|
|
|
|
* @param to New value of the attribute
|
|
|
|
*/
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.pushAttributeChange = function( node, key, from, to ) {
|
2012-05-04 23:13:54 +00:00
|
|
|
this.actions.push( {
|
2012-05-04 22:17:55 +00:00
|
|
|
'type': 'attributeChange',
|
|
|
|
'node': node,
|
|
|
|
'key': key,
|
|
|
|
'from': from,
|
|
|
|
'to': to
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a resize action to the queue. This changes the length of a text node.
|
|
|
|
* @param {ve.dm.TextNode} node Node to resize
|
|
|
|
* @param {Integer} adjustment Length adjustment to apply to the node
|
|
|
|
*/
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.pushResize = function( node, adjustment ) {
|
|
|
|
this.actions.push( {
|
|
|
|
'type': 'resize',
|
|
|
|
'node': node,
|
|
|
|
'adjustment': adjustment
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2012-05-09 00:35:45 +00:00
|
|
|
ve.dm.DocumentSynchronizer.prototype.pushRebuild = function( oldRange, newRange ) {
|
|
|
|
this.actions.push( {
|
|
|
|
'type': 'rebuild',
|
|
|
|
'oldRange': oldRange,
|
|
|
|
'newRange': newRange
|
|
|
|
} );
|
|
|
|
};
|
2012-05-04 22:17:55 +00:00
|
|
|
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.annotation = function( action ) {
|
2012-05-07 20:57:17 +00:00
|
|
|
// Queue events for all leaf nodes covered by the range
|
|
|
|
// TODO test me
|
|
|
|
var i, selection = this.document.selectNodes( action.range, 'leaves' );
|
|
|
|
for ( i = 0; i < selection.length; i++ ) {
|
|
|
|
this.queueEvent( selection[i].node, 'annotation' );
|
2012-05-09 00:34:04 +00:00
|
|
|
this.queueEvent( selection[i].node, 'update' );
|
2012-05-07 20:57:17 +00:00
|
|
|
}
|
2012-05-04 22:17:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.attributeChange = function( action ) {
|
2012-05-04 22:42:23 +00:00
|
|
|
this.queueEvent( action.node, 'attributeChange', action.key, action.from, action.to );
|
|
|
|
this.queueEvent( action.node, 'update' );
|
2012-05-04 22:17:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.resize = function( action ) {
|
|
|
|
action.node.adjustLength( action.adjustment );
|
2012-05-04 22:42:23 +00:00
|
|
|
this.queueEvent( action.node, 'update' );
|
2012-05-04 22:17:55 +00:00
|
|
|
};
|
|
|
|
|
2012-05-09 00:35:45 +00:00
|
|
|
ve.dm.DocumentSynchronizer.prototype.rebuild = function( action ) {
|
|
|
|
// Find the nodes contained by oldRange
|
|
|
|
var selection = this.document.selectNodes( action.oldRange, 'siblings' );
|
2012-05-09 22:27:18 +00:00
|
|
|
if ( selection.length === 0 ) {
|
2012-05-09 00:35:45 +00:00
|
|
|
// WTF? Nothing to rebuild, I guess. Whatever.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO index of firstNode in parent should be in the selectNodes result
|
|
|
|
var firstNode = selection[0].node,
|
|
|
|
parent = firstNode.getParent(),
|
|
|
|
index = parent.indexOf( firstNode );
|
|
|
|
|
|
|
|
this.document.rebuildNodes( parent, index, selection.length, action.oldRange.from,
|
|
|
|
action.newRange.getLength()
|
|
|
|
);
|
|
|
|
};
|
2012-05-04 22:17:55 +00:00
|
|
|
|
2012-05-04 22:42:23 +00:00
|
|
|
ve.dm.DocumentSynchronizer.prototype.queueEvent = function( node, event ) {
|
|
|
|
// Check if this is already queued
|
|
|
|
var args = Array.prototype.slice.call( arguments, 1 );
|
|
|
|
var hash = $.toJSON( args );
|
2012-05-09 22:27:18 +00:00
|
|
|
if ( !node.syncEventQueue ) {
|
|
|
|
node.syncEventQueue = {};
|
2012-05-04 22:17:55 +00:00
|
|
|
}
|
2012-05-09 22:27:18 +00:00
|
|
|
if ( !node.syncEventQueue[hash] ) {
|
|
|
|
node.syncEventQueue[hash] = true;
|
|
|
|
this.eventQueue.push( { 'node': node, 'args': args } );
|
2012-05-04 22:42:23 +00:00
|
|
|
}
|
2012-05-04 22:17:55 +00:00
|
|
|
};
|
|
|
|
|
2012-05-04 22:42:23 +00:00
|
|
|
ve.dm.DocumentSynchronizer.prototype.emitEvents = function() {
|
|
|
|
var i, event;
|
|
|
|
for ( i = 0; i < this.eventQueue.length; i++ ) {
|
|
|
|
event = this.eventQueue[i];
|
|
|
|
event.node.emit.apply( event.node, event.args );
|
2012-05-09 22:27:18 +00:00
|
|
|
delete event.node.syncEventQueue;
|
2012-05-04 22:17:55 +00:00
|
|
|
}
|
2012-05-04 22:42:23 +00:00
|
|
|
this.eventQueue = [];
|
2012-05-04 22:17:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.synchronize = function() {
|
|
|
|
var action, i;
|
|
|
|
// Execute the actions in the queue
|
|
|
|
for ( i = 0; i < this.actions.length; i++ ) {
|
|
|
|
action = this.actions[i];
|
|
|
|
if ( action.type in this ) {
|
|
|
|
this[action.type]( action );
|
|
|
|
} else {
|
|
|
|
throw 'Invalid action type ' + action.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 22:42:23 +00:00
|
|
|
this.emitEvents();
|
2012-05-04 22:17:55 +00:00
|
|
|
this.actions = [];
|
|
|
|
};
|