https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112150
Revision: 112150
Author: tparscal
Date: 2012-02-22 21:23:28 +0000 (Wed, 22 Feb 2012)
Log Message:
-----------
Replaced "set" and "clear" method for attribute transactions with "replace"
method, which allows correct reversion. Also fixed list item tools to correctly
use the new function signature.
Modified Paths:
--------------
trunk/extensions/VisualEditor/modules/ve/dm/nodes/ve.dm.DocumentNode.js
trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.Transaction.js
trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.TransactionProcessor.js
trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.IndentationButtonTool.js
trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.ListButtonTool.js
Modified:
trunk/extensions/VisualEditor/modules/ve/dm/nodes/ve.dm.DocumentNode.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/dm/nodes/ve.dm.DocumentNode.js
2012-02-22 21:21:49 UTC (rev 112149)
+++ trunk/extensions/VisualEditor/modules/ve/dm/nodes/ve.dm.DocumentNode.js
2012-02-22 21:23:28 UTC (rev 112150)
@@ -1175,7 +1175,7 @@
* @method
* @returns {ve.dm.Transaction}
*/
-ve.dm.DocumentNode.prototype.prepareElementAttributeChange = function( offset,
method, key, value ) {
+ve.dm.DocumentNode.prototype.prepareElementAttributeChange = function( offset,
key, to ) {
var tx = new ve.dm.Transaction();
if ( offset ) {
tx.pushRetain( offset );
@@ -1186,7 +1186,8 @@
if ( this.data[offset].type[0] === '/' ) {
throw 'Invalid element offset error. Can not set attributes on
closing element.';
}
- tx.pushChangeElementAttribute( method, key, value );
+ var from = 'attributes' in this.data[offset] ?
this.data[offset].attributes[key] : undefined;
+ tx.pushReplaceElementAttribute( key, from, to );
if ( offset < this.data.length ) {
tx.pushRetain( this.data.length - offset );
}
Modified: trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.Transaction.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.Transaction.js
2012-02-22 21:21:49 UTC (rev 112149)
+++ trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.Transaction.js
2012-02-22 21:23:28 UTC (rev 112150)
@@ -94,14 +94,15 @@
* @method
* @param {String} method Method to use, either "set" or "clear"
* @param {String} key Name of attribute to change
- * @param {Mixed} value Value to set attribute to, or value of attribute being
cleared
+ * @param {Mixed} from Value change attribute from
+ * @param {Mixed} to Value to change attribute to
*/
-ve.dm.Transaction.prototype.pushChangeElementAttribute = function( method,
key, value ) {
+ve.dm.Transaction.prototype.pushReplaceElementAttribute = function( key, from,
to ) {
this.operations.push( {
'type': 'attribute',
- 'method': method,
'key': key,
- 'value': value
+ 'from': from,
+ 'to': to
} );
};
Modified:
trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.TransactionProcessor.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.TransactionProcessor.js
2012-02-22 21:21:49 UTC (rev 112149)
+++ trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.TransactionProcessor.js
2012-02-22 21:23:28 UTC (rev 112150)
@@ -450,13 +450,9 @@
if ( element.type === undefined ) {
throw 'Invalid element error. Can not set attributes on
non-element data.';
}
- if ( ( op.method === 'set' && !invert ) || ( op.method === 'clear' &&
invert ) ) {
- // Automatically initialize attributes object
- if ( !element.attributes ) {
- element.attributes = {};
- }
- element.attributes[op.key] = op.value;
- } else if ( ( op.method === 'clear' && !invert ) || ( op.method ===
'set' && invert ) ) {
+ var to = invert ? op.from : op.to;
+ if ( to === undefined ) {
+ // Clear
if ( element.attributes ) {
delete element.attributes[op.key];
}
@@ -470,7 +466,12 @@
delete element.attributes;
}
} else {
- throw 'Invalid method error. Can not operate attributes this
way: ' + method;
+ // Automatically initialize attributes object
+ if ( !element.attributes ) {
+ element.attributes = {};
+ }
+ // Set
+ element.attributes[op.key] = to;
}
var node = this.model.getNodeFromOffset( this.cursor + 1 );
if ( node.hasChildren() ) {
Modified:
trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.IndentationButtonTool.js
===================================================================
---
trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.IndentationButtonTool.js
2012-02-22 21:21:49 UTC (rev 112149)
+++
trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.IndentationButtonTool.js
2012-02-22 21:23:28 UTC (rev 112150)
@@ -45,12 +45,10 @@
for ( i = 0; i < listItems.length; i++ ) {
styles = listItems[i].getElementAttribute( 'styles' );
if ( styles.length < 6 ) {
- styles.push( styles[styles.length - 1] );
tx =
surface.model.getDocument().prepareElementAttributeChange(
surface.documentView.model.getOffsetFromNode(
listItems[i], false ),
- 'set',
'styles',
- styles
+ styles.concat( styles[styles.length - 1] )
);
surface.model.transact( tx );
}
@@ -66,12 +64,10 @@
for ( i = 0; i < listItems.length; i++ ) {
styles = listItems[i].getElementAttribute( 'styles' );
if ( styles.length > 1 ) {
- styles.splice( styles.length - 1, 1);
tx =
surface.model.getDocument().prepareElementAttributeChange(
surface.documentView.model.getOffsetFromNode(
listItems[i], false ),
- 'set',
'styles',
- styles
+ styles.slice( 0, styles.length - 1 )
);
surface.model.transact( tx );
}
Modified:
trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.ListButtonTool.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.ListButtonTool.js
2012-02-22 21:21:49 UTC (rev 112149)
+++ trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.ListButtonTool.js
2012-02-22 21:23:28 UTC (rev 112150)
@@ -78,21 +78,19 @@
}
}
- for( i = 0; i < listItems.length; i++ ) {
+ for ( i = 0; i < listItems.length; i++ ) {
styles = listItems[i].getElementAttribute( 'styles' );
if ( styles[styles.length - 1] !== style ) {
- styles.splice( styles.length - 1, 1, style );
tx =
surface.model.getDocument().prepareElementAttributeChange(
surface.documentView.model.getOffsetFromNode(
listItems[i], false ),
- 'set',
'styles',
- styles
+ styles.slice( 0, styles.length - 1 ).concat(
style )
);
surface.model.transact( tx );
}
}
- for( i = 0; i < stacks.length; i++ ) {
+ for ( i = 0; i < stacks.length; i++ ) {
removeLength = 0;
insertAt = surface.documentView.model.getOffsetFromNode(
stacks[i][0], false );
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs