Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js |
— | — | @@ -62,7 +62,12 @@ |
63 | 63 | * 'children': null |
64 | 64 | * } |
65 | 65 | */ |
66 | | -es.DocumentModel.nodeRules = {}; |
| 66 | +es.DocumentModel.nodeRules = { |
| 67 | + 'document': { |
| 68 | + 'parents': null, |
| 69 | + 'children': null |
| 70 | + } |
| 71 | +}; |
67 | 72 | |
68 | 73 | /* Static Methods */ |
69 | 74 | |
Index: trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js |
— | — | @@ -88,29 +88,24 @@ |
89 | 89 | } |
90 | 90 | }; |
91 | 91 | |
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 | + } |
104 | 104 | } |
105 | | - this.buildNodes( newData, parent, index ); |
| 105 | + parent.splice.apply( |
| 106 | + parent, [index, remove].concat( es.DocumentModel.createNodesFromData( newData ) ) |
| 107 | + ); |
106 | 108 | }; |
107 | 109 | |
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 | | - |
115 | 110 | es.TransactionProcessor.prototype.getScope = function( node, data ) { |
116 | 111 | var i, |
117 | 112 | length, |
— | — | @@ -123,7 +118,7 @@ |
124 | 119 | } |
125 | 120 | } |
126 | 121 | if ( maxDepth > 0 ) { |
127 | | - for ( i = 0; i < maxDepth; i++ ) { |
| 122 | + for ( i = 1; i < maxDepth; i++ ) { |
128 | 123 | node = node.getParent(); |
129 | 124 | } |
130 | 125 | } |
— | — | @@ -180,22 +175,25 @@ |
181 | 176 | }; |
182 | 177 | |
183 | 178 | es.TransactionProcessor.prototype.insert = function( op ) { |
| 179 | + var node, |
| 180 | + index, |
| 181 | + offset; |
184 | 182 | if ( es.DocumentModel.isStructuralOffset( this.model.data, this.cursor ) ) { |
185 | 183 | es.insertIntoArray( this.model.data, this.cursor, op.data ); |
186 | 184 | 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 ); |
191 | 188 | } 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 ); |
196 | 197 | } |
197 | | - // Figure out how deep the data goes |
198 | | - |
199 | | - var offset = this.model.getOffsetFromNode( node ); |
200 | 198 | if ( es.DocumentModel.containsElementData( op.data ) ) { |
201 | 199 | // Perform insert on linear data model |
202 | 200 | es.insertIntoArray( this.model.data, this.cursor, op.data ); |