Index: trunk/extensions/VisualEditor/tests/ve/ve.dm.DocumentSynchronizer.test.js |
— | — | @@ -9,7 +9,7 @@ |
10 | 10 | // Delete bold "b" from first paragraph |
11 | 11 | model.data.splice( 2, 1 ); |
12 | 12 | // Push resize action |
13 | | - sync.pushAction( 'resize', model.getChildren()[0], 0, -1 ); |
| 13 | + sync.pushResize( model.getChildren()[0], -1 ); |
14 | 14 | // Sync |
15 | 15 | sync.synchronize(); |
16 | 16 | return model.getChildren()[0].getContentLength(); |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | // Insert element after first paragraph |
27 | 27 | ve.insertIntoArray( model.data, 5, data ); |
28 | 28 | // Push insertion action |
29 | | - sync.pushAction( 'insert', node, 5 ); |
| 29 | + sync.pushInsert( node, 5 ); |
30 | 30 | // Sync |
31 | 31 | sync.synchronize(); |
32 | 32 | return model.getChildren()[1].getContentData(); |
— | — | @@ -41,7 +41,7 @@ |
42 | 42 | // Insert element after first paragraph |
43 | 43 | ve.insertIntoArray( model.data, 0, data ); |
44 | 44 | // Push insertion action |
45 | | - sync.pushAction( 'insert', node, 0 ); |
| 45 | + sync.pushInsert( node, 0 ); |
46 | 46 | // Sync |
47 | 47 | sync.synchronize(); |
48 | 48 | return model.getChildren()[0].getContentData(); |
— | — | @@ -57,7 +57,7 @@ |
58 | 58 | // Insert element after first paragraph |
59 | 59 | ve.insertIntoArray( model.data, 34, data ); |
60 | 60 | // Push insertion action |
61 | | - sync.pushAction( 'insert', node, 34 ); |
| 61 | + sync.pushInsert( node, 34 ); |
62 | 62 | // Sync |
63 | 63 | sync.synchronize(); |
64 | 64 | return model.getChildren()[3].getContentData(); |
— | — | @@ -72,7 +72,7 @@ |
73 | 73 | // Delete the table |
74 | 74 | model.data.splice( 5, 26 ); |
75 | 75 | // Push deletion action |
76 | | - sync.pushAction( 'delete', node, 5 ); |
| 76 | + sync.pushDelete( node ); |
77 | 77 | // Sync |
78 | 78 | sync.synchronize(); |
79 | 79 | return model.getChildren().length; |
— | — | @@ -87,7 +87,7 @@ |
88 | 88 | // Delete the first paragraph |
89 | 89 | model.data.splice( 0, 5 ); |
90 | 90 | // Push deletion action |
91 | | - sync.pushAction( 'delete', node, 0 ); |
| 91 | + sync.pushDelete( node ); |
92 | 92 | // Sync |
93 | 93 | sync.synchronize(); |
94 | 94 | return model.getChildren().length; |
— | — | @@ -102,7 +102,7 @@ |
103 | 103 | // Delete the first paragraph |
104 | 104 | model.data.splice( 31, 3 ); |
105 | 105 | // Push deletion action |
106 | | - sync.pushAction( 'delete', node, 31 ); |
| 106 | + sync.pushDelete( node ); |
107 | 107 | // Sync |
108 | 108 | sync.synchronize(); |
109 | 109 | return model.getChildren().length; |
— | — | @@ -119,7 +119,7 @@ |
120 | 120 | model.data[0].attributes = { 'level': 1 }; |
121 | 121 | model.data[4].type = '/heading'; |
122 | 122 | // Push rebuild action |
123 | | - sync.pushAction( 'rebuild', node, 0 ); |
| 123 | + sync.pushRebuild( node, 0 ); |
124 | 124 | // Sync |
125 | 125 | sync.synchronize(); |
126 | 126 | return model.getChildren()[0].getElementType(); |
— | — | @@ -135,7 +135,7 @@ |
136 | 136 | // Insert element after first paragraph |
137 | 137 | ve.insertIntoArray( model.data, 5, data ); |
138 | 138 | // Push rebuild action with a length adustment of 3 to account for the new element |
139 | | - sync.pushAction( 'rebuild', node, 0, 3 ); |
| 139 | + sync.pushRebuild( node, 3 ); |
140 | 140 | // Sync |
141 | 141 | sync.synchronize(); |
142 | 142 | return model.getChildren()[1].getContentData(); |
— | — | @@ -151,15 +151,15 @@ |
152 | 152 | // Delete bold "b" from first paragraph |
153 | 153 | model.data.splice( 2, 1 ); |
154 | 154 | // Push resize action |
155 | | - sync.pushAction( 'resize', model.getChildren()[0], 0, -1 ); |
| 155 | + sync.pushResize( model.getChildren()[0], -1 ); |
156 | 156 | // Delete the first paragraph (offset adjusted for previous action) |
157 | 157 | model.data.splice( 30, 3 ); |
158 | | - // Push deletion action (note: using original offset) |
159 | | - sync.pushAction( 'delete', model.getChildren()[2], 31 ); |
| 158 | + // Push deletion action |
| 159 | + sync.pushDelete( model.getChildren()[2] ); |
160 | 160 | // Insert element after last paragraph |
161 | 161 | ve.insertIntoArray( model.data, 30, data ); |
162 | 162 | // Push insertion action (note: using original offset) |
163 | | - sync.pushAction( 'insert', node, 34 ); |
| 163 | + sync.pushInsert( node, 34 ); |
164 | 164 | // Sync |
165 | 165 | sync.synchronize(); |
166 | 166 | return [ |
Index: trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.DocumentSynchronizer.js |
— | — | @@ -20,26 +20,72 @@ |
21 | 21 | }; |
22 | 22 | |
23 | 23 | /** |
24 | | - * Adds an action to the synchronizer. |
25 | | - * |
26 | | - * @method |
27 | | - * @param {String} type Type of action, can be: "insert", "delete", "rebuild", "resize" or "update" |
28 | | - * @param {ve.dm.Node} node Node this action is related to |
29 | | - * @param {Integer|null} offset Offset of node, improves performance if this has already been calculated. |
30 | | - * Only used for insert and rebuild actions |
31 | | - * @param {Integer} adjustment Node length adjustment, if any |
| 24 | + * Add an insert action to the queue |
| 25 | + * @param {ve.dm.BranchNode} node Node to insert |
| 26 | + * @param {Integer} [offset] Offset of the inserted node, if known |
32 | 27 | */ |
33 | | -ve.dm.DocumentSynchronizer.prototype.pushAction = function( type, node, offset, adjustment ) { |
| 28 | +ve.dm.DocumentSynchronizer.prototype.pushInsert = function( node, offset ) { |
34 | 29 | this.actions.push( { |
35 | | - 'type': type, |
| 30 | + 'type': 'insert', |
36 | 31 | 'node': node, |
37 | | - 'offset': offset, |
38 | | - 'adjustment': adjustment || 0 |
| 32 | + 'offset': offset || null |
39 | 33 | } ); |
40 | 34 | }; |
41 | 35 | |
42 | 36 | /** |
43 | | - * Applies queued actions to the model tree. |
| 37 | + * Add a delete action to the queue |
| 38 | + * @param {ve.dm.BranchNode} node Node to delete |
| 39 | + */ |
| 40 | +ve.dm.DocumentSynchronizer.prototype.pushDelete = function( node ) { |
| 41 | + this.actions.push( { |
| 42 | + 'type': 'delete', |
| 43 | + 'node': node |
| 44 | + } ); |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Add a rebuild action to the queue. This rebuilds a node from data |
| 49 | + * found in the linear model. |
| 50 | + * @param {ve.dm.BranchNode} node Node to rebuild |
| 51 | + * @param {Integer} adjustment Length adjustment to apply to the node |
| 52 | + * @param {Integer} offset Offset of the node, if known |
| 53 | + */ |
| 54 | +ve.dm.DocumentSynchronizer.prototype.pushRebuild = function( node, adjustment, offset ) { |
| 55 | + this.actions.push( { |
| 56 | + 'type': 'rebuild', |
| 57 | + 'node': node, |
| 58 | + 'adjustment': adjustment, |
| 59 | + 'offset': offset || null |
| 60 | + } ); |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Add a resize action to the queue. This changes the content length of a leaf node. |
| 65 | + * @param {ve.dm.BranchNode} node Node to resize |
| 66 | + * @param {Integer} adjustment Length adjustment to apply to the node |
| 67 | + */ |
| 68 | +ve.dm.DocumentSynchronizer.prototype.pushResize = function( node, adjustment ) { |
| 69 | + this.actions.push( { |
| 70 | + 'type': 'resize', |
| 71 | + 'node': node, |
| 72 | + 'adjustment': adjustment |
| 73 | + } ); |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Add an update action to the queue |
| 78 | + * @param {ve.dm.BranchNode} node Node to update |
| 79 | + */ |
| 80 | +ve.dm.DocumentSynchronizer.prototype.pushUpdate = function( node ) { |
| 81 | + this.actions.push( { |
| 82 | + 'type': 'update', |
| 83 | + 'node': node |
| 84 | + } ); |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Apply queued actions to the model tree. This assumes that the linear model |
| 89 | + * has already been updated, but the model tree has not yet been. |
44 | 90 | * |
45 | 91 | * @method |
46 | 92 | */ |
— | — | @@ -51,7 +97,7 @@ |
52 | 98 | parent; |
53 | 99 | for ( var i = 0, len = this.actions.length; i < len; i++ ) { |
54 | 100 | action = this.actions[i]; |
55 | | - offset = action.offset; |
| 101 | + offset = action.offset || null; |
56 | 102 | switch ( action.type ) { |
57 | 103 | case 'insert': |
58 | 104 | // Compute the offset if it wasn't provided |