r101913 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101912‎ | r101913 | r101914 >
Date:23:19, 3 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Refactored some of the tree sync code to be reusable
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
@@ -65,45 +65,47 @@
6666 * Each function is called in the context of a state, and takes an operation object as a parameter.
6767 */
6868 es.DocumentModel.operations = ( function() {
69 - function invalidate( from, to ) {
70 - this.rebuild.push( { 'from': from, 'to': to } );
71 - }
72 -
7369 function retain( op ) {
7470 annotate.call( this, this.cursor + op.length );
7571 this.cursor += op.length;
7672 }
7773
 74+ function rebuild( newData, oldNodes ) {
 75+ var parent = oldNodes[0].getParent(),
 76+ index = parent.indexOf( oldNodes[0] );
 77+ // Remove the node we are about to insert into from the model tree
 78+ parent.splice( index, oldNodes.length );
 79+ // Regenerate nodes for the data we've affected
 80+ var newNodes = es.DocumentModel.createNodesFromData( newData );
 81+ // Insert new elements into the tree where the old ones used to be
 82+ for ( var i = newNodes.length; i >= 0; i-- ) {
 83+ parent.splice( index, 0, newNodes[i] );
 84+ }
 85+ }
 86+
7887 function insert( op ) {
7988 if ( es.DocumentModel.isStructuralOffset( this.data, this.cursor ) ) {
8089 // TODO: Support tree updates when inserting between elements
8190 } else {
8291 // Get the node we are about to insert into
8392 var node = this.tree.getNodeFromOffset( this.cursor );
 93+ if ( !node ) {
 94+ throw 'Missing node error. A node could not not be found at the cursor.';
 95+ }
8496 if ( es.DocumentModel.containsElementData( op.data ) ) {
85 - var nodeParent = node.getParent();
86 - if ( !nodeParent ) {
87 - throw 'Missing parent error. Node does not have a parent node.';
88 - }
89 - var offset = this.tree.getOffsetFromNode( node ),
90 - length = node.getElementLength() + op.data.length,
91 - index = nodeParent.indexOf( node );
92 - if ( index === -1 ) {
93 - throw 'Missing child error. Node could not be found in its parent node.';
94 - }
95 - // Remove the node we are about to insert into from the model tree
96 - nodeParent.splice( index, 1 );
9797 // Perform insert on linear data model
9898 es.insertIntoArray( this.data, this.cursor, op.data );
9999 annotate.call( this, this.cursor + op.data.length );
100 - // Regenerate nodes for the data we've affected
101 - var nodes = es.DocumentModel.createNodesFromData(
102 - this.data.slice( offset, length )
103 - );
104 - // Insert new elements into the tree where the old one used to be
105 - for ( var i = nodes.length; i >= 0; i-- ) {
106 - this.tree.splice( index, nodes[i] );
 100+ // Synchronize model tree
 101+ var offset = this.tree.getOffsetFromNode( node );
 102+ if ( offset === -1 ) {
 103+ throw 'Invalid offset error. Node is not in model tree';
107104 }
 105+ rebuild.call(
 106+ this,
 107+ this.data.slice( offset, offset + node.getElementLength() + op.data.length ),
 108+ [node]
 109+ );
108110 } else {
109111 // Perform insert on linear data model
110112 es.insertIntoArray( this.data, this.cursor, op.data );
@@ -1208,8 +1210,7 @@
12091211 'tree': this,
12101212 'cursor': 0,
12111213 'set': [],
1212 - 'clear': [],
1213 - 'rebuild': []
 1214+ 'clear': []
12141215 },
12151216 operations = transaction.getOperations();
12161217 for ( var i = 0, length = operations.length; i < length; i++ ) {
@@ -1220,7 +1221,6 @@
12211222 throw 'Invalid operation error. Operation type is not supported: ' + operation.type;
12221223 }
12231224 }
1224 - // TODO: Synchronize op.tree - insert elements and adjust lengths
12251225 };
12261226
12271227 /**
@@ -1235,8 +1235,7 @@
12361236 'tree': this,
12371237 'cursor': 0,
12381238 'set': [],
1239 - 'clear': [],
1240 - 'rebuild': []
 1239+ 'clear': []
12411240 },
12421241 operations = transaction.getOperations();
12431242 for ( var i = 0, length = operations.length; i < length; i++ ) {
@@ -1247,7 +1246,6 @@
12481247 throw 'Invalid operation error. Operation type is not supported: ' + operation.type;
12491248 }
12501249 }
1251 - // TODO: Synchronize op.tree - insert elements and adjust lengths
12521250 };
12531251
12541252 /* Inheritance */
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentModelBranchNode.js
@@ -200,6 +200,17 @@
201201 };
202202
203203 /**
 204+ * Gets the index of a given child node.
 205+ *
 206+ * @method
 207+ * @param {es.DocumentModelNode} node Child node to find index of
 208+ * @returns {Integer} Index of child node or -1 if node was not found
 209+ */
 210+es.DocumentModelBranchNode.prototype.indexOf = function( node ) {
 211+ return this.children.indexOf( node );
 212+};
 213+
 214+/**
204215 * Sets the root node to this and all of it's children.
205216 *
206217 * @method

Status & tagging log