Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit d05db3c

Browse files
committed
Merge branch 'master' into t/40
2 parents fd8b1b8 + db1cd82 commit d05db3c

14 files changed

+141
-88
lines changed

src/commands/mergecellcommand.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,26 @@ function getVerticalCell( tableCell, direction ) {
158158
return;
159159
}
160160

161-
const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
162-
const mergeRow = direction == 'down' ? rowIndex + rowspan : rowIndex;
161+
const currentCellRowSpan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
162+
const rowOfCellToMerge = direction == 'down' ? rowIndex + currentCellRowSpan : rowIndex;
163163

164-
const tableMap = [ ...new TableWalker( table, { endRow: mergeRow } ) ];
164+
const tableMap = [ ...new TableWalker( table, { endRow: rowOfCellToMerge } ) ];
165165

166166
const currentCellData = tableMap.find( value => value.cell === tableCell );
167167
const mergeColumn = currentCellData.column;
168168

169-
const cellToMergeData = tableMap.find( ( { row, column } ) => {
170-
return column === mergeColumn && ( direction == 'down' ? mergeRow === row : mergeRow === rowspan + row );
169+
const cellToMergeData = tableMap.find( ( { row, rowspan, column } ) => {
170+
if ( column !== mergeColumn ) {
171+
return false;
172+
}
173+
174+
if ( direction == 'down' ) {
175+
// If merging a cell below the mergeRow is already calculated.
176+
return row === rowOfCellToMerge;
177+
} else {
178+
// If merging a cell above calculate if it spans to mergeRow.
179+
return rowOfCellToMerge === row + rowspan;
180+
}
171181
} );
172182

173183
return cellToMergeData && cellToMergeData.cell;

src/table.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import TableEditing from './tableediting';
1313
import TableUI from './tableui';
1414
import Widget from '@ckeditor/ckeditor5-widget/src/widget';
1515

16+
import '../theme/table.css';
17+
1618
/**
1719
* The table plugin.
1820
*

src/tableediting.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import SetHeaderColumnCommand from './commands/setheadercolumncommand';
3333
import { getParentTable } from './commands/utils';
3434
import TableUtils from './tableutils';
3535

36-
import './../theme/table.css';
36+
import '../theme/tableediting.css';
3737

3838
/**
3939
* The table editing feature.

src/tabletoolbar.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ export default class TableToolbar extends Plugin {
112112
if ( !editor.ui.focusTracker.isFocused ) {
113113
this._hideToolbar();
114114
} else {
115-
const viewSeleciton = editor.editing.view.document.selection;
115+
const viewSelection = editor.editing.view.document.selection;
116116

117-
if ( isTableWidgetSelected( viewSeleciton ) || isTableContentSelected( viewSeleciton ) ) {
117+
if ( isTableContentSelected( viewSelection ) ) {
118118
this._showToolbar();
119119
} else {
120120
this._hideToolbar();

src/tableui.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export default class TableUI extends Plugin {
7575
editor.ui.componentFactory.add( 'tableColumn', locale => {
7676
const options = [
7777
{ commandName: 'setColumnHeader', label: t( 'Header column' ), bindIsActive: true },
78+
'|',
7879
{ commandName: 'insertColumnBefore', label: t( 'Insert column before' ) },
7980
{ commandName: 'insertColumnAfter', label: t( 'Insert column after' ) },
8081
{ commandName: 'removeColumn', label: t( 'Delete column' ) }
@@ -86,6 +87,7 @@ export default class TableUI extends Plugin {
8687
editor.ui.componentFactory.add( 'tableRow', locale => {
8788
const options = [
8889
{ commandName: 'setRowHeader', label: t( 'Header row' ), bindIsActive: true },
90+
'|',
8991
{ commandName: 'insertRowBelow', label: t( 'Insert row below' ) },
9092
{ commandName: 'insertRowAbove', label: t( 'Insert row above' ) },
9193
{ commandName: 'removeRow', label: t( 'Delete row' ) }
@@ -100,6 +102,7 @@ export default class TableUI extends Plugin {
100102
{ commandName: 'mergeCellRight', label: t( 'Merge cell right' ) },
101103
{ commandName: 'mergeCellDown', label: t( 'Merge cell down' ) },
102104
{ commandName: 'mergeCellLeft', label: t( 'Merge cell left' ) },
105+
'|',
103106
{ commandName: 'splitCellVertically', label: t( 'Split cell vertically' ) },
104107
{ commandName: 'splitCellHorizontally', label: t( 'Split cell horizontally' ) }
105108
];
@@ -161,20 +164,28 @@ export default class TableUI extends Plugin {
161164
// @param {Array.<module:core/command~Command>} commands List of commands to update.
162165
// @param {module:utils/collection~Collection} dropdownItems Collection of dropdown items to update with given option.
163166
function addListOption( option, editor, commands, dropdownItems ) {
164-
const { commandName, label, bindIsActive } = option;
165-
const command = editor.commands.get( commandName );
167+
const itemModel = new Model();
166168

167-
commands.push( command );
169+
if ( option === '|' ) {
170+
itemModel.set( {
171+
isSeparator: true
172+
} );
173+
} else {
174+
const { commandName, label, bindIsActive } = option;
175+
const command = editor.commands.get( commandName );
176+
177+
commands.push( command );
168178

169-
const itemModel = new Model( {
170-
commandName,
171-
label
172-
} );
179+
itemModel.set( {
180+
commandName,
181+
label
182+
} );
173183

174-
itemModel.bind( 'isEnabled' ).to( command );
184+
itemModel.bind( 'isEnabled' ).to( command );
175185

176-
if ( bindIsActive ) {
177-
itemModel.bind( 'isActive' ).to( command, 'value' );
186+
if ( bindIsActive ) {
187+
itemModel.bind( 'isActive' ).to( command, 'value' );
188+
}
178189
}
179190

180191
dropdownItems.add( itemModel );

src/ui/inserttableview.js

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export default class InsertTableView extends View {
8989
],
9090

9191
on: {
92+
mousedown: bind.to( evt => {
93+
evt.preventDefault();
94+
} ),
95+
9296
click: bind.to( () => {
9397
this.fire( 'execute' );
9498
} )

src/ui/utils.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
1111
import { getParentTable } from '../commands/utils';
12-
import { isTableWidgetSelected } from '../utils';
1312

1413
/**
1514
* A helper utility that positions the
@@ -36,13 +35,8 @@ export function getBalloonPositionData( editor ) {
3635
const editingView = editor.editing.view;
3736
const defaultPositions = BalloonPanelView.defaultPositions;
3837
const viewSelection = editingView.document.selection;
39-
let parentTable;
4038

41-
if ( isTableWidgetSelected( viewSelection ) ) {
42-
parentTable = viewSelection.getSelectedElement();
43-
} else {
44-
parentTable = getParentTable( viewSelection.getFirstPosition() );
45-
}
39+
const parentTable = getParentTable( viewSelection.getFirstPosition() );
4640

4741
return {
4842
target: editingView.domConverter.viewToDom( parentTable ),

tests/commands/mergecellcommand.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
7-
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
7+
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
88
import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
99

1010
import MergeCellCommand from '../../src/commands/mergecellcommand';
@@ -483,6 +483,17 @@ describe( 'MergeCellCommand', () => {
483483
expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
484484
} );
485485

486+
it( 'should be set to mergeable cell in rows with spanned cells', () => {
487+
setData( model, modelTable( [
488+
[ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
489+
[ { rowspan: 2, contents: '21' }, '22', '23' ],
490+
[ '32', { rowspan: 2, contents: '33[]' } ],
491+
[ { colspan: 2, contents: '40' }, '42' ]
492+
] ) );
493+
494+
expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
495+
} );
496+
486497
it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
487498
setData( model, modelTable( [
488499
[ { colspan: 2, contents: '00' }, '02' ],
@@ -500,10 +511,10 @@ describe( 'MergeCellCommand', () => {
500511
} );
501512

502513
describe( 'execute()', () => {
503-
it( 'should merge table cells ', () => {
514+
it( 'should merge table cells', () => {
504515
setData( model, modelTable( [
505516
[ '00', '01' ],
506-
[ '10', '11[]' ]
517+
[ '10', '[]11' ]
507518
] ) );
508519

509520
command.execute();
@@ -513,6 +524,24 @@ describe( 'MergeCellCommand', () => {
513524
[ '10' ]
514525
] ) );
515526
} );
527+
528+
it( 'should properly merge cells in rows with spaned cells', () => {
529+
setData( model, modelTable( [
530+
[ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
531+
[ { rowspan: 2, contents: '21' }, '22', '23' ],
532+
[ '32', { rowspan: 2, contents: '33[]' } ],
533+
[ { colspan: 2, contents: '40' }, '42' ]
534+
] ) );
535+
536+
command.execute();
537+
538+
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
539+
[ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
540+
[ { rowspan: 2, contents: '21' }, '22', { rowspan: 3, contents: '[2333]' } ],
541+
[ '32' ],
542+
[ { colspan: 2, contents: '40' }, '42' ]
543+
] ) );
544+
} );
516545
} );
517546
} );
518547
} );

tests/manual/table.html

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
<style>
2-
3-
table {
4-
border-collapse: collapse;
5-
border-spacing: 0;
6-
border-color: hsl(0,0%,87%);
7-
}
8-
9-
table, th, td {
10-
padding: 5px;
11-
border: 1px inset hsl(0,0%,87%);
12-
}
13-
14-
table th,
15-
table td {
16-
min-width: 1em;
17-
}
18-
19-
table th {
20-
background: hsl(0,0%,96%);
21-
font-weight: bold;
22-
}
23-
24-
table thead th {
25-
background: hsl(0,0%,90%);
2+
body {
3+
font-family: Helvetica, Arial, sans-serif;
4+
font-size: 14px;
265
}
276
</style>
287

@@ -47,7 +26,7 @@
4726
</thead>
4827
<tbody>
4928
<tr>
50-
<th colspan="2" rowspan="4" scope="rowgroup">Terrestial planets</th>
29+
<th colspan="2" rowspan="4" scope="rowgroup">Terrestrial planets</th>
5130
<th scope="row">Mercury</th>
5231
<td>0.330</td>
5332
<td>4,879</td>

tests/tabletoolbar.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ describe( 'TableToolbar', () => {
117117
editor.ui.focusTracker.isFocused = true;
118118
} );
119119

120-
it( 'should show the toolbar on render when the table is selected', () => {
120+
it( 'should not show the toolbar on render when the table is selected', () => {
121121
setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
122122

123-
expect( balloon.visibleView ).to.equal( toolbar );
123+
expect( balloon.visibleView ).to.be.null;
124124
} );
125125

126126
it( 'should show the toolbar on render when the table content is selected', () => {

0 commit comments

Comments
 (0)