Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/264290

Change subject: TableAction: Use first non-empty cell data when merging
......................................................................

TableAction: Use first non-empty cell data when merging

When merging [_|B] use the data from the first non-empty
cell ('B') instead of just the first cell. This allows users
to merge data 'up' and 'left' as well as 'down' and 'right'.

Change-Id: Iff4981e92f754d79fb78cd819dc0794eca2364df
---
M src/ui/actions/ve.ui.TableAction.js
M tests/ui/actions/ve.ui.TableAction.test.js
2 files changed, 47 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/90/264290/1

diff --git a/src/ui/actions/ve.ui.TableAction.js 
b/src/ui/actions/ve.ui.TableAction.js
index df9b329..a5811a3 100644
--- a/src/ui/actions/ve.ui.TableAction.js
+++ b/src/ui/actions/ve.ui.TableAction.js
@@ -192,9 +192,11 @@
  * @return {boolean} Action was executed
  */
 ve.ui.TableAction.prototype.mergeCells = function () {
-       var i, r, c, cell, cells, hasNonPlaceholders,
+       var i, l, r, c, cell, cells, hasNonPlaceholders,
+               contentData, contentRange,
                txs = [],
                surfaceModel = this.surface.getModel(),
+               documentModel = surfaceModel.getDocument(),
                selection = surfaceModel.getSelection(),
                matrix = selection.getTableNode().getMatrix();
 
@@ -207,7 +209,7 @@
                cells = selection.getMatrixCells( true );
                txs.push(
                        ve.dm.Transaction.newFromAttributeChanges(
-                               surfaceModel.getDocument(), cells[ 0 
].node.getOuterRange().start,
+                               documentModel, cells[ 0 
].node.getOuterRange().start,
                                { colspan: 1, rowspan: 1 }
                        )
                );
@@ -226,17 +228,39 @@
                cells = selection.getMatrixCells();
                txs.push(
                        ve.dm.Transaction.newFromAttributeChanges(
-                               surfaceModel.getDocument(), cells[ 0 
].node.getOuterRange().start,
+                               documentModel, cells[ 0 
].node.getOuterRange().start,
                                {
                                        colspan: 1 + selection.endCol - 
selection.startCol,
                                        rowspan: 1 + selection.endRow - 
selection.startRow
                                }
                        )
                );
+               // Find first cell with content
+               for ( i = 0, l = cells.length; i < l; i++ ) {
+                       contentData = new ve.dm.ElementLinearData(
+                               documentModel.getStore(),
+                               documentModel.getData( cells[ i 
].node.getRange() )
+                       );
+                       if ( contentData.hasContent() ) {
+                               // If the first cell contains content, we don't 
need to move any content
+                               if ( !i ) {
+                                       contentData = null;
+                               }
+                               break;
+                       }
+               }
                for ( i = cells.length - 1; i >= 1; i-- ) {
                        txs.push(
                                ve.dm.Transaction.newFromRemoval(
-                                       surfaceModel.getDocument(), cells[ i 
].node.getOuterRange()
+                                       documentModel, cells[ i 
].node.getOuterRange()
+                               )
+                       );
+               }
+               // Move the first-found content to the merged cell
+               if ( contentData ) {
+                       txs.push(
+                               ve.dm.Transaction.newFromReplacement(
+                                       documentModel, cells[ 0 
].node.getRange(), contentData.data
                                )
                        );
                }
diff --git a/tests/ui/actions/ve.ui.TableAction.test.js 
b/tests/ui/actions/ve.ui.TableAction.test.js
index 3a7b238..aa1f70c 100644
--- a/tests/ui/actions/ve.ui.TableAction.test.js
+++ b/tests/ui/actions/ve.ui.TableAction.test.js
@@ -332,6 +332,25 @@
                                msg: 'merge full columns'
                        },
                        {
+                               html: 
'<table><tr><td></td><td>A</td></tr><tr><td></td><td></td></tr></table>',
+                               selection: {
+                                       type: 'table',
+                                       tableRange: new ve.Range( 0, 25 ),
+                                       fromCol: 0,
+                                       fromRow: 0,
+                                       toCol: 1,
+                                       toRow: 0
+                               },
+                               method: 'mergeCells',
+                               args: [],
+                               expectedData: function ( data ) {
+                                       data.splice( 3, 4 );
+                                       data[ 3 ].attributes.colspan = 2;
+                                       data[ 3 ].attributes.rowspan = 1;
+                               },
+                               msg: 'merge when first cell is empty'
+                       },
+                       {
                                html: ve.dm.example.mergedCellsHtml,
                                selection: {
                                        type: 'table',

-- 
To view, visit https://gerrit.wikimedia.org/r/264290
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iff4981e92f754d79fb78cd819dc0794eca2364df
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to