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

Commit

Permalink
Merge branch 'master' into i/6403
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Mar 10, 2020
2 parents 9db5e56 + 149399e commit 6d1d492
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
21 changes: 6 additions & 15 deletions src/commands/splitcellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import Command from '@ckeditor/ckeditor5-core/src/command';
import { getTableCellsContainingSelection } from '../utils';
import { getSelectionAffectedTableCells } from '../utils';

/**
* The split cell command.
Expand Down Expand Up @@ -46,29 +46,20 @@ export default class SplitCellCommand extends Command {
* @inheritDoc
*/
refresh() {
const model = this.editor.model;
const doc = model.document;
const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );

const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];

this.isEnabled = !!tableCell;
this.isEnabled = selectedCells.length === 1;
}

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

const tableCell = getTableCellsContainingSelection( selection )[ 0 ];

const isHorizontally = this.direction === 'horizontally';

const tableCell = getSelectionAffectedTableCells( this.editor.model.document.selection )[ 0 ];
const isHorizontal = this.direction === 'horizontally';
const tableUtils = this.editor.plugins.get( 'TableUtils' );

if ( isHorizontally ) {
if ( isHorizontal ) {
tableUtils.splitCellHorizontally( tableCell, 2 );
} else {
tableUtils.splitCellVertically( tableCell, 2 );
Expand Down
33 changes: 32 additions & 1 deletion tests/commands/splitcellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model

import SplitCellCommand from '../../src/commands/splitcellcommand';
import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
import TableSelection from '../../src/tableselection';
import TableUtils from '../../src/tableutils';
import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';

Expand All @@ -17,7 +18,7 @@ describe( 'SplitCellCommand', () => {
beforeEach( () => {
return ModelTestEditor
.create( {
plugins: [ TableUtils ]
plugins: [ TableUtils, TableSelection ]
} )
.then( newEditor => {
editor = newEditor;
Expand Down Expand Up @@ -47,6 +48,36 @@ describe( 'SplitCellCommand', () => {
expect( command.isEnabled ).to.be.true;
} );

it( 'should be true if in an entire cell is selected', () => {
setData( model, modelTable( [
[ '00', '01' ]
] ) );

const tableSelection = editor.plugins.get( TableSelection );
const modelRoot = model.document.getRoot();
tableSelection._setCellSelection(
modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
modelRoot.getNodeByPath( [ 0, 0, 0 ] )
);

expect( command.isEnabled ).to.be.true;
} );

it( 'should be false if multiple cells are selected', () => {
setData( model, modelTable( [
[ '00', '01' ]
] ) );

const tableSelection = editor.plugins.get( TableSelection );
const modelRoot = model.document.getRoot();
tableSelection._setCellSelection(
modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
modelRoot.getNodeByPath( [ 0, 0, 1 ] )
);

expect( command.isEnabled ).to.be.false;
} );

it( 'should be false if not in cell', () => {
setData( model, '<paragraph>11[]</paragraph>' );

Expand Down

0 comments on commit 6d1d492

Please sign in to comment.