r113850 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113849‎ | r113850 | r113851 >
Date:21:02, 14 March 2012
Author:catrope
Status:deferred
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js
@@ -9,7 +9,7 @@
1010 // Delete bold "b" from first paragraph
1111 model.data.splice( 2, 1 );
1212 // Push resize action
13 - sync.pushAction( 'resize', model.getChildren()[0], 0, -1 );
 13+ sync.pushResize( model.getChildren()[0], -1 );
1414 // Sync
1515 sync.synchronize();
1616 return model.getChildren()[0].getContentLength();
@@ -25,7 +25,7 @@
2626 // Insert element after first paragraph
2727 ve.insertIntoArray( model.data, 5, data );
2828 // Push insertion action
29 - sync.pushAction( 'insert', node, 5 );
 29+ sync.pushInsert( node, 5 );
3030 // Sync
3131 sync.synchronize();
3232 return model.getChildren()[1].getContentData();
@@ -41,7 +41,7 @@
4242 // Insert element after first paragraph
4343 ve.insertIntoArray( model.data, 0, data );
4444 // Push insertion action
45 - sync.pushAction( 'insert', node, 0 );
 45+ sync.pushInsert( node, 0 );
4646 // Sync
4747 sync.synchronize();
4848 return model.getChildren()[0].getContentData();
@@ -57,7 +57,7 @@
5858 // Insert element after first paragraph
5959 ve.insertIntoArray( model.data, 34, data );
6060 // Push insertion action
61 - sync.pushAction( 'insert', node, 34 );
 61+ sync.pushInsert( node, 34 );
6262 // Sync
6363 sync.synchronize();
6464 return model.getChildren()[3].getContentData();
@@ -72,7 +72,7 @@
7373 // Delete the table
7474 model.data.splice( 5, 26 );
7575 // Push deletion action
76 - sync.pushAction( 'delete', node, 5 );
 76+ sync.pushDelete( node );
7777 // Sync
7878 sync.synchronize();
7979 return model.getChildren().length;
@@ -87,7 +87,7 @@
8888 // Delete the first paragraph
8989 model.data.splice( 0, 5 );
9090 // Push deletion action
91 - sync.pushAction( 'delete', node, 0 );
 91+ sync.pushDelete( node );
9292 // Sync
9393 sync.synchronize();
9494 return model.getChildren().length;
@@ -102,7 +102,7 @@
103103 // Delete the first paragraph
104104 model.data.splice( 31, 3 );
105105 // Push deletion action
106 - sync.pushAction( 'delete', node, 31 );
 106+ sync.pushDelete( node );
107107 // Sync
108108 sync.synchronize();
109109 return model.getChildren().length;
@@ -119,7 +119,7 @@
120120 model.data[0].attributes = { 'level': 1 };
121121 model.data[4].type = '/heading';
122122 // Push rebuild action
123 - sync.pushAction( 'rebuild', node, 0 );
 123+ sync.pushRebuild( node, 0 );
124124 // Sync
125125 sync.synchronize();
126126 return model.getChildren()[0].getElementType();
@@ -135,7 +135,7 @@
136136 // Insert element after first paragraph
137137 ve.insertIntoArray( model.data, 5, data );
138138 // Push rebuild action with a length adustment of 3 to account for the new element
139 - sync.pushAction( 'rebuild', node, 0, 3 );
 139+ sync.pushRebuild( node, 3 );
140140 // Sync
141141 sync.synchronize();
142142 return model.getChildren()[1].getContentData();
@@ -151,15 +151,15 @@
152152 // Delete bold "b" from first paragraph
153153 model.data.splice( 2, 1 );
154154 // Push resize action
155 - sync.pushAction( 'resize', model.getChildren()[0], 0, -1 );
 155+ sync.pushResize( model.getChildren()[0], -1 );
156156 // Delete the first paragraph (offset adjusted for previous action)
157157 model.data.splice( 30, 3 );
158 - // Push deletion action (note: using original offset)
159 - sync.pushAction( 'delete', model.getChildren()[2], 31 );
 158+ // Push deletion action
 159+ sync.pushDelete( model.getChildren()[2] );
