Index: trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js |
— | — | @@ -126,83 +126,121 @@ |
127 | 127 | surface.model.select( selection, true ); |
128 | 128 | }; |
129 | 129 | |
130 | | -es.ListButtonTool.prototype.unlist = function() { |
131 | | - alert( 'This functionality is not yet supported.' ); |
132 | | - /* |
133 | | - // unlist |
134 | | - // |
135 | | - // Step 1 |
136 | | - // |
137 | | - |
138 | | - var listItems = [], |
139 | | - listItem, |
140 | | - i, |
141 | | - j; |
142 | | - |
143 | | - for( i = 0; i < this.nodes.length; i++ ) { |
144 | | - listItem = this.nodes[i].getParent(); |
145 | | - if ( listItems.length > 0 ) { |
146 | | - if (listItem != listItems[listItems.length - 1]) { |
147 | | - listItems.push( listItem ); |
148 | | - } |
149 | | - } else { |
| 130 | +es.ListButtonTool.prototype.unlist = function( nodes ) { |
| 131 | + var listItems = [], |
| 132 | + listItem, |
| 133 | + i; |
| 134 | + |
| 135 | + for( i = 0; i < nodes.length; i++ ) { |
| 136 | + listItem = nodes[i].getParent(); |
| 137 | + if ( listItems.length > 0 ) { |
| 138 | + if (listItem != listItems[listItems.length - 1]) { |
150 | 139 | listItems.push( listItem ); |
151 | 140 | } |
| 141 | + } else { |
| 142 | + listItems.push( listItem ); |
152 | 143 | } |
153 | | - |
154 | | - var stacks = []; |
155 | | - var stack = { first: false, last: false, nodes: [], offset: -1, length: 0 }; |
156 | | - for( i = 0; i < listItems.length; i++ ) { |
157 | | - if( stack.nodes.length > 0 ) { |
158 | | - if ( stack.nodes[stack.nodes.length - 1].getParent() != listItems[i].getParent() ) { |
159 | | - stacks.push(stack); |
160 | | - stack = { first: false, last: false, nodes: [], offset: -1, length: 0 }; |
161 | | - } |
| 144 | + } |
| 145 | + |
| 146 | + var stacks = [], |
| 147 | + stack = { |
| 148 | + first: false, |
| 149 | + last: false, |
| 150 | + nodes: [], |
| 151 | + offset: 0, |
| 152 | + length: 0 |
| 153 | + }, |
| 154 | + surface = this.toolbar.surfaceView; |
| 155 | + |
| 156 | + for( i = 0; i < listItems.length; i++ ) { |
| 157 | + if( stack.nodes.length > 0 ) { |
| 158 | + if ( stack.nodes[stack.nodes.length - 1].getParent() != listItems[i].getParent() ) { |
| 159 | + stacks.push( stack ); |
| 160 | + stack = { |
| 161 | + first: false, |
| 162 | + last: false, |
| 163 | + nodes: [], |
| 164 | + offset: 0, |
| 165 | + length: 0 |
| 166 | + }; |
162 | 167 | } |
163 | | - |
164 | | - if ( listItems[i].getParent().indexOf( listItems[i] ) === 0 ) { |
165 | | - stack.first = true; |
166 | | - } |
167 | | - if ( listItems[i].getParent().indexOf( listItems[i] ) === listItems[i].getParent().children.length - 1 ) { |
168 | | - stack.last = true; |
169 | | - } |
170 | | - if( stack.nodes.length === 0 ){ |
171 | | - stack.offset = this.toolbar.surfaceView.documentView.model.getOffsetFromNode(listItems[i], false); |
172 | | - } |
173 | | - stack.length += listItems[i].getElementLength(); |
174 | | - stack.nodes.push( listItems[i] ); |
175 | | - |
176 | 168 | } |
177 | | - if( stack.nodes.length > 0 ) { |
178 | | - stacks.push(stack); |
| 169 | + if ( listItems[i].getParent().indexOf( listItems[i] ) === 0 ) { |
| 170 | + stack.first = true; |
179 | 171 | } |
| 172 | + if ( listItems[i].getParent().indexOf( listItems[i] ) === listItems[i].getParent().children.length - 1 ) { |
| 173 | + stack.last = true; |
| 174 | + } |
| 175 | + if( stack.nodes.length === 0 ){ |
| 176 | + stack.offset = surface.documentView.model.getOffsetFromNode(listItems[i], false); |
| 177 | + } |
| 178 | + stack.length += listItems[i].getElementLength(); |
| 179 | + stack.nodes.push( listItems[i] ); |
| 180 | + } |
| 181 | + if( stack.nodes.length > 0 ) { |
| 182 | + stacks.push(stack); |
| 183 | + } |
180 | 184 | |
181 | | - // |
182 | | - // Step 2 |
183 | | - // |
184 | | - var data; |
185 | | - |
186 | | - for( i = 0; i < stacks.length; i++ ) { |
187 | | - stack = stacks[i]; |
188 | | - if( stack.first === false && stack.last === false ) { |
| 185 | + var tx, |
| 186 | + j, |
| 187 | + extra, |
| 188 | + data; |
189 | 189 | |
| 190 | + for( i = stacks.length - 1; i >= 0; i-- ) { |
| 191 | + stack = stacks[i]; |
190 | 192 | |
191 | | - tx = this.toolbar.surfaceView.model.getDocument().prepareRemoval( |
192 | | - new es.Range( stack.offset, stack.offset+stack.length ) |
193 | | - ); |
194 | | - this.toolbar.surfaceView.model.transact( tx ); |
| 193 | + data = []; |
| 194 | + for( j = 0; j < stack.nodes.length; j++ ) { |
| 195 | + data = data.concat( stack.nodes[j].getContentData() ); |
| 196 | + } |
195 | 197 | |
196 | | - data = [ { 'type': '/list' }, { 'type': 'list' } ]; |
197 | | - tx = this.toolbar.surfaceView.model.getDocument().prepareInsertion( |
198 | | - stack.offset, |
199 | | - data |
200 | | - ); |
201 | | - console.log(tx); |
202 | | - this.toolbar.surfaceView.model.transact( tx ); |
| 198 | + if ( stack.first === true && stack.last === true ) { |
| 199 | + tx = surface.model.getDocument().prepareRemoval( |
| 200 | + new es.Range( stack.offset - 1 /* list */, stack.offset + stack.length + 1 /* /list */ ) |
| 201 | + ); |
| 202 | + surface.model.transact( tx ); |
| 203 | + tx = surface.model.getDocument().prepareInsertion( stack.offset - 1, data ); |
| 204 | + surface.model.transact( tx ); |
| 205 | + } else if ( stack.first === true && stack.last === false ) { |
| 206 | + tx = surface.model.getDocument().prepareRemoval( |
| 207 | + new es.Range( stack.offset, stack.offset + stack.length ) |
| 208 | + ); |
| 209 | + surface.model.transact( tx ); |
| 210 | + tx = surface.model.getDocument().prepareInsertion( stack.offset - 1, data ); |
| 211 | + surface.model.transact( tx ); |
| 212 | + } else if ( stack.first === false && stack.last === true ) { |
| 213 | + tx = surface.model.getDocument().prepareRemoval( |
| 214 | + new es.Range( stack.offset, stack.offset + stack.length ) |
| 215 | + ); |
| 216 | + surface.model.transact( tx ); |
| 217 | + tx = surface.model.getDocument().prepareInsertion( stack.offset + 1, data ); |
| 218 | + surface.model.transact( tx ); |
| 219 | + } else if ( stack.first === false && stack.last === false ) { |
| 220 | + var parent = stack.nodes[0].getParent(); |
| 221 | + var parentOffset = surface.documentView.model.getOffsetFromNode( parent, false ); |
| 222 | + var parentLength = parent.getElementLength(); |
| 223 | + |
| 224 | + tx = surface.model.getDocument().prepareRemoval( |
| 225 | + new es.Range( stack.offset, stack.offset + stack.length ) |
| 226 | + ); |
| 227 | + surface.model.transact( tx ); |
203 | 228 | |
204 | | - } |
| 229 | + var remainingRange = new es.Range( stack.offset, parentOffset + parentLength - stack.length - 1 ); |
| 230 | + var remainingData = surface.model.getDocument().getData( remainingRange ); |
| 231 | + |
| 232 | + tx = surface.model.getDocument().prepareRemoval( remainingRange ); |
| 233 | + surface.model.transact( tx ); |
| 234 | + |
| 235 | + tx = surface.model.getDocument().prepareInsertion( |
| 236 | + stack.offset + 1, |
| 237 | + [ { 'type': 'list' } ].concat( remainingData ).concat( [ { 'type': '/list' } ] ) |
| 238 | + ); |
| 239 | + surface.model.transact( tx ); |
| 240 | + |
| 241 | + tx = surface.model.getDocument().prepareInsertion( stack.offset + 1, data ); |
| 242 | + surface.model.transact( tx ); |
205 | 243 | } |
206 | | -*/ |
| 244 | + } |
207 | 245 | }; |
208 | 246 | |
209 | 247 | es.ListButtonTool.prototype.onClick = function() { |