Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed insert table row/column commands when a widget is selected inside the table cell #6752

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/ckeditor5-table/src/commands/insertcolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ export default class InsertColumnCommand extends Command {
const referencePosition = insertBefore ? selection.getFirstPosition() : selection.getLastPosition();
const referenceRange = insertBefore ? selection.getFirstRange() : selection.getLastRange();

const tableCell = referenceRange.getContainedElement() || findAncestor( 'tableCell', referencePosition );
const table = tableCell.parent.parent;
const containedElement = referenceRange.getContainedElement();
const isTableCell = containedElement && containedElement.is( 'tableCell' );

const tableCell = isTableCell ? containedElement : findAncestor( 'tableCell', referencePosition );
const table = findAncestor( 'table', tableCell );

const { column } = tableUtils.getCellLocation( tableCell );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the whole thing to:

		const affectedTableCells = getSelectionAffectedTableCells( selection );
		const columnIndexes = getColumnIndexes( affectedTableCells );

		const column = insertBefore ? columnIndexes.first : columnIndexes.last;
		const table = findAncestor( 'table', affectedTableCells[ 0 ] );

if the tests aren't too simplistic it works 🤷‍♂️.

AFAICS the same thing can be done with inserting rows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I would see that change is that it looks like this code tried to copy utility methods behavior that we already have. Using utility method for extracting this data makes me safe that we do it properly.


Expand Down
5 changes: 4 additions & 1 deletion packages/ckeditor5-table/src/commands/insertrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export default class InsertRowCommand extends Command {
const referencePosition = insertAbove ? selection.getFirstPosition() : selection.getLastPosition();
const referenceRange = insertAbove ? selection.getFirstRange() : selection.getLastRange();

const tableCell = referenceRange.getContainedElement() || findAncestor( 'tableCell', referencePosition );
const containedElement = referenceRange.getContainedElement();
const isTableCell = containedElement && containedElement.is( 'tableCell' );
const tableCell = isTableCell ? containedElement : findAncestor( 'tableCell', referencePosition );

const tableRow = tableCell.parent;
const table = tableRow.parent;

Expand Down
19 changes: 18 additions & 1 deletion packages/ckeditor5-table/tests/commands/insertcolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

import InsertColumnCommand from '../../src/commands/insertcolumncommand';
Expand All @@ -18,7 +19,7 @@ describe( 'InsertColumnCommand', () => {
beforeEach( () => {
return ModelTestEditor
.create( {
plugins: [ TableUtils, TableSelection ]
plugins: [ TableUtils, TableSelection, HorizontalLineEditing ]
} )
.then( newEditor => {
editor = newEditor;
Expand Down Expand Up @@ -181,6 +182,22 @@ describe( 'InsertColumnCommand', () => {
[ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
], { headingColumns: 5 } ) );
} );

it( 'should insert a column when a widget in the table cell is selected', () => {
setData( model, modelTable( [
[ '11', '12' ],
[ '21', '22' ],
[ '31', '[<horizontalLine></horizontalLine>]' ]
] ) );

command.execute();

assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
[ '11', '12', '' ],
[ '21', '22', '' ],
[ '31', '<horizontalLine></horizontalLine>', '' ]
] ) );
} );
} );
} );

Expand Down
20 changes: 19 additions & 1 deletion packages/ckeditor5-table/tests/commands/insertrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

import InsertRowCommand from '../../src/commands/insertrowcommand';
Expand All @@ -18,7 +19,7 @@ describe( 'InsertRowCommand', () => {
beforeEach( () => {
return ModelTestEditor
.create( {
plugins: [ TableUtils, TableSelection ]
plugins: [ TableUtils, TableSelection, HorizontalLineEditing ]
} )
.then( newEditor => {
editor = newEditor;
Expand Down Expand Up @@ -214,6 +215,23 @@ describe( 'InsertRowCommand', () => {
[ 0, 0 ]
] );
} );

it( 'should insert a row when a widget in the table cell is selected', () => {
setData( model, modelTable( [
[ '11', '12' ],
[ '21', '22' ],
[ '31', '[<horizontalLine></horizontalLine>]' ]
] ) );

command.execute();

assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
[ '11', '12' ],
[ '21', '22' ],
[ '31', '<horizontalLine></horizontalLine>' ],
[ '', '' ]
] ) );
} );
} );
} );

Expand Down