r104908 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104907‎ | r104908 | r104909 >
Date:22:45, 1 December 2011
Author:inez
Status:deferred
Tags:
Comment:
Added pretty solid implementation of prepareLeafConversion method
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
@@ -1179,36 +1179,40 @@
11801180 es.TransactionProcessor.rollback( this, transaction );
11811181 };
11821182
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 = [],
11851188 nodeOffset,
1186 - content,
1187 - tx = [];
 1189+ txs = [];
11881190
1189 - type = 'paragraph'
 1191+ this.traverseLeafNodes( function( node ) {
 1192+ nodes.push( node );
 1193+ if( node === toNode ) {
 1194+ return false;
 1195+ }
 1196+ }, fromNode );
11901197
 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+
11911202 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 );
11961204
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+ ) );
12081214 }
12091215
1210 - for ( var i = 0; i < tx.length; i++ ) {
1211 - this.commit( tx[i] );
1212 - }
 1216+ return txs;
12131217 };
12141218
12151219 /* Inheritance */

Status & tagging log