Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js |
— | — | @@ -75,51 +75,82 @@ |
76 | 76 | locationEnd = this.list.getLocationFromOffset( range.end ); |
77 | 77 | |
78 | 78 | if ( locationStart.item == locationEnd.item ) { |
| 79 | + |
| 80 | + // delete content within one item |
79 | 81 | |
80 | 82 | locationStart.item.content.remove( |
81 | 83 | new es.Range( locationStart.offset, locationStart.offset + range.getLength() ) |
82 | 84 | ); |
83 | 85 | |
84 | 86 | } else { |
85 | | - var contentToAppend = locationEnd.item.flow.content.getContent( |
| 87 | + |
| 88 | + // delete content across multiple items |
| 89 | + |
| 90 | + // delete selected content in first selected item |
| 91 | + locationStart.item.content.remove( |
86 | 92 | new es.Range( |
| 93 | + locationStart.offset, |
| 94 | + locationStart.item.content.getLength() |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + // grab not selected content from last selected item.. |
| 99 | + var toAppend = locationEnd.item.content.getContent( |
| 100 | + new es.Range( |
87 | 101 | locationEnd.offset, |
88 | | - locationEnd.item.flow.content.getLength() |
| 102 | + locationEnd.item.content.getLength() |
89 | 103 | ) |
90 | 104 | ); |
91 | | - locationStart.item.flow.content.remove( |
92 | | - new es.Range( locationStart.offset, locationStart.item.flow.content.getLength() ) |
93 | | - ); |
94 | | - locationStart.item.flow.content.insert( locationStart.offset, contentToAppend.data ); |
| 105 | + |
| 106 | + // ..and insert it at the end of first selected item |
| 107 | + locationStart.item.content.insert( locationStart.offset, toAppend.data ); |
95 | 108 | |
96 | | - var itemsToDelete; |
| 109 | + var toDelete = [], |
| 110 | + toAdopt = [], |
| 111 | + newParents = [], |
| 112 | + toDeleteCollecting = false, |
| 113 | + toAdoptCollecting = false; |
| 114 | + |
97 | 115 | this.traverseItems( function( item, index ) { |
98 | | - if ( $.isArray( itemsToDelete ) ) { |
99 | | - itemsToDelete.push( item ); |
| 116 | + |
| 117 | + if ( toDeleteCollecting === false && toDelete.length === 0 ) { |
| 118 | + newParents[ item.getLevel() ] = item; |
100 | 119 | } |
101 | | - if ( item == locationEnd.item ) { |
102 | | - return false; |
| 120 | + |
| 121 | + if ( toDeleteCollecting ) { |
| 122 | + toDelete.push( item ); |
103 | 123 | } |
| 124 | + |
104 | 125 | if ( item == locationStart.item ) { |
105 | | - itemsToDelete = []; |
| 126 | + toDeleteCollecting = true; |
| 127 | + } |
| 128 | + |
| 129 | + if ( item == locationEnd.item ) { |
| 130 | + toDeleteCollecting = false; |
| 131 | + toAdoptCollecting = true; |
| 132 | + return true; |
106 | 133 | } |
107 | | - } ); |
108 | 134 | |
109 | | - for ( var i = itemsToDelete.length - 1; i >= 0; i-- ) { |
110 | | - if ( itemsToDelete[i].getLength() - 1 === itemsToDelete[i].flow.content.getLength() ) { |
111 | | - itemsToDelete[i].list.remove(itemsToDelete[i]); |
112 | | - } else { |
113 | | - itemsToDelete[i].flow.content.remove( new es.Range( 0, itemsToDelete[i].flow.content.getLength() ) ); |
| 135 | + if ( toAdoptCollecting ) { |
| 136 | + if( toDelete.indexOf ( item.list.item ) !== -1 ) { |
| 137 | + toAdopt.push( item ); |
| 138 | + } |
114 | 139 | } |
115 | | - } |
116 | 140 | |
117 | | - /* |
118 | | - var start = locationEnd.item; |
119 | | - while(start) { |
120 | | - start = start.list.item; |
121 | | - console.log("-") |
| 141 | + } ); |
| 142 | + |
| 143 | + var toAdoptLevel, |
| 144 | + newParent; |
| 145 | + |
| 146 | + for ( var i = toAdopt.length - 1; i >= 0; i-- ) { |
| 147 | + toAdoptLevel = toAdopt[i].getLevel(); |
| 148 | + newParent = newParents[ toAdoptLevel ] ? newParents[ toAdoptLevel ] : newParents[ newParents.length - 1 ]; |
| 149 | + newParent.lists[0].prepend( toAdopt[i] ); |
122 | 150 | } |
123 | | - */ |
| 151 | + |
| 152 | + for ( var i = toDelete.length - 1; i >= 0; i-- ) { |
| 153 | + toDelete[i].list.remove(toDelete[i]); |
| 154 | + } |
124 | 155 | } |
125 | 156 | }; |
126 | 157 | |