2012-03-07 20:21:32 +00:00
|
|
|
/**
|
|
|
|
* Creates an ve.dm.DocumentSynchronizer object.
|
|
|
|
*
|
|
|
|
* This object is a one-time use utilitiy for collecting actions to be performed on the model tree
|
|
|
|
* in multiple steps and then processing those actions in a single step.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-03-07 23:48:58 +00:00
|
|
|
ve.dm.DocumentSynchronizer = function( model ) {
|
2012-03-07 20:21:32 +00:00
|
|
|
// Properties
|
|
|
|
this.model = model;
|
|
|
|
this.actions = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-03-08 19:35:51 +00:00
|
|
|
ve.dm.DocumentSynchronizer.prototype.getModel = function() {
|
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
2012-03-07 20:21:32 +00:00
|
|
|
/**
|
|
|
|
* Adds an action to the synchronizer.
|
|
|
|
*
|
|
|
|
* @method
|
2012-03-08 00:52:30 +00:00
|
|
|
* @param {String} type Type of action, can be: "insert", "delete", "rebuild", "resize" or "update"
|
2012-03-07 20:21:32 +00:00
|
|
|
* @param {ve.dm.Node} node Node this action is related to
|
|
|
|
* @param {Integer} offset Offset of node, improves performance if this has already been calculated
|
|
|
|
* @param {Integer} adjustment Node length adjustment, if any
|
|
|
|
*/
|
2012-03-08 00:52:30 +00:00
|
|
|
ve.dm.DocumentSynchronizer.prototype.pushAction = function( type, node, offset, adjustment ) {
|
2012-03-07 20:21:32 +00:00
|
|
|
if ( offset === undefined ) {
|
|
|
|
offset = this.model.getOffsetFromNode( node );
|
|
|
|
}
|
|
|
|
this.actions.push( {
|
|
|
|
'type': type,
|
|
|
|
'node': node,
|
|
|
|
'offset': offset,
|
|
|
|
'adjustment': adjustment || 0
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies queued actions to the model tree.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.DocumentSynchronizer.prototype.synchronize = function() {
|
|
|
|
// TODO: Normalize the actions list to clean up nested actions
|
|
|
|
// Perform all actions
|
|
|
|
var adjustment = 0,
|
|
|
|
action,
|
|
|
|
offset,
|
|
|
|
parent;
|
|
|
|
for ( var i = 0, len = this.actions.length; i < len; i++ ) {
|
|
|
|
action = this.actions[i];
|
|
|
|
offset = action.offset + adjustment;
|
|
|
|
switch ( action.type ) {
|
|
|
|
case 'insert':
|
|
|
|
// Insert the new node at the given offset
|
2012-03-08 00:52:30 +00:00
|
|
|
var target = this.model.getNodeFromOffset( offset + 1 );
|
2012-03-07 20:21:32 +00:00
|
|
|
if ( target === this.model ) {
|
|
|
|
// Insert at the beginning of the document
|
2012-03-08 00:52:30 +00:00
|
|
|
this.model.splice( 0, 0, action.node );
|
|
|
|
} else if ( target === null ) {
|
|
|
|
// Insert at the end of the document
|
|
|
|
this.model.splice( this.model.getElementLength(), 0, action.node );
|
2012-03-07 20:21:32 +00:00
|
|
|
} else {
|
|
|
|
// Insert before the element currently at the offset
|
|
|
|
parent = target.getParent();
|
|
|
|
parent.splice( parent.indexOf( target ), 0, action.node );
|
|
|
|
}
|
|
|
|
// Adjust proceeding offsets positively by the length of the node being inserted
|
|
|
|
adjustment += action.node.getElementLength();
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
// Replace original node with new node
|
|
|
|
parent = action.node.getParent();
|
2012-03-08 19:35:51 +00:00
|
|
|
parent.splice( parent.indexOf( action.node ), 1 );
|
2012-03-07 20:21:32 +00:00
|
|
|
// Adjust proceeding offsets negatively by the length of the node being deleted
|
|
|
|
adjustment -= action.node.getElementLength();
|
|
|
|
break;
|
|
|
|
case 'rebuild':
|
|
|
|
// Replace original node with new node
|
2012-03-08 19:35:51 +00:00
|
|
|
var newNodes = ve.dm.DocumentNode.createNodesFromData( this.model.getData(
|
2012-03-07 20:21:32 +00:00
|
|
|
new ve.Range( offset, action.node.getElementLength() + action.adjustment )
|
|
|
|
) );
|
|
|
|
parent = action.node.getParent();
|
2012-03-10 00:31:28 +00:00
|
|
|
ve.batchedSplice( parent, parent.indexOf( action.node ), 1, newNodes );
|
2012-03-07 20:21:32 +00:00
|
|
|
// Adjust proceeding offsets by the difference between the original and new nodes
|
2012-03-08 19:35:51 +00:00
|
|
|
var newNodesLength = 0;
|
|
|
|
for ( var j = 0, jlen = newNodes.length; j < jlen; j++ ) {
|
|
|
|
newNodesLength += newNodes[j].getElementLength();
|
|
|
|
}
|
|
|
|
adjustment += newNodesLength - action.node.getElementLength();
|
2012-03-07 20:21:32 +00:00
|
|
|
break;
|
|
|
|
case 'resize':
|
|
|
|
// Adjust node length - causes update events to be emitted
|
2012-03-08 00:52:30 +00:00
|
|
|
action.node.adjustContentLength( action.adjustment );
|
2012-03-07 20:21:32 +00:00
|
|
|
// Adjust proceeding offsets by the amount the node is being lengthened or shortened
|
|
|
|
adjustment += action.adjustment;
|
|
|
|
break;
|
|
|
|
case 'update':
|
|
|
|
// Emit update events
|
2012-03-08 00:52:30 +00:00
|
|
|
action.node.emit( 'update' );
|
2012-03-07 20:21:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|