Index: trunk/parsers/wikidom/tests/transactions/test.js |
— | — | @@ -87,12 +87,13 @@ |
88 | 88 | tx.add( 'remove', removal ); |
89 | 89 | tx.add( 'retain', 18 ); |
90 | 90 | |
91 | | - var committed = tx.commit( paragraph ); |
92 | | - equal( committed.getText(), after.getText(), 'Committing' ); |
93 | | - deepEqual( committed.getContent().getData(), after.getContent().getData(), 'Committing' ); |
94 | | - var rolledback = tx.rollback( committed ); |
95 | | - equal( rolledback.getText(), before.getText(), 'Rolling back' ); |
96 | | - deepEqual( rolledback.getContent().getData(), before.getContent().getData(), 'Rolling back' ); |
| 91 | + tx.commit( paragraph ); |
| 92 | + equal( paragraph.getText(), after.getText(), 'Committing' ); |
| 93 | + deepEqual( paragraph.getContent().getData(), after.getContent().getData(), 'Committing' ); |
| 94 | + |
| 95 | + tx.rollback( paragraph ); |
| 96 | + equal( paragraph.getText(), before.getText(), 'Rolling back' ); |
| 97 | + deepEqual( paragraph.getContent().getData(), before.getContent().getData(), 'Rolling back' ); |
97 | 98 | } ); |
98 | 99 | |
99 | 100 | test( 'Annotating', 4, function() { |
— | — | @@ -134,11 +135,11 @@ |
135 | 136 | tx.add( 'end', annotation ); |
136 | 137 | tx.add( 'retain', 18 ); |
137 | 138 | |
138 | | - var committed = tx.commit( paragraph ); |
| 139 | + tx.commit( paragraph ); |
| 140 | + equal( paragraph.getText(), after.getText(), 'Committing' ); |
| 141 | + deepEqual( paragraph.getContent().getData(), after.getContent().getData(), 'Committing' ); |
139 | 142 | |
140 | | - equal( committed.getText(), after.getText(), 'Committing' ); |
141 | | - deepEqual( committed.getContent().getData(), after.getContent().getData(), 'Committing' ); |
142 | | - var rolledback = tx.rollback( committed ); |
143 | | - equal( rolledback.getText(), before.getText(), 'Rolling back' ); |
144 | | - deepEqual( rolledback.getContent().getData(), before.getContent().getData(), 'Rolling back' ); |
| 143 | + tx.rollback( paragraph ); |
| 144 | + equal( paragraph.getText(), before.getText(), 'Rolling back' ); |
| 145 | + deepEqual( paragraph.getContent().getData(), before.getContent().getData(), 'Rolling back' ); |
145 | 146 | } ); |
Index: trunk/parsers/wikidom/lib/es/es.Mutation.js |
— | — | @@ -2,14 +2,18 @@ |
3 | 3 | this.transactions = transactions || []; |
4 | 4 | }; |
5 | 5 | |
6 | | -es.Mutation.prototype.add = function( id, transaction ) { |
7 | | - this.transactions.push( { 'block': id, 'transaction': transaction } ); |
| 6 | +es.Mutation.prototype.add = function( index, transaction ) { |
| 7 | + this.transactions.push( { 'block': index, 'transaction': transaction } ); |
8 | 8 | }; |
9 | 9 | |
10 | | -es.Mutation.prototype.commit = function() { |
11 | | - |
| 10 | +es.Mutation.prototype.commit = function( document ) { |
| 11 | + for ( var i = 0; i < this.transactions.length; i++ ) { |
| 12 | + this.transactions[i].transaction.commit( document.get( this.transactions[i].block ) ); |
| 13 | + } |
12 | 14 | }; |
13 | 15 | |
14 | | -es.Mutation.prototype.rollback = function() { |
15 | | - |
| 16 | +es.Mutation.prototype.rollback = function( document ) { |
| 17 | + for ( var i = 0; i < this.transactions.length; i++ ) { |
| 18 | + this.transactions[i].transaction.rollback( document.get( this.transactions[i].block ) ); |
| 19 | + } |
16 | 20 | }; |
Index: trunk/parsers/wikidom/lib/es/es.BlockTransaction.js |
— | — | @@ -50,7 +50,7 @@ |
51 | 51 | if ( add.length || rem.length ) { |
52 | 52 | annotate( con, add, rem ); |
53 | 53 | } |
54 | | - dst.insert( dst.getLength(), con.getData() ); |
| 54 | + dst.insertContent( dst.getLength(), con.getData() ); |
55 | 55 | return val; |
56 | 56 | } |
57 | 57 | function insert( val, cur, src, dst, add, rem ) { |
— | — | @@ -58,7 +58,7 @@ |
59 | 59 | if ( add.length || rem.length ) { |
60 | 60 | annotate( con, add, rem ); |
61 | 61 | } |
62 | | - dst.insert( dst.getLength(), con.getData() ); |
| 62 | + dst.insertContent( dst.getLength(), con.getData() ); |
63 | 63 | return 0; |
64 | 64 | } |
65 | 65 | function start( val, cur, src, dst, add, rem ) { |
— | — | @@ -158,28 +158,28 @@ |
159 | 159 | this.cursor += model.advance( val ); |
160 | 160 | }; |
161 | 161 | |
162 | | -es.BlockTransaction.prototype.commit = function( src ) { |
| 162 | +es.BlockTransaction.prototype.commit = function( dst ) { |
163 | 163 | var cur = 0, |
164 | | - dst = new es.Content(), |
| 164 | + src = dst.getContent(), |
165 | 165 | add = [], |
166 | 166 | rem = [], |
167 | 167 | adv; |
| 168 | + dst.clearContent(); |
168 | 169 | for ( var i = 0; i < this.operations.length; i++ ) { |
169 | 170 | var op = this.operations[i]; |
170 | 171 | cur += op.model.commit( op.val, cur, src, dst, add, rem ); |
171 | 172 | } |
172 | | - return dst; |
173 | 173 | }; |
174 | 174 | |
175 | | -es.BlockTransaction.prototype.rollback = function( src ) { |
| 175 | +es.BlockTransaction.prototype.rollback = function( dst ) { |
176 | 176 | var cur = 0, |
177 | | - dst = new es.Content(), |
| 177 | + src = dst.getContent(), |
178 | 178 | add = [], |
179 | 179 | rem = [], |
180 | 180 | adv; |
| 181 | + dst.clearContent(); |
181 | 182 | for ( var i = 0; i < this.operations.length; i++ ) { |
182 | 183 | var op = this.operations[i]; |
183 | 184 | cur += op.model.rollback( op.val, cur, src, dst, add, rem ); |
184 | 185 | } |
185 | | - return dst; |
186 | 186 | }; |
Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -397,6 +397,19 @@ |
398 | 398 | }; |
399 | 399 | |
400 | 400 | /** |
| 401 | + * Removes all content data. |
| 402 | + * |
| 403 | + * @method |
| 404 | + * @emits "clear" |
| 405 | + * @emits "change" with type:"clear" data property |
| 406 | + */ |
| 407 | +es.Content.prototype.clear = function() { |
| 408 | + this.data = []; |
| 409 | + this.emit( 'clear' ); |
| 410 | + this.emit( 'change', { 'type': 'clear' } ); |
| 411 | +}; |
| 412 | + |
| 413 | +/** |
401 | 414 | * Gets a list of indexes within the content data which use a given annotation. |
402 | 415 | * |
403 | 416 | * Strict coverage may be used to compare not only annotation types, but also their data. Since new |
Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js |
— | — | @@ -74,6 +74,15 @@ |
75 | 75 | }; |
76 | 76 | |
77 | 77 | /** |
| 78 | + * Deletes all content in a block. |
| 79 | + * |
| 80 | + * @method |
| 81 | + */ |
| 82 | +es.ParagraphBlock.prototype.clearContent = function() { |
| 83 | + this.content.clear(); |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
78 | 87 | * Applies an annotation to a given range. |
79 | 88 | * |
80 | 89 | * If a range arguments are not provided, all content will be annotated. |
Index: trunk/parsers/wikidom/lib/es/es.Container.js |
— | — | @@ -34,6 +34,16 @@ |
35 | 35 | /* Methods */ |
36 | 36 | |
37 | 37 | /** |
| 38 | + * Gets an item at a specific index. |
| 39 | + * |
| 40 | + * @method |
| 41 | + * @returns {Object} Child object at index |
| 42 | + */ |
| 43 | +es.Container.prototype.get = function( index ) { |
| 44 | + return this._list[index] || null; |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
38 | 48 | * Gets the first item in the container. |
39 | 49 | * |
40 | 50 | * @method |
Index: trunk/parsers/wikidom/lib/es/es.Selection.js |
— | — | @@ -46,7 +46,7 @@ |
47 | 47 | } else { |
48 | 48 | mutation = new es.Mutation(); |
49 | 49 | } |
50 | | - mutation.add( this.start.block.getId(), this.start.block.prepareInsert( content ) ); |
| 50 | + mutation.add( this.start.block.getIndex(), this.start.block.prepareInsert( content ) ); |
51 | 51 | return mutation; |
52 | 52 | } |
53 | 53 | throw 'Mutation preparation error. Can not insert content at undefined location.'; |
— | — | @@ -58,7 +58,7 @@ |
59 | 59 | if ( this.start.block === this.end.block ) { |
60 | 60 | // Single block deletion |
61 | 61 | mutation.add( |
62 | | - this.start.block.getId(), |
| 62 | + this.start.block.getIndex(), |
63 | 63 | this.start.block.prepareRemove( new es.Range( this.start.offset, this.end.offset ) ) |
64 | 64 | ); |
65 | 65 | } else { |
— | — | @@ -66,17 +66,17 @@ |
67 | 67 | var block = this.start.block.next(); |
68 | 68 | // First |
69 | 69 | mutation.add( |
70 | | - block.getId(), |
| 70 | + block.getIndex(), |
71 | 71 | block.prepareRemove( new es.Range( this.start.offset, block.getLength() ) ) |
72 | 72 | ); |
73 | 73 | while ( ( block = block.next() ) !== this.end.block ) { |
74 | 74 | // Middle |
75 | 75 | mutation.add( |
76 | | - block.getId(), block.prepareRemove( new es.Range( 0, block.getLength() ) ) |
| 76 | + block.getIndex(), block.prepareRemove( new es.Range( 0, block.getLength() ) ) |
77 | 77 | ); |
78 | 78 | } |
79 | 79 | // Last |
80 | | - mutation.add( block.getId(), block.prepareRemove( new es.Range( 0, this.end.offset ) ) ); |
| 80 | + mutation.add( block.getIndex(), block.prepareRemove( new es.Range( 0, this.end.offset ) ) ); |
81 | 81 | } |
82 | 82 | return mutation; |
83 | 83 | }; |
— | — | @@ -87,7 +87,7 @@ |
88 | 88 | if ( this.start.block === this.end.block ) { |
89 | 89 | // Single block annotation |
90 | 90 | mutation.add( |
91 | | - this.start.block.getId(), |
| 91 | + this.start.block.getIndex(), |
92 | 92 | this.start.block.prepareRemove( new es.Range( this.start.offset, this.end.offset ) ) |
93 | 93 | ); |
94 | 94 | } else { |
— | — | @@ -95,7 +95,7 @@ |
96 | 96 | var block = this.start.block.next(); |
97 | 97 | // First |
98 | 98 | mutation.add( |
99 | | - block.getId(), |
| 99 | + block.getIndex(), |
100 | 100 | block.prepareAnnotate( |
101 | 101 | annotation, new es.Range( this.start.offset, block.getLength() ) |
102 | 102 | ) |
— | — | @@ -103,13 +103,14 @@ |
104 | 104 | while ( ( block = block.next() ) !== this.end.block ) { |
105 | 105 | // Middle |
106 | 106 | mutation.add( |
107 | | - block.getId(), |
| 107 | + block.getIndex(), |
108 | 108 | block.prepareAnnotate( annotation, new es.Range( 0, block.getLength() ) ) |
109 | 109 | ); |
110 | 110 | } |
111 | 111 | // Last |
112 | 112 | mutation.add( |
113 | | - block.getId(), block.prepareAnnotate( annotation, new es.Range( 0, this.end.offset ) ) |
| 113 | + block.getIndex(), |
| 114 | + block.prepareAnnotate( annotation, new es.Range( 0, this.end.offset ) ) |
114 | 115 | ); |
115 | 116 | } |
116 | 117 | return mutation; |
Index: trunk/parsers/wikidom/lib/es/es.Block.js |
— | — | @@ -125,6 +125,15 @@ |
126 | 126 | }; |
127 | 127 | |
128 | 128 | /** |
| 129 | + * Deletes all content in a block. |
| 130 | + * |
| 131 | + * @method |
| 132 | + */ |
| 133 | +es.Block.prototype.clearContent = function() { |
| 134 | + throw 'Block.deleteContent not implemented in this subclass.'; |
| 135 | +}; |
| 136 | + |
| 137 | +/** |
129 | 138 | * Applies an annotation to a given range. |
130 | 139 | * |
131 | 140 | * If a range arguments are not provided, all content will be annotated. |