https://www.mediawiki.org/wiki/Special:Code/MediaWiki/103377

Revision: 103377
Author:   tparscal
Date:     2011-11-16 20:39:48 +0000 (Wed, 16 Nov 2011)
Log Message:
-----------
* Added support for inserting content at a structural offset
* Broke rebuildNodes into 2 parts so insert can use just buildNodes when 
inserting whole nodes
* Added getIndexFromOffset to es.DocumentModelBranchNode objects, which returns 
an index of a child node from an offset

Modified Paths:
--------------
    trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
    trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js

Modified: 
trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js     
2011-11-16 20:38:24 UTC (rev 103376)
+++ trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js     
2011-11-16 20:39:48 UTC (rev 103377)
@@ -257,6 +257,26 @@
 };
 
 /**
+ * Gets the index of a child node from a given offset.
+ * 
+ * @method
+ * @param {Integer} offset Offset to find index of
+ * @returns {Integer} Index of child node at offset or -1 if offset was out of 
range
+ */
+es.DocumentBranchNode.prototype.getIndexFromOffset = function( offset ) {
+       var left = 0,
+               elementLength;
+       for ( var i = 0; i < this.children.length; i++ ) {
+               elementLength = this.children[i].getElementLength();
+               if ( offset >= left && offset < left + elementLength ) {
+                       return i;
+               }
+               left += elementLength;
+       }
+       return -1;
+};
+
+/**
  * Gets a list of nodes and their sub-ranges which are covered by a given 
range.
  * 
  * @method

Modified: trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js 
2011-11-16 20:38:24 UTC (rev 103376)
+++ trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js 
2011-11-16 20:39:48 UTC (rev 103377)
@@ -101,6 +101,10 @@
                // Remove the node we are about to insert into from the model 
tree
                parent.splice( index, oldNodes.length );
        }
+       this.buildNodes( newData, parent, index );
+};
+
+es.TransactionProcessor.prototype.buildNodes = function( newData, parent, 
index ) {
        // Regenerate nodes for the data we've affected
        var newNodes = es.DocumentModel.createNodesFromData( newData );
        // Insert new elements into the tree where the old ones used to be
@@ -177,7 +181,12 @@
 
 es.TransactionProcessor.prototype.insert = function( op ) {
        if ( es.DocumentModel.isStructuralOffset( this.model.data, this.cursor 
) ) {
-               // TODO: Support tree updates when inserting between elements
+               es.insertIntoArray( this.model.data, this.cursor, op.data );
+               this.applyAnnotations( this.cursor + op.data.length );
+               var parent = this.model.getNodeFromOffset( this.cursor ),
+                       index = parent.getIndexFromOffset( this.cursor );
+               console.log( parent, index );
+               this.buildNodes( op.data, parent, index );
        } else {
                // Get the node we are about to insert into at the lowest depth 
possible
                var node = this.getScope( this.model.getNodeFromOffset( 
this.cursor ), op.data );


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to