Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js |
— | — | @@ -1179,36 +1179,40 @@ |
1180 | 1180 | es.TransactionProcessor.rollback( this, transaction ); |
1181 | 1181 | }; |
1182 | 1182 | |
1183 | | -es.DocumentModel.prototype.convertLeafs = function( range, type ) { |
1184 | | - var nodes = this.selectNodes( range, false ), |
| 1183 | +es.DocumentModel.prototype.prepareLeafConversion = function( range, type, attributes ) { |
| 1184 | + range.normalize(); |
| 1185 | + var fromNode = this.getNodeFromOffset( range.start ), |
| 1186 | + toNode = this.getNodeFromOffset( range.end ), |
| 1187 | + nodes = [], |
1185 | 1188 | nodeOffset, |
1186 | | - content, |
1187 | | - tx = []; |
| 1189 | + txs = []; |
1188 | 1190 | |
1189 | | - type = 'paragraph' |
| 1191 | + this.traverseLeafNodes( function( node ) { |
| 1192 | + nodes.push( node ); |
| 1193 | + if( node === toNode ) { |
| 1194 | + return false; |
| 1195 | + } |
| 1196 | + }, fromNode ); |
1190 | 1197 | |
| 1198 | + attributes = attributes || {}; // or maybe should be null instead of empty collection |
| 1199 | + |
| 1200 | + // TODO: skip nodes which have the same type and attributes as the target |
| 1201 | + |
1191 | 1202 | for ( var i = 0; i < nodes.length; i++ ) { |
1192 | | - if ( !nodes[i].node.hasChildren() ) { |
1193 | | - if ( nodes[i].node.getElementType() !== type ) { |
1194 | | - nodeOffset = this.getOffsetFromNode( nodes[i].node, false ); |
1195 | | - content = nodes[i].node.getContent(); |
| 1203 | + nodeOffset = this.getOffsetFromNode( nodes[i], false ); |
1196 | 1204 | |
1197 | | - tx.push( this.prepareRemoval( |
1198 | | - new es.Range( nodeOffset, nodeOffset+ nodes[i].node.getElementLength() ) |
1199 | | - ) ); |
1200 | | - |
1201 | | - tx.push( this.prepareInsertion( |
1202 | | - nodeOffset, [ { 'type': 'paragraph' }, { 'type': '/paragraph' } ] |
1203 | | - ) ); |
1204 | | - |
1205 | | - tx.push( this.prepareInsertion( nodeOffset + 1, content ) ); |
1206 | | - } |
1207 | | - } |
| 1205 | + txs.push( this.prepareRemoval( |
| 1206 | + new es.Range( nodeOffset, nodeOffset + nodes[i].getElementLength() ) |
| 1207 | + ) ); |
| 1208 | + txs.push( this.prepareInsertion( |
| 1209 | + nodeOffset, |
| 1210 | + [ { 'type': type, 'attributes': attributes } ] |
| 1211 | + .concat( nodes[i].getContent() ) |
| 1212 | + .concat( [ { 'type': '/' + type } ] ) |
| 1213 | + ) ); |
1208 | 1214 | } |
1209 | 1215 | |
1210 | | - for ( var i = 0; i < tx.length; i++ ) { |
1211 | | - this.commit( tx[i] ); |
1212 | | - } |
| 1216 | + return txs; |
1213 | 1217 | }; |
1214 | 1218 | |
1215 | 1219 | /* Inheritance */ |