r103447 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103446‎ | r103447 | r103448 >
Date:08:03, 17 November 2011
Author:tparscal
Status:resolved (Comments)
Tags:
Comment:
Reduced (dramatically) the number of re-renders on insert (but there's still more duplication being done atm, especially on load)
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.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
@@ -62,7 +62,12 @@
6363 * 'children': null
6464 * }
6565 */
66 -es.DocumentModel.nodeRules = {};
 66+es.DocumentModel.nodeRules = {
 67+ 'document': {
 68+ 'parents': null,
 69+ 'children': null
 70+ }
 71+};
6772
6873 /* Static Methods */
6974
Index: trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js
@@ -88,29 +88,24 @@
8989 }
9090 };
9191
92 -es.TransactionProcessor.prototype.rebuildNodes = function( newData, oldNodes ) {
93 - var parent,
94 - index;
95 - if ( oldNodes[0] === oldNodes[0].getRoot() ) {
96 - parent = oldNodes[0];
97 - parent.splice( 0, parent.getChildren().length );
98 - index = 0;
99 - } else {
100 - parent = oldNodes[0].getParent();
101 - index = parent.indexOf( oldNodes[0] );
102 - // Remove the node we are about to insert into from the model tree
103 - parent.splice( index, oldNodes.length );
 92+es.TransactionProcessor.prototype.rebuildNodes = function( newData, oldNodes, parent, index ) {
 93+ var remove = 0;
 94+ if ( oldNodes ) {
 95+ if ( oldNodes[0] === oldNodes[0].getRoot() ) {
 96+ parent = oldNodes[0];
 97+ index = 0;
 98+ remove = parent.getChildren().length;
 99+ } else {
 100+ parent = oldNodes[0].getParent();
 101+ index = parent.indexOf( oldNodes[0] );
 102+ remove = oldNodes.length;
 103+ }
104104 }
105 - this.buildNodes( newData, parent, index );
 105+ parent.splice.apply(
 106+ parent, [index, remove].concat( es.DocumentModel.createNodesFromData( newData ) )
 107+ );
106108 };
107109
108 -es.TransactionProcessor.prototype.buildNodes = function( newData, parent, index ) {
109 - // Regenerate nodes for the data we've affected
110 - var newNodes = es.DocumentModel.createNodesFromData( newData );
111 - // Insert new elements into the tree where the old ones used to be
112 - parent.splice.apply( parent, [index, 0].concat( newNodes ) );
113 -};
114 -
115110 es.TransactionProcessor.prototype.getScope = function( node, data ) {
116111 var i,
117112 length,
@@ -123,7 +118,7 @@
124119 }
125120 }
126121 if ( maxDepth > 0 ) {
127 - for ( i = 0; i < maxDepth; i++ ) {
 122+ for ( i = 1; i < maxDepth; i++ ) {
128123 node = node.getParent();
129124 }
130125 }
@@ -180,22 +175,25 @@
181176 };
182177
183178 es.TransactionProcessor.prototype.insert = function( op ) {
 179+ var node,
 180+ index,
 181+ offset;
184182 if ( es.DocumentModel.isStructuralOffset( this.model.data, this.cursor ) ) {
185183 es.insertIntoArray( this.model.data, this.cursor, op.data );
186184 this.applyAnnotations( this.cursor + op.data.length );
187 - var parent = this.model.getNodeFromOffset( this.cursor ),
188 - index = parent.getIndexFromOffset( this.cursor );
189 - console.log( parent, index );
190 - this.buildNodes( op.data, parent, index );
 185+ node = this.model.getNodeFromOffset( this.cursor );
 186+ index = node.getIndexFromOffset( this.cursor );
 187+ this.rebuildNodes( op.data, null, node, index );
191188 } else {
192 - // Get the node we are about to insert into at the lowest depth possible
193 - var node = this.getScope( this.model.getNodeFromOffset( this.cursor ), op.data );
194 - if ( !node ) {
195 - throw 'Missing node error. Scope could not be resolved';
 189+ node = this.model.getNodeFromOffset( this.cursor );
 190+ if ( node.getParent() === this.model ) {
 191+ offset = this.model.getOffsetFromNode( node );
 192+ index = this.model.getIndexFromOffset( this.cursor - offset );
 193+ } else {
 194+ node = this.getScope( node, op.data );
 195+ offset = this.model.getOffsetFromNode( node );
 196+ index = node.getIndexFromOffset( this.cursor - offset );
196197 }
197 - // Figure out how deep the data goes
198 -
199 - var offset = this.model.getOffsetFromNode( node );
200198 if ( es.DocumentModel.containsElementData( op.data ) ) {
201199 // Perform insert on linear data model
202200 es.insertIntoArray( this.model.data, this.cursor, op.data );

Follow-up revisions

RevisionCommit summaryAuthorDate
r103480Addressed some issues identified in review of r103447 - this could be cleaner...tparscal18:11, 17 November 2011
r103483Addressing another issue in r103447 - minor adjustment to looptparscal18:14, 17 November 2011

Comments

#Comment by Catrope (talk | contribs)   12:59, 17 November 2011
+	parent.splice.apply(
+		parent, [index, remove].concat( es.DocumentModel.createNodesFromData( newData ) )
+	);

You should use es.insertIntoArray() or whatever it's called here, because the number of elements in the return value of createNodesFromData() is unbounded.

-		for ( i = 0; i < maxDepth; i++ ) {
+		for ( i = 1; i < maxDepth; i++ ) {
 			node = node.getParent();
 		}

Using i = 0; i < maxDepth - 1; would be clearer.

+		index = node.getIndexFromOffset( this.cursor );

As mentioned in r103377, this has a local vs global offset problem. Strangely, the code in the else branch does do this correctly.

This code is not easy to understand because getScope() is undocumented. In getScope(), the maxDepth variable is very strangely named given that it tracks "negative depth" (i.e. it goes up).

#Comment by Trevor Parscal (WMF) (talk | contribs)   18:16, 17 November 2011

See follow-ups for the first 2 issues, the latter one was resolved in r103479 and r103484

Status & tagging log