Browse Source

Fix: InsertColumnCommand should work with block content.

Maciej Gołaszewski 7 years ago
parent
commit
d1c42ccf5a

+ 5 - 3
packages/ckeditor5-table/src/commands/insertcolumncommand.js

@@ -8,7 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { getParentTable } from './utils';
+import { getParentElement, getParentTable } from './utils';
 import TableUtils from '../tableutils';
 
 /**
@@ -72,8 +72,10 @@ export default class InsertColumnCommand extends Command {
 		const selection = editor.model.document.selection;
 		const tableUtils = editor.plugins.get( TableUtils );
 
-		const table = getParentTable( selection.getFirstPosition() );
-		const tableCell = selection.getFirstPosition().parent;
+		const firstPosition = selection.getFirstPosition();
+
+		const tableCell = getParentElement( 'tableCell', firstPosition );
+		const table = tableCell.parent.parent;
 
 		const { column } = tableUtils.getCellLocation( tableCell );
 		const insertAt = this.order === 'after' ? column + 1 : column;

+ 31 - 5
packages/ckeditor5-table/tests/commands/insertcolumncommand.js

@@ -50,7 +50,9 @@ describe( 'InsertColumnCommand', () => {
 					isLimit: true
 				} );
 
-				model.schema.register( 'p', { inheritAllFrom: '$block' } );
+				schema.extend( '$block', { allowIn: 'tableCell' } );
+
+				model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 
 				// Table conversion.
 				conversion.for( 'upcast' ).add( upcastTable() );
@@ -88,7 +90,7 @@ describe( 'InsertColumnCommand', () => {
 
 		describe( 'isEnabled', () => {
 			it( 'should be false if wrong node', () => {
-				setData( model, '<p>foo[]</p>' );
+				setData( model, '<paragraph>foo[]</paragraph>' );
 				expect( command.isEnabled ).to.be.false;
 			} );
 
@@ -99,7 +101,7 @@ describe( 'InsertColumnCommand', () => {
 		} );
 
 		describe( 'execute()', () => {
-			it( 'should insert column in given table at given index', () => {
+			it( 'should insert column in given table after selection\'s column', () => {
 				setData( model, modelTable( [
 					[ '11[]', '12' ],
 					[ '21', '22' ]
@@ -113,6 +115,18 @@ describe( 'InsertColumnCommand', () => {
 				] ) );
 			} );
 
+			it( 'should insert column in given table after selection\'s column (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '11', '<paragraph>12[]</paragraph>', '13' ]
+				] ) );
+
+				command.execute();
+
+				expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+					[ '11', '<paragraph>12[]</paragraph>', '', '13' ]
+				] ) );
+			} );
+
 			it( 'should insert columns at table end', () => {
 				setData( model, modelTable( [
 					[ '11', '12' ],
@@ -200,7 +214,7 @@ describe( 'InsertColumnCommand', () => {
 
 		describe( 'isEnabled', () => {
 			it( 'should be false if wrong node', () => {
-				setData( model, '<p>foo[]</p>' );
+				setData( model, '<paragraph>foo[]</paragraph>' );
 				expect( command.isEnabled ).to.be.false;
 			} );
 
@@ -211,7 +225,7 @@ describe( 'InsertColumnCommand', () => {
 		} );
 
 		describe( 'execute()', () => {
-			it( 'should insert column in given table at given index', () => {
+			it( 'should insert column in given table before selection\'s column', () => {
 				setData( model, modelTable( [
 					[ '11', '12[]' ],
 					[ '21', '22' ]
@@ -225,6 +239,18 @@ describe( 'InsertColumnCommand', () => {
 				] ) );
 			} );
 
+			it( 'should insert column in given table before selection\'s column (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '11', '<paragraph>12[]</paragraph>', '13' ]
+				] ) );
+
+				command.execute();
+
+				expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+					[ '11', '', '<paragraph>12[]</paragraph>', '13' ]
+				] ) );
+			} );
+
 			it( 'should insert columns at the table start', () => {
 				setData( model, modelTable( [
 					[ '11', '12' ],