Divec has uploaded a new change for review.
https://gerrit.wikimedia.org/r/317330
Change subject: Fix translateRange bug that expands selections
......................................................................
Fix translateRange bug that expands selections
Change-Id: Ib93e88afe8a4be61fa3306db87d6f51f6f495198
---
M src/dm/selections/ve.dm.LinearSelection.js
M src/dm/selections/ve.dm.NullSelection.js
M src/dm/selections/ve.dm.TableSelection.js
M src/dm/ve.dm.Selection.js
M src/dm/ve.dm.Surface.js
M src/dm/ve.dm.Transaction.js
M tests/dm/ve.dm.Surface.test.js
7 files changed, 63 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/30/317330/1
diff --git a/src/dm/selections/ve.dm.LinearSelection.js
b/src/dm/selections/ve.dm.LinearSelection.js
index e97d81c..4dc4251 100644
--- a/src/dm/selections/ve.dm.LinearSelection.js
+++ b/src/dm/selections/ve.dm.LinearSelection.js
@@ -106,6 +106,13 @@
/**
* @inheritdoc
*/
+ve.dm.Selection.prototype.translateByTransactionWithBias = function ( tx, bias
) {
+ return new this.constructor( this.getDocument(),
tx.translateRangeWithBias( this.getRange(), bias ) );
+};
+
+/**
+ * @inheritdoc
+ */
ve.dm.LinearSelection.prototype.getRanges = function () {
return [ this.range ];
};
diff --git a/src/dm/selections/ve.dm.NullSelection.js
b/src/dm/selections/ve.dm.NullSelection.js
index 7f0a370..aa34916 100644
--- a/src/dm/selections/ve.dm.NullSelection.js
+++ b/src/dm/selections/ve.dm.NullSelection.js
@@ -73,6 +73,8 @@
ve.dm.NullSelection.prototype.translateByTransaction =
ve.dm.NullSelection.prototype.clone;
+ve.dm.NullSelection.prototype.translateByTransactionWithBias =
ve.dm.NullSelection.prototype.clone;
+
/**
* @inheritdoc
*/
diff --git a/src/dm/selections/ve.dm.TableSelection.js
b/src/dm/selections/ve.dm.TableSelection.js
index def74f1..ad13733 100644
--- a/src/dm/selections/ve.dm.TableSelection.js
+++ b/src/dm/selections/ve.dm.TableSelection.js
@@ -303,6 +303,21 @@
};
/**
+ * @inheritdoc
+ */
+ve.dm.TableSelection.prototype.translateByTransactionWithBias = function ( tx,
bias ) {
+ var newRange = tx.translateRangeWithBias( this.tableRange, bias );
+
+ if ( newRange.isCollapsed() ) {
+ return new ve.dm.NullSelection( this.getDocument() );
+ }
+ return new this.constructor(
+ this.getDocument(), newRange,
+ this.fromCol, this.fromRow, this.toCol, this.toRow
+ );
+};
+
+/**
* Check if the selection spans a single cell
*
* @return {boolean} The selection spans a single cell
diff --git a/src/dm/ve.dm.Selection.js b/src/dm/ve.dm.Selection.js
index dc80892..92b519e 100644
--- a/src/dm/ve.dm.Selection.js
+++ b/src/dm/ve.dm.Selection.js
@@ -140,6 +140,17 @@
ve.dm.Selection.prototype.translateByTransaction = null;
/**
+ * Apply translations from a transaction
+ *
+ * @abstract
+ * @method
+ * @param {ve.dm.Transaction} tx Transaction
+ * @param {string} [bias] The bias, forward|backward
+ * @return {ve.dm.Selection} A new translated selection
+ */
+ve.dm.Selection.prototype.translateByTransactionWithBias = null;
+
+/**
* Apply translations from a set of transactions
*
* @param {ve.dm.Transaction[]} txs Transactions
diff --git a/src/dm/ve.dm.Surface.js b/src/dm/ve.dm.Surface.js
index 7e6ac5f..716c5fe 100644
--- a/src/dm/ve.dm.Surface.js
+++ b/src/dm/ve.dm.Surface.js
@@ -931,7 +931,7 @@
* @fires documentUpdate
*/
ve.dm.Surface.prototype.onDocumentTransact = function ( tx ) {
- this.setSelection( this.getSelection().translateByTransaction( tx ) );
+ this.setSelection( this.getSelection().translateByTransactionWithBias(
tx, 'backward' ) );
this.emit( 'documentUpdate', tx );
};
diff --git a/src/dm/ve.dm.Transaction.js b/src/dm/ve.dm.Transaction.js
index 36c05a2..c811b2b 100644
--- a/src/dm/ve.dm.Transaction.js
+++ b/src/dm/ve.dm.Transaction.js
@@ -1114,7 +1114,7 @@
};
/**
- * Translate a range based on a transaction.
+ * Translate a range based on the transaction, with grow/shrink preference at
changes
*
* This is useful when you want to anticipate what a selection will be after a
transaction is
* processed.
@@ -1133,6 +1133,20 @@
};
/**
+ * Translate a range based on the transaction, with forward/backward
preference at changes
+ *
+ * @see #translateOffset
+ * @param {ve.Range} range Range in the linear model before the transaction
has been processed
+ * @param {string} bias Preference for moving range boundaries at insertions:
forward|backward
+ * @return {ve.Range} Translated range, as it will be after processing
transaction
+ */
+ve.dm.Transaction.prototype.translateRangeWithBias = function ( range, bias ) {
+ var start = this.translateOffset( range.start, bias === 'backward' ),
+ end = this.translateOffset( range.end, bias === 'backward' );
+ return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range(
start, end );
+};
+
+/**
* Get the range that covers modifications made by this transaction.
*
* In the case of insertions, the range covers content the user intended to
insert.
diff --git a/tests/dm/ve.dm.Surface.test.js b/tests/dm/ve.dm.Surface.test.js
index 48cd63f..5a4ad57 100644
--- a/tests/dm/ve.dm.Surface.test.js
+++ b/tests/dm/ve.dm.Surface.test.js
@@ -95,6 +95,18 @@
} );
+QUnit.test( 'range translation', 2, function ( assert ) {
+ var sel, range,
+ surface = new ve.dm.SurfaceStub( null, new ve.Range( 3 ) ),
+ doc = surface.getDocument(),
+ tx = ve.dm.Transaction.newFromInsertion( doc, 3, [ 'x' ] );
+ surface.change( tx );
+ sel = surface.getSelection();
+ assert.ok( sel instanceof ve.dm.LinearSelection, 'Selection is linear'
);
+ range = sel.getRange();
+ assert.deepEqual( { from: range.from, to: range.to }, { from: 3, to: 3
}, 'Cursor is unmoved' );
+} );
+
QUnit.test( 'staging', 37, function ( assert ) {
var tx1, tx2,
surface = new ve.dm.SurfaceStub( null, new ve.Range( 1, 3 ) ),
--
To view, visit https://gerrit.wikimedia.org/r/317330
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib93e88afe8a4be61fa3306db87d6f51f6f495198
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Divec <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits