Index: trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js |
— | — | @@ -19,15 +19,73 @@ |
20 | 20 | /* Methods */ |
21 | 21 | |
22 | 22 | es.ListButtonTool.prototype.onClick = function() { |
23 | | - /* |
24 | | - var parent; |
25 | | - for( var i = 0; i < this.nodes.length; i++ ) { |
26 | | - parent = this.nodes[i].getParent(); |
27 | | - if ( parent.getElementType() !== 'listItem' ) { |
28 | | - console.log( this.nodes[i] ); |
| 23 | + var stacks = [], |
| 24 | + stack = [], |
| 25 | + i, |
| 26 | + j, |
| 27 | + data; |
| 28 | + |
| 29 | + // |
| 30 | + // Step 1 |
| 31 | + // |
| 32 | + |
| 33 | + for( i = 0; i < this.nodes.length; i++ ) { |
| 34 | + if ( this.nodes[i].getParent().getElementType() === 'listItem' ) { |
| 35 | + if( stack.length > 0 ) { |
| 36 | + stacks.push ( stack ); |
| 37 | + stack = []; |
| 38 | + } |
| 39 | + } else { |
| 40 | + if( stack.length > 0 ) { |
| 41 | + if ( this.nodes[i].getParent() === stack[stack.length - 1].getParent() ) { |
| 42 | + stack.push( this.nodes[i] ); |
| 43 | + } else { |
| 44 | + stacks.push ( stack ); |
| 45 | + stack = [this.nodes[i]]; |
| 46 | + } |
| 47 | + } else { |
| 48 | + stack.push( this.nodes[i] ); |
| 49 | + } |
29 | 50 | } |
30 | 51 | } |
31 | | - */ |
| 52 | + if( stack.length > 0 ) { |
| 53 | + stacks.push ( stack ); |
| 54 | + } |
| 55 | + |
| 56 | + // |
| 57 | + // Step 2 |
| 58 | + // |
| 59 | + |
| 60 | + var insertAt, |
| 61 | + removeLength, |
| 62 | + tx; |
| 63 | + |
| 64 | + for( i = 0; i < stacks.length; i++ ) { |
| 65 | + removeLength = 0; |
| 66 | + insertAt = this.toolbar.surfaceView.documentView.model.getOffsetFromNode( |
| 67 | + stacks[i][0], false |
| 68 | + ); |
| 69 | + data = [ { 'type': 'list' } ]; |
| 70 | + for( j = 0; j < stacks[i].length; j++ ) { |
| 71 | + removeLength += stacks[i][j].getElementLength(); |
| 72 | + data = data |
| 73 | + .concat( [ { 'type': 'listItem', 'attributes' : { 'styles': [ this.name ] } } ] ) |
| 74 | + .concat( stacks[i][j].getElementData() ) |
| 75 | + .concat( [ { 'type': '/listItem' } ] ); |
| 76 | + } |
| 77 | + data = data.concat( [ { 'type': '/list' } ] ); |
| 78 | + |
| 79 | + tx = this.toolbar.surfaceView.model.getDocument().prepareRemoval( |
| 80 | + new es.Range( insertAt, insertAt+removeLength) |
| 81 | + ); |
| 82 | + this.toolbar.surfaceView.model.transact( tx ); |
| 83 | + |
| 84 | + tx = this.toolbar.surfaceView.model.getDocument().prepareInsertion( |
| 85 | + insertAt, |
| 86 | + data |
| 87 | + ); |
| 88 | + this.toolbar.surfaceView.model.transact( tx ); |
| 89 | + } |
32 | 90 | }; |
33 | 91 | |
34 | 92 | es.ListButtonTool.prototype.updateState = function( annotations, nodes ) { |