https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113850
Revision: 113850
Author: catrope
Date: 2012-03-14 21:02:29 +0000 (Wed, 14 Mar 2012)
Log Message:
-----------
Break out pushAction() into separate functions for each action. This will allow
me to change the rebuild action to take totally different parameters.
Modified Paths:
--------------
trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.DocumentSynchronizer.js
trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js
Modified:
trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.DocumentSynchronizer.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.DocumentSynchronizer.js
2012-03-14 21:02:27 UTC (rev 113849)
+++ trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.DocumentSynchronizer.js
2012-03-14 21:02:29 UTC (rev 113850)
@@ -20,26 +20,72 @@
};
/**
- * Adds an action to the synchronizer.
- *
- * @method
- * @param {String} type Type of action, can be: "insert", "delete", "rebuild",
"resize" or "update"
- * @param {ve.dm.Node} node Node this action is related to
- * @param {Integer|null} offset Offset of node, improves performance if this
has already been calculated.
- * Only used for insert and rebuild actions
- * @param {Integer} adjustment Node length adjustment, if any
+ * Add an insert action to the queue
+ * @param {ve.dm.BranchNode} node Node to insert
+ * @param {Integer} [offset] Offset of the inserted node, if known
*/
-ve.dm.DocumentSynchronizer.prototype.pushAction = function( type, node,
offset, adjustment ) {
+ve.dm.DocumentSynchronizer.prototype.pushInsert = function( node, offset ) {
this.actions.push( {
- 'type': type,
+ 'type': 'insert',
'node': node,
- 'offset': offset,
- 'adjustment': adjustment || 0
+ 'offset': offset || null
} );
};
/**
- * Applies queued actions to the model tree.
+ * Add a delete action to the queue
+ * @param {ve.dm.BranchNode} node Node to delete
+ */
+ve.dm.DocumentSynchronizer.prototype.pushDelete = function( node ) {
+ this.actions.push( {
+ 'type': 'delete',
+ 'node': node
+ } );
+};
+
+/**
+ * Add a rebuild action to the queue. This rebuilds a node from data
+ * found in the linear model.
+ * @param {ve.dm.BranchNode} node Node to rebuild
+ * @param {Integer} adjustment Length adjustment to apply to the node
+ * @param {Integer} offset Offset of the node, if known
+ */
+ve.dm.DocumentSynchronizer.prototype.pushRebuild = function( node, adjustment,
offset ) {
+ this.actions.push( {
+ 'type': 'rebuild',
+ 'node': node,
+ 'adjustment': adjustment,
+ 'offset': offset || null
+ } );
+};
+
+/**
+ * Add a resize action to the queue. This changes the content length of a leaf
node.
+ * @param {ve.dm.BranchNode} 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
+ } );
+};
+
+/**
+ * Add an update action to the queue
+ * @param {ve.dm.BranchNode} node Node to update
+ */
+ve.dm.DocumentSynchronizer.prototype.pushUpdate = function( node ) {
+ this.actions.push( {
+ 'type': 'update',
+ 'node': node
+ } );
+};
+
+/**
+ * Apply queued actions to the model tree. This assumes that the linear model
+ * has already been updated, but the model tree has not yet been.
*
* @method
*/
@@ -51,7 +97,7 @@
parent;
for ( var i = 0, len = this.actions.length; i < len; i++ ) {
action = this.actions[i];
- offset = action.offset;
+ offset = action.offset || null;
switch ( action.type ) {
case 'insert':
// Compute the offset if it wasn't provided
Modified:
trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js
===================================================================
--- trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js
2012-03-14 21:02:27 UTC (rev 113849)
+++ trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js
2012-03-14 21:02:29 UTC (rev 113850)
@@ -9,7 +9,7 @@
// Delete bold "b" from first paragraph
model.data.splice( 2, 1 );
// Push resize action
- sync.pushAction( 'resize',
model.getChildren()[0], 0, -1 );
+ sync.pushResize( model.getChildren()[0], -1 );
// Sync
sync.synchronize();
return
model.getChildren()[0].getContentLength();
@@ -25,7 +25,7 @@
// Insert element after first paragraph
ve.insertIntoArray( model.data, 5, data );
// Push insertion action
- sync.pushAction( 'insert', node, 5 );
+ sync.pushInsert( node, 5 );
// Sync
sync.synchronize();
return model.getChildren()[1].getContentData();
@@ -41,7 +41,7 @@
// Insert element after first paragraph
ve.insertIntoArray( model.data, 0, data );
// Push insertion action
- sync.pushAction( 'insert', node, 0 );
+ sync.pushInsert( node, 0 );
// Sync
sync.synchronize();
return model.getChildren()[0].getContentData();
@@ -57,7 +57,7 @@
// Insert element after first paragraph
ve.insertIntoArray( model.data, 34, data );
// Push insertion action
- sync.pushAction( 'insert', node, 34 );
+ sync.pushInsert( node, 34 );
// Sync
sync.synchronize();
return model.getChildren()[3].getContentData();
@@ -72,7 +72,7 @@
// Delete the table
model.data.splice( 5, 26 );
// Push deletion action
- sync.pushAction( 'delete', node, 5 );
+ sync.pushDelete( node );
// Sync
sync.synchronize();
return model.getChildren().length;
@@ -87,7 +87,7 @@
// Delete the first paragraph
model.data.splice( 0, 5 );
// Push deletion action
- sync.pushAction( 'delete', node, 0 );
+ sync.pushDelete( node );
// Sync
sync.synchronize();
return model.getChildren().length;
@@ -102,7 +102,7 @@
// Delete the first paragraph
model.data.splice( 31, 3 );
// Push deletion action
- sync.pushAction( 'delete', node, 31 );
+ sync.pushDelete( node );
// Sync
sync.synchronize();
return model.getChildren().length;
@@ -119,7 +119,7 @@
model.data[0].attributes = { 'level': 1 };
model.data[4].type = '/heading';
// Push rebuild action
- sync.pushAction( 'rebuild', node, 0 );
+ sync.pushRebuild( node, 0 );
// Sync
sync.synchronize();
return model.getChildren()[0].getElementType();
@@ -135,7 +135,7 @@
// Insert element after first paragraph
ve.insertIntoArray( model.data, 5, data );
// Push rebuild action with a length adustment
of 3 to account for the new element
- sync.pushAction( 'rebuild', node, 0, 3 );
+ sync.pushRebuild( node, 3 );
// Sync
sync.synchronize();
return model.getChildren()[1].getContentData();
@@ -151,15 +151,15 @@
// Delete bold "b" from first paragraph
model.data.splice( 2, 1 );
// Push resize action
- sync.pushAction( 'resize',
model.getChildren()[0], 0, -1 );
+ sync.pushResize( model.getChildren()[0], -1 );
// Delete the first paragraph (offset adjusted
for previous action)
model.data.splice( 30, 3 );
- // Push deletion action (note: using original
offset)
- sync.pushAction( 'delete',
model.getChildren()[2], 31 );
+ // Push deletion action
+ sync.pushDelete( model.getChildren()[2] );
// Insert element after last paragraph
ve.insertIntoArray( model.data, 30, data );
// Push insertion action (note: using original
offset)
- sync.pushAction( 'insert', node, 34 );
+ sync.pushInsert( node, 34 );
// Sync
sync.synchronize();
return [
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs