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

Commit

Permalink
Merge pull request #295 from ckeditor/i/6500
Browse files Browse the repository at this point in the history
Feature: "Select entire column/row" added to table column and row dropdowns. Closes ckeditor/ckeditor5#6500.
  • Loading branch information
jodator authored Apr 6, 2020
2 parents 0e0e6fe + 302526b commit 729cc00
Show file tree
Hide file tree
Showing 12 changed files with 1,139 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/features/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ The {@link module:table/tabletoolbar~TableToolbar} plugin introduces two balloon
<td><code>'removeTableRow'</code></td>
<td>{@link module:table/commands/removerowcommand~RemoveRowCommand}</td>
</tr>
<tr>
<td><code>'selectTableColumn'</code></td>
<td>{@link module:table/commands/selectcolumncommand~SelectColumnCommand}</td>
</tr>
<tr>
<td><code>'selectTableRow'</code></td>
<td>{@link module:table/commands/selectrowcommand~SelectRowCommand}</td>
</tr>
<tr>
<td><code>'setTableColumnHeader'</code></td>
<td>{@link module:table/commands/setheadercolumncommand~SetHeaderColumnCommand}</td>
Expand Down
2 changes: 2 additions & 0 deletions lang/contexts.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"Insert column left": "Label for the insert table column to the left of the current one button.",
"Insert column right": "Label for the insert table column to the right of the current one button.",
"Delete column": "Label for the delete table column button.",
"Select column": "Label for the select table entire column button.",
"Column": "Label for the table column dropdown button.",
"Header row": "Label for the set/unset table header row button.",
"Insert row below": "Label for the insert row below button.",
"Insert row above": "Label for the insert row above button.",
"Delete row": "Label for the delete table row button.",
"Select row": "Label for the select table entire row button.",
"Row": "Label for the table row dropdown button.",
"Merge cell up": "Label for the merge table cell up button.",
"Merge cell right": "Label for the merge table cell right button.",
Expand Down
65 changes: 65 additions & 0 deletions src/commands/selectcolumncommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module table/commands/selectcolumncommand
*/

import Command from '@ckeditor/ckeditor5-core/src/command';

import TableWalker from '../tablewalker';
import { findAncestor } from './utils';
import { getSelectionAffectedTableCells } from '../utils';

/**
* The select column command.
*
* The command is registered by {@link module:table/tableediting~TableEditing} as the `'selectTableColumn'` editor command.
*
* To select the columns containing the selected cells, execute the command:
*
* editor.execute( 'selectTableColumn' );
*
* @extends module:core/command~Command
*/
export default class SelectColumnCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );

this.isEnabled = selectedCells.length > 0;
}

/**
* @inheritDoc
*/
execute() {
const model = this.editor.model;
const referenceCells = getSelectionAffectedTableCells( model.document.selection );
const firstCell = referenceCells[ 0 ];
const lastCell = referenceCells.pop();

const tableUtils = this.editor.plugins.get( 'TableUtils' );
const startLocation = tableUtils.getCellLocation( firstCell );
const endLocation = tableUtils.getCellLocation( lastCell );

const startColumn = Math.min( startLocation.column, endLocation.column );
const endColumn = Math.max( startLocation.column, endLocation.column );

const rangesToSelect = [];

for ( const cellInfo of new TableWalker( findAncestor( 'table', firstCell ) ) ) {
if ( cellInfo.column >= startColumn && cellInfo.column <= endColumn ) {
rangesToSelect.push( model.createRangeOn( cellInfo.cell ) );
}
}

model.change( writer => {
writer.setSelection( rangesToSelect );
} );
}
}
57 changes: 57 additions & 0 deletions src/commands/selectrowcommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module table/commands/selectrowcommand
*/

import Command from '@ckeditor/ckeditor5-core/src/command';

import { findAncestor } from './utils';
import { getRowIndexes, getSelectionAffectedTableCells } from '../utils';

/**
* The select row command.
*
* The command is registered by {@link module:table/tableediting~TableEditing} as the `'selectTableRow'` editor command.
*
* To select the rows containing the selected cells, execute the command:
*
* editor.execute( 'selectTableRow' );
*
* @extends module:core/command~Command
*/
export default class SelectRowCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );

this.isEnabled = selectedCells.length > 0;
}

/**
* @inheritDoc
*/
execute() {
const model = this.editor.model;
const referenceCells = getSelectionAffectedTableCells( model.document.selection );
const rowIndexes = getRowIndexes( referenceCells );

const table = findAncestor( 'table', referenceCells[ 0 ] );
const rangesToSelect = [];

for ( let rowIndex = rowIndexes.first; rowIndex <= rowIndexes.last; rowIndex++ ) {
for ( const cell of table.getChild( rowIndex ).getChildren() ) {
rangesToSelect.push( model.createRangeOn( cell ) );
}
}

model.change( writer => {
writer.setSelection( rangesToSelect );
} );
}
}
5 changes: 5 additions & 0 deletions src/tableediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import RemoveColumnCommand from './commands/removecolumncommand';
import SetHeaderRowCommand from './commands/setheaderrowcommand';
import SetHeaderColumnCommand from './commands/setheadercolumncommand';
import MergeCellsCommand from './commands/mergecellscommand';
import SelectRowCommand from './commands/selectrowcommand';
import SelectColumnCommand from './commands/selectcolumncommand';
import { getTableCellsContainingSelection } from './utils';
import TableUtils from '../src/tableutils';

Expand Down Expand Up @@ -142,6 +144,9 @@ export default class TableEditing extends Plugin {
editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );

editor.commands.add( 'selectTableRow', new SelectRowCommand( editor ) );
editor.commands.add( 'selectTableColumn', new SelectColumnCommand( editor ) );

injectTableLayoutPostFixer( model );
injectTableCellRefreshPostFixer( model );
injectTableCellParagraphPostFixer( model );
Expand Down
14 changes: 14 additions & 0 deletions src/tableui.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ export default class TableUI extends Plugin {
commandName: 'removeTableColumn',
label: t( 'Delete column' )
}
},
{
type: 'button',
model: {
commandName: 'selectTableColumn',
label: t( 'Select column' )
}
}
];

Expand Down Expand Up @@ -150,6 +157,13 @@ export default class TableUI extends Plugin {
commandName: 'removeTableRow',
label: t( 'Delete row' )
}
},
{
type: 'button',
model: {
commandName: 'selectTableRow',
label: t( 'Select row' )
}
}
];

Expand Down
2 changes: 1 addition & 1 deletion src/tableutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class TableUtils extends Plugin {
* | | f | g | | | | |
* 3 +---+---+---+ + +---+---+ 3
* | | d | e |
* +---+---+---+ 4
* + +---+---+ 4
* + + f | g |
* +---+---+---+ 5
*
Expand Down
37 changes: 37 additions & 0 deletions tests/_utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,43 @@ export function assertTableCellStyle( editor, tableCellStyle ) {
* The above call will check if table has second column selected (assuming no spans).
*
* **Note**: This function operates on child indexes - not rows/columns.
*
* Examples:
*
* +----+----+----+----+
* | 00 | 01 | 02 | 03 |
* +----+ +----+----+
* |[10]| |[12]|[13]|
* +----+----+----+----+
* | 20 | 21 | 22 | 23 |
* +----+----+----+----+
* | 30 | 31 | 33 |
* +----+----+----+----+
*
* assertSelectedCells( model, [
* [ 0, 0, 0, 0 ],
* [ 1, 1, 1 ],
* [ 0, 0, 0, 0 ],
* [ 0, 0, 0 ]
* ] );
*
* +----+----+----+----+
* | 00 |[01]| 02 | 03 |
* +----+ +----+----+
* | 10 | | 12 | 13 |
* +----+----+----+----+
* | 20 |[21]| 22 | 23 |
* +----+----+----+----+
* | 30 |[31] | 33 |
* +----+----+----+----+
*
* assertSelectedCells( model, [
* [ 0, 1, 0, 0 ],
* [ 0, 0, 0 ],
* [ 0, 1, 0, 0 ],
* [ 0, 1, 0 ]
* ] );
*
*/
export function assertSelectedCells( model, tableMap ) {
const tableIndex = 0;
Expand Down
Loading

0 comments on commit 729cc00

Please sign in to comment.