160160 // Insert element after last paragraph
161161 ve.insertIntoArray( model.data, 30, data );
162162 // Push insertion action (note: using original offset)
163 - sync.pushAction( 'insert', node, 34 );
 163+ sync.pushInsert( node, 34 );
164164 // Sync
165165 sync.synchronize();
166166 return [
Index: trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.DocumentSynchronizer.js
@@ -20,26 +20,72 @@
2121 };
2222
2323 /**
24 - * Adds an action to the synchronizer.
25 - *
26 - * @method
27 - * @param {String} type Type of action, can be: "insert", "delete", "rebuild", "resize" or "update"
28 - * @param {ve.dm.Node} node Node this action is related to
29 - * @param {Integer|null} offset Offset of node, improves performance if this has already been calculated.
30 - * Only used for insert and rebuild actions
31 - * @param {Integer} adjustment Node length adjustment, if any
 24+ * Add an insert action to the queue
 25+ * @param {ve.dm.BranchNode} node Node to insert
 26+ * @param {Integer} [offset] Offset of the inserted node, if known
3227 */
33 -ve.dm.DocumentSynchronizer.prototype.pushAction = function( type, node, offset, adjustment ) {
 28+ve.dm.DocumentSynchronizer.prototype.pushInsert = function( node, offset ) {
3429 this.actions.push( {
35 - 'type': type,
 30+ 'type': 'insert',
3631 'node': node,
37 - 'offset': offset,
38 - 'adjustment': adjustment || 0
 32+ 'offset': offset || null
3933 } );
4034 };
4135
4236 /**
43 - * Applies queued actions to the model tree.
 37+ * Add a delete action to the queue
 38+ * @param {ve.dm.BranchNode} node Node to delete
 39+ */
 40+ve.dm.DocumentSynchronizer.prototype.pushDelete = function( node ) {
 41+ this.actions.push( {
 42+ 'type': 'delete',
 43+ 'node': node
 44+ } );
 45+};
 46+
 47+/**
 48+ * Add a rebuild action to the queue. This rebuilds a node from data
 49+ * found in the linear model.
 50+ * @param {ve.dm.BranchNode} node Node to rebuild
 51+ * @param {Integer} adjustment Length adjustment to apply to the node
 52+ * @param {Integer} offset Offset of the node, if known
 53+ */
 54+ve.dm.DocumentSynchronizer.prototype.pushRebuild = function( node, adjustment, offset ) {
 55+ this.actions.push( {
 56+ 'type': 'rebuild',
 57+ 'node': node,
 58+ 'adjustment': adjustment,
 59+ 'offset': offset || null
 60+ } );
 61+};
 62+
 63+/**
 64+ * Add a resize action to the queue. This changes the content length of a leaf node.
 65+ * @param {ve.dm.BranchNode} node Node to resize
 66+ * @param {Integer} adjustment Length adjustment to apply to the node
 67+ */
 68+ve.dm.DocumentSynchronizer.prototype.pushResize = function( node, adjustment ) {
 69+ this.actions.push( {
 70+ 'type': 'resize',
 71+ 'node': node,
 72+ 'adjustment': adjustment
 73+ } );
 74+};
 75+
 76+/**
 77+ * Add an update action to the queue
 78+ * @param {ve.dm.BranchNode} node Node to update
 79+ */
 80+ve.dm.DocumentSynchronizer.prototype.pushUpdate = function( node ) {
 81+ this.actions.push( {
 82+ 'type': 'update',
 83+ 'node': node
 84+ } );
 85+};
 86+
 87+/**
 88+ * Apply queued actions to the model tree. This assumes that the linear model
 89+ * has already been updated, but the model tree has not yet been.
4490 *
4591 * @method
4692 */
@@ -51,7 +97,7 @@
5298 parent;
5399 for ( var i = 0, len = this.actions.length; i < len; i++ ) {
54100 action = this.actions[i];
55 - offset = action.offset;
 101+ offset = action.offset || null;
56102 switch ( action.type ) {
57103 case 'insert':
58104 // Compute the offset if it wasn't provided

Status & tagging log